TExcelTAD
Home › Basic Math & Pre-Algebra › First Probability

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 “roll an even number”={2,4,6}\text{``roll an even number''} = \{2, 4, 6\}.

When all outcomes are equally likely:

P(event)=number of favourable outcomestotal number of outcomesP(\text{event}) = \frac{\text{number of favourable outcomes}}{\text{total number of outcomes}}
P(roll a 6)=16≈0.167P(roll even)=36=12P(roll less than 3)=26=13P(roll a 7)=06=0impossibleP(roll 1 to 6)=66=1certain\begin{aligned} P(\text{roll a 6}) &= \tfrac{1}{6} \approx 0.167 \\ P(\text{roll even}) &= \tfrac{3}{6} = \tfrac{1}{2} \\ P(\text{roll less than 3}) &= \tfrac{2}{6} = \tfrac{1}{3} \\ P(\text{roll a 7}) &= \tfrac{0}{6} = 0 && \text{impossible} \\ P(\text{roll 1 to 6}) &= \tfrac{6}{6} = 1 && \text{certain} \end{aligned}

Probabilities can be written as fractions, decimals or percents: 14=0.25=25%\frac{1}{4} = 0.25 = 25\%.

Two basic rules

Complement. The chance that something does not happen is 1 minus the chance that it does:

P(not A)=1−P(A)P(not a 6)=1−16=56P(\text{not } A) = 1 - P(A) \qquad P(\text{not a 6}) = 1 - \tfrac{1}{6} = \tfrac{5}{6}

Independent events. If one result does not affect the other (two separate dice, two coin flips), multiply:

P(A and B)=P(A)⋅P(B)P(two sixes)=16⋅16=136P(A \text{ and } B) = P(A) \cdot P(B) \qquad P(\text{two sixes}) = \tfrac{1}{6} \cdot \tfrac{1}{6} = \tfrac{1}{36}

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:

P(no 6 in 4 rolls)=(56)4=6251296≈0.482P(at least one 6)=1−0.482≈0.518\begin{aligned} P(\text{no 6 in 4 rolls}) &= \left(\tfrac{5}{6}\right)^4 = \tfrac{625}{1296} \approx 0.482 \\ P(\text{at least one 6}) &= 1 - 0.482 \approx 0.518 \end{aligned}

Counting outcomes with Python

For small experiments you can list every outcome and count. Rolling two dice has 6×6=366 \times 6 = 36 equally likely outcomes:

python · try it yourself
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 (1+6,2+5,…,6+11+6, 2+5, \ldots, 6+1) but 2 only one way (1+11+1). 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:

python · try it yourself
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 0.9850≈36%0.98^{50} \approx 36\%. 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 0.9993≈99.7%0.999^3 \approx 99.7\%.
  • 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 P(5)P(5), P(odd)P(\text{odd}), P(greater than 4)P(\text{greater than 4}) and P(not a 1)P(\text{not a 1}).

Answer

16\frac{1}{6}, 12\frac{1}{2}, 13\frac{1}{3}, 56\frac{5}{6}.

Solution
P(5)=16P(odd)=∣{1,3,5}∣6=36=12P(>4)=∣{5,6}∣6=26=13P(not 1)=1−16=56\begin{aligned} P(5) &= \tfrac{1}{6} \\ P(\text{odd}) &= \frac{\lvert \{1, 3, 5\} \rvert}{6} = \tfrac{3}{6} = \tfrac{1}{2} \\ P(> 4) &= \frac{\lvert \{5, 6\} \rvert}{6} = \tfrac{2}{6} = \tfrac{1}{3} \\ P(\text{not } 1) &= 1 - \tfrac{1}{6} = \tfrac{5}{6} \end{aligned}

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

14\frac{1}{4} and 1625\frac{16}{25}.

Solution

There are 10 marbles. Because the first marble goes back, the draws are independent.

P(blue)=510=12P(both blue)=12⋅12=14P(not green)=810=45P(neither green)=45⋅45=1625\begin{aligned} P(\text{blue}) &= \tfrac{5}{10} = \tfrac{1}{2} & P(\text{both blue}) &= \tfrac{1}{2} \cdot \tfrac{1}{2} = \tfrac{1}{4} \\ P(\text{not green}) &= \tfrac{8}{10} = \tfrac{4}{5} & P(\text{neither green}) &= \tfrac{4}{5} \cdot \tfrac{4}{5} = \tfrac{16}{25} \end{aligned}

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
P(no rare in 20)=0.9520≈0.3585P(at least one)=1−0.3585≈0.6415\begin{aligned} P(\text{no rare in 20}) &= 0.95^{20} \approx 0.3585 \\ P(\text{at least one}) &= 1 - 0.3585 \approx 0.6415 \end{aligned}

Note that it is not 20×5%=100%20 \times 5\% = 100\%: the "add the chances" shortcut overcounts.

python · try it yourself
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 P(sum is 8)P(\text{sum is 8}). Compare with the exact value from counting.

Answer

The exact value is 536≈0.1389\frac{5}{36} \approx 0.1389; 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.

python · try it yourself
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?

  1. 113\frac{1}{13}
  2. 14\frac{1}{4}
  3. 152\frac{1}{52}
  4. 12\frac{1}{2}

Two fair coins are flipped. What is P(both heads)P(\text{both heads})?

  1. 12\frac{1}{2}
  2. 13\frac{1}{3}
  3. 14\frac{1}{4}
  4. 24\frac{2}{4}

Why do simulations in tests use a seeded random generator?

  1. Seeded numbers are more random
  2. So the same results appear on every run and failures can be reproduced
  3. Unseeded generators are slower
  4. Seeding makes every outcome equally likely