5

What's the difference between arc4random and random?

dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260

2 Answers2

9

random is known to be predictable and thus not to be used for secure pseudo-random number generation. arc4random is a newer, less predictable pseudo-random number generator. If you are not using it for anything secure, random will work fine.

coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • 4
    `random` is also significantly faster than `arc4random`, which is why you'd ever use it at all. – Colin Gislason May 08 '10 at 13:25
  • 2
    @Colin Cislason: How much faster? Does it make any difference if you aren't creating 1000nds of random numbers? – Emil May 08 '10 at 13:57
  • @Emil It may not have that much effect. I haven't run any tests myself. See the following link for a test, but this compares with `rand` and uses many iterations: http://www.indieappsalliance.org/forum/viewtopic.php?f=10&t=13 – Colin Gislason May 08 '10 at 19:20
0

Depending on your system, random() may or may not be the old, reliable, and predictable UNIX random(). On modern FreeBSD systems, and Linux 2.6 systems that have the /dev/random device, the default random() implementation reads from /dev/random until it is exhausted. This is likely to produce somewhat better random numbers than any numerical sequence generator, depending on the sources of randomness you've stirred into /dev/random.

Consulting the documentation (man pages, etc) for the system you are deploying the program on is highly suggested.

Wexxor
  • 1,919
  • 1
  • 15
  • 17
  • Really? -1? You really *should* go read the effing man pages before hitting that down arrow. – Wexxor May 23 '12 at 03:28
  • 3
    I can't speak for the user voting you down, but the question is tagged 'iphone'. An answer starting with "Depending on your system" and mentioning Linux might not be an appropriate answer. – user371320 Jun 09 '12 at 20:18