Hardware random generator

~ 9 Dec 2012, 16:09

I'm getting better at electronics :) When I decide to build something, it now takes me around six hours to complete it. This weekend was in a I'm-somewhat-sick-n-bored mood, so I decided to build a random generator.

And not just a random generator, but an arcade monster! With ridiculously big indicators and buttons! Blinking brightly and stuff...
It would be utilized in board games like The Settlers Of Catan, where you need to throw a pair of dice and sum their roll. In fact, the generator just "rolls two dice", and a jumper determines what to do with the outcomes.

Hardware random generator

Computers usually employ algorithms like LCPRNG (or more sophisticated), with the side effect that whenever a "supposedly random" lottery draw is done on a computer, you'd have a very hard time convincing the average joe that the drawing isn't manipulated :) Luckily, having a more close-to-the-metal access to the hardware allows you to code something like this:

	while not keypressed() {
	return (counter MOD 36);

What do we have here? A counter is incremented extremely fast (thousands and millions times per second). When the button is depressed, the counter immediately freezes at its last value, which is used to determine how did "the dice roll".

Now, imagine highway driving. And in additional to the usual per-kilometre signs, the asphalt is ruled finely, into metres and centimetres. Each centimetre tick has a number associated, and the numbers increase cyclically: 0, 1, 2, 3, ..., 34, 35, 0, 1, 2, ... While driving, you throw a steel ball out of the car. The ball lands somewhere and you take the number, associated with the closest centimetre tick. This is roughly what the code above does - the outcome is essentially random. Then one has to apply some 1:1 mapping between the 36 possible pair-of-six-sided-dice rolls and the numbers, and we're ready.


Here I'm testing some possible layout configurations. I got the huge button from SparkFun.
Image Image


The mandatory breadboard design phase:

Here I put a miniature counterpart of the big red button:


I swore that I'd never use a perfboard again, right? But this project would've been an overkill to do with a PCB, so I used something simpler: a stripboard (a.k.a. Veroboard). This is a prototyping board, which has parallel copper-clad strips. The elements are usually placed perpendicular to the strips, which leads to a characteristic order in the design - all paths are straight (in coherence with the inner peace of someone who grew up in Manhattan or Stara Zagora!).

My first job was to drill four holes with my new toy:

In retrospection, I wonder how I'd lived without a Dremel :)

Soldered some sockets:
Image Image

The strips in these these boards are very long, but are easily teared with a simple tool, so one horizontal may be used to connect several different groups of components. Here: we have to isolate the two sides of the MCU pins, and same for the indicators:


On the plus side for stripboards are: less soldering, very few custom connections, and error tolerance. If you mess up something, it can usually be fixed without desoldering. Run the tool to isolate the wrong part and reroute it somewhere else. On the other side: be careful with soldering, it's easy to introduce tiny "whiskers" of solder, that inadvertently connect neighbouring strips. I had two errors of this kind and debugged them for quite some time...

Filling up with resistors:

...and elements:

Please, install software:

Visibly progresses to near-finished:
Image Image

I decided to put the 7-segment indicators on sockets - they could be soldered directly, too, but that would make the huge space below them unusable.

Finishing touches

Test blink on the segments...:

... and some test digits...:

I imagined the finished thing to be like this:

However, I didn't like the power switch. It's big, and accessing it conveniently would've required drilling a big hole, and would need some redesign due to space constraints. However, I had these cute toggle switches...

So I put into action the other new toy:

Seemed like the drill bit was rotating lazily, but it drilled into the plastic as if it was butter...

Image Image


The timer is at 6 hours and a half after the first photo. Here's how it looks like with the lid closed:

Please, press the button:

Here, you're lucky:

And an useless sign:
something like the electrical equivalent of the code i++; /* incrementing i by one */

Here's it in action: YouTube

In the end, I wanted to make a random generator, but it turned like a lot like a timer-activated bomb (or probably has the looks that Hollywood formed in my imagination).

In conclusion



The C source code is available here.

I'm looking forward to the upcoming holyday season, there would be a lot of Catan, and I'm impatient to test my generator :)


#1 by Иван, posted on 30 Jan 2013, 08:56

Супер е станало! +++ за огромния бутон - така трябва да се правят аркадните машини :)

Само 2 забележки:
- при теб разпределението на случайни числа има линейна характеристика (random 0-35), а при реално хвърляне се получава bell curve. Това може много лесно да го елиминираш ако просто вземеш 2 случайни числа от 1 до 6 и ги събереш (може би от bouncing-a на бутона)
- ползваш ли динамична индикация за сегментите? Доста ток ще ти спести и батериите ще живеят по-дълго.

#2 by anrieff, posted on 31 Jan 2013, 00:26

1) Сбора от две числа 1-6 не е камбанка, а си е тъп триъгълник. Иначе правя точно това - 0-35 интервала го ползвам само за да генерирам две независими случайни числа от 1-6 (x/6 + 1 и x%6 + 1). По-добре е, вместо да разчитам на несигурни неща като шаването на бутона
2) То най-големия консуматор е лампичката в бутона :) Индикацията не е динамична, но съм сложил един MOSFET на катода на индикаторите, с който ШИМ-вам яркостта, като минат 10 секунди неактивност. Реално не е толкова зле. Пък и нали батериите са презареждаеми...

#3 by Бойко, posted on 9 May 2014, 20:39

Абе направо ми напиши кои числа ще се паднат от тотото на 5/35 и после ще ти дам половината пари хахахахахх :):):)

Contact: (Link to your blog/website/e-mail; not obligatory)
Your comment:

Calculate: fоrty-one minus thirty = (type the answer in digits)


Valid XHTML 1.0 Strict