If it is important for a sequence of numbers generated by random()
to differ, it is a good idea to specify a seed with randomSeed()
:
void setup() {
Serial.begin(9600);
// If analog pin 0 is left unconnected, analogRead(0) will produce a
// different random number each time the sketch is run.
randomSeed(analogRead(0));
}
void loop() {
long randomNumber = random(500); // Generate a random number between 0 and 499
Serial.println(randomNumber);
delay(100);
}