First Probability
Probability puts a number on "how likely": 0 for impossible, 1 for certain, and everything in between. With a few simple rules you can work out the chances of dice rolls, card draws and loot drops, and with a seeded random number generator you can check your answers by simulation, just as programmers test game and system behaviour.
Outcomes and events
An experiment is something with an uncertain result, like rolling a die. Its possible results are outcomes; a die has six: 1, 2, 3, 4, 5, 6. An event is a set of outcomes we care about, such as .
When all outcomes are equally likely:
Probabilities can be written as fractions, decimals or percents: .
Two basic rules
Complement. The chance that something does not happen is 1 minus the chance that it does:
Independent events. If one result does not affect the other (two separate dice, two coin flips), multiply:
Together they answer "at least once" questions, which come up constantly. What is the chance of at least one 6 in four rolls? It is easier to find the chance of no 6 at all and subtract:
Counting outcomes with Python
For small experiments you can list every outcome and count. Rolling two dice has equally likely outcomes:
from fractions import Fraction
from itertools import product
rolls = list(product(range(1, 7), repeat=2))
print("outcomes:", len(rolls))
def p(event):
hits = sum(1 for r in rolls if event(r))
return Fraction(hits, len(rolls))
print("P(sum is 7) =", p(lambda r: r[0] + r[1] == 7))
print("P(double) =", p(lambda r: r[0] == r[1]))
print("P(sum >= 10) =", p(lambda r: sum(r) >= 10))
print("P(at least one 6) =", p(lambda r: 6 in r))outcomes: 36
P(sum is 7) = 1/6
P(double) = 1/6
P(sum >= 10) = 1/6
P(at least one 6) = 11/36
Sums are not equally likely: 7 can be made 6 ways () but 2 only one way (). Listing outcomes is the cure for many probability mistakes.
Simulation: checking by experiment
Probability predicts long-run frequencies. Flip a fair coin 10 times and you may get 7 heads; flip it 100,000 times and the share of heads will be very close to 0.5. This is the law of large numbers, and it lets you check any probability by simulating it.
Use a seeded random number generator so the "random" results are the same every run. This is how games replay a level exactly and how tests stay reproducible:
import numpy as np
rng = np.random.default_rng(2024)
for n in [10, 1_000, 100_000]:
flips = rng.integers(0, 2, size=n) # 0 = tails, 1 = heads
print(f"{n:>7,} flips: share of heads = {flips.mean():.4f}")
# At least one 6 in four rolls: theory says 1 - (5/6)**4
rolls = rng.integers(1, 7, size=(100_000, 4))
hit = (rolls == 6).any(axis=1).mean()
print(f"simulated {hit:.4f} vs exact {1 - (5 / 6) ** 4:.4f}") 10 flips: share of heads = 0.5000
1,000 flips: share of heads = 0.4850
100,000 flips: share of heads = 0.5011
simulated 0.5165 vs exact 0.5177
Small runs can land anywhere (10 flips hit exactly 0.5 here by luck, 1,000 flips were 1.5 points off); large ones settle near the true value. A simulation estimate is never exact, but with enough trials it is close, and it is a great way to catch a mistake in a hand calculation.
Tip: Change the seed and rerun: the small samples change a lot, the large ones barely at all. That is the law of large numbers you can see.
In software
- Loot drops and gacha. A 2% drop chance does not mean "one drop every 50 kills". The chance of getting nothing in 50 kills is . Games often add "pity timers" because players notice these streaks. See seeded RNG and weighted random.
- Reliability. If each of 3 independent services is up 99.9% of the time and a request needs all three, it succeeds with probability .
- Hash collisions and A/B tests are probability questions too, as is how an LLM picks its next token (sampling).
- Testing: always seed random generators in tests and simulations, so a failing run can be reproduced.
For more, continue with Probability and Probability Distributions in the Statistics course, and later the full Probability Theory course.
Exercises
Exercise 1: One die
A fair die is rolled once. Find , , and .
Answer
, , , .
Solution
Exercise 2: Marbles
A bag has 3 red, 5 blue and 2 green marbles. You draw one, put it back, and draw again. What is the chance both are blue? What is the chance neither is green?
Answer
and .
Solution
There are 10 marbles. Because the first marble goes back, the draws are independent.
Exercise 3: At least one
A loot box has a 5% chance of a rare item. What is the chance of at least one rare item in 20 boxes?
Answer
About 64.2%.
Solution
Note that it is not : the "add the chances" shortcut overcounts.
print(round(1 - 0.95 ** 20, 4))0.6415
Exercise 4: Simulate it
Simulate 100,000 rolls of two dice with np.random.default_rng(7) and estimate . Compare with the exact value from counting.
Answer
The exact value is ; the simulation lands very close to it.
Solution
A sum of 8 comes from (2,6), (3,5), (4,4), (5,3), (6,2): 5 of the 36 outcomes.
import numpy as np
rng = np.random.default_rng(7)
dice = rng.integers(1, 7, size=(100_000, 2))
est = (dice.sum(axis=1) == 8).mean()
print(f"simulated {est:.4f} exact {5 / 36:.4f}")simulated 0.1391 exact 0.1389
Test yourself
A card is drawn from a standard 52-card deck. What is the probability it is a heart?
Two fair coins are flipped. What is ?
Why do simulations in tests use a seeded random generator?
- Seeded numbers are more random
- So the same results appear on every run and failures can be reproduced
- Unseeded generators are slower
- Seeding makes every outcome equally likely