Esp32

Generate Random Numbers Using ESP32 and Arduino IDE

Using ESP32 we can create different projects and make unique products that can solve multiple problems. ESP32 is a microcontroller board so using Arduino IDE programming functions we can create interesting output. This lesson guides how one can generate random numbers using the ESP32 board.

What are Random Numbers

Random numbers play a major role in security and encryption. You might have used Google password suggestions. It works on a principle which is somehow similar to generating random numbers.

Random numbers are a sequence of numbers that one cannot predict, and it is a number which is chosen from a set of numbers.

Why We Need Random Numbers

Random numbers are important for cryptographic operations, modern day computing and simulations. Random numbers also help to enhance computer security. Here are few main applications of random numbers:

  • Random numbers used in algorithms
  • Procedurally generated contents such as random images, names and many other
  • For simulations that are nondeterministic such as whether pattern, dice shuffling and weather patterns

As now we understand the basic applications of ESP32 random number generator lets take a look at code and generate some random numbers.

Function to Generate Random Number in ESP32

The ESP32 board contains a hardware random number generator which uses the function esp_random().

esp_random() takes no argument and gives us a value randomly generated from 0 to UINT32_MAX (it is the maximum value an unsigned int can store inside it).

Note: One thing to remember here is ESP32 hardware random number generator works using the WiFi and Bluetooth. The true random number is only generated when both are enabled. If these two are disabled ESP32 can only generate a pseudo number. For more details, please check the Espressif ESP32 Random Number Documentation.

Interesting information: An interesting information about ESP32 hardware random number generator is that ESP32 has passed the Dieharder Random Number Testsuite when a data sample of 2GB is taken using ESP32 when its WiFi is enabled. Die harder is a test for random number generator.

Syntax
Here is the syntax of ESP32 function for random number:

esp_random()

Return
This function returns a random value between 0 and UINT32_MAX.

Code to Generate a Random Number

Open Arduino IDE and upload the given code in ESP32 to generate a random number between 0 and 4294967295 (maximum unsigned int value).

void setup() {
  Serial.begin(115200); /*Baud Rate Defined*/
}
void loop() {
  Serial.println("**********");  
  Serial.print("Random Number= ");  
  Serial.println(esp_random());  /*Print any random number from 0 to largest unsigned int*/
  delay(2000);  /*delay of 2 sec*/
}

Output
After uploading code, we can see the output on the serial monitor. Here we can see ESP32 generated two different random numbers.

Code to Generate a Random Number between Specific Range

Suppose if the ESP32 WiFi is not enabled then there is an alternative to function esp_random(). We can use the Arduino random number generator function (random()).

Using this function, we can generate any random number in a specified range.

Open Arduino IDE and upload a given code that will give us a random number between 10-20.

void setup() {
  Serial.begin(115200); /*Baud Rate Defined*/
}
void loop() {
  Serial.println("**********");  
  Serial.print("Random Number Between 10 & 20= ");/*Print any random number between 10 and 20*/
  Serial.println(random(10,20));
  delay(2000);  /*delay of 2 sec*/
}

Output
Following output can be seen on the serial monitor: a random number is generated every 2 sec.

We have successfully covered the functions which ESP32 used to generate random numbers.

Conclusion

Random numbers play a major role in probability and statistics. It is also helpful in security encryption and cryptographic operations. This lesson will guide you on generating different random numbers. We can also define a range to get a random number.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.