Verify birthday "paradox" with program

The birthday "paradox" is actually not a paradox. It means that in a group with more than 23 people, the probability that at least two people will have a birthday on the same day is about 0.5. Because this theoretical probability does not match people's intuition, it is called a "paradox".

We can verify it with a simple applet:

import random
def birth_paradox(n):
    dic={}
    for i in range(n):
        m=random.randint(1,12)
        if m in [1,3,5,7,8,10,12]:
            d=random.randint(1,31)
        elif m in [2]:
            d=random.randint(1,28)
        else:
            d=random.randint(1,30)
        dic[i]=m,d
    if len(set(list(dic.values())))<n:
        return True
    else:
        return False
t=0
for i in range(1000):
    if birth_paradox(23):
        t+=1
print(t/1000)

The results are as follows:


Due to the introduction of random numbers, the result of each run is slightly different ~

Released nine original articles · won praise 1 · views 6066

Guess you like

Origin blog.csdn.net/wcysghww/article/details/80456640