mtrand.h
Go to the documentation of this file.
1 // mtrand.h
2 // C++ include file for MT19937, with initialization improved 2002/1/26.
3 // Coded by Takuji Nishimura and Makoto Matsumoto.
4 // Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/).
5 // The generators returning floating point numbers are based on
6 // a version by Isaku Wada, 2002/01/09
7 //
8 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
9 // All rights reserved.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions
13 // are met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. The names of its contributors may not be used to endorse or promote
23 // products derived from this software without specific prior written
24 // permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Any feedback is very welcome.
39 // http://www.math.keio.ac.jp/matumoto/emt.html
40 // email: matumoto@math.keio.ac.jp
41 //
42 // Feedback about the C++ port should be sent to Jasper Bedaux,
43 // see http://www.bedaux.net/mtrand/ for e-mail address and info.
44 
45 #ifndef MTRAND_H
46 #define MTRAND_H
47 
48 class MTRand_int32 { // Mersenne Twister random number generator
49 public:
50 // default constructor: uses default seed only if this is the first instance
51  MTRand_int32() { if (!init) seed(5489UL); init = true; }
52 // constructor with 32 bit int as seed
53  MTRand_int32(unsigned long s) { seed(s); init = true; }
54 // constructor with array of size 32 bit ints as seed
55  MTRand_int32(const unsigned long* array, int size) { seed(array, size); init = true; }
56 // the two seed functions
57  void seed(unsigned long); // seed with 32 bit integer
58  void seed(const unsigned long*, int size); // seed with array
59 // overload operator() to make this a generator (functor)
60  unsigned long operator()() { return rand_int32(); }
61 // 2007-02-11: made the destructor virtual; thanks "double more" for pointing this out
62  virtual ~MTRand_int32() {} // destructor
63 protected: // used by derived classes, otherwise not accessible; use the ()-operator
64  unsigned long rand_int32(); // generate 32 bit random integer
65 private:
66  static const int n = 624, m = 397; // compile time constants
67 // the variables below are static (no duplicates can exist)
68  static unsigned long state[n]; // state vector array
69  static int p; // position in state array
70  static bool init; // true if init function is called
71 // private functions used to generate the pseudo random numbers
72  unsigned long twiddle(unsigned long, unsigned long); // used by gen_state()
73  void gen_state(); // generate new state
74 // make copy constructor and assignment operator unavailable, they don't make sense
75  MTRand_int32(const MTRand_int32&); // copy constructor not defined
76  void operator=(const MTRand_int32&); // assignment operator not defined
77 };
78 
79 // inline for speed, must therefore reside in header file
80 inline unsigned long MTRand_int32::twiddle(unsigned long u, unsigned long v) {
81  return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1)
82  ^ ((v & 1UL) * 0x9908B0DFUL);
83 // 2013-07-22: line above modified for performance according to http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/Ierymenko.html
84 // thanks Vitaliy FEOKTISTOV for pointing this out
85 }
86 
87 inline unsigned long MTRand_int32::rand_int32() { // generate 32 bit random int
88  if (p == n) gen_state(); // new state vector needed
89 // gen_state() is split off to be non-inline, because it is only called once
90 // in every 624 calls and otherwise irand() would become too big to get inlined
91  unsigned long x = state[p++];
92  x ^= (x >> 11);
93  x ^= (x << 7) & 0x9D2C5680UL;
94  x ^= (x << 15) & 0xEFC60000UL;
95  return x ^ (x >> 18);
96 }
97 
98 // generates double floating point numbers in the half-open interval [0, 1)
99 class MTRand : public MTRand_int32 {
100 public:
102  MTRand(unsigned long seed) : MTRand_int32(seed) {}
103  MTRand(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
104  ~MTRand() {}
105  double operator()() {
106  return static_cast<double>(rand_int32()) * (1. / 4294967296.); } // divided by 2^32
107 private:
108  MTRand(const MTRand&); // copy constructor not defined
109  void operator=(const MTRand&); // assignment operator not defined
110 };
111 
112 // generates double floating point numbers in the closed interval [0, 1]
113 class MTRand_closed : public MTRand_int32 {
114 public:
116  MTRand_closed(unsigned long seed) : MTRand_int32(seed) {}
117  MTRand_closed(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
119  double operator()() {
120  return static_cast<double>(rand_int32()) * (1. / 4294967295.); } // divided by 2^32 - 1
121 private:
122  MTRand_closed(const MTRand_closed&); // copy constructor not defined
123  void operator=(const MTRand_closed&); // assignment operator not defined
124 };
125 
126 // generates double floating point numbers in the open interval (0, 1)
127 class MTRand_open : public MTRand_int32 {
128 public:
130  MTRand_open(unsigned long seed) : MTRand_int32(seed) {}
131  MTRand_open(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
133  double operator()() {
134  return (static_cast<double>(rand_int32()) + .5) * (1. / 4294967296.); } // divided by 2^32
135 private:
136  MTRand_open(const MTRand_open&); // copy constructor not defined
137  void operator=(const MTRand_open&); // assignment operator not defined
138 };
139 
140 // generates 53 bit resolution doubles in the half-open interval [0, 1)
141 class MTRand53 : public MTRand_int32 {
142 public:
144  MTRand53(unsigned long seed) : MTRand_int32(seed) {}
145  MTRand53(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
147  double operator()() {
148  return (static_cast<double>(rand_int32() >> 5) * 67108864. +
149  static_cast<double>(rand_int32() >> 6)) * (1. / 9007199254740992.); }
150 private:
151  MTRand53(const MTRand53&); // copy constructor not defined
152  void operator=(const MTRand53&); // assignment operator not defined
153 };
154 
155 #endif // MTRAND_H
MTRand_open(const unsigned long *seed, int size)
Definition: mtrand.h:131
MTRand_int32()
Definition: mtrand.h:51
MTRand(const unsigned long *seed, int size)
Definition: mtrand.h:103
MTRand()
Definition: mtrand.h:101
MTRand_int32(unsigned long s)
Definition: mtrand.h:53
MTRand_closed(const unsigned long *seed, int size)
Definition: mtrand.h:117
~MTRand_open()
Definition: mtrand.h:132
MTRand_open()
Definition: mtrand.h:129
~MTRand53()
Definition: mtrand.h:146
MTRand_closed()
Definition: mtrand.h:115
static bool init
Definition: mtrand.h:70
unsigned long operator()()
Definition: mtrand.h:60
void gen_state()
Definition: mtrand.cpp:12
MTRand_int32(const unsigned long *array, int size)
Definition: mtrand.h:55
MTRand_closed(unsigned long seed)
Definition: mtrand.h:116
double operator()()
Definition: mtrand.h:147
MTRand53(const unsigned long *seed, int size)
Definition: mtrand.h:145
unsigned long twiddle(unsigned long, unsigned long)
Definition: mtrand.h:80
MTRand_open(unsigned long seed)
Definition: mtrand.h:130
MTRand53()
Definition: mtrand.h:143
TFSIMD_FORCE_INLINE const tfScalar & x() const
static unsigned long state[n]
Definition: mtrand.h:68
void operator=(const MTRand_int32 &)
static int p
Definition: mtrand.h:69
unsigned long rand_int32()
Definition: mtrand.h:87
~MTRand_closed()
Definition: mtrand.h:118
double operator()()
Definition: mtrand.h:119
virtual ~MTRand_int32()
Definition: mtrand.h:62
Definition: mtrand.h:99
void seed(unsigned long)
Definition: mtrand.cpp:21
~MTRand()
Definition: mtrand.h:104
double operator()()
Definition: mtrand.h:133
MTRand(unsigned long seed)
Definition: mtrand.h:102
static const int m
Definition: mtrand.h:66
MTRand53(unsigned long seed)
Definition: mtrand.h:144
double operator()()
Definition: mtrand.h:105
static const int n
Definition: mtrand.h:66


rrt_exploration
Author(s): Hassan Umari
autogenerated on Mon Jun 10 2019 14:57:45