Generate true random numbers on microcontroller

Sometimes there is really a problem of how to generate true random numbers using your microcontroller. Usually computer processor or any other MCU is capable to generate a Pseudo Random Number (PRN). These numbers are generated by algorithms so called Pseudo Random Number Generators (PRNG). Everything what pure algorithm produces is predictable in some sort of level.

NOISE.GIF

There are many PRNG algorithms that generate random numbers, but there is always a defined number of iterations when random number sequence will repeat itself. Sometimes it may be acceptable. One popular way to generate pseudo random numbers is using Timers. Moreuniversal algorithm is concept of Linear Feedback Shift Register (LFSR). LSFR is a n -bit register which is initiated with non zero seed value and is clocked by shifting values to the left and loading new bit in to bit0. New bit is calculated by XOR’ing the bits of selected taps of LSFR. This method is used in rand() functions.

Usually we know simple solution of random number generation (AVR-GCC example):

//Example how to generate PRN in range (0 to 9)

uint8_t randNumber;
// Get a random number (0 to 255)
randNumber = (uint8_t) rand();

// Set number range to 0 to 15
randNumber = randNumber & 0x0F;
// Set number range to 0 to 9
if (randNumber > 9)
randNumber -= 6;

But this algorithm will always get the same numerical order as long the same seed for the rand() function is used. This is nothing more than mathematical function that cycles through a range of numbers which can be predictable. If you need really true randomness you need to find real world source that could inject some entropy. This could be any noisy diode connected to ADC. Such ramdom generators are so called Hardware Random Number Generators. They often use some microscopic phenomena like thermal noise, photoelectric effect, etc. There are complete random number generators in the market, that can be connected to PC via USB like this http://random.com.hr/products/hg400/index.html.

There is interesting reading about Random Noise Sources where as entropy source is Zener diode used. Measurements are done using PC Sound Card.

In general if you are prototyping some sort of Embedded platform with temperature sensor like AD7416 this menas that you already have a hardware random number generator. Because temperature sensor chip’s generates noise which can be used as source of entropy for your RNG. And you don’t need to connect additional devices like Zener diodes or photo cells.

One Comment:

  1. hallo..can I get an example source code for PRNG in microcontroller..thank’s

Leave a Reply

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