Worked Example: A Digital Dice Roller
โ† All articles

Worked Example: A Digital Dice Roller

5 min

A free-running source of 'random' numbers

A linear-feedback shift register driven continuously by a fast CLOCK cycles through its states so quickly relative to human reaction time that whatever value it holds the instant someone presses a button looks effectively unpredictable, even though the whole thing is completely deterministic underneath.

Freezing a value on button press

Feed the LFSR's SHIFT4 outputs into a REG4 (a register that loads on every clock edge) and clock it from the pulse of an edge detector watching a debounced button โ€” so the register captures and holds whatever the LFSR's state happened to be at the exact instant of the press, ignoring the LFSR's continued cycling until the next press.

LFSRREGbutton (clock)RemapSEG7

Mapping 4 bits onto six dice faces

An LFSR's 4 output bits span 0โ€“15, but a die only has six faces. A small comparator-based remap โ€” subtract 6, or 12, whenever the value is 6 or greater, or 12 or greater โ€” folds the range down to 0โ€“5, and adding a fixed 1 turns that into the familiar 1โ€“6. Feed the result into a SEG7 block to display it.

Try it yourself

Build the full chain in the circuit editor โ€” free-running LFSR, debounced button, a register clocked by the edge-detector pulse, range remap, SEG7 display โ€” and press the button repeatedly to confirm the displayed digit changes unpredictably but always lands between 1 and 6.

Build a dice roller in the circuit editor โ†’