Making true random numbers with radioactive decay

I plugged my Geiger counter’s audio cable into my oscilloscope just for kicks the other day and saw ~9V pulses coming out when it occurred to me that I could easily read those into an Arduino or Raspberry Pi or ESP8266 microcontroller and respond to them. As a demo, I made a hardware random number generator (HRNG) out of a esp8266.

The project

While we all know radioactive decay is random, converting random clicks into random numbers may not be immediately obvious. One great way of doing it is to use the timings between pulses as a random variable and compare the length of time between two subsequent pairs of pulses. If the length of the first gap is bigger than the length of the second, you emit a 1. Otherwise, you emit a 0. When you have enough bits to build an integer (or whatever kind of number you’re seeking), you emit the fully-assembled bitstream. Hotbits has a good explanation here.

First step as usual was to step the pulses from the Gieger counter down to around 3.3V for the ESP8266’s GPIO digital input pins. For this, as usual, I just made a little voltage divider out of 2 resistors. I grabbed them out of the grab bag until I had the ratio right, ending up with 22 kOhm and a 10 kOhm one. This brought pulses from around 7V peak down to an appropriate range.

For coding, I used ESP8266 GPIO interrupts. I made an interrupt that fires when the GPIO pin is RISING from LOW to HIGH (e.g. when a pulse is coming in). Here’s how it looks:

I’m running a pulseCount that increments from 0 to 3 and then loops around each time a pulse comes by (triggered by flag). The 1st and 3rd pulse reset the timePrev while the 2nd and 4th pulses compute the two durations. Once 4 pulses have come through, I have 2 durations that are ready for comparison.

You only ever want to do relatively simple things in interrupts, so that’s all that does. In the main loop, there’s a function that monitors for the flag being set by the interrupt and it does the rest of the work. It waits until all 4 pulses have been received and then it compares the two durations and sets the next bit in our bits variable. Once we have enough bits, it emits the final fully assembled number and starts over.

That’s it. Fun little project for sure.

Here’s a histogram of the results after running for a few minutes.

Some radiation-derived random numbers

A file full of numbers from it is available here:

I will probably use this Geiger counter interface for more stuff later.

Leave a Reply

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