python - 为什么 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)
解决方案
你已经找到了答案,但我想我会在此发布我的镜头:
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
推荐阅读
- http - Postman 400 Web API 的错误请求
- docker - 在跑步者和不同阶段之间共享人工制品
- mongodb - MongoDB中的标签自动完成
- javascript - 在 Javascript 中导入 Typescript 文件
- linux - Windows 和 Linux 之间的 git status 不一致
- python - 绘制图像的 PSD 与 x/y 轴的关系
- sql - SQL 更新表
- javascript - 如何将数据php返回到ajax
- macos - 如何使用 SystemConfiguration 框架检测 macOS 上的 IPv4 地址更改
- solace - 使用动态消息路由时,内部链接和外部链接有什么区别?