Sets the random seed for the duration of expr and restores the
caller's random state afterwards. This makes a single result
reproducible without hijacking the random stream of the surrounding
script: two calls with the same seed give the same result, and whatever
is drawn after them is unaffected by either.
Arguments
- seed
a single number, or NULL to leave the random stream
untouched and simply evaluate expr
- expr
the expression to evaluate; evaluated lazily, in the
caller's environment
Details
The plain idiom set.seed(s); expr lacks the second half. In a
script that generates a series of random objects, seeding one of them
shifts every draw that follows, so results that were correct before the
seed was added silently change.