gpt4 book ai didi

python - 为什么我的 Monty Hall 解决方案不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:15:52 26 4
gpt4 key购买 nike

下面的代码是我的实现(Python 3.3.1),看看我能否证明开关在蒙提霍尔问题中是有效的。当我让玩家保留他们的第一选择时,我大致得到大约 0.33 的正确猜测结果,这是预期的。当我切换播放器时会出现问题 - 而不是获得预期的 ~0.66,我始终得到 ~0.55。

任何人都可以看到错误吗? (另外,作为旁注,如果我能对代码做出任何改进,我们将不胜感激)

def runInstance(switch): #run a single Monty Hall Problem instance
choicesList = [False,False,False]
intTruth = randint(0,2)
choicesList[intTruth] = True #list has been set up with 2 False, 1 True
intChoice = randint(0,2)
for index in range(0,len(choicesList)): #Loop finds entry index which is not chosen and is False to "present" to player
if( (intChoice != index) and (choicesList[index] == False) ):
alternate = index
if(switch):
for index in range(0,len(choicesList)): #Loop finds entry index which hasn't been chosen, and isn't the "Opened Door", then switches to it
if( (index != intChoice) and (index != alternate) ):
intChoice = index
return choicesList[intChoice]

def runBatch(inputSize, switch): #Run batch of instances for stats
successCount = 0.0
for index in range(0,int(inputSize)):
if(runInstance(switch)):
successCount += 1.0
print(str(successCount/inputSize))

runBatch(100000.0, True) #Number of instances to run, and boolean indicating whether to switch

最佳答案

将代码更改为:

origChoice = intChoice
if(switch):
for index in range(0,len(choicesList)): #Loop finds entry index which hasn't been chosen, and isn't the "Opened Door", then switches to it
if( (index != origChoice) and (index != alternate) ):
intChoice = index

问题是有时你会切换然后又切换回原来的选择。

换句话说,如果 intChoice=2,alternate=1 那么:

  1. 在第一次交互时 intChoice 将变为 0
  2. 在第二次迭代中什么也不会发生(因为 index==alternate)
  3. 在第三次迭代时 intChoice 将变回 2

关于python - 为什么我的 Monty Hall 解决方案不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com