Home » ESP32 True random number generator example

ESP32 True random number generator example

by shedboy71

The ESP32 contains a hardware random number generator, values from it can be obtained using esp_random().

When Wi-Fi or Bluetooth are enabled, numbers returned by hardware random number generator (RNG) can be considered true random numbers. Without Wi-Fi or Bluetooth enabled, hardware RNG is a pseudo-random number generator

esp_random() description
Get one random 32-bit word from hardware RNG.

The hardware RNG is fully functional whenever an RF subsystem is running (ie Bluetooth or WiFi is enabled). For random values, call this function after WiFi or Bluetooth are started.

If the RF subsystem is not used by the program, the function bootloader_random_enable() can be called to enable an entropy source. bootloader_random_disable() must be called before RF subsystem or I2S peripheral are used. See these functions’ documentation for more details.

Any time the app is running without an RF subsystem (or bootloader_random) enabled, RNG hardware should be considered a PRNG. A very small amount of entropy is available due to pre-seeding while the IDF bootloader is running, but this should not be relied upon for any use.

Code

 

[codesyntax lang=”cpp”]

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

void loop()
{
Serial.println("-----------");
Serial.println(esp_random());
Serial.println(random(100));
Serial.println(random(1,100));
delay(1000);
}

[/codesyntax]

 

Output

Open the serial monitor
3296550307
18
3
———–
1828082797
8
21
———–
3150364294
91
92
———–
1412556175
97
92

You may also like

Leave a Comment

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.