More random than random()

Arduino
More random than random()

arduinoRandom_cc.jpg

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()

  1. Sean Michael Ragan says:

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

  2. BobsYourUncle says:

    Aren’t serial numbers supposed to be… I dunno, serial? Not random?

  3. Collin Cunningham says:

    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

    1. Peter says:

      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.

  4. pete says:

    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.

Comments are closed.

Discuss this article with the rest of the community on our Discord server!

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK