On Hackster, you get more that 1500 hits on articles, where random functions are used, but there are only four about presenting random number generators. If you ever have used a random function, probably it was a PRNG, meaning a Pseudo-Random-Number-Generator, giving the same sequence whenever you start your computer. See examples 1 and 2.
The opposite is a TRNG, this is a True-Random-Number-Generator. As you know, digital computers are working very precisely and never give random results. To obtain true random numbers, you need to access some kind of analog components. Actually, inside an ATMEL microcontroller you can find two kinds of analog components:
- the analog-to-digital converter
- the watchdog timer
It is often recommended to use an unused analog pin as input to generate random data. If you do not have a spare analog pin in your project you can even access the internal temperature of an ATmega328 (do not convert the raw value to °C or °F). See example 3.
The ATMEL doc "AVR132: Using the Enhanced Watchdog Timer" says "... the WDT is independent from the rest of the system. It has its own internal 128 kHz oscillator, which runs as long as one of the WDT operating modes is enabled. This ensures safe operation even if the main CPU oscillator fails." So the clock of the 128 kHz oscillator is in now way synchronized with the main CPU clock. So this also can be used to generate unpredictable data. See example 4.
To display the results, 16-bit values were requested from each random generator and plotted as a small square in the corresponding RGB color. If you had a better display, pictures could look like this:
Some fourty years ago, there was a proposal for a DIY random generator using only elementary mathematical operations that were available in the BASIC language (see attachment 1). As it is a very simple implementation, the patterns will repeat after 1024 samples.
Watch the squares indicated in white where equal patterns can be detected easily.
Example 2: The standard random functionThis is the standard random function available in the Arduino IDE.
Wherever you look, you will not find any repeating patterns.
Example 3: using analogReadThis version is based on the analogRead function of the Arduino system. It works on all analog inputs, given that this pin is available and not connected to anything. You also can use reading the internal temperature. This option is not available using the the ATmega2560.
No repeating patterns again.
Example 4: using the watchdog timerAs noted above the clock for the watchdog and the clock for the CPU are completely independent from each other. So you can have an interrupt invoked by one of them and read the timer value of the other.
All four versions were put together in one sketch. To select one of them you have to
#define GEN <version>
The sketch requires that you use a TFT with 160x128 pixels to show the results. The sketch also prints the time required to produce the random data on the Serial monitor. The pseudo random numbers are calculated much faster than the true random ones. See attachment 2.
Some Explorations using the Watchdog TimerAs using the watchdog timer for such a purpose, it was interesting to compare two square waves, one produced by the watchdog timer and another one produced in the conventional manner, trying to set it approximately to the same frequency. But whatever you do, as time goes by, they do not stay in phase to each other. Here, the loop function acts as a dual-beam oscilloscope:
or check it using a traditional scope:
See attachment 3.


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)

Comments