SLIDE 1 / 10
CSZone.co.uk
Click anywhere to advance · Arrow keys also work
AQA 7517 · Paper 1 · 4.1.1f

Random Number
Generation

AQA A-Level Computer Science · Section 4.1 Fundamentals of Programming

WHAT YOU'LL LEARN
RANDOM() function · Pseudo-random numbers · Real-world applications
AQA SPEC LINK
4.1.1 — Use of random number generation in programs
RANDOM() in AQA

The RANDOM() Function

AQA PSEUDOCODE SYNTAX
RANDOM(a, b) — returns a random INTEGER between a and b inclusive

die ← RANDOM(1, 6) — simulates a dice roll
coin ← RANDOM(0, 1) — 0 = heads, 1 = tails
card ← RANDOM(1, 52) — random playing card
Python equivalent: import random then random.randint(a, b) — also inclusive of both endpoints.
Pseudo-Random

Are Computers Truly Random?

Computers generate pseudo-random numbers — sequences that appear random but are produced by a deterministic algorithm using a seed value.
SAME SEED = SAME SEQUENCE
If you set the same seed, you get the same "random" numbers — useful for testing and reproducibility.
TYPICAL SEEDS
Current time (milliseconds), hardware noise, user keystrokes — all used to create unpredictable starting points.
Applications

Real-World Uses of RANDOM()

Games: Dice rolls, loot drops, enemy spawn positions, shuffling cards
Security: Generating one-time passwords, session tokens, encryption keys
Simulations: Monte Carlo methods, modelling random events in science
Testing: Random test data generation, fuzz testing software
Sampling: Random survey selection, A/B testing in software
Worked Example

Dice Game Program

TWO-PLAYER DICE GAME
player1 ← RANDOM(1, 6)
player2 ← RANDOM(1, 6)
OUTPUT "Player 1 rolled: " + STR(player1)
OUTPUT "Player 2 rolled: " + STR(player2)
IF player1 > player2 THEN
  OUTPUT "Player 1 wins!"
ELSEIF player2 > player1 THEN
  OUTPUT "Player 2 wins!"
ELSE
  OUTPUT "It's a draw!"
ENDIF
Python Implementation

Using random in Python

PYTHON CODE
import random

# Random integer between 1 and 6 inclusive
die = random.randint(1, 6)

# Random float between 0.0 and 1.0
probability = random.random()

# Set seed for reproducibility
random.seed(42)
print(random.randint(1, 100))
Security Context

Random Numbers in Cryptography

PSEUDO-RANDOM — NOT SECURE
Standard RANDOM() is predictable if the algorithm and seed are known. Not suitable for security-critical applications.
CSPRNG — CRYPTOGRAPHICALLY SECURE
Cryptographically Secure Pseudo-Random Number Generators use hardware entropy sources. Python: secrets.randbelow()
Simulation Example

Simulating a Coin Flip 100 Times

heads ← 0
tails ← 0
FOR i ← 1 TO 100
  flip ← RANDOM(0, 1)
  IF flip = 0 THEN
    heads ← heads + 1
  ELSE
    tails ← tails + 1
  ENDIF
ENDFOR
OUTPUT "Heads: " + STR(heads) + " Tails: " + STR(tails)
AQA Exam Style

Practice Question

AQA 7517 — Paper 1 Style
A programmer uses RANDOM(1, 10) to generate a secret number for a guessing game. The program asks the user to guess the number and says "Too high", "Too low" or "Correct!".

Explain what is meant by a pseudo-random number and give one reason why this matters for the game.
[4 marks]
2 marks
Pseudo-random means generated by a deterministic algorithm using a seed value, so the sequence appears random but is not truly random
2 marks
For the game: if the seed is known, a player could predict future numbers, allowing them to cheat / gain an unfair advantage
Summary

Key Points to Remember

RANDOM(a, b) — returns a random integer between a and b inclusive
Computers generate pseudo-random numbers using a seed and algorithm
Same seed → same sequence (useful for testing; risky for security)
Python: import randomrandom.randint(a, b)
Applications: games, simulations, security tokens, A/B testing
🎉 Lesson complete — move to the quiz!