怎么用R语言生成0-1之间的随机数?

Python020

怎么用R语言生成0-1之间的随机数?,第1张

1、第一步,调用runif()函数,这时函数内的值为6,生成6个随机数,如下图所示

2、第二步,再次调用runif()函数,runif(6)又生成6个随机数,如下图所示:

3、第三步,使用set.seed(12345)设置种子元素,然后调用runif(6),查看数据情况,如下图所示:

4、第四步,再次调用set.seed()函数,这时多加了一个6,如下图所示:

5、第五步,再次调用runif(6)函数,查看生成的6个随机数的不同,如下图所示:

6、第六步,再次调用set.seed()函数,这时是从1到9;然后调用runif()函数,如下图所示:

首先,如果想要别人复现出跟你一样的结果,要先设置随机种子

Set the seed of R‘s random number generator, which is useful for creating simulations or random objects that can be reproduced.

If you want to generate a decimal number where any value (including fractional values) between the stated minimum and maximum is equally likely, use the runif function.

The first argument is a vector of valid numbers to generate (here, the numbers 1 to 10), and the second argument indicates one number should be returned.

If we want to generate more than one random number, we have to add an additional argument to indicate that repeats are allowed: