# Monty Hall Problem -- Solve using loops more compactly # Chris Adolph # 1/6/2005 sims <- 10000 # Simulations run doors <- c(1,0,0) # The car (1) and the goats (0) cars.chosen <- 0 # Save cars from first choice here cars.rejected <- 0 # Save cars from switching here for (i in 1:sims) { first.choice <- sample(doors,3,replace=F) cars.chosen <- cars.chosen + first.choice[1] cars.rejected <- cars.rejected + sort(first.choice[2:3])[2] } cat("Probability of a car from staying with 1st door", cars.chosen/sims,"\n") cat("Probability of a car from switching to 2nd door", cars.rejected/sims,"\n")