選擇「不換門」的獲勝機率
來寫些程式碼來模擬 Monty Hall 遊戲吧。這個練習中,參賽者固定選擇第 1 號門,每次都「不換門」,並在遊戲重複執行 10000 次後,計算相對出現頻率來估計獲勝機率。
我們先撰寫只執行一次的程式碼。接著把它放進 for 迴圈裡重複執行,以取得對真實獲勝機率的模擬估計,並運用前面提過的計數器概念。
本練習屬於課程
R 的機率謎題
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
set.seed(1)
doors <- c(1,2,3)
# Randomly select one of the doors to have the prize
prize <- sample(x = ___, size = ___)
initial_choice <- 1
# Check if the initial choice equals the prize
if(___){
print("The initial choice was correct!")
}
print(prize)