randomSeed(analogRead(5)); // randomize using noise from analog pin 5
random()
random函数生成伪随机数。以下是语法。
random()语法
long random(max) // it generate random numbers from 0 to max
long random(min, max) // it generate random numbers from min to max
例子
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 299
Serial.print("random1=");
randNumber = random(300);
Serial.println(randNumber); // print a random number from 0to 299
Serial.print("random2=");
randNumber = random(10, 20);// print a random number from 10 to 19
Serial.println (randNumber);
delay(50);
}