設定種子

如果由 random() 生成的一系列數字的重要性不同,那麼用 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);
}