mersenne_twister.hpp 24 KB

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