I'm writing a function in R to perform a Monte Carlo simulation for the Monty Hall problem. The function is working when the doors are not switched it switch == FALSE, but when I call mean(replicate(10000, monty_hall(switch = TRUE))), the expected answer is about 0.66 but I actually get around 0.25.
Here is the code to the function:
monty_hall = function(switch = logical()){
doors <- c(1,2,3)
names(doors) <- rep(c("goat", "car"), c(2,1))
prize_door <- doors[3]
guess <- sample(doors, 1)
revealed_door <- sample(doors[!doors %in% c(guess, prize_door)],1)
if(switch){
switched_door <- sample(doors[!doors %in% c(guess, revealed_door)],1)
prize_door == switched_door
} else {
prize_door == guess
}
}
What changes should I make to get the correct output, which is around 0.66?