ENع
→Probability
الدرس 14 · الاحتمالات والإحصاء

Probability: outcomes, events and chance

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 14 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. The 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)
    36
  3. An 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)]
  4. 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.16666666666666666
  5. Always 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)
  6. 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)
  7. 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)
  8. 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)
  9. 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)
  10. 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)

اختبار يتاح بعد الخطوة الأخيرة.