sl@0: /* boost random/linear_congruential.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: linear_congruential.hpp,v 1.22 2005/05/21 15:57:00 dgregor 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_LINEAR_CONGRUENTIAL_HPP sl@0: #define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP sl@0: 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: // compile-time configurable linear congruential generator sl@0: template sl@0: class linear_congruential sl@0: { sl@0: public: sl@0: typedef IntType result_type; sl@0: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION sl@0: static const bool has_fixed_range = true; sl@0: static const result_type min_value = ( c == 0 ? 1 : 0 ); sl@0: static const result_type max_value = m-1; sl@0: #else sl@0: BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); sl@0: #endif sl@0: BOOST_STATIC_CONSTANT(IntType, multiplier = a); sl@0: BOOST_STATIC_CONSTANT(IntType, increment = c); sl@0: BOOST_STATIC_CONSTANT(IntType, modulus = m); sl@0: sl@0: // MSVC 6 and possibly others crash when encountering complicated integral sl@0: // constant expressions. Avoid the check for now. sl@0: // BOOST_STATIC_ASSERT(m == 0 || a < m); sl@0: // BOOST_STATIC_ASSERT(m == 0 || c < m); sl@0: sl@0: explicit linear_congruential(IntType x0 = 1) sl@0: : _modulus(modulus), _x(_modulus ? (x0 % _modulus) : x0) sl@0: { sl@0: assert(c || x0); /* if c == 0 and x(0) == 0 then x(n) = 0 for all n */ sl@0: // overflow check sl@0: // disabled because it gives spurious "divide by zero" gcc warnings sl@0: // assert(m == 0 || (a*(m-1)+c) % m == (c < a ? c-a+m : c-a)); sl@0: sl@0: // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope sl@0: #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS sl@0: BOOST_STATIC_ASSERT(std::numeric_limits::is_integer); sl@0: #endif sl@0: } sl@0: sl@0: template sl@0: linear_congruential(It& first, It last) { seed(first, last); } sl@0: sl@0: // compiler-generated copy constructor and assignment operator are fine sl@0: void seed(IntType x0 = 1) sl@0: { sl@0: assert(c || x0); sl@0: _x = (_modulus ? (x0 % _modulus) : x0); sl@0: } sl@0: sl@0: template sl@0: void seed(It& first, It last) sl@0: { sl@0: if(first == last) sl@0: throw std::invalid_argument("linear_congruential::seed"); sl@0: IntType value = *first++; sl@0: _x = (_modulus ? (value % _modulus) : value); sl@0: } sl@0: sl@0: result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return c == 0 ? 1 : 0; } sl@0: result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return modulus-1; } sl@0: sl@0: IntType operator()() sl@0: { sl@0: _x = const_mod::mult_add(a, _x, c); sl@0: return _x; sl@0: } sl@0: sl@0: static bool validation(IntType x) { return val == x; } sl@0: sl@0: #ifdef BOOST_NO_OPERATORS_IN_NAMESPACE sl@0: sl@0: // Use a member function; Streamable concept not supported. sl@0: bool operator==(const linear_congruential& rhs) const sl@0: { return _x == rhs._x; } sl@0: bool operator!=(const linear_congruential& rhs) const sl@0: { return !(*this == rhs); } sl@0: sl@0: #else sl@0: friend bool operator==(const linear_congruential& x, sl@0: const linear_congruential& y) sl@0: { return x._x == y._x; } sl@0: friend bool operator!=(const linear_congruential& x, sl@0: const linear_congruential& y) sl@0: { return !(x == y); } sl@0: sl@0: #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551)) sl@0: template sl@0: friend std::basic_ostream& sl@0: operator<<(std::basic_ostream& os, sl@0: const linear_congruential& lcg) sl@0: { sl@0: return os << lcg._x; sl@0: } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, sl@0: linear_congruential& lcg) sl@0: { sl@0: return is >> lcg._x; sl@0: } sl@0: sl@0: private: sl@0: #endif sl@0: #endif sl@0: sl@0: IntType _modulus; // work-around for gcc "divide by zero" warning in ctor sl@0: IntType _x; sl@0: }; sl@0: sl@0: // probably needs the "no native streams" caveat for STLPort sl@0: #if !defined(__SGI_STL_PORT) && BOOST_WORKAROUND(__GNUC__, == 2) sl@0: template sl@0: std::ostream& sl@0: operator<<(std::ostream& os, sl@0: const linear_congruential& lcg) sl@0: { sl@0: return os << lcg._x; sl@0: } sl@0: sl@0: template sl@0: std::istream& sl@0: operator>>(std::istream& is, sl@0: linear_congruential& lcg) sl@0: { sl@0: return is >> lcg._x; sl@0: } sl@0: #elif defined(BOOST_NO_OPERATORS_IN_NAMESPACE) || defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551)) sl@0: template sl@0: std::basic_ostream& sl@0: operator<<(std::basic_ostream& os, sl@0: const linear_congruential& lcg) sl@0: { sl@0: return os << lcg._x; sl@0: } sl@0: sl@0: template sl@0: std::basic_istream& sl@0: operator>>(std::basic_istream& is, sl@0: linear_congruential& lcg) sl@0: { sl@0: return is >> lcg._x; sl@0: } sl@0: #endif 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 linear_congruential::has_fixed_range; sl@0: template sl@0: const typename linear_congruential::result_type linear_congruential::min_value; sl@0: template sl@0: const typename linear_congruential::result_type linear_congruential::max_value; sl@0: template sl@0: const IntType linear_congruential::modulus; sl@0: #endif sl@0: sl@0: } // namespace random sl@0: sl@0: // validation values from the publications sl@0: typedef random::linear_congruential minstd_rand0; sl@0: typedef random::linear_congruential minstd_rand; sl@0: sl@0: sl@0: #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T) sl@0: // emulate the lrand48() C library function; requires support for uint64_t sl@0: class rand48 sl@0: { sl@0: public: sl@0: typedef int32_t result_type; sl@0: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION sl@0: static const bool has_fixed_range = true; sl@0: static const int32_t min_value = 0; sl@0: static const int32_t max_value = integer_traits::const_max; sl@0: #else sl@0: enum { has_fixed_range = false }; sl@0: #endif sl@0: int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; } sl@0: int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::numeric_limits::max BOOST_PREVENT_MACRO_SUBSTITUTION (); } sl@0: sl@0: explicit rand48(int32_t x0 = 1) : lcf(cnv(x0)) { } sl@0: explicit rand48(uint64_t x0) : lcf(x0) { } sl@0: template rand48(It& first, It last) : lcf(first, last) { } sl@0: // compiler-generated copy ctor and assignment operator are fine sl@0: void seed(int32_t x0 = 1) { lcf.seed(cnv(x0)); } sl@0: void seed(uint64_t x0) { lcf.seed(x0); } sl@0: template void seed(It& first, It last) { lcf.seed(first,last); } sl@0: sl@0: int32_t operator()() { return static_cast(lcf() >> 17); } sl@0: // by experiment from lrand48() sl@0: static bool validation(int32_t x) { return x == 1993516219; } 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 rand48& r) sl@0: { os << r.lcf; return os; } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, rand48& r) sl@0: { is >> r.lcf; return is; } sl@0: #endif sl@0: sl@0: friend bool operator==(const rand48& x, const rand48& y) sl@0: { return x.lcf == y.lcf; } sl@0: friend bool operator!=(const rand48& x, const rand48& y) sl@0: { return !(x == y); } sl@0: #else sl@0: // Use a member function; Streamable concept not supported. sl@0: bool operator==(const rand48& rhs) const sl@0: { return lcf == rhs.lcf; } sl@0: bool operator!=(const rand48& rhs) const sl@0: { return !(*this == rhs); } sl@0: #endif sl@0: private: sl@0: random::linear_congruential lcf; sl@0: static uint64_t cnv(int32_t x) sl@0: { return (static_cast(x) << 16) | 0x330e; } sl@0: }; sl@0: #endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */ sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP