The Birthday Paradox (http://en.wikipedia.org/wiki/Birthday_problem) states
that if 23 people are selected at random, there is better than 50% chance that at
least two of them will have the same birthday (not considering the birth year).
You are to write a Python function to simulate selecting n people at random
and checking the probability of having at least two people with the same
birthday. You should ignore the leap years and assume 365-day years. To be
more specific, devise a Python function, call it bdp(n,k), that once invoked, will
select n numbers with replacement from the set of numbers 1 through 365
inclusive, determine if two or more of the numbers selected are the same (call
it a hit), and repeat this task k times, and finally return the percentage of the
hits. Plot your results similar to the graph in the above URL.
题目如上
悬赏100
import random
def bdp(n,k):
cv = []
for i in range(k):
m = []
for j in range(n):
m.append(random.randint(1,365))
counter = 0
for k1 in m:
for k2 in m:
if k1 == k2:
counter += 1
cv.append(float(counter/2)/float(n))
ss = 0
for i in cv:
ss += i
return ss/float(len(cv))
亲测能用:
生日悖论是正确的吗?
经过严格的计算和实际模拟,可以明确肯定,生日悖论确实存在且是正确的。在课堂上,我用Python编写程序进行验证,当模拟50个人的班级,经过10万次样本,发现有97,142次样本中出现了至少两个同学有相同的生日,概率达到了惊人的97.142%。这个结果是通过随机生成计算机上的生日来得出的。从概率论的角度解释,...
生日悖论是正确的吗?
可以很负责告诉你,完全正确。这个问题是我们python老师上课布置的题,我用python写过一个程序来模拟这个问题,可以看到结果,如果是50个人的班,在经过10万个样本班级模拟,97142个样本有相同的,概率97.142%(生日是通过计计算机随机数生成的)现在从概率论来给你解释:有n个人,第一个人生日是365选365 ...