sl@0: /* boost random/inversive_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: inversive_congruential.hpp,v 1.11 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_INVERSIVE_CONGRUENTIAL_HPP sl@0: #define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP sl@0: 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: // Eichenauer and Lehn 1986 sl@0: template sl@0: class inversive_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 = (b == 0 ? 1 : 0); sl@0: static const result_type max_value = p-1; sl@0: #else sl@0: BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); sl@0: #endif sl@0: BOOST_STATIC_CONSTANT(result_type, multiplier = a); sl@0: BOOST_STATIC_CONSTANT(result_type, increment = b); sl@0: BOOST_STATIC_CONSTANT(result_type, modulus = p); sl@0: sl@0: result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return b == 0 ? 1 : 0; } sl@0: result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return p-1; } sl@0: sl@0: explicit inversive_congruential(IntType y0 = 1) : value(y0) sl@0: { sl@0: BOOST_STATIC_ASSERT(b >= 0); sl@0: BOOST_STATIC_ASSERT(p > 1); sl@0: BOOST_STATIC_ASSERT(a >= 1); sl@0: if(b == 0) sl@0: assert(y0 > 0); sl@0: } sl@0: template inversive_congruential(It& first, It last) sl@0: { seed(first, last); } sl@0: sl@0: void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); } sl@0: template void seed(It& first, It last) sl@0: { sl@0: if(first == last) sl@0: throw std::invalid_argument("inversive_congruential::seed"); sl@0: value = *first++; sl@0: } sl@0: IntType operator()() sl@0: { sl@0: typedef const_mod do_mod; sl@0: value = do_mod::mult_add(a, do_mod::invert(value), b); sl@0: return value; sl@0: } sl@0: sl@0: bool validation(result_type x) const { return val == x; } 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, inversive_congruential x) sl@0: { os << x.value; return os; } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, inversive_congruential& x) sl@0: { is >> x.value; return is; } sl@0: #endif sl@0: sl@0: friend bool operator==(inversive_congruential x, inversive_congruential y) sl@0: { return x.value == y.value; } sl@0: friend bool operator!=(inversive_congruential x, inversive_congruential y) sl@0: { return !(x == y); } sl@0: #else sl@0: // Use a member function; Streamable concept not supported. sl@0: bool operator==(inversive_congruential rhs) const sl@0: { return value == rhs.value; } sl@0: bool operator!=(inversive_congruential rhs) const sl@0: { return !(*this == rhs); } sl@0: #endif sl@0: private: sl@0: IntType value; 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 inversive_congruential::has_fixed_range; sl@0: template sl@0: const typename inversive_congruential::result_type inversive_congruential::min_value; sl@0: template sl@0: const typename inversive_congruential::result_type inversive_congruential::max_value; sl@0: template sl@0: const typename inversive_congruential::result_type inversive_congruential::multiplier; sl@0: template sl@0: const typename inversive_congruential::result_type inversive_congruential::increment; sl@0: template sl@0: const typename inversive_congruential::result_type inversive_congruential::modulus; sl@0: #endif sl@0: sl@0: } // namespace random sl@0: sl@0: typedef random::inversive_congruential hellekalek1995; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP