|
1 /* boost random/additive_combine.hpp header file |
|
2 * |
|
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) |
|
7 * |
|
8 * See http://www.boost.org for most recent version including documentation. |
|
9 * |
|
10 * $Id: additive_combine.hpp,v 1.11 2005/05/21 15:57:00 dgregor Exp $ |
|
11 * |
|
12 * Revision history |
|
13 * 2001-02-18 moved to individual header files |
|
14 */ |
|
15 |
|
16 #ifndef BOOST_RANDOM_ADDITIVE_COMBINE_HPP |
|
17 #define BOOST_RANDOM_ADDITIVE_COMBINE_HPP |
|
18 |
|
19 #include <iostream> |
|
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> |
|
24 |
|
25 namespace boost { |
|
26 namespace random { |
|
27 |
|
28 // L'Ecuyer 1988 |
|
29 template<class MLCG1, class MLCG2, |
|
30 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS |
|
31 typename MLCG1::result_type |
|
32 #else |
|
33 int32_t |
|
34 #endif |
|
35 val> |
|
36 class additive_combine |
|
37 { |
|
38 public: |
|
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; |
|
46 #else |
|
47 enum { has_fixed_range = false }; |
|
48 #endif |
|
49 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 1; } |
|
50 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_mlcg1.max)()-1; } |
|
51 |
|
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) { } |
|
58 |
|
59 void seed() |
|
60 { |
|
61 _mlcg1.seed(); |
|
62 _mlcg2.seed(); |
|
63 } |
|
64 |
|
65 void seed(typename MLCG1::result_type seed1, |
|
66 typename MLCG2::result_type seed2) |
|
67 { |
|
68 _mlcg1(seed1); |
|
69 _mlcg2(seed2); |
|
70 } |
|
71 |
|
72 template<class It> void seed(It& first, It last) |
|
73 { |
|
74 _mlcg1.seed(first, last); |
|
75 _mlcg2.seed(first, last); |
|
76 } |
|
77 |
|
78 result_type operator()() { |
|
79 result_type z = _mlcg1() - _mlcg2(); |
|
80 if(z < 1) |
|
81 z += MLCG1::modulus-1; |
|
82 return z; |
|
83 } |
|
84 static bool validation(result_type x) { return val == x; } |
|
85 |
|
86 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE |
|
87 |
|
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; } |
|
93 |
|
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; } |
|
98 #endif |
|
99 |
|
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); } |
|
104 #else |
|
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); } |
|
110 #endif |
|
111 private: |
|
112 MLCG1 _mlcg1; |
|
113 MLCG2 _mlcg2; |
|
114 }; |
|
115 |
|
116 } // namespace random |
|
117 |
|
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; |
|
122 |
|
123 } // namespace boost |
|
124 |
|
125 #endif // BOOST_RANDOM_ADDITIVE_COMBINE_HPP |