I tried simulating the "birthday paradox" in Python

"** 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}')

Execution result

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

I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I tried to graph the packages installed in Python
I wrote the queue in Python
I tried Line notification in Python
I wrote the stack in Python
I tried the accuracy of three Stirling's approximations in python
I tried programming the chi-square test in Python and Java.
I tried to implement the mail sending function in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
Python: I tried the traveling salesman problem
I tried the Python Tornado Testing Framework
Movement that changes direction in the coordinate system I tried Python 3
[Python] I tried substituting the function name for the function name
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
vprof --I tried using the profiler for Python
I tried "differentiating" the image with Python + OpenCV
I tried playing a typing game in Python
I tried Python> autopep8
I tried python programming for the first time.
[Memo] I tried a pivot table in Python
I tried "binarizing" the image with Python + OpenCV
I tried to implement TOPIC MODEL in Python
I tried using the Datetime module by Python
I tried non-blocking I / O Eventlet behavior in Python
I implemented the inverse gamma function in python
I tried adding a Python3 module in C
I tried to implement selection sort in python
I tried Python> decorator
I want to display the progress in Python!
I tried Python on Mac for the first time.
I tried using TradeWave (BitCoin system trading in Python)
I tried to solve the soma cube with python
I tried to implement a pseudo pachislot in Python
I tried python on heroku for the first time
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
[Python] I tried to graph the top 10 eyeshadow rankings
I want to write in Python! (3) Utilize the mock
I tried to summarize how to use pandas in python
I tried to solve the problem with Python Vol.1
I want to use the R dataset in python
Python OpenCV tried to display the image in text.
I tried hitting the API with echonest's python client
I tried to summarize the string operations of Python
I tried fp-growth with python
I tried scraping with Python
I wrote python in Japanese
Download the file in Python
Find the difference in Python
I tried the changefinder library!
I tried Python C extension
[Python] I tried using OpenPose
I tried gRPC with Python
I tried scraping with python
I understand Python in Japanese!