“Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin.” – John Von Neumann, 1951
I’ve blogged previously about the development process for my latest project, an artificial die designed for die-based Role Playing Games. When I started this project, I had several design goals in mind: I wanted it to be extremely small and easy to carry, yet have 6 buttons to “roll” each of the standard sizes of die (D4, D6, D8, D10, D12, and D20). I also wanted it to based on a Random Number Generator (RNG) implemented in hardware instead of using the built in pseudo-random number generator. As those who have been watching the Show And Tell will know, I’m proud to say that I’ve succeeded. If you’d like to see it in action, a short demo video is available here..
My first step was to learn as much as I could about the Random Generation process, so I could design my own generator. As described previously, I found an open source design that I was able to implement. In this design, the Zener Effect of a 1.22V Zener Diode is used to generate Avalanche noise, which is then amplified through a 2n3904 NPN transistor and a pair of high-speed Op Amps, then delivered to the Microprocessor as a digital input. The microprocessor implements a debiasing algorithm known as the Von Neumann corrector to take the input noise distribution, which could have an uneven balance of 0s and 1s, and turn it into an even distribution of 0s and 1s. When the conditions of the corrector are met, the new random bit is mixed into a 32 bit number. Since this is implemented within a timer interrupt that triggers every 32 microseconds, the random number is updated extremely quickly. If you want to learn more about the RNG support circuitry or the software, check out the source files for this project. The Hardware process is described on the Schematic, and the software process is documented within the source code.
To check that this device worked properly, I sampled the D20 option 250 times, and performed a Pearson’s Chi-Squared test based on the null hypothesis that the source data is uniformly distributed. Suffice it to say, it passed, and I could not reject the null hypothesis. I also borrowed the method of testing from the original designer of the RNG hardware, so if you’d like to look at the precise statistical methods, feel free to check them out here.
Because I wanted the device to be easy to use, I selected a two-digit seven-segment display, and found small tactile momentary switches to use for the UI. Since I wanted this project to be as small as possible, I decided to use Surface Mount devices for the RNG components and microprocessor and put them beneath the seven-segment display, on the underside of the board. I selected the Atmega8 to keep the LED driving simple (it has enough pins to drive both digits simultaneously, to keep the input switches on as few pins as possible (I’m using a resistor ladder, and an ADC to read the result), and to have a small package to fit within the .6” spaced rows of pins on the seven-segment display. Because I was able to place all of the components (besides the switches and display) on the underside of the board, the top of the device is clean and simple and the PCB is kept to 1.1” x 1.25”, small enough to easily attach it to a 2xAA battery pack. Since the batteries would run out in under 10 days due to the high (~10mA) quiescent current use of the ADC and RNG circuitry, a simple power switch is included to turn power to the whole device on and off. At one-off prices, all of the components can be bought for about $10. The PCB is small, so if you buy from Laen over at DorkbotPDX (Who, by the way, awesomely accepted several last-minute design file changes, on a weekend no less) it’s about $7 for 3 boards.
In short, this is a simple, easy to use artificial die. When a button is pressed, the machine generates a random number in the appropriate range, displays it on the screen for several seconds, and then turns off the display. If you use this device, you don’t have to worry about carrying around a bag of dice, finding an even rolling surface, or losing a die. And in the process, I learned about Hardware RNGs, and designed my first mostly surface-mount PCB. And again, thank you to Leon Maurer for laying a lot of the groundwork for the design of the RNG. This project is released under the CC BY/SA License, and the design files are available at my github page.