| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/utils.R |
| 3 |
\name{repeatable} |
| 4 |
\alias{repeatable} |
| 5 |
\title{Make a random number generator repeatable} |
| 6 |
\usage{ |
| 7 |
repeatable(rngfunc, seed = stats::runif(1, 0, .Machine$integer.max)) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{rngfunc}{The function that is affected by the R session's seed.} |
| 11 |
|
| 12 |
\item{seed}{The seed to set every time the resulting function is called.} |
| 13 |
} |
| 14 |
\value{ |
| 15 |
A repeatable version of the function that was passed in. |
| 16 |
} |
| 17 |
\description{ |
| 18 |
Given a function that generates random data, returns a wrapped version of |
| 19 |
that function that always uses the same seed when called. The seed to use can |
| 20 |
be passed in explicitly if desired; otherwise, a random number is used. |
| 21 |
} |
| 22 |
\note{ |
| 23 |
When called, the returned function attempts to preserve the R session's |
| 24 |
current seed by snapshotting and restoring |
| 25 |
\code{\link[base:Random]{base::.Random.seed()}}. |
| 26 |
} |
| 27 |
\examples{ |
| 28 |
rnormA <- repeatable(rnorm) |
| 29 |
rnormB <- repeatable(rnorm) |
| 30 |
rnormA(3) # [1] 1.8285879 -0.7468041 -0.4639111 |
| 31 |
rnormA(3) # [1] 1.8285879 -0.7468041 -0.4639111 |
| 32 |
rnormA(5) # [1] 1.8285879 -0.7468041 -0.4639111 -1.6510126 -1.4686924 |
| 33 |
rnormB(5) # [1] -0.7946034 0.2568374 -0.6567597 1.2451387 -0.8375699 |
| 34 |
} |