OpenRAND  0.9
OpenRAND: A C++ Library for Reproducible Random Number Generation in Parallel Computing Environments
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
openrand::BaseRNG< RNG > Class Template Reference

Base class for random number generators. More...

#include <base_state.h>

Public Types

using result_type = uint32_t
 

Public Member Functions

OPENRAND_DEVICE result_type operator() ()
 Generates a 32 bit unsigned integer from a uniform distribution. More...
 
template<typename T = float>
OPENRAND_DEVICE T rand ()
 Generates a random number from a uniform distribution between 0 and 1. More...
 
template<typename T = float>
OPENRAND_DEVICE T uniform (const T low, const T high)
 Generates a number from a uniform distribution between a and b. More...
 
template<typename T = float>
OPENRAND_DEVICE void fill_random (T *array, const int N)
 
template<typename T = float>
OPENRAND_DEVICE T randn ()
 Generates a random number from a normal distribution with mean 0 and std 1. More...
 
template<typename T = float>
OPENRAND_DEVICE vec2< T > randn2 ()
 More efficient version of randn, returns two values at once. More...
 
template<typename T = float>
OPENRAND_DEVICE T randn (const T mean, const T std_dev)
 Generates a random number from a normal distribution with mean and std. More...
 
template<bool biased = true, typename T = int>
OPENRAND_DEVICE T range (const T N)
 Generates a random integer of certain range. More...
 
template<typename T = float>
OPENRAND_DEVICE T gamma (T alpha, T b)
 Generates a random number from a gamma distribution with shape alpha and scale b. More...
 
template<typename T = RNG>
std::enable_if_t< has_counter< T >::value, RNG > forward_state (int n) const
 Returns a new generator with the internal state forwarded by a given number. More...
 

Static Public Member Functions

static constexpr result_type min ()
 
static constexpr result_type max ()
 

Protected Member Functions

template<typename Ftype , typename Utype >
OPENRAND_DEVICE Ftype u01 (const Utype in) const
 

Detailed Description

template<typename RNG>
class openrand::BaseRNG< RNG >

Base class for random number generators.

This class utilizes the CRTP pattern to inject some common functionalities into random number generators.

Template Parameters
RNGRandom number generator class. It must contain two public methods, a constructor and a templated draw() function that returns the next 32 or 64 bit random unsigned int from its stream.

Member Function Documentation

◆ forward_state()

template<typename RNG >
template<typename T = RNG>
std::enable_if_t<has_counter<T>::value, RNG> openrand::BaseRNG< RNG >::forward_state ( int  n) const
inline

Returns a new generator with the internal state forwarded by a given number.

The new generator's first output will be the n+1th output of the current generator and so on. This is O(1) operation.

Parameters
nNumber of steps to move the state forward
Returns
RNG A new RNG object with the state moved forward by n steps

◆ gamma()

template<typename RNG >
template<typename T = float>
OPENRAND_DEVICE T openrand::BaseRNG< RNG >::gamma ( alpha,
b 
)
inline

Generates a random number from a gamma distribution with shape alpha and scale b.

Adapted from the following implementation: https://www.hongliangjie.com/2012/12/19/how-to-generate-gamma-random-variables/

Template Parameters
Tfloating point type to be returned
Parameters
alphashape parameter of the gamma distribution
bscale parameter of the gamma distribution
Returns
T random number from a gamma distribution with shape alpha and scale b

◆ operator()()

template<typename RNG >
OPENRAND_DEVICE result_type openrand::BaseRNG< RNG >::operator() ( )
inline

Generates a 32 bit unsigned integer from a uniform distribution.

This function is needed to conform to C++ engine interface

Returns
uint32_t random number from a uniform distribution

◆ rand()

template<typename RNG >
template<typename T = float>
OPENRAND_DEVICE T openrand::BaseRNG< RNG >::rand ( )
inline

Generates a random number from a uniform distribution between 0 and 1.

Note
Some generators may expose a more efficient version of this function that returns multiple values at once.
Template Parameters
TData type to be returned. Can be 32 or 64 bit integer, float or double.
Returns
T random number from a uniform distribution between 0 and 1

◆ randn() [1/2]

template<typename RNG >
template<typename T = float>
OPENRAND_DEVICE T openrand::BaseRNG< RNG >::randn ( )
inline

Generates a random number from a normal distribution with mean 0 and std 1.

This function implements box-muller method. This method avoids branching, and therefore more efficient on GPU. see: https://developer.nvidia.com/gpugems/gpugems3/part-vi-gpu-computing/chapter-37-efficient-random-number-generation-and-application

Template Parameters
Tfloating point type to be returned
Returns
T random number from a normal distribution with mean 0 and std 1

◆ randn() [2/2]

template<typename RNG >
template<typename T = float>
OPENRAND_DEVICE T openrand::BaseRNG< RNG >::randn ( const T  mean,
const T  std_dev 
)
inline

Generates a random number from a normal distribution with mean and std.

Template Parameters
Tfloating point type to be returned
Parameters
meanmean of the normal distribution
std_devstandard deviation of the normal distribution
Returns
T random number from a normal distribution with mean and std

◆ randn2()

template<typename RNG >
template<typename T = float>
OPENRAND_DEVICE vec2<T> openrand::BaseRNG< RNG >::randn2 ( )
inline

More efficient version of randn, returns two values at once.

Template Parameters
Tfloating point type to be returned
Returns
T random number from a normal distribution with mean 0 and std 1

◆ range()

template<typename RNG >
template<bool biased = true, typename T = int>
OPENRAND_DEVICE T openrand::BaseRNG< RNG >::range ( const T  N)
inline

Generates a random integer of certain range.

This uses the method described in [1] to generate a random integer of range [0..N)

Attention
if using non-biased version, please make sure that N is not too large [2]
Template Parameters
biasedif true, the faster, but slightly biased variant is used.
Tinteger type (<=32 bit) to be returned
Parameters
NA random integral of range [0..N) will be returned
Returns
T random number from a uniform distribution between 0 and N
Note
[1] https://lemire.me/blog/2016/06/30/fast-random-shuffling/
[2] If N=2^b (b<32), pr(taking the branch) = p = 1/2^(32-b). For N=2^24, this value is 1/2^8 = .4%, quite negligible. But GPU complicates this simple math. Assuming a warp size of 32, the probability of a thread taking the branch becomes 1 - (1-p)^32. For N=2^24, that value is 11.8%. For N=2^20, it's 0.8%.

◆ uniform()

template<typename RNG >
template<typename T = float>
OPENRAND_DEVICE T openrand::BaseRNG< RNG >::uniform ( const T  low,
const T  high 
)
inline

Generates a number from a uniform distribution between a and b.

@Note For integer types, consider using range for greater control.

Template Parameters
TData type to be returned. Can be float or double. double.
Parameters
lowlower bound of the uniform distribution
highupper bound of the uniform distribution
Returns
T random number from a uniform distribution between a and b

The documentation for this class was generated from the following file: