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:

  • pcg64 The default 64 bit variant from the PCG family developed by Melissa O’Neill. See for more details.
  • Xoroshiro128+ and Xoshiro256+
    RNGs developed by David Blackman and Sebastiano Vigna. They are used as default RNGs in Erlang and Lua. See for more details.
  • Threefry
    The 64 bit version of the 20 rounds Threefry engine (Salmon et al., 2011) as provided by the package ‘sitmo’.

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:

Using dqrng is about three times faster:

system.time(cat("pi ~= ", piR(N, rng = dqrunif), "\n"))
#> pi ~=  3.14042
#>    user  system elapsed 
#>   0.274   0.168   0.442

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

Similar for the exponential distribution:

And for the normal distribution:

As well as for sampling with and without replacement:

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 header only library

The RNG wrapper and distributions functions can be used from C++ by including dqrng_generator.h and dqrng_distribution.h. In order to use these header files, you have to use at least C++11 and link to the BH and sitmo packages as well. 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: