13.3 Setting the Seed
Technically, when we simulate from a particular distribution, we are using a pre-programmed function to generate the numbers. Thus, we may call these pseudo-random numbers.
Because there is a recursive function behind the generation of numbers, we can control the sequence of the values generated. It might be that you want to be able to reproduce the simulation results at another time.
To do this, we initialize the seed of the data-generating process:
set.seed(1)
Then generate 10 values from a normal distribution with mean of 10 and standard deviation of 4.
<- rnorm(n = 10, mean = 10, sd = 4)
x describe(x)
And then, if we do this again:
set.seed(1)
<- rnorm(n = 10, mean = 10, sd = 4)
y describe(y)
Then we get the same values again. You can absolutely verify this by printing the values of x and y:
x y
Note that you need to re-set the seed so that the recursive function starts at the same place to generate values. If you do not re-set the seed, the recursive function to generate values continues from where it left off.