---
title: "Fast Pseudo Random Number Generators for R"
author: "Ralf Stubner"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Fast Pseudo Random Number Generators for R}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

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 https://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 https://xoroshiro.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 and set as default RNG.

## 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
* `dqsample()` and `dqsample.int` are analogue to `sample`  and ` sample.int` for creating random samples of vectors and vector indices

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:

```{r}
N <- 1e7
piR <- function(n, rng = stats::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"))
```

Using dqrng is about three times faster:

```{r}
library(dqrng)
dqRNGkind("Xoroshiro128+")
dqset.seed(42)
system.time(cat("pi ~= ", piR(N, rng = dqrng::dqrunif), "\n"))
```

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

```{r}
system.time(stats::runif(N))
system.time(dqrng::dqrunif(N))
```

Similar for the exponential distribution:

```{r}
system.time(stats::rexp(N))
system.time(dqrng::dqrexp(N))
```

And for the normal distribution:

```{r}
system.time(stats::rnorm(N))
system.time(dqrng::dqrnorm(N))
```

As well as for sampling with and without replacement:

```{r}
system.time(for (i in 1:100)   sample.int(N, N/100, replace = TRUE))
system.time(for (i in 1:100) dqsample.int(N, N/100, replace = TRUE))
system.time(for (i in 1:100)   sample.int(N, N/100))
system.time(for (i in 1:100) dqsample.int(N, N/100))
```



## 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:

* use `// [[Rcpp::depends(dqrng)]]` together with `Rcpp::sourceCpp()`
* use `Rcpp::cppFunction(depends = "dqrng", ...)`
* use an R package with `LinkingTo: dqrng`

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`.
The full ist of functions is available with `vignette("cpp-api", package = "dqrng")`.
Revisiting the example of approximating $\pi$ we can use:

```{r, eval=FALSE, engine='Rcpp'}
// [[Rcpp::depends(dqrng)]]
#include <Rcpp.h>
#include <dqrng.h>

using Rcpp::IntegerVector;
using Rcpp::NumericVector;
using Rcpp::sqrt;
using Rcpp::sum;
using dqrng::dqrunif;

// [[Rcpp::export]]
double piCpp(const int n) {
  dqrng::dqRNGkind("Xoroshiro128+");
  dqrng::dqset_seed(IntegerVector::create(42));
  NumericVector x = dqrunif(n);
  NumericVector y = dqrunif(n);
  NumericVector d = sqrt(x*x + y*y);
  return 4.0 * sum(d < 1.0) / n;
}
/*** R
system.time(cat("pi ~= ", piCpp(1e7), "\n"))
*/
```

Note that in C++ you have to use `dqrng::dqset_seed()`, whereas the analogue function in the R interface is called `dqrng::dqset.seed()`.
For sampling with and without replacement `dqrng::dqsample_int()` and `dqrng::dqsample_num()` are the analogue of `dqrng::dqsample.int()` in the R interface:

```{r, eval=FALSE, engine='Rcpp'}
// [[Rcpp::depends(dqrng)]]
#include <Rcpp.h>
#include <dqrng.h>

