Simpatico  v1.10
mtrand.h
1 #ifndef UTIL_MTRAND_H
2 #define UTIL_MTRAND_H
3 
4 // mtrand.h
5 // C++ include file for MT19937, with initialization improved 2002/1/26.
6 // Coded by Takuji Nishimura and Makoto Matsumoto.
7 // Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/).
8 // Modified by David Morse 2011/12/1
9 // The generators returning floating point numbers are based on
10 // a version by Isaku Wada, 2002/01/09
11 //
12 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
13 // All rights reserved.
14 //
15 // Redistribution and use in source and binary forms, with or without
16 // modification, are permitted provided that the following conditions
17 // are met:
18 //
19 // 1. Redistributions of source code must retain the above copyright
20 // notice, this list of conditions and the following disclaimer.
21 //
22 // 2. Redistributions in binary form must reproduce the above copyright
23 // notice, this list of conditions and the following disclaimer in the
24 // documentation and/or other materials provided with the distribution.
25 //
26 // 3. The names of its contributors may not be used to endorse or promote
27 // products derived from this software without specific prior written
28 // permission.
29 //
30 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 //
42 // Any feedback is very welcome.
43 // http://www.math.keio.ac.jp/matumoto/emt.html
44 // email: matumoto@math.keio.ac.jp
45 //
46 // Feedback about the C++ port should be sent to Jasper Bedaux,
47 // see http://www.bedaux.net/mtrand/ for e-mail address and info.
48 //
49 // Modifications by David Morse:
50 //
51 // 1) Made state variables non-static members of MTRand_int32,
52 // rather than static members.
53 //
54 // 2) Added a constructor to construct MTRand from an MTRand_int32,
55 // to allow use of original test, in which two generators shared
56 // state through shared static variables.
57 //
58 // 3) Removed private declarations of copy constructors and assigment,
59 // allowing use of the default versions.
60 
61 namespace Util
62 {
63 
71  {
72  public:
73 
77  MTRand_int32();
78 
82  MTRand_int32(unsigned long s);
83 
87  MTRand_int32(const unsigned long* array, int size);
88 
92  MTRand_int32(const MTRand_int32&);
93 
97  void operator=(const MTRand_int32&);
98 
105  virtual ~MTRand_int32() {} // destructor
106 
110  void seed(unsigned long);
111 
115  void seed(const unsigned long*, int size);
116 
120  unsigned long operator()();
121 
125  template <class Archive>
126  void serialize(Archive& ar, const unsigned int version);
127 
128  protected:
129 
130  unsigned long rand_int32(); // generate 32 bit random integer
131 
132  private:
133 
134  static const int n = 624, m = 397; // compile time constants
135 
136  // The state array and variable p were static members in the C++
137  // port by J. Bedaux. Made non-static by David Morse, Dec. 2011
138 
139  // state vector array
140  unsigned long state[n];
141 
142  // position in state array
143  int p;
144 
145  // true if init function is called
146  bool init;
147 
148  // Used by gen_state()
149  unsigned long twiddle(unsigned long, unsigned long);
150 
151  // Generate new state
152  void gen_state();
153 
154  };
155 
156  // Inline methods.
157 
158  inline
159  unsigned long MTRand_int32::twiddle(unsigned long u, unsigned long v)
160  {
161  return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1)
162  ^ ((v & 1UL) ? 0x9908B0DFUL : 0x0UL);
163  }
164 
165  /*
166  * Generate 32 bit random int, private method.
167  */
168  inline unsigned long MTRand_int32::rand_int32()
169  {
170  if (p == n) gen_state(); // new state vector needed
171  // gen_state() is split off to be non-inline, because it is only called
172  // once in every 624 calls and otherwise irand() would become too big
173  // to get inlined
174  unsigned long x = state[p++];
175  x ^= (x >> 11);
176  x ^= (x << 7) & 0x9D2C5680UL;
177  x ^= (x << 15) & 0xEFC60000UL;
178  return x ^ (x >> 18);
179  }
180 
181  /*
182  * Serialize to/from an archive.
183  */
184  template <class Archive>
185  void MTRand_int32::serialize(Archive& ar, const unsigned int version)
186  {
187  for (int i=0; i < n; ++i) {
188  ar & state[i];
189  }
190  ar & p;
191  ar & init;
192  }
193 
197  inline unsigned long MTRand_int32::operator()() { return rand_int32(); }
198 
202  class MTRand : public MTRand_int32
203  {
204  public:
205 
206  MTRand() : MTRand_int32() {}
207 
208  MTRand(unsigned long seed) : MTRand_int32(seed) {}
209 
210  MTRand(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
211 
212  MTRand(const MTRand_int32& other) : MTRand_int32(other) {}
213 
214  ~MTRand() {}
215 
216  double operator()()
217  { return static_cast<double>(rand_int32()) * (1. / 4294967296.); }
218  // divided by 2^32
219 
220  //private:
221 
222  //MTRand(const MTRand&); // copy constructor not defined
223  //void operator=(const MTRand&); // assignment operator not defined
224 
225  };
226 
231  {
232  public:
233 
234  MTRand_closed()
235  : MTRand_int32()
236  {}
237 
238  MTRand_closed(unsigned long seed)
239  : MTRand_int32(seed)
240  {}
241 
242  MTRand_closed(const unsigned long* seed, int size)
243  : MTRand_int32(seed, size)
244  {}
245 
246  ~MTRand_closed() {}
247 
248  double operator()()
249  {
250  return static_cast<double>(rand_int32()) * (1. / 4294967295.); }
251  // divided by 2^32 - 1
252 
253  //private:
254 
255  //MTRand_closed(const MTRand_closed&); // copy constructor not defined
256 
257  //void operator=(const MTRand_closed&); // assignment operator not defined
258  };
259 
263  class MTRand_open : public MTRand_int32
264  {
265 
266  public:
267 
268  MTRand_open()
269  : MTRand_int32()
270  {}
271 
272  MTRand_open(unsigned long seed)
273  : MTRand_int32(seed)
274  {}
275 
276  MTRand_open(const unsigned long* seed, int size)
277  : MTRand_int32(seed, size)
278  {}
279 
280  ~MTRand_open() {}
281 
282  double operator()()
283  {
284  return (static_cast<double>(rand_int32()) + .5)*(1./4294967296.);
285  // divided by 2^32
286  }
287 
288  //private:
289  //MTRand_open(const MTRand_open&); // copy constructor not defined
290  //void operator=(const MTRand_open&); // assignment operator not defined
291  };
292 
296  class MTRand53 : public MTRand_int32
297  {
298  public:
299 
300  MTRand53()
301  : MTRand_int32()
302  {}
303 
304  MTRand53(unsigned long seed)
305  : MTRand_int32(seed)
306  {}
307 
308  MTRand53(const unsigned long* seed, int size)
309  : MTRand_int32(seed, size)
310  {}
311 
312  ~MTRand53() {}
313  double operator()()
314  {
315  return (static_cast<double>(rand_int32() >> 5) * 67108864. +
316  static_cast<double>(rand_int32() >> 6)) * (1. / 9007199254740992.);
317  }
318 
319  //private:
320 
321  //MTRand53(const MTRand53&); // copy constructor not defined
322  //void operator=(const MTRand53&); // assignment operator not defined
323 
324  };
325 
326 }
327 #endif // MTRAND_H
void operator=(const MTRand_int32 &)
Assignment.
Definition: mtrand.cpp:61
virtual ~MTRand_int32()
Destructor.
Definition: mtrand.h:105
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
Definition: mtrand.h:185
Generates double floating point numbers in the closed interval [0, 1].
Definition: mtrand.h:230
Generates double floating point numbers in the open interval (0, 1).
Definition: mtrand.h:263
unsigned long operator()()
Overload operator() to make this a generator (functor)
Definition: mtrand.h:197
Generates double floating point numbers in the half-open interval [0, 1)
Definition: mtrand.h:202
void seed(unsigned long)
Seed with 32 bit integer.
Definition: mtrand.cpp:86
Mersenne Twister random number generator engine.
Definition: mtrand.h:70
MTRand_int32()
Default constructor.
Definition: mtrand.cpp:13
Utility classes for scientific computation.
Definition: accumulators.mod:1
generates 53 bit resolution doubles in the half-open interval [0, 1)
Definition: mtrand.h:296