

The “arc4” in the name was originally because the algorithm was “allegedly” compatible with the RC4 algorithm developed by RSA Labs. This means that, while useful for random generation, it is harder to test and less useful for dynamically generated content, distributions or modelling. Like /dev/urandom, arc4random can’t be seeded, so you can’t regenerate previous sequences. It is generally high quality but it is slow due to the use of large amounts of state, locks on the global data and periodic mixing of additional entropy from /dev/urandom. The only “general purpose” random number generator available by default in Swift is arc4random. The end result is that /dev/urandom, while useful for initializing initial seed values, is a poor choice for a general-use random number generator. In my testing, it is between 1 times slower than other generators and uses additional system resources on top of the userspace resources of typical random number generators. The problem is that reading from /dev/urandom is slow. I’ve used /dev/urandom to minimize portability problems.

The existence of two different names is largely for cross-compatibility with Linux where the different devices have historically had a complicated range of distinct security considerations with general advice leaning towards /dev/urandom.

#RANDOM NUMBER GENERATOR ALGORITHM IN C MAC#
On Mac and iOS, both /dev/random and /dev/urandom are identical and use the Yarrow algorithm in conjunction with bits accumulated from hardware entropy sources. If you need cryptographically secure random numbers, the only option you should consider is /dev/random. In iOS 10, macOS 10.12 and later, the first 3 are all “unavailable” in Swift, leaving us with just arc4random() and /dev/urandom as default sources of random numbers in Swift. The C standard library on the Mac and iOS contains a few different functions for random number generation: Built-in sources of randomness on Mac and iOS I don’t have any particular problems with it but the algorithm is nearing its 20th birthday so I was curious to see what else was around.

Historically, I’ve used a C implementation of the Mersenne Twister.
#RANDOM NUMBER GENERATOR ALGORITHM IN C CODE#
What happens if the C code does the same thing?.Other general-purpose random number generators.Built-in sources of randomness on Mac and iOS.Yes, it’s true: with a little tuning, C can be just as fast as Swift. Curious to understand what I had done, I ended up “fixing” the C version to be just as fast as the Swift version. The implementations can all seed themselves from /dev/urandom, can generate data of arbitrary size (although I’ll focus on 64-bit integer generation) and offer conversion to Double (preserving up to 52-bits of randomness in the significand).Īs an aside, my Swift implementation of the Mersenne Twister ended up 20% faster than the official mt19937-64.c implementation. Implementations will include wrappers around Mac/iOS built-in algorithms, my own implementations in Swift of some popular algorithms and some corresponding C implementations for comparison. In this article I’ll present 8 different implementations of the Swift RandomNumberGenerator protocol. What’s the best general purpose random number generating algorithm available?
