1 /* boost random/additive_combine.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: additive_combine.hpp 58649 2010-01-02 21:23:17Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_ADDITIVE_COMBINE_HPP
\r
17 #define BOOST_RANDOM_ADDITIVE_COMBINE_HPP
\r
20 #include <algorithm> // for std::min and std::max
\r
21 #include <boost/config.hpp>
\r
22 #include <boost/cstdint.hpp>
\r
23 #include <boost/random/detail/config.hpp>
\r
24 #include <boost/random/linear_congruential.hpp>
\r
30 template<class MLCG1, class MLCG2,
\r
31 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
\r
32 typename MLCG1::result_type
\r
37 class additive_combine
\r
40 typedef MLCG1 first_base;
\r
41 typedef MLCG2 second_base;
\r
42 typedef typename MLCG1::result_type result_type;
\r
43 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
44 static const bool has_fixed_range = true;
\r
45 static const result_type min_value = 1;
\r
46 static const result_type max_value = MLCG1::max_value-1;
\r
48 enum { has_fixed_range = false };
\r
50 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 1; }
\r
51 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_mlcg1.max)()-1; }
\r
53 additive_combine() : _mlcg1(), _mlcg2() { }
\r
54 additive_combine(typename MLCG1::result_type seed1,
\r
55 typename MLCG2::result_type seed2)
\r
56 : _mlcg1(seed1), _mlcg2(seed2) { }
\r
57 additive_combine(result_type aseed)
\r
58 : _mlcg1(aseed), _mlcg2(aseed) { }
\r
59 template<class It> additive_combine(It& first, It last)
\r
60 : _mlcg1(first, last), _mlcg2(first, last) { }
\r
68 void seed(result_type aseed)
\r
74 void seed(typename MLCG1::result_type seed1,
\r
75 typename MLCG2::result_type seed2)
\r
81 template<class It> void seed(It& first, It last)
\r
83 _mlcg1.seed(first, last);
\r
84 _mlcg2.seed(first, last);
\r
87 result_type operator()() {
\r
88 result_type z = _mlcg1() - _mlcg2();
\r
90 z += MLCG1::modulus-1;
\r
93 static bool validation(result_type x) { return val == x; }
\r
95 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
\r
97 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
98 template<class CharT, class Traits>
\r
99 friend std::basic_ostream<CharT,Traits>&
\r
100 operator<<(std::basic_ostream<CharT,Traits>& os, const additive_combine& r)
\r
101 { os << r._mlcg1 << " " << r._mlcg2; return os; }
\r
103 template<class CharT, class Traits>
\r
104 friend std::basic_istream<CharT,Traits>&
\r
105 operator>>(std::basic_istream<CharT,Traits>& is, additive_combine& r)
\r
106 { is >> r._mlcg1 >> std::ws >> r._mlcg2; return is; }
\r
109 friend bool operator==(const additive_combine& x, const additive_combine& y)
\r
110 { return x._mlcg1 == y._mlcg1 && x._mlcg2 == y._mlcg2; }
\r
111 friend bool operator!=(const additive_combine& x, const additive_combine& y)
\r
112 { return !(x == y); }
\r
114 // Use a member function; Streamable concept not supported.
\r
115 bool operator==(const additive_combine& rhs) const
\r
116 { return _mlcg1 == rhs._mlcg1 && _mlcg2 == rhs._mlcg2; }
\r
117 bool operator!=(const additive_combine& rhs) const
\r
118 { return !(*this == rhs); }
\r
125 } // namespace random
\r
127 typedef random::additive_combine<
\r
128 random::linear_congruential<int32_t, 40014, 0, 2147483563, 0>,
\r
129 random::linear_congruential<int32_t, 40692, 0, 2147483399, 0>,
\r
130 2060321752> ecuyer1988;
\r
132 } // namespace boost
\r
134 #endif // BOOST_RANDOM_ADDITIVE_COMBINE_HPP
\r