sl@0
|
1 |
// (C) Copyright John Maddock 2005.
|
sl@0
|
2 |
// (C) Copyright Henry S. Warren 2005.
|
sl@0
|
3 |
// Use, modification and distribution are subject to the
|
sl@0
|
4 |
// Boost Software License, Version 1.0. (See accompanying file
|
sl@0
|
5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
6 |
|
sl@0
|
7 |
#ifndef BOOST_TR1_RANDOM_HPP_INCLUDED
|
sl@0
|
8 |
# define BOOST_TR1_RANDOM_HPP_INCLUDED
|
sl@0
|
9 |
# include <boost/tr1/detail/config.hpp>
|
sl@0
|
10 |
|
sl@0
|
11 |
#ifdef BOOST_HAS_TR1_RANDOM
|
sl@0
|
12 |
# include BOOST_TR1_HEADER(random)
|
sl@0
|
13 |
#else
|
sl@0
|
14 |
// Boost.Random:
|
sl@0
|
15 |
#include <boost/random.hpp>
|
sl@0
|
16 |
#ifndef __SUNPRO_CC
|
sl@0
|
17 |
// Sunpros linker complains if we so much as include this...
|
sl@0
|
18 |
# include <boost/nondet_random.hpp>
|
sl@0
|
19 |
#endif
|
sl@0
|
20 |
#include <boost/tr1/detail/functor2iterator.hpp>
|
sl@0
|
21 |
#include <boost/type_traits/is_fundamental.hpp>
|
sl@0
|
22 |
#include <boost/type_traits/is_same.hpp>
|
sl@0
|
23 |
|
sl@0
|
24 |
namespace std { namespace tr1{
|
sl@0
|
25 |
|
sl@0
|
26 |
using ::boost::variate_generator;
|
sl@0
|
27 |
|
sl@0
|
28 |
template<class UIntType, UIntType a, UIntType c, UIntType m>
|
sl@0
|
29 |
class linear_congruential
|
sl@0
|
30 |
{
|
sl@0
|
31 |
private:
|
sl@0
|
32 |
typedef ::boost::random::linear_congruential<UIntType, a, c, m, 0> impl_type;
|
sl@0
|
33 |
public:
|
sl@0
|
34 |
// types
|
sl@0
|
35 |
typedef UIntType result_type;
|
sl@0
|
36 |
// parameter values
|
sl@0
|
37 |
BOOST_STATIC_CONSTANT(UIntType, multiplier = a);
|
sl@0
|
38 |
BOOST_STATIC_CONSTANT(UIntType, increment = c);
|
sl@0
|
39 |
BOOST_STATIC_CONSTANT(UIntType, modulus = m);
|
sl@0
|
40 |
// constructors and member function
|
sl@0
|
41 |
explicit linear_congruential(unsigned long x0 = 1)
|
sl@0
|
42 |
: m_gen(x0){}
|
sl@0
|
43 |
linear_congruential(const linear_congruential& that)
|
sl@0
|
44 |
: m_gen(that.m_gen){}
|
sl@0
|
45 |
template<class Gen> linear_congruential(Gen& g)
|
sl@0
|
46 |
{
|
sl@0
|
47 |
init1(g, ::boost::is_same<Gen,linear_congruential>());
|
sl@0
|
48 |
}
|
sl@0
|
49 |
void seed(unsigned long x0 = 1)
|
sl@0
|
50 |
{ m_gen.seed(x0); }
|
sl@0
|
51 |
template<class Gen> void seed(Gen& g)
|
sl@0
|
52 |
{
|
sl@0
|
53 |
init2(g, ::boost::is_fundamental<Gen>());
|
sl@0
|
54 |
}
|
sl@0
|
55 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
56 |
{ return (m_gen.min)(); }
|
sl@0
|
57 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
58 |
{ return (m_gen.max)(); }
|
sl@0
|
59 |
result_type operator()()
|
sl@0
|
60 |
{
|
sl@0
|
61 |
return m_gen();
|
sl@0
|
62 |
}
|
sl@0
|
63 |
bool operator==(const linear_congruential& that)const
|
sl@0
|
64 |
{ return m_gen == that.m_gen; }
|
sl@0
|
65 |
bool operator!=(const linear_congruential& that)const
|
sl@0
|
66 |
{ return m_gen != that.m_gen; }
|
sl@0
|
67 |
|
sl@0
|
68 |
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
sl@0
|
69 |
template<class CharT, class Traits>
|
sl@0
|
70 |
friend std::basic_ostream<CharT,Traits>&
|
sl@0
|
71 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
sl@0
|
72 |
const linear_congruential& lcg)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
return os << lcg.m_gen;
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
template<class CharT, class Traits>
|
sl@0
|
78 |
friend std::basic_istream<CharT,Traits>&
|
sl@0
|
79 |
operator>>(std::basic_istream<CharT,Traits>& is,
|
sl@0
|
80 |
linear_congruential& lcg)
|
sl@0
|
81 |
{
|
sl@0
|
82 |
return is >> lcg.m_gen;
|
sl@0
|
83 |
}
|
sl@0
|
84 |
#endif
|
sl@0
|
85 |
|
sl@0
|
86 |
private:
|
sl@0
|
87 |
template <class Gen>
|
sl@0
|
88 |
void init1(Gen& g, const ::boost::true_type&)
|
sl@0
|
89 |
{
|
sl@0
|
90 |
m_gen = g.m_gen;
|
sl@0
|
91 |
}
|
sl@0
|
92 |
template <class Gen>
|
sl@0
|
93 |
void init1(Gen& g, const ::boost::false_type&)
|
sl@0
|
94 |
{
|
sl@0
|
95 |
init2(g, ::boost::is_fundamental<Gen>());
|
sl@0
|
96 |
}
|
sl@0
|
97 |
template <class Gen>
|
sl@0
|
98 |
void init2(Gen& g, const ::boost::true_type&)
|
sl@0
|
99 |
{
|
sl@0
|
100 |
m_gen.seed(static_cast<unsigned long>(g));
|
sl@0
|
101 |
}
|
sl@0
|
102 |
template <class Gen>
|
sl@0
|
103 |
void init2(Gen& g, const ::boost::false_type&)
|
sl@0
|
104 |
{
|
sl@0
|
105 |
//typedef typename Gen::result_type gen_rt;
|
sl@0
|
106 |
boost::tr1_details::functor2iterator<Gen, unsigned long> f1(g), f2;
|
sl@0
|
107 |
m_gen.seed(f1, f2);
|
sl@0
|
108 |
}
|
sl@0
|
109 |
impl_type m_gen;
|
sl@0
|
110 |
};
|
sl@0
|
111 |
|
sl@0
|
112 |
template<class UIntType, int w, int n, int m, int r,
|
sl@0
|
113 |
UIntType a, int u, int s, UIntType b, int t, UIntType c, int l>
|
sl@0
|
114 |
class mersenne_twister
|
sl@0
|
115 |
{
|
sl@0
|
116 |
typedef ::boost::random::mersenne_twister
|
sl@0
|
117 |
<UIntType, w, n, m, r, a, u, s, b, t, c, l, 0> imp_type;
|
sl@0
|
118 |
public:
|
sl@0
|
119 |
// types
|
sl@0
|
120 |
typedef UIntType result_type;
|
sl@0
|
121 |
// parameter values
|
sl@0
|
122 |
BOOST_STATIC_CONSTANT(int, word_size = w);
|
sl@0
|
123 |
BOOST_STATIC_CONSTANT(int, state_size = n);
|
sl@0
|
124 |
BOOST_STATIC_CONSTANT(int, shift_size = m);
|
sl@0
|
125 |
BOOST_STATIC_CONSTANT(int, mask_bits = r);
|
sl@0
|
126 |
BOOST_STATIC_CONSTANT(UIntType, parameter_a = a);
|
sl@0
|
127 |
BOOST_STATIC_CONSTANT(int, output_u = u);
|
sl@0
|
128 |
BOOST_STATIC_CONSTANT(int, output_s = s);
|
sl@0
|
129 |
BOOST_STATIC_CONSTANT(UIntType, output_b = b);
|
sl@0
|
130 |
BOOST_STATIC_CONSTANT(int, output_t = t);
|
sl@0
|
131 |
BOOST_STATIC_CONSTANT(UIntType, output_c = c);
|
sl@0
|
132 |
BOOST_STATIC_CONSTANT(int, output_l = l);
|
sl@0
|
133 |
// constructors and member function
|
sl@0
|
134 |
mersenne_twister(){}
|
sl@0
|
135 |
explicit mersenne_twister(unsigned long value)
|
sl@0
|
136 |
: m_gen(value == 0 ? 4357UL : value){}
|
sl@0
|
137 |
template<class Gen> mersenne_twister(Gen& g)
|
sl@0
|
138 |
{
|
sl@0
|
139 |
init1(g, ::boost::is_same<mersenne_twister,Gen>());
|
sl@0
|
140 |
}
|
sl@0
|
141 |
void seed()
|
sl@0
|
142 |
{ m_gen.seed(); }
|
sl@0
|
143 |
void seed(unsigned long value)
|
sl@0
|
144 |
{ m_gen.seed(value == 0 ? 5489UL : value); }
|
sl@0
|
145 |
template<class Gen> void seed(Gen& g)
|
sl@0
|
146 |
{ init2(g, ::boost::is_fundamental<Gen>()); }
|
sl@0
|
147 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
148 |
{ return (m_gen.min)(); }
|
sl@0
|
149 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
150 |
{ return (m_gen.max)(); }
|
sl@0
|
151 |
result_type operator()()
|
sl@0
|
152 |
{ return m_gen(); }
|
sl@0
|
153 |
bool operator==(const mersenne_twister& that)const
|
sl@0
|
154 |
{ return m_gen == that.m_gen; }
|
sl@0
|
155 |
bool operator!=(const mersenne_twister& that)const
|
sl@0
|
156 |
{ return m_gen != that.m_gen; }
|
sl@0
|
157 |
|
sl@0
|
158 |
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
sl@0
|
159 |
template<class CharT, class Traits>
|
sl@0
|
160 |
friend std::basic_ostream<CharT,Traits>&
|
sl@0
|
161 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
sl@0
|
162 |
const mersenne_twister& lcg)
|
sl@0
|
163 |
{
|
sl@0
|
164 |
return os << lcg.m_gen;
|
sl@0
|
165 |
}
|
sl@0
|
166 |
|
sl@0
|
167 |
template<class CharT, class Traits>
|
sl@0
|
168 |
friend std::basic_istream<CharT,Traits>&
|
sl@0
|
169 |
operator>>(std::basic_istream<CharT,Traits>& is,
|
sl@0
|
170 |
mersenne_twister& lcg)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
return is >> lcg.m_gen;
|
sl@0
|
173 |
}
|
sl@0
|
174 |
#endif
|
sl@0
|
175 |
private:
|
sl@0
|
176 |
template <class Gen>
|
sl@0
|
177 |
void init1(Gen& g, const ::boost::true_type&)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
m_gen = g.m_gen;
|
sl@0
|
180 |
}
|
sl@0
|
181 |
template <class Gen>
|
sl@0
|
182 |
void init1(Gen& g, const ::boost::false_type&)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
init2(g, ::boost::is_fundamental<Gen>());
|
sl@0
|
185 |
}
|
sl@0
|
186 |
template <class Gen>
|
sl@0
|
187 |
void init2(Gen& g, const ::boost::true_type&)
|
sl@0
|
188 |
{
|
sl@0
|
189 |
m_gen.seed(static_cast<unsigned long>(g == 0 ? 4357UL : g));
|
sl@0
|
190 |
}
|
sl@0
|
191 |
template <class Gen>
|
sl@0
|
192 |
void init2(Gen& g, const ::boost::false_type&)
|
sl@0
|
193 |
{
|
sl@0
|
194 |
m_gen.seed(g);
|
sl@0
|
195 |
}
|
sl@0
|
196 |
imp_type m_gen;
|
sl@0
|
197 |
};
|
sl@0
|
198 |
|
sl@0
|
199 |
template<class IntType, IntType m, int s, int r>
|
sl@0
|
200 |
class subtract_with_carry
|
sl@0
|
201 |
{
|
sl@0
|
202 |
public:
|
sl@0
|
203 |
// types
|
sl@0
|
204 |
typedef IntType result_type;
|
sl@0
|
205 |
// parameter values
|
sl@0
|
206 |
BOOST_STATIC_CONSTANT(IntType, modulus = m);
|
sl@0
|
207 |
BOOST_STATIC_CONSTANT(int, long_lag = r);
|
sl@0
|
208 |
BOOST_STATIC_CONSTANT(int, short_lag = s);
|
sl@0
|
209 |
|
sl@0
|
210 |
// constructors and member function
|
sl@0
|
211 |
subtract_with_carry(){}
|
sl@0
|
212 |
explicit subtract_with_carry(unsigned long value)
|
sl@0
|
213 |
: m_gen(value == 0 ? 19780503UL : value){}
|
sl@0
|
214 |
template<class Gen> subtract_with_carry(Gen& g)
|
sl@0
|
215 |
{ init1(g, ::boost::is_same<Gen, subtract_with_carry<IntType, m, s, r> >()); }
|
sl@0
|
216 |
void seed(unsigned long value = 19780503ul)
|
sl@0
|
217 |
{ m_gen.seed(value == 0 ? 19780503UL : value); }
|
sl@0
|
218 |
template<class Gen> void seed(Gen& g)
|
sl@0
|
219 |
{ init2(g, ::boost::is_fundamental<Gen>()); }
|
sl@0
|
220 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
221 |
{ return (m_gen.min)(); }
|
sl@0
|
222 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
223 |
{ return (m_gen.max)(); }
|
sl@0
|
224 |
result_type operator()()
|
sl@0
|
225 |
{ return m_gen(); }
|
sl@0
|
226 |
bool operator==(const subtract_with_carry& that)const
|
sl@0
|
227 |
{ return m_gen == that.m_gen; }
|
sl@0
|
228 |
bool operator!=(const subtract_with_carry& that)const
|
sl@0
|
229 |
{ return m_gen != that.m_gen; }
|
sl@0
|
230 |
|
sl@0
|
231 |
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
sl@0
|
232 |
template<class CharT, class Traits>
|
sl@0
|
233 |
friend std::basic_ostream<CharT,Traits>&
|
sl@0
|
234 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
sl@0
|
235 |
const subtract_with_carry& lcg)
|
sl@0
|
236 |
{
|
sl@0
|
237 |
return os << lcg.m_gen;
|
sl@0
|
238 |
}
|
sl@0
|
239 |
|
sl@0
|
240 |
template<class CharT, class Traits>
|
sl@0
|
241 |
friend std::basic_istream<CharT,Traits>&
|
sl@0
|
242 |
operator>>(std::basic_istream<CharT,Traits>& is,
|
sl@0
|
243 |
subtract_with_carry& lcg)
|
sl@0
|
244 |
{
|
sl@0
|
245 |
return is >> lcg.m_gen;
|
sl@0
|
246 |
}
|
sl@0
|
247 |
#endif
|
sl@0
|
248 |
private:
|
sl@0
|
249 |
template <class Gen>
|
sl@0
|
250 |
void init1(Gen& g, const ::boost::true_type&)
|
sl@0
|
251 |
{
|
sl@0
|
252 |
m_gen = g.m_gen;
|
sl@0
|
253 |
}
|
sl@0
|
254 |
template <class Gen>
|
sl@0
|
255 |
void init1(Gen& g, const ::boost::false_type&)
|
sl@0
|
256 |
{
|
sl@0
|
257 |
init2(g, ::boost::is_fundamental<Gen>());
|
sl@0
|
258 |
}
|
sl@0
|
259 |
template <class Gen>
|
sl@0
|
260 |
void init2(Gen& g, const ::boost::true_type&)
|
sl@0
|
261 |
{
|
sl@0
|
262 |
m_gen.seed(static_cast<unsigned long>(g == 0 ? 19780503UL : g));
|
sl@0
|
263 |
}
|
sl@0
|
264 |
template <class Gen>
|
sl@0
|
265 |
void init2(Gen& g, const ::boost::false_type&)
|
sl@0
|
266 |
{
|
sl@0
|
267 |
m_gen.seed(g);
|
sl@0
|
268 |
}
|
sl@0
|
269 |
::boost::random::subtract_with_carry<IntType, m, s, r, 0> m_gen;
|
sl@0
|
270 |
};
|
sl@0
|
271 |
|
sl@0
|
272 |
template<class RealType, int w, int s, int r>
|
sl@0
|
273 |
class subtract_with_carry_01
|
sl@0
|
274 |
{
|
sl@0
|
275 |
public:
|
sl@0
|
276 |
// types
|
sl@0
|
277 |
typedef RealType result_type;
|
sl@0
|
278 |
// parameter values
|
sl@0
|
279 |
BOOST_STATIC_CONSTANT(int, word_size = w);
|
sl@0
|
280 |
BOOST_STATIC_CONSTANT(int, long_lag = r);
|
sl@0
|
281 |
BOOST_STATIC_CONSTANT(int, short_lag = s);
|
sl@0
|
282 |
|
sl@0
|
283 |
// constructors and member function
|
sl@0
|
284 |
subtract_with_carry_01(){}
|
sl@0
|
285 |
explicit subtract_with_carry_01(unsigned long value)
|
sl@0
|
286 |
: m_gen(value == 0 ? 19780503UL : value){}
|
sl@0
|
287 |
template<class Gen> subtract_with_carry_01(Gen& g)
|
sl@0
|
288 |
{ init1(g, ::boost::is_same<Gen, subtract_with_carry_01<RealType, w, s, r> >()); }
|
sl@0
|
289 |
void seed(unsigned long value = 19780503UL)
|
sl@0
|
290 |
{ m_gen.seed(value == 0 ? 19780503UL : value); }
|
sl@0
|
291 |
template<class Gen> void seed(Gen& g)
|
sl@0
|
292 |
{ init2(g, ::boost::is_fundamental<Gen>()); }
|
sl@0
|
293 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
294 |
{ return (m_gen.min)(); }
|
sl@0
|
295 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
296 |
{ return (m_gen.max)(); }
|
sl@0
|
297 |
result_type operator()()
|
sl@0
|
298 |
{ return m_gen(); }
|
sl@0
|
299 |
bool operator==(const subtract_with_carry_01& that)const
|
sl@0
|
300 |
{ return m_gen == that.m_gen; }
|
sl@0
|
301 |
bool operator!=(const subtract_with_carry_01& that)const
|
sl@0
|
302 |
{ return m_gen != that.m_gen; }
|
sl@0
|
303 |
|
sl@0
|
304 |
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
sl@0
|
305 |
template<class CharT, class Traits>
|
sl@0
|
306 |
friend std::basic_ostream<CharT,Traits>&
|
sl@0
|
307 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
sl@0
|
308 |
const subtract_with_carry_01& lcg)
|
sl@0
|
309 |
{
|
sl@0
|
310 |
return os << lcg.m_gen;
|
sl@0
|
311 |
}
|
sl@0
|
312 |
|
sl@0
|
313 |
template<class CharT, class Traits>
|
sl@0
|
314 |
friend std::basic_istream<CharT,Traits>&
|
sl@0
|
315 |
operator>>(std::basic_istream<CharT,Traits>& is,
|
sl@0
|
316 |
subtract_with_carry_01& lcg)
|
sl@0
|
317 |
{
|
sl@0
|
318 |
return is >> lcg.m_gen;
|
sl@0
|
319 |
}
|
sl@0
|
320 |
#endif
|
sl@0
|
321 |
private:
|
sl@0
|
322 |
template <class Gen>
|
sl@0
|
323 |
void init1(Gen& g, const ::boost::true_type&)
|
sl@0
|
324 |
{
|
sl@0
|
325 |
m_gen = g.m_gen;
|
sl@0
|
326 |
}
|
sl@0
|
327 |
template <class Gen>
|
sl@0
|
328 |
void init1(Gen& g, const ::boost::false_type&)
|
sl@0
|
329 |
{
|
sl@0
|
330 |
init2(g, ::boost::is_fundamental<Gen>());
|
sl@0
|
331 |
}
|
sl@0
|
332 |
template <class Gen>
|
sl@0
|
333 |
void init2(Gen& g, const ::boost::true_type&)
|
sl@0
|
334 |
{
|
sl@0
|
335 |
m_gen.seed(static_cast<unsigned long>(g == 0 ? 19780503UL : g));
|
sl@0
|
336 |
}
|
sl@0
|
337 |
template <class Gen>
|
sl@0
|
338 |
void init2(Gen& g, const ::boost::false_type&)
|
sl@0
|
339 |
{
|
sl@0
|
340 |
//typedef typename Gen::result_type gen_rt;
|
sl@0
|
341 |
boost::tr1_details::functor2iterator<Gen, unsigned long> f1(g), f2;
|
sl@0
|
342 |
m_gen.seed(f1, f2);
|
sl@0
|
343 |
}
|
sl@0
|
344 |
::boost::random::subtract_with_carry_01<RealType, w, s, r, 0> m_gen;
|
sl@0
|
345 |
};
|
sl@0
|
346 |
|
sl@0
|
347 |
using ::boost::random::discard_block;
|
sl@0
|
348 |
|
sl@0
|
349 |
template<class UniformRandomNumberGenerator1, int s1, class UniformRandomNumberGenerator2, int s2>
|
sl@0
|
350 |
class xor_combine
|
sl@0
|
351 |
{
|
sl@0
|
352 |
public:
|
sl@0
|
353 |
// types
|
sl@0
|
354 |
typedef UniformRandomNumberGenerator1 base1_type;
|
sl@0
|
355 |
typedef UniformRandomNumberGenerator2 base2_type;
|
sl@0
|
356 |
typedef unsigned long result_type;
|
sl@0
|
357 |
// parameter values
|
sl@0
|
358 |
BOOST_STATIC_CONSTANT(int, shift1 = s1);
|
sl@0
|
359 |
BOOST_STATIC_CONSTANT(int, shift2 = s2);
|
sl@0
|
360 |
// constructors and member function
|
sl@0
|
361 |
xor_combine(){ init_minmax(); }
|
sl@0
|
362 |
xor_combine(const base1_type & rng1, const base2_type & rng2)
|
sl@0
|
363 |
: m_b1(rng1), m_b2(rng2) { init_minmax(); }
|
sl@0
|
364 |
xor_combine(unsigned long s)
|
sl@0
|
365 |
: m_b1(s), m_b2(s+1) { init_minmax(); }
|
sl@0
|
366 |
template<class Gen> xor_combine(Gen& g)
|
sl@0
|
367 |
{
|
sl@0
|
368 |
init_minmax();
|
sl@0
|
369 |
init1(g, ::boost::is_same<Gen, xor_combine<UniformRandomNumberGenerator1, s1, UniformRandomNumberGenerator2, s2> >());
|
sl@0
|
370 |
}
|
sl@0
|
371 |
void seed()
|
sl@0
|
372 |
{
|
sl@0
|
373 |
m_b1.seed();
|
sl@0
|
374 |
m_b2.seed();
|
sl@0
|
375 |
}
|
sl@0
|
376 |
void seed(unsigned long s)
|
sl@0
|
377 |
{
|
sl@0
|
378 |
m_b1.seed(s);
|
sl@0
|
379 |
m_b2.seed(s+1);
|
sl@0
|
380 |
}
|
sl@0
|
381 |
template<class Gen> void seed(Gen& g)
|
sl@0
|
382 |
{
|
sl@0
|
383 |
init2(g, ::boost::is_fundamental<Gen>());
|
sl@0
|
384 |
}
|
sl@0
|
385 |
|
sl@0
|
386 |
const base1_type& base1() const
|
sl@0
|
387 |
{ return m_b1; }
|
sl@0
|
388 |
const base2_type& base2() const
|
sl@0
|
389 |
{ return m_b2; }
|
sl@0
|
390 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
391 |
{ return m_min; }
|
sl@0
|
392 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
|
sl@0
|
393 |
{ return m_max; }
|
sl@0
|
394 |
result_type operator()()
|
sl@0
|
395 |
{ return (m_b1() << s1) ^ (m_b2() << s2); }
|
sl@0
|
396 |
|
sl@0
|
397 |
bool operator == (const xor_combine& that)const
|
sl@0
|
398 |
{ return (m_b1 == that.m_b1) && (m_b2 == that.m_b2); }
|
sl@0
|
399 |
bool operator != (const xor_combine& that)const
|
sl@0
|
400 |
{ return !(*this == that); }
|
sl@0
|
401 |
|
sl@0
|
402 |
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
sl@0
|
403 |
template<class CharT, class Traits>
|
sl@0
|
404 |
friend std::basic_ostream<CharT,Traits>&
|
sl@0
|
405 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
sl@0
|
406 |
const xor_combine& lcg)
|
sl@0
|
407 |
{
|
sl@0
|
408 |
return os << lcg.m_b1 << " " << lcg.m_b2;
|
sl@0
|
409 |
}
|
sl@0
|
410 |
|
sl@0
|
411 |
template<class CharT, class Traits>
|
sl@0
|
412 |
friend std::basic_istream<CharT,Traits>&
|
sl@0
|
413 |
operator>>(std::basic_istream<CharT,Traits>& is,
|
sl@0
|
414 |
xor_combine& lcg)
|
sl@0
|
415 |
{
|
sl@0
|
416 |
return is >> lcg.m_b1 >> lcg.m_b2;
|
sl@0
|
417 |
}
|
sl@0
|
418 |
#endif
|
sl@0
|
419 |
|
sl@0
|
420 |
private:
|
sl@0
|
421 |
void init_minmax();
|
sl@0
|
422 |
base1_type m_b1;
|
sl@0
|
423 |
base2_type m_b2;
|
sl@0
|
424 |
result_type m_min;
|
sl@0
|
425 |
result_type m_max;
|
sl@0
|
426 |
|
sl@0
|
427 |
template <class Gen>
|
sl@0
|
428 |
void init1(Gen& g, const ::boost::true_type&)
|
sl@0
|
429 |
{
|
sl@0
|
430 |
m_b1 = g.m_b1;
|
sl@0
|
431 |
m_b2 = g.m_b2;
|
sl@0
|
432 |
}
|
sl@0
|
433 |
template <class Gen>
|
sl@0
|
434 |
void init1(Gen& g, const ::boost::false_type&)
|
sl@0
|
435 |
{
|
sl@0
|
436 |
init2(g, ::boost::is_fundamental<Gen>());
|
sl@0
|
437 |
}
|
sl@0
|
438 |
template <class Gen>
|
sl@0
|
439 |
void init2(Gen& g, const ::boost::true_type&)
|
sl@0
|
440 |
{
|
sl@0
|
441 |
m_b1.seed(static_cast<unsigned long>(g));
|
sl@0
|
442 |
m_b2.seed(static_cast<unsigned long>(g));
|
sl@0
|
443 |
}
|
sl@0
|
444 |
template <class Gen>
|
sl@0
|
445 |
void init2(Gen& g, const ::boost::false_type&)
|
sl@0
|
446 |
{
|
sl@0
|
447 |
m_b1.seed(g);
|
sl@0
|
448 |
m_b2.seed(g);
|
sl@0
|
449 |
}
|
sl@0
|
450 |
};
|
sl@0
|
451 |
|
sl@0
|
452 |
template<class UniformRandomNumberGenerator1, int s1, class UniformRandomNumberGenerator2, int s2>
|
sl@0
|
453 |
void xor_combine<UniformRandomNumberGenerator1, s1, UniformRandomNumberGenerator2, s2>::init_minmax()
|
sl@0
|
454 |
{
|
sl@0
|
455 |
//
|
sl@0
|
456 |
// The following code is based on that given in "Hacker's Delight"
|
sl@0
|
457 |
// by Henry S. Warren, (Addison-Wesley, 2003), and at
|
sl@0
|
458 |
// http://www.hackersdelight.org/index.htm.
|
sl@0
|
459 |
// Used here by permission.
|
sl@0
|
460 |
//
|
sl@0
|
461 |
// calculation of minimum value:
|
sl@0
|
462 |
//
|
sl@0
|
463 |
result_type a = (m_b1.min)() << s1;
|
sl@0
|
464 |
result_type b = (m_b1.max)() << s1;
|
sl@0
|
465 |
result_type c = (m_b2.min)() << s2;
|
sl@0
|
466 |
result_type d = (m_b2.max)() << s2;
|
sl@0
|
467 |
result_type m, temp;
|
sl@0
|
468 |
|
sl@0
|
469 |
m = 0x1uL << ((sizeof(result_type) * CHAR_BIT) - 1);
|
sl@0
|
470 |
while (m != 0) {
|
sl@0
|
471 |
if (~a & c & m) {
|
sl@0
|
472 |
temp = (a | m) & (static_cast<result_type>(0u) - m);
|
sl@0
|
473 |
if (temp <= b) a = temp;
|
sl@0
|
474 |
}
|
sl@0
|
475 |
else if (a & ~c & m) {
|
sl@0
|
476 |
temp = (c | m) & (static_cast<result_type>(0u) - m);
|
sl@0
|
477 |
if (temp <= d) c = temp;
|
sl@0
|
478 |
}
|
sl@0
|
479 |
m >>= 1;
|
sl@0
|
480 |
}
|
sl@0
|
481 |
m_min = a ^ c;
|
sl@0
|
482 |
|
sl@0
|
483 |
//
|
sl@0
|
484 |
// calculation of maximum value:
|
sl@0
|
485 |
//
|
sl@0
|
486 |
if((((std::numeric_limits<result_type>::max)() >> s1) < (m_b1.max)())
|
sl@0
|
487 |
|| ((((std::numeric_limits<result_type>::max)()) >> s2) < (m_b2.max)()))
|
sl@0
|
488 |
{
|
sl@0
|
489 |
m_max = (std::numeric_limits<result_type>::max)();
|
sl@0
|
490 |
return;
|
sl@0
|
491 |
}
|
sl@0
|
492 |
a = (m_b1.min)() << s1;
|
sl@0
|
493 |
b = (m_b1.max)() << s1;
|
sl@0
|
494 |
c = (m_b2.min)() << s2;
|
sl@0
|
495 |
d = (m_b2.max)() << s2;
|
sl@0
|
496 |
|
sl@0
|
497 |
m = 0x1uL << ((sizeof(result_type) * CHAR_BIT) - 1);
|
sl@0
|
498 |
|
sl@0
|
499 |
while (m != 0) {
|
sl@0
|
500 |
if (b & d & m) {
|
sl@0
|
501 |
temp = (b - m) | (m - 1);
|
sl@0
|
502 |
if (temp >= a) b = temp;
|
sl@0
|
503 |
else {
|
sl@0
|
504 |
temp = (d - m) | (m - 1);
|
sl@0
|
505 |
if (temp >= c) d = temp;
|
sl@0
|
506 |
}
|
sl@0
|
507 |
}
|
sl@0
|
508 |
m = m >> 1;
|
sl@0
|
509 |
}
|
sl@0
|
510 |
m_max = b ^ d;
|
sl@0
|
511 |
}
|
sl@0
|
512 |
|
sl@0
|
513 |
typedef linear_congruential< ::boost::int32_t, 16807, 0, 2147483647> minstd_rand0;
|
sl@0
|
514 |
typedef linear_congruential< ::boost::int32_t, 48271, 0, 2147483647> minstd_rand;
|
sl@0
|
515 |
typedef mersenne_twister< ::boost::uint32_t, 32,624,397,31,0x9908b0df,11,7,0x9d2c5680,15,0xefc60000,18> mt19937;
|
sl@0
|
516 |
typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
|
sl@0
|
517 |
typedef subtract_with_carry_01<double, 48, 10, 24> ranlux64_base_01;
|
sl@0
|
518 |
typedef discard_block<subtract_with_carry< ::boost::int32_t, (1<<24), 10, 24>, 223, 24> ranlux3;
|
sl@0
|
519 |
typedef discard_block<subtract_with_carry< ::boost::int32_t, (1<<24), 10, 24>, 389, 24> ranlux4;
|
sl@0
|
520 |
typedef discard_block<subtract_with_carry_01<float, 24, 10, 24>, 223, 24> ranlux3_01;
|
sl@0
|
521 |
typedef discard_block<subtract_with_carry_01<float, 24, 10, 24>, 389, 24> ranlux4_01;
|
sl@0
|
522 |
|
sl@0
|
523 |
#ifndef __SUNPRO_CC
|
sl@0
|
524 |
using ::boost::random_device;
|
sl@0
|
525 |
#endif
|
sl@0
|
526 |
using ::boost::uniform_int;
|
sl@0
|
527 |
|
sl@0
|
528 |
class bernoulli_distribution
|
sl@0
|
529 |
{
|
sl@0
|
530 |
public:
|
sl@0
|
531 |
// types
|
sl@0
|
532 |
typedef int input_type;
|
sl@0
|
533 |
typedef bool result_type;
|
sl@0
|
534 |
// constructors and member function
|
sl@0
|
535 |
explicit bernoulli_distribution(double p = 0.5)
|
sl@0
|
536 |
: m_dist(p){}
|
sl@0
|
537 |
double p() const
|
sl@0
|
538 |
{ return m_dist.p(); }
|
sl@0
|
539 |
void reset()
|
sl@0
|
540 |
{ m_dist.reset(); }
|
sl@0
|
541 |
template<class UniformRandomNumberGenerator>
|
sl@0
|
542 |
result_type operator()(UniformRandomNumberGenerator& urng)
|
sl@0
|
543 |
{
|
sl@0
|
544 |
return m_dist(urng);
|
sl@0
|
545 |
}
|
sl@0
|
546 |
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
sl@0
|
547 |
template<class CharT, class Traits>
|
sl@0
|
548 |
friend std::basic_ostream<CharT,Traits>&
|
sl@0
|
549 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
sl@0
|
550 |
const bernoulli_distribution& lcg)
|
sl@0
|
551 |
{
|
sl@0
|
552 |
return os << lcg.m_dist;
|
sl@0
|
553 |
}
|
sl@0
|
554 |
|
sl@0
|
555 |
template<class CharT, class Traits>
|
sl@0
|
556 |
friend std::basic_istream<CharT,Traits>&
|
sl@0
|
557 |
operator>>(std::basic_istream<CharT,Traits>& is,
|
sl@0
|
558 |
bernoulli_distribution& lcg)
|
sl@0
|
559 |
{
|
sl@0
|
560 |
return is >> lcg.m_dist;
|
sl@0
|
561 |
}
|
sl@0
|
562 |
#endif
|
sl@0
|
563 |
|
sl@0
|
564 |
private:
|
sl@0
|
565 |
::boost::bernoulli_distribution<double> m_dist;
|
sl@0
|
566 |
};
|
sl@0
|
567 |
//using ::boost::bernoulli_distribution;
|
sl@0
|
568 |
using ::boost::geometric_distribution;
|
sl@0
|
569 |
using ::boost::poisson_distribution;
|
sl@0
|
570 |
using ::boost::binomial_distribution;
|
sl@0
|
571 |
using ::boost::uniform_real;
|
sl@0
|
572 |
using ::boost::exponential_distribution;
|
sl@0
|
573 |
using ::boost::normal_distribution;
|
sl@0
|
574 |
using ::boost::gamma_distribution;
|
sl@0
|
575 |
|
sl@0
|
576 |
} }
|
sl@0
|
577 |
|
sl@0
|
578 |
#endif
|
sl@0
|
579 |
|
sl@0
|
580 |
#endif
|
sl@0
|
581 |
|