Random Number Generation
Practise Random Number Generation for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers for AQA GCSE Computer Science (8525). Random number generation produces unpredictable values, useful in games and simulations.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
Random number generation produces unpredictable values, which programs need for games, simulations and sampling.
A random number function typically takes a lower and an upper limit and returns a value between them. Whether the limits themselves can be produced varies between languages, which is why the documentation must be checked — an off-by-one here produces a dice that never rolls a six.
Computer-generated random numbers are strictly pseudo-random. They are produced by an algorithm from a starting value called a seed, so the same seed always produces the same sequence. This is genuinely useful for testing, because a program using random numbers can be made to behave identically each run while a bug is being traced.
Revision notes
Generating random numbers
A random function usually takes a lower and upper limit and returns a value between them.
Whether the limits themselves can be produced varies between languages. Getting this wrong gives a dice that never rolls a six, or an array index out of range.
Pseudo-random numbers
Computer random numbers are produced by an algorithm from a starting value called a seed.
The same seed always produces the same sequence, so the numbers are not truly random — they are pseudo-random. Truly random numbers require a physical source of unpredictability.
Why seeding is useful
Fixing the seed makes a program behave identically each run.
This is valuable when testing, because a bug involving random values can be reproduced reliably instead of appearing intermittently.
Key points
- Random numbers are used in games and simulations.
- A random function takes lower and upper limits.
- Check whether the limits are included.
- Computer random numbers are pseudo-random.
- They are generated from a seed.
- The same seed gives the same sequence.
Worked examples
Example 1
Explain why computer-generated random numbers are described as pseudo-random. [2 marks]
Working
Example 2
Explain one benefit of fixing the seed when testing a program. [2 marks]
Working
Example 3
A dice simulation should produce values 1 to 6. State one thing to check in the random function. [2 marks]
Working
Common mistakes
Assuming random numbers are truly random.
They are pseudo-random, generated from a seed.
Not checking whether limits are included.
This causes off-by-one errors in ranges.
Thinking a fixed seed is a fault.
It is deliberately useful for reproducible testing.
Using random numbers for security without care.
Pseudo-random sequences can be predicted if the seed is known.
Exam tips
- Check whether the function includes its limits.
- Use the term pseudo-random.
- Explain seeding through reproducible testing.
- Test the extremes of any random range.
Key terms
- Pseudo-random
- Generated by an algorithm, not truly unpredictable.
- Seed
- The starting value determining the sequence.
- Range
- The lower and upper limits of generated values.
- Reproducible
- Producing the same result each run, useful for testing.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.