我正在用R编写一个函数,以对Monty Hall问题进行蒙特卡洛模拟。当没有将门switch == FALSE
拨到mean(replicate(10000, monty_hall(switch = TRUE)))
时,该功能正常工作,但是当我打电话给0.66
时,预期的答案大约是0.25
,但实际上是monty_hall = function(switch = logical()){
doors <- c(1,2,3)
names(doors) <- rep(c("goat", "car"), c(2,1))
prize_door <- doors[3]
guess <- sample(doors, 1)
revealed_door <- sample(doors[!doors %in% c(guess, prize_door)],1)
if(switch){
switched_door <- sample(doors[!doors %in% c(guess, revealed_door)],1)
prize_door == switched_door
} else {
prize_door == guess
}
}
左右。>
以下是该函数的代码:
0.66
要获得正确的输出( Description: Item 'src\activation_kernels.cu' Code Generation value is not in the expected format '[Arch],[Code]'
Project: darknet
File: CUDA 10.0.targets, line 235
附近),我应该进行哪些更改?
答案 0 :(得分:5)
只需将门矢量更改为字符
monty_hall = function(switch = logical()){
doors <- c("1","2","3")
names(doors) <- rep(c("goat", "car"), c(2,1))
prize_door <- doors[3]
guess <- sample(doors, 1)
revealed_door <- sample(doors[!doors %in% c(guess, prize_door)],1)
if(switch){
switched_door <- sample(doors[!doors %in% c(guess, revealed_door)],1)
prize_door == switched_door
} else {
prize_door == guess
}
}
假设该人选择了1号门,奖品在2号门中,那么剩下要显示的就是3号门。
您将拥有revealed_door <- sample(3,1)
,并且此功能无法正常运行,它将变为revealed_door <- sample(c(1,2,3),1)
从功能文档中,只需输入?sample
如果x的长度为1,则为数字(即is.numeric),并且x> = 1, 通过样本采样从1:x开始。注意这个方便 当x的长度变化不定时,特征可能会导致不良行为 诸如sample(x)的呼叫
我认为最简单的解决方法是更改为字符,但是如果必须使用数字值,则只需检查向量的长度,然后返回该值(如果为1即可),否则可以执行示例