|
-
April 2nd, 2012, 04:07 PM
#5
Re: Simulation: Tortoise & Hare race (srand question)
 Originally Posted by VeNiX
That's not the issue, it's when the program terminates, then I relaunch it again... I get maybe 3-4 repeated sequences if I place srand() inside the functions.
As an experiment you can declare a global variable like say,
unsigned seed = 0;
Then instead of
srand(time(0));
you call
srand(seed++);
in the functions.
Now it appears to be working and in a sense it does. What happens is that you start a new random sequence (the random sequence associated with the current value of the seed variable) before any call to rand() and rand() will always return the first number of that just started sequence.
So now you get a sequence of numbers that are seemingly random, but are they? Not necessarily because you're using the random number generator in a way it wasn't designed for. In order not to abuse the random number generator and risk a low-quality sequence, seed it once at the beginning of the program and then draw numbers from that one sequence throughout the whole of the program.
Last edited by nuzzle; April 2nd, 2012 at 04:15 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|