views:

2339

answers:

2

There's a lot of conflicting information about this topic. So let's try to agree on a definitive answer:

Which one of these random number generator in C create better randomness: rand, random or arc4random?

note: Just to make the question clear, this is not a question about true randomness, it's only a clash between those 3.


As pointed out, this question doesn't make much sense, as this is not about C, but about a specific implementation, in my case, cocoa (more specifically the iphone sdk, but my guess is they are the same as far as these functions go). Still, there's some useful information here. I concluded by implementing arc4random, mostly because of its ease of use (no seeding needed), which is an important factor that no one pointed out.

I'm closing the question, and adding the cocoa tag for cocoa developers looking for information on RNGs. Many thanks for those who contributed, and sorry for the confusion.

+4  A: 

The implementation of rand() is not specified by the C standard, however most compilers use a linear congruential generator. random() and arc4random() aren't standard C either, but they are better than the usual implementation of rand()

I'd say: arc4random() is better than random() is better than rand()

rand() is really terrible. You could easily do better than all three however.

It also depends what you want the random numbers for. A good random number generator for encryption/security may not be a good random number generator for simulation and vice-versa.

FigBug
+10  A: 

Of these functions, only rand is part of standard C. random is part of POSIX, and arc4random is only provided in BSD (and derived). So only rand is "in C".

For rand, the C standard says nothing about the quality of the generator, i.e. returning always the same number would be conforming. It says that the number must be between 0 and RAND_MAX. The value of RAND_MAX, and the precise algorithm being used, are implementation defined (although RAND_MAX must be at least 32767).

For random, POSIX specifies that it must have a period of atleast 2^31 by default, and, if initstate is called with 256 bytes of state, then it must have a period of atleast 2^69; other details are again implementation-defined.

For arc4random, the specific implementation is part of its definition (RC4). It's specified that it gives 2^32 different values; I could not find anything about its period.

To compare them in detail, one would have to know what specific implementation you refer to.

Martin v. Löwis
hi martin, in my case the implementation is the iphone SDK. Is it really important for this question? Should I edit? I'd be better if I could avoid changing the scope of the question.
Steph Thirion
If the iphone SDK is similar to OSX, then the rand() implementation is from FreeBSD (rand.c 1.15). The algorithm then is from Parker and Miller, "Random number generators: good ones are hard to find". random then uses the very same algorithm if initstate's n is < 8; rand already is a good algorithm
Martin v. Löwis
Is the arc4random algorithm the same in all implementations? I read here it's the best in cocoa: http://iphonedevelopment.blogspot.com/2008/10/random-thoughts-rand-vs-arc4random.html . How does it compare in general with the other 2, in most implementations? Maybe we can reach some consensus.
Steph Thirion
I think the blog article is misguided. It claims that because arc4random produces 32bits instead of 31, it gives "much more precision" - but actually, it only gives one additional bit. In a typical application, you don't even need 31 bits per call, but much less. Both rand and arc4random are decent.
Martin v. Löwis