9 #ifndef RandomGenerator_H 10 #define RandomGenerator_H 46 std::uniform_int_distribution<uint32_t>
m_uint32;
47 std::uniform_int_distribution<uint64_t>
m_uint64;
91 template <
typename T,
typename U,
typename V>
93 T& ret_number,
const U min_val,
const V max_val)
95 const T
range = max_val - min_val + 1;
98 ret_number = min_val + (rnd %
range);
107 2.3283064370807973754314699618685e-10;
117 MAT&
matrix,
const double unif_min = 0,
const double unif_max = 1)
119 for (
size_t r = 0;
r <
matrix.getRowCount();
r++)
120 for (
size_t c = 0;
c <
matrix.getColCount();
c++)
130 VEC&
v,
const double unif_min = 0,
const double unif_max = 1)
132 const size_t N =
v.size();
133 for (
size_t c = 0;
c < N;
c++)
170 for (
size_t r = 0;
r <
matrix.getRowCount();
r++)
171 for (
size_t c = 0;
c <
matrix.getColCount();
c++)
181 const size_t dim,
const double std_scale = 1.0,
182 const double diagonal_epsilon = 1e-8);
190 VEC&
v,
const double mean = 0,
const double std = 1)
192 const size_t N =
v.size();
193 for (
size_t c = 0;
c < N;
c++)
205 template <
typename T>
207 std::vector<T>& out_result,
209 const std::vector<T>*
mean =
nullptr);
217 template <
class VECTORLIKE,
class COVMATRIX>
219 VECTORLIKE& out_result,
const COVMATRIX&
cov,
220 const VECTORLIKE*
mean =
nullptr)
222 const size_t N =
cov.rows();
227 Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>
230 typename Eigen::SelfAdjointEigenSolver<
231 typename COVMATRIX::PlainObject>::MatrixType eigVecs =
232 eigensolver.eigenvectors();
233 typename Eigen::SelfAdjointEigenSolver<
234 typename COVMATRIX::PlainObject>::RealVectorType eigVals =
235 eigensolver.eigenvalues();
239 eigVals = eigVals.array().sqrt();
240 for (
typename COVMATRIX::Index i = 0; i < eigVecs.cols(); i++)
241 eigVecs.col(i) *= eigVals[i];
244 out_result.assign(N, 0);
246 for (
size_t i = 0; i < N; i++)
249 for (
size_t d = 0; d < N; d++)
250 out_result[d] += eigVecs.coeff(d, i) * rnd;
253 for (
size_t d = 0; d < N; d++) out_result[d] += (*
mean)[d];
263 template <
typename VECTOR_OF_VECTORS,
typename COVMATRIX>
265 VECTOR_OF_VECTORS& ret,
size_t desiredSamples,
const COVMATRIX&
cov,
266 const typename VECTOR_OF_VECTORS::value_type*
mean =
nullptr)
272 Eigen::SelfAdjointEigenSolver<typename COVMATRIX::PlainObject>
275 typename Eigen::SelfAdjointEigenSolver<
276 typename COVMATRIX::PlainObject>::MatrixType eigVecs =
277 eigensolver.eigenvectors();
278 typename Eigen::SelfAdjointEigenSolver<
279 typename COVMATRIX::PlainObject>::RealVectorType eigVals =
280 eigensolver.eigenvalues();
284 eigVals = eigVals.array().sqrt();
285 for (
typename COVMATRIX::Index i = 0; i < eigVecs.cols(); i++)
286 eigVecs.col(i) *= eigVals[i];
289 ret.resize(desiredSamples);
290 const size_t N =
cov.cols();
291 for (
size_t k = 0; k < desiredSamples; k++)
294 for (
size_t i = 0; i < N; i++)
297 for (
size_t d = 0; d < N; d++)
298 ret[k][d] += eigVecs.coeff(d, i) * rnd;
301 for (
size_t d = 0; d < N; d++) ret[k][d] += (*
mean)[d];
316 out_result = in_vector;
317 const size_t N = out_result.size();
318 if (N > 1) std::random_shuffle(&out_result[0], &out_result[N - 1]);
345 MAT&
matrix,
const double unif_min = 0,
const double unif_max = 1)
347 for (
size_t r = 0;
r <
matrix.getRowCount();
r++)
348 for (
size_t c = 0;
c <
matrix.getColCount();
c++)
358 std::vector<T>& v_out,
const T& unif_min = 0,
const T& unif_max = 1)
360 size_t n = v_out.size();
361 for (
size_t r = 0;
r <
n;
r++)
374 for (
size_t r = 0;
r <
matrix.getRowCount();
r++)
375 for (
size_t c = 0;
c <
matrix.getColCount();
c++)
385 std::vector<T>& v_out,
const T&
mean = 0,
const T&
std = 1)
387 size_t n = v_out.size();
388 for (
size_t r = 0;
r <
n;
r++)
402 const std::vector<T>& in_vector, std::vector<T>& out_result)
412 template <
typename T>
415 std::vector<T>& out_result)
433 template <
typename T>
436 std::vector<std::vector<T>>& ret,
437 std::vector<T>* samplesLikelihoods =
nullptr)
440 ret, desiredSamples,
cov,
static_cast<const std::vector<T>*
>(
nullptr),
449 template <
typename T,
typename MATRIXLIKE>
451 const MATRIXLIKE&
cov,
size_t desiredSamples,
452 std::vector<std::vector<T>>& ret)
462 template <
typename T,
typename MATRIXLIKE>
464 const MATRIXLIKE&
cov, std::vector<T>& out_result)
#define ASSERT_EQUAL_(__A, __B)
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
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 ...
void drawUniformUnsignedInt(uint32_t &ret_number)
You can call this overloaded method with either 32 or 64bit unsigned ints for the sake of general cod...
void drawGaussian1DVector(VEC &v, const double mean=0, const double std=1)
Fills the given vector with independent, 1D-normally distributed samples.
void randomNormalMultiDimensional(const mrpt::math::CMatrixTemplateNumeric< T > &cov, std::vector< T > &out_result)
Generate multidimensional random samples according to a given covariance matrix.
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
void drawGaussian1DMatrix(MAT &matrix, const double mean=0, const double std=1)
Fills the given matrix with independent, 1D-normally distributed samples.
std::uniform_int_distribution< uint64_t > m_uint64
std::normal_distribution< double > m_normdistribution
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
void randomPermutation(const std::vector< T > &in_vector, std::vector< T > &out_result)
Returns a random permutation of a vector: all the elements of the input vector are in the output but ...
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 drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
void drawUniformMatrix(MAT &matrix, const double unif_min=0, const double unif_max=1)
Fills the given matrix with independent, uniformly distributed samples.
void randomNormalMultiDimensionalMany(const mrpt::math::CMatrixTemplateNumeric< T > &cov, size_t desiredSamples, std::vector< std::vector< T >> &ret, std::vector< T > *samplesLikelihoods=nullptr)
Generate a given number of multidimensional random samples according to a given covariance matrix...
void drawGaussianMultivariate(VECTORLIKE &out_result, const COVMATRIX &cov, const VECTORLIKE *mean=nullptr)
Generate multidimensional random samples according to a given covariance matrix.
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...
void Randomize(const uint32_t seed)
Randomize the generators.
std::mt19937_64 m_MT19937
Data used internally by the MT19937 PRNG algorithm.
void vectorRandomNormal(std::vector< T > &v_out, const T &mean=0, const T &std=1)
Generates a random vector with independent, normally distributed samples.
CRandomGenerator(const uint32_t seed)
Constructor for providing a custom random seed to initialize the PRNG.
ptrdiff_t random_generator_for_STL(ptrdiff_t i)
A random number generator for usage in STL algorithms expecting a function like this (eg...
uint64_t drawUniform64bit()
Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32b...
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*...
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
void matrixRandomNormal(MAT &matrix, const double mean=0, const double std=1)
Fills the given matrix with independent, normally distributed samples.
void matrixRandomUni(MAT &matrix, const double unif_min=0, const double unif_max=1)
Fills the given matrix with independent, uniformly distributed samples.
void randomize()
Randomize the generators, based on current time.
void MT19937_initializeGenerator(const uint32_t &seed)
std::uniform_int_distribution< uint32_t > m_uint32
CRandomGenerator()
Default constructor: initialize random seed based on current time.
CONTAINER::value_type element_t
void vectorRandomUni(std::vector< T > &v_out, const T &unif_min=0, const T &unif_max=1)
Fills the given matrix with independent, uniformly distributed samples.
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
void drawGaussianMultivariate(std::vector< T > &out_result, const mrpt::math::CMatrixTemplateNumeric< T > &cov, const std::vector< T > *mean=nullptr)
Generate multidimensional random samples according to a given covariance matrix.
unsigned __int32 uint32_t
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
void drawUniformVector(VEC &v, const double unif_min=0, const double unif_max=1)
Fills the given vector with independent, uniformly distributed samples.
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
void drawUniformUnsignedInt(uint64_t &ret_number)
double drawGaussian1D_normalized()
Generate a normalized (mean=0, std=1) normally distributed sample.