#
# Simulation estimation of the Monty Hall program with d >= 3 doors.
# It is assumed that if a change is made, one of the unopened doors is selected at random.
# The code is adapted from the Wiki.
#
montyhalld <- function(d=4,N=100){
selector <- function(x) { j <- (1:d)[-x]; if (length(j)>1) sample(j,1) else j }
prize <- sample(1:d,N,replace=TRUE)
choice <- sample(1:d,N,replace=TRUE)
winstick <- (choice==prize)
pwinstick <- sum(winstick)/N
opened <- apply(cbind(prize,choice),1,selector)
change <- apply(cbind(choice,opened),1,selector)
winchange <- (change==prize)
pwinchange <- sum(winchange)/N
return(list("pwinstick"=pwinstick,"pwinchange"=pwinchange))
}