The random-number generators (RNG) from this package can be
registered as user-supplied RNG. This way all r<dist>
functions make
use of the provided fast RNGs.
register_methods(kind = c("both", "rng"))
restore_methods()
Invisibly returns a three-element character vector of the RNG, normal and sample kinds before the call.
Caveats:
While runif
and dqrunif
as well as rnorm
and
dqrnorm
will produce the same results, this is not the case for
rexp
and dqrexp
.
The dqr<dist>
functions are still faster than r<dist>
when many random numbers are generated.
You can use only the RNG from this package using
register_method("rng")
or both the RNG and the Ziggurat method
for normal draws with register_method("both")
. The latter
approach is used by default. Using only the Ziggurat method will give
undefined behavior and is not supported!
Calling dqset.seed(NULL)
re-initializes the RNG from R's RNG.
This no longer makes sense when the RNG has been registered as user-supplied
RNG. In that case set.seed{NULL}
needs to be used.
With R's in-build RNGs one can get access to the internal state using
.Random.seed
. This is not possible here, since the internal state
is a private member of the used C++ classes.
You can automatically register these methods when loading this package by
setting the option dqrng.register_methods
to TRUE
, e.g.
with options(dqrng.register_methods=TRUE)
.
Notes on seeding:
When a user-supplied RNG is registered, it is also seeded from the
previously used RNG. You will therefore get reproducible (but different)
whether you call set.seed()
before or after register_methods()
.
When called with a single integer as argument, both set.seed()
and dqset.seed()
have the same effect. However, dqset.seed()
allows you to call it with two integers thereby supplying 64 bits of
initial state instead of just 32 bits.
RNGkind
and Random.user
register_methods()
# set.seed and dqset.seed influence both (dq)runif and (dq)rnorm
set.seed(4711); runif(5)
#> [1] 0.6458721 0.3948766 0.3068803 0.7015674 0.9091675
set.seed(4711); dqrunif(5)
#> [1] 0.6458721 0.3948766 0.3068803 0.7015674 0.9091675
dqset.seed(4711); rnorm(5)
#> [1] -0.5015739 -0.3312826 -0.4456174 1.6025723 -1.1733258
dqset.seed(4711); dqrnorm(5)
#> [1] -0.5015739 -0.3312826 -0.4456174 1.6025723 -1.1733258
# similarly for other r<dist> functions
set.seed(4711); rt(5, 10)
#> [1] -0.5734854 2.3350230 1.4165607 1.0935105 -0.8716930
dqset.seed(4711); rt(5, 10)
#> [1] -0.5734854 2.3350230 1.4165607 1.0935105 -0.8716930
# but (dq)rexp give different results
set.seed(4711); rexp(5, 10)
#> [1] 0.02917442 0.12726537 0.09206684 0.04031348 0.01696817
set.seed(4711); dqrexp(5, 10)
#> [1] 0.03531759 0.02444035 0.04677779 0.23058362 0.11377478
restore_methods()