The answersDownload the PDF
Worksheet

F3.7 Random numbers

Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.9, Edexcel 1CP2 6.6.1 · about 12 min

BugBotLab
NameClassDate

What this lesson is about

randint, choice and seeds: dice, random tunes and a robot that wanders.

Questions 5 marks in all

  1. [1 mark]Which values can random.randint(1, 6) give?

    1. A1, 2, 3, 4, 5 or 6
    2. B1 to 5
    3. C0 to 6
    4. DAny real number from 1 to 6
  2. [1 mark]What does random.choice(["red", "green", "blue"]) give?

    1. AOne of the three colours, picked at random
    2. BAll three in a random order
    3. CThe number 3
    4. DAlways red
  3. [1 mark]What does setting random.seed(42) before generating numbers do?

    1. AMakes the same sequence of numbers come out every run
    2. BMakes the numbers more random
    3. CLimits numbers to 42
    4. DStops random numbers
  4. [1 mark]Write the AQA pseudo-code call for a random whole number from 1 to 10.

  5. [1 mark]A dice program counts 600 rolls. About how many sixes should it see?

    1. AAbout 100, but rarely exactly 100
    2. BExactly 100
    3. CAbout 600
    4. DAbout 6

The task: dice drive

Roll a dice three times with random.randint(1, 6). For each roll, print roll: <n> and drive forward <n> * 5 cm. At the end print total: <cm> with the total distance, worked out from the rolls. This task fixes its random numbers, so you get the same rolls each run.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

import random

total = 0

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f3-7-random-numbers/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Flash the LED a random number of times, from 3 to 8, and ask the user to count the flashes.
  2. Play ten notes chosen at random from a list of notes: a random tune.
  3. Make the wandering robot turn a random whole number of degrees between 90 and 180, either way.