Title: | Set (Normal) Random Number Generator and Seed |
---|---|
Description: | Provides utilities to help set and record the setting of the seed and the uniform and normal generators used when a random experiment is run. The utilities can be used in other functions that do random experiments to simplify recording and/or setting all the necessary information for reproducibility. See the vignette and reference manual for examples. |
Authors: | Paul Gilbert <[email protected]> |
Maintainer: | Paul Gilbert <[email protected]> |
License: | GPL-2 | file LICENSE |
Version: | 2024.2-1 |
Built: | 2024-11-06 02:17:57 UTC |
Source: | https://github.com/r-forge/distr |
Programs to set random number generator (and seed) in R and S.
library("setRNG")
This library provides tools to simplify recording and resetting the random number generator, to help make monte carlo experiments easily reproducible. It uses the R/S tools for setting the seed, but also records and sets the mechanism for converting uniform numbers to normally distributed numbers. (It could be extended to other transformations, but I have not done that.)
The setRNG function would typically be called by simulation programs (see example) to set the RNG information if given, and record the RNG information in all cases. This information can be returned with the result of the simulation. That way the simulation can always be reproduced if necessary.
The library also implements an approach to random number generation which allows the same random experiments to be replicated in S and R. The functions in the S/ directory allow the R results using Wichmann-Hill and Box-Muller to be replicated in S. These were done with the aid of an example from B. D. Ripley. (The files in the S/ directory of the package are for use with S not R.) These functions are intended primarily as a way to confirm that simulations and estimations with simulated data work in the same way in both S and R, not as an improved RNG. (It has only been tested in Splus 3.3) Default and other RNGs can still be used and are preferred for both speed and theoretical reasons.
setRNG(kind="Wichmann-Hill", seed=c(979,1479,1542), normal.kind="Box-Muller") rnorm(10) sim <-function(rng=NULL) {if(!require("setRNG")) stop("This function requires the setRNG package.") if(is.null(rng)) rng <- setRNG() # returns setting so don't skip if NULL else {old.rng <- setRNG(rng); on.exit(setRNG(old.rng)) } x <- list(numbers=rnorm(10)) x$rng <- rng x } z <- sim() sim()$numbers sim(rng=getRNG(z))$numbers z$numbers
setRNG(kind="Wichmann-Hill", seed=c(979,1479,1542), normal.kind="Box-Muller") rnorm(10) sim <-function(rng=NULL) {if(!require("setRNG")) stop("This function requires the setRNG package.") if(is.null(rng)) rng <- setRNG() # returns setting so don't skip if NULL else {old.rng <- setRNG(rng); on.exit(setRNG(old.rng)) } x <- list(numbers=rnorm(10)) x$rng <- rng x } z <- sim() sim()$numbers sim(rng=getRNG(z))$numbers z$numbers
Programs to set random number generator (and seed) in R and S.
See setRNG-package
( in the help system use
package?setRNG or ?"setRNG-package") for an overview.
Get the random number generator and seed used to generate an object.
getRNG(e=NULL) ## Default S3 method: getRNG(e=NULL)
getRNG(e=NULL) ## Default S3 method: getRNG(e=NULL)
e |
an object generated by simulation (which stored the RNG information). |
Extract the RNG information used to generate the object. If e
is NULL
then getRNG
returns the RNG setting, as
returned by setRNG()
. Otherwise,
the default method assumes the object is a list and the RNG information
is in the element rng
or noise\$rng
.
The random seed and other random number generation information used to generate the object.
## Not run: if (require("dse")) { data("eg1.DSE.data.diff", package="dse") model <- estVARXls(eg1.DSE.data.diff) sim <- simulate(model) getRNG(sim) } ## End(Not run)
## Not run: if (require("dse")) { data("eg1.DSE.data.diff", package="dse") model <- estVARXls(eg1.DSE.data.diff) sim <- simulate(model) getRNG(sim) } ## End(Not run)
Test the random number generator.
random.number.test()
random.number.test()
None
This function checks that the
RNG is working properly and has not been changed. If the RNG does not return
values as in previous versions of R then the function executes
stop()
. Since changes to the RNG will cause comparisons of
simulation results to fail, this is a useful check before investigating more
complicated problems that may be a result of other "improvements" in your
simulation or estimation programs.
logical
Executes stop()
if the tests fail.
set.seed
RNGkind
runif
rnorm
setRNG
random.number.test()
random.number.test()
Set the RNG or return information about the setting of the RNG.
setRNG(kind=NULL, seed=NULL, normal.kind=NULL)
setRNG(kind=NULL, seed=NULL, normal.kind=NULL)
None required
kind |
a character string. |
seed |
a vector of numbers (depending on kind). |
normal.kind |
a character string. |
Sets the uniform and normal random number generators and the seed.
The old setting is returned using invisible()
in a format
which can be used in another call to setRNG
. (This would reset to the
original value.) If no arguments are given the current setting is returned,
not using invisible()
. In R see RNGkind
for more details.
Note that in a function using setRNG
it is good practice to
assign the old setting to a variable, then reset to the old value on exiting
the function (using on.exit
). This avoids the possibility that
overall RNG behaviour in a session, other than within your function, may be
disrupted by your function.
The old setting.
Sets global variables controlling the uniform and normal random number generators and the global seed.
RNGkind
,
set.seed
,
runif
,
rnorm
,
random.number.test
setRNG(kind="Wichmann-Hill", seed=c(979,1479,1542), normal.kind="Box-Muller") rnorm(10)
setRNG(kind="Wichmann-Hill", seed=c(979,1479,1542), normal.kind="Box-Muller") rnorm(10)