参赛者会看见三扇关闭了的门,其中一扇的后面有一辆汽车,选中后面有车的那扇门可赢得该汽车,另外两扇门后面则各藏有一只山羊。当参赛者选定了一扇门,但未去开启它的时候,节目主持人开启剩下两扇门的其中一扇,露出其中一只山羊。主持人其后会问参赛者要不要换另一扇仍然关上的门。问题是:换另一扇门会否增加参赛者赢得汽车的机率?如果严格按照上述的条件,即主持人清楚地知道,自己打开的那扇门后是羊,那么答案是会。不换门的话,赢得汽车的几率是1/3。换门的话,赢得汽车的几率是2/3。
def three_doors_question(times: int):
import random
not_change = 0
change = 0
for i in range(0, times):
doors = [1, 2, 3]
random.shuffle(doors)
first_choice = doors[random.randint(0, 2)]
doors.remove(first_choice)
if 1 in doors:
doors = [1]
else:
doors.remove(random.choice((2, 3)))
if first_choice == 1:
not_change = not_change + 1
if doors[0] == 1:
change = change + 1
print(
f'Number of simulations:{times}, No change:{not_change / times}, 'f'change:{change / times}')
three_doors_question(1000)
three_doors_question(10000)
three_doors_question(50000)
three_doors_question(100000)
three_doors_question(1000000)