tags:

views:

38

answers:

1
+1  A: 

Java is just as high level as Objecive C here - in this case Java' Random.getInt() is the same as arc4random in that they both return a 32-bit pseudo-random number.

The issue raised in the URL (and I have seen elsewhere) is that rand()

could be repeating itself every 32768 values.

Whilst OSX's arc4random could have (2**1700) states.

But as in all uses of pseudo-random generators you need to be aware of their weaknesses before using them e.g. a preference for low bits in some generators and also the comment in the OpenBSD arc4random man page where it says

arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.

Mark
I'm not too worried about the value not being random enough, but I am worried about a non-uniform distribution. I do not have arc4random_uniform in Objective-C (that I know of). What should I use instead?
Yar
arc4random() % upper_bound is probably OK or get the code from OpenBSD (or use a generator from Boost)
Mark
Oh, I get it now. "For linear congruential random number generators, which rand() often is, the lower bytes are much less random than the higher bytes." So if arc4random doesn't have this problem, then the modulus is no problem. That's cool.
Yar