我正在尝试编写一个算法,它将允许我计算在一个可变数目的骰子上滚动一个6的概率。以下是我所拥有的:
num.dice <- 6
## x is the number of iterations
x <- 1
a <- c()
while(x < 1000) {
a[(x:x)] <- sum(sample(1:6, num.dice, replace=TRUE)==1)
x <- x + 1
}
sum(a)/1000
我更喜欢使用蒙特卡洛式的模拟,但我相信我不能正确计算6s。
我哪里出问题了?
发布于 2021-03-11 16:27:32
我的数学有点生疏,但这是你要找的吗?
library( gtools )
num_dice <- 1:6
lapply(num_dice, function(x) {
sum(
apply(
permutations( 6, x, 1:6, repeats.allowed = TRUE ),
1,
function(y) any( y == 6 )
)
) / 6^x
})
[[1]]
[1] 0.1666667
[[2]]
[1] 0.3055556
[[3]]
[1] 0.4212963
[[4]]
[1] 0.5177469
[[5]]
[1] 0.5981224
[[6]]
[1] 0.665102
https://stackoverflow.com/questions/66586253
复制相似问题