Random Number

This library allows users to get a random number.

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 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

Output random numbers between 0 and 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);
}