Pages: [1] 2   Go Down
Author Topic: Using an Arduino to process white noise into Random Numbers  (Read 5296 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am currently trying to build a Random Number Generator using an arduino.

My plan is to build a circuit which generates white noise (which I have done), connect that to the arduino, and use analog read to collect random numbers from the white noise.

The white noise generator looks good on the oscilloscope, but I cannot seem to figure out the next step.
I have tried various graphing and "arduino oscilloscope" programs but the waveform on the arduino does not seem to look like the waveform on the oscilloscope, is it possibly incompatible in some way? Frequency to high, low?  I am getting something on the arduino scope programs, but it doesn't look right.

What would be the best way to process the white noise into random numbers? Would it be better to use digital read insead of analog read, and read the bits into a byte? Or perform some kind of smoothing or randomising algorithm on the data before using it?

Thanks for any advice!
Logged

0
Offline Offline
Full Member
***
Karma: 5
Posts: 238
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If it is good idea to process white noise to get random numbers it would be widely referenced in statistics literature so maybe you want to start there.  Once you find a algorithm from the statistical experts and scholars you could ask here how to implement them.
Logged

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

Try this sketch, it fetches one bit of the white noise per sample and merges this to a unsigned long / byte
Code: (not tested)
void setup()
{
  Serial.begin(115200);
}

void loop()
{
   uint32_t x = getRandomUL();
   Serial.println(x);
}

uint32_t getRandomUL()
{
  uint32_t x = 0;
  for (uint8_t i = 0; i < 32; i++)
  {
    x |= ((analogRead(A0) & 0x01) << i);
  }
}

uint8_t getRandomByte()
{
  uint8_t x = 0;
  for (uint8_t i = 0; i < 8; i++)
  {
    x |= ((analogRead(A0) & 0x01) << i);
  }
  return x;
}
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: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you want to create a random number every time with the white noise ?

Normally, an analog input is used to get a random value, and that is used to feed a true random algoritme. The white noise would be used just once.
You could capture more analog inputs, in case the analog signal is not very random. I use a LDR on one input and the unconnected other analog inputs and combine them. White noise would be a lot better than a LDR ofcourse.
Logged

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

Quote
to feed a true random algoritme
I would like to have that one ... 

best I could find sofar is the external service described here - http://forum.arduino.cc/index.php?topic=38091.msg282225#msg282225 -

And because you just don't know how many others users there are it will be quite unrepeatable ...
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: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

User perhop replied me in an item about encryption:
http://forum.arduino.cc/index.php?topic=175504.msg1302452#msg1302452

He uses this:
https://github.com/chicoplusplus/arduino/tree/master/libraries/TrueRandom
It turns out, that the analog inputs are used for every bit, so it is just like the code by robtillaart in this item.

For my project, I used XTEA so it will fit into a ATmega8.
In the package that I transmit is a random number, for which I use reading the analog channel.
Logged

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

Thank you very much for the replies!

@robtillaart:
I tried your code, it seemed to work well, however it failed the tests I did. I used a program called ENT:
http://www.fourmilab.ch/random/

To test both the number generated from white noise and the number generated from just background radiation (nothing connected to the analog pin).
They were both the same, and both non-random.

@ Erdin@
I would like to do whichever is the most random. Ideally, I would have liked to prove the concept using only numbers generated from white noise, but that is not looking like it's possible. I may have to use the white noise as a seed to a pseudo random generator? I could then test the numbers generated with numbers seeded using the system time, and see if the white noise produces a noticable improvement.
I think continuously seeding from white noise would be more random than seeding just once, would it not?

If I'm not mistaken, is that true random code not just taking the input from an analog port as "random" noise and using that as a seed? If so, I could combine that with white noise on the analog port, I suppose?

Thanks again!
Logged

Offline Offline
Edison Member
*
Karma: 48
Posts: 1631
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the true random code is taking its input from the circuit described here connected to an analog pin:
http://robseward.com/misc/RNG2/
and note that its input must be 12V. 5V won't work.

Pete
Logged

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

Thanks Pete for the link, bookmarked
Logged

Rob Tillaart

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

New England
Offline Offline
Sr. Member
****
Karma: 7
Posts: 295
Natural Semiinductor
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello JackStone, I made some random number hardware in the past at Intel. Please describe your white noise hardware for us. In particular, what is its output impedance, output voltage range, and output bandwidth? Do you have photos of oscilloscope traces of good looking white noise you made. Please post the digital numbers you get that seem bad from your existing software work.
Logged

I am going to get going.

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


@el_supremo:
That circuit looks quite similiar to the one that I am using, so it should work fairly ok. That is an excellent link too, thanks for that.

@AmbiLobe: Really, you worked for intel developing RNG hardware? That's impressive!
My electronics skills are quite basic to be honest, my main skill is in programming. The circuit I am using is this one: http://electro-music.com/forum/phpbb-files/whitenoise_969.jpg

It's a very simple circuit, but it seems to be producing the correct results.

I attached a trace from my oscilloscope. The yellow is the noise going into the amp, the blue is the amplified noise coming out, I think I may have an issue with that, I'm still debugging it.

I also attached a text file with my random numbers in it, which failed the ENT tests.

Thanks for your help!



* NewFile7.jpg (113.07 KB, 320x234 - viewed 59 times.)
* RNDOUT.log (1042.55 KB - downloaded 31 times.)
Logged

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

I have made some changes to my noise generator, and I am now certain that it is producing white noise. However, the numbers being picked up by the arduino (using robtillaart's code) are not random, they have a very predictable pattern.
I have enclosed the log file. Anyone have any idea on this?

* rngtrace.log (3347.98 KB - downloaded 21 times.)
Logged

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


I checked my code and there is some "sawtooth" behaviour in it, the number of shifts when building up the number increases. These are typically the most significant bits 
That should not have any effect, still here an alternative with a constant shift. If this shows the same distribution the shift is not the cause.

Code: (not tested)
void setup()
{
  Serial.begin(115200);
}

void loop()
{
   uint32_t x = getRandomUL();
   Serial.println(x);
}

uint32_t getRandomUL()
{
  uint32_t x = 0;
  for (uint8_t i = 0; i < 32; i++)
  {
    x |= (analogRead(A0) & 0x01);
    x <<= 1;
  }
}

uint8_t getRandomByte()
{
  uint8_t x = 0;
  for (uint8_t i = 0; i < 8; i++)
  {
    x |= (analogRead(A0) & 0x01);
    x <<= 1;
  }
  return x;
}
Logged

Rob Tillaart

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

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


TruyRandom is worthless.  Stay away from it.
Logged

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


I suspect this will be helpful...
http://forum.arduino.cc/index.php?topic=108380.0
Logged

Pages: [1] 2   Go Up
Jump to: