I built a hardware RNG for Christmas

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.


Hi, I'm Marco Cecconi. I am the founder of Intelligent Hack, developer, hacker, blogger, conference lecturer. Bio: ex Stack Overflow core team, ex Toptal EM.

Read more

Newest Posts

What can Stack Overflow learn from ChatGPT?

Stack Overflow could benefit from adopting a using conversational AI to provide specific answers

Read more
Fan mail

Multiple people with my name use my email address and I can read their email, chaos ensues!

Read more
Intelligent Trip

After years of building, our top-notch consultancy to help start-ups and scale-ups create great, scalable products, I think it is high time I added an update to how it is going and what's next for us.

Read more
Guest blog: Building, in partnership with communities by Shog9

A lesson in building communities by Stack Overflow's most prominent community manager emeritus, Shog9

Read more
Can you migrate a company from on-premise to remote-only?

Some lessons learned over the past 8 years of remote work in some of the best remote companies on the planet

Read more

Gleanings

And the Most Realistic Developer in Fiction is...
Julia Silge • Mar 28, 2017

We can say that Mr. Robot is having a moment. The main character was one of the top choices and thus is perhaps the most/least realistic/annoying/inspiring portrayal of what it’s like to be a computer programmer today.

Read more…