By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,433 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,433 IT Pros & Developers. It's quick & easy.

Random Number in While Loop

P: 5
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
Mar 10 '07 #1
Share this Question
Share on Google+
4 Replies


DeMan
100+
P: 1,806
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....
Mar 10 '07 #2

Expert 100+
P: 1,510
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
Expand|Select|Wrap|Line Numbers
  1.   // seed random number generator with Time Stamp Counter
  2.    unsigned long long int clockCount;
  3.    __asm__ volatile (".byte 0x0f, 0x31" : "=A" (clockCount));
  4.   srand((unsigned)clockCount);
  5.   int Rand1 = rand() % 100 + 1;
  6.  
Mar 10 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
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
Expand|Select|Wrap|Line Numbers
  1. while (J < 3)
  2. {
  3.   srand((unsigned)time(NULL)); //Seed the random number generator.
  4.   int Rand1 = rand() % 100 + 1; 
  5.   int Rand2 = rand()% 10 + 1; 
  6.   int Rand3 = (rand() % 34 + 4); 
  7.   J++;
  8.  
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).
Mar 10 '07 #4

P: 5
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
Mar 10 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.