Here is how to create two random numbers from 1 to 5 on an Arduino:
x = random (1,6) ;
y = random (1,6) ;
Don't be tricked by the 6. The actual output ranges from 1 to 5 in these examples. Anyway.
If you don't use the function randomSeed(iSeed)you will get the same sequence of random numbers every time you run your program. This is because random actually generates a pseudo random number, which means it loops through all available answers and then loops back to the beginning again.
So randomSeed(iSeed)is used to start in a different place in the loop every time you run the program. iSeed must vary to change the starting point of random.
Often randomSeed is called in the setup part of the Arduino program.
But how can you choose a different iSeed every time you run the program? An often suggested method is to read a floating analog input. "Floating" means it is not connected to anything, so electrical fields wafting in from your hand or cell phone or brain move the electrons on the circuit board track which connects to the input. The moving electrons produce a tiny current and the tiny current produces a tiny change in voltage, which you read using the function ???
You can improve the random variation of voltage at A0 (for example) simply by adding a long wire to it!
Here's the board with no connection to A0:
And here are the variations in voltage...
A0, rand seed: 265, min 265, max 265, delta 0
A0, rand seed: 263, min 263, max 265, delta 2
A0, rand seed: 261, min 261, max 265, delta 4
A0, rand seed: 259, min 259, max 265, delta 6
A0, rand seed: 258, min 258, max 265, delta 7
A0, rand seed: 257, min 257, max 265, delta 8
A0, rand seed: 256, min 256, max 265, delta 9
A0, rand seed: 255, min 255, max 265, delta 10
A0, rand seed: 254, min 254, max 265, delta 11
A0, rand seed: 254, min 254, max 265, delta 11
A0, rand seed: 253, min 253, max 265, delta 12
A0, rand seed: 252, min 252, max 265, delta 13
A0, rand seed: 251, min 251, max 265, delta 14
A0, rand seed: 251, min 251, max 265, delta 14
A0, rand seed: 251, min 251, max 265, delta 14
A0, rand seed: 250, min 250, max 265, delta 15
A0, rand seed: 250, min 250, max 265, delta 15
A0, rand seed: 249, min 249, max 265, delta 16
A0, rand seed: 249, min 249, max 265, delta 16
A0, rand seed: 248, min 248, max 265, delta 17
A0, rand seed: 247, min 247, max 265, delta 18
A0, rand seed: 248, min 247, max 265, delta 18
A0, rand seed: 247, min 247, max 265, delta 18
A0, rand seed: 246, min 246, max 265, delta 19
A0, rand seed: 247, min 246, max 265, delta 19
... going from 246 to 265.
Here is the Arduino with a long wire connected to A0:
And here are the variations in voltage (with a long wire)...
A0, rand seed: 48, min 48, max 48, delta 0
A0, rand seed: 59, min 48, max 59, delta 11
A0, rand seed: 67, min 48, max 67, delta 19
A0, rand seed: 81, min 48, max 81, delta 33
A0, rand seed: 93, min 48, max 93, delta 45
A0, rand seed: 133, min 48, max 133, delta 85
A0, rand seed: 193, min 48, max 193, delta 145
A0, rand seed: 249, min 48, max 249, delta 201
A0, rand seed: 308, min 48, max 308, delta 260
A0, rand seed: 369, min 48, max 369, delta 321
A0, rand seed: 431, min 48, max 431, delta 383
A0, rand seed: 488, min 48, max 488, delta 440
A0, rand seed: 537, min 48, max 537, delta 489
A0, rand seed: 577, min 48, max 577, delta 529
A0, rand seed: 612, min 48, max 612, delta 564
A0, rand seed: 629, min 48, max 629, delta 581
A0, rand seed: 799, min 48, max 799, delta 751
A0, rand seed: 747, min 48, max 799, delta 751
A0, rand seed: 325, min 48, max 799, delta 751
A0, rand seed: 310, min 48, max 799, delta 751
A0, rand seed: 297, min 48, max 799, delta 751
A0, rand seed: 288, min 48, max 799, delta 751
A0, rand seed: 278, min 48, max 799, delta 751
A0, rand seed: 273, min 48, max 799, delta 751
A0, rand seed: 268, min 48, max 799, delta 751
...going from 48 to 799, clearly an improvement.
Remember the analog signal is not the random number, but how to make the seed for the random number generator function, random().
Here is the code I used to do the test above:
/*
* Adding a wire to a floating analog input can give a better range
* of random seeds.
*/
// The setup() method runs once, when the sketch starts
void setup() {
Serial.begin (9600) ;
}
// the loop() method runs over and over again,
// as long as the Arduino has power
int iMin = 1024 ;
int iMax = 0 ;
int iLoopResetCount = 0 ;
void loop()
{
int iVal = analogRead (A0) ;
if (iVal < iMin) {
iMin = iVal ;
}
if (iVal > iMax) {
iMax = iVal ;
}
Serial.print ("A0, rand seed: ") ;
Serial.print (iVal) ;
Serial.print (", min ") ;
Serial.print (iMin) ;
Serial.print (", max ") ;
Serial.print (iMax) ;
Serial.print (", delta ") ;
Serial.print (iMax-iMin) ;
Serial.println() ;
delay(500);
iLoopResetCount+=1 ;
if (iLoopResetCount == 25) {
iLoopResetCount=0 ;
Serial.println("loop reset") ;
iMin = 1024 ;
iMax = 0 ;
}
}
And here is how in practice you use randomSeed:
/*
* Move a servo to random positions every 10 seconds.
*/
// The setup() method runs once, when the sketch starts
void setup() {
Serial.begin (9600) ;
int iSeed = analogRead (A0) ;
randomSeed(iSeed);
Serial.print ("Seed = ") ;
Serial.println (iSeed) ;
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
// Move a servo randomly between 0 and 180 degrees
int iServoDegrees = random(0,181) ;
Serial.print ("Would move the servo to ") ;
Serial.print (iServoDegrees) ;
Serial.println (" degrees ") ;
delay(10000); // 10000ms = 10 sec
}
.