Winter Bash is a festive celebration of Stack Overflow and Stack Exchange communities. People compete to be awarded crazy hats for completing random, and often undisclosed actions. I've been involved in building a lot of this year's celebration, which will start soon and will be kept secret until then.

Last year we had a hat referencing "the dress", which at the time was a hugely popular meme. The hat that could be won would be black and blue or white and gold depending on the user receiving it. Well, a little known secret is that this was achieved by using looking up the bit corresponding to each user's account id in a file, and that file was an actual image of the dress. Pretty neat, eh?

This year we also needed a source of "replicable randomness". While we could have used the same means, why not over do it? So I took an afternoon off and built a hardware RNG. Only the randomest bits for Stack Overflow! This post talks a bit about how I did it.

The circuit

The first step was to find a plan for a hardware RNG that was simple enough, and with common enough components, that I could just build it with my existing stash of parts. What I found was an extremely simple circuit design based on avalanche noise in a reverse-biased PN junction. In other words, two transistors are connected the wrong way and oscillate randomly. A third transistor amplifies the noise. Finally the random noise is sent to an Arduino port, digitized and then cleaned up.

Here's what the circuit looks like.

It's amazing that such a simple circuit could output quantum randomness, at least to me :-)

Here's the bill of materials if you want to build it yourself:

Label Part Type
C1 Electrolytic Capacitor, 10µF
C2 Ceramic Capacitor 0.1µF
Part1 Arduino Uno (Rev3)
Q1-Q2-Q3 3 × TRANSISTOR 2N2222
R1-R2 4.7kΩ Resistor
R3 10kΩ Resistor
R4 1.5MΩ Resistor

All was assembled on a mini bread board and the (untidy) result was this.

Before we proceed further, I want to make it clear that in order to build this, I followed the instructions of another blogger, Rob Seward. All kudos are to him, not me!

The output

I've hooked up my cheap Hantek 6022 USB scope to ground and A0, and here's what the output looks like: a randomly oscillating voltage, between ±100mA.

The software

The software, also written by Rob, implements two algorithms to remove bias from the source. A XOR filter an a Von Neumann randomness extractor. Both are simple state machines that are well adapted to Arduino's C variant.

The output is finally sent to the serial port as a series of zeros and ones, and then converted to an actual bin file by this simlple linqpad script.

And there you have it! Now we have the best possible hardware generated random bits to assign winter bash hats to Stack Overflow users, nicely packaged in a .bin file so we can replicate the exact same sequence on all platforms!

Yes, the irony of this post does not escape me.


A software engineer & Stack Overflow alumnus in London. I write about software development, coding, architecture and team leadership. I also speak at conferences worldwide.

About me

Follow me on Twitter

Gleanings

No, I Don’t Want to Subscribe to Your Newsletter
The Bureau of Programming • Feb 25, 2017

No, I don’t want to subscribe to your newsletter. No, I don’t want to complete a short survey. And no, I don’t want to become a member.

Read more…