python - 蒙蒂霍尔模拟器有一个错误的结果

标签 python python-3.x math

我想做一些看似简单的事情,同时通过代码证明 Monty Hall,遗憾的是我得到的并不是问题的证明,而是完全相反的。无论我是否切换,我在 10000 次模拟中获胜的几率约为 33%。你能检查一下代码看看有什么问题吗?请记住,我是一名 Web 开发人员,所以我的 Python 技能不太好,代码可能看起来不太好(例如,我制作了一个可移动门列表,其中只有一个变量就可以正常工作)。干杯!

import random
av_doors = [1, 2, 3]
door_v = [1, 2, 3]
removable_doors = [1, 2, 3]
score = 0
loses = 0

door = int(input("Choose your door (1-3)"))
pick = int(door)-1

dec = input("Stay or switch? (0/1)")

n = 0
while n < 10000:
    removable_doors = [1, 2, 3]
    av_doors = [1, 2, 3]
    val = random.randint(1,3)
    if val == 1:
        door_v[0] = 1
        door_v[1] = 0
        door_v[2] = 0
        removable_doors.remove(1)
    elif val == 2:
        door_v[0] = 0
        door_v[1] = 1
        door_v[2] = 0
        removable_doors.remove(2)
    elif val == 3:
        door_v[0] = 0
        door_v[1] = 0
        door_v[2] = 1
        removable_doors.remove(3)

    try:
        removable_doors.remove(int(door))
    except:
        pass

    av_doors.remove(door)

    if len(removable_doors) == 2:
        rand_door = random.randint(0,1)
        if rand_door == 0:
            del av_doors[0]
        elif rand_door ==1:
            del av_doors[1]
    else:
        del av_doors[0]

    if dec == "1":   
        door = av_doors[0]
    else:
        pass

    pick = door-1
    if door_v[pick] == 1:
        score+=1
    else:
        loses+=1

    n+=1

print(score)
print(loses)

最佳答案

你的主人有时会移除后面有汽车的门...这是一个可行的解决方案,并且(我认为)写得更好一些,同时保留你的结构(在 python3 中,但这不应该引起麻烦):

import random

win = 0
lose = 0

switch = int(input("Stay or switch? (0-stay/1-switch):")) == 1

n = 0
while n < 10000:
    # Init
    door_values = [0, 0, 0]
    removable_doors = [0, 1, 2]
    available_doors = [0, 1, 2]

    # Placing the reward somewhere
    car_place = random.randint(0, 2)
    door_values[car_place] = 1
    removable_doors.remove(car_place)

    # Choose a door
    door_chosen = random.randint(0, 2)
    available_doors.remove(door_chosen)
    if door_chosen != car_place:
        removable_doors.remove(door_chosen)

    # Host removes a door that does not have the car and has not been chosen by the player

    door_removed_by_host = removable_doors[random.randint(0, len(removable_doors)-1)]
    available_doors.remove(door_removed_by_host)

    # Switch if specified
    if switch:
        assert(len(available_doors) == 1)
        door_chosen = available_doors[0]

    # Check the result
    if car_place == door_chosen:
        win += 1
    else:
        lose += 1

    n+=1

print('win=%s'%str(win))
print('lose=%s'%str(lose))
print('ratio=%s'%str(win/(win+lose)))

switch=False时,它给我0.3332,对于switch=True,它给我0.6738,我想这更合适证实了 Monty Hall 的解决方案:)

关于python - 蒙蒂霍尔模拟器有一个错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987782/

相关文章:

python - 在 Python 中构建二维数组

python - Boto3 S3,按上次修改对桶进行排序

c# - 如何计算旋转图像的正确位置偏移?

performance - 计算淡入淡出动画不同速度的方程

python - 打印由随机数选择的变量

python - 如何从分区 DF(非唯一索引)中选择带有索引列表的数据?

python-3.x - python 3 集合中的 Discard() 和 Remove() 函数有什么区别

javascript - 为什么 JavaScript Math.log(1.001) 返回错误值?

python - “coroutine”对象没有属性 get || pyppeteer

python - sys.stdout 在 bpython 中没有刷新属性