| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/seed.R |
| 3 |
\name{with_seed} |
| 4 |
\alias{with_seed} |
| 5 |
\alias{local_seed} |
| 6 |
\alias{with_preserve_seed} |
| 7 |
\alias{local_preserve_seed} |
| 8 |
\title{Random seed} |
| 9 |
\usage{ |
| 10 |
with_seed( |
| 11 |
seed, |
| 12 |
code, |
| 13 |
.rng_kind = NULL, |
| 14 |
.rng_normal_kind = NULL, |
| 15 |
.rng_sample_kind = NULL |
| 16 |
) |
| 17 |
|
| 18 |
local_seed( |
| 19 |
seed, |
| 20 |
.local_envir = parent.frame(), |
| 21 |
.rng_kind = NULL, |
| 22 |
.rng_normal_kind = NULL, |
| 23 |
.rng_sample_kind = NULL |
| 24 |
) |
| 25 |
|
| 26 |
with_preserve_seed(code) |
| 27 |
|
| 28 |
local_preserve_seed(.local_envir = parent.frame()) |
| 29 |
} |
| 30 |
\arguments{ |
| 31 |
\item{seed}{\verb{[integer(1)]}\cr The random seed to use to evaluate the code.} |
| 32 |
|
| 33 |
\item{code}{\code{[any]}\cr Code to execute in the temporary environment} |
| 34 |
|
| 35 |
\item{.rng_kind, .rng_normal_kind, .rng_sample_kind}{\verb{[character(1)]}\cr Kind of RNG to use. Passed as the \code{kind}, |
| 36 |
\code{normal.kind}, and \code{sample.kind} arguments of \code{\link[=RNGkind]{RNGkind()}}.} |
| 37 |
|
| 38 |
\item{.local_envir}{\verb{[environment]}\cr The environment to use for scoping.} |
| 39 |
} |
| 40 |
\value{ |
| 41 |
\code{[any]}\cr The results of the evaluation of the \code{code} |
| 42 |
argument. |
| 43 |
} |
| 44 |
\description{ |
| 45 |
\code{with_seed()} runs code with a specific random seed and resets it afterwards. |
| 46 |
|
| 47 |
\code{with_preserve_seed()} runs code with the current random seed and resets it |
| 48 |
afterwards. |
| 49 |
} |
| 50 |
\examples{ |
| 51 |
# Same random values: |
| 52 |
with_preserve_seed(runif(5)) |
| 53 |
with_preserve_seed(runif(5)) |
| 54 |
|
| 55 |
# Use a pseudorandom value as seed to advance the RNG and pick a different |
| 56 |
# value for the next call: |
| 57 |
with_seed(seed <- sample.int(.Machine$integer.max, 1L), runif(5)) |
| 58 |
with_seed(seed, runif(5)) |
| 59 |
with_seed(seed <- sample.int(.Machine$integer.max, 1L), runif(5)) |
| 60 |
} |
| 61 |
\seealso{ |
| 62 |
\code{\link{withr}} for examples |
| 63 |
} |