← Teacher guide

Pick from a list

Students pick an item from a list at random with pick random 0 to length − 1, so every item can come up however long the list grows.

micro:bit Foundations · Lists · about 40 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 7 min · Investigate 10 min · Modify 8 min · Mix it up 3 min · Make 8 min · Team picker 4 min

Before class

pick random (Math) gives a whole number between two numbers. length of array (Arrays) counts the items. A random item is get value at (pick random 0 to length − 1): the length is one more than the last place, because places start at 0. Using the length means new items are included without changing anything else.

Running the lesson

  1. Predict: some students think it cycles through or never repeats. Random has no memory.
  2. Run: shake several times. Repeats happen.
  3. Investigate: Kai counts from 1; Ana's range stays fixed when her list grows. Both are fixed by 0 to length − 1.
  4. Modify: students fix Ana's 8-ball with length, then show the length.
  5. Mix it up: last lesson's chooser, traced by hand.
  6. Make: rock, paper, scissors is the same pattern with new items.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Why it is length − 1. Ask them to count the places of a 3-item list aloud from 0.

When students get stuck

What you will seeA question to askThe fix
One item never comes up.Which places can pick random choose from?Pick from 0 to length − 1, not from 1 and not a fixed number.
The minus block will not fit.Is it the round number minus block from Math?Drag Math's 0 − 0 block into the slot, then put length of array on its left and 1 on its right.
A number shows instead of the word.Is the random number inside get value at?pick random gives the place; get value at turns the place into the item.

Answers to the checks

  1. Predict: You shake this magic 8-ball twice. Can it give the same answer both times?
    Yes. Each shake picks a random place from 0 to 2, with no memory of the last one, so the same place can come up twice.
  2. Which places?: The list has 3 answers. Which places can pick random 0 to length − 1 choose?
    0, 1 or 2
    The length is 3, so it picks from 0 to 2: exactly the three places.
  3. Kai's 8-ball: Kai's magic 8-ball never says Yes, and sometimes shows nothing at all. Why?
    Places start at 0, but Kai picks from 1 to 3: he skips Yes and asks for a place that is not there
    Yes is at place 0, which Kai never picks, and place 3 does not exist. Picking from 0 to length − 1 covers every item.
  4. Ana's longer list: Ana added two answers, so the list has five. Her code still picks from 0 to 2. What happens?
    The two new answers never come up
    0 to 2 only reaches the first three places. Picking from 0 to length − 1 grows with the list.
  5. Mix it up: This is last lesson's mood chooser. choice starts at 0. You press A four times. Which mood shows after the 4th press?
    Tired
    The presses make choice 1, 2, then 3, which goes back to 0, then 1. Place 1 is Tired.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Every answer

The program as blocks
Show as code
let answers = ["Yes", "No", "Maybe", "Ask again", "Never"]
input.onGesture(Gesture.Shake, function () {
    basic.showString(answers[randint(0, answers.length - 1)])
})

Modify › How many?

The program as blocks
Show as code
let answers = ["Yes", "No", "Maybe", "Ask again", "Never"]
input.onGesture(Gesture.Shake, function () {
    basic.showString(answers[randint(0, answers.length - 1)])
})
input.onButtonPressed(Button.A, function () {
    basic.showNumber(answers.length)
})

Make

The program as blocks
Show as code
let choices = ["Rock", "Paper", "Scissors"]
input.onGesture(Gesture.Shake, function () {
    basic.showString(choices[randint(0, choices.length - 1)])
})

Change a team picker

Open task: any program that does what the step asks.

Stretch and support

Stretch: Make a dice: a list of six show leds pictures is hard to make, so use show number pick random 1 to 6 instead, and say why this one starts at 1.

Support: Give students the list and the on shake block with show string already in, and ask them only to build get value at with pick random.

Exit ticket

A list has 6 items. What should pick random choose between, and why?

0 and 5 (0 to length − 1), because places start at 0, so the last of 6 items is at place 5.

What each step is for

Where it leads

Next: the final challenge, a pocket pet that uses everything in this course.