[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
範例程式碼
long randNumber; void setup(){ Serial.begin(9600); randomSeed(analogRead(0)); } void loop(){ randNumber = random(300); Serial.println(randNumber); delay(50); }
參考
- [程式語法] random()
語法參考主頁面
86Duino 參考的文本是根據 知識共享署名-相同方式分享 3.0 許可證,部分文本是從 Arduino 參考 修改的。 參考中的代碼示例已發佈到公共領域。