Debian Codegraph @

terps/tads/tads3/vmmersenne.h

raw symbols SCIP
1 /* $Header$ */
2
3 /* Copyright (c) 2012 by Michael J. Roberts. All Rights Reserved. */
4 /*
5 Name
6 vmmersenne.h - Mersenne twister random number generator definitions
7 Function
8
9 Notes
10
11 Modified
12 02/28/12 MJRoberts - Creation
13 */
14
15 #ifndef VMMERSENNE_H
16 #define VMMERSENNE_H
17
18 #include "t3std.h"
19
20
21 /*
22 * Mersenne Twister MT19937 algorithm
23 */
24 class CVmMT19937
25 {
26 public:
27 CVmMT19937()
28 {
29 idx = 0;
30 }
31
32 /* seed from OS-generated random data */
33 void seed_random()
34 {
35 /* fill the mt[] array with random bytes */
36 os_gen_rand_bytes((unsigned char *)mt, sizeof(mt));
37 }
38
39 /*
40 * Seed from an integer value. This uses an LCG to extend the integer
41 * to fill the array. This follows the reference implementation's
42 * seeding mechanism, which lets us test against reference output.
43 */
44 void seed(uint32_t s)
45 {
46 /* do a pass through the mt array with LCG values from the seed */
47 base_seed(s);
48
49 /*
50 * discard a few initial values to prime the pump - the MT
51 * algorithm is considered slow at getting started (i.e., it needs
52 * to run through a few iterations before it starts generating good
53 * numbers)
54 */
55 for (int i = 0 ; i < 37 ; ++i)
56 rand();
57 }
58
59 /*
60 * Seed the generator from bytes. The seed can be any size; we'll use
61 * a combination of hashing and LCG generation to populate the internal
62 * state vector.
63 */
64 void seed(const char *buf, size_t len)
65 {
66 /*
67 * Use the mechanism from the reference implementation for seeding
68 * from multiple values. First do a fixed seeding of the mt array
69 * to fill it with nonzero bits.
70 */
71 base_seed(19650218);
72
73 /* treat the source as an array of int32s */
74 size_t icnt = (len + 3)/4;
75
76 /* if there's a partial int32 at the end, figure its value */
77 uint32_t partial = 0;
78 if (icnt*4 > len)
79 {
80 int mul = 1;
81 for (size_t i = (len/4)*4 ; i < len ; ++i, mul *= 256)
82 partial += buf[i]*mul;
83 }
84
85 /*
86 * do a pass over the mt array, doing the sort of LCG
87 * initialization we do with a single int seed, but folding each
88 * seed value into the running LCG feedback register
89 */
90 int i = 1, j = 0, k = NMT > icnt ? NMT : icnt;
91 for ( ; k != 0 ; --k)
92 {
93 /* get the current seed (the last one might be partial) */
94 uint32_t seed = (j+1)*4 <= (int)len ? osrp4(buf + j*4) : partial;
95
96 /* generate an LCG value mixing in this seed value */
97 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 0x19660D))
98 + seed + j;
99
100 /* on to the next mt index, wrapping at the end */
101 if (++i >= NMT)
102 {
103 mt[0] = mt[NMT-1];
104 i = 1;
105 }
106
107 /* on to the next seed index, wrapping at the end */
108 if (++j >= (int)icnt)
109 j = 0;
110 }
111
112 /* make another pass over the mt array */
113 for (k = NMT-1 ; k != 0 ; --k)
114 {
115 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 0x5D588B65)) - i;
116 if (++i >= NMT)
117 {
118 mt[0] = mt[NMT-1];
119 i = 1;
120 }
121 }
122
123 /* and finally, make sure the initial array isn't all zeros */
124 mt[0] = 0x80000000UL;
125
126 /* flush out the first full mt array */
127 idx = 0;
128 for (i = 0 ; i <= NMT ; ++i)
129 rand();
130 }
131
132 /* generate a random number in the full uint32_t range */
133 uint32_t rand()
134 {
135 /* if we're out of untempered numbers, generate the next set */
136 if (idx >= NMT)
137 {
138 generate_untempered();
139 idx = 0;
140 }
141
142 /* generate a tempered result */
143 uint32_t y = mt[idx];
144 y ^= (y >> 11);
145 y ^= ((y << 7) & 0x9d2c5680);
146 y ^= ((y << 15) & 0xefc60000);
147 y ^= (y >> 18);
148
149 /* on to the next index */
150 ++idx;
151
152 /* return the result */
153 return y;
154 }
155
156 /* get the size of the serialized state vector */
157 size_t get_state_size() const { return 2 + NMT*4; }
158
159 /* serialize the state into a byte buffer */
160 void get_state(char *buf)
161 {
162 oswp2(buf, (short)idx);
163 for (int i = 0, dst = 2 ; i < NMT ; ++i, dst += 4)
164 oswp4(buf + dst, mt[i]);
165 }
166
167 /* restore the state from a byte buffer */
168 void put_state(const char *buf)
169 {
170 idx = osrp2(buf) % NMT;
171 for (int i = 0, src = 2 ; i < NMT ; ++i, src += 4)
172 mt[i] = osrp4(buf + src);
173 }
174
175 protected:
176 /* basic seed generator */
177 void base_seed(uint32_t s)
178 {
179 mt[0] = s;
180 for (idx = 1 ; idx < NMT ; ++idx)
181 mt[idx] = 0x6c078965 * (mt[idx-1] ^ (mt[idx-1] >> 30)) + idx;
182 }
183
184 /* generate a new set of 624 untempered numbers */
185 void generate_untempered()
186 {
187 for (int i = 0 ; i < NMT ; ++i)
188 {
189 uint32_t y = (mt[i] & 0x80000000) + (mt[(i+1) % NMT] & 0x7fffffff);
190 mt[i] = mt[(i + 397) % NMT] ^ (y >> 1);
191 if ((y & 1) != 0)
192 mt[i] ^= 0x9908b0df;
193 }
194 }
195
196 /* internal state vector */
197 static const int NMT = 624;
198 uint32_t mt[NMT];
199
200 /* current element index */
201 int idx;
202 };
203
204
205 #endif /* VMMERSENNE_H */