1 // (C) Copyright John Maddock 2005.
2 // (C) Copyright Henry S. Warren 2005.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_TR1_RANDOM_HPP_INCLUDED
8 # define BOOST_TR1_RANDOM_HPP_INCLUDED
9 # include <boost/tr1/detail/config.hpp>
11 #ifdef BOOST_HAS_TR1_RANDOM
12 # if defined(BOOST_HAS_INCLUDE_NEXT) && !defined(BOOST_TR1_DISABLE_INCLUDE_NEXT)
13 # include_next BOOST_TR1_HEADER(random)
15 # include <boost/tr1/detail/config_all.hpp>
16 # include BOOST_TR1_STD_HEADER(BOOST_TR1_PATH(random))
20 #include <boost/random.hpp>
22 // Sunpros linker complains if we so much as include this...
23 # include <boost/nondet_random.hpp>
25 #include <boost/tr1/detail/functor2iterator.hpp>
26 #include <boost/type_traits/is_fundamental.hpp>
27 #include <boost/type_traits/is_same.hpp>
29 namespace std { namespace tr1{
31 using ::boost::variate_generator;
33 template<class UIntType, UIntType a, UIntType c, UIntType m>
34 class linear_congruential
37 typedef ::boost::random::linear_congruential<UIntType, a, c, m, 0> impl_type;
40 typedef UIntType result_type;
42 BOOST_STATIC_CONSTANT(UIntType, multiplier = a);
43 BOOST_STATIC_CONSTANT(UIntType, increment = c);
44 BOOST_STATIC_CONSTANT(UIntType, modulus = m);
45 // constructors and member function
46 explicit linear_congruential(unsigned long x0 = 1)
48 linear_congruential(const linear_congruential& that)
50 template<class Gen> linear_congruential(Gen& g)
52 init1(g, ::boost::is_same<Gen,linear_congruential>());
54 void seed(unsigned long x0 = 1)
56 template<class Gen> void seed(Gen& g)
58 init2(g, ::boost::is_fundamental<Gen>());
60 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
61 { return (m_gen.min)(); }
62 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
63 { return (m_gen.max)(); }
64 result_type operator()()
68 bool operator==(const linear_congruential& that)const
69 { return m_gen == that.m_gen; }
70 bool operator!=(const linear_congruential& that)const
71 { return m_gen != that.m_gen; }
73 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
74 template<class CharT, class Traits>
75 friend std::basic_ostream<CharT,Traits>&
76 operator<<(std::basic_ostream<CharT,Traits>& os,
77 const linear_congruential& lcg)
79 return os << lcg.m_gen;
82 template<class CharT, class Traits>
83 friend std::basic_istream<CharT,Traits>&
84 operator>>(std::basic_istream<CharT,Traits>& is,
85 linear_congruential& lcg)
87 return is >> lcg.m_gen;
93 void init1(Gen& g, const ::boost::true_type&)
98 void init1(Gen& g, const ::boost::false_type&)
100 init2(g, ::boost::is_fundamental<Gen>());
103 void init2(Gen& g, const ::boost::true_type&)
105 m_gen.seed(static_cast<unsigned long>(g));
108 void init2(Gen& g, const ::boost::false_type&)
110 //typedef typename Gen::result_type gen_rt;
111 boost::tr1_details::functor2iterator<Gen, unsigned long> f1(g), f2;
117 template<class UIntType, int w, int n, int m, int r,
118 UIntType a, int u, int s, UIntType b, int t, UIntType c, int l>
119 class mersenne_twister
121 typedef ::boost::random::mersenne_twister
122 <UIntType, w, n, m, r, a, u, s, b, t, c, l, 0> imp_type;
125 typedef UIntType result_type;
127 BOOST_STATIC_CONSTANT(int, word_size = w);
128 BOOST_STATIC_CONSTANT(int, state_size = n);
129 BOOST_STATIC_CONSTANT(int, shift_size = m);
130 BOOST_STATIC_CONSTANT(int, mask_bits = r);
131 BOOST_STATIC_CONSTANT(UIntType, parameter_a = a);
132 BOOST_STATIC_CONSTANT(int, output_u = u);
133 BOOST_STATIC_CONSTANT(int, output_s = s);
134 BOOST_STATIC_CONSTANT(UIntType, output_b = b);
135 BOOST_STATIC_CONSTANT(int, output_t = t);
136 BOOST_STATIC_CONSTANT(UIntType, output_c = c);
137 BOOST_STATIC_CONSTANT(int, output_l = l);
138 // constructors and member function
140 explicit mersenne_twister(unsigned long value)
141 : m_gen(value == 0 ? 5489UL : value){}
142 template<class Gen> mersenne_twister(Gen& g)
144 init1(g, ::boost::is_same<mersenne_twister,Gen>());
148 void seed(unsigned long value)
149 { m_gen.seed(value == 0 ? 5489UL : value); }
150 template<class Gen> void seed(Gen& g)
151 { init2(g, ::boost::is_fundamental<Gen>()); }
152 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
153 { return (m_gen.min)(); }
154 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
155 { return (m_gen.max)(); }
156 result_type operator()()
158 bool operator==(const mersenne_twister& that)const
159 { return m_gen == that.m_gen; }
160 bool operator!=(const mersenne_twister& that)const
161 { return m_gen != that.m_gen; }
163 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
164 template<class CharT, class Traits>
165 friend std::basic_ostream<CharT,Traits>&
166 operator<<(std::basic_ostream<CharT,Traits>& os,
167 const mersenne_twister& lcg)
169 return os << lcg.m_gen;
172 template<class CharT, class Traits>
173 friend std::basic_istream<CharT,Traits>&
174 operator>>(std::basic_istream<CharT,Traits>& is,
175 mersenne_twister& lcg)
177 return is >> lcg.m_gen;
182 void init1(Gen& g, const ::boost::true_type&)
187 void init1(Gen& g, const ::boost::false_type&)
189 init2(g, ::boost::is_fundamental<Gen>());
192 void init2(Gen& g, const ::boost::true_type&)
194 m_gen.seed(static_cast<unsigned long>(g == 0 ? 4357UL : g));
197 void init2(Gen& g, const ::boost::false_type&)
204 template<class IntType, IntType m, int s, int r>
205 class subtract_with_carry
209 typedef IntType result_type;
211 BOOST_STATIC_CONSTANT(IntType, modulus = m);
212 BOOST_STATIC_CONSTANT(int, long_lag = r);
213 BOOST_STATIC_CONSTANT(int, short_lag = s);
215 // constructors and member function
216 subtract_with_carry(){}
217 explicit subtract_with_carry(unsigned long value)
218 : m_gen(value == 0 ? 19780503UL : value){}
219 template<class Gen> subtract_with_carry(Gen& g)
220 { init1(g, ::boost::is_same<Gen, subtract_with_carry<IntType, m, s, r> >()); }
221 void seed(unsigned long value = 19780503ul)
222 { m_gen.seed(value == 0 ? 19780503UL : value); }
223 template<class Gen> void seed(Gen& g)
224 { init2(g, ::boost::is_fundamental<Gen>()); }
225 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
226 { return (m_gen.min)(); }
227 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
228 { return (m_gen.max)(); }
229 result_type operator()()
231 bool operator==(const subtract_with_carry& that)const
232 { return m_gen == that.m_gen; }
233 bool operator!=(const subtract_with_carry& that)const
234 { return m_gen != that.m_gen; }
236 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
237 template<class CharT, class Traits>
238 friend std::basic_ostream<CharT,Traits>&
239 operator<<(std::basic_ostream<CharT,Traits>& os,
240 const subtract_with_carry& lcg)
242 return os << lcg.m_gen;
245 template<class CharT, class Traits>
246 friend std::basic_istream<CharT,Traits>&
247 operator>>(std::basic_istream<CharT,Traits>& is,
248 subtract_with_carry& lcg)
250 return is >> lcg.m_gen;
255 void init1(Gen& g, const ::boost::true_type&)
260 void init1(Gen& g, const ::boost::false_type&)
262 init2(g, ::boost::is_fundamental<Gen>());
265 void init2(Gen& g, const ::boost::true_type&)
267 m_gen.seed(static_cast<unsigned long>(g == 0 ? 19780503UL : g));
270 void init2(Gen& g, const ::boost::false_type&)
274 ::boost::random::subtract_with_carry<IntType, m, s, r, 0> m_gen;
277 template<class RealType, int w, int s, int r>
278 class subtract_with_carry_01
282 typedef RealType result_type;
284 BOOST_STATIC_CONSTANT(int, word_size = w);
285 BOOST_STATIC_CONSTANT(int, long_lag = r);
286 BOOST_STATIC_CONSTANT(int, short_lag = s);
288 // constructors and member function
289 subtract_with_carry_01(){}
290 explicit subtract_with_carry_01(unsigned long value)
291 : m_gen(value == 0 ? 19780503UL : value){}
292 template<class Gen> subtract_with_carry_01(Gen& g)
293 { init1(g, ::boost::is_same<Gen, subtract_with_carry_01<RealType, w, s, r> >()); }
294 void seed(unsigned long value = 19780503UL)
295 { m_gen.seed(value == 0 ? 19780503UL : value); }
296 template<class Gen> void seed(Gen& g)
297 { init2(g, ::boost::is_fundamental<Gen>()); }
298 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
299 { return (m_gen.min)(); }
300 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
301 { return (m_gen.max)(); }
302 result_type operator()()
304 bool operator==(const subtract_with_carry_01& that)const
305 { return m_gen == that.m_gen; }
306 bool operator!=(const subtract_with_carry_01& that)const
307 { return m_gen != that.m_gen; }
309 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
310 template<class CharT, class Traits>
311 friend std::basic_ostream<CharT,Traits>&
312 operator<<(std::basic_ostream<CharT,Traits>& os,
313 const subtract_with_carry_01& lcg)
315 return os << lcg.m_gen;
318 template<class CharT, class Traits>
319 friend std::basic_istream<CharT,Traits>&
320 operator>>(std::basic_istream<CharT,Traits>& is,
321 subtract_with_carry_01& lcg)
323 return is >> lcg.m_gen;
328 void init1(Gen& g, const ::boost::true_type&)
333 void init1(Gen& g, const ::boost::false_type&)
335 init2(g, ::boost::is_fundamental<Gen>());
338 void init2(Gen& g, const ::boost::true_type&)
340 m_gen.seed(static_cast<unsigned long>(g == 0 ? 19780503UL : g));
343 void init2(Gen& g, const ::boost::false_type&)
345 //typedef typename Gen::result_type gen_rt;
346 boost::tr1_details::functor2iterator<Gen, unsigned long> f1(g), f2;
349 ::boost::random::subtract_with_carry_01<RealType, w, s, r, 0> m_gen;
352 using ::boost::random::discard_block;
354 template<class UniformRandomNumberGenerator1, int s1, class UniformRandomNumberGenerator2, int s2>
359 typedef UniformRandomNumberGenerator1 base1_type;
360 typedef UniformRandomNumberGenerator2 base2_type;
361 typedef unsigned long result_type;
363 BOOST_STATIC_CONSTANT(int, shift1 = s1);
364 BOOST_STATIC_CONSTANT(int, shift2 = s2);
365 // constructors and member function
366 xor_combine(){ init_minmax(); }
367 xor_combine(const base1_type & rng1, const base2_type & rng2)
368 : m_b1(rng1), m_b2(rng2) { init_minmax(); }
369 xor_combine(unsigned long s)
370 : m_b1(s), m_b2(s+1) { init_minmax(); }
371 template<class Gen> xor_combine(Gen& g)
374 init1(g, ::boost::is_same<Gen, xor_combine<UniformRandomNumberGenerator1, s1, UniformRandomNumberGenerator2, s2> >());
381 void seed(unsigned long s)
386 template<class Gen> void seed(Gen& g)
388 init2(g, ::boost::is_fundamental<Gen>());
391 const base1_type& base1() const
393 const base2_type& base2() const
395 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION() const
397 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
399 result_type operator()()
400 { return (m_b1() << s1) ^ (m_b2() << s2); }
402 bool operator == (const xor_combine& that)const
403 { return (m_b1 == that.m_b1) && (m_b2 == that.m_b2); }
404 bool operator != (const xor_combine& that)const
405 { return !(*this == that); }
407 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
408 template<class CharT, class Traits>
409 friend std::basic_ostream<CharT,Traits>&
410 operator<<(std::basic_ostream<CharT,Traits>& os,
411 const xor_combine& lcg)
413 return os << lcg.m_b1 << " " << lcg.m_b2;
416 template<class CharT, class Traits>
417 friend std::basic_istream<CharT,Traits>&
418 operator>>(std::basic_istream<CharT,Traits>& is,
421 return is >> lcg.m_b1 >> lcg.m_b2;
433 void init1(Gen& g, const ::boost::true_type&)
439 void init1(Gen& g, const ::boost::false_type&)
441 init2(g, ::boost::is_fundamental<Gen>());
444 void init2(Gen& g, const ::boost::true_type&)
446 m_b1.seed(static_cast<unsigned long>(g));
447 m_b2.seed(static_cast<unsigned long>(g));
450 void init2(Gen& g, const ::boost::false_type&)
457 template<class UniformRandomNumberGenerator1, int s1, class UniformRandomNumberGenerator2, int s2>
458 void xor_combine<UniformRandomNumberGenerator1, s1, UniformRandomNumberGenerator2, s2>::init_minmax()
461 // The following code is based on that given in "Hacker's Delight"
462 // by Henry S. Warren, (Addison-Wesley, 2003), and at
463 // http://www.hackersdelight.org/index.htm.
464 // Used here by permission.
466 // calculation of minimum value:
468 result_type a = (m_b1.min)() << s1;
469 result_type b = (m_b1.max)() << s1;
470 result_type c = (m_b2.min)() << s2;
471 result_type d = (m_b2.max)() << s2;
474 m = 0x1uL << ((sizeof(result_type) * CHAR_BIT) - 1);
477 temp = (a | m) & (static_cast<result_type>(0u) - m);
478 if (temp <= b) a = temp;
480 else if (a & ~c & m) {
481 temp = (c | m) & (static_cast<result_type>(0u) - m);
482 if (temp <= d) c = temp;
489 // calculation of maximum value:
491 if((((std::numeric_limits<result_type>::max)() >> s1) < (m_b1.max)())
492 || ((((std::numeric_limits<result_type>::max)()) >> s2) < (m_b2.max)()))
494 m_max = (std::numeric_limits<result_type>::max)();
497 a = (m_b1.min)() << s1;
498 b = (m_b1.max)() << s1;
499 c = (m_b2.min)() << s2;
500 d = (m_b2.max)() << s2;
502 m = 0x1uL << ((sizeof(result_type) * CHAR_BIT) - 1);
506 temp = (b - m) | (m - 1);
507 if (temp >= a) b = temp;
509 temp = (d - m) | (m - 1);
510 if (temp >= c) d = temp;
518 typedef linear_congruential< ::boost::int32_t, 16807, 0, 2147483647> minstd_rand0;
519 typedef linear_congruential< ::boost::int32_t, 48271, 0, 2147483647> minstd_rand;
520 typedef mersenne_twister< ::boost::uint32_t, 32,624,397,31,0x9908b0df,11,7,0x9d2c5680,15,0xefc60000,18> mt19937;
521 typedef subtract_with_carry_01<float, 24, 10, 24> ranlux_base_01;
522 typedef subtract_with_carry_01<double, 48, 10, 24> ranlux64_base_01;
523 typedef discard_block<subtract_with_carry< ::boost::int32_t, (1<<24), 10, 24>, 223, 24> ranlux3;
524 typedef discard_block<subtract_with_carry< ::boost::int32_t, (1<<24), 10, 24>, 389, 24> ranlux4;
525 typedef discard_block<subtract_with_carry_01<float, 24, 10, 24>, 223, 24> ranlux3_01;
526 typedef discard_block<subtract_with_carry_01<float, 24, 10, 24>, 389, 24> ranlux4_01;
529 using ::boost::random_device;
531 using ::boost::uniform_int;
533 class bernoulli_distribution
537 typedef int input_type;
538 typedef bool result_type;
539 // constructors and member function
540 explicit bernoulli_distribution(double p = 0.5)
543 { return m_dist.p(); }
546 template<class UniformRandomNumberGenerator>
547 result_type operator()(UniformRandomNumberGenerator& urng)
551 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
552 template<class CharT, class Traits>
553 friend std::basic_ostream<CharT,Traits>&
554 operator<<(std::basic_ostream<CharT,Traits>& os,
555 const bernoulli_distribution& lcg)
557 return os << lcg.m_dist;
560 template<class CharT, class Traits>
561 friend std::basic_istream<CharT,Traits>&
562 operator>>(std::basic_istream<CharT,Traits>& is,
563 bernoulli_distribution& lcg)
565 return is >> lcg.m_dist;
570 ::boost::bernoulli_distribution<double> m_dist;
572 //using ::boost::bernoulli_distribution;
573 using ::boost::geometric_distribution;
574 using ::boost::poisson_distribution;
575 using ::boost::binomial_distribution;
576 using ::boost::uniform_real;
577 using ::boost::exponential_distribution;
578 using ::boost::normal_distribution;
579 using ::boost::gamma_distribution;