views:

62

answers:

1

Hi, What is better? I have the prioritized in the order of "goodness", am I correct?

  1. arc4random
  2. randmom
  3. rand

1 - best, 3 - worst

I need a really good randmon number gen for a lowe number (< 50), so I using % 50 to obtain numbers below 50.

Thanks

A: 

I'm pretty sure randMOM is the one to use.

Oh, well, first google hit.

If you need a number in the range [n, m), the very best approach is to use a function explicitly designed to do so. Both scaling (from say [0, 1)) and applying a modulus may slightly alter the distribution. Of course, such skewing may be entirely irrelevant (and for most applications is probably only a theoretical consideration) -- especially if only a basic PRNG is employed.

pst
Hmm, arc4random gives much bigger distribution, hence is this sense it is better. From the docs:DescriptionThe arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and random(3) .
leon
@leon ...if it even matters
pst