9.1 Random Seeds
It is important to note that R uses a pseudorandom number generator to generate all of its (pseudo)random results. This is important because that allows us to set a random seed so that all of our work is reproducible. It is also important in a grading context because you may need to set the same seed as your grader in order to get the same, correct results. We can use set.seed()
to set the random seed, and we should always see the same results from code run after setting the seed.
set.seed(1)
sample(1:6, size = 3, replace = TRUE)
## [1] 2 3 4
sample(1:6, size = 3, replace = TRUE)
## [1] 6 2 6
set.seed(1)
sample(1:6, size = 3, replace = TRUE)
## [1] 2 3 4
sample(1:6, size = 3, replace = TRUE)
## [1] 6 2 6