题目
用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.
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.
提问时间:2020-10-12
答案
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,人们染上烟瘾,最终因吸烟使自己丧命.
最新试题
- 1黄鹂在树林里婉转地鸣叫改为拟人句
- 2矩阵方程在什么情况下无解?
- 35/6-2.63+5又1/6-0.37
- 4描写月亮的作文
- 5by与before在表示在……之前有什么区别?
- 6《陈太丘与友期》中,文章最后,友人“惭”,下车“引”之,而元方却“入门不顾”.你认为元方的做法对吗
- 7用金属钠和乙醇反应产氢气,若以含少量水的乙醇代替同质量的无水乙醇相同条件下,测的氢气的体积增大
- 8若A的平方等于负八,则A等于多少
- 9要完全燃烧12g的碳,得到44g二氧化碳,实际消耗的氧气的质量是多少g?A:大于32g,B,小于32g,C:等于32
- 101.一个牧场,在、草每天均速生长,1头牛1天吃的草可供1只羊吃3天,这个牧场上的草可供18头牛吃30天,或者可供11头牛和24只羊吃24天.牧场主准备全部养羊,那么这个牧场最多可养多少只羊,才能使羊永
热门考点
- 11.青藏地区属于我国的第________级阶梯,此单元的主要升级行政单位是________和________,此单元在________朝归属于我国版图,中央社行政机构________管辖.
- 2P43.
- 3多边形的每个内角都等于150°,则从此多边形的一个顶点出发可作的对角线共有( ) A.8条 B.9条 C.10条 D.11条
- 4有关三角函数诱导公式sin(-α)=-sinα,cos(-α)=cosα,tan(-α)=-tanα
- 5观察下列等式:1*2=1/3(1*2*3-0*1*2),2*3=1/3*(2*3*4-1*2*3),3*4=1/3*(3*4*5-2*3*4)·····
- 6一个商场打折销售,规定购买200元以下(包括200元)的商品不打折200元以上,500元以下,包括500元,则全部打九折,如果买500元以上的商品那就把500元以内,包括500元的打九折,超出的打八折
- 7两题.如下.快
- 8解方程. 1、x+21%x=36.3 2、x-29%=213 3、60%x+25%x=10.2 4、90%x-18%x=54
- 9butt-steak什么意思
- 10Do you think a movie has to be expensive to make to become a blockbuster什么意思