From Microduino Wiki
Jump to: navigation, search


  • Description

Use function random() will generate pseudo random numbers.

  • Syntax

random(max) random(min, max)

  • Parameter

min - The minimum value of a random number, which is included in the random number. (optional) max - The maximum value of a random number, which is not included in the random number.

  • Return

A random number between min and max-1(the data type is long ).

  • Note

If you need a truly random number to be generated in a random() sequence, when its subsequence is being executed , use function randomSeed() to preset an absolute random input, for example, the returned value of function analogRead() in a disconnected pin.

On contrary, sometimes, accurate repeat of the pseudo random bumber is also useful. This can be completed before the start of a random sequence, through calling a randomSeed() with a fixed value.

  • Example

long randNumber;
void setup(){
//If the analog input pin 0 is disconnected, it will imitate noise randomly. 
//It will call randomSeed() to generate when each run of the code. 
//Different seed values. 
//RandomSeed() will randomly disturb function random.
void loop() {
//Print a random number between 0 to 299.
  randNumber = random(300);
//Print a random number between 10 to 19. 
  randNumber = random(10, 20);

[Return to Arduino Syntax Manual]