I always thought set.seed()
only makes random variable generators (e.g., rnorm
) to generate a unique sequence for any specific set of input values.
However, I'm wondering, why when we set the set.seed()
, then the function sample()
doesn't do its job correctly?
Question
Specifically, given the below example, is there a way I can use set.seed
before the rnorm
but sample
would still produce new random samples from this rnorm
if sample
is run multiple times?
Here is an R code:
set.seed(123458)
x.y = rnorm(1e2)
sampled = sample(x = x.y, size = 20, replace = TRUE)
plot(sampled)
As per the help file at
?set.seed
So, since
rnorm
andsample
are both affected byset.seed()
, you can do:Instead of resetting the seed with
NULL
, I think it makes more sense to save the current state and restore it.