ESP32 Arduino: Random Number Generation

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 FireBeetle board.

 

Introduction

The objective of this post 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.

ESP32 Arduino Random Number Generation.png

Figure 1 – Random numbers generation.

 

References

[1] http://espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf

[2] http://webhome.phy.duke.edu/~rgb/General/dieharder.php

[3] https://github.com/espressif/arduino-esp32/blob/39fb8c30440a4abd5fe0e2c87609ba6798ae8013/tools/sdk/include/esp32/esp_system.h

[4] https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

Advertisements
This entry was posted in ESP32 and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s