randomSeed()

[Random Numbers]

描述

randomSeed() initializes the pseudo-random number generator, causing it to start at an arbitrary point in its random sequence. This sequence, while very long, and random, is always the same.

If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.

Conversely, it can occasionally be useful to use pseudo-random sequences that repeat exactly. This can be accomplished by calling randomSeed() with a fixed number, before starting the random sequence.

語法

randomSeed(seed)

參數

long, int – pass a number to generate the seed

回傳

no returns

Example Code

long randNumber;
 
void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}
 
void loop(){
  randNumber = random(300);
  Serial.println(randNumber);
 
  delay(50);
}

參考


語法參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール