0

I am using R Version 3.6.1 and would like to set a seed globally for a R session to get the same sequences of random number for all random number generators I call in a script.
A behavior just as described here: setting seed locally (not globally) in R Currently I would have to call set.seed() in front of every call to a random number generator to get this (see code). Is there a option to set seeds globally?

# Example output
set.seed(2)
runif(1) 
[1] 0.1848823
set.seed(2)
runif(1)
[1] 0.1848823
runif(1)
[1] 0.702374

Also, when I looked it up I found alternating behavior.
Here it is described as more global setting seed locally (not globally) in R
And here it is more local: set.seed with R 2.15.2

Thanks in advance

E.L
  • 3
  • 4
  • 1
    You could alias the random number generator functions to include a set.seed call, eg `runif <- function(...) { set.seed(42); runif(...) }`. But setting the seed for every RNG means the numbers are no longer pseudorandom. – alan ocallaghan Feb 06 '20 at 13:11
  • 1
    What do you mean? The seed in R is always set globally. – AEF Feb 06 '20 at 13:12
  • 2
    Sorry, I don't understand your question. Could you provide an example with expected output? I'm not even sure what you mean by "all random number generators". Are you using other RNGs than the default Mersenne-Twister? – Roland Feb 06 '20 at 13:13
  • Okay, looking at the comments I think I had/have troubles with understanding the concept of seeding. Is this correct: the seed is the starting point of a sequence of numbers. When I set a seed and call multiple RNGs they won't have the exact same output but but the same sequence of outputs? – E.L Feb 06 '20 at 14:28
  • You don't even seem to know what an [RNG](https://en.wikipedia.org/wiki/Random_number_generation) is. Simplified, the number a (pseudo) RNG produces, depends on its "state". That state usually is initialized from the (transient) state of the operating system (that part is truly random), but details are not important here. After each number produced, the state of the RNG changes depending on the previous state (that part is deterministic). All setting a seed does, is creating a new (deterministic) initial state. – Roland Feb 07 '20 at 06:58
  • So, each time you initialize the RNG to the same state (by setting the same seed), it will produce the same sequence of numbers afterwards. – Roland Feb 07 '20 at 06:59
  • It's also important to know that all functions like `runif`, `rnorm`, etc. use the same RNG internally: a version of [the Mersenne Twister](https://en.wikipedia.org/wiki/Mersenne_Twister), see `help("RNG")`. – Roland Feb 07 '20 at 07:17

0 Answers0