Monty+Hall+Simulation+with+R

montyhall <- function(n) { doors <- c("A","B","C") xdata=c for(i in 1:n) { prize <- sample(doors)[1] pick <- sample(doors)[1] open <- sample(doors[which(doors != pick & doors != prize)])[1] switchyes <- doors[which(doors != pick & doors != open)] if(pick == prize){xdata=c(xdata,"noswitchwin")} if(switchyes == prize){xdata=c(xdata,"switchwin")} } switchwin <- length(which(xdata == "switchwin")) noswitchwin <- length(which(xdata == "noswitchwin")) results <- c(switchwin,noswitchwin) print(results) xdata }