rand() function

This is a discussion on rand() function within the C Programming forums, part of the General Programming Boards category; I need to do HW assignment that creates a sentence. I need to use random generation. I understand it using ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    rand() function

    I need to do HW assignment that creates a sentence. I need to use random generation. I understand it using numbers but I am clueless when it comes to something like this. Here's my portion of code I have so far:

    Code:
    /* Joseph Duke fig 8.11 p4.c
         Using random generation to create sentence */
    
    
    #include<stdlib.h>
    #include<stdio.h>
    #include <string.h>
    #include <time.h>
    
    #define SIZE 5
    int main()
    {
       char *article[5][20] = {"the", "a", "one", "some", "any"};
       char *noun[5][20] = {"boy", "girl", "dog", "town", "car"};
       char *verb[5][20] = {"drove", "jumped", "ran", "walked", "skipped"};
     
       char *prep[5][20] = {"to", "from", "over", "under", "on"};
    
       char *sentence[] = {0};          /* sentence the words form */
       char *word;
       int i;                              /* for loop variable */
    
       srand(time(NULL));
    
       for (i=0; i < SIZE; i++)
       {
    
    /* joins and prints the sentence  */
    
           article = rand();
    I know I am way off base here but I don't know how to use rand() function in this case. Can any one give me any hints? Thanks

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    you need rand() to make a number between 0 and 4 based on your arrays. I think you do it like int random = srand(seed)%4; I can't remember exactly but that should put out the correct random numbers. Then just pop those numbers into your array indexes and there you go. Since this is HW I didn't go in great detail but that should get you on the right track. May I ask why you're making a [5][20] array? a simple [5] will do.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Thanks for the quick response. I appreciate you getting me started. This gives me an idea as to what I am trying to do. As far as the [5][20], I thought I saw that in an example in the book or on the net and since I couldn't figure this out I was trying things out. Since I don't need it, I will take it out. Thanks again.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by durban
    you need rand() to make a number between 0 and 4 based on your arrays. I think you do it like int random = srand(seed)%4; I can't remember exactly but that should put out the correct random numbers. Then just pop those numbers into your array indexes and there you go. Since this is HW I didn't go in great detail but that should get you on the right track. May I ask why you're making a [5][20] array? a simple [5] will do.
    Use rand() to get random numbers.

    rand() returns a random number 0 to RAND_MAX.
    srand() seeds the random number generator, and is usually called once before any calls to rand().

    You should read:
    Generating Random Numbers
    Prelude's 'Using rand()', if you want to know lots about rand().

    Also, he should be using:
    Code:
    char article[5][20] = {"the", "...
    A simple [5] will not do, as he has an array of strings. Strings are arrays of char's. So he has an array of an array of char's. He doesn't need the * at the front though. (Gcc even gives me warnings.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Thanks for your input. I know I read somewhere about using the [5][20]. I will read that link. Thanks again.

    I just clicked on it. I thought I looked at the FAQ for this. Well, maybe it was for one of my other 4 projects that are due. Thanks.

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by Cactus_Hugger
    Also, he should be using:
    Code:
    char article[5][20] = {"the", "...
    A simple [5] will not do, as he has an array of strings. Strings are arrays of char's. So he has an array of an array of char's. He doesn't need the * at the front though. (Gcc even gives me warnings.)
    A simple [5] will do, as you know a char* is a pointer to a dynamically allocated charset. If I did...

    Code:
    char* sentence = "Cactus_Hugger hugs cactuses";
    cout >> sentence;
    The output would be : Cactus_Hugger hugs cactuses. This proves that an array of char*'s would actually be an array of strings. This is not only easier but introdudes the concept of using pointers which he will use ALOT. Just proving my case, you can use whichever is easier for you jduke.

    also, srand() is ALOT more efficient for generating random numbers because the number will not be the same everytime due to the seed like cactus said. I recommend it.
    Last edited by durban; 10-04-2005 at 09:06 PM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> is the overloaded extraction (input) operator of C++. srand() merely seeds the random number generator rand(). This data is not dynamically allocated. No cases were proved. Something silly like this might be of help:

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define SIZE 5
    int main(int argc, char *argv[])
    {
    	char *article[SIZE] = {"the", "a", "one", "some", "any"};
    	char *noun[SIZE] = {"boy", "girl", "dog", "town", "car"};
    	char *verb[SIZE] = {"drove", "jumped", "ran", "walked", "skipped"};
    	char *prep[SIZE] = {"to", "from", "over", "under", "on"};
    
    	char sentence[200];
    
    	srand(time(0));
    	sprintf(sentence, "%s %s %s %s %s %s", 
    			article[rand() % SIZE], 
    			noun[rand() % SIZE],
    			verb[rand() % SIZE],
    			prep[rand() % SIZE],
    			article[rand() % SIZE], 
    			noun[rand() % SIZE]);
    
    	printf("%s\n", sentence);
    	return 0;
    }
    Edit: Minor fix to the code
    Last edited by Tonto; 10-05-2005 at 01:51 AM.

  8. #8
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by Tonto
    >> is the overloaded extraction (input) operator of C++. srand() merely seeds the random number generator rand(). This data is not dynamically allocated. No cases were proved. Something silly like this might be of help:
    Excuse my operator, I no longer program console based applications so my operator was wrong . char* sets a pointer to the 0 index of a char array that has no set maximum value, so it is dynamic or constantly changing. whatever, it worked man(you're using it) so I did prove my case. And you just did the man's homework for him...Cheers to you!
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  9. #9
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by durban
    whatever, it worked man(you're using it) so I did prove my case.
    You did indeed. I though when you said 'a simple [5]' will do, you meant a 'char verb[5]'. I understand now, and it does make sense. (Although it's never occurred to my wee mind that it could be used this way, even though I repeatedly use char *argv[]... This will make my programming in the future easier -- thanks. Guess more than one person learned something in this thread. :-)

    Sorry for the confusion!
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    much appreciated!

    I had 4 other programs to write so I left this for a couple days and noticed the other replies. Thanks guys for your help. I have been racking my brain trying to figure this out. I had part of it just couldn't put the pieces together. Even though Tonto did do that portion of the homework for me, I have alot more to do with it. I really appreciate it. This gave me a great start. Thanks again.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21