1 /* boost random/additive_combine.hpp header file
3 * Copyright Jens Maurer 2000-2001
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: additive_combine.hpp,v 1.11 2005/05/21 15:57:00 dgregor Exp $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_ADDITIVE_COMBINE_HPP
17 #define BOOST_RANDOM_ADDITIVE_COMBINE_HPP
20 #include <algorithm> // for std::min and std::max
21 #include <boost/config.hpp>
22 #include <boost/cstdint.hpp>
23 #include <boost/random/linear_congruential.hpp>
29 template<class MLCG1, class MLCG2,
30 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
31 typename MLCG1::result_type
36 class additive_combine
39 typedef MLCG1 first_base;
40 typedef MLCG2 second_base;
41 typedef typename MLCG1::result_type result_type;
42 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
43 static const bool has_fixed_range = true;
44 static const result_type min_value = 1;
45 static const result_type max_value = MLCG1::max_value-1;
47 enum { has_fixed_range = false };
49 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 1; }
50 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_mlcg1.max)()-1; }
52 additive_combine() : _mlcg1(), _mlcg2() { }
53 additive_combine(typename MLCG1::result_type seed1,
54 typename MLCG2::result_type seed2)
55 : _mlcg1(seed1), _mlcg2(seed2) { }
56 template<class It> additive_combine(It& first, It last)
57 : _mlcg1(first, last), _mlcg2(first, last) { }
65 void seed(typename MLCG1::result_type seed1,
66 typename MLCG2::result_type seed2)
72 template<class It> void seed(It& first, It last)
74 _mlcg1.seed(first, last);
75 _mlcg2.seed(first, last);
78 result_type operator()() {
79 result_type z = _mlcg1() - _mlcg2();
81 z += MLCG1::modulus-1;
84 static bool validation(result_type x) { return val == x; }
86 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
88 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
89 template<class CharT, class Traits>
90 friend std::basic_ostream<CharT,Traits>&
91 operator<<(std::basic_ostream<CharT,Traits>& os, const additive_combine& r)
92 { os << r._mlcg1 << " " << r._mlcg2; return os; }
94 template<class CharT, class Traits>
95 friend std::basic_istream<CharT,Traits>&
96 operator>>(std::basic_istream<CharT,Traits>& is, additive_combine& r)
97 { is >> r._mlcg1 >> std::ws >> r._mlcg2; return is; }
100 friend bool operator==(const additive_combine& x, const additive_combine& y)
101 { return x._mlcg1 == y._mlcg1 && x._mlcg2 == y._mlcg2; }
102 friend bool operator!=(const additive_combine& x, const additive_combine& y)
103 { return !(x == y); }
105 // Use a member function; Streamable concept not supported.
106 bool operator==(const additive_combine& rhs) const
107 { return _mlcg1 == rhs._mlcg1 && _mlcg2 == rhs._mlcg2; }
108 bool operator!=(const additive_combine& rhs) const
109 { return !(*this == rhs); }
116 } // namespace random
118 typedef random::additive_combine<
119 random::linear_congruential<int32_t, 40014, 0, 2147483563, 0>,
120 random::linear_congruential<int32_t, 40692, 0, 2147483399, 0>,
121 2060321752> ecuyer1988;
125 #endif // BOOST_RANDOM_ADDITIVE_COMBINE_HPP