Random Number Generator using ESP32

Last Updated on December 22, 2024

Random Numbers are useful in Testing, Data Sampling and Gaming. Generating Random Number is a hard task but with ESP32 you can easily do that., Yes, because ESP32 microcontroller contains built in Hardware Random Number Generator (RNG). I’m also wondered when i randomly came to know. This block can be used for applications requiring randomness or random number. Let’s learn this by doing simple experiment.




Random Number Generator

Any mechanism or electronic system that produces a sequence of numbers that cannot be easily predicted is RNG. It can be work in two different category.

  1. Pseudo-Random Number Generator (PRNGs) – These are Algorithm based number generator but can be reproduced.
  2. True Random Number Generator (TRNGs) – These are rely on physical phenomena such as noise to generate non deterministic numbers. ESP32 microcontroller comes under this category.

“Just connect ESP32 board with computer and upload the following Sketch through Arduino IDE and see the Random numbers in serial monitor“.

How the ESP32 RNG Works?

The ESP32 microcontroller contains in built Hardware called RNG and it uses hardware noise generated by its Wi-Fi, Bluetooth and ADC as a entropy source. Which is processed by the RNG block to generate truly random numbers.

Conditions for RNG in ESP32

Internal RNG Hardware of ESP32 works when the following conditions are met.

  • RF subsystem must enabled.
  • The internal entropy source (SAR ADC) should not be disabled.
  • Proper Bootloading.

Refer Official Document for more information.

Implementing RNG

Using this hardware based Random Number Generator is better than software based one. ESP32 hardware RNG makes fast and efficient output. It uses 32 bit register to produce random numbers. It does not require additional configuration or external components. “esp_random( )” function provided by the ESP_IDF frame work, used in the following code to start RNG.

Arduino Code for 32 bit RNG

/* Code from theoryCIRCUIT */
#include <Arduino.h>

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  delay(1000);

  Serial.println("ESP32 Random Number Generator");
}

void loop() {
  // Generate a random 32-bit number
  uint32_t randomNumber = esp_random();

  // Print the random number to the Serial Monitor
  Serial.print("Generated Random Number: ");
  Serial.println(randomNumber);

  // Wait for a second before generating the next number
  delay(1000);
}

32 Bit Random Numbers output Screenshot

Arduino Code for Random Number Generation 0 to 100

/* Code from theoryCIRCUIT */
#include <Arduino.h>

// Function to generate random numbers within a specific range
int randomInRange(int min, int max) {
  return min + (esp_random() % (max - min + 1));
}

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  delay(1000);

  Serial.println("ESP32 Random Number Generator with Range");
}

void loop() {
  // Generate a random number between 1 and 100
  int boundedRandom = randomInRange(1, 100);
  Serial.print("Bounded Random Number: ");
  Serial.println(boundedRandom);

  // Wait for a second before generating the next number
  delay(1000);
}

0 to 100 Random Numbers output Screenshot




Leave a Reply

Your email address will not be published. Required fields are marked *