|
criptic v1
Cosmic Ray Interstellar Propagation Tool using Itô Calculus
|
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 |
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.
| RngThread::RngThread | ( | const ParmParser & | pp | ) |
Constructs an RngThread object.
| pp | The parameter parser, which contains the input deck |
| RngThread::RngThread | ( | std::string & | buf | ) |
Constructs an RngThread object from a serialized buffer.
| buf | A string containing the serialized state |
| RngThread::~RngThread | ( | ) |
Free RngThread object memory.
|
inline |
Randomly choose an element from a const vector.
| T | Type held by vector |
| elem | Vector of elements from which to choose |
| w | Relative probability of each element |
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.
|
inline |
Randomly choose an element from a vector.
| T | Type held by vector |
| elem | Vector of elements from which to choose |
| w | Relative probability of each element |
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.
| const std::string RngThread::getState | ( | ) | const |
Get a serialized buffer holding 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.
Return a normally-distributed random number.
| mean | Central value of distribution |
| stddev | Standard deviation of distribution |
Return an integer drawn from a Poisson distribution.
| ex | Expectation value of Poisson distribution |
| void RngThread::setState | ( | const std::string & | buf | ) |
Restore the RNG state from a buffer.
| buf | A 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.
|
static |
Return the number of bytes in the RNG state buffer.
This method is defined as static so that it can be called without access to any particular RNG.
Return a random number uniformly-distributed in (a,b)
| a | Minimum random value |
| b | Maximum random value |
|
inline |
Return a random integer uniformly-distributed in (a,b)
| a | Minimum random value |
| b | Maximum random value |
|
private |
Pointers to rng engines