By Artoo Maker Lab Editors Fact-checked by Hugo Lindgren Published
A dice roller is one of the best first projects after blinking an LED. It needs only a button, a few LEDs or a small display and a handful of lines of code. Better still, it raises a question that goes far beyond hobby electronics: how does a machine produce a random number at all?
The Parts and the Basic Sketch
You need an Arduino Uno or similar board, a push button, a resistor and either seven LEDs arranged like dice dots or a small seven-segment display. Wire the button to a digital pin with a pull-down resistor or use the board’s internal pull-up. In the sketch, read the button, and when it is pressed, call random(1, 7) to get a number from one to six. Then light the matching pattern.
Upload it, press the button and it works. Press reset and roll again, though, and you may notice something odd: the sequence of numbers repeats each time the board starts.
Why the Numbers Repeat
The Arduino random() function is a pseudo-random number generator. It produces a sequence that looks random but is fully determined by its starting value, the seed. If the seed never changes, the sequence never changes.
The common fix is to call randomSeed(analogRead(A0)) in setup, with nothing connected to pin A0. The floating pin picks up electrical noise, giving a different seed on most power-ups. It is not perfect, but for dice it is more than enough. For better results, some makers time the gap between button presses in microseconds and use that as extra randomness.
Where Randomness Really Matters
For a hobby dice roller, a slightly predictable sequence does no harm. In other fields it matters a great deal. Encryption keys, scientific simulations and online games of chance all depend on random numbers that cannot be guessed. Online casinos, for example, rely on random number generators that independent testing labs audit. Players browsing an australian online casino comparison often check for exactly that kind of certification before signing up. Those games are only for adults 18+, and it is wise to play responsibly with firm limits, because no RNG, fair or not, changes the house edge.
Ideas to Extend the Project
- Add a second button to roll two dice and show the total on a display.
- Log a thousand rolls over serial and chart how evenly the numbers fall.
- Compare results with and without randomSeed to see the repetition.
- Add a small buzzer for a rolling sound before the result appears.
Logging rolls is especially instructive. With enough samples, each face should appear roughly one sixth of the time. If one number shows up far more often, check your code for an off-by-one error in the random range, the most common mistake in this project.
A dice roller looks like a toy, but it opens the door to one of computing’s oldest puzzles. Build it, break it and measure it, and you will understand randomness better than most people who use it every day.
Notes on this article
- Published







