The Making of Digital Dice

Idea & art by Castpixel & JO3RI

Code by JO3RI

I wanted to make something simple, that could replace real dice. Our kids tend to lose every dice of every boardgame they have. Also the older ones are constantly picking the youngest one for not rolling the dice correct. Having a dice emulator would solve all my problems.

I talked to Castpixel and she mailed me a mockup of a dice simulator, not only using the usual 6 sided dice, but with a selection screen where you can choose between 4, 6, 8, 12 and 20 sized dice. This was even better, than what I had in mind (just 2 normal dice).

So I started coding. This simulator is actuall not so hard to understand. The only thing we need is a randomizer. That is something Arduino (Arduboy) has build in. The only problem is, it isn't real random. Reboot the device and you get the exact same randomness ... But that has been fixed in the arduboy libraries by using the function:

arduboy.initRandomSeed();

If you call this function once, random is beeing seed with the fluctuation of temperture, voltage reading and time in microseconds since boot. At least this makes that on every boot, you have a different random sequence. If the random sequence is any good, is hard to tell. :) Now that we have our randomgenerator, we just need to choose 1 up to 5 dice out of 5 different dice types. In an array we define the upper bound of each dice type:

byte diceMax[] = {5, 7, 9, 13, 21};

When the player sets his currentDice - 1 (-1 because computers start at 0, people start at 1) and the amountOfDice we can use the random generator to get the results. We generate a number for all 5 dice, even if the player has chosen a smaller amount. And as long as the dice are "rolling", we generate a new number for every dice, at the speed of 16000000 times per second. (16 Mhz). It will actually be lesser, beacuse the Arduboy has to do other things too, like showing the images, listening to button input ...

rollingDice[i].result = random (1, diceMax[currentDice - 1]);

Now random uses 1 as the lowest number and diceMax[currentDice - 1] as the upper bound (the number itself not included). For every dice type we have the following bounderies:

D4 = 1, 5 meaning romdomly picked 1, 2, 3 or 4

D6 = 1, 7 meaning romdomly picked 1, 2, 3, 4, 5 or 6

D8 = 1, 9 meaning romdomly picked 1, 2, 3, 4, 5, 6, 7 or 8

D12 = 1, 13 meaning romdomly picked 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 or 12

D20 = 1, 21 meaning romdomly picked 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 or 20

Once the player presses the button to stop rolling the dice, the numbers are no longer changed and we get the result. We have also added the sum of all the invidual dice shown in an nice animation.

By community request we decided to add in a coin flip too. You can only flip 1 coin, but that should just do it. From now on, you can use Dice of Fate to make all those hard choices for you.