我正在编写蒙蒂霍尔问题的变体,建立在另一个人的代码之上。不同之处在于,我有“n”个门,而不是 3 个门。n = 4
就这个问题来说吧。门上有标签A, B, C and D
。
代码如下:
n <- 4
doors <- LETTERS[seq( from = 1, to = n )]
xdata = c()
for(i in 1:10000) {
prize <- sample(doors)[1]
pick <- sample(doors)[1]
open1 <- doors[which(doors != pick & doors != prize)]
open <- sample(open1,n-2)
# the line with the problem
switchyes <- doors[which( doors != open & doors != pick)]
if(pick==prize) {
xdata <- c(xdata, "noswitchwin")
}
if(switchyes==prize) {
xdata=c(xdata, "switchwin")
}
}
当我运行代码时,我收到警告:
There were 50 or more warnings (use warnings() to see the first 50)
问题似乎是由于该行:
switchyes <- doors[which( doors != open & doors != pick)]
这应该只返回 1 个项目 ( C
),因为声明doors != open
和doors != pick
消除了门A
和B
和D
。但是,我得到的不止一个,B
而且C
. 有人看到发生了什么吗?
length(which(xdata == "switchwin"))
# [1] 4728
length(which(xdata == "noswitchwin"))
# [1] 2424
switchyes
# [1] "B" "C"
open
# [1] "B" "D"
open1
# [1] "B" "D"
pick
# [1] "A"
prize
# [1] "C"