AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Fewer steps, less time

Week 28 · Unit 12: Be a problem solver · 3 lessons
3

Pick once, test often

Computing 5: Unit 12, pp. 159–170
GoalTrace a micro:bit program that picks a random number once and tests the stored value with equal-to.
New words
pick random · a block that chooses a whole number by chance between two limitsequal to · the test = that is true when two values are the samevariable · a named place that stores a valuemicro:bit blocks · the picture blocks that program a micro:bit board
Explanation

A micro:bit can pick a random number between two limits. The program should store this number in a variable, once, and then test the variable with the equal-to block. This is efficient and correct. If a program calls pick random again inside every test, each test gets a new number. Then the board may show two pictures, or none. So pick once, then test the stored value as often as needed. A variable holds one value at a time, so only one of the tests ‘mood = 1’, ‘mood = 2’ and ‘mood = 3’ can be true.

setmoodtopick random1to3show iconhappyifmood=1thenshow iconasleepifmood=2thenshow iconsurprisedifmood=3thenon logopressed
Worked examples
PressmoodTest that is trueBoard shows
12mood = 2asleep face
23mood = 3surprised face
33mood = 3surprised face
41mood = 1happy face
This is the correct mood board, shown above. On four presses of the logo the board picks 2, 3, 3 and 1. Each press picks only one number and shows one picture.
show iconhappyifpick random1to3=1thenshow iconasleepifpick random1to3=2thenshow iconsurprisedifpick random1to3=3thenon logopressed
Here is a wrong version. Each if block has its own pick random, so the three tests use three different numbers.
Three numbers pickedPictures shown by the wrong version
2, 3, 1none
1, 1, 3happy, then surprised
1, 2, 3happy, asleep, surprised
In the wrong version the three numbers can be any of 1, 2 and 3. Sometimes no picture is shown, and sometimes there are two or three. The correct version always shows exactly one.
Class activity

Dice mood board (pairs, a die, paper). Draw three faces on paper: happy, asleep and surprised. Correct way: roll the die once, write the number in a cup labelled mood (1–2 means 1, 3–4 means 2, 5–6 means 3) and then check the three tests against the cup. Wrong way: roll the die again for each test. Do each way 10 times. In how many of the 10 rounds did you get exactly one picture, each way?

Practice
1
The board picks 3, 1 and 2 on three presses of button B. What does it show each time?
setturntopick random1to3show stringGoifturn=1thenshow stringStopifturn=2thenshow stringWaitifturn=3thenon buttonBpressed
2
A variable holds a random number from 1 to 3. How many different results are possible? How many of them show a picture if the tests are for 1 and 2 only?
3
What is wrong with this program, and how can you fix it?
show iconduckifpick random1to2=1thenshow iconhouseifpick random1to2=2thenon buttonApressed
4
A program picks a number from 1 to 4 and has tests only for 1, 2 and 3. On how many of the 4 possible numbers is nothing shown?