我最近观看了一段关于Monty Hall Problem的视频,并对参赛者在转换时有2/3的获胜机会感兴趣。所以,我决定为它自己编写一个模拟模型。但是,我的模拟给出的答案是50%。有人可以指出为什么吗?注意:可以通过更改num_of_doors
。
from random import randint
num_of_doors = 3
num_of_simulations = 0
wins = 0
while True:
num_of_simulations += 1
doors = {k: "Donkey" for k in range(1, num_of_doors + 1)}
car = randint(1, num_of_doors)
doors[car] = "Car"
choice = randint(1, num_of_doors)
while len(doors) > 2:
reveal = randint(1, num_of_doors)
if reveal in doors:
if reveal != choice and doors[reveal] != "Car":
del doors[reveal]
for k in doors:
if k != choice:
choice = k
if doors[choice] == "Car":
wins += 1
print(100 * wins / num_of_simulations)
答案 0 :(得分:1)
你找到了答案,但我想我只是发布了我的镜头:
from random import randint
def monty(n_doors):
car = randint(0, n_doors - 1)
first_choice = 0 # always pick 0 for convenience
remaining_door = car if first_choice != car else 1 # 1 for convenience
return remaining_door == car
total_runs = 10000
trials = [monty(3) for x in range(total_runs)]
print(sum(trials) / total_runs)
给出:
0.6705