Skip to content

Activity

Dice

Beginner | MakeCode, Python | Accelerometer, LED display | Input/output, Number & place value, Probability, Randomisation, Sensors

Step 1: Make it

What is it?

Shake your micro:bit to make random numbers.

These two videos show you what you'll make and how to code it:

Introduction

Coding guide

How it works

  1. Like the Get silly project this program uses the micro:bit’s accelerometer to make something happen when you shake it.
  2. When you shake your micro:bit, the program selects a random number between 1 and 6 and shows it on the LED display.
  3. It's really hard for computers to make truly random numbers because they’re machines that work precisely and regularly.
  4. Make a tally chart of how often each number comes up. Are these numbers really random? Compare it with real dice.

What you need

  • micro:bit (or MakeCode simulator)
  • MakeCode or Python editor
  • battery pack (optional)
  • real dice (optional)

Step 2: Code it

1from microbit import *
2import random
3
4while True:
5    if accelerometer.was_gesture('shake'):
6        display.show(random.randint(1, 6))

Step 3: Improve it

  • Make the number appear for a few seconds, then clear the LED display to save batteries.
  • Make it roll 2 dice. You can make a random number between 2 and 12, or you can make two random numbers between 1 and 6 and add them together.
  • Try both methods and tally how often each score occurs. Does it make a difference? Do some numbers come up more often than others?