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:

  • Mersenne-Twister
    The 64 bit variant of the well-known Mersenne-Twister, which is also used as default. This is a conservative default that allows you to take advantage of the fast distribution functions provided by dqrng while staying close to R’s default RNG (32 bit Mersenne-Twister).
  • pcg64 The default 64 bit variant from the PCG family developed by Melissa O’Neill. See http://www.pcg-random.org 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 http://xoshiro.di.unimi.it/ 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.

Usage from R

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

  • dqRNGkind() is analogue to RNGkind() and sets the RNG
  • dqset.seed() is analogue to set.seed() and sets the seed
  • dqrunif(), dqrnorm(), and dqrexp() are analogue to runif(), rnorm(), and rexp() and sample from uniform, normal or exponential distributions

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:

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:

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. 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.