Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
The same class have the same birthday probability.
A class has 25 students, the number of birthdays in the same day, the probability is that?
Code:
遇到问题没人解答?小编创建了一个Python学习交流QQ群:895817687 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
import numpy as np
from numpy import random
NUM_STUDENT = 25
NUM_EXER = 10000
count=0
for i in range(NUM_EXER):
birtdays = []
for j in range(NUM_STUDENT):
birtdays.append(np.random.randint(356))
if len(set(birtdays)) < NUM_STUDENT:
count +=1
print(count/NUM_EXER)
Repeat 10,000 times test python, the probability of 57.6%. As long as there are 25 students, there is a possibility that more than half of likely birthdays on the same day.
Direct calculation:
Probability does not appear the same birthday:
probability is the same birthday occurs: 1-p 1-p1- p
When n = 25, is calculated using the python:
def factorial(n):
a = 1
for i in range(1, n+1):
a = a*i
return a
k = 1 - factorial(365)/(factorial(365-NUM_STUDENT)*365**NUM_STUDENT)
print(k)
Output 56.8%
Several tests have some errors, perhaps python calculation rounding? Or what went wrong?
postscript
Why is there such a situation?