MRPT  2.0.0
List of all members | Protected Attributes
mrpt::random::CRandomGenerator Class Reference

Detailed Description

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 79 of file RandomGenerators.h.

#include <mrpt/random/RandomGenerators.h>

Public Member Functions

Initialization
 CRandomGenerator ()
 Default constructor: initialize random seed based on current time. More...
 
 CRandomGenerator (const uint32_t seed)
 Constructor for providing a custom random seed to initialize the PRNG. More...
 
void randomize (const uint32_t seed)
 Initialize the PRNG from the given random seed. More...
 
void randomize ()
 Randomize the generators, based on std::random_device. More...
 
Uniform pdf
uint32_t drawUniform32bit ()
 Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers. More...
 
uint64_t drawUniform64bit ()
 Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32bit() More...
 
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. More...
 
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) More...
 
template<typename return_t = double>
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. More...
 
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. More...
 
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. More...
 
Normal/Gaussian pdf
double drawGaussian1D_normalized ()
 Generate a normalized (mean=0, std=1) normally distributed sample. More...
 
template<typename return_t = double>
return_t drawGaussian1D (const double mean, const double std)
 Generate a normally distributed pseudo-random number. More...
 
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. More...
 
template<class MATRIX , class AUXVECTOR_T = MATRIX>
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. More...
 
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. More...
 
template<typename T , typename MATRIX >
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. More...
 
template<class VECTORLIKE , class COVMATRIX >
void drawGaussianMultivariate (VECTORLIKE &out_result, const COVMATRIX &cov, const VECTORLIKE *mean=nullptr)
 Generate multidimensional random samples according to a given covariance matrix. More...
 
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=nullptr)
 Generate a given number of multidimensional random samples according to a given covariance matrix. More...
 
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. More...
 

Protected Attributes

Generator_MT19937 m_MT19937
 Data used internally by the MT19937 PRNG algorithm. More...
 
std::normal_distribution< double > m_normdistribution
 
std::uniform_int_distribution< uint32_t > m_uint32
 
std::uniform_int_distribution< uint64_t > m_uint64
 

Constructor & Destructor Documentation

◆ CRandomGenerator() [1/2]

mrpt::random::CRandomGenerator::CRandomGenerator ( )
inline

Default constructor: initialize random seed based on current time.

Definition at line 94 of file RandomGenerators.h.

References randomize().

Here is the call graph for this function:

◆ CRandomGenerator() [2/2]

mrpt::random::CRandomGenerator::CRandomGenerator ( const uint32_t  seed)
inline

Constructor for providing a custom random seed to initialize the PRNG.

Definition at line 96 of file RandomGenerators.h.

References randomize().

Here is the call graph for this function:

Member Function Documentation

◆ drawDefinitePositiveMatrix()

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

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.

Definition at line 220 of file RandomGenerators.h.

References mrpt::math::cov(), drawGaussian1DMatrix(), mrpt::math::MatrixBase< Scalar, Derived >::matProductOf_AAt(), and mrpt::math::CMatrixDynamic< T >::resize().

Referenced by GraphSlamLevMarqTest< my_graph_t >::addEdge(), and TEST().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawGaussian1D()

template<typename return_t = double>
return_t mrpt::random::CRandomGenerator::drawGaussian1D ( const double  mean,
const double  std 
)
inline

Generate a normally distributed pseudo-random number.

Parameters
meanThe mean value of desired normal distribution
stdThe standard deviation value of desired normal distribution

Definition at line 194 of file RandomGenerators.h.

References drawGaussian1D_normalized(), and mrpt::math::mean().

Referenced by drawGaussian1DMatrix(), drawGaussian1DVector(), mrpt::maps::CBeaconMap::internal_insertObservation(), ransac_data_assoc_run(), mrpt::slam::CRejectionSamplingRangeOnlyLocalization::RS_drawFromProposal(), mrpt::apps::CGridMapAlignerApp::run(), mrpt::maps::CBeaconMap::simulateBeaconReadings(), and mrpt::maps::CLandmarksMap::simulateRangeBearingReadings().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawGaussian1D_normalized()

double CRandomGenerator::drawGaussian1D_normalized ( )

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

Parameters
likelihoodIf 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.

Definition at line 96 of file RandomGenerator.cpp.

References m_MT19937, and m_normdistribution.

Referenced by mrpt::obs::CActionRobotMovement2D::computeFromOdometry_modelThrun(), mrpt::poses::CPoseRandomSampler::do_sample_2D(), mrpt::poses::CPoseRandomSampler::do_sample_3D(), drawGaussian1D(), drawGaussianMultivariate(), drawGaussianMultivariateMany(), mrpt::obs::CActionRobotMovement2D::drawSingleSample_modelThrun(), mrpt::obs::CActionRobotMovement2D::fastDrawSingleSample_modelGaussian(), mrpt::random::matrixRandomNormal(), mrpt::maps::CLandmarksMap::simulateBeaconReadings(), mrpt::kinematics::CVehicleSimulVirtualBase::simulateOneTimeStep(), mrpt::maps::CLandmarksMap::simulateRangeBearingReadings(), mrpt::maps::COccupancyGridMap2D::simulateScanRay(), and mrpt::random::vectorRandomNormal().

