Possible Duplicate:
How does rand() work? Does it have certain tendencies? Is there something better to use?
I know how to implement it. However, I would like to understand how the rand behaves internally and why is it necessary to initialise a 'seed' value for the rand function.
Alternately put — how does the rand function use the seed value to generate random numbers?
See Wikipedia for a more detailed explanation.
If you're interested in what algorithms are used to implement
rand()
in practice, What common algorithms are used for C's rand()? might be of interest. glibc's implementation is there as well as a few links to articles explaining other algorithms.As for the question of why you must set a seed, the seed is what prevents the random number generator from producing the same sequence of numbers every time. Since there are no true random number generators, if you call
srand(constant)
andrand()
5 times, the 5 "random" numbers you get back will always be the same. However, if you seed with a value that will be different each timerand()
is used (the default is the time in seconds since the Unix epoch I think), then you won't have this issue.The exact implementation details are up to the implementors. But the GNU implementation (glibc) implements rand() like that: http://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random_r.c;hb=glibc-2.15#l361
The comment explains it pretty well.
Regarding your question why you always need a seed value: There are no truly random numbers in computer science. Computers are (in computation theory) completely deterministic machines. They can't perform any operations with an outcome which is up to chance.
There are only pseudorandom number generators which generate streams of numbers which look random, but they are still the results of deterministic calculations. That's why you need a seed value: each seed results in a different sequence of numbers. When you use the same seed, you get the same sequence of pseudorandom numbers.
The behavior that a RNG always returns the same sequence when getting the same seed can be exploited: The classic space simulation Elite, for example, was able to store a huge universe with hundreds of planets in a single integer. How did it do that? The whole universe was randomly-generated. All data which was required to recreate the universe was the seed value, which always resulted in the exact same universe being generated.
Most
rand
implementations are an LCG, which uses basic math to perform the mixing. Like most PRNG's, it requires the randomized seed to partially remove its deterministic nature (this is both good and bad, depends what its intended use is) created by using fixed & predictable mathematical functions.