Skip to content

Roll a die

Generate twelve numbered rolls of a six-sided die.

  • The Craft

Twelve rolls will not settle whether a die is fair. They are enough to make a small experiment you can inspect: one numbered result at a time.

Use Sinclair BASIC on a stock 48K ZX Spectrum. Begin with a new program and enter this listing. BASIC runs numbered lines in numerical order; the gaps leave room for later additions. Meet BASIC offers a slower introduction to entering and editing programs.

dice-roller.bas
  10 BORDER 0: PAPER 0: INK 7: BRIGHT 0: CLS
  20 RANDOMIZE
  30 LET b=12
 440 FOR i=1 TO b
 450 LET d=INT (RND*6)+1
 460 PRINT i;": ";d
 490 NEXT i
 750 STOP

Enter RUN without a line number. You should see twelve numbered rolls, each from 1 to 6, followed by a STOP statement report. STOP is deliberate: this first version has finished its work. The actual rolls will vary; they do not need to include every face.

Turn a fraction into a face

RND produces a pseudorandom number from zero up to, but not including, one. It follows a calculated sequence. RANDOMIZE without a number chooses a starting point using the machine’s clock; it does not turn the computer into a source of physical chance.

Multiplying RND by six gives a number from zero up to, but not including, six. INT rounds down to a whole number. The result is therefore 0, 1, 2, 3, 4 or 5. Adding one makes the six face numbers.

LET stores that result in d. Each visit to line 450 calls RND once and stores one roll; printing d does not roll again.

Could this expression produce a seven? Could it produce a zero?

Show the explanation

Neither. Before adding one, INT can produce only 0 through 5. Afterwards the range is 1 through 6. The order matters: the six positions are the result we want, not a limit we check after rolling.

Repeat a small experiment

Line 30 stores twelve in b, the batch size. FOR starts i at one. NEXT increases i and returns to the loop until all twelve iterations have run. In PRINT i;": ";d, semicolons keep the number, separator and die face together. The end of the PRINT statement moves to a new row.

Line 10 chooses a black border and paper, white ink and normal brightness, then clears the screen with CLS. Colons separate statements on the same numbered line. This is presentation setup; it does not change the roll.

RUN again. Repeats and clusters are allowed. A row of repeated faces is not evidence of a broken loop: the numbered labels show that the loop is advancing. If there are fewer than twelve labels, inspect line 30 and the FOR/NEXT pair.

For now, keep the batch at twelve so every numbered result fits on screen. Later, a fixed display will let you compare much larger batches without scrolling.

Keyboard notes

The Spectrum enters keywords using their keys rather than spelling them out. RANDOMIZE uses T in keyword mode; RND uses T in extended mode, reached with Caps Shift and Symbol Shift together. INT uses R in extended mode. FOR uses F, NEXT uses N, and TO is Symbol Shift with F. The ROM supplies keyword spacing; preserve the spaces inside quoted text.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 3, chapter 4, chapter 9, chapter 11.