import math
# x is the size of your population
def birthday(x):
# Probability of having birthay in a specific day of the year
p = 1/365
q = 1-p
acc = 0
# Summing from 2 until size of population (you need at least 2)
for i in range(2,x+1):
acc += (math.factorial(n)*(p**i)*(q**(n-i)))/(math.factorial(i)*math.factorial(n-i))
return acc
#For y peoples; Or for y plays of roulette and x outcomes
def odds(x,y):
return x/x**y
print (odds(365,2))
#For at least 2 peoples within a group of n peoples
import math
def grp_odds(x,n):
return 1-math.factorial(x)/(x**n*math.factorial(x-n))
print (grp_odds(365,2))
print (grp_odds(365,5))
您试图计算的是Cumulative Probability of a Binomial Distribution。因此,对于
x
的人口,你想知道至少有两个人的生日在同一天的概率。您可以使用以下选项:输出:
你可以通过考虑2月29日怎么样来让它更华丽;)
相关问题 更多 >
编程相关推荐