Arduino 官网:
参考 > 语言 > 功能 >随机数 >随机数
随机数函数:
-
random()
-
randomSeed()
long randNumber;
void setup() {
Serial.begin(9600);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
//randomSeed(analogRead(0));
}
void loop() {
// print a random number from 0 to 30
randNumber = random(40);
Serial.print(randNumber);
Serial.print("-");
// print a random number from 0 to 40
randNumber = random(40);
Serial.print(randNumber);
Serial.print("-");
//print a random number from 0 to 10
randNumber = random(40);
Serial.print(randNumber);
Serial.print("-");
randNumber = random(40);
Serial.print(randNumber);
Serial.println("\n");
delay(200);
}