Probability: outcomes, events and chance
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Probability puts a number from 0 to 1 on how likely something is. Roll two dice and lay all 36 outcomes out on a 6 × 6 board. Watch events light up as sets of squares, count and divide to get their chance, combine them with the complement, the addition rule and independence, and check the answer with 60,000 simulated rolls in Python.
Roll two dice
Probability measures how likely something is. Start with an experiment: roll two fair dice. One result, like die 1 showing 3 and die 2 showing 4, is called an outcome. Write it as the pair (3, 4).
>>> from fractions import Fraction >>> from random import randint, seed >>> D = range(1, 7) # a die: 1 to 6 >>> (3, 4) == (4, 3) # order matters: die 1 first False
اقرأ الدرس خطوة بخطوة
Roll two dice
Probability measures how likely something is. Start with an experiment: roll two fair dice. One result, like die 1 showing 3 and die 2 showing 4, is called an outcome. Write it as the pair (3, 4).
>>> from fractions import Fraction >>> from random import randint, seed >>> D = range(1, 7) # a die: 1 to 6 >>> (3, 4) == (4, 3) # order matters: die 1 first FalseThe sample space
The sample space is the set of every possible outcome. Die 1 has 6 faces and die 2 has 6 faces, so there are 6 × 6 = 36 pairs. Each square on the board is one outcome.
>>> S = {(a, b) for a in D for b in D} >>> len(S) 36An event is a set
An event is a set of outcomes we care about. Let A be "the sum is 7". Six squares belong to A: (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1). They lie on one diagonal.
>>> A = {s for s in S if sum(s) == 7} >>> sorted(A) [(1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1)]Count, then divide
When every outcome is equally likely, as with fair dice, P(A) = (outcomes in A) ÷ (all outcomes). For a sum of 7 that is 6/36 = 1/6, about 0.167. Expect a 7 about once in every six rolls.
>>> def P(E): return Fraction(len(E), len(S)) ... >>> P(A) Fraction(1, 6) >>> float(P(A)) 0.16666666666666666Always from 0 to 1
A probability is always between 0 and 1. A sum of 13 is impossible: no square has it, so P = 0/36 = 0. A sum from 2 to 12 is certain: all 36 squares, so P = 36/36 = 1.
>>> P({s for s in S if sum(s) == 13}) Fraction(0, 1) >>> P({s for s in S if 2 <= sum(s) <= 12}) Fraction(1, 1)The complement: not A
"Not A" holds every outcome outside A. Since some outcome always happens, P(not A) = 1 − P(A). The sum is not 7 in 30 squares: 30/36 = 5/6, the same as 1 − 1/6.
>>> 1 - P(A) Fraction(5, 6) >>> P(S - A) # the set of outcomes not in A Fraction(5, 6)At least one six
Let B be "at least one die shows 6". Die 1 is 6 in the bottom row, 6 squares. Die 2 is 6 in the last column, 6 squares. (6, 6) is in both, so B has 6 + 6 − 1 = 11: 11/36.
>>> B = {s for s in S if 6 in s} >>> len(B), P(B) (11, Fraction(11, 36)) >>> 1 - Fraction(5, 6) ** 2 # 1 − P(no six at all) Fraction(11, 36)A or B: the overlap
For "A or B", add both events, then subtract the overlap: P(A or B) = P(A) + P(B) − P(A and B). (1, 6) and (6, 1) are in both: 6 + 11 − 2 = 15, so 15/36 = 5/12.
>>> sorted(A.intersection(B)) [(1, 6), (6, 1)] >>> P(A) + P(B) - P(A.intersection(B)) Fraction(5, 12) >>> P(A.union(B)) Fraction(5, 12)Independent events multiply
The two dice do not affect each other: they are independent. Then P(both happen) = P(first) × P(second). "Die 1 is 6" covers one row, 1/6. "Die 2 is 6" covers one column, 1/6. Both: one square, 1/6 × 1/6 = 1/36.
>>> first6 = {s for s in S if s[0] == 6} >>> second6 = {s for s in S if s[1] == 6} >>> P(first6.intersection(second6)) Fraction(1, 36) >>> P(first6) * P(second6) Fraction(1, 36)Chance in the long run
A probability also predicts the long run. Python's randint(1, 6) rolls a die. In 4 rolls we got two 7s, far from 1/6. In 60,000 rolls we got 9,978 sevens: 0.166, very close to 1/6. More rolls, closer match.
>>> seed(2026) # same seed, same rolls >>> rolls = [(randint(1, 6), randint(1, 6)) for _ in range(60000)] >>> rolls[:4] [(1, 3), (5, 5), (6, 1), (2, 5)] >>> sevens = sum(1 for a, b in rolls if a + b == 7) >>> sevens, round(sevens / 60000, 3) (9978, 0.166)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01You roll two fair dice. What is the probability that the sum is 2?
Only one of the 36 equally likely outcomes, (1, 1), sums to 2. The 11 possible sums are not equally likely, so 1/11 is wrong.
02P(it rains tomorrow) = 0.3. What is P(it does not rain tomorrow)?
Not raining is the complement of raining, so its probability is 1 − 0.3 = 0.7.
03P(A) = 0.5, P(B) = 0.4 and P(A and B) = 0.2. What is P(A or B)?
Add both and subtract the overlap, which was counted twice: 0.5 + 0.4 − 0.2 = 0.7.
04You flip a fair coin three times. What is the probability of three heads?
The flips are independent, so multiply: 1/2 × 1/2 × 1/2 = 1/8. Listing all 8 outcomes (HHH, HHT, ..., TTT) gives the same answer.
05A program rolls two dice 600 times and counts 104 sevens. What does this show?
A frequency from a finite number of trials varies around the true probability. With more rolls it tends to get closer; small gaps like this one are normal.
المسرد
- Experiment
- Something with an uncertain result that you can repeat, such as rolling two dice.
- Outcome
- One possible result of an experiment, such as the pair (3, 4).
- Sample space
- The set of all possible outcomes. For two dice it has 6 × 6 = 36 pairs.
- Event
- A set of outcomes, such as "the sum is 7". It happens if the outcome is in the set.
- Probability
- A number from 0 (impossible) to 1 (certain) that says how likely an event is. With equally likely outcomes it is the count of outcomes in the event divided by the count of all outcomes.
- Complement
- The event "not A": every outcome that is not in A. P(not A) = 1 − P(A).
- Independent events
- Events where one happening does not change the chance of the other; then P(A and B) = P(A) × P(B).
- Relative frequency
- How often an event happened in repeated trials, divided by the number of trials. Over many trials it tends toward the probability.
الملاحظات والمصادر
- Count-and-divide only works when every outcome is equally likely. If you listed the 11 possible sums (2 to 12) as the outcomes, they would not be: a sum of 7 happens 6 ways, a sum of 2 only 1 way. So P(sum is 7) is 1/6, not 1/11.
- Order matters in this sample space: (1, 6) and (6, 1) are different outcomes, because the two dice are different objects. That is why there are 36 outcomes and not 21.
- Fraction keeps answers exact: Fraction(6, 36) prints as Fraction(1, 6). Floats round, so float(Fraction(1, 6)) prints 0.16666666666666666.
- seed(2026) makes the random rolls repeatable, so you can check the numbers here. Python only promises the same random() sequence across versions, so randint may give other rolls in a different Python version; the share of 7s will still be close to 1/6.
- The Python lines in the steps build on each other: run them in order in one session, starting with the imports in step 1.
- Grinstead & Snell, Introduction to Probability (AMS), chapters 1, 4 and 8
- MIT OCW 18.05 Introduction to Probability and Statistics (Orloff & Bloom), class notes
- Lehman, Leighton & Meyer, Mathematics for Computer Science (MIT 6.042J), chapters 17 and 18
- Python library: fractions (exact rational numbers)
- Python library: random (seed, randint and reproducibility)
- Python library: set types (union, intersection)
اختبار يتاح بعد الخطوة الأخيرة.