Community resourceWorksheet
J277 2.2.3 Random number generation
Part 4 of 5 · J277 2.2.3 · Applied programming
Random number generation in an algorithm, and turning a requirement into the correct inclusive bounds.
Students will:
- use random.randint() with inclusive bounds
- turn a stated requirement into the correct bounds
- map random integers onto outcomes
- generate several values in one program
- explain why the program behaves differently each run
Inside: 5 explanation cells, 3 runnable Python tasks, 2 multiple-choice questions and 2 written answers. 14 marks, about 45 minutes.
Series: J277 2.2.3 · Applied programming, part 4 of 5.
Shared by Coding PathwayVerified teacher
- 12 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
- Updated 9 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Random number generation
Programs use random number generation for games, simulations and sampling. OCR expects students to use random number generation in an algorithm. In Python, random.randint(low, high) returns an integer from low to high, including both boundaries. In an OCR written algorithm, the precise notation may differ, but the lower and upper bounds and the way the generated value is used must still be clear.
Which expression can generate an integer from 1 to 6 inclusive after import random?
- Arandom.randint(1, 5)
- Brandom(6)
- Crange(1, 6)
- Drandom.randint(1, 6)
Worked example: mapping random integers to outcomes
Run the example several times. The dice value is generated directly from 1 to 6. The coin uses a generated value of 0 or 1, then selection maps those two possible integers to the strings "heads" and "tails".
The result may repeat between runs. A random program is allowed to produce the same valid outcome more than once.
import random
dice_roll = random.randint(1, 6)
coin_value = random.randint(0, 1)
if coin_value == 0:
coin = "heads"
else:
coin = "tails"
print(dice_roll, coin)
Turning a requirement into bounds
Read inclusive range wording carefully. A ten-sided spinner labelled 0 to 9 uses random.randint(0, 9). Random output should still obey every stated rule. Test the boundaries and map generated numbers to outcomes deliberately.
The generated value is produced by the program rather than entered by a user, so input validation is not applied to it. The programmer instead chooses the correct bounds and tests that generated values stay within them.
Random does not mean that every short run contains each possible result equally often. It means each generated value is chosen unpredictably according to the generator.
Supported practice: generate within an inclusive range
Generate one integer from 20 to 30 inclusive and store it in number. Use random.randint() with the requirement's exact lower and upper boundaries. Do not replace the random call with one fixed value that happens to be in range.
# Generate one integer from 20 to 30 inclusive and store it in number.
import random
A game needs one of four equally mapped directions using values 1 to 4. Which value range is suitable?
- A1 to 4 inclusive
- B0 to 4 inclusive
- C1 to 3 inclusive
- D4 to 8 inclusive
Independent practice: generate several values
Simulate five ordinary six-sided dice rolls. Use a count-controlled loop with five repetitions, generate an integer from 1 to 6 inclusive on each repetition, and append every result to rolls.
The exact values will differ between runs, so test the properties instead: the list must contain five integers and every value must be within the required range.
# Generate five dice rolls and store them in rolls.
import random
rolls = []
# Use a count-controlled loop.
Design an algorithm that randomly selects one of the strings red, green or blue using generated integers. State the number range and the mapping.
Give exactly three possible generated values and map each to one colour using selection.
Students type their answer here.
A student tests a dice program three times, does not see a 6 and concludes that 6 can never be generated. Explain why this conclusion is invalid and state a better test.
Distinguish a small random sample from the allowed boundaries in the code.
Students type their answer here.
Review
Translate the stated inclusive range directly into the bounds of random.randint(). Test invariant properties such as type, length and permitted range rather than expecting one particular random result or every possible result in a small sample.