301.14 Random Number Generation
Practise exam questionsRandom Number Generation — Note 2 of 3
Use random in game logic.
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.
random.randint(1, 6)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!")Back to Random Number Generation
Specification notes for programming are in Paper 2.