我需要多次运行公平的骰子模拟 [英] I need to run a fair dice simulation multiple times

查看:40
本文介绍了我需要多次运行公平的骰子模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以模拟掷骰子 100 次.我需要知道如何运行这个程序 10^5 次,我认为这与数字有关.

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 )

推荐答案

按照@markus 的建议,你可以使用 replicate:

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 )

这篇关于我需要多次运行公平的骰子模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