您的位置:首页 > 其它

How to generate a random number in R

2016-07-17 23:13 316 查看
Generate a random number between 5.0 and 7.5
x1 <- runif(1, 5.0, 7.5) # 参数1表示产生一个随机数
x2 <- runif(10, 5.0, 7.5)# 参数10表示产生10个随机数

Generate a random integer between 1 and 10
x3 <- sample(1:10, 1) # 参数1表示产生一个随机数
x4 <- sample(1:10, 5, replace=T) # 参数5表示产生5个随机数, 参数replace=T表示数字可以重复出现(类似于放回抽样)

Select 6 random numbers between 1 and 40, without replacement
x5 <- sample(1:40, 6, replace=F)# 参数6表示产生6个随机数, 参数replace=F表示数字不可以重复出现(类似于不放回抽样)

Select 10 items from a list of 50
sample(state.name, 10)
sample(state.name, 52)
sample(state.name, 50)

REF:

http://www.inside-r.org/howto/how-generate-random-number-r
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: