设置种子

如果由 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);
}