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