[mrpt-random]
Overview
Random number generator C++ library support uniform, Gaussian, multivariable distributions.
Library mrpt-random
This library is part of MRPT but it’s pure C++11 and has no other dependencies. It can be installed in Debian-based systems with:
sudo apt install libmrpt-random-dev
Read also how to import MRPT into your CMake scripts.
Overview
mrpt-random provides pseudo-random number generators based on a portable implementation of the MT19937 (Mersenne twister) algorithm, which produces the same sequence for a given seed on any compiler and platform. Seeding the generator makes simulations and unit tests reproducible. Note that Gaussian samples and 64-bit integers are drawn with C++ standard library distributions, so those sequences may differ between standard libraries.
Key classes and functions
mrpt::random::CRandomGenerator : the main generator, with methods to draw uniform and Gaussian scalars, vectors and matrices, multivariate Gaussian samples from a covariance matrix, and random permutations.
mrpt::random::getRandomGenerator() : a thread-local, ready-to-use CRandomGenerator. In multi-threaded programs that need reproducible results, create one generator per thread with its own seed instead.
mrpt::random::Generator_MT19937 : the bare MT19937 engine, usable as a standard C++
UniformRandomBitGenerator.mrpt::random::shuffle() and mrpt::random::portable_uniform_distribution() : platform-independent replacements of
std::shuffle()andstd::uniform_int_distribution, to be used with Generator_MT19937.
Example
#include <mrpt/random/RandomGenerators.h> // Fixed seed: the same sequence on every run. mrpt::random::CRandomGenerator rng(1234); const double u = rng.drawUniform(-1.0, 1.0); const double g = rng.drawGaussian1D(0.0, 0.5); // mean, std. deviation std::vector<double> noise(100); rng.drawGaussian1DVector(noise, 0.0, 0.1); const std::vector<int> ids = {0, 1, 2, 3, 4}; const auto shuffled = rng.permuteVector(ids);
Examples
random_examples: histograms of samples from several distributions.
Python: mrpt_random_example.py.
Library contents
// classes class mrpt::random::CRandomGenerator; class mrpt::random::Generator_MT19937; // global functions template <class URBG> uint64_t mrpt::random::portable_uniform_distribution(URBG&& g, uint64_t min, uint64_t max); template <class RandomIt, class URBG> void mrpt::random::shuffle( RandomIt first, RandomIt last, URBG&& g ); template <class RandomIt> void mrpt::random::shuffle(RandomIt first, RandomIt last); template <class RandomIt, class URBG> void mrpt::random::partial_shuffle( RandomIt first, RandomIt last, URBG&& g, size_t N );
Global Functions
template <class URBG> uint64_t mrpt::random::portable_uniform_distribution( URBG&& g, uint64_t min, uint64_t max )
A compiler and platform-independent uniform distribution generator.
Returns an integer in the closed range [min, max], built from two consecutive 32-bit outputs of the generator (higher bits first).
Parameters:
URGB |
Can be a C++11 std random generator, or mrpt::random::Generator_MT19937 |
template <class RandomIt, class URBG> void mrpt::random::shuffle( RandomIt first, RandomIt last, URBG&& g )
Uniform shuffle a sequence (Fisher-Yates), using the given random generator.
template <class RandomIt> void mrpt::random::shuffle( RandomIt first, RandomIt last )
Uniform shuffle a sequence.
It uses an internal std::mt19937 seeded from std::random_device, so results are not reproducible and do not depend on the seed of mrpt::random::getRandomGenerator(). Use the overload taking a generator for reproducible results.
template <class RandomIt, class URBG> void mrpt::random::partial_shuffle( RandomIt first, RandomIt last, URBG&& g, size_t N )
Shuffle the first N elements of a sequence.
Note that elements at positions [N:end] may also change if they are randomly picked for permutation with the first [0,N-1] elements.
[New in MRPT 2.4.0]