Here is the caller graph for this function:

◆ drawGaussian1DMatrix()

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

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

Matrix classes can be mrpt::math::CMatrixDynamic or mrpt::math::CMatrixFixed

See also
drawGaussian1D

Definition at line 206 of file RandomGenerators.h.

References drawGaussian1D(), and mrpt::math::mean().

Referenced by do_test_init_random(), drawDefinitePositiveMatrix(), PosePDFGaussTests::generateRandomPose2DPDF(), Pose3DPDFGaussTests::generateRandomPose3DPDF(), and Pose3DQuatPDFGaussTests::generateRandomPose3DPDF().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawGaussian1DVector()

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

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

See also
drawGaussian1D

Definition at line 239 of file RandomGenerators.h.

References drawGaussian1D(), and mrpt::math::mean().

Referenced by TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawGaussianMultivariate() [1/2]

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

Generate multidimensional random samples according to a given covariance matrix.

Mean is assumed to be zero if mean==nullptr.

Exceptions
std::exceptionOn invalid covariance matrix
See also
drawGaussianMultivariateMany

Computes the eigenvalues/eigenvector decomposition of this matrix, so that: M = Z * D * ZT, where columns in Z are the eigenvectors and the diagonal matrix D contains the eigenvalues as diagonal elements, sorted in ascending order.

Definition at line 255 of file RandomGenerators.h.

References mrpt::math::MatrixVectorBase< Scalar, Derived >::array(), mrpt::math::CMatrixDynamic< T >::cols(), mrpt::math::cov(), drawGaussian1D_normalized(), mrpt::math::mean(), and mrpt::math::CMatrixDynamic< T >::rows().

Referenced by mrpt::poses::CPoint2DPDFGaussian::drawSingleSample(), mrpt::poses::CPose3DQuatPDFGaussianInf::drawSingleSample(), mrpt::poses::CPose3DQuatPDFGaussian::drawSingleSample(), mrpt::poses::CPose3DPDFGaussianInf::drawSingleSample(), mrpt::poses::CPosePDFGaussian::drawSingleSample(), mrpt::poses::CPointPDFGaussian::drawSingleSample(), mrpt::poses::CPose3DPDFGaussian::drawSingleSample(), mrpt::poses::CPosePDFGaussianInf::drawSingleSample(), mrpt::poses::CPointPDFSOG::drawSingleSample(), mrpt::maps::CMultiMetricMapPDF::prediction_and_update_pfOptimalProposal(), and mrpt::random::randomNormalMultiDimensional().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawGaussianMultivariate() [2/2]

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

Generate multidimensional random samples according to a given covariance matrix.

Mean is assumed to be zero if mean==nullptr.

Exceptions
std::exceptionOn invalid covariance matrix
See also
drawGaussianMultivariateMany

Definition at line 295 of file RandomGenerators.h.

References mrpt::math::CMatrixDynamic< T >::cols(), mrpt::math::cov(), drawGaussian1D_normalized(), mrpt::math::MatrixBase< Scalar, Derived >::eig_symmetric(), mrpt::math::mean(), and mrpt::math::CMatrixDynamic< T >::rows().

Here is the call graph for this function:

◆ drawGaussianMultivariateMany()

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

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

Parameters
covThe covariance matrix where to draw the samples from.
desiredSamplesThe number of samples to generate.
retThe output list of samples
meanThe mean, or zeros if mean==nullptr.

Definition at line 342 of file RandomGenerators.h.

References mrpt::math::CMatrixDynamic< T >::cols(), mrpt::math::cov(), drawGaussian1D_normalized(), mrpt::math::MatrixBase< Scalar, Derived >::eig_symmetric(), mrpt::math::mean(), and mrpt::math::CMatrixDynamic< T >::rows().

Referenced by mrpt::poses::CPosePDFParticles::copyFrom(), mrpt::poses::CPose3DQuatPDFGaussianInf::drawManySamples(), mrpt::poses::CPose3DQuatPDFGaussian::drawManySamples(), mrpt::poses::CPose3DPDFGaussianInf::drawManySamples(), mrpt::poses::CPosePDFGaussian::drawManySamples(), mrpt::poses::CPose3DPDFGaussian::drawManySamples(), mrpt::poses::CPosePDFGaussianInf::drawManySamples(), mrpt::random::randomNormalMultiDimensionalMany(), and mrpt::math::transform_gaussian_montecarlo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawUniform()

template<typename return_t = double>
return_t mrpt::random::CRandomGenerator::drawUniform ( const double  Min,
const double  Max 
)
inline

◆ drawUniform32bit()

uint32_t CRandomGenerator::drawUniform32bit ( )

◆ drawUniform64bit()

uint64_t CRandomGenerator::drawUniform64bit ( )

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

Definition at line 90 of file RandomGenerator.cpp.

References m_MT19937, and m_uint64.

◆ drawUniformMatrix()

template<class MAT >
void mrpt::random::CRandomGenerator::drawUniformMatrix ( MAT &  matrix,
const double  unif_min = 0,
const double  unif_max = 1 
)
inline

Fills the given matrix with independent, uniformly distributed samples.

Matrix classes can be mrpt::math::CMatrixDynamic or mrpt::math::CMatrixFixed

See also
drawUniform

Definition at line 155 of file RandomGenerators.h.

References drawUniform().

Here is the call graph for this function:

◆ drawUniformUnsignedInt() [1/2]

void mrpt::random::CRandomGenerator::drawUniformUnsignedInt ( uint32_t &  ret_number)
inline

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

Definition at line 118 of file RandomGenerators.h.

References m_MT19937, and m_uint32.

Referenced by drawUniformUnsignedIntRange().

Here is the caller graph for this function:

◆ drawUniformUnsignedInt() [2/2]

void mrpt::random::CRandomGenerator::drawUniformUnsignedInt ( uint64_t &  ret_number)
inline

Definition at line 122 of file RandomGenerators.h.

References m_MT19937, and m_uint64.

◆ drawUniformUnsignedIntRange()

template<typename T , typename U , typename V >
void mrpt::random::CRandomGenerator::drawUniformUnsignedIntRange ( T &  ret_number,
const U  min_val,
const V  max_val 
)
inline

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

Definition at line 130 of file RandomGenerators.h.

References drawUniformUnsignedInt().

Here is the call graph for this function:

◆ drawUniformVector()

template<class VEC >
void mrpt::random::CRandomGenerator::drawUniformVector ( VEC &  v,
const double  unif_min = 0,
const double  unif_max = 1 
)
inline

Fills the given vector with independent, uniformly distributed samples.

See also
drawUniform

Definition at line 168 of file RandomGenerators.h.

References drawUniform().

Referenced by mrpt::bayes::CParticleFilterCapable::computeResampling(), and mrpt::math::RANSAC_Template< NUMTYPE >::execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ permuteVector()

template<class VEC >
void mrpt::random::CRandomGenerator::permuteVector ( const VEC &  in_vector,
VEC &  out_result 
)
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 393 of file RandomGenerators.h.

References mrpt::random::shuffle().

Referenced by mrpt::random::randomPermutation(), mrpt::tfest::se2_l2_robust(), and mrpt::tfest::se3_l2_robust().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ randomize() [1/2]

void CRandomGenerator::randomize ( const uint32_t  seed)

Initialize the PRNG from the given random seed.

Definition at line 92 of file RandomGenerator.cpp.

References m_MT19937, and mrpt::random::Generator_MT19937::seed().

Referenced by mrpt::apps::MonteCarloLocalization_Base::do_pf_localization(), mrpt::random::Randomize(), ransac_data_assoc_run(), mrpt::apps::CGridMapAlignerApp::run(), run_test_pf_localization(), TEST(), mrpt::hmtslam::CHMTSLAM::thread_LSLAM(), and mrpt::hmtslam::CHMTSLAM::thread_TBI().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ randomize() [2/2]

void CRandomGenerator::randomize ( )

Randomize the generators, based on std::random_device.

Definition at line 94 of file RandomGenerator.cpp.

References m_MT19937, and mrpt::random::Generator_MT19937::seed().

Referenced by CRandomGenerator().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_MT19937

Generator_MT19937 mrpt::random::CRandomGenerator::m_MT19937
protected

Data used internally by the MT19937 PRNG algorithm.

Definition at line 83 of file RandomGenerators.h.

Referenced by drawGaussian1D_normalized(), drawUniform32bit(), drawUniform64bit(), drawUniformUnsignedInt(), and randomize().

◆ m_normdistribution

std::normal_distribution<double> mrpt::random::CRandomGenerator::m_normdistribution
protected

Definition at line 85 of file RandomGenerators.h.

Referenced by drawGaussian1D_normalized().

◆ m_uint32

std::uniform_int_distribution<uint32_t> mrpt::random::CRandomGenerator::m_uint32
protected

Definition at line 86 of file RandomGenerators.h.

Referenced by drawUniform32bit(), and drawUniformUnsignedInt().

◆ m_uint64

std::uniform_int_distribution<uint64_t> mrpt::random::CRandomGenerator::m_uint64
protected

Definition at line 87 of file RandomGenerators.h.

Referenced by drawUniform64bit(), and drawUniformUnsignedInt().




Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020