(First post - hope this isn't already covered elsewhere...)
I was having a problem with randomSeed() - calling it like this:
randomSeed(analogRead(0));
Is very unrandom. However, in digging, it appears to be the fault of the analogRead(0).
My question - does anyone else have this problem, or is it just my Arduino?
So I put together a test program:
void setup()
{
Serial.begin(9600);
for (int i=0;i<256;++i)
{
Serial.print(analogRead(0));
if (0==(i&0xF)) // for line breaking/formatting
Serial.print("\n");
Serial.print(" ");
}
}
void loop()
{
}
Here’s some of my results:
450 448 448 449 449 447 447 448 448 446 446 447 446 445 444 445
445 443 443 444 444 442 442 443 442 441 441 442 441 440 440 440
440 439 439 440 439 438 438 439 438 437 437 438 437 436 436 437
435 434 435 435 434 433 434 434 433 432 432 433 432 431 431 432
As you can see, it's nowhere near the 0-1023 (10 bit) range expected.
To use, just run it for your Arduino – I’m very curious if everyone else has this very narrow range of values.
My reason for asking is that I solved the problem, but realized I don't know if anyone else has the same issue!
The whole solution (with explanation and code) is here:
http://www.utopiamechanicus.com/53/arduino-better-random-numbers/
Obviously, if random bias is not affecting your Arduino, then I've solved a non-existent problem - so I'd appreciate any feedback.
Thanks!