Pages: [1] 2 3 ... 6   Go Down
Author Topic: Random Seeds and Random Numbers  (Read 8030 times)
0 Members and 1 Guest are viewing this topic.
Connecticut
Offline Offline
Edison Member
*
Karma: 16
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the best (read: less likely to repeat over time) random-number generator that I can get on my arduino? I have a project in which I want the "random" start conditions to be different every time. analogRead()ing a floating pin seems to be the accepted practice, however, these values often repeat: I took about 20,000 samples on an unconnected pin; all of them fell between 300 and 550. Would digitalRead()ing an unconnected pin be better? Also: I don't really care about time as long as the computation doesn't take some hideously long amount of time. Under a second or so is fine.


Thanks a lot!

baum
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Would digitalRead()ing an unconnected pin be better?
What range of values would you expect?
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just reading an analog input won't do it. We've talked about accumulating the LSB of consecutive analogRead()s in the past. That works OK but still has clumping of values.

Have a search for this on the forum, there are a couple of mega threads about it and several suggested algorithms.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've ran across tinkerit's TrueRandom library.  Maybe that will give you better results?
http://code.google.com/p/tinkerit/wiki/TrueRandom

Disclaimer: I am in no way, shape, or form, neither physically, nor mentally, related, linked, or otherwise part of the tinkerit team.  I just happen to stumble across that library one day.  I have not had a chance to test it out either.
Logged

Connecticut
Offline Offline
Edison Member
*
Karma: 16
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Quote
Would digitalRead()ing an unconnected pin be better?
What range of values would you expect?

Assume I need an int: then I would get 16 readings and combine them. Also, a binary method may be more random as a floating analog pin may have a reading of, say, 500, no such value exists in the digital realm, hopefully causing the random-ish-ness that we would like.


I looked at TrueRandom, but it uses analog readings...

baum
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have a look at this thread

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

Plenty of info there about using analog and other methods.

Quote
then I would get 16 readings and combine them
Better off taking a lot more and using a "whitening" technique to smooth out the runs of 1s and 0s.

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Random number generators aren't.

You could try using a Geiger counter and radiation source like maybe from a smoke detector.

Logged

Examples can be found in your IDE.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep. Bottom line it that without an external entropy source there appears to be no way to get true random on an Arduino.

For many applications the "accumulating the LSB of several analogReads" does pretty well, it depends on just how important it is to have true random.
______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Switzerland
Offline Offline
Sr. Member
****
Karma: 6
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If avoiding repetition is your main concern, the easiest thing to do is to store a random seed in eeprom.  The logic goes something like this:

  • Startup
  • Read number from eeprom and use it to seed the random() function
  • Use random() to generate a new seed for next time
  • Check that the new seed is not the same as the previous, and save it in eeprom
  • Run the rest of the program
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That is still not random, the pattern is totally predictable and will be the same for every Arduino running the code, unless you seed the EEPROM with a genuine random number, maybe from the PC when you burn the chip.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Connecticut
Offline Offline
Edison Member
*
Karma: 16
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hold on.... Would this be of any use?

http://hackaday.com/2011/09/02/intels-new-way-of-creating-randomness-from-digital-orderliness/

Or what if I tied one inverter's input to its output? ----> high freq. oscillation!!!!

I was thinking of the eeprom, but, as Rob pointed out, it will repeat across many arduinos.

Any ideas?

baum
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've ran across tinkerit's TrueRandom library.  Maybe that will give you better results?

No.  It has at least two serious flaws.

This is reliable...
http://arduino.cc/forum/index.php/topic,66206.msg537783.html#msg537783

Store an initial random seed in the EEPROM.  The following are a good source for the initial seed(s)...

http://www.random.org/
http://www.fourmilab.ch/hotbits/
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hold on.... Would this be of any use?

http://hackaday.com/2011/09/02/intels-new-way-of-creating-randomness-from-digital-orderliness/

Or what if I tied one inverter's input to its output? ----> high freq. oscillation!!!!

I was thinking of the eeprom, but, as Rob pointed out, it will repeat across many arduinos.

Any ideas?

baum

If the running sketch allows then new value(s) could be stored to EEPROM. Use the low-order digits from the stock market report for instance and maybe have the PC communicating the new data modify those using date-time digits if the one isn't enough. OTOH without a PC, waving the thing around to gather light level data combined with capacitance or sound readings might do.

Logged

Examples can be found in your IDE.

Connecticut
Offline Offline
Edison Member
*
Karma: 16
Posts: 1216
RTFD (Datasheet in our case)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But couldn't a high-freq oscillating inverter be read as a random-ish source? Not too much hardware/software needed. (propogation time of the inverter: ~10ns on 7404, so ~100MHz)

baum
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Heh guys;

I read in one of my electronics magazines is to use a transistor circuit which is a "noise generator" transistor circuit. So connect the output to one of the analog input pins, you may have the choice of reading between 0 to 1024 ( between 0 to 5V ).

To bad I can not find that schematic.

My idea...
Logged

Pages: [1] 2 3 ... 6   Go Up
Jump to: