用python编写“生日悖论”的解决方法

用python编写“生日悖论”的解决方法

题目
用python编写“生日悖论”的解决方法
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.
答案

import random

def bdp(n,k):

x09    cv = []

x09    for i in range(k):

x09x09        m = []

x09x09        for j in range(n):

x09x09x09            m.append(random.randint(1,365))

x09x09        counter = 0

x09x09            for k1 in m:

x09x09x09                for k2 in m:

x09x09x09x09                    if k1 == k2:

x09x09x09x09x09                        counter += 1

x09x09        cv.append(float(counter/2)/float(n))

x09        ss = 0

x09        for i in cv:

x09x09            ss += i

        return ss/float(len(cv))


亲测能用:

举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
最新试题
热门考点

超级试练试题库

© 2017-2019 超级试练试题库,All Rights Reserved.