Chris@16
|
1 /* boost random/mersenne_twister.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2000-2001
|
Chris@16
|
4 * Copyright Steven Watanabe 2010
|
Chris@16
|
5 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 *
|
Chris@16
|
9 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
10 *
|
Chris@101
|
11 * $Id$
|
Chris@16
|
12 *
|
Chris@16
|
13 * Revision history
|
Chris@101
|
14 * 2013-10-14 fixed some warnings with Wshadow (mgaunard)
|
Chris@16
|
15 * 2001-02-18 moved to individual header files
|
Chris@16
|
16 */
|
Chris@16
|
17
|
Chris@16
|
18 #ifndef BOOST_RANDOM_MERSENNE_TWISTER_HPP
|
Chris@16
|
19 #define BOOST_RANDOM_MERSENNE_TWISTER_HPP
|
Chris@16
|
20
|
Chris@16
|
21 #include <iosfwd>
|
Chris@16
|
22 #include <istream>
|
Chris@16
|
23 #include <stdexcept>
|
Chris@16
|
24 #include <boost/config.hpp>
|
Chris@16
|
25 #include <boost/cstdint.hpp>
|
Chris@16
|
26 #include <boost/integer/integer_mask.hpp>
|
Chris@16
|
27 #include <boost/random/detail/config.hpp>
|
Chris@16
|
28 #include <boost/random/detail/ptr_helper.hpp>
|
Chris@16
|
29 #include <boost/random/detail/seed.hpp>
|
Chris@16
|
30 #include <boost/random/detail/seed_impl.hpp>
|
Chris@16
|
31 #include <boost/random/detail/generator_seed_seq.hpp>
|
Chris@101
|
32 #include <boost/random/detail/polynomial.hpp>
|
Chris@101
|
33
|
Chris@101
|
34 #include <boost/random/detail/disable_warnings.hpp>
|
Chris@16
|
35
|
Chris@16
|
36 namespace boost {
|
Chris@16
|
37 namespace random {
|
Chris@16
|
38
|
Chris@16
|
39 /**
|
Chris@16
|
40 * Instantiations of class template mersenne_twister_engine model a
|
Chris@16
|
41 * \pseudo_random_number_generator. It uses the algorithm described in
|
Chris@16
|
42 *
|
Chris@16
|
43 * @blockquote
|
Chris@16
|
44 * "Mersenne Twister: A 623-dimensionally equidistributed uniform
|
Chris@16
|
45 * pseudo-random number generator", Makoto Matsumoto and Takuji Nishimura,
|
Chris@16
|
46 * ACM Transactions on Modeling and Computer Simulation: Special Issue on
|
Chris@101
|
47 * Uniform Random Number Generation, Vol. 8, No. 1, January 1998, pp. 3-30.
|
Chris@16
|
48 * @endblockquote
|
Chris@16
|
49 *
|
Chris@16
|
50 * @xmlnote
|
Chris@16
|
51 * The boost variant has been implemented from scratch and does not
|
Chris@16
|
52 * derive from or use mt19937.c provided on the above WWW site. However, it
|
Chris@16
|
53 * was verified that both produce identical output.
|
Chris@16
|
54 * @endxmlnote
|
Chris@16
|
55 *
|
Chris@16
|
56 * The seeding from an integer was changed in April 2005 to address a
|
Chris@16
|
57 * <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html">weakness</a>.
|
Chris@101
|
58 *
|
Chris@16
|
59 * The quality of the generator crucially depends on the choice of the
|
Chris@16
|
60 * parameters. User code should employ one of the sensibly parameterized
|
Chris@16
|
61 * generators such as \mt19937 instead.
|
Chris@16
|
62 *
|
Chris@16
|
63 * The generator requires considerable amounts of memory for the storage of
|
Chris@16
|
64 * its state array. For example, \mt11213b requires about 1408 bytes and
|
Chris@16
|
65 * \mt19937 requires about 2496 bytes.
|
Chris@16
|
66 */
|
Chris@16
|
67 template<class UIntType,
|
Chris@16
|
68 std::size_t w, std::size_t n, std::size_t m, std::size_t r,
|
Chris@16
|
69 UIntType a, std::size_t u, UIntType d, std::size_t s,
|
Chris@16
|
70 UIntType b, std::size_t t,
|
Chris@16
|
71 UIntType c, std::size_t l, UIntType f>
|
Chris@16
|
72 class mersenne_twister_engine
|
Chris@16
|
73 {
|
Chris@16
|
74 public:
|
Chris@16
|
75 typedef UIntType result_type;
|
Chris@16
|
76 BOOST_STATIC_CONSTANT(std::size_t, word_size = w);
|
Chris@16
|
77 BOOST_STATIC_CONSTANT(std::size_t, state_size = n);
|
Chris@16
|
78 BOOST_STATIC_CONSTANT(std::size_t, shift_size = m);
|
Chris@16
|
79 BOOST_STATIC_CONSTANT(std::size_t, mask_bits = r);
|
Chris@16
|
80 BOOST_STATIC_CONSTANT(UIntType, xor_mask = a);
|
Chris@16
|
81 BOOST_STATIC_CONSTANT(std::size_t, tempering_u = u);
|
Chris@16
|
82 BOOST_STATIC_CONSTANT(UIntType, tempering_d = d);
|
Chris@16
|
83 BOOST_STATIC_CONSTANT(std::size_t, tempering_s = s);
|
Chris@16
|
84 BOOST_STATIC_CONSTANT(UIntType, tempering_b = b);
|
Chris@16
|
85 BOOST_STATIC_CONSTANT(std::size_t, tempering_t = t);
|
Chris@16
|
86 BOOST_STATIC_CONSTANT(UIntType, tempering_c = c);
|
Chris@16
|
87 BOOST_STATIC_CONSTANT(std::size_t, tempering_l = l);
|
Chris@16
|
88 BOOST_STATIC_CONSTANT(UIntType, initialization_multiplier = f);
|
Chris@16
|
89 BOOST_STATIC_CONSTANT(UIntType, default_seed = 5489u);
|
Chris@101
|
90
|
Chris@16
|
91 // backwards compatibility
|
Chris@16
|
92 BOOST_STATIC_CONSTANT(UIntType, parameter_a = a);
|
Chris@16
|
93 BOOST_STATIC_CONSTANT(std::size_t, output_u = u);
|
Chris@16
|
94 BOOST_STATIC_CONSTANT(std::size_t, output_s = s);
|
Chris@16
|
95 BOOST_STATIC_CONSTANT(UIntType, output_b = b);
|
Chris@16
|
96 BOOST_STATIC_CONSTANT(std::size_t, output_t = t);
|
Chris@16
|
97 BOOST_STATIC_CONSTANT(UIntType, output_c = c);
|
Chris@16
|
98 BOOST_STATIC_CONSTANT(std::size_t, output_l = l);
|
Chris@101
|
99
|
Chris@16
|
100 // old Boost.Random concept requirements
|
Chris@16
|
101 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
Chris@16
|
102
|
Chris@16
|
103
|
Chris@16
|
104 /**
|
Chris@16
|
105 * Constructs a @c mersenne_twister_engine and calls @c seed().
|
Chris@16
|
106 */
|
Chris@16
|
107 mersenne_twister_engine() { seed(); }
|
Chris@16
|
108
|
Chris@16
|
109 /**
|
Chris@16
|
110 * Constructs a @c mersenne_twister_engine and calls @c seed(value).
|
Chris@16
|
111 */
|
Chris@16
|
112 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(mersenne_twister_engine,
|
Chris@16
|
113 UIntType, value)
|
Chris@16
|
114 { seed(value); }
|
Chris@16
|
115 template<class It> mersenne_twister_engine(It& first, It last)
|
Chris@16
|
116 { seed(first,last); }
|
Chris@16
|
117
|
Chris@16
|
118 /**
|
Chris@16
|
119 * Constructs a mersenne_twister_engine and calls @c seed(gen).
|
Chris@16
|
120 *
|
Chris@16
|
121 * @xmlnote
|
Chris@16
|
122 * The copy constructor will always be preferred over
|
Chris@16
|
123 * the templated constructor.
|
Chris@16
|
124 * @endxmlnote
|
Chris@16
|
125 */
|
Chris@16
|
126 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(mersenne_twister_engine,
|
Chris@16
|
127 SeedSeq, seq)
|
Chris@16
|
128 { seed(seq); }
|
Chris@16
|
129
|
Chris@16
|
130 // compiler-generated copy ctor and assignment operator are fine
|
Chris@16
|
131
|
Chris@16
|
132 /** Calls @c seed(default_seed). */
|
Chris@16
|
133 void seed() { seed(default_seed); }
|
Chris@16
|
134
|
Chris@16
|
135 /**
|
Chris@16
|
136 * Sets the state x(0) to v mod 2w. Then, iteratively,
|
Chris@16
|
137 * sets x(i) to
|
Chris@16
|
138 * (i + f * (x(i-1) xor (x(i-1) rshift w-2))) mod 2<sup>w</sup>
|
Chris@16
|
139 * for i = 1 .. n-1. x(n) is the first value to be returned by operator().
|
Chris@16
|
140 */
|
Chris@16
|
141 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(mersenne_twister_engine, UIntType, value)
|
Chris@16
|
142 {
|
Chris@101
|
143 // New seeding algorithm from
|
Chris@16
|
144 // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
|
Chris@16
|
145 // In the previous versions, MSBs of the seed affected only MSBs of the
|
Chris@16
|
146 // state x[].
|
Chris@16
|
147 const UIntType mask = (max)();
|
Chris@16
|
148 x[0] = value & mask;
|
Chris@16
|
149 for (i = 1; i < n; i++) {
|
Chris@16
|
150 // See Knuth "The Art of Computer Programming"
|
Chris@16
|
151 // Vol. 2, 3rd ed., page 106
|
Chris@16
|
152 x[i] = (f * (x[i-1] ^ (x[i-1] >> (w-2))) + i) & mask;
|
Chris@16
|
153 }
|
Chris@101
|
154
|
Chris@101
|
155 normalize_state();
|
Chris@16
|
156 }
|
Chris@101
|
157
|
Chris@16
|
158 /**
|
Chris@16
|
159 * Seeds a mersenne_twister_engine using values produced by seq.generate().
|
Chris@16
|
160 */
|
Chris@16
|
161 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(mersenne_twister_engine, SeeqSeq, seq)
|
Chris@16
|
162 {
|
Chris@16
|
163 detail::seed_array_int<w>(seq, x);
|
Chris@16
|
164 i = n;
|
Chris@16
|
165
|
Chris@101
|
166 normalize_state();
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 /** Sets the state of the generator using values from an iterator range. */
|
Chris@16
|
170 template<class It>
|
Chris@16
|
171 void seed(It& first, It last)
|
Chris@16
|
172 {
|
Chris@16
|
173 detail::fill_array_int<w>(first, last, x);
|
Chris@16
|
174 i = n;
|
Chris@16
|
175
|
Chris@101
|
176 normalize_state();
|
Chris@16
|
177 }
|
Chris@101
|
178
|
Chris@16
|
179 /** Returns the smallest value that the generator can produce. */
|
Chris@16
|
180 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
181 { return 0; }
|
Chris@16
|
182 /** Returns the largest value that the generator can produce. */
|
Chris@16
|
183 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
184 { return boost::low_bits_mask_t<w>::sig_bits; }
|
Chris@101
|
185
|
Chris@16
|
186 /** Produces the next value of the generator. */
|
Chris@16
|
187 result_type operator()();
|
Chris@16
|
188
|
Chris@16
|
189 /** Fills a range with random values */
|
Chris@16
|
190 template<class Iter>
|
Chris@16
|
191 void generate(Iter first, Iter last)
|
Chris@16
|
192 { detail::generate_from_int(*this, first, last); }
|
Chris@16
|
193
|
Chris@16
|
194 /**
|
Chris@16
|
195 * Advances the state of the generator by @c z steps. Equivalent to
|
Chris@16
|
196 *
|
Chris@16
|
197 * @code
|
Chris@16
|
198 * for(unsigned long long i = 0; i < z; ++i) {
|
Chris@16
|
199 * gen();
|
Chris@16
|
200 * }
|
Chris@16
|
201 * @endcode
|
Chris@16
|
202 */
|
Chris@16
|
203 void discard(boost::uintmax_t z)
|
Chris@16
|
204 {
|
Chris@101
|
205 #ifndef BOOST_RANDOM_MERSENNE_TWISTER_DISCARD_THRESHOLD
|
Chris@101
|
206 #define BOOST_RANDOM_MERSENNE_TWISTER_DISCARD_THRESHOLD 10000000
|
Chris@101
|
207 #endif
|
Chris@101
|
208 if(z > BOOST_RANDOM_MERSENNE_TWISTER_DISCARD_THRESHOLD) {
|
Chris@101
|
209 discard_many(z);
|
Chris@101
|
210 } else {
|
Chris@101
|
211 for(boost::uintmax_t j = 0; j < z; ++j) {
|
Chris@101
|
212 (*this)();
|
Chris@101
|
213 }
|
Chris@16
|
214 }
|
Chris@16
|
215 }
|
Chris@16
|
216
|
Chris@16
|
217 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
|
Chris@16
|
218 /** Writes a mersenne_twister_engine to a @c std::ostream */
|
Chris@16
|
219 template<class CharT, class Traits>
|
Chris@16
|
220 friend std::basic_ostream<CharT,Traits>&
|
Chris@16
|
221 operator<<(std::basic_ostream<CharT,Traits>& os,
|
Chris@16
|
222 const mersenne_twister_engine& mt)
|
Chris@16
|
223 {
|
Chris@16
|
224 mt.print(os);
|
Chris@16
|
225 return os;
|
Chris@16
|
226 }
|
Chris@101
|
227
|
Chris@16
|
228 /** Reads a mersenne_twister_engine from a @c std::istream */
|
Chris@16
|
229 template<class CharT, class Traits>
|
Chris@16
|
230 friend std::basic_istream<CharT,Traits>&
|
Chris@16
|
231 operator>>(std::basic_istream<CharT,Traits>& is,
|
Chris@16
|
232 mersenne_twister_engine& mt)
|
Chris@16
|
233 {
|
Chris@16
|
234 for(std::size_t j = 0; j < mt.state_size; ++j)
|
Chris@16
|
235 is >> mt.x[j] >> std::ws;
|
Chris@16
|
236 // MSVC (up to 7.1) and Borland (up to 5.64) don't handle the template
|
Chris@16
|
237 // value parameter "n" available from the class template scope, so use
|
Chris@16
|
238 // the static constant with the same value
|
Chris@16
|
239 mt.i = mt.state_size;
|
Chris@16
|
240 return is;
|
Chris@16
|
241 }
|
Chris@16
|
242 #endif
|
Chris@16
|
243
|
Chris@16
|
244 /**
|
Chris@16
|
245 * Returns true if the two generators are in the same state,
|
Chris@16
|
246 * and will thus produce identical sequences.
|
Chris@16
|
247 */
|
Chris@101
|
248 friend bool operator==(const mersenne_twister_engine& x_,
|
Chris@101
|
249 const mersenne_twister_engine& y_)
|
Chris@16
|
250 {
|
Chris@101
|
251 if(x_.i < y_.i) return x_.equal_imp(y_);
|
Chris@101
|
252 else return y_.equal_imp(x_);
|
Chris@16
|
253 }
|
Chris@101
|
254
|
Chris@16
|
255 /**
|
Chris@16
|
256 * Returns true if the two generators are in different states.
|
Chris@16
|
257 */
|
Chris@101
|
258 friend bool operator!=(const mersenne_twister_engine& x_,
|
Chris@101
|
259 const mersenne_twister_engine& y_)
|
Chris@101
|
260 { return !(x_ == y_); }
|
Chris@16
|
261
|
Chris@16
|
262 private:
|
Chris@16
|
263 /// \cond show_private
|
Chris@16
|
264
|
Chris@16
|
265 void twist();
|
Chris@16
|
266
|
Chris@16
|
267 /**
|
Chris@16
|
268 * Does the work of operator==. This is in a member function
|
Chris@16
|
269 * for portability. Some compilers, such as msvc 7.1 and
|
Chris@16
|
270 * Sun CC 5.10 can't access template parameters or static
|
Chris@16
|
271 * members of the class from inline friend functions.
|
Chris@16
|
272 *
|
Chris@16
|
273 * requires i <= other.i
|
Chris@16
|
274 */
|
Chris@16
|
275 bool equal_imp(const mersenne_twister_engine& other) const
|
Chris@16
|
276 {
|
Chris@16
|
277 UIntType back[n];
|
Chris@16
|
278 std::size_t offset = other.i - i;
|
Chris@16
|
279 for(std::size_t j = 0; j + offset < n; ++j)
|
Chris@16
|
280 if(x[j] != other.x[j+offset])
|
Chris@16
|
281 return false;
|
Chris@16
|
282 rewind(&back[n-1], offset);
|
Chris@16
|
283 for(std::size_t j = 0; j < offset; ++j)
|
Chris@16
|
284 if(back[j + n - offset] != other.x[j])
|
Chris@16
|
285 return false;
|
Chris@16
|
286 return true;
|
Chris@16
|
287 }
|
Chris@16
|
288
|
Chris@16
|
289 /**
|
Chris@16
|
290 * Does the work of operator<<. This is in a member function
|
Chris@16
|
291 * for portability.
|
Chris@16
|
292 */
|
Chris@16
|
293 template<class CharT, class Traits>
|
Chris@16
|
294 void print(std::basic_ostream<CharT, Traits>& os) const
|
Chris@16
|
295 {
|
Chris@16
|
296 UIntType data[n];
|
Chris@16
|
297 for(std::size_t j = 0; j < i; ++j) {
|
Chris@16
|
298 data[j + n - i] = x[j];
|
Chris@16
|
299 }
|
Chris@16
|
300 if(i != n) {
|
Chris@16
|
301 rewind(&data[n - i - 1], n - i);
|
Chris@16
|
302 }
|
Chris@16
|
303 os << data[0];
|
Chris@16
|
304 for(std::size_t j = 1; j < n; ++j) {
|
Chris@16
|
305 os << ' ' << data[j];
|
Chris@16
|
306 }
|
Chris@16
|
307 }
|
Chris@16
|
308
|
Chris@16
|
309 /**
|
Chris@16
|
310 * Copies z elements of the state preceding x[0] into
|
Chris@16
|
311 * the array whose last element is last.
|
Chris@16
|
312 */
|
Chris@16
|
313 void rewind(UIntType* last, std::size_t z) const
|
Chris@16
|
314 {
|
Chris@16
|
315 const UIntType upper_mask = (~static_cast<UIntType>(0)) << r;
|
Chris@16
|
316 const UIntType lower_mask = ~upper_mask;
|
Chris@16
|
317 UIntType y0 = x[m-1] ^ x[n-1];
|
Chris@16
|
318 if(y0 & (static_cast<UIntType>(1) << (w-1))) {
|
Chris@16
|
319 y0 = ((y0 ^ a) << 1) | 1;
|
Chris@16
|
320 } else {
|
Chris@16
|
321 y0 = y0 << 1;
|
Chris@16
|
322 }
|
Chris@16
|
323 for(std::size_t sz = 0; sz < z; ++sz) {
|
Chris@16
|
324 UIntType y1 =
|
Chris@16
|
325 rewind_find(last, sz, m-1) ^ rewind_find(last, sz, n-1);
|
Chris@16
|
326 if(y1 & (static_cast<UIntType>(1) << (w-1))) {
|
Chris@16
|
327 y1 = ((y1 ^ a) << 1) | 1;
|
Chris@16
|
328 } else {
|
Chris@16
|
329 y1 = y1 << 1;
|
Chris@16
|
330 }
|
Chris@16
|
331 *(last - sz) = (y0 & upper_mask) | (y1 & lower_mask);
|
Chris@16
|
332 y0 = y1;
|
Chris@16
|
333 }
|
Chris@16
|
334 }
|
Chris@16
|
335
|
Chris@16
|
336 /**
|
Chris@101
|
337 * Converts an arbitrary array into a valid generator state.
|
Chris@101
|
338 * First we normalize x[0], so that it contains the same
|
Chris@101
|
339 * value we would get by running the generator forwards
|
Chris@101
|
340 * and then in reverse. (The low order r bits are redundant).
|
Chris@101
|
341 * Then, if the state consists of all zeros, we set the
|
Chris@101
|
342 * high order bit of x[0] to 1. This function only needs to
|
Chris@101
|
343 * be called by seed, since the state transform preserves
|
Chris@101
|
344 * this relationship.
|
Chris@101
|
345 */
|
Chris@101
|
346 void normalize_state()
|
Chris@101
|
347 {
|
Chris@101
|
348 const UIntType upper_mask = (~static_cast<UIntType>(0)) << r;
|
Chris@101
|
349 const UIntType lower_mask = ~upper_mask;
|
Chris@101
|
350 UIntType y0 = x[m-1] ^ x[n-1];
|
Chris@101
|
351 if(y0 & (static_cast<UIntType>(1) << (w-1))) {
|
Chris@101
|
352 y0 = ((y0 ^ a) << 1) | 1;
|
Chris@101
|
353 } else {
|
Chris@101
|
354 y0 = y0 << 1;
|
Chris@101
|
355 }
|
Chris@101
|
356 x[0] = (x[0] & upper_mask) | (y0 & lower_mask);
|
Chris@101
|
357
|
Chris@101
|
358 // fix up the state if it's all zeroes.
|
Chris@101
|
359 for(std::size_t j = 0; j < n; ++j) {
|
Chris@101
|
360 if(x[j] != 0) return;
|
Chris@101
|
361 }
|
Chris@101
|
362 x[0] = static_cast<UIntType>(1) << (w-1);
|
Chris@101
|
363 }
|
Chris@101
|
364
|
Chris@101
|
365 /**
|
Chris@16
|
366 * Given a pointer to the last element of the rewind array,
|
Chris@16
|
367 * and the current size of the rewind array, finds an element
|
Chris@16
|
368 * relative to the next available slot in the rewind array.
|
Chris@16
|
369 */
|
Chris@16
|
370 UIntType
|
Chris@16
|
371 rewind_find(UIntType* last, std::size_t size, std::size_t j) const
|
Chris@16
|
372 {
|
Chris@16
|
373 std::size_t index = (j + n - size + n - 1) % n;
|
Chris@16
|
374 if(index < n - size) {
|
Chris@16
|
375 return x[index];
|
Chris@16
|
376 } else {
|
Chris@16
|
377 return *(last - (n - 1 - index));
|
Chris@16
|
378 }
|
Chris@16
|
379 }
|
Chris@16
|
380
|
Chris@101
|
381 /**
|
Chris@101
|
382 * Optimized algorithm for large jumps.
|
Chris@101
|
383 *
|
Chris@101
|
384 * Hiroshi Haramoto, Makoto Matsumoto, and Pierre L'Ecuyer. 2008.
|
Chris@101
|
385 * A Fast Jump Ahead Algorithm for Linear Recurrences in a Polynomial
|
Chris@101
|
386 * Space. In Proceedings of the 5th international conference on
|
Chris@101
|
387 * Sequences and Their Applications (SETA '08).
|
Chris@101
|
388 * DOI=10.1007/978-3-540-85912-3_26
|
Chris@101
|
389 */
|
Chris@101
|
390 void discard_many(boost::uintmax_t z)
|
Chris@101
|
391 {
|
Chris@101
|
392 // Compute the minimal polynomial, phi(t)
|
Chris@101
|
393 // This depends only on the transition function,
|
Chris@101
|
394 // which is constant. The characteristic
|
Chris@101
|
395 // polynomial is the same as the minimal
|
Chris@101
|
396 // polynomial for a maximum period generator
|
Chris@101
|
397 // (which should be all specializations of
|
Chris@101
|
398 // mersenne_twister.) Even if it weren't,
|
Chris@101
|
399 // the characteristic polynomial is guaranteed
|
Chris@101
|
400 // to be a multiple of the minimal polynomial,
|
Chris@101
|
401 // which is good enough.
|
Chris@101
|
402 detail::polynomial phi = get_characteristic_polynomial();
|
Chris@101
|
403
|
Chris@101
|
404 // calculate g(t) = t^z % phi(t)
|
Chris@101
|
405 detail::polynomial g = mod_pow_x(z, phi);
|
Chris@101
|
406
|
Chris@101
|
407 // h(s_0, t) = \sum_{i=0}^{2k-1}o(s_i)t^{2k-i-1}
|
Chris@101
|
408 detail::polynomial h;
|
Chris@101
|
409 const std::size_t num_bits = w*n - r;
|
Chris@101
|
410 for(std::size_t j = 0; j < num_bits * 2; ++j) {
|
Chris@101
|
411 // Yes, we're advancing the generator state
|
Chris@101
|
412 // here, but it doesn't matter because
|
Chris@101
|
413 // we're going to overwrite it completely
|
Chris@101
|
414 // in reconstruct_state.
|
Chris@101
|
415 if(i >= n) twist();
|
Chris@101
|
416 h[2*num_bits - j - 1] = x[i++] & UIntType(1);
|
Chris@101
|
417 }
|
Chris@101
|
418 // g(t)h(s_0, t)
|
Chris@101
|
419 detail::polynomial gh = g * h;
|
Chris@101
|
420 detail::polynomial result;
|
Chris@101
|
421 for(std::size_t j = 0; j <= num_bits; ++j) {
|
Chris@101
|
422 result[j] = gh[2*num_bits - j - 1];
|
Chris@101
|
423 }
|
Chris@101
|
424 reconstruct_state(result);
|
Chris@101
|
425 }
|
Chris@101
|
426 static detail::polynomial get_characteristic_polynomial()
|
Chris@101
|
427 {
|
Chris@101
|
428 const std::size_t num_bits = w*n - r;
|
Chris@101
|
429 detail::polynomial helper;
|
Chris@101
|
430 helper[num_bits - 1] = 1;
|
Chris@101
|
431 mersenne_twister_engine tmp;
|
Chris@101
|
432 tmp.reconstruct_state(helper);
|
Chris@101
|
433 // Skip the first num_bits elements, since we
|
Chris@101
|
434 // already know what they are.
|
Chris@101
|
435 for(std::size_t j = 0; j < num_bits; ++j) {
|
Chris@101
|
436 if(tmp.i >= n) tmp.twist();
|
Chris@101
|
437 if(j == num_bits - 1)
|
Chris@101
|
438 assert((tmp.x[tmp.i] & 1) == 1);
|
Chris@101
|
439 else
|
Chris@101
|
440 assert((tmp.x[tmp.i] & 1) == 0);
|
Chris@101
|
441 ++tmp.i;
|
Chris@101
|
442 }
|
Chris@101
|
443 detail::polynomial phi;
|
Chris@101
|
444 phi[num_bits] = 1;
|
Chris@101
|
445 detail::polynomial next_bits = tmp.as_polynomial(num_bits);
|
Chris@101
|
446 for(std::size_t j = 0; j < num_bits; ++j) {
|
Chris@101
|
447 int val = next_bits[j] ^ phi[num_bits-j-1];
|
Chris@101
|
448 phi[num_bits-j-1] = val;
|
Chris@101
|
449 if(val) {
|
Chris@101
|
450 for(std::size_t k = j + 1; k < num_bits; ++k) {
|
Chris@101
|
451 phi[num_bits-k-1] ^= next_bits[k-j-1];
|
Chris@101
|
452 }
|
Chris@101
|
453 }
|
Chris@101
|
454 }
|
Chris@101
|
455 return phi;
|
Chris@101
|
456 }
|
Chris@101
|
457 detail::polynomial as_polynomial(std::size_t size) {
|
Chris@101
|
458 detail::polynomial result;
|
Chris@101
|
459 for(std::size_t j = 0; j < size; ++j) {
|
Chris@101
|
460 if(i >= n) twist();
|
Chris@101
|
461 result[j] = x[i++] & UIntType(1);
|
Chris@101
|
462 }
|
Chris@101
|
463 return result;
|
Chris@101
|
464 }
|
Chris@101
|
465 void reconstruct_state(const detail::polynomial& p)
|
Chris@101
|
466 {
|
Chris@101
|
467 const UIntType upper_mask = (~static_cast<UIntType>(0)) << r;
|
Chris@101
|
468 const UIntType lower_mask = ~upper_mask;
|
Chris@101
|
469 const std::size_t num_bits = w*n - r;
|
Chris@101
|
470 for(std::size_t j = num_bits - n + 1; j <= num_bits; ++j)
|
Chris@101
|
471 x[j % n] = p[j];
|
Chris@101
|
472
|
Chris@101
|
473 UIntType y0 = 0;
|
Chris@101
|
474 for(std::size_t j = num_bits + 1; j >= n - 1; --j) {
|
Chris@101
|
475 UIntType y1 = x[j % n] ^ x[(j + m) % n];
|
Chris@101
|
476 if(p[j - n + 1])
|
Chris@101
|
477 y1 = (y1 ^ a) << UIntType(1) | UIntType(1);
|
Chris@101
|
478 else
|
Chris@101
|
479 y1 = y1 << UIntType(1);
|
Chris@101
|
480 x[(j + 1) % n] = (y0 & upper_mask) | (y1 & lower_mask);
|
Chris@101
|
481 y0 = y1;
|
Chris@101
|
482 }
|
Chris@101
|
483 i = 0;
|
Chris@101
|
484 }
|
Chris@101
|
485
|
Chris@16
|
486 /// \endcond
|
Chris@16
|
487
|
Chris@16
|
488 // state representation: next output is o(x(i))
|
Chris@16
|
489 // x[0] ... x[k] x[k+1] ... x[n-1] represents
|
Chris@16
|
490 // x(i-k) ... x(i) x(i+1) ... x(i-k+n-1)
|
Chris@16
|
491
|
Chris@101
|
492 UIntType x[n];
|
Chris@16
|
493 std::size_t i;
|
Chris@16
|
494 };
|
Chris@16
|
495
|
Chris@16
|
496 /// \cond show_private
|
Chris@16
|
497
|
Chris@16
|
498 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
Chris@16
|
499 // A definition is required even for integral static constants
|
Chris@16
|
500 #define BOOST_RANDOM_MT_DEFINE_CONSTANT(type, name) \
|
Chris@16
|
501 template<class UIntType, std::size_t w, std::size_t n, std::size_t m, \
|
Chris@16
|
502 std::size_t r, UIntType a, std::size_t u, UIntType d, std::size_t s, \
|
Chris@16
|
503 UIntType b, std::size_t t, UIntType c, std::size_t l, UIntType f> \
|
Chris@16
|
504 const type mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::name
|
Chris@16
|
505 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, word_size);
|
Chris@16
|
506 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, state_size);
|
Chris@16
|
507 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, shift_size);
|
Chris@16
|
508 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, mask_bits);
|
Chris@16
|
509 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, xor_mask);
|
Chris@16
|
510 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_u);
|
Chris@16
|
511 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, tempering_d);
|
Chris@16
|
512 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_s);
|
Chris@16
|
513 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, tempering_b);
|
Chris@16
|
514 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_t);
|
Chris@16
|
515 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, tempering_c);
|
Chris@16
|
516 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_l);
|
Chris@16
|
517 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, initialization_multiplier);
|
Chris@16
|
518 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, default_seed);
|
Chris@16
|
519 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, parameter_a);
|
Chris@16
|
520 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_u );
|
Chris@16
|
521 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_s);
|
Chris@16
|
522 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, output_b);
|
Chris@16
|
523 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_t);
|
Chris@16
|
524 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, output_c);
|
Chris@16
|
525 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_l);
|
Chris@16
|
526 BOOST_RANDOM_MT_DEFINE_CONSTANT(bool, has_fixed_range);
|
Chris@16
|
527 #undef BOOST_RANDOM_MT_DEFINE_CONSTANT
|
Chris@16
|
528 #endif
|
Chris@16
|
529
|
Chris@16
|
530 template<class UIntType,
|
Chris@16
|
531 std::size_t w, std::size_t n, std::size_t m, std::size_t r,
|
Chris@16
|
532 UIntType a, std::size_t u, UIntType d, std::size_t s,
|
Chris@16
|
533 UIntType b, std::size_t t,
|
Chris@16
|
534 UIntType c, std::size_t l, UIntType f>
|
Chris@16
|
535 void
|
Chris@16
|
536 mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::twist()
|
Chris@16
|
537 {
|
Chris@16
|
538 const UIntType upper_mask = (~static_cast<UIntType>(0)) << r;
|
Chris@16
|
539 const UIntType lower_mask = ~upper_mask;
|
Chris@16
|
540
|
Chris@16
|
541 const std::size_t unroll_factor = 6;
|
Chris@16
|
542 const std::size_t unroll_extra1 = (n-m) % unroll_factor;
|
Chris@16
|
543 const std::size_t unroll_extra2 = (m-1) % unroll_factor;
|
Chris@16
|
544
|
Chris@16
|
545 // split loop to avoid costly modulo operations
|
Chris@16
|
546 { // extra scope for MSVC brokenness w.r.t. for scope
|
Chris@16
|
547 for(std::size_t j = 0; j < n-m-unroll_extra1; j++) {
|
Chris@16
|
548 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
|
Chris@16
|
549 x[j] = x[j+m] ^ (y >> 1) ^ ((x[j+1]&1) * a);
|
Chris@16
|
550 }
|
Chris@16
|
551 }
|
Chris@16
|
552 {
|
Chris@16
|
553 for(std::size_t j = n-m-unroll_extra1; j < n-m; j++) {
|
Chris@16
|
554 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
|
Chris@16
|
555 x[j] = x[j+m] ^ (y >> 1) ^ ((x[j+1]&1) * a);
|
Chris@16
|
556 }
|
Chris@16
|
557 }
|
Chris@16
|
558 {
|
Chris@16
|
559 for(std::size_t j = n-m; j < n-1-unroll_extra2; j++) {
|
Chris@16
|
560 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
|
Chris@16
|
561 x[j] = x[j-(n-m)] ^ (y >> 1) ^ ((x[j+1]&1) * a);
|
Chris@16
|
562 }
|
Chris@16
|
563 }
|
Chris@16
|
564 {
|
Chris@16
|
565 for(std::size_t j = n-1-unroll_extra2; j < n-1; j++) {
|
Chris@16
|
566 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
|
Chris@16
|
567 x[j] = x[j-(n-m)] ^ (y >> 1) ^ ((x[j+1]&1) * a);
|
Chris@16
|
568 }
|
Chris@16
|
569 }
|
Chris@16
|
570 // last iteration
|
Chris@16
|
571 UIntType y = (x[n-1] & upper_mask) | (x[0] & lower_mask);
|
Chris@16
|
572 x[n-1] = x[m-1] ^ (y >> 1) ^ ((x[0]&1) * a);
|
Chris@16
|
573 i = 0;
|
Chris@16
|
574 }
|
Chris@16
|
575 /// \endcond
|
Chris@16
|
576
|
Chris@16
|
577 template<class UIntType,
|
Chris@16
|
578 std::size_t w, std::size_t n, std::size_t m, std::size_t r,
|
Chris@16
|
579 UIntType a, std::size_t u, UIntType d, std::size_t s,
|
Chris@16
|
580 UIntType b, std::size_t t,
|
Chris@16
|
581 UIntType c, std::size_t l, UIntType f>
|
Chris@16
|
582 inline typename
|
Chris@16
|
583 mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::result_type
|
Chris@16
|
584 mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::operator()()
|
Chris@16
|
585 {
|
Chris@16
|
586 if(i == n)
|
Chris@16
|
587 twist();
|
Chris@16
|
588 // Step 4
|
Chris@16
|
589 UIntType z = x[i];
|
Chris@16
|
590 ++i;
|
Chris@16
|
591 z ^= ((z >> u) & d);
|
Chris@16
|
592 z ^= ((z << s) & b);
|
Chris@16
|
593 z ^= ((z << t) & c);
|
Chris@16
|
594 z ^= (z >> l);
|
Chris@16
|
595 return z;
|
Chris@16
|
596 }
|
Chris@16
|
597
|
Chris@16
|
598 /**
|
Chris@16
|
599 * The specializations \mt11213b and \mt19937 are from
|
Chris@16
|
600 *
|
Chris@16
|
601 * @blockquote
|
Chris@16
|
602 * "Mersenne Twister: A 623-dimensionally equidistributed
|
Chris@16
|
603 * uniform pseudo-random number generator", Makoto Matsumoto
|
Chris@16
|
604 * and Takuji Nishimura, ACM Transactions on Modeling and
|
Chris@16
|
605 * Computer Simulation: Special Issue on Uniform Random Number
|
Chris@101
|
606 * Generation, Vol. 8, No. 1, January 1998, pp. 3-30.
|
Chris@16
|
607 * @endblockquote
|
Chris@16
|
608 */
|
Chris@16
|
609 typedef mersenne_twister_engine<uint32_t,32,351,175,19,0xccab8ee7,
|
Chris@16
|
610 11,0xffffffff,7,0x31b6ab00,15,0xffe50000,17,1812433253> mt11213b;
|
Chris@16
|
611
|
Chris@16
|
612 /**
|
Chris@16
|
613 * The specializations \mt11213b and \mt19937 are from
|
Chris@16
|
614 *
|
Chris@16
|
615 * @blockquote
|
Chris@16
|
616 * "Mersenne Twister: A 623-dimensionally equidistributed
|
Chris@16
|
617 * uniform pseudo-random number generator", Makoto Matsumoto
|
Chris@16
|
618 * and Takuji Nishimura, ACM Transactions on Modeling and
|
Chris@16
|
619 * Computer Simulation: Special Issue on Uniform Random Number
|
Chris@101
|
620 * Generation, Vol. 8, No. 1, January 1998, pp. 3-30.
|
Chris@16
|
621 * @endblockquote
|
Chris@16
|
622 */
|
Chris@16
|
623 typedef mersenne_twister_engine<uint32_t,32,624,397,31,0x9908b0df,
|
Chris@16
|
624 11,0xffffffff,7,0x9d2c5680,15,0xefc60000,18,1812433253> mt19937;
|
Chris@16
|
625
|
Chris@16
|
626 #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
|
Chris@16
|
627 typedef mersenne_twister_engine<uint64_t,64,312,156,31,
|
Chris@16
|
628 UINT64_C(0xb5026f5aa96619e9),29,UINT64_C(0x5555555555555555),17,
|
Chris@16
|
629 UINT64_C(0x71d67fffeda60000),37,UINT64_C(0xfff7eee000000000),43,
|
Chris@16
|
630 UINT64_C(6364136223846793005)> mt19937_64;
|
Chris@16
|
631 #endif
|
Chris@16
|
632
|
Chris@16
|
633 /// \cond show_deprecated
|
Chris@16
|
634
|
Chris@16
|
635 template<class UIntType,
|
Chris@16
|
636 int w, int n, int m, int r,
|
Chris@16
|
637 UIntType a, int u, std::size_t s,
|
Chris@16
|
638 UIntType b, int t,
|
Chris@16
|
639 UIntType c, int l, UIntType v>
|
Chris@16
|
640 class mersenne_twister :
|
Chris@16
|
641 public mersenne_twister_engine<UIntType,
|
Chris@16
|
642 w, n, m, r, a, u, ~(UIntType)0, s, b, t, c, l, 1812433253>
|
Chris@16
|
643 {
|
Chris@16
|
644 typedef mersenne_twister_engine<UIntType,
|
Chris@16
|
645 w, n, m, r, a, u, ~(UIntType)0, s, b, t, c, l, 1812433253> base_type;
|
Chris@16
|
646 public:
|
Chris@16
|
647 mersenne_twister() {}
|
Chris@16
|
648 BOOST_RANDOM_DETAIL_GENERATOR_CONSTRUCTOR(mersenne_twister, Gen, gen)
|
Chris@16
|
649 { seed(gen); }
|
Chris@16
|
650 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(mersenne_twister, UIntType, val)
|
Chris@16
|
651 { seed(val); }
|
Chris@16
|
652 template<class It>
|
Chris@16
|
653 mersenne_twister(It& first, It last) : base_type(first, last) {}
|
Chris@16
|
654 void seed() { base_type::seed(); }
|
Chris@16
|
655 BOOST_RANDOM_DETAIL_GENERATOR_SEED(mersenne_twister, Gen, gen)
|
Chris@16
|
656 {
|
Chris@16
|
657 detail::generator_seed_seq<Gen> seq(gen);
|
Chris@16
|
658 base_type::seed(seq);
|
Chris@16
|
659 }
|
Chris@16
|
660 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(mersenne_twister, UIntType, val)
|
Chris@16
|
661 { base_type::seed(val); }
|
Chris@16
|
662 template<class It>
|
Chris@16
|
663 void seed(It& first, It last) { base_type::seed(first, last); }
|
Chris@16
|
664 };
|
Chris@16
|
665
|
Chris@16
|
666 /// \endcond
|
Chris@16
|
667
|
Chris@16
|
668 } // namespace random
|
Chris@16
|
669
|
Chris@16
|
670 using random::mt11213b;
|
Chris@16
|
671 using random::mt19937;
|
Chris@16
|
672 using random::mt19937_64;
|
Chris@16
|
673
|
Chris@16
|
674 } // namespace boost
|
Chris@16
|
675
|
Chris@16
|
676 BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt11213b)
|
Chris@16
|
677 BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt19937)
|
Chris@16
|
678 BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt19937_64)
|
Chris@16
|
679
|
Chris@101
|
680 #include <boost/random/detail/enable_warnings.hpp>
|
Chris@101
|
681
|
Chris@16
|
682 #endif // BOOST_RANDOM_MERSENNE_TWISTER_HPP
|