Fast Pseudo Random Number Generators for R

Ralf Stubner

2019-03-11

The dqrng package provides fast random number generators (RNG) with good statistical properties for usage with R. It combines these RNGs with fast distribution functions to sample from uniform, normal or exponential distributions. Both the RNGs and the distribution functions are distributed as C++ header-only library.

Supported Random Number Generators

Support for the following 64bit RNGs are currently included:

Of these RNGs Xoroshiro128+ is fastest and therefore used in the examples and set as default RNG.

Usage from R

Using these RNGs from R is deliberately similar to using R’s build-in RNGs:

Let’s look at the classical example of calculating \(\pi\) via simulation. The basic idea is to generate a large number of random points within the unit square. An approximation for \(\pi\) can then be calculated from the ratio of points within the unit circle to the total number of points. A vectorized implementation in R where we can switch the RNG might look like this:

N <- 1e7
piR <- function(n, rng = runif) {
    x <- rng(n)
    y <- rng(n)
    4 * sum(sqrt(x^2 + y^2) < 1.0) / n
}
set.seed(42)
system.time(cat("pi ~= ", piR(N), "\n"))
#> pi ~=  3.140899
#>    user  system elapsed 
#>   0.768   0.076   0.843

Using dqrng is about three times faster:

library(dqrng)
dqRNGkind("Xoroshiro128+")
dqset.seed(42)
system.time(cat("pi ~= ", piR(N, rng = dqrunif), "\n"))
#> pi ~=  3.14042
#>    user  system elapsed 
#>   0.180   0.060   0.241

Since the calculations add a constant off-set, the speed-up for the RNGs alone has to be even greater:

system.time(runif(N))
#>    user  system elapsed 
#>   0.268   0.020   0.290
system.time(dqrunif(N))
#>    user  system elapsed 
#>   0.044   0.016   0.059

Similar for the exponential distribution:

system.time(rexp(N))
#>    user  system elapsed 
#>   0.436   0.008   0.444
system.time(dqrexp(N))
#>    user  system elapsed 
#>   0.060   0.016   0.075

And for the normal distribution:

system.time(rnorm(N))
#>    user  system elapsed 
#>   0.604   0.024   0.628
system.time(dqrnorm(N))
#>    user  system elapsed 
#>   0.108   0.012   0.123

Usage from C++

The RNGs and distributions functions can also be used from C++ at various levels of abstraction. Technically there are three ways to make use of dqrng at the C++ level:

Here only the first approach is shown.

Using the compiled library functions

The functions available in R directly call corresponding C++ functions. These functions are also available at the C++ level if you include dqrng.h. Revisiting the example of approximating \(\pi\) we can use:

Note that in C++ you have to use dqrng::dqset_seed(), whereas the analogue function in the R interface is called dqrng::dqset.seed().

Using the header only library

The RNG wrapper and distributions functions can be used from C++ by including dqrng_generator.h and dqrng_distribution.h. For example, you can use the distribution functions from dqrng together with some foreign 64bit RNG. Here a minimal SplitMix generator is used together with dqrng::normal_distribution:

Since SplitMix is a very fast RNG, the speed of this function is comparable to dqrnorm. Generally speaking you can use any C++11 compliant RNG with 64 bit output size. For example, here the 64 bit Threefry engine with 13 rounds from package sitmo is used:

Alternatively, you could combine the included RNGs together with dqrng’s tooling and some other distribution function. For example, this function generates random numbers according to the normal distribution using the standard library from C++11:

Typically this is not as fast as dqrnorm, but the technique is useful to support distributions not (yet) included in dqrng. Note however, that the algorithms used for the distributions from C++11 are implementation defined.

Finally you could directly use the base generators, which are provided as header-only libraries, without dqrng’s tooling. For example, the following function uses the 32 bit PCG variant together with Boost’s normal distribution function:

This is quite fast since boost::random::normal_distribution uses the fast Ziggurat algorithm. For some applications it is necessary to draw random numbers from multiple distributions with varying parameters. The following function uses a binomial distribution (from boost.random) as well as the normal distribution from dqrng. The parameters of the distributions are adjusted for every draw using <distribution>::param_type: