Specialized alternative to rdo()
or rfun()
for creating rvar
s from
existing randomnumber generator functions (such as rnorm()
, rbinom()
, etc).
rvar_rng(.f, n, ..., ndraws = NULL)
.f  (function) A function (or string naming a function) representing a
randomnumber generating function that follows the pattern of base random
number generators (like


n  (positive integer) The length of the output 
...  Arguments passed to 
ndraws  (positive integer) The number of draws used to construct the
returned random variable if no 
A singledimensional rvar
of length n
.
This function unwraps the arrays underlying the input rvar
s in
...
and then passes them to .f
, relying on the vectorization of .f
to evaluate it across draws from the input rvar
s. This is why the arguments
of .f
must be vectorized. It asks for n
times the number of draws
in the input rvar
s (or ndraws
if none are given) draws from the
random number generator .f
, then reshapes the output from .f
into an
rvar
with length n
.
rvar_rng()
is a fast alternative to rdo()
or rfun()
, but you must
ensure that .f
satisfies the preconditions described above for the result
to be correct. Most base random number generators satisfy these conditions.
It is advisable to test against rdo()
or rfun()
(which should be correct,
but slower) if you are uncertain.
mu < rvar_rng(rnorm, 10, mean = 1:10, sd = 1) sigma < rvar_rng(rgamma, 1, shape = 1, rate = 1) x < rvar_rng(rnorm, 10, mu, sigma) x#> rvar<4000>[10] mean ± sd: #> [1] 1 ± 1.8 2 ± 1.7 3 ± 1.7 4 ± 1.7 5 ± 1.8 6 ± 1.7 7 ± 1.8 #> [8] 8 ± 1.7 9 ± 1.7 10 ± 1.7