Bridge++  Ver. 2.0.2
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.h
RandomNumbers_MT19937::write_file
void write_file(const std::string &)
Definition: randomNumbers_MT19937.cpp:106
RandomNumbers_MT19937::m_state
unsigned long m_state[N]
Definition: randomNumbers_MT19937.h:91
randomNumbers.h
RandomNumbers_MT19937
Definition: randomNumbers_MT19937.h:43
RandomNumbers_MT19937::m_left
int m_left
Definition: randomNumbers_MT19937.h:90
RandomNumbers_MT19937::init
void init(unsigned long s)
Definition: randomNumbers_MT19937.cpp:137
RandomNumbers
Base class of random number generators.
Definition: randomNumbers.h:43
RandomNumbers_MT19937::N
@ N
Definition: randomNumbers_MT19937.h:88
RandomNumbers_MT19937::RandomNumbers_MT19937
RandomNumbers_MT19937(int s)
Definition: randomNumbers_MT19937.cpp:27
RandomNumbers_MT19937::get
double get()
Definition: randomNumbers_MT19937.h:56
RandomNumbers_MT19937::randDouble1
double randDouble1() const
Definition: randomNumbers_MT19937.cpp:242
RandomNumbers_MT19937::randRes53
double randRes53() const
Definition: randomNumbers_MT19937.cpp:269
RandomNumbers_MT19937::read_file
void read_file(const std::string &)
save and load random number status.
Definition: randomNumbers_MT19937.cpp:67
RandomNumbers_MT19937::randDouble3
double randDouble3() const
Definition: randomNumbers_MT19937.cpp:260
RandomNumbers_MT19937::M
@ M
Definition: randomNumbers_MT19937.h:88
RandomNumbers_MT19937::nextState
void nextState() const
Definition: randomNumbers_MT19937.cpp:186
RandomNumbers_MT19937::randDouble2
double randDouble2() const
Definition: randomNumbers_MT19937.cpp:251
RandomNumbers_MT19937::randInt31
long randInt31() const
Definition: randomNumbers_MT19937.cpp:234
RandomNumbers_MT19937::randInt32
unsigned long randInt32() const
Definition: randomNumbers_MT19937.cpp:219
RandomNumbers_MT19937::class_name
static const std::string class_name
Definition: randomNumbers_MT19937.h:45
RandomNumbers_MT19937::~RandomNumbers_MT19937
~RandomNumbers_MT19937()
Definition: randomNumbers_MT19937.h:54
RandomNumbers_MT19937::twist
unsigned long twist(unsigned long u, unsigned long v) const
Definition: randomNumbers_MT19937.cpp:204
RandomNumbers_MT19937::m_next
unsigned long * m_next
Definition: randomNumbers_MT19937.h:92
RandomNumbers_MT19937::reset
void reset(unsigned long seed)
reset state with new seed.
Definition: randomNumbers_MT19937.cpp:130
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512