Seed 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.
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.
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.
dhenry:
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.
If you want true randomness, try biasing a diode near the "knee" of the I-V curve (around 0.7V for a silicon diode). The current will fluctuate randomly
due to thermal noise.
There is an easier way: reverse connect a npn to form an esaki diode. Power that up via a capacitor + a low. It will form a flasher. The period of its flashes is random and can be easily measured by a timer.