criptic v1
Cosmic Ray Interstellar Propagation Tool using Itô Calculus
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
criptic::RngThread Class Reference

Thread-safe random number generator. More...

#include <RngThread.H>

Public Member Functions

 RngThread (const ParmParser &pp)
 Constructs an RngThread object.
 
 RngThread (std::string &buf)
 Constructs an RngThread object from a serialized buffer.
 
 ~RngThread ()
 Free RngThread object memory.
 
Real uniform (const Real a=0.0, const Real b=1.0) const
 Return a random number uniformly-distributed in (a,b)
 
IdxType uniformInt (const IdxType a=0, const IdxType b=maxIdx) const
 Return a random integer uniformly-distributed in (a,b)
 
Real normal (const Real mean=0.0, const Real stddev=1.0) const
 Return a normally-distributed random number.
 
IdxType poisson (const Real ex) const
 Return an integer drawn from a Poisson distribution.
 
template<typename T >
T & choose (std::vector< T > &elem, const std::vector< Real > &w=std::vector< Real >()) const
 Randomly choose an element from a vector.
 
template<typename T >
const T & choose (const std::vector< T > &elem, const std::vector< Real > &w=std::vector< Real >()) const
 Randomly choose an element from a const vector.
 
const std::string getState () const
 Get a serialized buffer holding the RNG state.
 
void setState (const std::string &buf)
 Restore the RNG state from a buffer.
 

Static Public Member Functions

static IdxType stateSize ()
 Return the number of bytes in the RNG state buffer.
 

Private Attributes

ThreadVec< pcg64 * > r
 

Detailed Description

Thread-safe random number generator.

This class implements a thread-safe random number generator, based on the pcg family of random number generators. Each thread maintains a separate, private random stream, and calls to the random number generator from a given thread are automatically drawn from the correct stream.

Constructor & Destructor Documentation

◆ RngThread() [1/2]

RngThread::RngThread ( const ParmParser pp)

Constructs an RngThread object.

Parameters
ppThe parameter parser, which contains the input deck

◆ RngThread() [2/2]

RngThread::RngThread ( std::string &  buf)

Constructs an RngThread object from a serialized buffer.

Parameters
bufA string containing the serialized state

◆ ~RngThread()

RngThread::~RngThread ( )

Free RngThread object memory.

Member Function Documentation

◆ choose() [1/2]

template<typename T >
const T & criptic::RngThread::choose ( const std::vector< T > &  elem,
const std::vector< Real > &  w = std::vector<Real>() 
) const
inline

Randomly choose an element from a const vector.

Template Parameters
TType held by vector
Parameters
elemVector of elements from which to choose
wRelative probability of each element
Returns
Const reference to a randomly selected element of elem

If w is an empty vector, or an argument is not provided, all elements are treated as having equal probability. The sum of w need not be normalized to 1. It is an error if w has a non-zero size that is different than the size of elem.

◆ choose() [2/2]

template<typename T >
T & criptic::RngThread::choose ( std::vector< T > &  elem,
const std::vector< Real > &  w = std::vector<Real>() 
) const
inline

Randomly choose an element from a vector.

Template Parameters
TType held by vector
Parameters
elemVector of elements from which to choose
wRelative probability of each element
Returns
Reference to a randomly selected element of elem

If w is an empty vector, or an argument is not provided, all elements are treated as having equal probability. The sum of w need not be normalized to 1. It is an error if w has a non-zero size that is different than the size of elem.

◆ getState()

const std::string RngThread::getState ( ) const

Get a serialized buffer holding the RNG state.

Returns
A string holding a serialized buffer containing the RNG state

The buffer returned by this routine can be supplied to the setState routine to restore the RNG state. Note that save-restore is only available when the maximum number of openMP threads is the same between the saved and restored states. If the number of threads differs, the resulting behavior is undefined; it is the responsibility of the calling program to ensure consistency.

◆ normal()

Real criptic::RngThread::normal ( const Real  mean = 0.0,
const Real  stddev = 1.0 
) const
inline

Return a normally-distributed random number.

Parameters
meanCentral value of distribution
stddevStandard deviation of distribution
Returns
A random number drawn from a normal distribution

◆ poisson()

IdxType criptic::RngThread::poisson ( const Real  ex) const
inline

Return an integer drawn from a Poisson distribution.

Parameters
exExpectation value of Poisson distribution
Returns
A random integer

◆ setState()

void RngThread::setState ( const std::string &  buf)

Restore the RNG state from a buffer.

Parameters
bufA string containing the serialized state

This routine operates on a stream provided by getState. Save-restore functionality only works if the maximum number of openMP threads is the same between the saved and restored states. If the number of threads differs, the resulting behavior is undefined; it is the responsibility of the calling program to ensure consistency.

◆ stateSize()

IdxType RngThread::stateSize ( )
static

Return the number of bytes in the RNG state buffer.

Returns
Number of characters in the RNG state buffer

This method is defined as static so that it can be called without access to any particular RNG.

◆ uniform()

Real criptic::RngThread::uniform ( const Real  a = 0.0,
const Real  b = 1.0 
) const
inline

Return a random number uniformly-distributed in (a,b)

Parameters
aMinimum random value
bMaximum random value
Returns
A random number in (a,b)

◆ uniformInt()

IdxType criptic::RngThread::uniformInt ( const IdxType  a = 0,
const IdxType  b = maxIdx 
) const
inline

Return a random integer uniformly-distributed in (a,b)

Parameters
aMinimum random value
bMaximum random value
Returns
A random integer in [a,b]

Member Data Documentation

◆ r

ThreadVec<pcg64 *> criptic::RngThread::r
private

Pointers to rng engines


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