Dice
Random(1, 6) rolls a die and gives a whole number from 1 to 6.
This lesson rolls two dice, and shows why two dice are not the same as one
roll from 2 to 12.
Contents
button Held -> chip roll
chip text -> decal Text
Code
@left in roll: exec
var a: int = 1
var b: int = 1
@right out text = "${a.Value} + ${b.Value} = ${a.Value + b.Value}"
on roll {
a = Random(1, 6)
b = Random(1, 6)
}
Random(min, max)gives a whole number frommintomax, and both ends can come up:Random(1, 6)gives 1, 2, 3, 4, 5 or 6.- Each
Randomis a separate roll.aandbget two different rolls, so they can match or not. Randomis a step, like a variable write, so it runs in a handler. The sign readsaandbfrom their variables.
Press the button many times. Which total comes up most often? Would
Random(2, 12) give totals in the same way?
Run
- Compile the chip and paste it in-game. Place a Button and a brick with a Text Decal.
- Wire the Button’s Held to
roll, andtextto the Text Decal’s Text. - Press the button and watch the totals. 7 comes up most. 2 and 12 are rare.
Under the Hood
Open the chip. Each Random(1, 6) is one Random gate, with 1 typed into
Min and 6 into Max. Its Exec input runs it, and it puts out a new
number on Output each time. The two Random gates run one after the other
in the chain, each followed by a Set Variable.
There are 36 ways two dice can land, and six of them add to 7 (1 + 6, 2 + 5,
and so on), but only one adds to 2. Random(2, 12) has no such pattern.
Each of its 11 totals is as likely as the others.
A Random outside a handler has nothing to run it:
@left in roll: exec
@right out face = Random(1, 6)
Error: [WS007] exec call 'Random' outside an exec context (pass exec = ... to override)
Move the roll into on roll { ... } and keep the result in a variable, as
the lesson’s code does.
Modify
-
Flip a coin.
Random(0, 1)gives 0 or 1. StoreRandom(0, 1) == 1in aboolvariable and showHeadsorTailswithif heads.Value then "Heads" else "Tails", which gives the first text whenheadsis true and the second when it is false. -
Count the sevens. Roll both dice into one
let, and keep two counts:@left in roll: exec var rolls: int = 0 var sevens: int = 0 @right out text = "${sevens.Value} sevens in ${rolls.Value} rolls" on roll { let total = Random(1, 6) + Random(1, 6) rolls += 1 if total == 7 { sevens += 1 } }A
letis one roll, however often you read it, sototalis the same number in theifas on the line above. Over many rolls, about one in six is a 7. -
Roll a twenty-sided die,
Random(1, 20), and light a Point Light on a 20.
Make
Make a doubles game. Each press rolls two dice and shows them. Doubles (both dice the same) light a Point Light. Three doubles in a row light a second light, and any roll that is not a double resets the streak.
button Held -> chip roll
chip text -> decal Text
chip double -> light1 Enabled
chip streak -> light2 Enabled
Good to Know
Random(1, 6)includes both 1 and 6.Random(0, 6)would give seven different numbers.- Every
Randomcall rolls again. To use one roll in two places, roll it once into aletor a variable and read that. - Each roll is new. The dice do not remember earlier rolls, and a 6 is as likely right after a 6 as at any other time.
See Random in the reference.
Next: Random Colors.