"** How many people will have a 50% chance of having two (or more) people with the same birthday? **" That is.
――The probability that there are people with the same birthday combination in the group exceeds ** 50% ** from ** 23 people **. --When 70 people gather, it exceeds ** 99.9% **
[Birthday Paradox-Wikipedia](https://ja.wikipedia.org/wiki/%E8%AA%95%E7%94%9F%E6%97%A5%E3%81%AE%E3%83% 91% E3% 83% A9% E3% 83% 89% E3% 83% 83% E3% 82% AF% E3% 82% B9)
Even if only 70 people gather, the probability is over 99.9%, and only 23 people are needed to exceed 50%.
** * Leap years and twins are not considered **
birthday_paradox.py
import math
from random import random
n_trials = 50000 #Number of trials per trial
max_people = 100 #Maximum number of people to verify
print('Number of people,Number of times there was a group with the same birthday,Number of trials,probability')
for n_people in range(2, max_people + 1):
count = 0 #Number of times there were people with the same birthday
for _ in range(n_trials):
#Store a random integer from 1 to 365 that looks like a birthday in the list.
births = []
for __ in range(n_people):
birthday = math.ceil(random() * 365)
births.append(birthday)
#If the number of elements excluding duplicates does not match the original number of elements, there is a duplicate birthday.
if len(births) != len(set(births)):
count += 1
print(f'{n_people},{count},{n_trials},{count / n_trials}')
Number of people,Number of times there was a group with the same birthday,Number of trials,probability
2,105,50000,0.0021
3,390,50000,0.0078
4,773,50000,0.01546
5,1370,50000,0.0274
6,1992,50000,0.03984
7,2853,50000,0.05706
8,3686,50000,0.07372
...
17,15850,50000,0.317
18,17422,50000,0.34844
19,18898,50000,0.37796
20,20602,50000,0.41204
21,22207,50000,0.44414
22,23795,50000,0.4759
23,25531,50000,0.51062 # <-50 when over 23 people%Beyond
24,27170,50000,0.5434
25,28517,50000,0.57034
26,29820,50000,0.5964
27,31400,50000,0.628
...
68,49925,50000,0.9985
69,49940,50000,0.9988
70,49952,50000,0.99904 # <-99 when over 70 people.9%Beyond
71,49971,50000,0.99942
72,49970,50000,0.9994
73,49971,50000,0.99942
74,49985,50000,0.9997
75,49987,50000,0.99974
76,49989,50000,0.99978
77,49994,50000,0.99988
78,49994,50000,0.99988
79,49994,50000,0.99988
...
99,50000,50000,1.0
100,50000,50000,1.0 # <-No longer 100%!!
Recommended Posts