Skip to content
Game 5 Unit 1 of 5 1 hr learning time

Rolling Dice

A die is one expression you already know; a fistful of dice is that expression inside a FOR loop. Roll five at once and watch the numbers vary.

20% of Dice Roller

A die roll is one expression you have used since Lucky Number: INT (RND * 6) + 1, a random whole number from 1 to 6. Rolling one die is barely a program. The interesting version rolls a handful — and you already know the tool for "do this five times" from Meet BASIC: a FOR loop.

  10 BORDER 0: PAPER 0: INK 7: CLS
  20 RANDOMIZE
  60 CLS
  90 PRINT "Rolling 5 dice:"
 100 PRINT
 110 FOR i = 1 TO 5
 120 LET d = INT (RND * 6) + 1
 190 PRINT d; " ";
 250 NEXT i
Black ZX Spectrum screen reading Rolling 5 dice: above a row of five numbers, 1 6 6 6 1
Five dice in one run — the FOR loop rolls and prints each in turn.

One formula, then a loop

INT (RND * 6) + 1 is the same shape as Lucky Number's INT (RND * 100) + 1, with a smaller range. The pattern — INT (RND * range) + base — covers any die you like: a coin is INT (RND * 2) + 1, a card (1–13) is INT (RND * 13) + 1. Line 120 rolls one die into d; the FOR i = 1 TO 5 around it (lines 110–250) runs that roll five times, and line 190 prints each result with a trailing space so they sit in a row.

Nothing here is new — the range formula is from Lucky Number, the FOR loop from Meet BASIC. What's new is why you'd loop a random roll: to gather enough of them to see a pattern.

Is it fair?

Run it a few times. Five dice, five numbers, different every run. But is each face equally likely? Five rolls cannot tell you — nor can fifty by hand. To see whether a die is fair you need hundreds of rolls, counted accurately, and that is exactly the kind of tedious, repetitive work a loop was made for.

Next: roll a hundred, and keep score.