码迷,mamicode.com
首页 > 编程语言 > 详细

Python羊车门作业

时间:2017-04-04 21:32:46      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:random   rand   man   img   and   blog   概率   验证   ges   

1、我认为会增加选中的概率。

原因如下:

如果不更换选择,则选手选中汽车的概率是1/3;

如果更换选择,则选手选中汽车的概率是1/3*0+2/3*1=2/3。

2、程序源代码如下:

from random import *

def once(doors = 3): # 一次事件的模拟

  car = randrange(doors) # 一扇门后面停着汽车

  man = randrange(doors) # 参赛者预先选择一扇门

  return car == man # 参赛者是否最初就选择到车
h = 0 # 坚持选择赢得汽车的次数

c = 0 # 改变选择赢得汽车的次数

times = int(1e6) # 重复实验的次数
for i in range(times):

  if once(): h += 1

   else: c += 1

print("维持选择:",h/times*100,"%\n改变选择:",c/times*100,"%")

3、运行的验证结果如下:

技术分享

Python羊车门作业

标签:random   rand   man   img   and   blog   概率   验证   ges   

原文地址:http://www.cnblogs.com/Python15/p/6666468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!