Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: Poker, Lottery and Birthdays

Apply counting to real probabilities: count all poker hands exactly, compute lottery match odds with hypergeometric counting, and compare the birthday paradox with a simulation.

Probability on equally likely outcomes is counting: P(event) = (favorable outcomes) / (all outcomes). In this project you compute three famous probabilities exactly, using nothing but the tools from this course, and then confirm one of them by simulation.

Part 1: five-card poker hands

A deck has 52 cards, 13 ranks times 4 suits. A hand is a 5-card subset: binom(52, 5) = 2,598,960 hands. We count each category.

  • Four of a kind: choose the rank (13), take all four, choose the fifth card from the other 48: 13 * 48 = 624.
  • Full house: choose the rank of the triple and its 3 suits, then a different rank for the pair and 2 suits: 13 * binom(4,3) * 12 * binom(4,2) = 3744.
  • Straight flush (including royal): 10 starting ranks (A-5 up to 10-A) times 4 suits: 40.
  • Flush (not straight): 4 * binom(13,5) same-suit hands, minus the 40 straight flushes: 5108.
  • Straight (not flush): 10 rank sequences, each card any suit 4^5, minus the straight flushes: 10200.
  • Three of a kind: rank and suits for the triple, two other distinct ranks and one suit each: 13 * binom(4,3) * binom(12,2) * 4^2 = 54912.
  • Two pair: two ranks for the pairs, their suit pairs, then the fifth card from the other 44: binom(13,2) * binom(4,2)^2 * 44 = 123552.
  • One pair: 13 * binom(4,2) * binom(12,3) * 4^3 = 1098240.
  • High card: five distinct ranks not forming a straight, not all one suit: (binom(13,5) - 10)(4^5 - 4) = 1302540.

These categories are disjoint and exhaustive, so they must add up to the total. That is the best possible check.

Python All poker hands, and the lottery

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output shows the familiar table: a pair appears in about 42.3% of hands, a full house in 0.14%, and a straight flush in 0.0015%. The True confirms that the nine counts add to 2,598,960 exactly.

Part 2: lottery odds

In a 6-from-49 lottery you pick 6 numbers; the draw takes 6 distinct numbers. The total number of draws is binom(49, 6) = 13,983,816. To match exactly k of your numbers, choose which k of your 6 are drawn and which 6 - k of the 43 other numbers fill the rest:

binom(6,k) * binom(43, 6-k).

This is the hypergeometric distribution, built from the product rule over two groups. The printed table gives 1,851,150 ways to match 2 numbers (13.2%), 246,820 for 3 (1.77%), 13,545 for 4, 258 for 5 and just 1 for all 6, probability about 7.2 * 10^-8. Buying one ticket a week, you would expect to win the jackpot about once in 269,000 years.

Part 3: the birthday paradox

How likely is it that among n people two share a birthday? Count the complement: all n birthdays distinct, 365 * 364 * ... * (365 - n + 1) (a partial permutation style count) out of 365^n. So

P(shared) = 1 - prod_{i=0}^{n-1} (365 - i)/365.

The classic surprise: with just 23 people the probability is already above 50%. A good approximation is 1 - exp(-n(n-1)/(2*365)), because there are binom(n,2) pairs, each matching with probability 1/365.

The same formula governs hash collisions. For a 32-bit hash (N = 2^32 values), a collision becomes a coin flip after only about 77,000 items.

Python Exact vs simulated vs approximate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For n = 23 the exact probability is 0.5073, the simulation (20,000 random rooms, fixed seed) gives 0.5062, and the approximation 0.5. Exact and simulated values agree to about 0.01, as expected from random sampling error of order 1/sqrt(20000). For 32-bit hashes, 1,000 items collide with probability 0.0001, 77,163 items with 0.5 and 200,000 items with 0.99.

flowchart TD
    A["Probability question"] --> B["Define equally likely outcomes"]
    B --> C["Count favorable outcomes"]
    C --> D["Divide by total count"]
    D --> E["Sanity check: categories sum to the total"]
    E --> F["Optional: simulate"]

Probability by counting.

Extensions

  1. Count seven-card hands (Texas hold'em) for a pair or better.
  2. Compute the chance that someone in a class of 30 shares your birthday (1 - (364/365)^29, a different question!).
  3. Find the smallest group where three people share a birthday (harder; simulate first).
  4. Replace comb(43, 6-k) by a function for a 5-from-35 lottery with a bonus ball.

"Someone shares a birthday with you" and "some two people share a birthday" are different events. The first needs about 253 people for 50%; the second only 23.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many 5-card hands are there in a 52-card deck?

    Order does not matter, so it is binom(52,5) = 2,598,960.
  2. 2. Why must the nine poker categories add up to binom(52,5)?

    Every hand falls in exactly one category, so any counting mistake would break the total.
  3. 3. How many ways match exactly 3 numbers in a 6-from-49 lottery?

    binom(6,3) * binom(43,3) = 20 * 12341 = 246,820.
  4. 4. The birthday paradox: roughly how many people make a shared birthday more likely than not?

    With 23 people the probability is about 0.507.

Summary

  • Probability with equally likely outcomes is a ratio of counts; check that case counts sum to the total.
  • Poker: nine categories of the 2,598,960 hands; lottery: hypergeometric binom(6,k) binom(43,6-k)/binom(49,6).
  • Birthday collisions: 1 - prod (365-i)/365 ~ 1 - exp(-n(n-1)/(2N)), the same law behind hash collisions.

Next: Practice Set: Counting Problems