gpt4 book ai didi

Python3 脚本未产生预期结果

转载 作者:行者123 更新时间:2023-11-28 22:32:58 26 4
gpt4 key购买 nike

我制作了一个 Python 脚本来测试 Monty Hall Problem我的问题是代码似乎输出了 30% 的赢和 60% 的输,而本应相反。

from random import randint
wins = 0
losses = 0
for i in range(1000):
correctDoor = randint(1, 3)
guessDoor = randint(1, 3)
while True:
newGuessDoor = randint(1, 3)
if newGuessDoor != guessDoor:
break
elif newGuessDoor == guessDoor:
pass
if newGuessDoor == correctDoor:
wins = wins+1
else:
losses = losses+1
print('Wins = ' + str(wins) + '\nLosses = ' + str(losses) + '')

我觉得我遗漏了一些明显的东西。任何帮助将不胜感激。

最佳答案

这实际上并没有正确实现 Monty Hall 问题。当最初的猜测不正确时,另一扇不正确的门在被提供切换之前会被显示出来,因此这是一个确定性的选择。当初始猜测正确时,将随机显示其他不正确的门之一。

在您的实现中,您在最初猜测后没有得到任何额外信息——您只是完全随机选择第二扇门,它与您的第一扇门不同。

以下是如何实现正确方法的示例:

In [168]: from random import randint, choice
...: wins = 0
...: losses = 0
...: for i in range(1000):
...: correctDoor = randint(1, 3)
...: guessDoor = randint(1, 3)
...: if guessDoor != correctDoor:
...: newGuessDoor = correctDoor
...: else:
...: newGuessDoor = choice([i for i in [1,2,3] if i != guessDoor])
...: if newGuessDoor == correctDoor:
...: wins = wins+1
...: else:
...: losses = losses+1
...: print('Wins = ' + str(wins) + '\nLosses = ' + str(losses) + '')
...:
Wins = 653
Losses = 347

关于Python3 脚本未产生预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40410654/

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