set.seed

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

Example. Create simulated values that are reproducible.
> set.seed(5)
> rnorm(5)
[1] -0.84085548  1.38435934 -1.25549186  0.07014277  1.71144087
> set.seed(5)
> rnorm(5)
[1] -0.84085548  1.38435934 -1.25549186  0.07014277  1.71144087
The random numbers are the same, and they would continue to be the same no matter how far out in the sequence we went.
Tip. Use the set.seed function when running simulations to ensure all results, figures, etc are reproducible.

Leave a Reply