
The STM32H5 series includes a hardware random number generator (RNG) that provides true random values, essential for cryptographic and security applications. This guide walks you through enabling and using the RNG peripheral in STM32H5 using STM32CubeIDE and HAL drivers.
In this guide, we shall cover the following:
- Introduction to random number generator
- STM32CubeIDE setup.
- Firmware Development.
- Results.
1. Introduction to Random Number Generator:
A random number generator is a device or algorithm that can produce numbers that are unpredictable and have no discernible pattern. Random numbers are useful for many purposes, such as cryptography, gaming, simulation, statistics, and more. There are two main types of random number generators: true and pseudo-random. True random number generators use physical phenomena, such as atmospheric noise, to produce randomness. Pseudo-random number generators use mathematical formulas to create sequences of numbers that appear random, but are actually deterministic. ¹²³
1.2. How STM32 Generate Random Numbers:
The hardware random number generator (RNG) in STM32 microcontrollers typically exploits the inherent unpredictability in physical processes. It often relies on the thermal noise present in semiconductor devices.
Here’s a simplified explanation:
- Thermal Noise: Microcontrollers naturally exhibit thermal noise due to temperature fluctuations at the microscopic level within the silicon.
- Analog-to-Digital Conversion (ADC): The RNG circuit samples this thermal noise using an Analog-to-Digital Converter (ADC). The continuous and unpredictable nature of thermal noise provides a source of randomness.
- Bit Extraction: The digitized noise is then processed to extract random bits. Various techniques, like Von Neumann extractor or more advanced algorithms, may be employed to ensure randomness and eliminate bias.
- Output: The generated random bits are then made available for use by software.
1.3. Features of the RNG Peripheral of STM32:
RNG main features
• It delivers 32-bit random numbers, produced by an analog generator
• 40 periods of the RNG_CLK clock signal between two consecutive random numbers
• Monitoring of the RNG entropy to flag abnormal behavior (generation of stable values, or of a stable sequence of values)
• It can be disabled to reduce power consumption
Note: Not all STM32H5 has the RNG peripheral, please refer to the datasheet of the desired MCU to see if the MCU supports RNG or not.
2. STM32CubeIDE Configuration:
We start off by creating new project with name of Internal RandomNumberGenerator. For how to create project using STM32CubeIDE, please refer to this guide here.

Once the project has been created, STM32CubeMX window will appear.
Head to Security, then RNG and enable RNG as following:

Keep every as is. No need to modify any thing.
Thats all for STM32CubeIDE. Save the project and this will generate the codes.
3. Firmware Development:
Once the project has been generated, main.c will be opened.
In main.c in user PV begin(Private Variable), declare a variable of uint32_t to hold the random number as following:
uint32_t randomNumber;
In user code begin in while loop, state generating the random number as following:
HAL_RNG_GenerateRandomNumber(&hrng,&randomNumber);
The function HAL_RNG_GenerateRandomNumber takes the following parameters:
- Instant to random number generator which is hrng.
- Variable’s address to hold the generated random.
That all for this guide.
Save the project, build and run it on your board.

4. Results:
Open debug session and add randomNumber variable to Live Expression and you should get the following:

Now, you can use the random number in your project.
Happy coding 😉
Add Comment