
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 Tinker.it 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()”
Comments are closed.
Good info. Randomness is a fascinating topic. Also, I really, really like that image. Did you make it?
Aren’t serial numbers supposed to be… I dunno, serial? Not random?
yah, me & my old pal Adobe worked that there image up :)
oh and by the by – I’m guessing the standard issue random() uses a linear feedback shift register:
http://en.wikipedia.org/wiki/Linear_feedback_shift_register
but have yet to confirm.
eom
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.
You could use a “true” random number generator like I did on this project http://petemills.blogspot.com/2010/02/trng-rgb-lamp.html. An added advantage is that no analog inputs are used up.