Thread: Generating a random number?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    Generating a random number?

    I've followed the random number generating tutorial, but while I'm in a loop, it uses the same generated number. Say for example, I run the program and the random number is 3, then for each loop, the random number will be 3 until the loop ends, I would like the random number to change each time the loop runs.

    How do I do this?

    Thanks


    Dave

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Seed the random number generator only once at the beginning. srand() should not be called within the loop.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by swoopy View Post
    Seed the random number generator only once at the beginning. srand() should not be called within the loop.
    Even better, don't seed the generator at all. This makes debugging your program easier, since you get the exact same sequence of random numbers each time.

    It is infuriating when your program crashes, and you cannot reproduce the crash because it depended on some particular sequence of random numbers.

    Once you are confident that the code is working, THEN add a call to srand() to seed the generator. Also, print out the value of the seed, so that if you do get a crash, you can at least re-seed the generator with the appropriate value.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    True, I've commented the seeding during the debugging process until the code appears to be working. If the program is a simulation it's particularly useful.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The alternative, for use when debugging, is to record the seed used.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by grumpy View Post
    The alternative, for use when debugging, is to record the seed used.
    A very useful bit of code:

    Code:
    char *seed;
    if((seed = getenv("RANDOM_SEED") != NULL)
    {
        /* Seed the random generator */
        srand(atoi(seed));
    }
    This way, you can specify the seed to use via the environment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 07:34 AM
  2. Random Number: Mersenne Twister Source Needed
    By mercury529 in forum C++ Programming
    Replies: 12
    Last Post: 11-26-2006, 11:40 AM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM