class mrpt::random::CRandomGenerator

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

#include <mrpt/random/RandomGenerators.h>

class CRandomGenerator
{
public:
    // construction

    CRandomGenerator();
    CRandomGenerator(const uint32_t seed);

    //
methods

    void randomize(const uint32_t seed);
    void randomize();
    uint32_t drawUniform32bit();
    uint64_t drawUniform64bit();
    void drawUniformUnsignedInt(uint32_t& ret_number);
    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
        );

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

    template <class MAT>
    void drawUniformMatrix(
        MAT& matrix,
        const double unif_min = 0,
        const double unif_max = 1
        );

    template <class VEC>
    void drawUniformVector(
        VEC& v,
        const double unif_min = 0,
        const double unif_max = 1
        );

    double drawGaussian1D_normalized();

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

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

    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
        );

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

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

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

    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
        );

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

    template <class VEC>
    VEC permuteVector(const VEC& in_vector);
};

Construction

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.

Methods

void randomize(const uint32_t seed)

Initialize the PRNG from the given random seed.

void randomize()

Randomize the generators, based on std::random_device.

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()

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.

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)

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.

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.

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

See also:

drawUniform

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.

See also:

drawUniform

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

Parameters:

std::exception

On invalid covariance matrix

See also:

drawGaussianMultivariateMany

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.

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

Parameters:

std::exception

On invalid covariance matrix

See also:

drawGaussianMultivariateMany

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.

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.

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.

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.