The Random Module
Alright, developer — time to add some unpredictability to your game.
Because let’s be honest: A game without randomness is just… homework with graphics.
Enter Python’s random module — the toolbox that lets your game roll dice, pick secret words, spawn enemies, and generally behave like it had too much caffeine.
Your First Taste of Chaos
import random
print("Rolling a dice:", random.randint(1, 6))
print("Random color:", random.choice(["red", "green", "blue"]))
What’s happening?
import randomYou’re telling Python: “Bring me the chaos generator.”
random.randint(1, 6)Gives you a random number between 1 and 6.
Perfect for dice, loot drops, or deciding who goes first when both players yell “ME!” at the same time.
random.choice([...])Picks a random item from a list.
Great for choosing:
Practice (Your Turn!)
Mini Challenge 1: Upgrade the Dice
Make the dice roll from 1 to 10 instead of 1 to 6.
Hint:Change this part:
random.randint(1, 6)
to random.randint(1, 10)
And yes – Update the print text if too.Your future self will thank you
Mini Challenge 2: Random Treasure Generator
Add one more line that picks a random treasure:
["gold", "diamonds", "magic potion", "mystery box"]
Use: random.choice(...)
If your game starts handing out “mystery boxes,” don’t blame me when players get addicted.


