Monty Hall simulator
This is a simulation of the famous Monty Hall problem. Writting out the code helped me have a better understanding of the problem, and it also helped me understand why it is always best to choose again.
Check out the code, run it and see for yourself. The only parameter that you have to give is “simu”, which is short for how many simulations do you want to run before you are convinced?
#Monty Hall problem
goat_simulator = function(repick){ doors = c('a', 'b', 'c')
prize = sample(doors, 1); pick = sample(doors, 1)
show = sample(doors[(doors != pick) & (doors!= prize)], 1)
if(repick == TRUE)
{ pick = doors[(doors != pick) & (doors!= show)] }
return (pick == prize) }
#many runs
comparison = function(simu){
change = c(); stay = c()
for(i in 1:simu){
change[[i]] = goat_simulator(TRUE)
stay[[i]] = goat_simulator(FALSE)
}
change = rapportools::percent(change == TRUE)
stay = rapportools::percent(stay == TRUE)
return(list(paste("changed = ", change, "%"," right", sep = ''),
paste("kept =", stay, "%"," right", sep = '')))
}
comparison(10000)
Conclusion
Out of 10 thousand runs, you get ~66% right if you change your initial choice. You only get ~33% right if you do not.