3. R语言随机数生成

1. 均匀分布

函数: runif(n, min=0, max=1),n 表示生成的随机数数量,min 表示均匀分布的下限,max 表示均匀分布的上限,若省略参min、max,则默认生成[0,1]上的均匀分布随机数。

> q = runif(5,-1,1)
> q
[1]  0.73539909  0.72895000 -0.04357151  0.81696252  0.50210058

2. 正太分布

函数:rnorm(n, mean=0, sd=1),其中,n 表示生成的随机数数量,mean是正态分布的均值,默认为0,sd 是正态分布的标准差,默认时为1。

> x = rnorm(10,5,10)
> x
 [1] 10.319216 -3.697041 24.565294 -9.691016 -7.324058
 [6] -6.185308 -2.107426 -1.915519 13.306308 22.763153

3. 二项分布

函数:rbinom(n, size, prob),n 表示生成的随机数数量,size 表示进行贝努力试验的次数,prob 表示一次贝努力试验成功的概率。

> x = rbinom(10,10,0.9)
> x
 [1]  9  9 10  9  8  9  9  6 10 10

4. 指数分布

函数:rexp(n,lamda = 1),n 表示生成的随机数个数,lamda=1/mean

> x = rexp(10,3)
> x
 [1] 0.13044259 0.52299630 0.35504953 0.50061743 0.03373871
 [6] 1.03543586 2.08565786 0.81414981 0.31333523 0.02681090

5. 其他

除了生成上面介绍的几种分布的随机数,还可以生成poisson分布、t 分布、F 分布等很多种分布的随机数,只要在相应的分布名前加“r”就可以。
这里写图片描述

除了在分布名前面加r还可以加其他的参数,例如:p,q,d。功能见下图:
这里写图片描述

我的博客即将同步至腾讯云+社区,邀请大家一同入驻。

已标记关键词 清除标记
Problem Description Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form seed(x+1) = [seed(x) + STEP] % MOD where &#39;%&#39; is the modulus operator. Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1. For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations. If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1. Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers. Input Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000). Output For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either "Good Choice" or "Bad Choice" left-justified starting in column 25. The "Good Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message "Bad Choice". After each output test set, your program should print exactly one blank line. Sample Input 3 5 15 20 63923 99999 Sample Output 3 5 Good Choice 15 20 Bad Choice 63923 99999 Good Choice
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页