A simulation of the Monty Hall problem outcomes for n doors (k opened) à la Tidyverse…
library(tidyverse) # sample vectors whether they have one or more elements resample <- function(x, ...) x[sample.int(length(x), ...)] monty <- function(doors = 3, monty_opens_doors = 1, n = 10000, seed = 0) { set.seed(seed) tibble(car = sample(doors, n, replace = TRUE), choice = sample(doors, n, replace = TRUE)) %>% rowwise() %>% mutate(monty_chose = list(resample(setdiff(1:doors, c(car, choice)), monty_opens_doors)), win_no_switch = car == choice, win_switch = car == resample(setdiff(1:doors, unlist(c(choice, monty_chose))), 1)) %>% ungroup() %>% summarise(win_if_not_switching = sum(win_no_switch) / n() * 100, win_with_switching = sum(win_switch) / n() * 100) }
> monty() # classic values # A tibble: 1 x 2 win_if_not_switching win_with_switching <dbl> <dbl> 1 33.4 66.6 > monty(10) # more doors (10), 1 opened # A tibble: 1 x 2 win_if_not_switching win_with_switching <dbl> <dbl> 1 10.4 11.0 > monty(10, 3) # 10 doors, 3 opened # A tibble: 1 x 2 win_if_not_switching win_with_switching <dbl> <dbl> 1 10.4 15.2
So, switch…