Go Down

Topic: Random number seed (Read 1 time) previous topic - next topic


Any recommendations as to what can be used for seeding the pRNG ? Is there any semi-random value that can be used after startup? I was thinking about using analogRead from some unconnected port but I don't know if that value is going to be sufficiently random every time I start the chip.


Feb 23, 2009, 11:28 pm Last Edit: Feb 23, 2009, 11:29 pm by BroHogan Reason: 1
Using analogRead from an unconnected port is commonly done. I don't know for sure how effective it is (always been a bit curious) but it's commonly used, and easy to test how effective it is by printing it.
"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll


I've used liberlab to monitor 4 channels of analog inputs from the Arduino. I would say it is not a great source for random seed as while there is noise all the disconnected analog values are under 5% or so of full scale. If there is a valid analog voltage wired to any one of the pins the other pins tend to raise somewhat, most likely from small amount of capacitance coupling between the floating inputs and the valid wired input. So maybe of some value for a seed value but I would look for something better. Isn't there some free running counter register that one could read instead?



I've read somewhere something about big capacitors being used for the electrical noise they make.

Correct me if I'm wrong : white noise is a true random noise because each frequency of the spectrum is equally present. so, a white noise generator would be a good source of random number, wouldn't it ?

Here's what I found :

And an interesting source for randomness :


IF -- and that's a capital IF -- your application receives input at an unpredictable amount of time after it starts up, then millis() can be an excellent seed for random().  For example, I have a little toy I made for my son.  To play, he plugs it in, and anytime after the several seconds of startup stuff is finished, he presses a button indicating he wants to play.  I use the value of millis() to seed the random() at that time.

The advantage is that the range of possible seed values is several thousand (or possibly much more depending on when he pushes the button) compared to the maximum of 1024 that you could get in theory -- in practice much less -- from analogRead.  The microsecond counter might be much better.

However, this technique might not be appropriate for your application.  It certainly would NOT be appropriate to use millis() at some fixed point in your setup() function.

I've often thought that analogReading two different pins and combining their values might be an improvement, but I have never tried this:

Code: [Select]
long seed = analogRead(pin1) + 1024 * analogRead(pin2);


Go Up