首页 > 解决方案 > 为什么 Monty Hall 的这种实现给出的概率是 50%?

问题描述

我最近看了一个关于蒙蒂霍尔问题的视频,并对参赛者在转换时有 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)

标签: pythonpython-3.xmathprobabilityprobability-density

解决方案


你已经找到了答案,但我想我会在此发布我的镜头:

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

推荐阅读