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?
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.