Skip to content
Game 4 Unit 1 of 6 1 hr learning time

A Die Roll

Tally begins with a single die. You met RND in Oracle picking answers; here the same command rolls a 1 to 6. Roll it ten times in a loop and watch the faces fall — the raw material the whole game is built from.

17% of Tally

Everything in Tally is built from one thing: a die roll. You already met RND in Oracle, where it picked an answer at random. Here it does a job you can check by eye — it rolls a die. So start by rolling one, ten times over, and watching the faces come up.

10 PRINT CHR$(147)
20 FOR I=1 TO 10
30 D=INT(RND(1)*6)+1
40 PRINT "YOU ROLLED:";D
50 NEXT I
A C64 screen listing ten rolls: YOU ROLLED: 2, YOU ROLLED: 1, YOU ROLLED: 5, and so on.
Ten rolls of one die. Every face from 1 to 6 turns up, none favoured — the fairness you'd expect, and the starting point for everything that follows.

The roll is line 30: D = INT(RND(1)*6)+1. Take it apart from the inside. RND(1) gives a fraction between 0 and 1. Multiply by 6 and you have a number from 0 up to (just under) 6. INT throws away the decimals, leaving a whole number from 0 to 5. Add 1 and you have 1 to 6 — a die. This little formula, INT(RND(1)*6)+1, is the way to roll a die in BASIC, and you'll reuse it everywhere.

The FOR I = 1 TO 10 loop around it rolls ten times so you can see the spread in one go. Each face turns up about as often as any other — a single die is fair, every value equally likely. Hold on to that, because in the next unit two dice together behave nothing like it.

Try this

  • Roll more. Change 10 to 30 and tally the faces by eye. The more you roll, the closer each face creeps to one-in-six.
  • A different die. Swap *6 for *20 and add 1 — now you're rolling a twenty-sided die, 1 to 20. The formula stretches to any number of sides.

What's next

One die is fair — every face equally likely. In Unit 2 you roll two dice and add them, and the totals are anything but even: some come up far more often than others.