An STM32 HAL example of an electronic die/dice1.
The main goal for this example is to lure you into experimenting with the (T)RNG peripheral of the STM32 uC. The example also implements the Charlieplexing technique to reduce the pin count. Here Charlieplexing is performed in the blocking mode. It is just for the concept demonstration purposes - to deploy it in a control system you have to rewrite the code into a non-blocking one. Before we move on I must confess that my agenda is much more evil - I want to lure you into the world of unpredictability manifested by radioactive decay ☢️, quantum mechanics and chaotic systems 🎲 2
Because it's fun. Because they are more versatile - you can emulate any RPG dice in practically no time. And they are usually much more fair than an unbalanced/imperfect/unsymmetrical physical dice. "The RNG is a true random number generator that continuously provides 32-bit entropy samples, based on an analog noise source. (...) The RNG true random number generator has been tested using NIST statistical test suite SP800 rev1a (April 2010)." [RM0394].
Don't worry 🙂 Just hit Alt-K to generate /Drivers/CMCIS/ and /Drivers/STM32L4xx_HAL_Driver/ based on the .ioc file. After a couple of seconds your project will be ready for building.
- OLED: stm32-ssd1306 (MIT license)
- Monty Hall problem (Wikipedia)
- P-value, a true test of statistical significance? A cautionary note (Tukur Dahiru)
- Birthday problem (Wikipedia)
- Misuse of p-values (Wikipedia)
- The Bingo Paradox: 3× more likely to win3 (Stand-up Maths)
And now exercise your critical thinking skills
- Randomness is Random - Numberphile
- Man vs. Machine.
- How random can you be? (Ilya Perederiy)
- Benford's law and accounting fraud detection (Wikipedia)
- Almost All Random Numbers Are Actually Fake (Thoughty2)
- The Incredible Story Of Randomness (New Mind)
- Standard polyhedral dice optimized for fairness
- d20 Dice Randomness Test: Chessex vs GameScience
- Optidice (Henry Segerman)
- Charlieplexing (Wikipedia)
- NEW Arduino Uno R4 Boards - Minima & WiFi (DroneBot Workshop)
Hooked on randomness? Good! This is just the first chapter. Now test the RNG on your own. Use STM32 as USB HID keyboard to feed the data directly into an online bitstream tester that implements the NIST Statistical Test Suite for Random and Pseudorandom Number Generators such as https://mzsoltmolnar.github.io/random-bitstream-tester/. Or use UART if you are up to more than, let's say, 1000 bits - it's much faster. Then build your own TRNGs. Experiment with different sources of entropy. A Geiger counter4. A thermal noise (ADC)5. A physical dice roller with automatic face detection6. I will join you along the way with my contraptions if time allows ⌚ And first and foremost, never ever trust your gut when assessing randomness. We humans tend to be very bad at that. Successful thinking in terms of probabilities, including the probabilities of probabilities, probability density functions and Bayesian probabilities, without proper training is usually hard - explore Readings and videos for some illustration of that. Train yourself in probabilistic thinking. Funny thing is that thinking itself can be regarded as the process of using all the stats being continuously stored in your brain. For example, learning a natural language is a Bayesian endeavour.
Create your own home laboratory/workshop/garage! Get inspired by ControllersTech, DroneBot Workshop, Andreas Spiess, GreatScott!, ElectroBOOM, Phil's Lab, atomic14, That Project, Paul McWhorter, and many other professional hobbyists sharing their awesome projects and tutorials! Shout-out/kudos to all of them!
Warning
Automation and control engineering - do try this at home ❗
190+ challenges to start from: Control Engineering for Hobbyists at the Warsaw University of Technology.
Stay tuned!
Footnotes
-
Dice vs. Die – Which Is Singular and Which Is Plural? (Grammarist) ↩
-
Randomness, uncertainty, chaos and unpredictability are not synonyms if we want to be rigorous. For example, rolling dice create a chaotic system, even a single rolling die creates a chaotic system with the environment it interacts with, whose behavior is highly sensitive to initial conditions - when we roll dice we do NOT intend to exploit the fundamental unpredictability at the level of a quantum event (such as a single atom's decay). ↩
-
I wouldn't call it a paradox. The numbers on the bingo card are not uniformly distributed. The initial guess should be then that the probability of hitting a row vs. the probability of hitting a column are probably 😉 not equal. Nevertheless, it is still an example of a counter-intuitive result when the ratio of these probabilities is to be guessed. ↩
-
Arduino True Random Number Generator (Instructables) ↩
-
A true random number generator that utilizes thermal noise in a programmable system-on-chip (PSoC) by Shunsuke Matsuoka, Shuichi Ichikawa and Naoki Fujieda ↩
-
Fair Dice Tester (Pt. 1) by Gerry Chen ↩