Thread: Random variables

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    Random variables

    I'd really appreciate some help with this.

    I don't know how to select a variable at random and use it in a printf statement.

    The task is to design code that generates poetry, so I have a selection of adjectives, nouns, adverbs etc. stored as strings. Say I had 3 adjectives, called adj1, adj2, adj3, I want one of these to be randomly selected and printed to the console.

    Thanks.

  2. #2
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    I don't understand how you wanna pull of a poetry generator, but here's how you generate a rand()m number:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
      int a;
    
    
      srand(time(NULL));
    /*the point of seeding is that there's a NEW number generated each
    time you start the program*/
    
      a = rand();
    
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Instead of three variables like that, store them in an array:
    Code:
    char *Adjectives[3];
    Then generate a random integer between 0 and 2 (inclusive), which information can be found on in the FAQ (The link is near the top of any page, beside "User CP"). Then use that ranom number as the index for the array and pick your random Adjective.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    char *adjectives[3] = { "fast", "slow", "green" };
    char *nouns[3] = { "fred", "arthur", "jim" };
    printf( "%s %s\n", nouns[rand()%3], adjectives[rand()%3] );

  5. #5
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    It'll probably be easier if you put the strings in an array rather than separate variables. Then you can use a random index, which is far easier to figure out than picking a random variable:
    Code:
    puts ( words[(int)( rand() * ( 1.0 / ( RAND_MAX + 1.0 ) ) * N )] );
    Last edited by Slacker; 01-06-2006 at 08:16 AM.

  6. #6
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    why doesn't it make a difference if I declare

    char array[2];

    or

    char *array[2];

    ?

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    It's a huge difference.

    One's an array of characters.
    The other's an array of pointers to characters.

    An array of characters (in this case of size 2) can hold:
    Code:
    char array[2] = {'A', 'B'};
    An array of pointers to characters can hold:
    Code:
    char *array[2] = {"My First String", "My Second String"};
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  8. #8
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by Epo
    It's a huge difference.

    One's an array of characters.
    The other's an array of pointers to characters.

    An array of characters (in this case of size 2) can hold:
    Code:
    char array[2] = {'A', 'B'};
    An array of pointers to characters can hold:
    Code:
    char *array[2] = {"My First String", "My Second String"};

    thx for clearing up that one. quite important indeed

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Epo
    It's a huge difference.

    One's an array of characters.
    The other's an array of pointers to characters.
    Indeed


    An array of characters (in this case of size 2) can hold:
    Code:
    char array[2] = {'A', 'B'};
    An array of pointers to characters can hold:
    Code:
    char *array[2] = {"My First String", "My Second String"};
    I feel the need to elaborate on the above. An array of characters can also be initialised like so:
    Code:
    /* This is an array of 10 characters that is initialised with
    the null terminated character array of "hello", it can be modified */
    char array[10] = "hello";
    
    /* This is an array of 6 characters, automatically made just large
    enough to fit the null terminated character array of "world", it too
    of course can be modified */
    char array[] = "world";
    
    /* This is an array of two pointers to char that are initialised to point to a pair
    of string literals "hello" and "world".  These strings cannot be modified, but
    the pointers could be made to later point to something else.  They do not "hold" 
    the strings, just point to them. */
    char *array[2] = {"hello", "world"};

  10. #10
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by cwr
    Indeed




    I feel the need to elaborate on the above. An array of characters can also be initialised like so:
    Code:
    /* This is an array of 10 characters that is initialised with
    the null terminated character array of "hello", it can be modified */
    char array[10] = "hello";
    
    /* This is an array of 6 characters, automatically made just large
    enough to fit the null terminated character array of "world", it too
    of course can be modified */
    char array[] = "world";
    
    /* This is an array of two pointers to char that are initialised to point to a pair
    of string literals "hello" and "world".  These strings cannot be modified, but
    the pointers could be made to later point to something else.  They do not "hold" 
    the strings, just point to them. */
    char *array[2] = {"hello", "world"};

    when you say can't be modified on the last one, you can still write to those positions. So where's hte problem? What do you mean with "can be modified"...

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    String literals are read-only.
    Code:
    char *pointer = "hippo";
    char array[] = "hippo";
    /* pointer[1] = 'e';  /* not allowed */
    array[1] = 'e';  /* allowed */
    Some compilers will let you modify string literals (like yours, evidently), but they are supposed to be read only, and on some compilers they are. Just treat them as such, to be safe.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Ah, thanks for pointing that out. I wasn't aware, but it sure would explain some of the troubles I've run across in the past.

    Here's how to NOT declare an array of modifiable "strings":
    Code:
    char MyArray[][/*Compiler Expects Dimension Here*/] = {"Hi", "Howdy", "Hello", "Umbrella"};
    I guess then, an array of modifiable "strings" *could* be declared as:
    Code:
    char MyArray[][9] = {"Hi", "Howdy", "Hello", "Umbrella"};
    My compiler didn't complain about that. Of course, there are many times it doesnt' complain when it should. Apparently the second dimension must be initialized with the length of (your longest element + 1), plus one because there needs to be room for the '\0' null-terminated character. Is it safe to assume that the first dimension would be automatically sized based on how many elements are created?
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is it safe to assume that the first dimension would be automatically sized based on how many elements are created?
    Yes, just like this:
    Code:
    int array[] = {1, 2, 3};
    char carray[] = "hello";
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I guess then, an array of modifiable "strings" *could* be declared as:
    Code:
    char MyArray[][9] = {"Hi", "Howdy", "Hello", "Umbrella"};
    My compiler didn't complain about that. Of course, there are many times it doesnt' complain when it should.
    It shouldn't complain about that. That's how you do it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69

    Thumbs up

    that looks quite usefull for when you're working with structs. nice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM