Entropy and The Arduino: When Clock Jitter is Useful

What do you do, when you need a random number in your programming? The chances are that you reach for your environment’s function to do the job, usually something like rand() or similar. This returns the required number, and you go happily on your way.

A shift register configured as a pseudo-random number generator.
A shift register configured as a pseudo-random
number generator. [by KCAuXy4p CC0 1.0]
Except of course the reality isn’t quite that simple, and as many of you will know it all comes down to the level of randomness that you require. The simplest way to generate a random number in software is through a pseudo-random number generator, or PRNG. If you prefer to think in hardware terms, the most elementary PRNG is a shift register with a feedback loop from two of its cells through an XOR gate. While it provides a steady stream of bits it suffers from the fatal flaw that the stream is an endlessly repeating sequence rather than truly random. A PRNG is random enough to provide a level of chance in a computer game, but that predictability would make it entirely unsuitable to be used in cryptographic security for a financial transaction.

There is a handy way to deal with the PRNG predictability problem, and it lies in ensuring that its random number generation starts at a random point. Imagine the  shift register in the previous paragraph being initialised with a random number rather than a string of zeros. This random point is referred to as the seed, and if a PRNG algorithm can be started with a seed derived from a truly unpredictable source, then its output becomes no longer predictable.

Selecting Unpredictable Seeds

Computer systems that use a PRNG will therefore often have some form of seed() function alongside their rand() function. Sometimes this will take a number as an argument allowing the user to provide their own random number, at other times they will take a random number from some source of their own. The Sinclair 8-bit home computers for example took their seed from a count of the number of TV frames since switch-on.

The not-very-random result of a thousand analogRead() calls.
The not-very-random result of a thousand analogRead() calls.

The Arduino Uno has a random() function that returns a random number from a PRNG, and as you might expect it also has a randomSeed() function to ensure that the PRNG is seeded with something that will underpin its randomness. All well and good, you might think, but sadly the Atmel processor on which it depends has no hardware entropy source from which to derive that seed. The user is left to search for a random number of their own, and sadly as we were alerted by a Twitter conversation between @scanlime and @cybergibbons, this is the point at which matters start to go awry. The documentation for randomSeed() suggests reading the random noise on an unused pin via analogRead(), and using that figure does not return anything like the required level of entropy. A very quick test using the Arduino Graph example yields a stream of readings from a pin, and aggregating several thousand of them into a spreadsheet shows an extremely narrow distribution. Clearly a better source is called for.

Noisy Hardware or a Jittery Clock

As a slightly old-school electronic engineer, my thoughts turn straight to a piece of hardware. Source a nice and noisy germanium diode, give it a couple of op-amps to amplify and filter the noise before feeding it to that Arduino pin. Maybe you were thinking about radioactive decay and Geiger counters at that point, or even bouncing balls. Unfortunately though, even if they scratch the urge to make an interesting piece of engineering, these pieces of hardware run the risk of becoming overcomplex and perhaps a bit messy.

The significantly more random result of a thousand Arduino Entropy Library calls.
The significantly more random result of a thousand Arduino Entropy Library calls.

The best of the suggestions in the Twitter thread brings us to the Arduino Entropy Library, which uses jitter in the microcontroller clock to generate truly random numbers that can be used as seeds. Lifting code from the library’s random number example gave us a continuous stream of numbers, and taking a thousand of them for the same spreadsheet treatment shows a much more even distribution. The library performs as it should, though it should be noted that it’s not a particularly fast way to generate a random number.

So should you ever need a truly random number in your Arduino sketch rather than one that appears random enough for some purposes, you now know that you can safely disregard the documentation for a random seed and use the entropy library instead. Of course this comes at the expense of adding an extra library to the overhead of your sketch, but if space is at a premium you still have the option of some form of hardware noise generator. Meanwhile perhaps it is time for the Arduino folks to re-appraise their documentation.

The subject of entropy and generating random numbers is one that has appeared on these pages many times. [Voja Antonic] made a in-depth study using uninitialized RAM as an entropy source for microcontrollers. If you have an insatiable appetite for understanding Linux entropy, we point you at [Elliot Williams]’ comprehensive examination of the subject.

[Arduino image: DustyDingo Public domain]

The Grooviest Random Number Generator Ever

Cloudflare is one of those Internet companies you use all the time, but don’t usually know it. Big websites you visit use Cloudflare to shore up their defenses against denial of service attacks. The company needed some truly random numbers for its security solutions, so it turned to some groovy old tech: lava lamps. In their office is a wall of 100 lava lamps monitored by cameras. The reaction of the lamps is unpredictable, and this allows them to generate really random numbers. [Joshua], a Cloudflare employee, talks about the technical details of the system in a recent blog post.

You might think this is a new and novel idea, but it turns out the LavaRnd (or maybe it is LavaRand — there’s some dispute if you read the comments below) system has been around for a while. In fact, we covered it way back in 2005. Silicon Graphics patented the system in 1996.

Continue reading “The Grooviest Random Number Generator Ever”

Generate Random Numbers The Hard Way

Your job is to create a random number generator.

Your device starts with a speaker and a membrane. On this membrane will sit a handful of small, marble-size copper balls. An audio source feeds the speaker and causes the balls to bounce to and fro. If a ball bounces high enough, it will gain the opportunity to travel down one of seven copper tubes. Optical sensors in each of the tubes detect the ball and feed data to an Ardunio Mega. When the ball reaches the end of the tube, a robotic hand will take the ball and put it back on the speaker membrane. The magic happens when we write an algorithm such that the audio output for the speaker is a function of how many balls fall down the pipes.

The above is a rough description of [::vtol::]’s art piece: kinetic random number generator. We’re pretty sure that there are easier ways to get some non-determinstic bits, but there may be none more fun to watch.

[::vtol::] is a frequent flyer here on Hackaday Airlines. Where else would you showcase your 8-bit Game Boy Photo Gun or your brainwave-activated ferrofluid monster bath? Would it shock you to find out that we’ve even covered another kinetic random number generator of his?  Fun stuff!

Follow the Bouncing Ball of Entropy

When [::vtol::] wants to generate random numbers he doesn’t simply type rand() into his Arduino IDE, no, he builds a piece of art. It all starts with a knob, presumably connected to a potentiometer, which sets a frequency. An Arduino UNO takes the reading and generates a tone for an upward-facing speaker. A tiny ball bounces on that speaker where it occasionally collides with a piezoelectric element. The intervals between collisions become our sufficiently random number.

The generated number travels up the Rube Goldberg-esque machine to an LCD mounted at the top where a word, corresponding to our generated number, is displayed. As long as the button is held, a tone will continue to sound and words will be generated so poetry pours forth.

If this take on beat poetry doesn’t suit you, the construction of the Ball-O-Bol has an aesthetic quality that’s eye-catching, whereas projects like his Tape-Head Robot That Listens to the Floor and 8-Bit Digital Photo Gun showed the electronic guts front and center with their own appeal.

Continue reading “Follow the Bouncing Ball of Entropy”

Retrotechtacular: ERNIE

Wherever you may live in the world, who do you wish to smile upon you and deliver good fortune? You may be surprised to discover that for a significant number of Brits this role is taken by someone called [Ernie].

What, [Jim Henson]’s Ernie from Sesame Street‘s famous duo Bert and Ernie? Sadly not, because the owner of a [Rubber Duckie] can’t offer you the chance of a million quid every month. Instead, [Ernie] is a computer that has been anthropomorphised in the national imagination. More properly referred to as E.R.N.I.E, for Electronic Random Number Indicator Equipment, he is the machine that picks the winning bond numbers for the Premium Bonds, a lottery investment scheme  run by the British Government.

Brits have been able to buy £1 bonds, up to 50,000 of them today, since the 1950s, and every month they are entered into a drawing from which ERNIE picks the winners. The top two prizes are a million pounds, but for most bond holders the best they can hope for is the occasional £25 cheque. Premium Bonds are often bought for young children so a lot of Brits will have a few, often completely forgotten. Prizes never expire, so if you are the holder of old bonds you should consider asking National Savings and Investments whether anything is owed to you.

The Great Grandfather of Premium Bond Drawings

The original 1957 ERNIE, now in the collection of the Science Museum, London. Geni [CC BY-SA 4.0-3.0-2.5-2.0-1.0, via Wikimedia Commons.
The original 1957 ERNIE, now in the collection of the Science Museum, London. Geni [CC BY-SA 4.0-3.0-2.5-2.0-1.0, via Wikimedia Commons.
The current ERNIE is the fourth-generation model, but our attention today is on its 1950s ancestor. In a way it’s the most interesting of the machines because it has an unusual pedigree, being a creation of the Post Office Research Station, at Dollis Hill, London. As such it came from the lab of the Colossus engineer [Tommy Flowers], and is described as being a descendant of the now-famous but then still top-secret first digital computer used by the World War Two codebreakers. It’s thus a fascinating study for the student of computer history as well as for its role in British postwar social history, because it represents the only glimpse (had they known it at the time) that the British public had of the technology that had helped them so much a decade earlier.

A significant effort was made to ensure that the draw was truly random, and the solution employed by [Flowers] and his team was thoroughly tested before each draw. The thermionic noise generated across a neon tube was sampled, and this random voltage delivered the truly random numbers used to generate the winning bond numbers. The machine’s construction is extremely reminiscent of its wartime predecessor, however it is as well to bear in mind that it owes this to the standard racking and paint used in British telephone exchanges of the day. Gone though are the octal tubes, and in their place are their more familiar miniature successors.

We have two films for you showing this incarnation of ERNIE in action. The first is a National Savings promotional film which explains ERNIE’s purpose, while the second shows us the Minister of the time starting the first draw. Believe it or not, this was a cause of major national excitement at the time.

Continue reading “Retrotechtacular: ERNIE”

Hackaday Prize Entry: Nuclear Powered Random Number Generator

Random number generators come in all shapes and sizes. Some are software based while others, known as true random number generators, are hardware based. These can be created from thermal noise, the photoelectric effect and other methods. But none of these were good enough for [M.daSilva]. He would base his off of the radioactive decay of Uranium 238, and construct a working nuclear powered random number generator.


Because radioactive decay is unpredictable by nature, it makes for an excellent source for truly random data. The process is fairly simple. A piece of old fiestaware plate is used for the radioactive source. Put it in a lead enclosure along with a Geiger tube. Then wire in some pulse shaping circuitry and a microcontroller to count the alpha particles. And that’s about it. [M.daSilva] still has to do some statistical analysis to ensure the numbers are truly random, along with making a nice case for his project. But all in all, it seems to be working quite well.

Be sure to check out the video for quick rundown of [M.daSilva’s] project. If randomness is your thing, make sure you check out entropy harvested from uninitialized RAM, and the story behind the NIST randomness beacon.

Continue reading “Hackaday Prize Entry: Nuclear Powered Random Number Generator”

Clocked 8-Bit Random Pattern Generator For a CMOS Synth

A random noise generator is pretty handy when working with music, and building one using a micro-controller can be pretty trivial. So it’s nice when someone comes along and builds it from a few analog and digital parts. [acidbourbon] built his Clocked 8-BIT Random Pattern Generator for  CMOS Synth  inspired and motivated by the recent article Logic Noise: Sweet, Sweet Oscillator Sounds by [Elliot Williams]. It’s 8-bit output can be used as a random sequencer for DIY CMOS synths.
This pattern generator is suited to to be used in combination with a 4051 8-channel analog multiplexer. But it sounds quite interesting on it’s own (best enjoyed in stereo, check out the video after the break). After building some CMOS synth circuits, [acidbourbon] moved on to make some sequencers and multiplexers which then let him to devise this random pattern generator which could be gated using a clock signal.

The basic principle is straight forward – generate noise, amplify it, apply a clock to get the gated noise output. His design choices for the various sections are well explained, based on constraints that he had to work with. Everything needs to work at 5V, but his noise generator circuit requires 12V to work. He choose to use a charge pump to generate -5V, resulting in a 10V supply, which was barely enough, but worked. A boost regulator might probably have served better to generate 12V, but maybe he already had the ICL7660 charge pump IC lying around in his parts bin. The rest of the circuit uses standard CMOS/TTL devices, and [acidbourbon] provides all of the design files for what looks like a neat, single sided PCB that can easily be made using the toner-transfer method.

Video below.

Continue reading “Clocked 8-Bit Random Pattern Generator For a CMOS Synth”