Utility functions for working with Random Number Generators

Description

This package contains a set of functions for working with Random Number Generators (RNGs). In particular, it defines a generic S4 framework for getting/setting the current RNG, or RNG data that are embedded into objects for reproducibility.

Details

Notably, convenient default methods greatly facilitate the way current RNG settings can be changed.

Examples


showRNG()
## # RNG kind:  Mersenne-Twister / Inversion 
## # RNG state: 403L, 624L, ..., -1896522223L [eacb7c1288e03421d428feb736099548]
s <- getRNG()
RNGstr(s)
## [1] "403L, 624L, ..., -1896522223L [eacb7c1288e03421d428feb736099548]"
RNGtype(s)
## [1] "Mersenne-Twister" "Inversion"

# get what would be the RNG seed after set.seed
s <- nextRNG(1234)
showRNG(s)
## # RNG kind:  Mersenne-Twister / Inversion 
## # RNG state: 403L, 624L, ..., -1896522223L [eacb7c1288e03421d428feb736099548]
showRNG( nextRNG(1234, ndraw=10) )
## # RNG kind:  Mersenne-Twister / Inversion 
## # RNG state: 403L, 10L, ..., 467264024L [b9a4fab9a7ad828dbffae696eef6a31a]

# change of RNG kind
showRNG()
## # RNG kind:  Mersenne-Twister / Inversion 
## # RNG state: 403L, 624L, ..., -1896522223L [eacb7c1288e03421d428feb736099548]
k <- RNGkind()
k[2L] <- 'Ahrens'
try( RNGkind(k) )
setRNG(k)
showRNG()
## # RNG kind:  Mersenne-Twister / Ahrens-Dieter 
## # RNG state: 103L, 624L, ..., -912150329L [c96e855238c32059d90ca179f9b1b16a]
# set encoded kind
setRNG(501L)
showRNG()
## # RNG kind:  Marsaglia-Multicarry / Kinderman-Ramage 
## # RNG state: 501L, 733861771L, 53988009L

# use as set seed
setRNG(1234)
showRNG()
## # RNG kind:  Marsaglia-Multicarry / Kinderman-Ramage 
## # RNG state: 501L, -305777535L, -1394370482L
r <- getRNG()

# extract embedded RNG specifications
runif(10)
##  [1] 0.02612 0.29453 0.31487 0.41449 0.88494 0.33931 0.43921 0.30835
##  [9] 0.62447 0.05537
setRNG(list(1, rng=1234))
rng.equal(r)
## [1] TRUE

# restore default RNG (e.g., after errors)
RNGrecovery()
showRNG()
## # RNG kind:  Mersenne-Twister / Inversion 
## # RNG state: 403L, 624L, ..., -325092206L [6b9b92cc9debdb741d96ff4ef849aa33]