MRPT  2.0.4
RandomGenerator.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "random-precomp.h" // Precompiled headers
11 
13 
14 using namespace mrpt::random;
15 
16 // The global instance of CRandomGenerator for single-thread programs:
18 
19 #if defined(_MSC_VER)
20 #pragma warning(push)
21 #pragma warning(disable : 4146)
22 #endif
23 
24 // Code from the implementation by Rick Wagner
25 // http://www-personal.umich.edu/~wagnerr/MersenneTwister.html
26 inline uint32_t hiBit(const uint32_t u) { return u & 0x80000000UL; }
27 inline uint32_t loBit(const uint32_t u) { return u & 0x00000001UL; }
28 inline uint32_t loBits(const uint32_t u) { return u & 0x7fffffffUL; }
29 inline uint32_t mixBits(const uint32_t u, const uint32_t v)
30 {
31  return hiBit(u) | loBits(v);
32 }
33 inline uint32_t twist(const uint32_t m, const uint32_t s0, const uint32_t s1)
34 {
35  return m ^ (mixBits(s0, s1) >> 1) ^ (-loBit(s1) & 0x9908b0dfUL);
36 }
37 
38 #if defined(_MSC_VER)
39 #pragma warning(pop)
40 #endif
41 
43 {
44  if (!m_index) generateNumbers();
45 
46  uint32_t y = m_MT[m_index];
47  y ^= y >> 11;
48  y ^= (y << 7) & 2636928640U; // 0x9d2c5680
49  y ^= (y << 15) & 4022730752U; // 0xefc60000
50  y ^= (y >> 18);
51 
52  // Wrap index to [0,623].
53  m_index++;
54  if (m_index >= 624) m_index = 0;
55 
56  return y;
57 }
58 
60 {
61  if (!m_seed_initialized) this->seed(std::random_device{}());
62 
63  // Code from the implementation by Rick Wagner
64  // http://www-personal.umich.edu/~wagnerr/MersenneTwister.html
65  // and:
66  // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
67  //
68  const int N = 624; // length of state vector
69  const int M = 397; // period parameter
70 
71  uint32_t* p = m_MT;
72  for (int i = N - M; i--; ++p) *p = twist(p[M], p[0], p[1]);
73  for (int i = M; --i; ++p) *p = twist(p[M - N], p[0], p[1]);
74  *p = twist(p[M - N], p[0], m_MT[0]);
75 }
76 
77 void Generator_MT19937::seed(const uint32_t seed)
78 {
79  m_seed_initialized = true;
80  m_MT[0] = seed;
81  // 0x6c078965
82  for (uint32_t i = 1; i < 624; i++)
83  m_MT[i] = static_cast<uint32_t>(
84  1812433253 * (m_MT[i - 1] ^ (m_MT[i - 1] >> 30)) + i);
85 
86  m_index = 0;
87 }
88 
92 void CRandomGenerator::randomize(const uint32_t seed) { m_MT19937.seed(seed); }
93 
94 void CRandomGenerator::randomize() { m_MT19937.seed(std::random_device{}()); }
95 
97 {
99 }
A namespace of pseudo-random numbers generators of diferent distributions.
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
uint32_t loBit(const uint32_t u)
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...
static CRandomGenerator randomGenerator
uint32_t hiBit(const uint32_t u)
uint64_t drawUniform64bit()
Returns a uniformly distributed pseudo-random number by joining two 32bit numbers from drawUniform32b...
uint32_t mixBits(const uint32_t u, const uint32_t v)
void randomize()
Randomize the generators, based on std::random_device.
std::uniform_int_distribution< uint32_t > m_uint32
uint32_t twist(const uint32_t m, const uint32_t s0, const uint32_t s1)
Generator_MT19937 m_MT19937
Data used internally by the MT19937 PRNG algorithm.
void seed(const uint32_t seed)
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
uint32_t loBits(const uint32_t u)
double drawGaussian1D_normalized()
Generate a normalized (mean=0, std=1) normally distributed sample.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020