## Generating Random Numbers with Arduino

I am working on a project that uses the lolshield and Arduino. A part of this application relies on random number generation. I found the Arduino reference for random numbers to be lacking. While the information is all accurate, it is so sparse that it is misleading to anyone without a programming background. In other words, if you’ve already performed programming tasks that use random numbers, the Arduino reference pages for either randomSeed() or random() are sufficient to quickly answer all of your concerns.

Beginners, if you model your random number generation code based on the examples provided on the randomSeed() or random() reference pages, you will be OK. I’d like to fill in the info that seems to be missing from the Language Reference.

The Arduino, or any computer for that matter, is precise and logical; it is not random at all. Computers make use of pseudo-random number generators. This is a complicated mathematical function that outputs a sequence of numbers that do indeed pass a series of tests for randomness. However, pseudo random numbers eventually repeat. This is not a problem for most games and other uses of random numbers, but it is not sufficient for certain applications like cryptography or high-stakes gambling.

The randomSeed() page starts out ok. “*randomSeed() initializes the pseudo-random number generator*” . For pedestrian applications you’ll only need to do this once. Call randomSeed() from the setup() function. There, it’s done. Well, not so fast.

So randomSeed() starts the pseudo-random number generator at some point in the number sequence and it chugs through the predetermined sequence of random-looking numbers each time the random() function is called.

If you start the pseudo-random number generator with something like randomSeed(8) each time you call the random() function, you’ll get another number in a sequence of random-looking numbers, but every time you run your program, you’ll get the same numbers in the same sequence. Modify the program on the randomSeed() page and try it. This method of using randomSeed() for a predetermined random number output is very useful for software debugging.

The language reference kindly tells us to *“use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin…”* I think I’d need an oscilloscope for a better peek into this, but I’m doing exactly this in my sketch, for a while the analog inputs seem stuck near 0 volts and the first number output from random() was always toward the low side of my range of allowed values. I suspect that using AnalogRead() may sometimes be a shaky way to do things, but my random number generator now seems to be operating good enough for my purposes. So how to other computers get their random seed?

Quite simply, to generate a random number a computer needs a random input. There is no way around it. Getting a good random input can be expensive and all hardware-based random numbers are slow, Thus, the use of pseudo-random number software; it’s fast and good enough for most applications. Wikipedia offers a no-nonsense definition of electrical noise as “*random fluctuation in an electrical signal*”. Usually noise is the undesirable portion of an electrical signal. In the case of a random number generator, the noise is the signal and its unpredictability is its value. This noise signal may come from one of many sources. Commonplace sources of noise signals are, diode junctions, transistor junctions, measuring atmospheric noise through a radio antenna, measuring nuclear decay. Or, keeping things digital, some random number generator sample the lowest significant bits of a large incrementing number, the theory being that the least significant digits of a large changing number are unpredictable.

I am generalizing, so I’m sure there will be certain obscure instances where my advice won’t work. However, I hope this gives you better insight into randomSeed() and random() in Arduino.

[...] http://circuitfun.wordpress.com/2011/01/27/generating-random-numbers-with-arduino/ [...]

Queue Theory and Random Number Generation | ceng314station said this on February 24, 2013 at 3:27 pm |