Random number generation in a programming language — AQA GCSE Computer Science
Verified courseTeaching note checked against the official specification.
Random number generation in programming allows algorithms to produce unpredictable values within a specified range, vital for simulations, games, and security. In AQA GCSE Computer Science, students must know how to generate pseudo-random integers and floats using standard pseudocode and high-level languages like Python.
Random number generation in a programming language explained
Random number generation lets a program produce unpredictable values, for example to simulate a dice roll or shuffle a quiz question.
Read the full explanation
In Python you import the random module and call random.randint(1, 6) to get a whole number from 1 to 6 inclusive, or random.random() for a value between 0 and 1. You must be able to use these calls inside your own programs, storing the result in a variable and using it in output or decisions. You are not required to understand how pseudo-random numbers are generated internally; you only need to use the facility correctly. Note the range is inclusive at both ends for randint.
Your focus
- Use a random number function correctly within a program.
- Select a function call that produces values in a specified inclusive range.
- Apply a generated random value in output or a decision.
Which command word is this?
Produce clear, syntactically coherent code or pseudocode implementing the required functionality, including correct use of random functions and variable storage.
Random number generation in a programming language exam tips
Topic Overview
Random number generation is a fundamental programming tool that allows software to introduce unpredictability and non-deterministic behavior. In AQA GCSE Computer Science, students learn how to apply randomisation functions to solve computational problems, simulate real-world events, and model probability.
This topic connects closely to core programming constructs including selection, iteration, and array manipulation. Mastering random number generation is essential for Paper 1 programming tasks, enabling students to construct games, automated tests, and statistical models.
Key Concepts
- →Pseudo-randomness: Algorithms generate sequences of numbers that appear random but are determined by an initial seed value and mathematical formulae.
- →Parameter ranges: Random functions require parameters specifying lower and upper limits, which are typically inclusive in AQA pseudocode: 'random(min, max)'.
- →Data types: Random values can be integers (discrete steps, e.g., rolling dice) or floating-point numbers (continuous ranges, e.g., generating percentages).
- →Application in algorithms: Random values are frequently combined with loops (e.g., guessing games) or arrays (e.g., selecting a random list element).
Marking Points
- Selects an appropriate random function call, e.g. random.randint(1, 6), to produce a value in a required range.
- Recognises that randint includes both endpoints of the stated range.
- Uses the generated value correctly, e.g. stored in a variable and used in output or a condition.
- Distinguishes using random generation from explaining how pseudo-random numbers are produced, which is not required.
Examiner Tips
- 💡Check the required range and choose a function whose endpoints match it.
- 💡Confirm the module import appears before any random call in the code.
- 💡Trace what value the variable holds after the call before predicting output.
- 💡Memorise the AQA standard pseudocode format: 'variable = random(start, end)' where both bounds are inclusive.
- 💡When tracing algorithms containing random values, pay attention to test data constraints specified in the question prompt.
- 💡Ensure you import the relevant module if writing in a specific language (e.g., 'import random' in Python) during coding tasks.
Common Mistakes
- Assuming randint(1, 6) excludes 6: correct this by stating both endpoints are included.
- Forgetting to import the random module before calling its functions: correct this by adding the import statement.
- Believing you must explain the pseudo-random algorithm: correct this by noting only correct use is required.
- Thinking 'random()' generates true randomness: Software-generated numbers are pseudo-random because they are produced by deterministic mathematical algorithms.
- Off-by-one errors with array indices: Generating random values from 1 to length instead of 0 to length - 1 leads to 'index out of bounds' runtime errors.
- Assuming repeated calls yield the same number: Calling a random function multiple times in an expression evaluates each call independently (e.g., 'random(1, 6) + random(1, 6)' is not equivalent to '2 * random(1, 6)').
Revision Plan
- 1Week 1, Day 1-2: Understand the difference between true random and pseudo-random numbers, and study the syntax for random generation in AQA pseudocode and Python.
- 2Week 1, Day 3-5: Practice writing basic programs that simulate dice rolls, coin flips, and lottery number pickers using selection statements.
- 3Week 2, Day 1-3: Combine random generation with arrays and while loops by building an interactive guessing game with upper/lower bounds.
- 4Week 2, Day 4-7: Complete past paper tracing questions and exam-style algorithm design questions under timed conditions.
Exam Question Types
- 📋Algorithm Design: Writing pseudocode or high-level code to simulate a game or real-world system involving chance.
- 📋Code Trace and Dry Run: Filling in trace tables where simulated random outputs are provided in the question scenario.
- 📋Error Identification: Spotting off-by-one errors in random range parameters or missing assignment statements.
Command Word Expectations (AQA)
Produce clear, syntactically coherent code or pseudocode implementing the required functionality, including correct use of random functions and variable storage.
Give a single concise fact, value, or line of syntax without needing extended justification (e.g., stating the range of values a random call produces).
Set out the causes, reasons, or mechanisms of a concept, such as explaining why computer-generated numbers are classified as pseudo-random.
How Students Lose Marks (Examiner Pitfalls)
Step-by-Step Worked Solutions
Question: Write an algorithm in AQA pseudocode that simulates rolling two six-sided dice. The algorithm should calculate the total score and output 'Win' if the total is 7 or 11, otherwise output 'Try Again'.
- 1.Step 1: Generate two random integers, each between 1 and 6 inclusive, using the standard pseudocode random function.
- 2.Step 2: Assign these values to distinct descriptive variables, such as 'dice1' and 'dice2'.
- 3.Step 3: Calculate the sum of the two variables and store it in 'total'.
- 4.Step 4: Use a selection structure (IF-ELSE) with a boolean OR operator to test if 'total' equals 7 or 11.
- 5.Step 5: Output 'Win' if true, and 'Try Again' in the ELSE block.
Question: A game requires selecting a random student from an array named 'students' of size 30 (indexed from 0 to 29). State the pseudocode command to generate a valid random index and store the chosen name in 'selectedStudent'.
- 1.Step 1: Identify the valid index range for the array: lower bound is 0, upper bound is 29.
- 2.Step 2: Use the random function with parameters 0 and 29 to generate an integer index.
- 3.Step 3: Use array indexing syntax to retrieve the element at that generated index from 'students'.