问题名字来自该节目的主持人蒙提·霍尔(Monty Hall)。参赛者会看见三扇关闭了的门,其中一扇的后面有一辆汽车,选中后面有车的那扇门可赢得该汽车,另外两扇门后面则各藏有一只山羊。当参赛者选定了一扇门,但未去开启它的时候,节目主持人开启剩下两扇门的其中一扇,露出其中一只山羊。主持人其后会问参赛者要不要换另一扇仍然关上的门。
利用Python编程,验证换门与不换门获胜概率。
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 23 16:00:51 2018
@author: Ivan
"""
import numpy as np
repetition = 100000 #游戏重复次数
result_list = [] #创建空列表用于储存结果
for i in range(repetition):
award_position = np.random.randint(0,3)
doors = np.zeros(3)
doors[award_position] = 1
doors = doors.tolist() #生成待猜列表
guess_position = np.random.randint(0,3) #第一次猜测的门编号
first_result = doors[guess_position] #第一次猜测的结果
del doors[guess_position]
doors.remove(0) #打开一个空门
change = True #选择是否换门
if change:
final_result = doors[0]
else:
final_result = first_result
result_list.append(final_result)
win = result_list.count(1) #统计获胜次数
probability = win/repetition
经统计,换门获胜概率为三分之二,不换门获胜概率为三分之一。