python_蒙提霍尔问题

蒙提霍尔问题:假如你参与一个有主持人的游戏,你会看见三扇关闭了的门,其中一扇的后面有一辆汽车,另外2扇门后面各是一只山羊,你看不见门后面的情况,但主持人知道一切。你被主持人要求在三扇门中选择一扇,但不能打开,在你选定之后主持人开启了另一扇后面有山羊的门,然后你可以坚持原来选定的门,也可以改主意重新选择。
问题是:改与不改对选中汽车的概率有影响吗?请使用模拟实验的方法回答该问题。

import numpy as np

def hanshu(n):
    x = np.random.randint(0, 3, n)
    first_time = 0; second_time = 0
    for xx in x:
        answer = np.random.randint(0, 3)
        if(xx == answer):
            first_time += 1
        else:
            second_time += 1

    first_rate = first_time / n
    second_rate = second_time / n
    fi = round(first_time / second_time, 2)
    return first_rate, second_rate, fi

first_rate, second_rate, fi = hanshu(100000)
print("first_rate: ", first_rate)
print("second_rate: ", second_rate)
print("fi: ", fi)


可见,改变后的概率是不改变的两倍

posted @ 2021-10-25 09:13  pha创噬  阅读(60)  评论(0编辑  收藏  举报