Class CRandomGenerator

Class Documentation

class CRandomGenerator

A thread-safe pseudo random number generator, based on an internal MT19937 randomness generator. The base algorithm for randomness is platform-independent. See http://en.wikipedia.org/wiki/Mersenne_twister

For real thread-safety, each thread must create and use its own instance of this class.

Single-thread programs can use the static object mrpt::random::randomGenerator

Initialization

inline CRandomGenerator()

Default constructor: initialize random seed based on current time

inline CRandomGenerator(const uint32_t seed)

Constructor for providing a custom random seed to initialize the PRNG

void randomize(uint32_t seed)

Initialize the PRNG from the given random seed

void randomize()

Randomize the generators, based on std::random_device

Uniform pdf

uint32_t drawUniform32bit()

Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers. See: http://en.wikipedia.org/wiki/Mersenne_twister

uint64_t drawUniform64bit()

Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32bit()

inline void drawUniformUnsignedInt(uint32_t &ret_number)

You can call this overloaded method with either 32 or 64bit unsigned ints for the sake of general coding.

inline void drawUniformUnsignedInt(uint64_t &ret_number)
template<typename T, typename U, typename V>
inline void drawUniformUnsignedIntRange(T &ret_number, const U min_val, const V max_val)

Return a uniform unsigned integer in the range [min_val,max_val] (both inclusive)

template<typename return_t = double>
inline return_t drawUniform(const double Min, const double Max)

Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.

template<class MAT>
inline void drawUniformMatrix(MAT &matrix, const double min = 0, const double max = 1)

Fills the given matrix with independent, uniformly distributed samples. Matrix classes can be mrpt::math::CMatrixDynamic or mrpt::math::CMatrixFixed

See also

drawUniform

template<class VEC>
inline void drawUniformVector(VEC &v, const double min = 0, const double max = 1)

Fills the given vector with independent, uniformly distributed samples.

See also

drawUniform

Normal/Gaussian pdf

double drawGaussian1D_normalized()

Generate a normalized (mean=0, std=1) normally distributed sample.

Parameters:

likelihood – If desired, pass a pointer to a double which will receive the likelihood of the given sample to have been obtained, that is, the value of the normal pdf at the sample value.

template<typename return_t = double>
inline return_t drawGaussian1D(const double mean, const double std)

Generate a normally distributed pseudo-random number.

Parameters:
  • mean – The mean value of desired normal distribution

  • std – The standard deviation value of desired normal distribution

template<class MAT>
inline void drawGaussian1DMatrix(MAT &matrix, const double mean = 0, const double std = 1)

Fills the given matrix with independent, 1D-normally distributed samples. Matrix classes can be mrpt::math::CMatrixDynamic or mrpt::math::CMatrixFixed

See also

drawGaussian1D

template<class MATRIX, class AUXVECTOR_T = MATRIX>
inline MATRIX drawDefinitePositiveMatrix(const size_t dim, const double std_scale = 1.0, const double diagonal_epsilon = 1e-8)

Generates a random definite-positive matrix of the given size, using the formula C = v*v^t + epsilon*I, with “v” being a vector of gaussian random samples.

template<class VEC>
inline void drawGaussian1DVector(VEC &v, const double mean = 0, const double std = 1)

Fills the given vector with independent, 1D-normally distributed samples.

See also

drawGaussian1D

template<typename T, typename MATRIX>
inline void drawGaussianMultivariate(std::vector<T> &out_result, const MATRIX &cov, const std::vector<T> *mean = nullptr)

Generate multidimensional random samples according to a given covariance matrix. Mean is assumed to be zero if mean==nullptr.

Throws:

std::exception – On invalid covariance matrix

template<class VECTORLIKE, class COVMATRIX>
inline void drawGaussianMultivariate(VECTORLIKE &out_result, const COVMATRIX &cov, const VECTORLIKE *mean = nullptr)

Generate multidimensional random samples according to a given covariance matrix. Mean is assumed to be zero if mean==nullptr.

Throws:

std::exception – On invalid covariance matrix

template<typename VECTOR_OF_VECTORS, typename COVMATRIX>
inline void drawGaussianMultivariateMany(VECTOR_OF_VECTORS &ret, size_t desiredSamples, const COVMATRIX &cov, const typename VECTOR_OF_VECTORS::value_type *mean = nullptr)

Generate a given number of multidimensional random samples according to a given covariance matrix.

Parameters:
  • cov – The covariance matrix where to draw the samples from.

  • desiredSamples – The number of samples to generate.

  • ret – The output list of samples

  • mean – The mean, or zeros if mean==nullptr.

Miscellaneous

template<class VEC>
inline void permuteVector(const VEC &in_vector, VEC &out_result)

Returns a random permutation of a vector: all the elements of the input vector are in the output but at random positions.

template<class VEC>
inline VEC permuteVector(const VEC &in_vector)

Protected Attributes

Generator_MT19937 m_MT19937

Data used internally by the MT19937 PRNG algorithm.

std::normal_distribution<double> m_normdistribution
std::uniform_int_distribution<uint32_t> m_uint32
std::uniform_int_distribution<uint64_t> m_uint64