Skip to content

Arduino - Random Numbers

Generating random numbers is a common requirement in projects involving games, simulations, or unpredictable behaviors. The Arduino core provides simple functions to produce pseudo-random numbers.

Computers, including microcontrollers, are deterministic machines. They can’t create truly random numbers on their own. Instead, they use a mathematical formula to generate a sequence of numbers that appears random but is actually predictable if you know the starting point. This is called a pseudo-random sequence, and the starting point is called the seed.

If you use the same seed every time, you will get the exact same sequence of ‘random’ numbers. To make our program feel truly random, we must provide an unpredictable seed.

The randomSeed() function initializes the pseudo-random number generator with a seed value. It should be called once, usually in your setup() function. To get a good seed, we need a source of unpredictability. A common technique on the Arduino is to read the electrical noise from an unconnected analog pin.

// Seed the generator using environmental noise from a floating analog pin.
// This makes the sequence different each time the board powers on.
randomSeed(analogRead(A0));

Once seeded, the random() function generates the next number in the sequence. It can be used in two ways.

// Generates a random number between 0 and (max - 1)
long randomNumber = random(max);
// Generates a random number between min and (max - 1)
long randomNumber = random(min, max);

Important Note: The maximum value in the range is exclusive. For example, random(1, 7) will return a number from the set {1, 2, 3, 4, 5, 6}, which is perfect for a standard die roll.

This sketch simulates rolling a six-sided die every two seconds and prints the result to the Serial Monitor.

/*
Digital Dice Roll Simulator
This sketch demonstrates how to properly seed and generate
a random number within a specific range.
*/
void setup() {
// Start serial communication to display the results
Serial.begin(9600);
// Wait for serial port to connect. Needed for native USB port boards (e.g., Leonardo)
while (!Serial) {
; // wait
}
// Seed the random number generator. Using a floating analog pin is a simple
// way to get a different seed on each power-up.
randomSeed(analogRead(A0));
Serial.println("--- Random Dice Roll ---_\n");
}
void loop() {
// Generate a random number from 1 to 6 (inclusive).
// The upper bound of random() is exclusive, so we use 7.
long dieRoll = random(1, 7);
Serial.print("You rolled a: ");
Serial.println(dieRoll);
// Wait for 2 seconds before the next roll
delay(2000);
}

Understanding how numbers are stored can be helpful. The fundamental unit of data is a bit, which can be a 0 or a 1. A group of 8 bits is called a byte. A byte can represent 2^8 = 256 different values (typically 0 to 255).

  • The random() function returns a long, which is a 4-byte (32-bit) number, allowing for a very large range of possible values.
  • The position of a bit within a byte determines its value. The rightmost bit is the Least Significant Bit (LSB), and the leftmost is the Most Significant Bit (MSB).