I understand that the C specification does not give any specification about the specific implementation of rand()
. What different algorithms are commonly used on different major platforms? How do they differ?
views:
1078answers:
5See this article: http://en.wikipedia.org/wiki/List_of_random_number_generators
This is the source code of glibc's rand(): http://qa.coreboot.org/docs/libpayload/rand_8c_source.html
As you can see, it's a simple multiply with an addition and a shift. The values are carefully chosen to make sure that you get no repeat of the output for RAND_MAX iterations.
You could use Boost Random library for different random number generators, if you need something specific, or more advanced.
The documentation of Boost Random is here.
@Aaron I can not comment but RAND_MAX is the highest possible value returned, not the number of iterations before repeating.
The field of PRNGs (Pseudo Random Number Generators) is quite vast.
First of all you have to understand that without having an external input (usually physical) you can't get a real source of random numbers.. That's why these algorithms are called pseudo random: they usually use a seed to initialize a position in a very long sequence that seems random but it's not random at all.
One of the simplest algorithms is the Linear Congruential Generator (LCG), that has some costraints to guarantee a long sequence and it's not secure at all.
Another funny one (at least for the name) is the Blum Blum Shub Generator (BBS) that is unusual for normal PRNGs because it relies on exponentiation in modulo arithmetic giving a security comparable to other algorithms like RSA and El Gamal in breaking the sequence (also if I'm not sure about the proof of it)