Setting the seed

  • set.seed()

Computers in general, and R specifically, can, in fact, only provide pseudorandom number generators.
A pseudorandom number generator’s number sequence is completely determined by its seed, i.e. a number used to initialize that sequence.

Thus, if a pseudorandom number generator is reinitialized with the same seed, it will produce the same sequence of numbers. You can set the seed (with any arbitrary number) at the beginning of a script, and, if commands drawing random numbers are run in the exact same order, they will provide the same output in subsequent runs.

This is useful for sharing code and reproduce simulations, as well as for debugging code.


YOUR TURN:
In your local exercise script, play around with the function set.seed() by running and modifying the examples provided (e.g. compare outputs with and without seeds, change the seed number).


Back to top