Pages: [1] 2   Go Down
Author Topic: Using analogRead() on floating pin for random number generator seeding/generator  (Read 2621 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
Nokia 6100 LCD Library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As a followup of the discussion in this thread, I decided to look at the frequency distribution of values you get by using analogRead() on a floating (unconnected) analog pin.

Background
A "good" random number generator should have a "flat" frequency distribution (equal probability of getting any number in range) - which of course is unlikely in this approach. The better approach is to "seed" the default pseudorandom generator in Arduino library - which does approximate to flat distribution - with some kind of random value - either a timer counter or as discussed in the post, analogRead() on a floating pin. The seed should be a fairly random, wide ranging number.

Test
I generated 1 million values from analogRead() on a floating pin, sent each value to my computer using serial & built a frequency distribution in excel.

Results
99.5% of the 1 million values were distributed between just 38 values out of 1024 possible values. Hmmm.... does not seem to be a wide "seed" range by any means smiley-sad

Comments / Thoughts welcome.


* analogread.png (5.51 KB, 630x490 - viewed 33 times.)
Logged

Ardgrafix6100 - A fast, full-featured Arduino graphics driver for Nokia 6100 LCDs http://code.google.com/p/ardgrafix6100/

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But what if you build your seed from the LSBs of 32 consecutive reads?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I decided to look at the frequency distribution of values you get by using analogRead() on a floating (unconnected) analog pin.

With an Uno?  Did you also test the Mega?  What about the bigger Mega?  The Teensys?  What about when the Teensy is plugged into a breadboard?  What if a neighbor pin is generating a PWM signal?  What if a neighbor pin is HIGH?  Or LOW?  What if the board is powered by USB?  What if the board is powered from VIN?  What happens when the board is placed close to a desk lamp?  Did you try the board near an OttLite?  What happens when your hand is close to the pin?  What about your mobile phone?  Did you test after running the air conditioner a bit so the air is drier?

The point is, an unconnected pin is a terrible way to generate a random number.  Even if you get good results some of the time there is no way to guarantee good results all of the time.  AWOL's suggestion helps in the case that the voltage is changing but it obviously won't help in the copious cases that the voltage is not changing or is heavily influenced by something near.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
Nokia 6100 LCD Library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But what if you build your seed from the LSBs of 32 consecutive reads?

Nice & Noisy across the spectrum! :-) i truncated the 32 bit numbers to 16 bits to generate freq dist attached.

@Coding Badly - just an evaluation of an easy alternative that's not PseudoRandom - ie not replicable if u kinda know the seed range.


* 16 bit part 1.png (7.03 KB, 652x362 - viewed 25 times.)

* 16 bit part 2.png (6.61 KB, 652x362 - viewed 24 times.)
Logged

Ardgrafix6100 - A fast, full-featured Arduino graphics driver for Nokia 6100 LCDs http://code.google.com/p/ardgrafix6100/

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Feed the data through this...
http://www.fourmilab.ch/random/
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
Nokia 6100 LCD Library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Feed the data through this...
http://www.fourmilab.ch/random/

interesting.... will try over the weekend. Thanks !
Logged

Ardgrafix6100 - A fast, full-featured Arduino graphics driver for Nokia 6100 LCDs http://code.google.com/p/ardgrafix6100/

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are entirely correct. I did extensive research on this and essentially debunked this claim made by Arduino. I did not find a stable and viable way to generate randomness on-board the Arduino.

See the full paper here: http://benedikt.sudo.is/ardrand.pdf
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5378
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Results
99.5% of the 1 million values were distributed between just 38 values out of 1024 possible values. Hmmm.... does not seem to be a wide "seed" range by any means smiley-sad

Comments / Thoughts welcome.


That's what cryptographers use hash functions for - take a sample of the bottom bits of the values and put them through a hash function (CRC32, MD5...whatever size you need).
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I did extensive research on this and essentially debunked this claim made by Arduino.

Take a look at this thread:

http://arduino.cc/forum/index.php/topic,133261.0.html

The stock analogRead() is essentially incorrectly coded.

I did a Chi-squared test on about 7k data points and it passed with flying color using the revised myanalogRead() provided in that thread. The distribution is also quite uniform.

It also talked about other ways to potentially produce truly random numbers from your arduino. The rc oscillator approach for example can be done with just a resistor + cap + onboard comparator.

The ideal is solid, except it is incorrectly implemented.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I did a Chi-squared test on about 7k data points and it passed with flying color using the revised myanalogRead() provided in that thread. The distribution is also quite uniform.

So did I.  The result was a spectacular failure.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The floating analog pin is an antenna receiving signals. The ADC converts this to 10 bits. Depending on what the analog pin receives the result will be more or less random.

What does the antenna receive? signals from all electronics in the neighbourhood and beyond ...

True randomness is very difficult to reach imho (although the frequency of this topic popping up in this forum is quite random smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Depending on what the analog pin receives the result will be more or less random.

That is true but it ignores the issue that the distribution of that randomness is not desirable - which is the point raised by the paper cited in this thread, putting aside 1) the loading by the adc module; and 2) the inherent issue with the analogRead() call, well documented in a separate thread.

Thus, the lsb approach. It actually doesn't rely on the randomness of a signal on the adc pin, but the randomness of the last digit returned by the adc process. You can tie the pin to a known voltage (Vcc or ground), yet the adc's lsb is unstable.

Obviously, adc'ng a floating pin adds more uncertainty to the process.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/forum/index.php/topic,108380.0.html
http://arduino.cc/forum/index.php/topic,66206.0.html
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thus, the lsb approach. It actually doesn't rely on the randomness of a signal on the adc pin, but the randomness of the last digit returned by the adc process. You can tie the pin to a known voltage (Vcc or ground), yet the adc's lsb is unstable.

Nope.  The technique failed again.  Analog input 1 ... failed.  Analog input 2 ... failed.  Speed up the ADC clock ... failed.  Speed it up even more ... failed.  Set it to the value used in the core ... failed.

Quote
It actually doesn't rely on the randomness of a signal on the adc pin...

It may not but it most certainly does not work the way you described.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the inherent issue with the analogRead() call, well documented in a separate thread.

Are you referring to http://arduino.cc/forum/index.php/topic,133261.0.html ?

I might pick this up again..
Logged

Pages: [1] 2   Go Up
Jump to: