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.