TopMyGrade

GCSE/Computer Science/AQA

CS2.8Random number generation: using a built-in function to generate pseudo-random integers within a stated range

Notes

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 exampleWorked example — dice game

SET roll TO RANDOM_INT(1, 6)
OUTPUT "You rolled a " + roll
IF roll = 6 THEN
  OUTPUT "You win!"
ENDIF

Worked exampleWorked 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 exampleWorked 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

  1. 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.
  2. Repeated seed. If you seed with a constant, you'll get the same sequence every run — useful for tests but a bug in production.
  3. Using floor incorrectly with floats. FLOOR(RANDOM() * 6) gives 0-5; for 1-6 use FLOOR(RANDOM() * 6) + 1. AQA's RANDOM_INT avoids this confusion.
  4. Assuming uniform distribution implies independence. Each call is independent — past results don't influence future ones (no "due for a 6").

Worked exampleWorked 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 thisQuick 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

Practice questions

Try each before peeking at the worked solution.

  1. Question 12 marks

    Generate a dice roll

    Write a single line of pseudocode that simulates the roll of a six-sided die and stores the result in a variable called roll.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 24 marks

    Random index for an array

    An array names has length 12. Write pseudocode to pick and output a random name from the array.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 33 marks

    Pseudo-random meaning

    Explain what is meant by pseudo-random numbers.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 43 marks

    Off-by-one bug

    A pupil writes:

    SET arr TO ["a","b","c","d","e"]
    SET i TO RANDOM_INT(1, LEN(arr))
    OUTPUT arr[i]
    

    The program sometimes crashes with an index error. Explain why and fix it.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 55 marks

    Coin toss simulation

    Write pseudocode that simulates flipping a coin 50 times and outputs how many were heads, where 0 represents tails and 1 represents heads.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 63 marks

    Security and randomness

    Explain why a standard random number function is not suitable for generating cryptographic keys.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS2.8 — Random number generation

10-card SR deck for AQA GCSE Computer Science topic CS2.8

10 cards · spaced repetition (SM-2)