Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created August 30, 2013 01:47
Show Gist options
  • Select an option

  • Save actsasgeek/6385450 to your computer and use it in GitHub Desktop.

Select an option

Save actsasgeek/6385450 to your computer and use it in GitHub Desktop.
R implementation of the Monty Hall Problem.
evaluate_a_monty_hall_scenario <- function(switch=FALSE) {
options <- c(1,2,3)
car <- sample( 1:3, 1)
pick <- sample( 1:3, 1)
opened <- options[!(options %in% c( car, pick))]
closed <- options[!(options %in% c( pick, opened[ 1]))]
if (switch == TRUE) { pick = closed[ 1]}
return( car == pick)
}
evaluate_monty_hall_problem <- function(switch=FALSE) {
trials <- 10000
count = 0
for (i in 1:trials){
result = evaluate_a_monty_hall_scenario(switch)
if (result == TRUE) { count = count + 1}
}
answer = (count) / trials
return( answer)
}
evaluate_monty_hall_problem()
evaluate_monty_hall_problem( TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment