When working with Arduino projects, it's common to encounter the need to generate random numbers. Maybe you're building a game, an interactive device, or you simply need events that don't follow a predictable pattern. But are the numbers generated by an Arduino really completely random? The short answer is no, and hence the need to understand the concept of numbers. pseudorandom and how to implement them effectively.
In this article, we will teach you how to generate random numbers in Arduino, how to use the functions random y randomSeed, and why these are key to making sure our number sequences are as random as possible. You'll see that although the numbers generated are not completely random, we can achieve a much higher degree of randomness by properly manipulating the seeds that Arduino uses.
How does Arduino generate random numbers?
In principle, a microcontroller like Arduino is not capable of generating true random numbers, as it is a device designed to be predictable and accurate. What Arduino does is run mathematical calculations with a seed (a base number) to generate a sequence of numbers. pseudorandomThis means that from the same seed, you will always get the same sequence of numbers.
To avoid numbers being repeated many times, the seed can be varied with the function randomSeed (). This allows you to change the starting point for generating the sequence of numbers. Using this tool along with some clever techniques for obtaining unpredictable seeds, such as reading an unconnected analog pin or measuring the program execution time, we can make the numbers vary with each execution of the code.
Using the random function
Arduino provides us with two basic ways to invoke the function random: one to generate a random number between 0 and a maximum number (max – 1), and another to generate a random number between a minimum value and a maximum value (min and max).
The basic syntax is as follows:
- random(max); generates a number between 0 and max – 1.
- random(min, max); generates a number between min y max – 1.
For example, if we call random(250), we will get a number between 0 and 249. Similarly, if we execute random(100,200), we will get a random number between 100 and 199.
Importance of the randomSeed function
As we have already mentioned, to prevent Arduino from always generating the same sequence of numbers in each execution of the program, we must make sure to vary the seed that we use in randomSeed ()The trick is to choose an unpredictable seed each time the code is run.
One of the most common ways to generate a random seed is to read an unconnected analog pin. In this case, that pin behaves unpredictably, picking up electrical noise, ensuring that the value read will constantly vary. The seed can be set as follows:
randomSeed(analogRead(0));
Alternatively, we can use the function millis () to get the elapsed time since the program started and use it as a seed. This is especially useful in interactive projects where the user may press a button at different points in time, which will generate a different start for each sequence of numbers.
For example, we can use randomSeed (millis ()); so that the seed varies depending on the execution time.
Practical example: Electronic dice

A classic application of using random numbers in Arduino is the creation of an electronic dice. In this case, each time we press a button, a number between 1 and 6 is generated, and LEDs are lit to represent the corresponding face of the dice.
This is an example of how the code could be structured:
push button = 8; // Button pin
numRandom = random(1, 7); // Generates a random number between 1 and 6
When the button is pressed, Arduino generates the random number and automatically activates the corresponding LEDs for 1,5 seconds so that the user can view the final result. After a short time, the LEDs turn off until the button is pressed again.
Generating improved pseudorandom numbers
To further improve randomness, some users prefer to focus on the first few bits of random noise that the analog pin can pick up. This is because the first few bits contain a greater amount of variability, which can be exploited in projects that require higher randomness. An interesting way to do this would be to use the bitwise operation:
randomSeed(analogRead(A0) & 3);
This method allows the first few bits to be extracted from the noise picked up by the analog pin A0, which improves the randomness of the generated numbers. Arduino has the ability to generate up to 4.294.967.295 different pseudo-random numbers, which is more than enough for most home projects.
Finally, once you properly set the seed and range, you can enjoy greater variability in the numbers generated by Arduino, which is ideal for a wide variety of projects such as games, interactive systems, and simulations. For example:
void setup() {
Serial.begin(9600); // Inicializamos la comunicación serial
randomSeed(analogRead(0)); // Semilla para generar números más aleatorios
}
void loop() {
int numeroAleatorio = random(1, 11); // Genera un número aleatorio entre 1 y 10
Serial.println(numeroAleatorio);
delay(1000); // Esperamos 1 segundo
}
Please note that although Arduino is not capable of generating truly random numbers, applying techniques such as those mentioned here will significantly improve the results and allow you to simulate randomness in most cases.