This code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
printf ("First number: %d\n", rand() % 100);
srand ( time(NULL) );
printf ("Random number: %d\n", rand() % 100);
srand ( 1 );
printf ("Again the first number: %d\n", rand() %100);
return 0;
}
has the following output:
First number: 41
Random number: 13
Again the first number: 41
There is also the following rule:
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.
I understand the words but I just don't understand the method itself. Why did it return 41 again? Is it random or must it return the same result in every case according to this code?
srand(3)
lets you get repeatable output from the random number generator. This way, you can write programs that depend on random numbers, but test them deterministically. (Typically by having a command-line option to callsrand()
with a supplied argument, so it could be run a hundred times with 1, 2, 3, 4, 5, ... as inputs.)So this is behaving as expected.
If you call
rand()
without first callingsrand()
, the behavior is as if you calledsrand()
with1
as the argument.This behavior is defined in the original C standard. I don't have a full copy of it handy, but The Open Group's POSIX standards are the next best thing, as they incorporate the full C standard (with some extensions):
http://www.opengroup.org/onlinepubs/000095399/functions/rand.html
The actual result of a
rand()
call for any given seed is implementation-defined, except that for any seed x, call n torand()
after the seeding of the generator must return the same result. So while on your implementation you will always get 41 on the firstrand()
call after callingsrand(1)
, and you will always get the same result (whatever it is) for the secondrand()
call, other implementations may use a different algorithm that produces different results.The C++ standard defers to the C standard for
rand
andsrand
(see section 26.8 of C++0x):The C standard (7.20.2.2 of C99) states quite categorically:
So the first time you call
rand
, the seed is 1. It's also 1 the third time you call it which is why you get the same value.The second time that you called
rand
, the seed had been set based on the current time, which is why you got a different result. It's usually not a good idea to re-seed the random number generator unless you actually want the same sequence (such as in testing).I think this one may be a problem:
If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
srand reference
It's probably because in your initial call to rand the random number generator has not been seeded so it uses a default value of 1 to seed it.
Calling
srand
with a value of1
resets the generator to its initial state when the program started.