昨天提到了三門問題
答案是應該要換門
我們可以用以下程式碼驗證
import numpy as np
import random
import matplotlib.pyplot as plt
# 設定實驗次數
play_times = 1000
# 門數
door_num = 3
doors = list(np.arange(door_num))
# 紀錄
change_win = 0
hold_win = 0
change_winrate_record = []
hold_winrate_record = []
for play in range(play_times):
# 決定寶物位置
treasure = random.sample(doors, 1)
# 第一次猜測
first_choice = random.sample(doors, 1)
# 開一個不是寶物也不是第一次猜測的門
open_door = random.sample([d for d in doors if d not in treasure + first_choice], 1)
# 重新選擇
change_choice = random.sample([d for d in doors if d not in open_door + first_choice], 1)
if treasure[0] == first_choice[0]:
hold_win = hold_win + 1
elif treasure[0] == change_choice[0]:
change_win = change_win + 1
# 取成比例
change_winrate_record.append(np.round(change_win/(play+1), 3))
hold_winrate_record.append(np.round(hold_win/(play+1), 3))
fig = plt.figure()
plt.plot(change_winrate_record, color = 'black', label = 'change')
plt.plot(hold_winrate_record, color = 'blue', label = 'hold')
plt.title('door_num = '+str(door_num)+', change : '+str(change_winrate_record[-1]))
plt.legend()
plt.show()
結果如圖
36:感覺好奇怪唷
RN:的確這剛開始的時候也有許多人很不適應呢
不過數學就是這麼奇妙(不會就是不會)~XD