How to generate random numbers and set seeds in R

1

Lets learn how to generate random numbers in R. There might be occasions where you’d want to draw random numbers but want to be able to replicate those random numbers. To replicate we will set a seed, which means we are locking in the sequence of random numbers that R gave us originally and allows us to reproduce later.

First, we will generate a random number with no interest in replicating that number:


> rnorm(20)
 [1] -0.37770830  0.46507361  0.74168737  1.05569452  0.02123152  1.08508396  1.01533830  0.35247237
 [9]  1.84569972 -2.54635927  1.75746010  0.75914791  0.22402652  1.14659855  0.26116467  0.87986403
[17]  0.21480711  1.52551898 -2.38377619 -0.70210525

The above generated 20 random numbers from a normal distribution.

The syntax for rnorm is the following: rnorm(n, mean, sd)

Another way to generate random numbers in R:


> rnorm(10, 4, .25)
 [1] 3.988073 3.911958 3.618821 4.011305 3.883013 4.173513 4.458148 3.803371 3.997958 4.060140

What happened here? This simulation generated 10 random numbers from a normal distribution with a mean of 4 and a standard deviation of .25.

What if we wanted to be able to reproduce the above randomly generated numbers? This is where we’d set a seed:


> set.seed(5)
> rnorm(10)
 [1] -0.84085548  1.38435934 -1.25549186  0.07014277  1.71144087 -0.60290798 -0.47216639 -0.63537131
 [9] -0.28577363  0.13810822
> rnorm(10)
 [1]  1.2276303 -0.8017795 -1.0803926 -0.1575344 -1.0717600 -0.1389861 -0.5973131 -2.1839668
 [9]  0.2408173 -0.2593554
> set.seed(5)
> rnorm(10)
 [1] -0.84085548  1.38435934 -1.25549186  0.07014277  1.71144087 -0.60290798 -0.47216639 -0.63537131
 [9] -0.28577363  0.13810822

First, we set.seed(5) then ran rnorm in order to hold the 10 random numbers that can be held and reproduced later. If you look at the rnorm(10) without the set.seed you’ll notice another 10 random numbers. But when we run set.seed(5) once again with rnorm(10) we get the exact same random numbers from the above when set.seed(5) was set.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here