# 【4.1】R生成随机数

## 一、服从正态分布的随机数

rnorm(n,mean=0,sd=1)


> rnorm(10)
[1] -0.7125585  2.2070410  1.4730548 -0.6655749  0.5349409 -0.9700122  0.4970766
[8] -1.7385672  0.9300553 -0.4663140


> rnorm(10,mean=1,sd=4)
[1] -2.520978  3.567172  1.099416  1.029909 -1.239032  1.363111  0.670465
[8]  4.015274 -7.095845  3.050106


dnorm(x, mean = 0, sd = 1, log = FALSE)
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0, sd = 1)


## 二、服从均匀分布的随机数

runif(n, min = 0, max = 1)


> runif(10)
[1] 0.68835411 0.14208554 0.53551407 0.30101879 0.64202722 0.06249627 0.80834355
[8] 0.26193046 0.90841919 0.94294355


> runif(10,min=5,max=10)
[1] 7.771348 8.747055 8.721152 7.517620 6.084385 5.209369 5.532953 5.845175
[9] 6.683199 9.708647


## 三、Sample

> sample(1:10, 1)
[1] 6


> sample(1:10, 5, replace=T)
[1] 4 5 5 8 7
> sample(1:10, 5, replace=F)
[1] 7 5 1 2 9
> sample(state.name, 10)
[1] "Rhode Island"  "Alaska"        "Michigan"      "Maryland"      "West Virginia" "Vermont"
[7] "Tennessee"     "Nevada"        "South Dakota"  "Delaware"


## 四、其他

rexp 指数分布
rf F分布
rgamma Gamma分布
rgeom 几何分布
rhyper 超几何分布
rlnorm 对数正态分布
rlogis Logistic分布
rmultinom 多项分布
rnbinom 负二项分布
rpois 泊松分布
rt t分布
rchisq 卡方分布

## 参考资料：

http://blog.csdn.net/zhyoulun/article/details/46413727

http://blog.sina.com.cn/s/blog_4b9acb5201016z68.html

R中help(rnorm)