Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Birthday Paradox in Python
The Birthday Paradox is a famous probability problem that asks: "How many people need to be in a room before there's a 50% chance that two people share the same birthday?"
The counterintuitive answer is just 23 people! This paradox demonstrates how our intuition about probability can be misleading.
Understanding the Mathematics
The probability that two people have different birthdays is 364/365, which equals (1 - 1/365) in a non-leap year.
For multiple people, the probability that all have different birthdays is:
P(different) = 1 × (1-1/365) × (1-2/365) × (1-3/365) × ...
Therefore, the probability that at least two people share the same birthday is:
P(same) = 1 - P(different)
Formula for Required People
To find the approximate number of people needed for a given probability p, we use:
N = ?(2 × 365 × ln(1/(1-p)))
Python Implementation
Basic Formula
Here's how to calculate the required number of people for a given probability:
import math
def people_needed(probability):
"""Calculate people needed for given probability of shared birthday"""
return math.ceil(math.sqrt(2 * 365 * math.log(1 / (1 - probability))))
# Find people needed for 70% probability
result = people_needed(0.70)
print(f"People needed for 70% probability: {result}")
# Find people needed for 50% probability (classic birthday paradox)
result_50 = people_needed(0.50)
print(f"People needed for 50% probability: {result_50}")
People needed for 70% probability: 30 People needed for 50% probability: 23
Simulation Approach
We can also simulate the birthday paradox to verify our mathematical results:
import random
def simulate_birthday_paradox(num_people, trials=10000):
"""Simulate birthday paradox with given number of people"""
matches = 0
for _ in range(trials):
birthdays = set()
for _ in range(num_people):
birthday = random.randint(1, 365)
if birthday in birthdays:
matches += 1
break
birthdays.add(birthday)
return matches / trials
# Test with different group sizes
for people in [20, 23, 30, 50]:
probability = simulate_birthday_paradox(people)
print(f"{people} people: {probability:.2%} chance of shared birthday")
20 people: 41.12% chance of shared birthday 23 people: 50.73% chance of shared birthday 30 people: 70.63% chance of shared birthday 50 people: 97.04% chance of shared birthday
Comparison Table
| Number of People | Probability of Shared Birthday | Common Scenario |
|---|---|---|
| 23 | 50.7% | Classic birthday paradox |
| 30 | 70.6% | School classroom |
| 50 | 97.0% | Large office team |
| 70 | 99.9% | Almost certain match |
Conclusion
The Birthday Paradox demonstrates how probability can be counterintuitive. With just 23 people, there's a 50% chance of shared birthdays. The mathematical formula ?(2 × 365 × ln(1/(1-p))) provides accurate estimates for any desired probability.
---