Simpatico  v1.10
mtrand.cpp
1 // mtrand.cpp, see include file mtrand.h for information
2 
3 #include "mtrand.h"
4 
5 namespace Util
6 {
7 
8  /*
9  * Default constructor.
10  *
11  * Uses default seed only if this is the first instance
12  */
14  : p(0),
15  init(false)
16  {
17  for (int i=0; i< n; ++i) state[i] = 0x0UL;
18  if (!init) seed(5489UL);
19  init = true;
20  }
21 
22  /*
23  * Constructor with 32 bit int as seed.
24  */
25  MTRand_int32::MTRand_int32(unsigned long s)
26  : p(0),
27  init(false)
28  {
29  for (int i=0; i< n; ++i) state[i] = 0x0UL;
30  seed(s);
31  init = true;
32  }
33 
34  /*
35  * Constructor with array of size 32 bit ints as seed
36  */
37  MTRand_int32::MTRand_int32(const unsigned long* array, int size)
38  : p(0),
39  init(false)
40  {
41  for (int i=0; i< n; ++i) state[i] = 0x0UL;
42  seed(array, size);
43  init = true;
44  }
45 
46  /*
47  * Copy constructor.
48  */
50  : p(other.p),
51  init(other.init)
52  {
53  for (int i=0; i< n; ++i) {
54  state[i] = other.state[i];
55  }
56  }
57 
58  /*
59  * Assignment constructor.
60  */
62  {
63  p = other.p;
64  init = other.init;
65  for (int i=0; i< n; ++i) {
66  state[i] = other.state[i];
67  }
68  }
69 
70  /*
71  * Generate new state vector.
72  */
73  void MTRand_int32::gen_state()
74  {
75  for (int i = 0; i < (n - m); ++i)
76  state[i] = state[i + m] ^ twiddle(state[i], state[i + 1]);
77  for (int i = n - m; i < (n - 1); ++i)
78  state[i] = state[i + m - n] ^ twiddle(state[i], state[i + 1]);
79  state[n - 1] = state[m - 1] ^ twiddle(state[n - 1], state[0]);
80  p = 0; // reset position
81  }
82 
83  /*
84  * Init by 32 bit seed
85  */
86  void MTRand_int32::seed(unsigned long s) {
87  state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines
88  for (int i = 1; i < n; ++i) {
89  state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
90  // see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier
91  // in the previous versions, MSBs of the seed affect only MSBs of the array state
92  // 2002/01/09 modified by Makoto Matsumoto
93  state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
94  }
95  p = n; // force gen_state() to be called for next random number
96  }
97 
98  void MTRand_int32::seed(const unsigned long* array, int size) { // init by array
99  seed(19650218UL);
100  int i = 1, j = 0;
101  for (int k = ((n > size) ? n : size); k; --k) {
102  state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL))
103  + array[j] + j; // non linear
104  state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
105  ++j; j %= size;
106  if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
107  }
108  for (int k = n - 1; k; --k) {
109  state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i;
110  state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
111  if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
112  }
113  state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
114  p = n; // force gen_state() to be called for next random number
115  }
116 
117 }
void operator=(const MTRand_int32 &)
Assignment.
Definition: mtrand.cpp:61
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