The latest version...
#include <avr/eeprom.h>
/*==============================================================================
Call reseedRandom once in setup to start random on a new sequence. Uses
four bytes of EEPROM.
==============================================================================*/
void reseedRandom( uint32_t* address )
{
static const uint32_t HappyPrime = 127807 /*937*/;
uint32_t raw;
unsigned long seed;
// Read the previous raw value from EEPROM
raw = eeprom_read_dword( address );
// Loop until a seed within the valid range is found
do
{
// Incrementing by a prime (except 2) every possible raw value is visited
raw += HappyPrime;
// Park-Miller is only 31 bits so ignore the most significant bit
seed = raw & 0x7FFFFFFF;
}
while ( (seed < 1) || (seed > 2147483646) );
// Seed the random number generator with the next value in the sequence
srandom( seed );
// Save the new raw value for next time
eeprom_write_dword( address, raw );
}
inline void reseedRandom( unsigned short address )
{
reseedRandom( (uint32_t*)(address) );
}
/*==============================================================================
So the reseedRandom raw value can be initialized allowing different
applications or instances to have different random sequences.
Generate initial raw values...
https://www.random.org/cgi-bin/randbyte?nbytes=4&format=h
https://www.fourmilab.ch/cgi-bin/Hotbits?nbytes=4&fmt=c&npass=1&lpass=8&pwtype=3
==============================================================================*/
void reseedRandomInit( uint32_t* address, uint32_t value )
{
eeprom_write_dword( address, value );
}
inline void reseedRandomInit( unsigned short address, uint32_t value )
{
reseedRandomInit( (uint32_t*)(address), value );
}
uint32_t reseedRandomSeed EEMEM = 0xFFFFFFFF;
void setup( void )
{
/*
// Example that sets the seed to a specific value
// Typically only done during debugging
// Uses EEMEM to determine the EEPROM address
reseedRandomInit( &reseedRandomSeed, 42 );
*/
// Example that reseeds the random number generator each time the application starts
// Uses EEMEM to determine the EEPROM address
// Most common use
reseedRandom( &reseedRandomSeed );
/*
// Example that sets the seed to a specific value
// Typically only done during debugging
// EEPROM address 0 (through 3) is used to store the seed
reseedRandomInit( (unsigned short) 0, 42 );
*/
/*
// Example that reseeds the random number generator each time the application starts
// EEPROM address 0 (through 3) is used to store the seed
reseedRandom( (unsigned short) 0 );
*/
}
void loop( void )
{
}
Edit: 937 changed to 127807 to avoid the "dead zone".