I need to run a fair dice simulation multiple times

时光怂恿深爱的人放手 提交于 2019-12-25 18:31:21

问题


I have a program that simulates a dice roll 100 times. I need to know how to run this program 10^5 times, I think it is something to do with numeric.

 set.seed(123)

 x <- sample(1:6, size=100, replace = TRUE)

 hist(x,
 main="10^6 fair rolls",
 xlab = "Dice Result",
 ylab = "Probability",
 xlim=c(0.5,6.5),
 breaks=-1:100+.5,
 prob=TRUE )

回答1:


As suggested by @markus, you can use replicate:

set.seed(123)

nTime <- 10^5

x <- replicate(nTime, sample(1:6, size=100, replace = TRUE))

hist(x,
     main="10^6 fair rolls",
     xlab = "Dice Result",
     ylab = "Probability",
     xlim=c(0.5,6.5),
     breaks=-1:100+.5,
     prob=TRUE )



来源:https://stackoverflow.com/questions/55806482/i-need-to-run-a-fair-dice-simulation-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!