A thred-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
Definition at line 32 of file RandomGenerators.h.
#include <mrpt/random/RandomGenerators.h>
Classes | |
struct | TMT19937_data |
Data used internally by the MT19937 PRNG algorithm. More... | |
Public Member Functions | |
Initialization | |
CRandomGenerator () | |
Default constructor: initialize random seed based on current time. | |
CRandomGenerator (const uint32_t seed) | |
Constructor for providing a custom random seed to initialize the PRNG. | |
void | randomize (const uint32_t seed) |
Initialize the PRNG from the given random seed. | |
void | randomize () |
Randomize the generators, based on current time. | |
Uniform pdf | |
uint32_t | drawUniform32bit () |
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers. | |
uint64_t | drawUniform64bit () |
Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32bit() | |
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. | |
void | drawUniformUnsignedInt (uint64_t &ret_number) |
template<typename T , typename U , typename V > | |
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) | |
double | 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 > | |
void | drawUniformMatrix (MAT &matrix, const double unif_min=0, const double unif_max=1) |
Fills the given matrix with independent, uniformly distributed samples. | |
template<class VEC > | |
void | drawUniformVector (VEC &v, const double unif_min=0, const double unif_max=1) |
Fills the given vector with independent, uniformly distributed samples. | |
Normal/Gaussian pdf | |
double | drawGaussian1D_normalized (double *likelihood=NULL) |
Generate a normalized (mean=0, std=1) normally distributed sample. | |
double | drawGaussian1D (const double mean, const double std) |
Generate a normally distributed pseudo-random number. | |
template<class MAT > | |
void | drawGaussian1DMatrix (MAT &matrix, const double mean=0, const double std=1) |
Fills the given matrix with independent, 1D-normally distributed samples. | |
mrpt::math::CMatrixDouble | 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 > | |
void | drawGaussian1DVector (VEC &v, const double mean=0, const double std=1) |
Fills the given vector with independent, 1D-normally distributed samples. | |
template<typename T > | |
void | drawGaussianMultivariate (std::vector< T > &out_result, const mrpt::math::CMatrixTemplateNumeric< T > &cov, const std::vector< T > *mean=NULL) |
Generate multidimensional random samples according to a given covariance matrix. | |
template<class VECTORLIKE , class COVMATRIX > | |
void | drawGaussianMultivariate (VECTORLIKE &out_result, const COVMATRIX &cov, const VECTORLIKE *mean=NULL) |
Generate multidimensional random samples according to a given covariance matrix. | |
template<typename VECTOR_OF_VECTORS , typename COVMATRIX > | |
void | drawGaussianMultivariateMany (VECTOR_OF_VECTORS &ret, size_t desiredSamples, const COVMATRIX &cov, const typename VECTOR_OF_VECTORS::value_type *mean=NULL) |
Generate a given number of multidimensional random samples according to a given covariance matrix. | |
Miscellaneous | |
template<class VEC > | |
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. | |
Protected Member Functions | |
void | MT19937_generateNumbers () |
void | MT19937_initializeGenerator (const uint32_t &seed) |
Protected Attributes | |
struct mrpt::random::CRandomGenerator::TMT19937_data | m_MT19937_data |
bool | m_std_gauss_set |
double | m_std_gauss_next |
|
inline |
Default constructor: initialize random seed based on current time.
Definition at line 57 of file RandomGenerators.h.
|
inline |
Constructor for providing a custom random seed to initialize the PRNG.
Definition at line 60 of file RandomGenerators.h.
mrpt::math::CMatrixDouble mrpt::random::CRandomGenerator::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.
|
inline |
Generate a normally distributed pseudo-random number.
mean | The mean value of desired normal distribution |
std | The standard deviation value of desired normal distribution |
Definition at line 139 of file RandomGenerators.h.
References mean().
double mrpt::random::CRandomGenerator::drawGaussian1D_normalized | ( | double * | likelihood = NULL | ) |
Generate a normalized (mean=0, std=1) normally distributed sample.
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. |
Referenced by mrpt::random::matrixRandomNormal(), and mrpt::random::vectorRandomNormal().
|
inline |
Fills the given matrix with independent, 1D-normally distributed samples.
Matrix classes can be mrpt::math::CMatrixTemplateNumeric or mrpt::math::CMatrixFixedNumeric
Definition at line 148 of file RandomGenerators.h.
References mean().
|
inline |
Fills the given vector with independent, 1D-normally distributed samples.
Definition at line 166 of file RandomGenerators.h.
References mean().
void mrpt::random::CRandomGenerator::drawGaussianMultivariate | ( | std::vector< T > & | out_result, |
const mrpt::math::CMatrixTemplateNumeric< T > & | cov, | ||
const std::vector< T > * | mean = NULL |
||
) |
Generate multidimensional random samples according to a given covariance matrix.
Mean is assumed to be zero if mean==NULL.
std::exception | On invalid covariance matrix |
Referenced by mrpt::random::randomNormalMultiDimensional().
|
inline |
Generate multidimensional random samples according to a given covariance matrix.
Mean is assumed to be zero if mean==NULL.
std::exception | On invalid covariance matrix |
Definition at line 195 of file RandomGenerators.h.
References ASSERT_, ASSERT_EQUAL_, and mean().
|
inline |
Generate a given number of multidimensional random samples according to a given covariance matrix.
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==NULL. |
Definition at line 238 of file RandomGenerators.h.
References ASSERT_EQUAL_, and mean().
Referenced by mrpt::random::randomNormalMultiDimensionalMany(), and mrpt::math::transform_gaussian_montecarlo().
|
inline |
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
Definition at line 92 of file RandomGenerators.h.
Referenced by mrpt::random::matrixRandomUni(), and mrpt::random::vectorRandomUni().
uint32_t mrpt::random::CRandomGenerator::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
Referenced by mrpt::slam::PF_implementation< PARTICLE_TYPE, MYSELF >::PF_SLAM_aux_perform_one_rejection_sampling_step(), mrpt::slam::PF_implementation< PARTICLE_TYPE, MYSELF >::PF_SLAM_implementation_pfAuxiliaryPFStandardAndOptimal(), and mrpt::random::random_generator_for_STL().
uint64_t mrpt::random::CRandomGenerator::drawUniform64bit | ( | ) |
Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32bit()
|
inline |
Fills the given matrix with independent, uniformly distributed samples.
Matrix classes can be mrpt::math::CMatrixTemplateNumeric or mrpt::math::CMatrixFixedNumeric
Definition at line 101 of file RandomGenerators.h.
|
inline |
You can call this overloaded method with either 32 or 64bit unsigned ints for the sake of general coding.
Definition at line 78 of file RandomGenerators.h.
|
inline |
Definition at line 79 of file RandomGenerators.h.
|
inline |
Return a uniform unsigned integer in the range [min_val,max_val] (both inclusive)
Definition at line 83 of file RandomGenerators.h.
|
inline |
Fills the given vector with independent, uniformly distributed samples.
Definition at line 115 of file RandomGenerators.h.
|
protected |
|
protected |
|
inline |
Returns a random permutation of a vector: all the elements of the input vector are in the output but at random positions.
Definition at line 287 of file RandomGenerators.h.
Referenced by mrpt::random::randomPermutation().
void mrpt::random::CRandomGenerator::randomize | ( | ) |
Randomize the generators, based on current time.
void mrpt::random::CRandomGenerator::randomize | ( | const uint32_t | seed | ) |
Initialize the PRNG from the given random seed.
Referenced by mrpt::random::Randomize().
|
protected |
|
protected |
Definition at line 46 of file RandomGenerators.h.
|
protected |
Definition at line 45 of file RandomGenerators.h.
Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Thu Jan 19 22:37:47 UTC 2023 |