Creating Random Events
Random events add unpredictability and excitement to programs. By combining random.choice() with lists of options, you can create programs that generate random events, outcomes, and scenarios.
This is useful for games, simulations, decision-making tools, and any program where you want varied, unpredictable behavior based on random selection.
Basic Random Event Generator
Use random.choice() to randomly select from a list of possible events or outcomes:
import random
events = ["Rain", "Sunny", "Cloudy", "Snow"]
today_weather = random.choice(events)
print(f"Today's weather: {today_weather}")
for day in range(5):
weather = random.choice(events)
print(f"Day {day + 1}: {weather}")
Random Outcomes with Messages
Combine random selection with formatted messages to create meaningful random events:
import random
outcomes = ["Success!", "Try again", "Almost there", "Great job!"]
result = random.choice(outcomes)
print(f"Your result: {result}")
coin_outcomes = ["Heads - You win!", "Tails - Try again"]
coin_result = random.choice(coin_outcomes)
print(coin_result)
Multiple Random Events
Generate multiple random events in sequence to create more complex scenarios:
import random
actions = ["Find treasure", "Meet a friend", "Discover a cave", "See a dragon"]
locations = ["forest", "mountain", "beach", "desert"]
action = random.choice(actions)
location = random.choice(locations)
print(f"You {action} in the {location}!")
for i in range(3):
event = random.choice(actions)
place = random.choice(locations)
print(f"Event {i+1}: {event} at {place}")
Weighted Random Events
You can create weighted probabilities by repeating items in a list, making some events more likely than others:
import random
outcomes = ["Common", "Common", "Common", "Rare", "Epic"]
result = random.choice(outcomes)
print(f"You got: {result}")
results_count = {"Common": 0, "Rare": 0, "Epic": 0}
for i in range(100):
result = random.choice(outcomes)
results_count[result] += 1
print("Distribution (100 trials):", results_count)
Practical Applications
Random event generators are useful in many scenarios:
import random
motivations = [
"Keep going!",
"You've got this!",
"Stay strong!",
"Believe in yourself!"
]
print(f"Today's motivation: {random.choice(motivations)}")
activities = ["Read a book", "Go for a walk", "Learn something new", "Call a friend"]
print(f"Suggested activity: {random.choice(activities)}")
characters = ["wizard", "knight", "princess"]
settings = ["castle", "forest", "cave"]
character = random.choice(characters)
setting = random.choice(settings)
print(f"Once upon a time, a {character} lived in a {setting}.")
Combining with Conditional Logic
Combine random events with if statements to create different behaviors based on the random outcome:
import random
event = random.choice(["sunny", "rainy", "snowy"])
if event == "sunny":
print("Great day for a picnic!")
elif event == "rainy":
print("Perfect day to stay inside!")
else:
print("Time to build a snowman!")
print(f"Weather: {event}")
Best Practices
✅ Tips for Random Events
• Store event options in lists for easy modification
• Use descriptive variable names for your event lists
• Combine multiple random choices for complex scenarios
• Use weighted lists if you need different probabilities
• Add conditional logic to create dynamic responses
💡 Remember
Each call to random.choice() gives a new random result. The same list can produce different outcomes every time you run the program!
🎉
Lesson Complete!
Great work! Continue to the next lesson.