I am writing a program in R to do some simulations of the Monty Hall Problem explained here, https://www.youtube.com/watch?v=4Lb-6rxZxx0
Consider this code, sample(setdiff(doors, c(pick, car)),1)
"should" be 3 every time, but it isn't.
doors <- 1:3
pick <- 2
car <- 1
sample(setdiff(doors, c(pick, car)),1)
[1] 3
sample(setdiff(doors, c(pick, car)),1)
[1] 1
Any idea where I am going wrong?
Thank you.
Your issue is that you end up calling sample.int
since
doors <- 3L
pick <- sample(doors, 1)
car <- sample(doors, 1)
class(setdiff(doors, c(pick, car)))
#R [1] "integer"
and
length(setdiff(doors, c(pick, car)))
#R [1] 1
See help("sample.int")
or
body(sample)
#R {
#R if (length(x) == 1L && is.numeric(x) && is.finite(x) && x >=
#R 1) {
#R if (missing(size))
#R size <- x
#R sample.int(x, size, replace, prob)
#R }
#R else {
#R ...
There is no point in sampling unless you have more than one variable in your set.