$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: Random Number Generation

DFRobot Dec 27 2017 1477

The objective of this post is to explain how to generate random numbers on the ESP32, using the Arduino core. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 development board.
 

Introduction

The objective of this ESP32 Arduino Tutorial is to explain how to generate random numbers on the ESP32, using the Arduino core.

In terms of hardware, the ESP32 has a True Random Number Generator, meaning the values obtained from it are truly random [1].

Those true random numbers are generated based on the noise of the WiFi / Bluetooth RF subsystem, which means that if the Bluetooth and WiFi are both disabled, then only pseudo random numbers are generated [1].

Although for this simple tutorial it is not relevant if the numbers are truly random or pseudo random, the capability of the ESP32 to generate truly random numbers is very important since they can be used for cryptographic operations [1]. You can read here an explanation about the difference of these two types of randomness.

One interesting thing to mention regarding the ESP32 Random Number Generator is that a data sample of 2 GB obtained from it with the WiFi enabled has passed all the tests of the Dieharder Random Number Testsuite [1], which is a testing suite for random number generators [2].

As a lower level analysis of the ESP32 Random Number Generation, you can check this great article.

The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

If you prefer, you can check the video version of this tutorial below.


The code

We will start our code by opening a serial connection on the Arduino setup function, so we can output the results of our program.

void setup() {
  Serial.begin(115200);
}

We will then obtain and periodically print the random numbers on the main loop function.

In order to obtain a random number, we can use the low level esp_random function, which is defined here. This function receives no arguments and returns a random value between 0 and UINT32_MAX [3] (the largest value that an unsigned int can have).

Note however that, as mentioned in the introductory section, the value will only be truly random if either the WiFi or the Bluetooth RF system is running [3].

Serial.println(esp_random());

As alternative to this lower level function, we can use the Arduino random function, which is also implemented on the ESP32 Arduino core.

The random function is overloaded and can be called by passing one or two input parameters.

In case we only pass one parameter, we are specifying the upper bound of the random number generated (exclusive) [4]. Thus, the result will be a number between 0 and the value of the input parameter – 1.

So, in the call below, we will get a number between 0 and 9.

Serial.println(random(10));

If we call the function by passing two input parameters, the first one will be the lower bound of the random number generated (inclusive) and the second one will be the upper bound (exclusive).

So, in the call below, we will obtain a random number between 10 and 19.

Serial.println(random(10,20));

Note that both versions of the random function call the esp_random function in their implementation, as can be seen here. Thus, the considerations regarding the need for either the WiFi or the Bluetooth RF system being connected for obtaining truly random generated numbers also apply.

The final complete code can be seen below. We have added an extra print for better readability and a small delay between each iteration of the Arduino loop.

void setup() {
  Serial.begin(115200);
}
 
void loop() {
 
  Serial.println("-----------");
  Serial.println(esp_random());
  Serial.println(random(10));
  Serial.println(random(10,20));
 
  delay(1000);
}


Testing the code

For testing the code, simply compile it and upload it to your device. Then, open the Arduino IDE serial monitor and check the results that get printed. They should be similar to figure 1, which shows the random numbers generated with the previous function calls.

Figure 1 – Random numbers generation.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.

REVIEW