views:

1078

answers:

5

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?

+7  A: 

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

Aaron Digulla
That's it? Surprisingly simple. Thanks muchly.
Azrael
Yes, that's it, most general purpose PNRGs are surprisingly simple to implement. If you're looking for something more complex try the Mersenne Twister.
Jasper Bekkers
Even MT19937 isn't exactly hard to implement.
Joey
via @Tiemen below: @Aaron I can not comment but RAND_MAX is the highest possible value returned, not the number of iterations before repeating.
Jason Watkins
@Jason: Any link for that? I was under the impression that there won't be a repeat for RAND_MAX calls (unless you change the seed, of course)
Aaron Digulla
@Aaron: If that were the case, it wouldn't be a very good random number generator. What you'd have in that case is sampling without replacement.
Mike Daniels
@Mike: rand() is not random but pseudo random, so the numbers aren't really random at all. IIRC, the values of the standard rand() are chosen to yield the maximum of iterations before a repeat.
Aaron Digulla
Your http://qa.coreboot.org/docs/libpayload/rand_8c-source.html link doesn't work, you probably meant http://qa.coreboot.org/docs/libpayload/rand_8c_source.html .
Adrian
@Adrian: Thanks, fixed!
Aaron Digulla
+2  A: 

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.

Cătălin Pitiș
A: 

@Aaron I can not comment but RAND_MAX is the highest possible value returned, not the number of iterations before repeating.

Tiemen
Yes, but I think he qualified his statement in a way that it's mostly true: The values are typically carefully chosen so that you don't get a repeat for (at most) RAND_MAX iterations.Sometimes, the cycles are smaller.IIRC, the rule of thumb is that RAND_MAX is ought to be co-prime with the other constants.
hythlodayr
If I recall correctly, r=r*5+1 is such a beast. It cycles through all possible bit combinations, no matter how many bits you choose.
Nosredna
+1  A: 

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)

Jack
+1  A: 
eloj