sl@0: /* boost random/additive_combine.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: additive_combine.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_ADDITIVE_COMBINE_HPP sl@0: #define BOOST_RANDOM_ADDITIVE_COMBINE_HPP sl@0: sl@0: #include sl@0: #include // for std::min and std::max sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: namespace random { sl@0: sl@0: // L'Ecuyer 1988 sl@0: template sl@0: class additive_combine sl@0: { sl@0: public: sl@0: typedef MLCG1 first_base; sl@0: typedef MLCG2 second_base; sl@0: typedef typename MLCG1::result_type 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 = 1; sl@0: static const result_type max_value = MLCG1::max_value-1; sl@0: #else sl@0: enum { has_fixed_range = false }; sl@0: #endif sl@0: result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 1; } sl@0: result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_mlcg1.max)()-1; } sl@0: sl@0: additive_combine() : _mlcg1(), _mlcg2() { } sl@0: additive_combine(typename MLCG1::result_type seed1, sl@0: typename MLCG2::result_type seed2) sl@0: : _mlcg1(seed1), _mlcg2(seed2) { } sl@0: template additive_combine(It& first, It last) sl@0: : _mlcg1(first, last), _mlcg2(first, last) { } sl@0: sl@0: void seed() sl@0: { sl@0: _mlcg1.seed(); sl@0: _mlcg2.seed(); sl@0: } sl@0: sl@0: void seed(typename MLCG1::result_type seed1, sl@0: typename MLCG2::result_type seed2) sl@0: { sl@0: _mlcg1(seed1); sl@0: _mlcg2(seed2); sl@0: } sl@0: sl@0: template void seed(It& first, It last) sl@0: { sl@0: _mlcg1.seed(first, last); sl@0: _mlcg2.seed(first, last); sl@0: } sl@0: sl@0: result_type operator()() { sl@0: result_type z = _mlcg1() - _mlcg2(); sl@0: if(z < 1) sl@0: z += MLCG1::modulus-1; sl@0: return z; sl@0: } sl@0: static bool validation(result_type x) { 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, const additive_combine& r) sl@0: { os << r._mlcg1 << " " << r._mlcg2; return os; } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, additive_combine& r) sl@0: { is >> r._mlcg1 >> std::ws >> r._mlcg2; return is; } sl@0: #endif sl@0: sl@0: friend bool operator==(const additive_combine& x, const additive_combine& y) sl@0: { return x._mlcg1 == y._mlcg1 && x._mlcg2 == y._mlcg2; } sl@0: friend bool operator!=(const additive_combine& x, const additive_combine& y) sl@0: { return !(x == y); } sl@0: #else sl@0: // Use a member function; Streamable concept not supported. sl@0: bool operator==(const additive_combine& rhs) const sl@0: { return _mlcg1 == rhs._mlcg1 && _mlcg2 == rhs._mlcg2; } sl@0: bool operator!=(const additive_combine& rhs) const sl@0: { return !(*this == rhs); } sl@0: #endif sl@0: private: sl@0: MLCG1 _mlcg1; sl@0: MLCG2 _mlcg2; sl@0: }; sl@0: sl@0: } // namespace random sl@0: sl@0: typedef random::additive_combine< sl@0: random::linear_congruential, sl@0: random::linear_congruential, sl@0: 2060321752> ecuyer1988; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_ADDITIVE_COMBINE_HPP