Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.9, Edexcel 1CP2 6.6.1 · about 12 min
randint, choice and seeds: dice, random tunes and a robot that wanders.
[1 mark]Which values can random.randint(1, 6) give?
[1 mark]What does random.choice(["red", "green", "blue"]) give?
[1 mark]What does setting random.seed(42) before generating numbers do?
[1 mark]Write the AQA pseudo-code call for a random whole number from 1 to 10.
[1 mark]A dice program counts 600 rolls. About how many sixes should it see?
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
The hint students can ask for: Roll inside the loop, print the roll, then drive a distance worked out from it. Keep a running total as you go.
from bugbot import *
connect()
import random
total = 0
for i in range(3):
roll = random.randint(1, 6)
print("roll:", roll)
forward(50, distance=roll * 5)
total = total + roll * 5
print("total:", total)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.