Random number generation
Many programs need a touch of randomness — dice rolls, lottery numbers, shuffling a playlist, randomising test data, password salts. AQA's spec asks you to use a built-in random integer function within a stated range.
AQA pseudocode
SET roll TO RANDOM_INT(1, 6)
RANDOM_INT(low, high) returns a pseudo-random integer between low and high inclusive of both endpoints. The exact convention is on the AQA pseudocode reference sheet.
Inclusive vs exclusive — read carefully
Be wary that real languages disagree:
- Python:
random.randint(1, 6)is inclusive of both — like AQA pseudocode. - Python:
random.randrange(1, 6)is exclusive of 6 — gives 1-5 only. - JavaScript:
Math.random()returns a real in [0, 1).
In an exam stick with the AQA function and assume both endpoints are included.
Pseudo-random — not truly random
Computers can't generate true randomness from deterministic logic. They use a pseudo-random number generator (PRNG) — a function that produces a long sequence of seemingly random numbers from a starting seed.
- Same seed → same sequence (useful for reproducible tests).
- Different seeds → different sequences.
- Seeded with the current time, the sequence appears random in practice.
You won't be asked to implement a PRNG, but you should know pseudo-random means deterministic but unpredictable.
✦Worked example— Worked example — dice game
SET roll TO RANDOM_INT(1, 6)
OUTPUT "You rolled a " + roll
IF roll = 6 THEN
OUTPUT "You win!"
ENDIF
✦Worked example— Worked example — pick a word from a list
SET words TO ["apple", "banana", "cherry", "date"]
SET index TO RANDOM_INT(0, LEN(words) - 1)
OUTPUT words[index]
The range 0 to LEN(words) - 1 ensures we always pick a valid array index.
✦Worked example— Worked example — random password digit
SET digits TO ""
FOR i FROM 1 TO 4
SET digits TO digits + INT_TO_STRING(RANDOM_INT(0, 9))
ENDFOR
OUTPUT digits -- e.g. "5103"
Why not use RANDOM for security?
The standard PRNG is not cryptographically secure. For passwords, encryption keys or session tokens, programmers use a CSPRNG (cryptographically secure PRNG) provided by the operating system (e.g. secrets.token_bytes() in Python). At GCSE you don't need to use one, but recognise that 'random' for a game is different from 'random' for security.
Bias and pitfalls
- Off-by-one with array indices.
RANDOM_INT(1, LEN(arr))is wrong — it generates 1 to LEN(arr) but valid indices are 0 to LEN(arr)-1. - Repeated seed. If you seed with a constant, you'll get the same sequence every run — useful for tests but a bug in production.
- Using floor incorrectly with floats.
FLOOR(RANDOM() * 6)gives 0-5; for 1-6 useFLOOR(RANDOM() * 6) + 1. AQA's RANDOM_INT avoids this confusion. - Assuming uniform distribution implies independence. Each call is independent — past results don't influence future ones (no "due for a 6").
✦Worked example— Worked example — coin toss simulation
Simulate 100 coin tosses and count heads:
SET heads TO 0
FOR i FROM 1 TO 100
IF RANDOM_INT(0, 1) = 1 THEN
SET heads TO heads + 1
ENDIF
ENDFOR
OUTPUT "Heads: " + heads
You'd expect around 50 heads with substantial variation — that's normal.
➜Try this— Quick check
Write pseudocode that picks a random colour from ["red", "green", "blue", "yellow"] and outputs it.
SET colours TO ["red", "green", "blue", "yellow"]
SET pick TO RANDOM_INT(0, 3)
OUTPUT colours[pick]
AI-generated · claude-opus-4-7 · v3-deep-computer-science