Arduino Programming Language: Random

Name

random()

Description

Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If one parameter is passed to the function it will return a float between zero and the value of the parameter. The function call random(5) returns values between 0 and 5. If two parameters are passed, it will return a float with a value between the the parameters.

Syntax

random(value1);
random(value1, value2);

Parameters

  • value1: int or float
  • value2: int or float

Returns

float 

Usage

Application

Examples

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);
}

Related Functions


Based on the reference originally written by Arduino Community,  wiring.org.co and  arduino.cc