// [[Rcpp::export]]
void sampleCpp(const int n) {
  dqrng::dqRNGkind("Xoroshiro128+");
  dqrng::dqset_seed(Rcpp::IntegerVector::create(42));
  Rcpp::IntegerVector sample = dqrng::dqsample_int(n, n/100, true);
  Rcpp::Rcout << sample << std::endl;
}
/*** R
sampleCpp(1000)
*/
```


### 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](https://xoroshiro.di.unimi.it/splitmix64.c) generator is used together with `dqrng::normal_distribution`:

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
#include <dqrng_distribution.h>
// [[Rcpp::plugins(cpp11)]]
#include <cstdint>

class SplitMix {
public:
  typedef uint64_t result_type;
  SplitMix (result_type seed) : state(seed) {};
  result_type operator() () {
    result_type z = (state += 0x9e3779b97f4a7c15ULL);
    z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
    z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
    return z ^ (z >> 31);
  }
  void seed(result_type seed) {state = seed;}
  static constexpr result_type min() {return 0;};
  static constexpr result_type max() {return UINT64_MAX;};

private:
  result_type state;
};

// [[Rcpp::export]]
Rcpp::NumericVector splitmix_rnorm(const int n, const double mean = 0.0, const double sd = 1.0) {
  auto rng = dqrng::generator<SplitMix>(42);
  dqrng::normal_distribution dist(mean, sd);
  Rcpp::NumericVector result(n);
  std::generate(result.begin(), result.end(), [&dist, &rng](){return dist(*rng);});
  return result;
}
/*** R
splitmix_rnorm(10)
system.time(splitmix_rnorm(1e7))
*/
```

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:

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
#include <dqrng_distribution.h>
#include <threefry.h>
// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
Rcpp::NumericVector threefry_rnorm(const int n, const double mean = 0.0, const double sd = 1.0) {
  auto rng = dqrng::generator<sitmo::threefry_13_64>(42);
  dqrng::normal_distribution dist(mean, sd);
  Rcpp::NumericVector result(n);
  std::generate(result.begin(), result.end(), [&dist, &rng](){return dist(*rng);});
  return result;
}
/*** R
threefry_rnorm(10)
system.time(threefry_rnorm(1e7))
*/
```

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:

```{r, eval=FALSE, engine='Rcpp'}
#include <random>
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
#include <dqrng_distribution.h>
#include <xoshiro.h>
// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
Rcpp::NumericVector std_rnorm(const int n, const double mean = 0.0, const double sd = 1.0) {
  auto rng = dqrng::generator<dqrng::xoroshiro128plus>(42);
  std::normal_distribution<double> dist(mean, sd);
  Rcpp::NumericVector result(n);
  std::generate(result.begin(), result.end(), [&dist, &rng](){return dist(*rng);});
  return result;
}
/*** R
std_rnorm(10)
system.time(std_rnorm(1e7))
*/
```

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:

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
#include <pcg_random.hpp>
#include <boost/random/normal_distribution.hpp>

// [[Rcpp::plugins(cpp11)]]   
// [[Rcpp::export]]
Rcpp::NumericVector boost_pcg_rnorm(const int n, const double mean = 0.0, const double sd = 1.0) {
  pcg32 rng(42);
  boost::random::normal_distribution<double> dist(mean, sd);
  Rcpp::NumericVector result(n);
  std::generate(result.begin(), result.end(), [&dist, &rng](){return dist(rng);});
  return result;
}
/*** R
boost_pcg_rnorm(10)
system.time(boost_pcg_rnorm(1e7))
*/
```

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`: 

```{r, eval=FALSE, engine='Rcpp'}
#include <Rcpp.h>
// [[Rcpp::depends(dqrng, BH, sitmo)]]
#include <boost/random/binomial_distribution.hpp>
#include <dqrng_distribution.h>
#include <xoshiro.h>
// [[Rcpp::plugins(cpp11)]]   

// aliases for the used ditributions
using binomial = boost::random::binomial_distribution<int>;
using normal = dqrng::normal_distribution;
// [[Rcpp::export]]
Rcpp::NumericMatrix multiple_distributions(int n) {
  dqrng::xoshiro256plus rng(42);
  // distributions with default parameters
  binomial bernoulli;
  normal normal;
  Rcpp::NumericMatrix out(n, 3);
  double p = 0.0;
  for (int i = 0; i < n; ++i) {
    p = double(i) / double(n);
    out(i,0) = bernoulli(rng, binomial::param_type(1, p));
    out(i,1) = normal(rng, normal::param_type(p, 1.0));
    out(i,2) = normal(rng, normal::param_type(4.0, 3.0 - p));
  }
  Rcpp::colnames(out) = Rcpp::CharacterVector::create("Bernoulli", "Normal1", "Normal2");
  return out;
}

/*** R
multiple_distributions(5)
*/
```

<!-- STATIC_ARBITRARY_SEED -->
