annotate DEPENDENCIES/generic/include/boost/random/mersenne_twister.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
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@16 11 * $Id: mersenne_twister.hpp 74867 2011-10-09 23:13:31Z steven_watanabe $
Chris@16 12 *
Chris@16 13 * Revision history
Chris@16 14 * 2001-02-18 moved to individual header files
Chris@16 15 */
Chris@16 16
Chris@16 17 #ifndef BOOST_RANDOM_MERSENNE_TWISTER_HPP
Chris@16 18 #define BOOST_RANDOM_MERSENNE_TWISTER_HPP
Chris@16 19
Chris@16 20 #include <iosfwd>
Chris@16 21 #include <istream>
Chris@16 22 #include <stdexcept>
Chris@16 23 #include <boost/config.hpp>
Chris@16 24 #include <boost/cstdint.hpp>
Chris@16 25 #include <boost/integer/integer_mask.hpp>
Chris@16 26 #include <boost/random/detail/config.hpp>
Chris@16 27 #include <boost/random/detail/ptr_helper.hpp>
Chris@16 28 #include <boost/random/detail/seed.hpp>
Chris@16 29 #include <boost/random/detail/seed_impl.hpp>
Chris@16 30 #include <boost/random/detail/generator_seed_seq.hpp>
Chris@16 31
Chris@16 32 namespace boost {
Chris@16 33 namespace random {
Chris@16 34
Chris@16 35 /**
Chris@16 36 * Instantiations of class template mersenne_twister_engine model a
Chris@16 37 * \pseudo_random_number_generator. It uses the algorithm described in
Chris@16 38 *
Chris@16 39 * @blockquote
Chris@16 40 * "Mersenne Twister: A 623-dimensionally equidistributed uniform
Chris@16 41 * pseudo-random number generator", Makoto Matsumoto and Takuji Nishimura,
Chris@16 42 * ACM Transactions on Modeling and Computer Simulation: Special Issue on
Chris@16 43 * Uniform Random Number Generation, Vol. 8, No. 1, January 1998, pp. 3-30.
Chris@16 44 * @endblockquote
Chris@16 45 *
Chris@16 46 * @xmlnote
Chris@16 47 * The boost variant has been implemented from scratch and does not
Chris@16 48 * derive from or use mt19937.c provided on the above WWW site. However, it
Chris@16 49 * was verified that both produce identical output.
Chris@16 50 * @endxmlnote
Chris@16 51 *
Chris@16 52 * The seeding from an integer was changed in April 2005 to address a
Chris@16 53 * <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html">weakness</a>.
Chris@16 54 *
Chris@16 55 * The quality of the generator crucially depends on the choice of the
Chris@16 56 * parameters. User code should employ one of the sensibly parameterized
Chris@16 57 * generators such as \mt19937 instead.
Chris@16 58 *
Chris@16 59 * The generator requires considerable amounts of memory for the storage of
Chris@16 60 * its state array. For example, \mt11213b requires about 1408 bytes and
Chris@16 61 * \mt19937 requires about 2496 bytes.
Chris@16 62 */
Chris@16 63 template<class UIntType,
Chris@16 64 std::size_t w, std::size_t n, std::size_t m, std::size_t r,
Chris@16 65 UIntType a, std::size_t u, UIntType d, std::size_t s,
Chris@16 66 UIntType b, std::size_t t,
Chris@16 67 UIntType c, std::size_t l, UIntType f>
Chris@16 68 class mersenne_twister_engine
Chris@16 69 {
Chris@16 70 public:
Chris@16 71 typedef UIntType result_type;
Chris@16 72 BOOST_STATIC_CONSTANT(std::size_t, word_size = w);
Chris@16 73 BOOST_STATIC_CONSTANT(std::size_t, state_size = n);
Chris@16 74 BOOST_STATIC_CONSTANT(std::size_t, shift_size = m);
Chris@16 75 BOOST_STATIC_CONSTANT(std::size_t, mask_bits = r);
Chris@16 76 BOOST_STATIC_CONSTANT(UIntType, xor_mask = a);
Chris@16 77 BOOST_STATIC_CONSTANT(std::size_t, tempering_u = u);
Chris@16 78 BOOST_STATIC_CONSTANT(UIntType, tempering_d = d);
Chris@16 79 BOOST_STATIC_CONSTANT(std::size_t, tempering_s = s);
Chris@16 80 BOOST_STATIC_CONSTANT(UIntType, tempering_b = b);
Chris@16 81 BOOST_STATIC_CONSTANT(std::size_t, tempering_t = t);
Chris@16 82 BOOST_STATIC_CONSTANT(UIntType, tempering_c = c);
Chris@16 83 BOOST_STATIC_CONSTANT(std::size_t, tempering_l = l);
Chris@16 84 BOOST_STATIC_CONSTANT(UIntType, initialization_multiplier = f);
Chris@16 85 BOOST_STATIC_CONSTANT(UIntType, default_seed = 5489u);
Chris@16 86
Chris@16 87 // backwards compatibility
Chris@16 88 BOOST_STATIC_CONSTANT(UIntType, parameter_a = a);
Chris@16 89 BOOST_STATIC_CONSTANT(std::size_t, output_u = u);
Chris@16 90 BOOST_STATIC_CONSTANT(std::size_t, output_s = s);
Chris@16 91 BOOST_STATIC_CONSTANT(UIntType, output_b = b);
Chris@16 92 BOOST_STATIC_CONSTANT(std::size_t, output_t = t);
Chris@16 93 BOOST_STATIC_CONSTANT(UIntType, output_c = c);
Chris@16 94 BOOST_STATIC_CONSTANT(std::size_t, output_l = l);
Chris@16 95
Chris@16 96 // old Boost.Random concept requirements
Chris@16 97 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
Chris@16 98
Chris@16 99
Chris@16 100 /**
Chris@16 101 * Constructs a @c mersenne_twister_engine and calls @c seed().
Chris@16 102 */
Chris@16 103 mersenne_twister_engine() { seed(); }
Chris@16 104
Chris@16 105 /**
Chris@16 106 * Constructs a @c mersenne_twister_engine and calls @c seed(value).
Chris@16 107 */
Chris@16 108 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(mersenne_twister_engine,
Chris@16 109 UIntType, value)
Chris@16 110 { seed(value); }
Chris@16 111 template<class It> mersenne_twister_engine(It& first, It last)
Chris@16 112 { seed(first,last); }
Chris@16 113
Chris@16 114 /**
Chris@16 115 * Constructs a mersenne_twister_engine and calls @c seed(gen).
Chris@16 116 *
Chris@16 117 * @xmlnote
Chris@16 118 * The copy constructor will always be preferred over
Chris@16 119 * the templated constructor.
Chris@16 120 * @endxmlnote
Chris@16 121 */
Chris@16 122 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(mersenne_twister_engine,
Chris@16 123 SeedSeq, seq)
Chris@16 124 { seed(seq); }
Chris@16 125
Chris@16 126 // compiler-generated copy ctor and assignment operator are fine
Chris@16 127
Chris@16 128 /** Calls @c seed(default_seed). */
Chris@16 129 void seed() { seed(default_seed); }
Chris@16 130
Chris@16 131 /**
Chris@16 132 * Sets the state x(0) to v mod 2w. Then, iteratively,
Chris@16 133 * sets x(i) to
Chris@16 134 * (i + f * (x(i-1) xor (x(i-1) rshift w-2))) mod 2<sup>w</sup>
Chris@16 135 * for i = 1 .. n-1. x(n) is the first value to be returned by operator().
Chris@16 136 */
Chris@16 137 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(mersenne_twister_engine, UIntType, value)
Chris@16 138 {
Chris@16 139 // New seeding algorithm from
Chris@16 140 // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
Chris@16 141 // In the previous versions, MSBs of the seed affected only MSBs of the
Chris@16 142 // state x[].
Chris@16 143 const UIntType mask = (max)();
Chris@16 144 x[0] = value & mask;
Chris@16 145 for (i = 1; i < n; i++) {
Chris@16 146 // See Knuth "The Art of Computer Programming"
Chris@16 147 // Vol. 2, 3rd ed., page 106
Chris@16 148 x[i] = (f * (x[i-1] ^ (x[i-1] >> (w-2))) + i) & mask;
Chris@16 149 }
Chris@16 150 }
Chris@16 151
Chris@16 152 /**
Chris@16 153 * Seeds a mersenne_twister_engine using values produced by seq.generate().
Chris@16 154 */
Chris@16 155 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(mersenne_twister_engine, SeeqSeq, seq)
Chris@16 156 {
Chris@16 157 detail::seed_array_int<w>(seq, x);
Chris@16 158 i = n;
Chris@16 159
Chris@16 160 // fix up the state if it's all zeroes.
Chris@16 161 if((x[0] & (~static_cast<UIntType>(0) << r)) == 0) {
Chris@16 162 for(std::size_t j = 1; j < n; ++j) {
Chris@16 163 if(x[j] != 0) return;
Chris@16 164 }
Chris@16 165 x[0] = static_cast<UIntType>(1) << (w-1);
Chris@16 166 }
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@16 176 // fix up the state if it's all zeroes.
Chris@16 177 if((x[0] & (~static_cast<UIntType>(0) << r)) == 0) {
Chris@16 178 for(std::size_t j = 1; j < n; ++j) {
Chris@16 179 if(x[j] != 0) return;
Chris@16 180 }
Chris@16 181 x[0] = static_cast<UIntType>(1) << (w-1);
Chris@16 182 }
Chris@16 183 }
Chris@16 184
Chris@16 185 /** Returns the smallest value that the generator can produce. */
Chris@16 186 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 187 { return 0; }
Chris@16 188 /** Returns the largest value that the generator can produce. */
Chris@16 189 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 190 { return boost::low_bits_mask_t<w>::sig_bits; }
Chris@16 191
Chris@16 192 /** Produces the next value of the generator. */
Chris@16 193 result_type operator()();
Chris@16 194
Chris@16 195 /** Fills a range with random values */
Chris@16 196 template<class Iter>
Chris@16 197 void generate(Iter first, Iter last)
Chris@16 198 { detail::generate_from_int(*this, first, last); }
Chris@16 199
Chris@16 200 /**
Chris@16 201 * Advances the state of the generator by @c z steps. Equivalent to
Chris@16 202 *
Chris@16 203 * @code
Chris@16 204 * for(unsigned long long i = 0; i < z; ++i) {
Chris@16 205 * gen();
Chris@16 206 * }
Chris@16 207 * @endcode
Chris@16 208 */
Chris@16 209 void discard(boost::uintmax_t z)
Chris@16 210 {
Chris@16 211 for(boost::uintmax_t j = 0; j < z; ++j) {
Chris@16 212 (*this)();
Chris@16 213 }
Chris@16 214 }
Chris@16 215
Chris@16 216 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
Chris@16 217 /** Writes a mersenne_twister_engine to a @c std::ostream */
Chris@16 218 template<class CharT, class Traits>
Chris@16 219 friend std::basic_ostream<CharT,Traits>&
Chris@16 220 operator<<(std::basic_ostream<CharT,Traits>& os,
Chris@16 221 const mersenne_twister_engine& mt)
Chris@16 222 {
Chris@16 223 mt.print(os);
Chris@16 224 return os;
Chris@16 225 }
Chris@16 226
Chris@16 227 /** Reads a mersenne_twister_engine from a @c std::istream */
Chris@16 228 template<class CharT, class Traits>
Chris@16 229 friend std::basic_istream<CharT,Traits>&
Chris@16 230 operator>>(std::basic_istream<CharT,Traits>& is,
Chris@16 231 mersenne_twister_engine& mt)
Chris@16 232 {
Chris@16 233 for(std::size_t j = 0; j < mt.state_size; ++j)
Chris@16 234 is >> mt.x[j] >> std::ws;
Chris@16 235 // MSVC (up to 7.1) and Borland (up to 5.64) don't handle the template
Chris@16 236 // value parameter "n" available from the class template scope, so use
Chris@16 237 // the static constant with the same value
Chris@16 238 mt.i = mt.state_size;
Chris@16 239 return is;
Chris@16 240 }
Chris@16 241 #endif
Chris@16 242
Chris@16 243 /**
Chris@16 244 * Returns true if the two generators are in the same state,
Chris@16 245 * and will thus produce identical sequences.
Chris@16 246 */
Chris@16 247 friend bool operator==(const mersenne_twister_engine& x,
Chris@16 248 const mersenne_twister_engine& y)
Chris@16 249 {
Chris@16 250 if(x.i < y.i) return x.equal_imp(y);
Chris@16 251 else return y.equal_imp(x);
Chris@16 252 }
Chris@16 253
Chris@16 254 /**
Chris@16 255 * Returns true if the two generators are in different states.
Chris@16 256 */
Chris@16 257 friend bool operator!=(const mersenne_twister_engine& x,
Chris@16 258 const mersenne_twister_engine& y)
Chris@16 259 { return !(x == y); }
Chris@16 260
Chris@16 261 private:
Chris@16 262 /// \cond show_private
Chris@16 263
Chris@16 264 void twist();
Chris@16 265
Chris@16 266 /**
Chris@16 267 * Does the work of operator==. This is in a member function
Chris@16 268 * for portability. Some compilers, such as msvc 7.1 and
Chris@16 269 * Sun CC 5.10 can't access template parameters or static
Chris@16 270 * members of the class from inline friend functions.
Chris@16 271 *
Chris@16 272 * requires i <= other.i
Chris@16 273 */
Chris@16 274 bool equal_imp(const mersenne_twister_engine& other) const
Chris@16 275 {
Chris@16 276 UIntType back[n];
Chris@16 277 std::size_t offset = other.i - i;
Chris@16 278 for(std::size_t j = 0; j + offset < n; ++j)
Chris@16 279 if(x[j] != other.x[j+offset])
Chris@16 280 return false;
Chris@16 281 rewind(&back[n-1], offset);
Chris@16 282 for(std::size_t j = 0; j < offset; ++j)
Chris@16 283 if(back[j + n - offset] != other.x[j])
Chris@16 284 return false;
Chris@16 285 return true;
Chris@16 286 }
Chris@16 287
Chris@16 288 /**
Chris@16 289 * Does the work of operator<<. This is in a member function
Chris@16 290 * for portability.
Chris@16 291 */
Chris@16 292 template<class CharT, class Traits>
Chris@16 293 void print(std::basic_ostream<CharT, Traits>& os) const
Chris@16 294 {
Chris@16 295 UIntType data[n];
Chris@16 296 for(std::size_t j = 0; j < i; ++j) {
Chris@16 297 data[j + n - i] = x[j];
Chris@16 298 }
Chris@16 299 if(i != n) {
Chris@16 300 rewind(&data[n - i - 1], n - i);
Chris@16 301 }
Chris@16 302 os << data[0];
Chris@16 303 for(std::size_t j = 1; j < n; ++j) {
Chris@16 304 os << ' ' << data[j];
Chris@16 305 }
Chris@16 306 }
Chris@16 307
Chris@16 308 /**
Chris@16 309 * Copies z elements of the state preceding x[0] into
Chris@16 310 * the array whose last element is last.
Chris@16 311 */
Chris@16 312 void rewind(UIntType* last, std::size_t z) const
Chris@16 313 {
Chris@16 314 const UIntType upper_mask = (~static_cast<UIntType>(0)) << r;
Chris@16 315 const UIntType lower_mask = ~upper_mask;
Chris@16 316 UIntType y0 = x[m-1] ^ x[n-1];
Chris@16 317 if(y0 & (static_cast<UIntType>(1) << (w-1))) {
Chris@16 318 y0 = ((y0 ^ a) << 1) | 1;
Chris@16 319 } else {
Chris@16 320 y0 = y0 << 1;
Chris@16 321 }
Chris@16 322 for(std::size_t sz = 0; sz < z; ++sz) {
Chris@16 323 UIntType y1 =
Chris@16 324 rewind_find(last, sz, m-1) ^ rewind_find(last, sz, n-1);
Chris@16 325 if(y1 & (static_cast<UIntType>(1) << (w-1))) {
Chris@16 326 y1 = ((y1 ^ a) << 1) | 1;
Chris@16 327 } else {
Chris@16 328 y1 = y1 << 1;
Chris@16 329 }
Chris@16 330 *(last - sz) = (y0 & upper_mask) | (y1 & lower_mask);
Chris@16 331 y0 = y1;
Chris@16 332 }
Chris@16 333 }
Chris@16 334
Chris@16 335 /**
Chris@16 336 * Given a pointer to the last element of the rewind array,
Chris@16 337 * and the current size of the rewind array, finds an element
Chris@16 338 * relative to the next available slot in the rewind array.
Chris@16 339 */
Chris@16 340 UIntType
Chris@16 341 rewind_find(UIntType* last, std::size_t size, std::size_t j) const
Chris@16 342 {
Chris@16 343 std::size_t index = (j + n - size + n - 1) % n;
Chris@16 344 if(index < n - size) {
Chris@16 345 return x[index];
Chris@16 346 } else {
Chris@16 347 return *(last - (n - 1 - index));
Chris@16 348 }
Chris@16 349 }
Chris@16 350
Chris@16 351 /// \endcond
Chris@16 352
Chris@16 353 // state representation: next output is o(x(i))
Chris@16 354 // x[0] ... x[k] x[k+1] ... x[n-1] represents
Chris@16 355 // x(i-k) ... x(i) x(i+1) ... x(i-k+n-1)
Chris@16 356
Chris@16 357 UIntType x[n];
Chris@16 358 std::size_t i;
Chris@16 359 };
Chris@16 360
Chris@16 361 /// \cond show_private
Chris@16 362
Chris@16 363 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
Chris@16 364 // A definition is required even for integral static constants
Chris@16 365 #define BOOST_RANDOM_MT_DEFINE_CONSTANT(type, name) \
Chris@16 366 template<class UIntType, std::size_t w, std::size_t n, std::size_t m, \
Chris@16 367 std::size_t r, UIntType a, std::size_t u, UIntType d, std::size_t s, \
Chris@16 368 UIntType b, std::size_t t, UIntType c, std::size_t l, UIntType f> \
Chris@16 369 const type mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::name
Chris@16 370 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, word_size);
Chris@16 371 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, state_size);
Chris@16 372 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, shift_size);
Chris@16 373 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, mask_bits);
Chris@16 374 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, xor_mask);
Chris@16 375 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_u);
Chris@16 376 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, tempering_d);
Chris@16 377 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_s);
Chris@16 378 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, tempering_b);
Chris@16 379 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_t);
Chris@16 380 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, tempering_c);
Chris@16 381 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, tempering_l);
Chris@16 382 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, initialization_multiplier);
Chris@16 383 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, default_seed);
Chris@16 384 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, parameter_a);
Chris@16 385 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_u );
Chris@16 386 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_s);
Chris@16 387 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, output_b);
Chris@16 388 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_t);
Chris@16 389 BOOST_RANDOM_MT_DEFINE_CONSTANT(UIntType, output_c);
Chris@16 390 BOOST_RANDOM_MT_DEFINE_CONSTANT(std::size_t, output_l);
Chris@16 391 BOOST_RANDOM_MT_DEFINE_CONSTANT(bool, has_fixed_range);
Chris@16 392 #undef BOOST_RANDOM_MT_DEFINE_CONSTANT
Chris@16 393 #endif
Chris@16 394
Chris@16 395 template<class UIntType,
Chris@16 396 std::size_t w, std::size_t n, std::size_t m, std::size_t r,
Chris@16 397 UIntType a, std::size_t u, UIntType d, std::size_t s,
Chris@16 398 UIntType b, std::size_t t,
Chris@16 399 UIntType c, std::size_t l, UIntType f>
Chris@16 400 void
Chris@16 401 mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::twist()
Chris@16 402 {
Chris@16 403 const UIntType upper_mask = (~static_cast<UIntType>(0)) << r;
Chris@16 404 const UIntType lower_mask = ~upper_mask;
Chris@16 405
Chris@16 406 const std::size_t unroll_factor = 6;
Chris@16 407 const std::size_t unroll_extra1 = (n-m) % unroll_factor;
Chris@16 408 const std::size_t unroll_extra2 = (m-1) % unroll_factor;
Chris@16 409
Chris@16 410 // split loop to avoid costly modulo operations
Chris@16 411 { // extra scope for MSVC brokenness w.r.t. for scope
Chris@16 412 for(std::size_t j = 0; j < n-m-unroll_extra1; j++) {
Chris@16 413 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
Chris@16 414 x[j] = x[j+m] ^ (y >> 1) ^ ((x[j+1]&1) * a);
Chris@16 415 }
Chris@16 416 }
Chris@16 417 {
Chris@16 418 for(std::size_t j = n-m-unroll_extra1; j < n-m; j++) {
Chris@16 419 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
Chris@16 420 x[j] = x[j+m] ^ (y >> 1) ^ ((x[j+1]&1) * a);
Chris@16 421 }
Chris@16 422 }
Chris@16 423 {
Chris@16 424 for(std::size_t j = n-m; j < n-1-unroll_extra2; j++) {
Chris@16 425 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
Chris@16 426 x[j] = x[j-(n-m)] ^ (y >> 1) ^ ((x[j+1]&1) * a);
Chris@16 427 }
Chris@16 428 }
Chris@16 429 {
Chris@16 430 for(std::size_t j = n-1-unroll_extra2; j < n-1; j++) {
Chris@16 431 UIntType y = (x[j] & upper_mask) | (x[j+1] & lower_mask);
Chris@16 432 x[j] = x[j-(n-m)] ^ (y >> 1) ^ ((x[j+1]&1) * a);
Chris@16 433 }
Chris@16 434 }
Chris@16 435 // last iteration
Chris@16 436 UIntType y = (x[n-1] & upper_mask) | (x[0] & lower_mask);
Chris@16 437 x[n-1] = x[m-1] ^ (y >> 1) ^ ((x[0]&1) * a);
Chris@16 438 i = 0;
Chris@16 439 }
Chris@16 440 /// \endcond
Chris@16 441
Chris@16 442 template<class UIntType,
Chris@16 443 std::size_t w, std::size_t n, std::size_t m, std::size_t r,
Chris@16 444 UIntType a, std::size_t u, UIntType d, std::size_t s,
Chris@16 445 UIntType b, std::size_t t,
Chris@16 446 UIntType c, std::size_t l, UIntType f>
Chris@16 447 inline typename
Chris@16 448 mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::result_type
Chris@16 449 mersenne_twister_engine<UIntType,w,n,m,r,a,u,d,s,b,t,c,l,f>::operator()()
Chris@16 450 {
Chris@16 451 if(i == n)
Chris@16 452 twist();
Chris@16 453 // Step 4
Chris@16 454 UIntType z = x[i];
Chris@16 455 ++i;
Chris@16 456 z ^= ((z >> u) & d);
Chris@16 457 z ^= ((z << s) & b);
Chris@16 458 z ^= ((z << t) & c);
Chris@16 459 z ^= (z >> l);
Chris@16 460 return z;
Chris@16 461 }
Chris@16 462
Chris@16 463 /**
Chris@16 464 * The specializations \mt11213b and \mt19937 are from
Chris@16 465 *
Chris@16 466 * @blockquote
Chris@16 467 * "Mersenne Twister: A 623-dimensionally equidistributed
Chris@16 468 * uniform pseudo-random number generator", Makoto Matsumoto
Chris@16 469 * and Takuji Nishimura, ACM Transactions on Modeling and
Chris@16 470 * Computer Simulation: Special Issue on Uniform Random Number
Chris@16 471 * Generation, Vol. 8, No. 1, January 1998, pp. 3-30.
Chris@16 472 * @endblockquote
Chris@16 473 */
Chris@16 474 typedef mersenne_twister_engine<uint32_t,32,351,175,19,0xccab8ee7,
Chris@16 475 11,0xffffffff,7,0x31b6ab00,15,0xffe50000,17,1812433253> mt11213b;
Chris@16 476
Chris@16 477 /**
Chris@16 478 * The specializations \mt11213b and \mt19937 are from
Chris@16 479 *
Chris@16 480 * @blockquote
Chris@16 481 * "Mersenne Twister: A 623-dimensionally equidistributed
Chris@16 482 * uniform pseudo-random number generator", Makoto Matsumoto
Chris@16 483 * and Takuji Nishimura, ACM Transactions on Modeling and
Chris@16 484 * Computer Simulation: Special Issue on Uniform Random Number
Chris@16 485 * Generation, Vol. 8, No. 1, January 1998, pp. 3-30.
Chris@16 486 * @endblockquote
Chris@16 487 */
Chris@16 488 typedef mersenne_twister_engine<uint32_t,32,624,397,31,0x9908b0df,
Chris@16 489 11,0xffffffff,7,0x9d2c5680,15,0xefc60000,18,1812433253> mt19937;
Chris@16 490
Chris@16 491 #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
Chris@16 492 typedef mersenne_twister_engine<uint64_t,64,312,156,31,
Chris@16 493 UINT64_C(0xb5026f5aa96619e9),29,UINT64_C(0x5555555555555555),17,
Chris@16 494 UINT64_C(0x71d67fffeda60000),37,UINT64_C(0xfff7eee000000000),43,
Chris@16 495 UINT64_C(6364136223846793005)> mt19937_64;
Chris@16 496 #endif
Chris@16 497
Chris@16 498 /// \cond show_deprecated
Chris@16 499
Chris@16 500 template<class UIntType,
Chris@16 501 int w, int n, int m, int r,
Chris@16 502 UIntType a, int u, std::size_t s,
Chris@16 503 UIntType b, int t,
Chris@16 504 UIntType c, int l, UIntType v>
Chris@16 505 class mersenne_twister :
Chris@16 506 public mersenne_twister_engine<UIntType,
Chris@16 507 w, n, m, r, a, u, ~(UIntType)0, s, b, t, c, l, 1812433253>
Chris@16 508 {
Chris@16 509 typedef mersenne_twister_engine<UIntType,
Chris@16 510 w, n, m, r, a, u, ~(UIntType)0, s, b, t, c, l, 1812433253> base_type;
Chris@16 511 public:
Chris@16 512 mersenne_twister() {}
Chris@16 513 BOOST_RANDOM_DETAIL_GENERATOR_CONSTRUCTOR(mersenne_twister, Gen, gen)
Chris@16 514 { seed(gen); }
Chris@16 515 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(mersenne_twister, UIntType, val)
Chris@16 516 { seed(val); }
Chris@16 517 template<class It>
Chris@16 518 mersenne_twister(It& first, It last) : base_type(first, last) {}
Chris@16 519 void seed() { base_type::seed(); }
Chris@16 520 BOOST_RANDOM_DETAIL_GENERATOR_SEED(mersenne_twister, Gen, gen)
Chris@16 521 {
Chris@16 522 detail::generator_seed_seq<Gen> seq(gen);
Chris@16 523 base_type::seed(seq);
Chris@16 524 }
Chris@16 525 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(mersenne_twister, UIntType, val)
Chris@16 526 { base_type::seed(val); }
Chris@16 527 template<class It>
Chris@16 528 void seed(It& first, It last) { base_type::seed(first, last); }
Chris@16 529 };
Chris@16 530
Chris@16 531 /// \endcond
Chris@16 532
Chris@16 533 } // namespace random
Chris@16 534
Chris@16 535 using random::mt11213b;
Chris@16 536 using random::mt19937;
Chris@16 537 using random::mt19937_64;
Chris@16 538
Chris@16 539 } // namespace boost
Chris@16 540
Chris@16 541 BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt11213b)
Chris@16 542 BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt19937)
Chris@16 543 BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt19937_64)
Chris@16 544
Chris@16 545 #endif // BOOST_RANDOM_MERSENNE_TWISTER_HPP