Python Monty hall模拟返回50%几率? 来自随机导入randint 门数=3 成功=0 尝试次数=0 尽管如此: 尝试: 门=[0]*门的数量 门[randint(0,numberOfDoors-1)]=1 所选=随机数(0,门数-1) 当门数>2时: notIn=-1 而notIn==-1: 索引=randint(0,门数-1) 如果门[索引]==0且索引!=被选中的: notIn=索引 如果未选择,则: 选择-=1 德尔多尔斯[诺丁] 门数-=1 #doors为2,因此未选择(0或1)将返回相反的值(1或0) 成功+=门[未选择] 尝试次数+=1 如果尝试%1000000==0: 打印浮动(成功)/浮动(尝试) 除键盘中断外: 打印浮动(成功)/浮动(尝试) 打破
经过几个小时的模拟,我的结果几乎精确到了50%——我是不是做错了什么 理论上,你选择的门的赔率介于1/3和2/3之间,所以你至少应该得到高于50的赔率Python Monty hall模拟返回50%几率? 来自随机导入randint 门数=3 成功=0 尝试次数=0 尽管如此: 尝试: 门=[0]*门的数量 门[randint(0,numberOfDoors-1)]=1 所选=随机数(0,门数-1) 当门数>2时: notIn=-1 而notIn==-1: 索引=randint(0,门数-1) 如果门[索引]==0且索引!=被选中的: notIn=索引 如果未选择,则: 选择-=1 德尔多尔斯[诺丁] 门数-=1 #doors为2,因此未选择(0或1)将返回相反的值(1或0) 成功+=门[未选择] 尝试次数+=1 如果尝试%1000000==0: 打印浮动(成功)/浮动(尝试) 除键盘中断外: 打印浮动(成功)/浮动(尝试) 打破,python,statistics,Python,Statistics,经过几个小时的模拟,我的结果几乎精确到了50%——我是不是做错了什么 理论上,你选择的门的赔率介于1/3和2/3之间,所以你至少应该得到高于50的赔率 答案似乎和我做了同样的事情(忽略了他对蒙蒂的选择没有做任何事情-我想说明这个概念)。你忘记了将门数(仍然关闭的门数,对吗?)重置回3。由于第一个的每次迭代,而True:代表一个新的游戏节目运行,因此该节目开始时所有三扇门都已关闭 from random import randint numberOfDoors = 3 success = 0
答案似乎和我做了同样的事情(忽略了他对蒙蒂的选择没有做任何事情-我想说明这个概念)。你忘记了将
门数
(仍然关闭的门数,对吗?)重置回3。由于第一个的每次迭代,而True:
代表一个新的游戏节目运行,因此该节目开始时所有三扇门都已关闭
from random import randint
numberOfDoors = 3
success = 0
attempts = 0
while True:
try:
doors = [0] * numberOfDoors
doors[randint(0, numberOfDoors - 1)] = 1
chosen = randint(0, numberOfDoors - 1)
while numberOfDoors > 2:
notIn = -1
while notIn == -1:
index = randint(0, numberOfDoors - 1)
if doors[index] == 0 and index != chosen:
notIn = index
if notIn < chosen:
chosen -= 1
del doors[notIn]
numberOfDoors -= 1
# doors is 2, so not chosen (0 or 1) will return the opposite (1 or 0)
success += doors[not chosen]
attempts += 1
if attempts % 1000000 == 0:
print float(success) / float(attempts)
except KeyboardInterrupt:
print float(success) / float(attempts)
break
下次,请尝试添加
print
语句以帮助您进行调试。在这种情况下,在分配汽车后立即添加打印门
,表明门
在第一次迭代后只有两个元素。不久前我自己写了一个Monty Hall模拟问题。也许它会帮助您编写代码-尤其是注释可能会很有用:
...
while True:
numberOfDoors = 3
try:
doors = [0] * numberOfDoors
doors[randint(0, numberOfDoors - 1)] = 1
...
来自未来进口部的
随机输入
结果=[]#包含模拟结果的列表,可以是“w”或“l”,也可以是“赢”或“输”
计数=0
虽然计算数学测验:什么是1/3和2/3之间的一半?不确定你的意思好吧…如果“理论上你选择的门在1/3和2/3之间”,为什么平均结果不应该介于两者之间?但我总是切换到未选择的选项。这就是monty hall问题的实质,正如你在解决方案Wesome中看到的那样,这不是问题所在。正在取得预期的结果。
from __future__ import division
import random
results = [] # a list containing the results of the simulations, either 'w' or 'l', win or lose
count = 0
while count <200: #number of simulations to perform
l = []
prize = random.randint(1, 3) #choose the prize door
initialchoice = random.randint(1, 3) #make an initial choice (the door the contestant chooses)
exposed = random.randint(1, 3) #choose the exposed door (the door the host chooses)
while exposed == initialchoice or exposed == prize: #make sure exposed is not same as prize or the initial choice
exposed = random.randint(1, 3)
if initialchoice != prize:
results.append('l') #if the initial choice was incorrect, append 'l'
else:
results.append('w') #if the initial choice was correct, append 'w'
count += 1
print 'prize door:', prize, 'initial choice:',initialchoice, 'exposed door:',exposed #print the results of the simulation
print
w = 0
for i in results:
if i == 'w':
w += 1
print w/len(results) #fraction of times sticking with the original door was successful