An STM32 HAL example of a true random number generator using a Geiger counter.
Important
The experiment can be carried out in two ways: a totally safe one using a background radiation around us and a doped one using uranium ceramic. Both of them let you generate a stream of truly random bits. The difference lies in the bitrate they offer. In my case the background radiation was insufficient to generate, in a reasonable time frame, a stream of bits long enough for some conclusive statistical testing of its randomness.
Caution
Dope your Geiger counter with a radioactive sample if and ONLY if you know what you are doing ❗ Even if individuals can in you country legally buy samples of uranium ore/ceramic1, such materials require proper handling to be considered safe for human health. Educate yourself on the topic before you make any purchasing decisions - and it is not only about its radioactivity ☢️ but also about its toxicity 🤮
Uranium ceramic sample inside the counter stuck to the tube - hence the over standard readings:
The algorithm itself is extremely simple. As the decay events are fundamentally unpredictable it is sufficient to measure time span between four consecutive event detections and compare the length of two time intervals to generate a random bit. In my case the resolution of HAL_GetTick() proved to be sufficient to pass selected tests for randomness collected by NIST in "A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications"2. Why just selected ones? All ones that can be performed on ca. 100 000 bits. Some tests have to be performed on much longer streams to be considered conclusive. My generator operates at the level of 30 random bytes per minute. Generating of 1 000 000 bits would take ca. 3 days. Doable but still not accomplished at the time of writing. And this something definitely in my mind - notice the Winbond W25Q128 in the photo 🙂 Two tests require even more bits than that, e.g. 10 000 000 bits are recommended3.
The low bitrate of TRNGs is not something uncommon. They can be used as an entropy source for PRNGs that can offer higher bitrates4.
Tip
If the resolution of 1 ms is not sufficient in your case, go for a timer in the input capture mode.
Don't worry 🙂 Just hit Alt-K to generate /Drivers/CMCIS/ and /Drivers/STM32G4xx_HAL_Driver/ based on the .ioc file. After a couple of seconds your project will be ready for building.
- OLED: stm32-ssd1306 (MIT license)
- RealTerm: Serial/TCP Terminal - check the Capture tab 🛠️
- Geiger counter
- Sound detector (for a non-invasive integration)
- Random Number Generating Geiger Counter (SparkFun)
- Geiger–Muller tube (Wikipedia)
- Arduino True Random Number Generator (Instructables)
- FNIRSI GC-01 [IMPORTANT PRODUCT UPDATE] (before you make your purchasing decisions) (Project 326)
- The Perfect Cheap Geiger Counter? (Project 326)
- How to predict random numbers (AlphaPhoenix)
- FNIRSI Firmware Upgrade Review (Project 326)
- Alpha particle (Wikipedia)
- Beta particle (Wikipedia)
- Gamma ray (Wikipedia)
- X-ray (Wikipedia)
- Uranium Radiation Properties (WISE Uranium Project)
- WISE (World Information Service on Energy)
- Radium Girls & the Failure of Unregulated Capitalism (Meromorphic)
- Bell's theorem (Wikipedia)
- A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications (NIST)
- NIST Randomness Testsuit (Python)
- The NIST Statistical Test Suite (Euphoria)
- NIST Statistical Test Suite (C)
- NIST SP 800-22 Statistical Test Suite (Python)
- NIST Statistical Test Suite (C)
- NIST-Tests (MATLAB)
- NistRng (Python)
- ENT — Fourmilab Random Sequence Tester (C, HTML)
- NIST randomness test suite (JavaScript)
- EntropyAssessment (C++)
- nist-sts-ml (MATLAB)
- A variant of NIST SP 800-22 revison.1a test suit (C)
- TRNG-Test-Suite (Python)
- Random Bitstream Tester (JavaScript, HTML)
- Random Bitstream Tester (online tester)
The last one is perfect for newbies - you just ctrl+c ctrl+v the bitstream and hit the button. Note that some tests require at least one million of bits to be conclusive. I generated ca. 100 000 bits. Half of the tests are flagged as erroneous due to the insufficient number of bits. The other half is passed.
I will stop tests here to proceed to other sources of entropy. Some of them happen to be quite attractive visually5 - I have to build one using STM32 (with HASH and DCMI peripherals) and OV2640. An ESP32 version has just been completed in the meantime of editing this README.md and it passes all the NIST tests without any lava lamps, just by staring at the celling. Apparently the CMOS image sensor noise does the necessary job. Nevertheless, I have to investigate it further before I can give any conclusions on it - saying anything with certainty about the randomness can be tricky 😄
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
-
Random Bit Generation (National Institute of Standards and Technology) ↩
-
NIST random excursion results (StackExchange) ↩
-
Hardware random number generator (Wikipedia) ↩
-
The Randomness Problem: How Lava Lamps Protect the Internet 🌋 + Method for seeding a pseudo-random number generator with a cryptographic hash of a digitization of a chaotic system ↩