Skip to main content

Random Number

Random Number

This library obtains random numbers.

randomSeed

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.
Syntax
randomSeed(unsigned int seed)
Parameters
seed: A number to generate the seed
Returns
None

random

Description
Obtains the random numbers. Always call the randomSeed function before using this function.
Syntax
long random(long min_num, long max_num)
Parameters
min_num: Lower bound of the random value, inclusive (optional)
max_num: Upper bound of the random value, exclusive (optional)
Returns
A random number (long)

Sample Program

This sample outputs a random number from 0 to 99 in 100ms intervals via serial communications.


#include <Arduino.h>
#define INTERVAL 100
 
void setup()
{
    Serial.begin(9600);
    randomSeed(millis());
}
 
void loop()
{
    Serial.println( random(0, 100) );
    delay(INTERVAL);
}