gpt4 book ai didi

python - 我的 python Monty hall 模拟有什么问题吗?

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

我哪里做错了吗?即使在切换后我仍然保持 50% 的胜利。

import random

def monty_hall():

#-----setup ----#

prizes = ["Car" , "Goat" , "Goat"]
random.shuffle(prizes)

#----person chooses at random----#

choose_index = random.randint(0,2)

##-------host reveals a goat------#

while True:
goat_gate = random.randint(0, 2)
if prizes[goat_gate] == "Goat":
break

##------person switches -------##

while True:
switch_choice = random.randint(0, 2)
if (switch_choice!= choose_index) & (switch_choice!= goat_gate):
break

## -- check if won---#

if prizes[switch_choice] == "Car":
return True

win = 0
games = 100000

for times in range(games):
if monty_hall() == True:
win += 1

print(win/games)

最佳答案

是的,您没有包括主持人不会透露所选门后的内容这一事实。

while True :
goat_gate = random.randint(0, 2)
if prizes[goat_gate] == "Goat" :
break

应该是

while True :
goat_gate = random.randint(0, 2)
if prizes[goat_gate] == "Goat" and goat_gate != choose_index:
break

关于python - 我的 python Monty hall 模拟有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44073609/

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