Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Additional Programming Techniques

Random number generation

All topicsPractise exam questions
Knowledge organiser

Additional Programming Techniques

2.2.3l

Be able to create and use random numbers in a program

What you need to know

Random number generation allows programs to produce unpredictable values within a specified range. This is useful for games (dice rolls, card draws), simulations, testing, and security. In OCR pseudocode the syntax is random(min, max); in Python you use the random module.

Key points

  • random(min, max) generates a random integer between min and max inclusive (OCR pseudocode).
  • In Python: import random, then random.randint(min, max) for integers.
  • random.randint() is inclusive of both endpoints.
  • Store the random value in a variable if it will be used more than once.
  • Useful for: dice rolls, random questions, game outcomes, test data generation.
  • Can generate both integer and real (float) random numbers.

Code examples

Random number generation
python
import random

# Random integer between 1 and 6 (dice roll)
dice = random.randint(1, 6)
print("You rolled:", dice)

# Random float between 0.0 and 1.0
chance = random.random()
print("Random chance:", chance)

# Random choice from a list
colours = ["red", "blue", "green"]
pick = random.choice(colours)
print("Chosen colour:", pick)