GGCSE Revision Hub

3.2.9 Random number generation in a programming language

AQA Computer Science 8525Topic 3.2 — ProgrammingFoundation & Higher

This is the longest topic in the specification and the one that carries most of the Paper 1 marks. It covers the statements every program is built from, the operators that do the work, the structures that hold data, and the discipline that makes code safe to rely on. Two of the specification's supported languages are Python and C#, so the examples here use Python-style syntax with the AQA pseudo-code where the specification demands it. Learn this as something you write, not something you memorise.

What you need to know

What it is for

Be able to use random number generation. Students will be expected to use random number generation within their computer programs. In practice this means anything that needs to vary each time it runs: rolling a die, shuffling a deck, generating a password, picking a random question from a quiz, or simulating something where the outcome is meant to be uncertain.

Key fact: Random number generation makes a program's outcome vary from run to run.

Pseudo-random

An understanding of how pseudo-random numbers are generated is not required. That is a relief, because the idea is a good one: the numbers are not truly random at all but are produced by a formula from a starting value, which is why they are called pseudo-random. The important practical consequence is that a program with a fixed seed produces exactly the same sequence every time.

Key fact: Pseudo-random numbers come from a formula, so a fixed seed gives the same sequence every run.

Using it in a program

In Python the random module gives you functions such as randint(a, b) for a whole number in a range, random() for a decimal between 0 and 1, and choice(list) for a random item from a list. What matters for the exam is that you can read a line of code that generates a random number and say what range it can produce.

Key fact: Know how to read a random number generator and what range it produces.

Getting the range right

The classic exam trap is the off-by-one error. If you want a die to roll from 1 to 6, the range must include both 1 and 6. A generator that returns a whole number from 0 to 5 has to have 1 added to it, and a generator that returns 0 to 6 has to exclude 6. Read the bounds carefully and state the possible outcomes.

Key fact: Check the bounds: a die needs 1 to 6 inclusive, so a 0-based generator needs adjusting.

Seeding

Many languages let you set the seed. Seeding with a fixed value makes the sequence predictable, which is exactly what you want when testing: you can run the program again and get the same random choices, so a bug reproduces and you can debug it. Using the current time as the seed makes the sequence different on every run, which is what a game wants.

Key fact: A fixed seed makes a run repeatable for testing; a time-based seed makes it different each run.

Where it is examined

Random number generation appears in Paper 1 as part of a program you have to trace or write, and the questions usually ask what values the variable could take, or what a particular statement does. Randomness makes tracing awkward, so most exam programs seed the generator and give you the sequence, or restrict the range so that there are only a few possible outcomes to consider.

Key fact: Exam programs usually fix the seed or a small range so the possible outcomes can be traced.

Flashcards

Exam questions

Written in the style of AQA Computer Science, with a mark scheme for each one. No question here is a real past paper question.

mcq1 mark

A program generates a whole number between 1 and 6 inclusive to simulate a die roll. Which range is correct?

  • 0 to 6
  • 1 to 6
  • 0 to 5
  • 1 to 5

Mark scheme: 1 mark for 1 to 6, because both endpoints must be possible. 0 to 6 could produce a zero and 0 to 5 or 1 to 5 could never produce a six.

short3 marks

Explain in English why a program might fix the random number seed when it is being tested, and what happens if it does not.

Mark scheme: 1 mark for stating that fixing the seed makes the sequence of random numbers the same on every run. 1 mark for explaining that this means any bug can be reproduced and the same random choices can be traced while debugging. 1 mark for explaining that without a fixed seed each run gives different values, so a failure cannot be reliably reproduced.

six6 marks

Explain in English how random number generation is used in programs, and what a programmer must be careful about.

Mark scheme: Level 1 (1-2 marks): A short answer saying it produces different values each run and is used for dice, shuffles and passwords. Level 2 (3-4 marks): A structured answer covering uses, ranges and seeding. Level 3 (5-6 marks): A detailed answer giving practical uses, explaining that the numbers are pseudo-random and come from a formula so a fixed seed repeats the sequence, explaining the importance of getting the range right and avoiding an off-by-one error, and explaining why a fixed seed is used in testing so that failures can be reproduced, with a time-based seed used when genuine variety is wanted.

Other sections in this topic

Practise these questions

Answer them in the interactive view and they get marked against the same mark scheme, one mark at a time, with the reason for each mark.

Open the interactive topic →