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

Knowledge organisers / Random Number Generation

Games

All topicsPractise exam questions
Knowledge organiser

Random Number Generation

301.14b

Use random in game logic.

What you need to know

Random numbers are essential in games for dice rolls, card shuffling, enemy spawning, and guessing games. Combine random.randint() with loops and selection for game logic.

Key points

  • Dice games: random.randint(1, 6)
  • Guessing games: pick a secret number, player guesses
  • Use loops for repeated rounds
  • Compare user input against the random number

Code examples

Number guessing game
python
import random
secret = random.randint(1, 100)
guess = 0
while guess != secret:
    guess = int(input("Guess (1-100): "))
    if guess < secret:
        print("Higher!")
    elif guess > secret:
        print("Lower!")
print("Correct!")