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.
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
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
10to30and tally the faces by eye. The more you roll, the closer each face creeps to one-in-six. - A different die. Swap
*6for*20and 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.