Shake-to-roll dice

Shake the micro:bit to roll a number from 1 to 6, then roll 60 times and test whether the dice is fair.

Go to the code ↓

Lesson at a glance

45 to 60 minutes · Grades 4 to 6

maze.education/microbit/lessons/dice

Grades
Grades 4 to 6
Time
45 to 60 minutes
The CAPS gives Coding and Robotics 2 hours a week (Coding and Robotics CAPS, Grades 4 to 6, page 22).
Level
Getting started
A good first or second micro:bit lesson. No coding needed before.
Board
Any micro:bit
Code with
MakeCode and Purple Mash blocks, with Python as an extension
CAPS
Mathematics, Coding and Robotics
Aligned: Grade 6, Term 2 · Where it fits

Learning intentions

Learners will learn to:

  1. Use a variable and a random number to make a digital dice.
  2. Record the outcomes of repeated rolls in a tally table.
  3. Compare the results with what a fair dice should give.

Success criteria

Each learner can say:

  • I can explain what pick random 1 to 6 does, and why the number is kept in a variable.
  • I can build a dice that shows a number from 1 to 6 each time I shake it.
  • I can record 60 rolls in a tally table.
  • I can compare our results with about 10 of each number, and say whether the dice seems fair.

Competencies

From the Coding and Robotics CAPS places this lesson cites.

  • R.6Mimic the operations of a robot

Key words

random
Chosen by chance, so you cannot predict it.
variable
A named box in the computer's memory that holds a value.
outcome
One possible result, such as rolling a 4.
tally
Marks in groups of five used to count results.
fair
Every outcome has the same chance of happening.
IPO table
A plan with three columns: the input, the processing and the output.

What you need

  • One micro:bit per pair, with a USB cable and a battery holder
  • A computer with Chrome or Edge for each pair
  • A dice per pair (a real one, or one folded from a paper net), and a tally table with rows for 1 to 6

From our shop: BBC micro:bit V2 Go, BBC micro:bit V2.21 Next Gen Classroom Pack

Safety

  • Shake the micro:bit with the battery holder held firmly in the same hand, so neither flies off.
  • Use the battery holder with two AAA batteries. Never mix old and new batteries, and take the batteries out before the kits are packed away.
  • Unplug the battery holder by holding its plug, never by pulling the wires.

The lesson plan

One lesson of 45 to 60 minutes in three parts: introduction, lesson development and consolidation. The CAPS gives Coding and Robotics 2 hours a week, so it fits one of the week's lessons. Times are a guide.

  1. Introduction

    5 to 10 minutes
    Retrieval practice

    What are two inputs the micro:bit can sense? (Buttons A and B, a shake or a tilt, light and temperature.)

    Problem statement

    The school's board-game club keeps losing its dice under the tables at aftercare. Build a dice on the micro:bit that cannot roll away, and test whether it is as fair as a real one.

    1. Roll a real dice a few times. Ask: can anyone say what comes next? Why not? Agree that the outcome is random.
    2. Read the problem statement together. Ask: if we roll 60 times, how many sixes would you expect? Write the predictions on the board.
  2. Lesson development

    35 to 40 minutes
    1. Unplugged first. Pairs roll a real dice 30 times and tally the results. Keep the tables for the consolidation, to compare with the micro:bit dice.
    2. Plan on paper first. Pairs fill in an IPO table for the dice: what goes in, what the micro:bit does with it and what comes out. Then they write the algorithm as numbered steps.
    3. Predict. Before anyone runs the program, each pair writes down what they expect to see after three shakes, and whether the same number can come up twice in a row.
    4. Build in blocks. Pairs share one computer. The driver builds the dice from the steps on the Blocks tab; the navigator holds the paper plan, reads out each step and checks each block against it. Swap roles halfway, after step 4. Point out the variable roll: it holds the number until the next shake.
    5. Test and debug. Test in the simulator with the SHAKE button. Did it match your prediction? If it shows 0 or never changes, find the fault in the common mistakes, then download it.
    6. Pairs roll the micro:bit dice 60 times: one shakes, the other keeps the tally. Total the class results for each number on the board.

    Plan on paper: a model answer

    In pairs, fill in an IPO table for the dice, then write its algorithm as numbered steps.

    Algorithm
    1. Wait for a shake.
    2. Pick a random number from 1 to 6.
    3. Store it in roll.
    4. Show roll on the display.
    5. Wait for the next shake.
    IPO table
    InputProcessingOutput
    The micro:bit is shakenPick a random whole number from 1 to 6 and keep it in the variable rollThe number in roll shows on the display
  3. Consolidation

    5 to 10 minutes
    1. Compare the class totals with the real dice tallies: are the six numbers roughly equal? Did anyone predict well?
    2. Ask: why do 30 rolls look uneven, while the class total looks fairer?
    3. Back to the problem: is the micro:bit dice fair enough for the board-game club? How could we find out for certain? Learners check themselves against the success criteria.

Build it, step by step

Pairs build the program in blocks, following the numbered steps, with the same program as text beside them to check against or paste. Test in the simulator before sending it to a board.

A shake sets roll to a random number from 1 to 6 and shows it. Six tiles show 1 to 6, each a 1 in 6 chance, and a chart marks about 10 of each in 60 fair rolls.
Each number from 1 to 6 has the same chance, so 60 rolls of a fair dice should give about 10 of each.

These are the MakeCode steps. The other routes are on the lesson's web page.

