Pages: [1]   Go Down
Author Topic: Example sketch for seeding the libraries pseudo-random number generator  (Read 801 times)
0 Members and 1 Guest are viewing this topic.
Dallas, Texas
Offline Offline
God Member
*****
Karma: 31
Posts: 887
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I received an email request from someone today who wanted to have a sketch that had a different seed value for the seedRandom() function every time the sketch ran, but found the standard seedRandom(analogRead(0)) didn't work, and the example from my true random number generation library, Entropy added too much overhead for their sketch.  So I created a sketch which uses the same basic technique used as the entropy source in my library, but dropped all of the other overhead, and disables the interrupt after the seed is created.  A link to this sketch is now on the Arduino Playground or it can be found on my  entropy site here: https://sites.google.com/site/astudyofentropy/project-definition/timer-jitter-entropy-sources/entropy-library/arduino-random-seed

Code:
// TrueRandomSeed.ino
// This example sketch shows how to provide a truly random seed value to the built in
// library pseudo random number generator. This ensures that your sketch will be
// using a different sequence of random numbers every time it runs. Unlike the
// usually suggested randomSeed(analogRead(0)) this method will provide a much more
// uniform and varied seed value. For more information about the basic technique used
// here to produce a random number or if you need more than one such number you can
// find a library, Entropy from the following web site along with documentation of how
// the library has been tested to provide TRUE random numbers on a variety of AVR
// chips and arduino environments.
//
// https://sites.google.com/site/astudyofentropy/project-definition/
// timer-jitter-entropy-sources/entropy-library
//
// Copyright 2014 by Walter Anderson, wandrson01 at gmail dot com
//
 
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/atomic.h>
// The following addresses a problem in version 1.0.5 and earlier of the Arduino IDE
// that prevents randomSeed from working properly.
// https://github.com/arduino/Arduino/issues/575
#define randomSeed(s) srandom(s)
 
volatile uint32_t seed; // These two variables can be reused in your program after the
volatile int8_t nrot; // function CreateTrulyRandomSeed()executes in the setup()
                     // function.
 
void CreateTrulyRandomSeed()
{
seed = 0;
nrot = 32; // Must be at least 4, but more increased the uniformity of the produced
// seeds entropy.
// The following five lines of code turn on the watch dog timer interrupt to create
// the seed value
cli();
MCUSR = 0;
_WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (1<<WDE);
_WD_CONTROL_REG = (1<<WDIE);
sei();
 
while (nrot > 0); // wait here until seed is created
 
// The following five lines turn off the watch dog timer interrupt
cli();
MCUSR = 0;
_WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (0<<WDE);
_WD_CONTROL_REG = (0<< WDIE);
sei();
}
 
ISR(WDT_vect)
{
nrot--;
seed = seed << 8;
seed = seed ^ TCNT1L;
}
 
void setup()
{
CreateTrulyRandomSeed();
randomSeed(seed);
// The preceeding two function calls will take approximately 0.5 second to execute if
// nrot is set to 32 ... the rest of your setup code should FOLLOW from here.
}
 
void loop()
{
}
« Last Edit: April 11, 2014, 08:45:18 am by wanderson » Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

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

Still the randomSeed function in Arduino.h fails to accept an uint32 properly ...(IDE 1.5.4 /1.0.4)

Code:
void randomSeed(unsigned int);

See issue - https://github.com/arduino/Arduino/issues/575 -
Logged

Rob Tillaart

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

Dallas, Texas
Offline Offline
God Member
*****
Karma: 31
Posts: 887
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the heads up!  I modified both the included sketch and the gist hub version to address the problem!
Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

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

good solution!
now we can access "Pseudo Random Number Chains" we never could access before !
Thanks

Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: