【Arduino 函数练习】随机数random ()和 randomSeed()

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);
}
发布了75 篇原创文章 · 获赞 58 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/Naiva/article/details/93930937