Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« on: November 17, 2012, 07:31:04 pm » |
Off-topic posts split from... http://arduino.cc/forum/index.php/topic,132949.0.htmlSeed the random number generator with your adc: adc a spare pin and get its lowest bit for as many times as you need - 8 times if your seed is an 8-bit type and 32-times if your seed is a 32-bit type.
|
|
|
|
« Last Edit: November 19, 2012, 09:44:16 pm by Coding Badly »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #1 on: November 17, 2012, 07:32:48 pm » |
I'm not sure if I completely understand what you are saying... I'm a real newbie w/ the arduino IDE.
|
|
|
|
|
Logged
|
|
|
|
|
Anaheim CA.
Offline
Edison Member
Karma: 31
Posts: 2311
Experienced old Whitebeard with a Full head of Hair...
|
 |
« Reply #2 on: November 17, 2012, 07:39:43 pm » |
He doesn't either or he would have offered his opinion... FWIW.
Bob
|
|
|
|
|
Logged
|
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #3 on: November 17, 2012, 08:56:34 pm » |
I'm not sure if I completely understand what you are saying google rand() srand(). The approach I outlined will provide a truly random number.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #4 on: November 18, 2012, 07:10:00 am » |
That looks good. One suggestion: you want to seed the random number generator with a truly random number. So do successive fills with the adc's lowest bit. unsigned long tmp = 0x01, rseed=0; //create a 32-bit random number do { if (analogRead(1) & 0x01) rseed |= tmp; //test bit 0 else rseed |= 0x00; tmp = tmp << 1; //left shift the mask while (tmp); randomSeed(tmp); //seed the random number generator
I would actually use rand() and srand() for their portability.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #5 on: November 18, 2012, 07:11:09 am » |
You can actually use this approach to generate a truly random number (vs. the pesudo random number from rand() or random()), but it takes a lot more time.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 73
Posts: 6837
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #6 on: November 18, 2012, 08:41:37 am » |
Using the LSB of an ADC reading can give pretty good results, but even with whitening it's not truly random.
There's a mega thread around where I (and others) tried it, when you graph the results you get definite banding.
It's well good enough for most applications though.
_____ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #7 on: November 18, 2012, 10:05:34 am » |
Here is the 1st ~2000 of adc data, using myanalogRead(): unsigned short myanalogRead(unsigned char pin) { ADMUX = 0x00; //Vref = DEFAULT, adc on adc0, right justified ADCSRA = (1<<ADEN) | //enable adc (0<<ADSC) | //don't start the adc yet (0<<ADATE) | //no auto sampling (1<<ADIF) | //clear adc flag by writing 1 to it (0<<ADIE) | //no adc interrupt (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0); //1/16 adc prescaler ADCSRA |= (1<<ADSC); //start the conversion while (ADCSRA & (1<<ADSC)) continue; //wait for the conversion to end return ADC; }
I had it fixed on adc0.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #8 on: November 18, 2012, 10:09:41 am » |
First 2500+ data points using the stock analogRead():
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #9 on: November 18, 2012, 10:21:16 am » |
myanalogRead(), over 4500 samples.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #10 on: November 18, 2012, 10:21:37 am » |
Looks pretty random to me.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 73
Posts: 6837
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #11 on: November 18, 2012, 11:13:26 am » |
Yes it does. I used analogRead() IIRC, interesting difference I wonder why. Maybe the /16 div factor you're using, They seem to use 2.
_____ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 120
Posts: 10181
|
 |
« Reply #12 on: November 18, 2012, 03:47:22 pm » |
google rand() srand(). The approach I outlined will provide a truly random number. No, it doesn't. Using your function, I get a steady stream of... 1023 1023 1023 1023 1023 1023 1023 ... 1023 I'll give you a hint, @dhenry, making a claim that something provides "truly random numbers" requires a mathematical proof. Posting a single function with one person's casual observations is not even close.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #13 on: November 18, 2012, 04:46:42 pm » |
With the stock analogRead(), here are the top value counts after about 2500*8 samples: value count 0 107 84 96 42 89 21 89 10 81 64 79 80 78 8 78 85 77 2 77
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #14 on: November 18, 2012, 04:55:41 pm » |
Maybe the /16 div factor you're using, That's not done without reason,  They seem to use 2. 
|
|
|
|
|
Logged
|
|
|
|
|
|