sl@0: /* boost random/mersenne_twister.hpp header file sl@0: * sl@0: * Copyright Jens Maurer 2000-2001 sl@0: * Distributed under the Boost Software License, Version 1.0. (See sl@0: * accompanying file LICENSE_1_0.txt or copy at sl@0: * http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: * See http://www.boost.org for most recent version including documentation. sl@0: * sl@0: * $Id: mersenne_twister.hpp,v 1.20 2005/07/21 22:04:31 jmaurer Exp $ sl@0: * sl@0: * Revision history sl@0: * 2001-02-18 moved to individual header files sl@0: */ sl@0: sl@0: #ifndef BOOST_RANDOM_MERSENNE_TWISTER_HPP sl@0: #define BOOST_RANDOM_MERSENNE_TWISTER_HPP sl@0: sl@0: #include sl@0: #include // std::copy sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: namespace random { sl@0: sl@0: // http://www.math.keio.ac.jp/matumoto/emt.html sl@0: template sl@0: class mersenne_twister sl@0: { sl@0: public: sl@0: typedef UIntType result_type; sl@0: BOOST_STATIC_CONSTANT(int, word_size = w); sl@0: BOOST_STATIC_CONSTANT(int, state_size = n); sl@0: BOOST_STATIC_CONSTANT(int, shift_size = m); sl@0: BOOST_STATIC_CONSTANT(int, mask_bits = r); sl@0: BOOST_STATIC_CONSTANT(UIntType, parameter_a = a); sl@0: BOOST_STATIC_CONSTANT(int, output_u = u); sl@0: BOOST_STATIC_CONSTANT(int, output_s = s); sl@0: BOOST_STATIC_CONSTANT(UIntType, output_b = b); sl@0: BOOST_STATIC_CONSTANT(int, output_t = t); sl@0: BOOST_STATIC_CONSTANT(UIntType, output_c = c); sl@0: BOOST_STATIC_CONSTANT(int, output_l = l); sl@0: sl@0: BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); sl@0: sl@0: mersenne_twister() { seed(); } sl@0: sl@0: #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520) sl@0: // Work around overload resolution problem (Gennadiy E. Rozental) sl@0: explicit mersenne_twister(const UIntType& value) sl@0: #else sl@0: explicit mersenne_twister(UIntType value) sl@0: #endif sl@0: { seed(value); } sl@0: template mersenne_twister(It& first, It last) { seed(first,last); } sl@0: sl@0: template sl@0: explicit mersenne_twister(Generator & gen) { seed(gen); } sl@0: sl@0: // compiler-generated copy ctor and assignment operator are fine sl@0: sl@0: void seed() { seed(UIntType(5489)); } sl@0: sl@0: #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520) sl@0: // Work around overload resolution problem (Gennadiy E. Rozental) sl@0: void seed(const UIntType& value) sl@0: #else sl@0: void seed(UIntType value) sl@0: #endif sl@0: { sl@0: // New seeding algorithm from sl@0: // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html sl@0: // In the previous versions, MSBs of the seed affected only MSBs of the sl@0: // state x[]. sl@0: const UIntType mask = ~0u; sl@0: x[0] = value & mask; sl@0: for (i = 1; i < n; i++) { sl@0: // See Knuth "The Art of Computer Programming" Vol. 2, 3rd ed., page 106 sl@0: x[i] = (1812433253UL * (x[i-1] ^ (x[i-1] >> (w-2))) + i) & mask; sl@0: } sl@0: } sl@0: sl@0: // For GCC, moving this function out-of-line prevents inlining, which may sl@0: // reduce overall object code size. However, MSVC does not grok sl@0: // out-of-line definitions of member function templates. sl@0: template sl@0: void seed(Generator & gen) sl@0: { sl@0: #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS sl@0: BOOST_STATIC_ASSERT(!std::numeric_limits::is_signed); sl@0: #endif sl@0: // I could have used std::generate_n, but it takes "gen" by value sl@0: for(int j = 0; j < n; j++) sl@0: x[j] = gen(); sl@0: i = n; sl@0: } sl@0: sl@0: template sl@0: void seed(It& first, It last) sl@0: { sl@0: int j; sl@0: for(j = 0; j < n && first != last; ++j, ++first) sl@0: x[j] = *first; sl@0: i = n; sl@0: if(first == last && j < n) sl@0: throw std::invalid_argument("mersenne_twister::seed"); sl@0: } sl@0: sl@0: result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; } sl@0: result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const sl@0: { sl@0: // avoid "left shift count >= with of type" warning sl@0: result_type res = 0; sl@0: for(int i = 0; i < w; ++i) sl@0: res |= (1u << i); sl@0: return res; sl@0: } sl@0: sl@0: result_type operator()(); sl@0: static bool validation(result_type v) { return val == v; } sl@0: sl@0: #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE sl@0: sl@0: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS sl@0: template sl@0: friend std::basic_ostream& sl@0: operator<<(std::basic_ostream& os, const mersenne_twister& mt) sl@0: { sl@0: for(int j = 0; j < mt.state_size; ++j) sl@0: os << mt.compute(j) << " "; sl@0: return os; sl@0: } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, mersenne_twister& mt) sl@0: { sl@0: for(int j = 0; j < mt.state_size; ++j) sl@0: is >> mt.x[j] >> std::ws; sl@0: // MSVC (up to 7.1) and Borland (up to 5.64) don't handle the template sl@0: // value parameter "n" available from the class template scope, so use sl@0: // the static constant with the same value sl@0: mt.i = mt.state_size; sl@0: return is; sl@0: } sl@0: #endif sl@0: sl@0: friend bool operator==(const mersenne_twister& x, const mersenne_twister& y) sl@0: { sl@0: for(int j = 0; j < state_size; ++j) sl@0: if(x.compute(j) != y.compute(j)) sl@0: return false; sl@0: return true; sl@0: } sl@0: sl@0: friend bool operator!=(const mersenne_twister& x, const mersenne_twister& y) sl@0: { return !(x == y); } sl@0: #else sl@0: // Use a member function; Streamable concept not supported. sl@0: bool operator==(const mersenne_twister& rhs) const sl@0: { sl@0: for(int j = 0; j < state_size; ++j) sl@0: if(compute(j) != rhs.compute(j)) sl@0: return false; sl@0: return true; sl@0: } sl@0: sl@0: bool operator!=(const mersenne_twister& rhs) const sl@0: { return !(*this == rhs); } sl@0: #endif sl@0: sl@0: private: sl@0: // returns x(i-n+index), where index is in 0..n-1 sl@0: UIntType compute(unsigned int index) const sl@0: { sl@0: // equivalent to (i-n+index) % 2n, but doesn't produce negative numbers sl@0: return x[ (i + n + index) % (2*n) ]; sl@0: } sl@0: void twist(int block); sl@0: sl@0: // state representation: next output is o(x(i)) sl@0: // x[0] ... x[k] x[k+1] ... x[n-1] x[n] ... x[2*n-1] represents sl@0: // x(i-k) ... x(i) x(i+1) ... x(i-k+n-1) x(i-k-n) ... x[i(i-k-1)] sl@0: // The goal is to always have x(i-n) ... x(i-1) available for sl@0: // operator== and save/restore. sl@0: sl@0: UIntType x[2*n]; sl@0: int i; sl@0: }; sl@0: sl@0: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION sl@0: // A definition is required even for integral static constants sl@0: template sl@0: const bool mersenne_twister::has_fixed_range; sl@0: template sl@0: const int mersenne_twister::state_size; sl@0: template sl@0: const int mersenne_twister::shift_size; sl@0: template sl@0: const int mersenne_twister::mask_bits; sl@0: template sl@0: const UIntType mersenne_twister::parameter_a; sl@0: template sl@0: const int mersenne_twister::output_u; sl@0: template sl@0: const int mersenne_twister::output_s; sl@0: template sl@0: const UIntType mersenne_twister::output_b; sl@0: template sl@0: const int mersenne_twister::output_t; sl@0: template sl@0: const UIntType mersenne_twister::output_c; sl@0: template sl@0: const int mersenne_twister::output_l; sl@0: #endif sl@0: sl@0: template sl@0: void mersenne_twister::twist(int block) sl@0: { sl@0: const UIntType upper_mask = (~0u) << r; sl@0: const UIntType lower_mask = ~upper_mask; sl@0: sl@0: if(block == 0) { sl@0: for(int j = n; j < 2*n; j++) { sl@0: UIntType y = (x[j-n] & upper_mask) | (x[j-(n-1)] & lower_mask); sl@0: x[j] = x[j-(n-m)] ^ (y >> 1) ^ (y&1 ? a : 0); sl@0: } sl@0: } else if (block == 1) { sl@0: // split loop to avoid costly modulo operations sl@0: { // extra scope for MSVC brokenness w.r.t. for scope sl@0: for(int j = 0; j < n-m; j++) { sl@0: UIntType y = (x[j+n] & upper_mask) | (x[j+n+1] & lower_mask); sl@0: x[j] = x[j+n+m] ^ (y >> 1) ^ (y&1 ? a : 0); sl@0: } sl@0: } sl@0: sl@0: for(int j = n-m; j < n-1; j++) { sl@0: UIntType y = (x[j+n] & upper_mask) | (x[j+n+1] & lower_mask); sl@0: x[j] = x[j-(n-m)] ^ (y >> 1) ^ (y&1 ? a : 0); sl@0: } sl@0: // last iteration sl@0: UIntType y = (x[2*n-1] & upper_mask) | (x[0] & lower_mask); sl@0: x[n-1] = x[m-1] ^ (y >> 1) ^ (y&1 ? a : 0); sl@0: i = 0; sl@0: } sl@0: } sl@0: sl@0: template sl@0: inline typename mersenne_twister::result_type sl@0: mersenne_twister::operator()() sl@0: { sl@0: if(i == n) sl@0: twist(0); sl@0: else if(i >= 2*n) sl@0: twist(1); sl@0: // Step 4 sl@0: UIntType z = x[i]; sl@0: ++i; sl@0: z ^= (z >> u); sl@0: z ^= ((z << s) & b); sl@0: z ^= ((z << t) & c); sl@0: z ^= (z >> l); sl@0: return z; sl@0: } sl@0: sl@0: } // namespace random sl@0: sl@0: sl@0: typedef random::mersenne_twister mt11213b; sl@0: sl@0: // validation by experiment from mt19937.c sl@0: typedef random::mersenne_twister mt19937; sl@0: sl@0: } // namespace boost sl@0: sl@0: BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt19937) sl@0: sl@0: #endif // BOOST_RANDOM_MERSENNE_TWISTER_HPP