Random Number in While Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Buckaroo Banzai
    New Member
    • Mar 2007
    • 5

    Random Number in While Loop

    Hi,
    I'm trying to generate 3 random numbers inside a while loop.
    The problem I encountered is that I always get the same 3 numbers even after I reseed the number using srand((unsigned )time(NULL));

    while (J < 3)
    {
    srand((unsigned )time(NULL)); //Seed the random number generator.
    int Rand1 = rand() % 100 + 1;

    srand((unsigned )time(NULL)); //Seed the random number generator.
    int Rand2 = rand()% 10 + 1;

    srand((unsigned )time(NULL)); //Seed the random number generator.
    int Rand3 = (rand() % 34 + 4);
    J++;
    }

    If I put a pause at the end of the while loop then it works, as the time changes,
    but I need it to be more frequent.

    any help would be greatly appreciated.

    Regards,
    BB
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    you reseed each time you call rand.....
    since the call to time returnsd a time in seconds, the processor would have to be pretty slow for this to work.
    As rand() returns a pseaudo-random STREAM of numbers (which I might use for basic applications, but wouldn't recommend using for important (especiall security critical) applications). This means you don't need to reseed it each time you use it (and reseeding will set the seed to the same number again in this case, returning the same number)....
    Try using
    [/code]
    while (J < 3)
    {
    srand((unsigned )time(NULL)); //Seed the random number generator.
    int Rand1 = rand() % 100 + 1;
    int Rand2 = rand()% 10 + 1;
    int Rand3 = (rand() % 34 + 4);
    J++;
    }
    [/code]
    instead....

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      if you are using a Pentium PC you can use the Time Stamp Counter to seed the random number generator
      http://improv.sapp.org/doc/class/SigTimer/

      code using the gcc compiler
      Code:
        // seed random number generator with Time Stamp Counter
         unsigned long long int clockCount;
         __asm__ volatile (".byte 0x0f, 0x31" : "=A" (clockCount));
        srand((unsigned)clockCount);
        int Rand1 = rand() % 100 + 1;

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by DeMan
        you reseed each time you call rand.....
        since the call to time returnsd a time in seconds, the processor would have to be pretty slow for this to work.
        As rand() returns a pseaudo-random STREAM of numbers (which I might use for basic applications, but wouldn't recommend using for important (especiall security critical) applications). This means you don't need to reseed it each time you use it (and reseeding will set the seed to the same number again in this case, returning the same number)....
        Try using
        Code:
        while (J < 3)
        {
          srand((unsigned)time(NULL)); //Seed the random number generator.
          int Rand1 = rand() % 100 + 1; 
          int Rand2 = rand()% 10 + 1; 
          int Rand3 = (rand() % 34 + 4); 
          J++;
        }
        instead....
        This code will still be inadequate, as each execution of the loop will produce the same 3 numbers every time. You should use srand ONCE in your program, at the very beginning of your main program, and then NEVER AGAIN. This will ensure that there is no resetting of the generator, and you will not get repeats in this manner (You may still get repeats of numbers because of the randomness).

        Comment

        • Buckaroo Banzai
          New Member
          • Mar 2007
          • 5

          #5
          Originally posted by Ganon11
          This code will still be inadequate, as each execution of the loop will produce the same 3 numbers every time. You should use srand ONCE in your program, at the very beginning of your main program, and then NEVER AGAIN. This will ensure that there is no resetting of the generator, and you will not get repeats in this manner (You may still get repeats of numbers because of the randomness).
          Thank you Ganon11, this did it...
          I've looked everywhere for this...
          Now I can run my loop without any pauses and it works great.

          thanks a lot..
          BB

          Comment

          Working...