Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
randomNumbers_MT19937.h
Go to the documentation of this file.
1 
14 #ifndef RANDOMNUMBERS_MT19937_INCLUDED
15 #define RANDOMNUMBERS_MT19937_INCLUDED
16 
17 //==========================================================
18 // Random number generator.
19 // MT19937 random number generator.
20 // Original version in C was written by
21 // Takuji Nishimura and Makoto Matsumoto.
22 // See URL http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/mt19937ar.html
23 //
24 // 25 Dec 2004 H.Matsufuru
25 //==========================================================
26 
27 #include <cmath>
28 #include <string>
29 #include <vector>
30 #include <assert.h>
31 #include <iostream>
32 #include <fstream>
33 #include <vector>
34 using std::vector;
35 #include <string>
36 using std::string;
37 
38 #include "randomNumbers.h"
39 
40 #include "IO/bridgeIO.h"
41 using Bridge::vout;
42 
44 {
45  static const std::string class_name;
46 
47  public:
48  RandomNumbers_MT19937(int s);
49  //RandomNumbers_MT19937(unsigned long s = 5489UL);
50  RandomNumbers_MT19937(unsigned long s);
51  RandomNumbers_MT19937(std::vector<unsigned long>& key);
52  RandomNumbers_MT19937(const std::string& filename);
53 
55 
56  double get() { return randDouble2(); }
57 
58  void write_file(const std::string&);
59  void read_file(const std::string&);
60 
61  void reset(unsigned long seed);
62 
63  private:
64  void init(unsigned long s);
65  void init(unsigned long s, std::vector<unsigned long>& key);
66 
67  void nextState() const;
68  unsigned long twist(unsigned long u, unsigned long v) const;
69 
70  // return [0,0xffffffff]
71  unsigned long randInt32() const;
72 
73  // return [0,0x7fffffff]
74  long randInt31() const;
75 
76  // return [0,1] random number
77  double randDouble1() const;
78 
79  // return [0,1) random number
80  double randDouble2() const;
81 
82  // return (0,1) random number
83  double randDouble3() const;
84 
85  // return [0,1) random number with 53-bit resolution
86  double randRes53() const;
87 
88  enum { N=624, M=397 };
89 
90  mutable int m_left;
91  mutable unsigned long m_state[N];
92  mutable unsigned long *m_next;
93 
94 #ifdef USE_FACTORY
95  private:
96  static RandomNumbers *create_object_with_int(const int& iseed)
97  {
98  return new RandomNumbers_MT19937(iseed);
99  }
100 
101  static RandomNumbers *create_object_with_file(const std::string& filename)
102  {
103  return new RandomNumbers_MT19937(filename);
104  }
105 
106  public:
107  static bool register_factory()
108  {
109  bool init1 = RandomNumbers::Factory_int::Register("MT19937", create_object_with_int);
110  bool init2 = RandomNumbers::Factory_file::Register("MT19937", create_object_with_file);
111 
112  return init1 && init2;
113  }
114 #endif
115 };
116 #endif
BridgeIO vout
Definition: bridgeIO.cpp:503
static const std::string class_name
unsigned long twist(unsigned long u, unsigned long v) const
void init(unsigned long s)
void write_file(const std::string &)
unsigned long randInt32() const
void read_file(const std::string &)
save and load random number status.
void reset(unsigned long seed)
reset state with new seed.
Base class of random number generators.
Definition: randomNumbers.h:43