Skip to topic
    ← Back to course topics

    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.

    Read the Random number generation in a programming language study guideFull revision notes for AQA GCSE Computer Science

    Your focus

    1. Use a random number function correctly within a program.
    2. Select a function call that produces values in a specified inclusive range.
    3. 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
    1. 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.
    2. 2Week 1, Day 3-5: Practice writing basic programs that simulate dice rolls, coin flips, and lottery number pickers using selection statements.
    3. 3Week 2, Day 1-3: Combine random generation with arrays and while loops by building an interactive guessing game with upper/lower bounds.
    4. 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)
    Write

    Produce clear, syntactically coherent code or pseudocode implementing the required functionality, including correct use of random functions and variable storage.

    State

    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).

    Explain

    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)
    Pitfall: Confusing inclusive and exclusive upper bounds in random integer generation functions.
    ❌ Weak Answer (Loses Marks):Use random(1, 6) to roll a dice.
    Example improved answer:In AQA pseudocode, rolling a standard six-sided dice is written as 'diceRoll = random(1, 6)' where both endpoints are inclusive. In Python, using 'random.randint(1, 6)' produces an inclusive range between 1 and 6.
    Examiner Tip: Always check whether the programming environment or pseudocode convention treats upper limits as inclusive or exclusive. AQA standard pseudocode uses inclusive parameters for random(a, b).
    Pitfall: Believing that computer-generated random numbers are truly random rather than pseudo-random.
    ❌ Weak Answer (Loses Marks):The computer picks a completely random number that nobody can ever predict.
    Example improved answer:Computers generate pseudo-random numbers using deterministic mathematical algorithms starting from a seed value; they simulate randomness rather than being truly random.
    Examiner Tip: Use the term 'pseudo-random' when discussing algorithmic generation to demonstrate high-level technical accuracy on written Paper 1 questions.
    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. 1.Step 1: Generate two random integers, each between 1 and 6 inclusive, using the standard pseudocode random function.
    2. 2.Step 2: Assign these values to distinct descriptive variables, such as 'dice1' and 'dice2'.
    3. 3.Step 3: Calculate the sum of the two variables and store it in 'total'.
    4. 4.Step 4: Use a selection structure (IF-ELSE) with a boolean OR operator to test if 'total' equals 7 or 11.
    5. 5.Step 5: Output 'Win' if true, and 'Try Again' in the ELSE block.
    Final Answer: dice1 = random(1, 6) dice2 = random(1, 6) total = dice1 + dice2 IF total == 7 OR total == 11 THEN OUTPUT 'Win' ELSE OUTPUT 'Try Again' ENDIF

    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. 1.Step 1: Identify the valid index range for the array: lower bound is 0, upper bound is 29.
    2. 2.Step 2: Use the random function with parameters 0 and 29 to generate an integer index.
    3. 3.Step 3: Use array indexing syntax to retrieve the element at that generated index from 'students'.
    Final Answer: randomIndex = random(0, 29) selectedStudent = students[randomIndex]
    Active Recall Memory Test
    What does the term 'pseudo-random' mean in computer science?
    Key Fact: Numbers that appear random but are generated using a deterministic mathematical algorithm from an initial seed.
    In AQA standard pseudocode, what range of numbers is returned by the expression 'random(5, 10)'?
    Key Fact: Any integer from 5 to 10 inclusive (5, 6, 7, 8, 9, or 10).
    Which Python module must be imported to access random integer generation functions?
    Key Fact: The 'random' module (using 'import random').
    Why is 'random(1, 6) + random(1, 6)' different from '2 * random(1, 6)'?
    Key Fact: 'random(1, 6) + random(1, 6)' simulates two independent dice rolls (totals 2-12), whereas '2 * random(1, 6)' can only produce even values (2, 4, 6, 8, 10, 12).
    Frequently Asked Questions
    How do you generate a random number in Python for GCSE Computer Science?
    In Python, you first import the built-in module using 'import random'. To generate an inclusive random integer between two values, use 'random.randint(lower, upper)'. For example, 'secret = random.randint(1, 100)' will generate a random number between 1 and 100 inclusive.
    What is the difference between random() and randint()?
    In Python, 'random.random()' takes no arguments and returns a floating-point number between 0.0 (inclusive) and 1.0 (exclusive). In contrast, 'random.randint(a, b)' takes two arguments and returns an integer between 'a' and 'b' inclusive. In AQA pseudocode, 'random(a, b)' is typically used to represent integer generation.
    Why are random numbers in computers called pseudo-random?
    Computers are deterministic machines that follow strict logical rules, meaning they cannot produce true randomness on their own. Instead, they use mathematical formulas based on a starting number called a 'seed' (often taken from the system clock). Because the sequence of numbers can be reproduced if the seed is known, it is called pseudo-random.
    Does AQA GCSE pseudocode use inclusive or exclusive bounds for random numbers?
    AQA GCSE pseudocode defines 'random(a, b)' as returning a random integer between 'a' and 'b' inclusive. This means both the lower bound 'a' and the upper bound 'b' can be selected by the generator.
    Can I pick a random item directly from a list in Python?
    Yes, Python provides the 'random.choice(sequence)' function, which directly returns a randomly selected element from a list or tuple. Alternatively, you can generate a random index with 'random.randint(0, len(myList) - 1)' and access the item via subscript notation.