Knowledge organisersAdditional Programming Techniques
Be able to create and use random numbers in a program
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.
random(min, max) generates a random integer between min and max inclusive (OCR pseudocode).import random, then random.randint(min, max) for integers.random.randint() is inclusive of both endpoints.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)