Python Monty Hall 模拟没有给出预期的答案

问题描述 投票:0回答:3

我在 Monty Hall 模拟中得到答案 0.5。

来自教科书:我们假设通过滚动三面骰子将汽车放在门后面,这使得所有三种选择的可能性相同。蒙蒂知道汽车在哪里,并且总是打开一扇门,后面有一只山羊。最后,我们假设如果 Monty 可以选择门(即参赛者选择了后面有汽车的门),他以 1/2 的概率选择每扇门。玛丽莲显然希望她的读者假设游戏是这样玩的。

Marilyn 的答案是 0.66,我想模拟这个答案,但我得到了 0.5,不知道我的代码有什么问题。

n = 1000000
count = 0
for i in range(n):
    doors = [1,2,3]  
    # the inital doors that monty can choose
    monty_choose = [1,2,3]
    # suppose the car is behind door 1
    car = 1   
    # monty cannot choose the door that has car
    monty_choose.remove(car)    
    ichoose = random.choice(doors)  
    if ichoose in monty_choose:
        # monty cannot choose the door i select
        monty_choose.remove(ichoose)  
        monty = random.choice(monty_choose)  
    else:
        monty = random.choice(monty_choose)
    # i cannot choose the door that monty chose  
    doors.remove(monty)   
    s = random.choice(doors)
    if s == car:
        count = count + 1 
print(count/n)
python logic simulation
3个回答
1
投票

您的代码可以正常查找,直到到达最后一位。您正在随机选择门:


 s = random.choice(doors)
    if s == car:
        count = count + 1 

当您想做的是换门时。您可以通过简单地删除您的第一个选择然后将列表索引为 0 来完成此操作。

    doors.remove(ichoose)
    
    if doors[0] == car:
        count = count + 1 

完整代码和结果

import random

n = 1000000
count = 0
for i in range(n):
    doors = [1,2,3]  
    # the inital doors that monty can choose
    monty_choose = [1,2,3]
    # suppose the car is behind door 1
    car = 1   
    # monty cannot choose the door that has car
    
    monty_choose.remove(car)    
    ichoose = random.choice(doors)  
    if ichoose in monty_choose:
        # monty cannot choose the door i select
        monty_choose.remove(ichoose) 
        monty = random.choice(monty_choose)
    else:
        monty = random.choice(monty_choose)
    # i cannot choose the door that monty chose  
    doors.remove(monty)  
    
    
    doors.remove(ichoose)
    
    if doors[0] == car:
        count = count + 1 
        
        
print(count/n)
0.667145


1
投票

您的代码计算

0.5
的概率只是因为
s = random.choice(doors)
同等地选择汽车或山羊。

您的代码并未反映 Monty Hall 问题的工作原理。

如果参赛者做出选择并坚持该选择,那么概率显然是

0.33
。你永远不允许
ichoose
坚持他们的选择。

不太明显的部分是,参赛者可以改变他们的选择,那么概率是

0.66
。你永远不允许
ichoose
改变他们的选择。


0
投票
doors = [1,2,3]  # total doors
i_choose = [1,2,3] # the inital doors that I can choose
car = 1   # suppose the car is behind door 1
host_choose = [2,3] # the empty doors the host could show
n = 1000000
count = 0
car = 1
for i in range(n):
    # you can randomize car here, but remember to change host_choose accordingly
    first_choice = random.choice(doors) # I choose one door randomly
    if first_choice in host_choose:
        host_choose.remove(first_choice) # the host cannot open the chosen door
    host_choice = random.choice(host_choose) # the host shows that a door is empty
    i_choose.remove(host_choice)
    i_choose.remove(first_choice)
    # the goal is to show that always changing door results in 66% winrate
    i_choice = random.choice(i_choose) # this is actually a one-element list
    if i_choice == car:
        count = count + 1
    i_choose = [1,2,3]
    doors = [1,2,3]
    host_choose = [2,3]
print(count/n)

所以基本上,你混淆了选择的对象和时间:

  1. A 在 [1, 2, 3] 中选择一扇门
  2. B 知道汽车在哪里,并显示一扇空门(A 没有选择)
  3. 现在可以选择保留他们的门或更换它

你的目标是证明换门导致获得汽车的概率为 0.66。

© www.soinside.com 2019 - 2024. All rights reserved.