Arduino入門:乱数

ArduinoのSketch(スケッチ)で使用できる乱数について解説します。

randomSeed

randomSeedは疑似乱数ジェネレータを初期化し、乱数列の任意の点からスタートします。乱数列はとても長いものですが、常に同一である事に注意する必要があります。スケッチを実行するたびに異なった乱数列を発生する必要がある場合、未接続のピンをanalogReadした値のような、真のランダム値と組み合わせて、randomSeedを実行させます。逆に、疑似乱数が毎回同じ数列を作り出す性質を利用したい場合は、randomSeedを毎回同じ値で実行します。

//Arduino Sketch Example: randomSeed
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)

//Syntax
random(value)

//Parameters
//long, int - pass a number to generate the seed.

//Description
/*
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.
*/

//Example

long randNumber;

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  randNumber = random(300);
  Serial.println(randNumber);

  delay(50);
}

Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
Arduino Reference:randomSeed(seed)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original

上の例では、26行目でanalogReadの値を使って乱数を初期化しています。

random

randomは、疑似乱数を生成します。設定パラメータは次の通りです。

min: 生成する乱数の下限。省略可能
max: 生成する乱数の上限

戻り値は、minからmax-1の間の整数 (long) となります。

//Arduino Sketch Example: random
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)

//Syntax
random(max)
random(min, max)

//Parameters
//min - lower bound of the random value, inclusive (optional)
//max - upper bound of the random value, exclusive

//Returns
//a random number between min and max-1 (long)

//Description
//The random function generates pseudo-random numbers.

//Note:
/*
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.
*/

//Example

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
  randNumber = random(300);
  Serial.println(randNumber);  

  // print a random number from 10 to 19
  randNumber = random(10, 20);
  Serial.println(randNumber);

  delay(50);
}

Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
Arduino Reference:random()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original

上の例は、下限を指定しない場合とする場合の2例です。

→その他のArduino関連情報

Sponsored Link