OCR · GCSE · Computer Science

    Course version: J277 · Version from Sept 2022

    Additional programming techniques: Random number generation

    Random number generation produces values that are not predictable in advance within a chosen range, useful for games, simulations, sampling and testing.

    Checked against the OCR J277 specification on .

    • Point 7 of 7
    • 4 key terms

    Study notes

    From the checked teaching notes for this specification point.

    The idea

    Random number generation produces values that are not predictable in advance within a chosen range, useful for games, simulations, sampling and testing. Most languages provide a library routine, so the complex generation algorithm need not be written from scratch. These routines are usually pseudorandom: values look random but are produced by a deterministic algorithm, so they are predictable if the seed and algorithm are known. Syntax and bounds are language-specific. In OCR Reference Language, `random(x, y)` returns an integer from x to y inclusive, so a die roll is `random(1, 6)`. In Python, after `import random`, `random.randint(1, 6)` returns an integer from 1 to 6 inclusive. Generated values can be stored in variables, tested with selection, used to index an array, or repeated inside loops.

    What earns the marks

    • States that random number generation produces values that are not predictable in advance within a defined range.
    • Identifies how to call a library routine to generate a random number, giving the correct syntax and inclusive bounds for the language or pseudocode used.
    • Applies random values to a context, for example simulating a dice roll with a random integer from 1 to 6, or selecting a random item from an array.
    • Describes how generated values can be stored in variables, tested with selection statements or repeated inside loops.
    • Recognises that random number generation is a built-in library routine in most programming languages, avoiding the need to write the generation algorithm from scratch.
    • Uses OCR Reference Language correctly, writing `random(x, y)` for an integer from x to y inclusive, e.g. `random(1, 6)` for a die roll.
    • Explains that most language routines are pseudorandom, so values are predictable if the seed and algorithm are known.

    Common mistakes and how to put them right

    • Assuming a random function can return a value outside its stated bounds.

      Put it right: State the bounds and note that both are inclusive, e.g. `random(1, 6)` returns 1 to 6.

    • Writing `random(n)` in OCR Reference Language.

      Put it right: The routine takes two arguments, so write `random(1, 6)` for a die roll.

    • Writing `randint(1, 6)` in Python without importing.

      Put it right: Write `import random` then `random.randint(1, 6)`, or `from random import randint`.

    • Forgetting to store the generated random number in a variable before using it multiple times.

      Put it right: Assign the random value to a variable so the same value can be checked in multiple conditions.

    • Confusing the index of an array with the random value itself.

      Put it right: Generate a random integer within the bounds of the array's indices (e.g. 0 to length-1) and use it to access the element.

    Exam tips

    • When writing pseudocode or code, clearly state the bounds for your random number generator, ensuring they match the scenario (e.g. 1 to 6 for a die).
    • When a question asks for a simulation, state the range, how the value is stored in a variable, and how it is used in the program's logic.
    • Check which language or pseudocode the question uses: OCR Reference Language `random(1, 6)` and Python `random.randint(1, 6)` both give a die roll but use different syntax.

    Try the lesson

    The start of the Additional programming techniques: Random number generation lesson, made from checked teaching notes for this specification point.

    Have a go first

    Which statement correctly describes what a random number generator does in a program?

    Listen

    Random number generation produces values that are not predictable in advance within a chosen range, useful for games, simulations, sampling and testing. Most languages provide a library routine, so the complex generation algorithm need not be written from scratch. These routines are usually pseudorandom: values look random but are produced by a deterministic algorithm, so they are predictable if the seed and algorithm are known. Syntax and bounds are language-specific. In OCR Reference Language, random(x, y) returns an integer from x to y inclusive, so a die roll is random(1, 6). In Python, after import random, random.randint(1, 6) returns an integer from 1 to 6 inclusive. Generated values can be stored in variables, tested with selection, used to index an array, or repeated inside loops. A strong answer earns marks for these points. States that random number generation produces values that are not predictable in advance within a defined range. Identifies how to call a library routine to generate a random number, giving the correct syntax and inclusive bounds for the language or pseudocode used. Applies random values to a context, for example simulating a dice roll with a random integer from 1 to 6, or selecting a random item from an array. Describes how generated values can be stored in variables, tested with selection statements or repeated inside loops. A common slip: Assuming a random function can return a value outside its stated bounds. Instead, state the bounds and note that both are inclusive, e.g. random(1, 6) returns 1 to 6. Exam tip: When writing pseudocode or code, clearly state the bounds for your random number generator, ensuring they match the scenario (e.g. 1 to 6 for a die).

    Key words

    Say what each word means before you turn the card.

    Unpredictable values

    Try answering aloud or in your head before turning the card.

    Range and bounds

    Try answering aloud or in your head before turning the card.

    Simulation and games

    Try answering aloud or in your head before turning the card.

    Pseudorandomness

    Try answering aloud or in your head before turning the card.

    See every definition
    Unpredictable values
    Values that are not predictable in advance, produced within a chosen range and useful for games, simulations, sampling and testing.
    Range and bounds
    The limits of the values produced, which are language-specific; in OCR Reference Language random(x, y) returns an integer from x to y inclusive.
    Simulation and games
    Uses of random number generation such as simulating a dice roll with a random integer from 1 to 6, or selecting a random item from an array.
    Pseudorandomness
    Values that look random but are produced by a deterministic algorithm, so they are predictable if the seed and algorithm are known.

    There is more to this lesson

    The full lesson goes on to check your understanding and help you earn the marks. Create a free account to carry on, with your progress saved.

    Carry on with the full lesson

    A week of Premium from your first lesson · Then the free plan continues · No card, no charge

    Test your understanding of Additional programming techniques: Random number generation

    Active recall beats re-reading. Take an instant quiz now to test what you’ve learned and lock it into long-term memory.

    Take topic quiz

    A week of Premium from your first lesson · Then the free plan continues · No card, no charge