template<class CBRNG>
tk::Random123 class

Random123-based random number generator used polymorphically with tk::RNG.

Constructors, destructors, conversion operators

Random123(uint64_t n = 1, uint64_t seed = 0) explicit

Public functions

void uniform(int tid, ncomp_t num, double* r) const
void gaussian(int tid, ncomp_t num, double* r) const
void gaussianmv(int tid, ncomp_t num, ncomp_t d, const double*const mean, const double*const cov, double* r) const
Multi-variate Gaussian RNG: Generate multi-variate Gaussian random numbers.
void beta(int tid, ncomp_t num, double p, double q, double a, double b, double* r) const
void gamma(int tid, ncomp_t num, double a, double b, double* r) const
auto nthreads() const -> uint64_t noexcept
Accessor to the number of threads we operate on.

Function documentation

template<class CBRNG>
tk::Random123<CBRNG>::Random123(uint64_t n = 1, uint64_t seed = 0) explicit

Parameters
in Initialize RNG using this many independent streams
seed in RNG seed

Constructor

template<class CBRNG>
void tk::Random123<CBRNG>::uniform(int tid, ncomp_t num, double* r) const

Parameters
tid in Thread (or more precisely) stream ID
num in Number of RNGs to generate
in/out Pointer to memory to write the random numbers to

Uniform RNG: Generate uniform random numbers

template<class CBRNG>
void tk::Random123<CBRNG>::gaussian(int tid, ncomp_t num, double* r) const

Parameters
tid in Thread (or more precisely stream) ID
num in Number of RNGs to generate
in/out Pointer to memory to write the random numbers to

Gaussian RNG: Generate Gaussian random numbers Generating Gaussian random numbers is implemented via an adaptor, modeling std::UniformRandomNumberGenerator, outsourcing the transformation of uniform random numbers to Gaussian ones, to the standard library. The adaptor is instantiated here because a standard distribution, such as e.g., std::normal_distribution, generates numbers using operator() with no arguments, thus the RNG state and the thread ID (this latter only known here) must be stored in the adaptor functor's state. Even though creating the adaptor seems like a potentially costly operation for every call, using the standard library implementation is still faster than a hand-coded implementation of the Box-Muller algorithm. Note that libc++ uses a cache, as Box-Muller, implemented using the polar algorithm generates 2 Gaussian numbers for each pair of uniform ones, caching every 2nd.

template<class CBRNG>
void tk::Random123<CBRNG>::gaussianmv(int tid, ncomp_t num, ncomp_t d, const double*const mean, const double*const cov, double* r) const

Multi-variate Gaussian RNG: Generate multi-variate Gaussian random numbers.

Parameters
tid in Thread (or more precisely stream) ID
num in Number of RNGs to generate
in Dimension d ( d ≥ 1) of output random vectors
mean in Mean vector of dimension d
cov in Lower triangle of covariance matrix, stored as a vector of length d(d+1)/2
in/out Pointer to memory to write the random numbers to

template<class CBRNG>
void tk::Random123<CBRNG>::beta(int tid, ncomp_t num, double p, double q, double a, double b, double* r) const

Parameters
tid in Thread (or more precisely stream) ID
num in Number of RNGs to generate
in First beta shape parameter
in Second beta shape parameter
in Beta displacement parameter
in Beta scale factor
in/out Pointer to memory to write the random numbers to

Beta RNG: Generate beta random numbers Generating beta-distributed random numbers is implemented via an adaptor, modeling boost::UniformRandomNumberGenerator, outsourcing the transformation of uniform random numbers to beta-distributed ones, to boost::random. The adaptor is instantiated here because a boost random number distribution, such as e.g., boost::random::beta_distribution, generates numbers using operator() with no arguments, thus the RNG state and the thread ID (this latter only known here) must be stored in the adaptor functor's state.

template<class CBRNG>
void tk::Random123<CBRNG>::gamma(int tid, ncomp_t num, double a, double b, double* r) const

Parameters
tid in Thread (or more precisely stream) ID
num in Number of RNGs to generate
in Gamma shape parameter
in Gamma scale factor
in/out Pointer to memory to write the random numbers to

Gamma RNG: Generate gamma random numbers Generating gamma-distributed random numbers is implemented via an adaptor, modeling boost::UniformRandomNumberGenerator, outsourcing the transformation of uniform random numbers to gamma-distributed ones, to boost::random. The adaptor is instantiated here because a boost random number distribution, such as e.g., boost::random::gamma_distribution, generates numbers using operator() with no arguments, thus the RNG state and the thread ID (this latter only known here) must be stored in the adaptor functor's state.