Build it in MakeCode blocks

  1. Start a New Project called Dice.
  2. From Variables, click Make a Variable and call it roll.
  3. From Input, drag on shake onto the workspace.
  4. From Variables, drag set roll to 0 into on shake.
  5. From Math, drag pick random 0 to 10 into the 0 and change it to 1 to 6.
  6. From Basic, add show number under it, and from Variables drag the round roll block into it.
  7. Test with the simulator's SHAKE button, then Download.
The same blocks as text (to paste into MakeCode)
let roll = 0
input.onGesture(Gesture.Shake, function () {
    roll = randint(1, 6)
    basic.showNumber(roll)
})

To use the text instead: open MakeCode, start a new project, click JavaScript at the top, replace what is there with this code, then click Blocks to see it as blocks.

To send it to the board, click Download. In Chrome or Edge, MakeCode can connect to the micro:bit by USB so the program goes straight onto it. Otherwise a .hex file downloads: drag it onto the MICROBIT drive.

Extension or homework

Challenges for learners who finish early, or to take further in a club or as homework.

  1. Challenge 1

    Make a dice for a board game you know that needs a different number, such as 1 to 12 or 1 to 20.

  2. Challenge 2

    Roll two dice at once (A for one, B for the other) and record the totals. Which total comes up most often, and why?

  3. Challenge 3

    Turn the dice into a decision maker: shake, and it shows yes, no or maybe.

  4. Challenge 4

    Make it a musical die, like the one in the Grade 6 CAPS: play a different note for each number on the micro:bit V2's built-in speaker.

Support and extension

Support

For learners who need more help

  • Use a ready-made tally table with the numbers 1 to 6 already written in.
  • Roll 30 times instead of 60, and compare with a partner's table.

Extension

For learners who are ready for more

  • Draw real dice faces with show leds inside an if block for each number.
  • Work out the percentage of rolls that gave each number, and compare it with one sixth (about 17%).

Informal assessment

Ask these as learners work. Informal assessment happens every day and is not recorded.

  1. What does pick random 1 to 6 do?
  2. Why do we store the number in a variable?
  3. After 60 rolls of a fair dice, about how many of each number would you expect?
Exit question

Is getting three sixes in a row impossible? Explain.

Observation checklist

Watch pairs as they plan, build and test. Write each pair's initials under the level you see.

Watch forBeginningDevelopingAccomplishedExemplary
Fills in the IPO table before building: a shake in, a random number processed, a number out.
Builds the dice as the driver, or checks each block against the plan as the navigator.
Predicts before testing, and compares the result with the prediction.
Tallies 60 rolls accurately and compares them with about 10 of each.
Beginning:
not yet, even with help
Developing:
with help
Accomplished:
on their own
Exemplary:
on their own, and explains it or takes it further

The four levels are the CAPS rubric levels (Coding and Robotics CAPS, Grades 4 to 6, page 176). The short descriptions are ours.

This lesson is not a formal task, but it is good practice for the term's practical task in Grades 4 to 6 (Coding and Robotics CAPS, Grades 4 to 6, page 170).

Common mistakes and fixes

  • Problem: The dice shows 0 sometimes.Fix: The random block still says 0 to 10 or 0 to 6. Change it to 1 to 6.
  • Problem: It always shows the same number.Fix: Check the show number block uses the roll variable, not the number 0.
  • Problem: Nothing happens when we shake it.Fix: Check the event block says shake, and shake a little harder.

Where it sits in CAPS

  • Coding and RoboticsAlignedGrade 6, Term 2: shake the micro:bit to roll a number from 1 to 6 (the CAPS musical die).Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 6, Term 2, Robotics, R.6 Mimic the operations of a robot, page 144: Shake a microcontroller to display a random number between 1 and 6 that can be used as a die.
  • MathematicsGrades 4 to 6 Mathematics: probability experiments, such as rolling a die, repeated many times.Source: CAPS Mathematics, Grades 4 to 6 (opens in a new tab), Section 2, topic 5.4 Probability (Grades 4, 5 and 6): Perform simple repeated events and list possible outcomes for experiments

Coding and Robotics quotes come from the Department of Basic Education's final CAPS, with the page number of the 2024 printed edition (the Department's October 2025 library copies number their pages differently). Schools may teach a topic in another term: the CAPS leaves the order to each school's annual teaching plan.

Ready to teach it?

Get a micro:bit for every learner or pair, and let us train your staff to run lessons like this one.

More lessons

All lessons →
  • Grades 4 to 6

    Grades 4 to 645 to 60 minLevel: Some experience

    Rock, paper, scissors

    Shake to throw rock, paper or scissors: learners use a random number and if, else if and else to choose a picture.

    Subjects: Coding and Robotics · Mathematics

    Code with: MakeCodePurple MashPython

  • Grades 4 to 6

    Grades 4 to 645 to 60 minLevel: Some experience

    Times-table true or false

    The micro:bit shows a times-table fact that may be wrong: A for true, B for false, and a score after ten questions.

    Subjects: Mathematics · Coding and Robotics

    Code with: MakeCodePython

  • Grades 4 to 6

    Grades 4 to 645 to 60 minLevel: Some experience

    Compass

    Learners turn the micro:bit into a compass that shows N, E, S or W, then use it to map their school grounds.

    Subjects: Social Sciences · Coding and Robotics

    Code with: MakeCodePython

micro:bit is a trade mark of the Micro:bit Educational Foundation, and BBC is a trade mark of the British Broadcasting Corporation. Maze Education is an independent reseller of genuine BBC micro:bit products. Our teacher training and our other products and services are our own, and are not endorsed by the Foundation or the BBC.

WhatsApp