No. Just copy my line as posted and paste it at the start of main(). That's it.
If you want to know how it works.....
Random number generators are basically just mathematical formulas. A very simple RNG might look something like this:
|
output = (previous_output * X) + Y;
|
Where 'X' and 'Y' are constants chosen to produce "randomness" (typically they're prime numbers).
say for example you have a 32-bit RNG with the above formula, where X=69069 and Y=362437
Starting with 'previous_output = 0', that gives you the below string of "random" numbers:
(printed in hex)
000587C5
D41D4186
994CCE13
771083FC
B2EF0491
71388CE2
1657D2BF
2CCBFDB8
38FA0C1D
5EFBB3FE
...
What srand does, is it simply tells the RNG where to start. That is, it gives it the first 'previous_output' value. If you pass srand the same number, you will get the same string of random numbers every time. In order to get different strings, you need to give it a different seed. By giving it time(), we're just using the current time as the seed... since the user won't run the program more than once at the exact same time -- every time he runs it time() will be different, so you have a different seed, which starts the RNG in a different state, which produces different random numbers.
To help illustrate further, here's one way to mimic rand/srand:
1 2 3 4 5 6 7 8 9 10 11 12
|
unsigned rng_state = 0;
void srand(unsigned seed)
{
rng_state = seed;
}
unsigned rand()
{
rng_state = (rng_state * 69069) + 362437;
return rng_state;
}
|