我的python Monty大厅模拟有什么问题吗?

时间:2017-05-19 15:15:46

标签: python statistics

我在某处出错了吗?切换后我仍然获得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)

1 个答案:

答案 0 :(得分:4)

是的,你没有提到主持人不会透露所选门背后的事实。

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
相关问题