## Monte Carlo Script for Video ## Monte Carlo Procedure for Monty Hall MontyHall <- function(num.trials, change = TRUE){ # function to simulate Monty Hall problem # ARGS: number of trials and whether contestant changes door # OUTPUT: winning probability car.door <- sample(3, num.trials, replace=TRUE) selected.door <- sample(3, num.trials, replace=TRUE) # if you change doors, only lose if selected.door is car.door ifelse(change,return(mean(!car.door == selected.door)), return(mean(car.door == selected.door))) } MontyHall(20) MontyHall(20) set.seed(02022017) MontyHall(20, change=F) set.seed(02022017) MontyHall(20, change=F) MontyHall(1e6)