The random module in Python: simulate what you can't predict
Demand will average about 800 tees. It will not be exactly 800. You can't know next season's number, but you can simulate a thousand plausible seasons and see the range.
The big idea
The random module produces unpredictable values, which lets a program simulate chance and measure how often outcomes occur.
See it in code
random.gauss(800, 250) draws one value from a bell curve centered on 800, where 250 controls the spread. Each call is one imagined season:
import random
for season in range(3):
demand = max(0, round(random.gauss(800, 250)))
print("Season demand:", demand)Season demand: 736 Season demand: 928 Season demand: 743
Run it again and the numbers change. max(0, ...) clamps the result, because a bell curve can occasionally dip below zero and demand can't.
One season tells you little. A thousand tell you a lot, and their average settles near the center of the curve:
import random
seasons = 1_000
demands = []
for season in range(seasons):
demands.append(max(0, round(random.gauss(800, 250))))
print(f"Average of {seasons:,} seasons: {sum(demands) / len(demands):.0f} tees")
print("Weakest season:", min(demands), "| Strongest:", max(demands))Average of 1,000 seasons: 807 tees Weakest season: 0 | Strongest: 1499
Individual seasons range widely, but the average lands close to 800. That's the law of large numbers: more trials, steadier averages.
Count how often an outcome happens and divide by the number of trials. The result is an estimated probability:
import random
made = 1_000
seasons = 1_000
sold_out = 0
for season in range(seasons):
demand = max(0, round(random.gauss(800, 250)))
if demand > made:
sold_out = sold_out + 1
print(f"Seasons that sell out {made:,} tees: {sold_out / seasons:.0%}")Seasons that sell out 1,000 tees: 21%
Roughly one season in five. No formula was needed, only a loop, a counter, and enough trials. This technique is called Monte Carlo simulation.
Simulation answers questions that are painful to solve by hand: the odds of a sequence appearing by chance, a strategy's worst drawdown, how long a queue gets.
Try it yourself
Change made to 800 and predict the sell-out rate before running it. Then raise the spread from 250 to 400 and watch how uncertainty changes the answer.
The common mistake
Asserting an exact result from a simulation. Run it twice and you might see 21%, then 20%. Report what's stable ("about one in five"), and raise the number of trials if you need more precision.
What it unlocks
random comes in through an import, and every simulation is an accumulator at heart: a counter or a total updated across many trials.