Topic 2.2.11: Random Number Generation
Using randint, Simulations & Logic.
Two applications of Random Numbers?
✅ Mark Scheme
Computer Games (Simulations), Cryptography/Security, Financial Modelling.
Score:
myNum = random(1, 5)
(a) Lowest possible value?
(b) Highest possible value?
✅ Mark Scheme
(a) 1
(b) 5 (Inclusive in OCR/Python generally assumed unless specified).
Score:
if random.randint(1, 3) == 1:
print("Yes")
elif random.randint(1, 3) == 2:
print("No")
elif random.randint(1, 3) == 3:
print("Maybe")
(a) Logic Error? (Why sometimes no output?)
(b) Rewrite code correctly.
✅ Mark Scheme
(a) It generates a NEW number for each check. Possible to fail all checks (e.g. roll
2, then roll 3, then roll 1).
num = random.randint(1, 3)
if num == 1:
print("Yes")
elif num == 2:
print("No")
else:
print("Maybe")
Score:
Write a Coin Toss program (Heads/Tails).
✅ Mark Scheme
num = random.randint(0, 1)
if num == 0:
print("Heads")
else:
print("Tails")
Score:
students = ["Ali", "Bea", "Caz", "Dan", "Eve"]
Pick a random student name.
Constraint: Must work if more names added (don't hardcode 4).
✅ Mark Scheme
import random
max_index = len(students) - 1
r = random.randint(0, max_index)
print(students[r])
Score: