More random than random()


The Arduino random() function can be used to easily generate a seemingly unpredictable series of numbers. The only problem is, from the first time it’s used in a sketch, random() actually begins spitting out the same series of seemingly random numbers. The standard solution to this conundrum is to start (or “seed”) the formula with a noisy reading taken from an unused analog pin like so: randomSeed(analogRead(0)); But for some the data returned by that little trick may still not be random enough, and that’s why created an extra randomy randomabration by the name of TrueRandom

It is hard to get a truly random number from Arduino. TrueRandom does it by setting up a noisy voltage on Analog pin 0, measuring it, and then discarding all but the least significant bit of the measured value. However, that isn’t noisy enough, so a von Neumann whitening algorithm gathers enough entropy from multiple readings to ensure a fair distribution of 1s and 0s.

And if you’re left wondering what all that random is good for, think – games, cryptography, generating serial numbers and similar. Swing by the TrueRandom code repository to download the library.

10 thoughts on “More random than random()

  1. Good info. Randomness is a fascinating topic. Also, I really, really like that image. Did you make it?

    1. Has a good introduction. It’s tougher than you think. For example, if the noise on the Arduino pin contains periodic interference, it’s no longer a good approximation of a random voltage. Knuth tells you how to analyze a RNG to see how random it really is.

Comments are closed.