Random Number Generation

beginner20 min

Learning objectives

  • Explain pseudo-random number generation
  • Generate random integers within specified ranges
  • Apply randomisation within games and simulations

Learn

AQA 4.1.1.9 — Pseudo-random number generation

Retrieval: you've just used file reading and writing to store data between runs of a program — this lesson introduces a different kind of variability: values that change within a single run, generated on demand.

"Random" numbers aren't truly random

A computer follows instructions deterministically, so it can't generate genuinely unpredictable numbers on its own. Instead, it uses a pseudo-random number generator (PRNG): an algorithm that starts from a seed value and produces a long sequence of numbers that looks statistically random but is, technically, entirely determined by that seed. In practice (and for everything at this level of the course) pseudo-random numbers behave exactly like real randomness — the distinction mostly matters for cryptography, which needs true unpredictability and uses a different technique entirely.

Generating random integers in Python

import random

roll = random.randint(1, 6)   # a random integer from 1 to 6
print(roll)

Common misconception

random.randint(a, b) is inclusive of both ends — random.randint(1, 6) can return 6, not just up to 5. This trips people up because Python's other range-like tools (range(1, 6), list slicing) are normally exclusive of the upper bound. Always check which convention a function uses rather than assuming.

Worked example — a number-guessing game

import random

secret = random.randint(1, 100)
guess = int(input("Guess a number between 1 and 100: "))

if guess == secret:
    print("Correct!")
elif guess < secret:
    print("Too low.")
else:
    print("Too high.")

Applying it — Rock, Paper, Scissors

import random

options = ["rock", "paper", "scissors"]
computer_choice = random.choice(options)   # picks one item at random from a list
player_choice = input("rock, paper or scissors? ")

print(f"Computer chose {computer_choice}.")
if player_choice == computer_choice:
    print("Draw!")

random.choice(sequence) picks one random item from an existing list — useful whenever you're choosing between options rather than generating a number within a range.

Challenge

Extend the Rock, Paper, Scissors example so it correctly determines a winner (not just a draw) for every possible combination of player and computer choice, and keeps a running score across multiple rounds using a variable.

Looking ahead: the consolidation project that follows this lesson is a natural place to combine randomisation with everything else from Sequences 1–2 — a quiz that randomly selects questions, or a game with randomised elements, both use exactly this technique.

Practise

Apply what you've just learned in the Coding Lab.

Open Coding Lab

Dice Roll Simulator

A coding challenge for this lesson, from the question bank.

Open challenge

Retrieval questions

1 question for this lesson, in Year 12 Programming & Data Structures Retrieval Quiz.

Take the quiz
Log in to track this lesson on your progress dashboard.
Log in