1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2006, 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1/random.tcc
31 * This is a TR1 C++ Library header.
36 _GLIBCXX_BEGIN_NAMESPACE(tr1)
39 * (Further) implementation-space details.
43 // General case for x = (ax + c) mod m -- use Schrage's algorithm to avoid
46 // Because a and c are compile-time integral constants the compiler kindly
47 // elides any unreachable paths.
49 // Preconditions: a > 0, m > 0.
51 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
61 static const _Tp __q = __m / __a;
62 static const _Tp __r = __m % __a;
64 _Tp __t1 = __a * (__x % __q);
65 _Tp __t2 = __r * (__x / __q);
69 __x = __m - __t2 + __t1;
74 const _Tp __d = __m - __x;
84 // Special case for m == 0 -- use unsigned integer overflow as modulo
86 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
87 struct _Mod<_Tp, __a, __c, __m, true>
91 { return __a * __x + __c; }
93 } // namespace __detail
96 * Seeds the LCR with integral value @p __x0, adjusted so that the
97 * ring identity is never a member of the convergence set.
99 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
101 linear_congruential<_UIntType, __a, __c, __m>::
102 seed(unsigned long __x0)
104 if ((__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0)
105 && (__detail::__mod<_UIntType, 1, 0, __m>(__x0) == 0))
106 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(1);
108 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(__x0);
112 * Seeds the LCR engine with a value generated by @p __g.
114 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
117 linear_congruential<_UIntType, __a, __c, __m>::
118 seed(_Gen& __g, false_type)
120 _UIntType __x0 = __g();
121 if ((__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0)
122 && (__detail::__mod<_UIntType, 1, 0, __m>(__x0) == 0))
123 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(1);
125 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(__x0);
129 * Gets the next generated value in sequence.
131 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
132 typename linear_congruential<_UIntType, __a, __c, __m>::result_type
133 linear_congruential<_UIntType, __a, __c, __m>::
136 _M_x = __detail::__mod<_UIntType, __a, __c, __m>(_M_x);
140 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
141 typename _CharT, typename _Traits>
142 std::basic_ostream<_CharT, _Traits>&
143 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
144 const linear_congruential<_UIntType, __a, __c, __m>& __lcr)
146 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
147 typedef typename __ostream_type::ios_base __ios_base;
149 const typename __ios_base::fmtflags __flags = __os.flags();
150 const _CharT __fill = __os.fill();
151 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
152 __os.fill(__os.widen(' '));
161 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
162 typename _CharT, typename _Traits>
163 std::basic_istream<_CharT, _Traits>&
164 operator>>(std::basic_istream<_CharT, _Traits>& __is,
165 linear_congruential<_UIntType, __a, __c, __m>& __lcr)
167 typedef std::basic_istream<_CharT, _Traits> __istream_type;
168 typedef typename __istream_type::ios_base __ios_base;
170 const typename __ios_base::fmtflags __flags = __is.flags();
171 __is.flags(__ios_base::dec);
180 template<class _UIntType, int __w, int __n, int __m, int __r,
181 _UIntType __a, int __u, int __s,
182 _UIntType __b, int __t, _UIntType __c, int __l>
184 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
185 __b, __t, __c, __l>::
186 seed(unsigned long __value)
188 _M_x[0] = __detail::__mod<_UIntType, 1, 0,
189 __detail::_Shift<_UIntType, __w>::__value>(__value);
191 for (int __i = 1; __i < state_size; ++__i)
193 _UIntType __x = _M_x[__i - 1];
194 __x ^= __x >> (__w - 2);
197 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
198 __detail::_Shift<_UIntType, __w>::__value>(__x);
203 template<class _UIntType, int __w, int __n, int __m, int __r,
204 _UIntType __a, int __u, int __s,
205 _UIntType __b, int __t, _UIntType __c, int __l>
208 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
209 __b, __t, __c, __l>::
210 seed(_Gen& __gen, false_type)
212 for (int __i = 0; __i < state_size; ++__i)
213 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
214 __detail::_Shift<_UIntType, __w>::__value>(__gen());
218 template<class _UIntType, int __w, int __n, int __m, int __r,
219 _UIntType __a, int __u, int __s,
220 _UIntType __b, int __t, _UIntType __c, int __l>
222 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
223 __b, __t, __c, __l>::result_type
224 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
225 __b, __t, __c, __l>::
228 // Reload the vector - cost is O(n) amortized over n calls.
229 if (_M_p >= state_size)
231 const _UIntType __upper_mask = (~_UIntType()) << __r;
232 const _UIntType __lower_mask = ~__upper_mask;
234 for (int __k = 0; __k < (__n - __m); ++__k)
236 _UIntType __y = ((_M_x[__k] & __upper_mask)
237 | (_M_x[__k + 1] & __lower_mask));
238 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
239 ^ ((__y & 0x01) ? __a : 0));
242 for (int __k = (__n - __m); __k < (__n - 1); ++__k)
244 _UIntType __y = ((_M_x[__k] & __upper_mask)
245 | (_M_x[__k + 1] & __lower_mask));
246 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
247 ^ ((__y & 0x01) ? __a : 0));
250 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
251 | (_M_x[0] & __lower_mask));
252 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
253 ^ ((__y & 0x01) ? __a : 0));
257 // Calculate o(x(i)).
258 result_type __z = _M_x[_M_p++];
260 __z ^= (__z << __s) & __b;
261 __z ^= (__z << __t) & __c;
267 template<class _UIntType, int __w, int __n, int __m, int __r,
268 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
269 _UIntType __c, int __l,
270 typename _CharT, typename _Traits>
271 std::basic_ostream<_CharT, _Traits>&
272 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
273 const mersenne_twister<_UIntType, __w, __n, __m,
274 __r, __a, __u, __s, __b, __t, __c, __l>& __x)
276 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
277 typedef typename __ostream_type::ios_base __ios_base;
279 const typename __ios_base::fmtflags __flags = __os.flags();
280 const _CharT __fill = __os.fill();
281 const _CharT __space = __os.widen(' ');
282 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
285 for (int __i = 0; __i < __n - 1; ++__i)
286 __os << __x._M_x[__i] << __space;
287 __os << __x._M_x[__n - 1];
294 template<class _UIntType, int __w, int __n, int __m, int __r,
295 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
296 _UIntType __c, int __l,
297 typename _CharT, typename _Traits>
298 std::basic_istream<_CharT, _Traits>&
299 operator>>(std::basic_istream<_CharT, _Traits>& __is,
300 mersenne_twister<_UIntType, __w, __n, __m,
301 __r, __a, __u, __s, __b, __t, __c, __l>& __x)
303 typedef std::basic_istream<_CharT, _Traits> __istream_type;
304 typedef typename __istream_type::ios_base __ios_base;
306 const typename __ios_base::fmtflags __flags = __is.flags();
307 __is.flags(__ios_base::dec | __ios_base::skipws);
309 for (int __i = 0; __i < __n; ++__i)
310 __is >> __x._M_x[__i];
317 template<typename _IntType, _IntType __m, int __s, int __r>
319 subtract_with_carry<_IntType, __m, __s, __r>::
320 seed(unsigned long __value)
325 std::tr1::linear_congruential<unsigned long, 40014, 0, 2147483563>
328 for (int __i = 0; __i < long_lag; ++__i)
329 _M_x[__i] = __detail::__mod<_UIntType, 1, 0, modulus>(__lcg());
331 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
335 template<typename _IntType, _IntType __m, int __s, int __r>
338 subtract_with_carry<_IntType, __m, __s, __r>::
339 seed(_Gen& __gen, false_type)
341 const int __n = (std::numeric_limits<_UIntType>::digits + 31) / 32;
343 for (int __i = 0; __i < long_lag; ++__i)
346 _UIntType __factor = 1;
347 for (int __j = 0; __j < __n; ++__j)
349 __tmp += __detail::__mod<__detail::_UInt32Type, 1, 0, 0>
350 (__gen()) * __factor;
351 __factor *= __detail::_Shift<_UIntType, 32>::__value;
353 _M_x[__i] = __detail::__mod<_UIntType, 1, 0, modulus>(__tmp);
355 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
359 template<typename _IntType, _IntType __m, int __s, int __r>
360 typename subtract_with_carry<_IntType, __m, __s, __r>::result_type
361 subtract_with_carry<_IntType, __m, __s, __r>::
364 // Derive short lag index from current index.
365 int __ps = _M_p - short_lag;
369 // Calculate new x(i) without overflow or division.
370 // NB: Thanks to the requirements for _IntType, _M_x[_M_p] + _M_carry
373 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
375 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
380 __xi = modulus - _M_x[_M_p] - _M_carry + _M_x[__ps];
385 // Adjust current index to loop around in ring buffer.
386 if (++_M_p >= long_lag)
392 template<typename _IntType, _IntType __m, int __s, int __r,
393 typename _CharT, typename _Traits>
394 std::basic_ostream<_CharT, _Traits>&
395 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
396 const subtract_with_carry<_IntType, __m, __s, __r>& __x)
398 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
399 typedef typename __ostream_type::ios_base __ios_base;
401 const typename __ios_base::fmtflags __flags = __os.flags();
402 const _CharT __fill = __os.fill();
403 const _CharT __space = __os.widen(' ');
404 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
407 for (int __i = 0; __i < __r; ++__i)
408 __os << __x._M_x[__i] << __space;
409 __os << __x._M_carry;
416 template<typename _IntType, _IntType __m, int __s, int __r,
417 typename _CharT, typename _Traits>
418 std::basic_istream<_CharT, _Traits>&
419 operator>>(std::basic_istream<_CharT, _Traits>& __is,
420 subtract_with_carry<_IntType, __m, __s, __r>& __x)
422 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
423 typedef typename __istream_type::ios_base __ios_base;
425 const typename __ios_base::fmtflags __flags = __is.flags();
426 __is.flags(__ios_base::dec | __ios_base::skipws);
428 for (int __i = 0; __i < __r; ++__i)
429 __is >> __x._M_x[__i];
430 __is >> __x._M_carry;
437 template<typename _RealType, int __w, int __s, int __r>
439 subtract_with_carry_01<_RealType, __w, __s, __r>::
440 _M_initialize_npows()
442 for (int __j = 0; __j < __n; ++__j)
443 #if _GLIBCXX_USE_C99_MATH_TR1
444 _M_npows[__j] = std::tr1::ldexp(_RealType(1), -__w + __j * 32);
446 _M_npows[__j] = std::pow(_RealType(2), -__w + __j * 32);
450 template<typename _RealType, int __w, int __s, int __r>
452 subtract_with_carry_01<_RealType, __w, __s, __r>::
453 seed(unsigned long __value)
458 // _GLIBCXX_RESOLVE_LIB_DEFECTS
459 // 512. Seeding subtract_with_carry_01 from a single unsigned long.
460 std::tr1::linear_congruential<unsigned long, 40014, 0, 2147483563>
466 template<typename _RealType, int __w, int __s, int __r>
469 subtract_with_carry_01<_RealType, __w, __s, __r>::
470 seed(_Gen& __gen, false_type)
472 for (int __i = 0; __i < long_lag; ++__i)
474 for (int __j = 0; __j < __n - 1; ++__j)
475 _M_x[__i][__j] = __detail::__mod<_UInt32Type, 1, 0, 0>(__gen());
476 _M_x[__i][__n - 1] = __detail::__mod<_UInt32Type, 1, 0,
477 __detail::_Shift<_UInt32Type, __w % 32>::__value>(__gen());
481 for (int __j = 0; __j < __n; ++__j)
482 if (_M_x[long_lag - 1][__j] != 0)
491 template<typename _RealType, int __w, int __s, int __r>
492 typename subtract_with_carry_01<_RealType, __w, __s, __r>::result_type
493 subtract_with_carry_01<_RealType, __w, __s, __r>::
496 // Derive short lag index from current index.
497 int __ps = _M_p - short_lag;
501 _UInt32Type __new_carry;
502 for (int __j = 0; __j < __n - 1; ++__j)
504 if (_M_x[__ps][__j] > _M_x[_M_p][__j]
505 || (_M_x[__ps][__j] == _M_x[_M_p][__j] && _M_carry == 0))
510 _M_x[_M_p][__j] = _M_x[__ps][__j] - _M_x[_M_p][__j] - _M_carry;
511 _M_carry = __new_carry;
514 if (_M_x[__ps][__n - 1] > _M_x[_M_p][__n - 1]
515 || (_M_x[__ps][__n - 1] == _M_x[_M_p][__n - 1] && _M_carry == 0))
520 _M_x[_M_p][__n - 1] = __detail::__mod<_UInt32Type, 1, 0,
521 __detail::_Shift<_UInt32Type, __w % 32>::__value>
522 (_M_x[__ps][__n - 1] - _M_x[_M_p][__n - 1] - _M_carry);
523 _M_carry = __new_carry;
525 result_type __ret = 0.0;
526 for (int __j = 0; __j < __n; ++__j)
527 __ret += _M_x[_M_p][__j] * _M_npows[__j];
529 // Adjust current index to loop around in ring buffer.
530 if (++_M_p >= long_lag)
536 template<typename _RealType, int __w, int __s, int __r,
537 typename _CharT, typename _Traits>
538 std::basic_ostream<_CharT, _Traits>&
539 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
540 const subtract_with_carry_01<_RealType, __w, __s, __r>& __x)
542 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
543 typedef typename __ostream_type::ios_base __ios_base;
545 const typename __ios_base::fmtflags __flags = __os.flags();
546 const _CharT __fill = __os.fill();
547 const _CharT __space = __os.widen(' ');
548 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
551 for (int __i = 0; __i < __r; ++__i)
552 for (int __j = 0; __j < __x.__n; ++__j)
553 __os << __x._M_x[__i][__j] << __space;
554 __os << __x._M_carry;
561 template<typename _RealType, int __w, int __s, int __r,
562 typename _CharT, typename _Traits>
563 std::basic_istream<_CharT, _Traits>&
564 operator>>(std::basic_istream<_CharT, _Traits>& __is,
565 subtract_with_carry_01<_RealType, __w, __s, __r>& __x)
567 typedef std::basic_istream<_CharT, _Traits> __istream_type;
568 typedef typename __istream_type::ios_base __ios_base;
570 const typename __ios_base::fmtflags __flags = __is.flags();
571 __is.flags(__ios_base::dec | __ios_base::skipws);
573 for (int __i = 0; __i < __r; ++__i)
574 for (int __j = 0; __j < __x.__n; ++__j)
575 __is >> __x._M_x[__i][__j];
576 __is >> __x._M_carry;
583 template<class _UniformRandomNumberGenerator, int __p, int __r>
584 typename discard_block<_UniformRandomNumberGenerator,
585 __p, __r>::result_type
586 discard_block<_UniformRandomNumberGenerator, __p, __r>::
589 if (_M_n >= used_block)
591 while (_M_n < block_size)
602 template<class _UniformRandomNumberGenerator, int __p, int __r,
603 typename _CharT, typename _Traits>
604 std::basic_ostream<_CharT, _Traits>&
605 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
606 const discard_block<_UniformRandomNumberGenerator,
609 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
610 typedef typename __ostream_type::ios_base __ios_base;
612 const typename __ios_base::fmtflags __flags = __os.flags();
613 const _CharT __fill = __os.fill();
614 const _CharT __space = __os.widen(' ');
615 __os.flags(__ios_base::dec | __ios_base::fixed
619 __os << __x._M_b << __space << __x._M_n;
626 template<class _UniformRandomNumberGenerator, int __p, int __r,
627 typename _CharT, typename _Traits>
628 std::basic_istream<_CharT, _Traits>&
629 operator>>(std::basic_istream<_CharT, _Traits>& __is,
630 discard_block<_UniformRandomNumberGenerator, __p, __r>& __x)
632 typedef std::basic_istream<_CharT, _Traits> __istream_type;
633 typedef typename __istream_type::ios_base __ios_base;
635 const typename __ios_base::fmtflags __flags = __is.flags();
636 __is.flags(__ios_base::dec | __ios_base::skipws);
638 __is >> __x._M_b >> __x._M_n;
645 template<class _UniformRandomNumberGenerator1, int __s1,
646 class _UniformRandomNumberGenerator2, int __s2>
648 xor_combine<_UniformRandomNumberGenerator1, __s1,
649 _UniformRandomNumberGenerator2, __s2>::
652 const int __w = std::numeric_limits<result_type>::digits;
654 const result_type __m1 =
655 std::min(result_type(_M_b1.max() - _M_b1.min()),
656 __detail::_Shift<result_type, __w - __s1>::__value - 1);
658 const result_type __m2 =
659 std::min(result_type(_M_b2.max() - _M_b2.min()),
660 __detail::_Shift<result_type, __w - __s2>::__value - 1);
662 // NB: In TR1 s1 is not required to be >= s2.
664 _M_max = _M_initialize_max_aux(__m2, __m1, __s2 - __s1) << __s1;
666 _M_max = _M_initialize_max_aux(__m1, __m2, __s1 - __s2) << __s2;
669 template<class _UniformRandomNumberGenerator1, int __s1,
670 class _UniformRandomNumberGenerator2, int __s2>
671 typename xor_combine<_UniformRandomNumberGenerator1, __s1,
672 _UniformRandomNumberGenerator2, __s2>::result_type
673 xor_combine<_UniformRandomNumberGenerator1, __s1,
674 _UniformRandomNumberGenerator2, __s2>::
675 _M_initialize_max_aux(result_type __a, result_type __b, int __d)
677 const result_type __two2d = result_type(1) << __d;
678 const result_type __c = __a * __two2d;
680 if (__a == 0 || __b < __two2d)
683 const result_type __t = std::max(__c, __b);
684 const result_type __u = std::min(__c, __b);
686 result_type __ub = __u;
688 for (__p = 0; __ub != 1; __ub >>= 1)
691 const result_type __two2p = result_type(1) << __p;
692 const result_type __k = __t / __two2p;
695 return (__k + 1) * __two2p - 1;
698 return (__k + 1) * __two2p + _M_initialize_max_aux((__t % __two2p)
702 return (__k + 1) * __two2p + _M_initialize_max_aux((__u % __two2p)
707 template<class _UniformRandomNumberGenerator1, int __s1,
708 class _UniformRandomNumberGenerator2, int __s2,
709 typename _CharT, typename _Traits>
710 std::basic_ostream<_CharT, _Traits>&
711 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
712 const xor_combine<_UniformRandomNumberGenerator1, __s1,
713 _UniformRandomNumberGenerator2, __s2>& __x)
715 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
716 typedef typename __ostream_type::ios_base __ios_base;
718 const typename __ios_base::fmtflags __flags = __os.flags();
719 const _CharT __fill = __os.fill();
720 const _CharT __space = __os.widen(' ');
721 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
724 __os << __x.base1() << __space << __x.base2();
731 template<class _UniformRandomNumberGenerator1, int __s1,
732 class _UniformRandomNumberGenerator2, int __s2,
733 typename _CharT, typename _Traits>
734 std::basic_istream<_CharT, _Traits>&
735 operator>>(std::basic_istream<_CharT, _Traits>& __is,
736 xor_combine<_UniformRandomNumberGenerator1, __s1,
737 _UniformRandomNumberGenerator2, __s2>& __x)
739 typedef std::basic_istream<_CharT, _Traits> __istream_type;
740 typedef typename __istream_type::ios_base __ios_base;
742 const typename __ios_base::fmtflags __flags = __is.flags();
743 __is.flags(__ios_base::skipws);
745 __is >> __x._M_b1 >> __x._M_b2;
752 template<typename _IntType, typename _CharT, typename _Traits>
753 std::basic_ostream<_CharT, _Traits>&
754 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
755 const uniform_int<_IntType>& __x)
757 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
758 typedef typename __ostream_type::ios_base __ios_base;
760 const typename __ios_base::fmtflags __flags = __os.flags();
761 const _CharT __fill = __os.fill();
762 const _CharT __space = __os.widen(' ');
763 __os.flags(__ios_base::scientific | __ios_base::left);
766 __os << __x.min() << __space << __x.max();
773 template<typename _IntType, typename _CharT, typename _Traits>
774 std::basic_istream<_CharT, _Traits>&
775 operator>>(std::basic_istream<_CharT, _Traits>& __is,
776 uniform_int<_IntType>& __x)
778 typedef std::basic_istream<_CharT, _Traits> __istream_type;
779 typedef typename __istream_type::ios_base __ios_base;
781 const typename __ios_base::fmtflags __flags = __is.flags();
782 __is.flags(__ios_base::dec | __ios_base::skipws);
784 __is >> __x._M_min >> __x._M_max;
791 template<typename _CharT, typename _Traits>
792 std::basic_ostream<_CharT, _Traits>&
793 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
794 const bernoulli_distribution& __x)
796 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
797 typedef typename __ostream_type::ios_base __ios_base;
799 const typename __ios_base::fmtflags __flags = __os.flags();
800 const _CharT __fill = __os.fill();
801 const std::streamsize __precision = __os.precision();
802 __os.flags(__ios_base::scientific | __ios_base::left);
803 __os.fill(__os.widen(' '));
804 __os.precision(__gnu_cxx::__numeric_traits<double>::__max_digits10);
810 __os.precision(__precision);
815 template<typename _IntType, typename _RealType>
816 template<class _UniformRandomNumberGenerator>
817 typename geometric_distribution<_IntType, _RealType>::result_type
818 geometric_distribution<_IntType, _RealType>::
819 operator()(_UniformRandomNumberGenerator& __urng)
821 // About the epsilon thing see this thread:
822 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
823 const _RealType __naf =
824 (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
825 // The largest _RealType convertible to _IntType.
826 const _RealType __thr =
827 std::numeric_limits<_IntType>::max() + __naf;
831 __cand = std::ceil(std::log(__urng()) / _M_log_p);
832 while (__cand >= __thr);
834 return result_type(__cand + __naf);
837 template<typename _IntType, typename _RealType,
838 typename _CharT, typename _Traits>
839 std::basic_ostream<_CharT, _Traits>&
840 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
841 const geometric_distribution<_IntType, _RealType>& __x)
843 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
844 typedef typename __ostream_type::ios_base __ios_base;
846 const typename __ios_base::fmtflags __flags = __os.flags();
847 const _CharT __fill = __os.fill();
848 const std::streamsize __precision = __os.precision();
849 __os.flags(__ios_base::scientific | __ios_base::left);
850 __os.fill(__os.widen(' '));
851 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
857 __os.precision(__precision);
862 template<typename _IntType, typename _RealType>
864 poisson_distribution<_IntType, _RealType>::
867 #if _GLIBCXX_USE_C99_MATH_TR1
870 const _RealType __m = std::floor(_M_mean);
871 _M_lm_thr = std::log(_M_mean);
872 _M_lfm = std::tr1::lgamma(__m + 1);
873 _M_sm = std::sqrt(__m);
875 const _RealType __pi_4 = 0.7853981633974483096156608458198757L;
876 const _RealType __dx = std::sqrt(2 * __m * std::log(32 * __m
878 _M_d = std::tr1::round(std::max(_RealType(6),
879 std::min(__m, __dx)));
880 const _RealType __cx = 2 * __m + _M_d;
881 _M_scx = std::sqrt(__cx / 2);
884 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
885 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2)) / _M_d;
889 _M_lm_thr = std::exp(-_M_mean);
893 * A rejection algorithm when mean >= 12 and a simple method based
894 * upon the multiplication of uniform random variates otherwise.
895 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
899 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
900 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
902 template<typename _IntType, typename _RealType>
903 template<class _UniformRandomNumberGenerator>
904 typename poisson_distribution<_IntType, _RealType>::result_type
905 poisson_distribution<_IntType, _RealType>::
906 operator()(_UniformRandomNumberGenerator& __urng)
908 #if _GLIBCXX_USE_C99_MATH_TR1
913 // See comments above...
914 const _RealType __naf =
915 (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
916 const _RealType __thr =
917 std::numeric_limits<_IntType>::max() + __naf;
919 const _RealType __m = std::floor(_M_mean);
921 const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
922 const _RealType __c1 = _M_sm * __spi_2;
923 const _RealType __c2 = _M_c2b + __c1;
924 const _RealType __c3 = __c2 + 1;
925 const _RealType __c4 = __c3 + 1;
927 const _RealType __e178 = 1.0129030479320018583185514777512983L;
928 const _RealType __c5 = __c4 + __e178;
929 const _RealType __c = _M_cb + __c5;
930 const _RealType __2cx = 2 * (2 * __m + _M_d);
932 bool __reject = true;
935 const _RealType __u = __c * __urng();
936 const _RealType __e = -std::log(__urng());
942 const _RealType __n = _M_nd(__urng);
943 const _RealType __y = -std::abs(__n) * _M_sm - 1;
944 __x = std::floor(__y);
945 __w = -__n * __n / 2;
949 else if (__u <= __c2)
951 const _RealType __n = _M_nd(__urng);
952 const _RealType __y = 1 + std::abs(__n) * _M_scx;
953 __x = std::ceil(__y);
954 __w = __y * (2 - __y) * _M_1cx;
958 else if (__u <= __c3)
959 // NB: This case not in the book, nor in the Errata,
960 // but should be ok...
962 else if (__u <= __c4)
964 else if (__u <= __c5)
968 const _RealType __v = -std::log(__urng());
969 const _RealType __y = _M_d + __v * __2cx / _M_d;
970 __x = std::ceil(__y);
971 __w = -_M_d * _M_1cx * (1 + __y / 2);
974 __reject = (__w - __e - __x * _M_lm_thr
975 > _M_lfm - std::tr1::lgamma(__x + __m + 1));
977 __reject |= __x + __m >= __thr;
981 return result_type(__x + __m + __naf);
987 _RealType __prod = 1.0;
994 while (__prod > _M_lm_thr);
1000 template<typename _IntType, typename _RealType,
1001 typename _CharT, typename _Traits>
1002 std::basic_ostream<_CharT, _Traits>&
1003 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1004 const poisson_distribution<_IntType, _RealType>& __x)
1006 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1007 typedef typename __ostream_type::ios_base __ios_base;
1009 const typename __ios_base::fmtflags __flags = __os.flags();
1010 const _CharT __fill = __os.fill();
1011 const std::streamsize __precision = __os.precision();
1012 const _CharT __space = __os.widen(' ');
1013 __os.flags(__ios_base::scientific | __ios_base::left);
1015 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1017 __os << __x.mean() << __space << __x._M_nd;
1019 __os.flags(__flags);
1021 __os.precision(__precision);
1025 template<typename _IntType, typename _RealType,
1026 typename _CharT, typename _Traits>
1027 std::basic_istream<_CharT, _Traits>&
1028 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1029 poisson_distribution<_IntType, _RealType>& __x)
1031 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1032 typedef typename __istream_type::ios_base __ios_base;
1034 const typename __ios_base::fmtflags __flags = __is.flags();
1035 __is.flags(__ios_base::skipws);
1037 __is >> __x._M_mean >> __x._M_nd;
1038 __x._M_initialize();
1040 __is.flags(__flags);
1045 template<typename _IntType, typename _RealType>
1047 binomial_distribution<_IntType, _RealType>::
1050 const _RealType __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1054 #if _GLIBCXX_USE_C99_MATH_TR1
1055 if (_M_t * __p12 >= 8)
1058 const _RealType __np = std::floor(_M_t * __p12);
1059 const _RealType __pa = __np / _M_t;
1060 const _RealType __1p = 1 - __pa;
1062 const _RealType __pi_4 = 0.7853981633974483096156608458198757L;
1063 const _RealType __d1x =
1064 std::sqrt(__np * __1p * std::log(32 * __np
1065 / (81 * __pi_4 * __1p)));
1066 _M_d1 = std::tr1::round(std::max(_RealType(1), __d1x));
1067 const _RealType __d2x =
1068 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1069 / (__pi_4 * __pa)));
1070 _M_d2 = std::tr1::round(std::max(_RealType(1), __d2x));
1073 const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
1074 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1075 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1076 _M_c = 2 * _M_d1 / __np;
1077 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1078 const _RealType __a12 = _M_a1 + _M_s2 * __spi_2;
1079 const _RealType __s1s = _M_s1 * _M_s1;
1080 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1082 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1083 const _RealType __s2s = _M_s2 * _M_s2;
1084 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1085 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1086 _M_lf = (std::tr1::lgamma(__np + 1)
1087 + std::tr1::lgamma(_M_t - __np + 1));
1088 _M_lp1p = std::log(__pa / __1p);
1090 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1094 _M_q = -std::log(1 - __p12);
1097 template<typename _IntType, typename _RealType>
1098 template<class _UniformRandomNumberGenerator>
1099 typename binomial_distribution<_IntType, _RealType>::result_type
1100 binomial_distribution<_IntType, _RealType>::
1101 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1104 _RealType __sum = 0;
1108 const _RealType __e = -std::log(__urng());
1109 __sum += __e / (__t - __x);
1112 while (__sum <= _M_q);
1118 * A rejection algorithm when t * p >= 8 and a simple waiting time
1119 * method - the second in the referenced book - otherwise.
1120 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1124 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1125 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1127 template<typename _IntType, typename _RealType>
1128 template<class _UniformRandomNumberGenerator>
1129 typename binomial_distribution<_IntType, _RealType>::result_type
1130 binomial_distribution<_IntType, _RealType>::
1131 operator()(_UniformRandomNumberGenerator& __urng)
1134 const _RealType __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1136 #if _GLIBCXX_USE_C99_MATH_TR1
1141 // See comments above...
1142 const _RealType __naf =
1143 (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
1144 const _RealType __thr =
1145 std::numeric_limits<_IntType>::max() + __naf;
1147 const _RealType __np = std::floor(_M_t * __p12);
1148 const _RealType __pa = __np / _M_t;
1151 const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
1152 const _RealType __a1 = _M_a1;
1153 const _RealType __a12 = __a1 + _M_s2 * __spi_2;
1154 const _RealType __a123 = _M_a123;
1155 const _RealType __s1s = _M_s1 * _M_s1;
1156 const _RealType __s2s = _M_s2 * _M_s2;
1161 const _RealType __u = _M_s * __urng();
1167 const _RealType __n = _M_nd(__urng);
1168 const _RealType __y = _M_s1 * std::abs(__n);
1169 __reject = __y >= _M_d1;
1172 const _RealType __e = -std::log(__urng());
1173 __x = std::floor(__y);
1174 __v = -__e - __n * __n / 2 + _M_c;
1177 else if (__u <= __a12)
1179 const _RealType __n = _M_nd(__urng);
1180 const _RealType __y = _M_s2 * std::abs(__n);
1181 __reject = __y >= _M_d2;
1184 const _RealType __e = -std::log(__urng());
1185 __x = std::floor(-__y);
1186 __v = -__e - __n * __n / 2;
1189 else if (__u <= __a123)
1191 const _RealType __e1 = -std::log(__urng());
1192 const _RealType __e2 = -std::log(__urng());
1194 const _RealType __y = _M_d1 + 2 * __s1s * __e1 / _M_d1;
1195 __x = std::floor(__y);
1196 __v = (-__e2 + _M_d1 * (1 / (_M_t - __np)
1197 -__y / (2 * __s1s)));
1202 const _RealType __e1 = -std::log(__urng());
1203 const _RealType __e2 = -std::log(__urng());
1205 const _RealType __y = _M_d2 + 2 * __s2s * __e1 / _M_d2;
1206 __x = std::floor(-__y);
1207 __v = -__e2 - _M_d2 * __y / (2 * __s2s);
1211 __reject = __reject || __x < -__np || __x > _M_t - __np;
1214 const _RealType __lfx =
1215 std::tr1::lgamma(__np + __x + 1)
1216 + std::tr1::lgamma(_M_t - (__np + __x) + 1);
1217 __reject = __v > _M_lf - __lfx + __x * _M_lp1p;
1220 __reject |= __x + __np >= __thr;
1224 __x += __np + __naf;
1226 const _IntType __z = _M_waiting(__urng, _M_t - _IntType(__x));
1227 __ret = _IntType(__x) + __z;
1231 __ret = _M_waiting(__urng, _M_t);
1234 __ret = _M_t - __ret;
1238 template<typename _IntType, typename _RealType,
1239 typename _CharT, typename _Traits>
1240 std::basic_ostream<_CharT, _Traits>&
1241 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1242 const binomial_distribution<_IntType, _RealType>& __x)
1244 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1245 typedef typename __ostream_type::ios_base __ios_base;
1247 const typename __ios_base::fmtflags __flags = __os.flags();
1248 const _CharT __fill = __os.fill();
1249 const std::streamsize __precision = __os.precision();
1250 const _CharT __space = __os.widen(' ');
1251 __os.flags(__ios_base::scientific | __ios_base::left);
1253 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1255 __os << __x.t() << __space << __x.p()
1256 << __space << __x._M_nd;
1258 __os.flags(__flags);
1260 __os.precision(__precision);
1264 template<typename _IntType, typename _RealType,
1265 typename _CharT, typename _Traits>
1266 std::basic_istream<_CharT, _Traits>&
1267 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1268 binomial_distribution<_IntType, _RealType>& __x)
1270 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1271 typedef typename __istream_type::ios_base __ios_base;
1273 const typename __ios_base::fmtflags __flags = __is.flags();
1274 __is.flags(__ios_base::dec | __ios_base::skipws);
1276 __is >> __x._M_t >> __x._M_p >> __x._M_nd;
1277 __x._M_initialize();
1279 __is.flags(__flags);
1284 template<typename _RealType, typename _CharT, typename _Traits>
1285 std::basic_ostream<_CharT, _Traits>&
1286 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1287 const uniform_real<_RealType>& __x)
1289 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1290 typedef typename __ostream_type::ios_base __ios_base;
1292 const typename __ios_base::fmtflags __flags = __os.flags();
1293 const _CharT __fill = __os.fill();
1294 const std::streamsize __precision = __os.precision();
1295 const _CharT __space = __os.widen(' ');
1296 __os.flags(__ios_base::scientific | __ios_base::left);
1298 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1300 __os << __x.min() << __space << __x.max();
1302 __os.flags(__flags);
1304 __os.precision(__precision);
1308 template<typename _RealType, typename _CharT, typename _Traits>
1309 std::basic_istream<_CharT, _Traits>&
1310 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1311 uniform_real<_RealType>& __x)
1313 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1314 typedef typename __istream_type::ios_base __ios_base;
1316 const typename __ios_base::fmtflags __flags = __is.flags();
1317 __is.flags(__ios_base::skipws);
1319 __is >> __x._M_min >> __x._M_max;
1321 __is.flags(__flags);
1326 template<typename _RealType, typename _CharT, typename _Traits>
1327 std::basic_ostream<_CharT, _Traits>&
1328 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1329 const exponential_distribution<_RealType>& __x)
1331 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1332 typedef typename __ostream_type::ios_base __ios_base;
1334 const typename __ios_base::fmtflags __flags = __os.flags();
1335 const _CharT __fill = __os.fill();
1336 const std::streamsize __precision = __os.precision();
1337 __os.flags(__ios_base::scientific | __ios_base::left);
1338 __os.fill(__os.widen(' '));
1339 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1341 __os << __x.lambda();
1343 __os.flags(__flags);
1345 __os.precision(__precision);
1351 * Polar method due to Marsaglia.
1353 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1354 * New York, 1986, Ch. V, Sect. 4.4.
1356 template<typename _RealType>
1357 template<class _UniformRandomNumberGenerator>
1358 typename normal_distribution<_RealType>::result_type
1359 normal_distribution<_RealType>::
1360 operator()(_UniformRandomNumberGenerator& __urng)
1364 if (_M_saved_available)
1366 _M_saved_available = false;
1371 result_type __x, __y, __r2;
1374 __x = result_type(2.0) * __urng() - 1.0;
1375 __y = result_type(2.0) * __urng() - 1.0;
1376 __r2 = __x * __x + __y * __y;
1378 while (__r2 > 1.0 || __r2 == 0.0);
1380 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1381 _M_saved = __x * __mult;
1382 _M_saved_available = true;
1383 __ret = __y * __mult;
1386 __ret = __ret * _M_sigma + _M_mean;
1390 template<typename _RealType, typename _CharT, typename _Traits>
1391 std::basic_ostream<_CharT, _Traits>&
1392 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1393 const normal_distribution<_RealType>& __x)
1395 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1396 typedef typename __ostream_type::ios_base __ios_base;
1398 const typename __ios_base::fmtflags __flags = __os.flags();
1399 const _CharT __fill = __os.fill();
1400 const std::streamsize __precision = __os.precision();
1401 const _CharT __space = __os.widen(' ');
1402 __os.flags(__ios_base::scientific | __ios_base::left);
1404 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1406 __os << __x._M_saved_available << __space
1407 << __x.mean() << __space
1409 if (__x._M_saved_available)
1410 __os << __space << __x._M_saved;
1412 __os.flags(__flags);
1414 __os.precision(__precision);
1418 template<typename _RealType, typename _CharT, typename _Traits>
1419 std::basic_istream<_CharT, _Traits>&
1420 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1421 normal_distribution<_RealType>& __x)
1423 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1424 typedef typename __istream_type::ios_base __ios_base;
1426 const typename __ios_base::fmtflags __flags = __is.flags();
1427 __is.flags(__ios_base::dec | __ios_base::skipws);
1429 __is >> __x._M_saved_available >> __x._M_mean
1431 if (__x._M_saved_available)
1432 __is >> __x._M_saved;
1434 __is.flags(__flags);
1439 template<typename _RealType>
1441 gamma_distribution<_RealType>::
1445 _M_l_d = std::sqrt(2 * _M_alpha - 1);
1447 _M_l_d = (std::pow(_M_alpha, _M_alpha / (1 - _M_alpha))
1452 * Cheng's rejection algorithm GB for alpha >= 1 and a modification
1453 * of Vaduva's rejection from Weibull algorithm due to Devroye for
1457 * Cheng, R. C. "The Generation of Gamma Random Variables with Non-integral
1458 * Shape Parameter." Applied Statistics, 26, 71-75, 1977.
1460 * Vaduva, I. "Computer Generation of Gamma Gandom Variables by Rejection
1461 * and Composition Procedures." Math. Operationsforschung and Statistik,
1462 * Series in Statistics, 8, 545-576, 1977.
1464 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1465 * New York, 1986, Ch. IX, Sect. 3.4 (+ Errata!).
1467 template<typename _RealType>
1468 template<class _UniformRandomNumberGenerator>
1469 typename gamma_distribution<_RealType>::result_type
1470 gamma_distribution<_RealType>::
1471 operator()(_UniformRandomNumberGenerator& __urng)
1479 const result_type __b = _M_alpha
1480 - result_type(1.3862943611198906188344642429163531L);
1481 const result_type __c = _M_alpha + _M_l_d;
1482 const result_type __1l = 1 / _M_l_d;
1485 const result_type __k = 2.5040773967762740733732583523868748L;
1489 const result_type __u = __urng();
1490 const result_type __v = __urng();
1492 const result_type __y = __1l * std::log(__v / (1 - __v));
1493 __x = _M_alpha * std::exp(__y);
1495 const result_type __z = __u * __v * __v;
1496 const result_type __r = __b + __c * __y - __x;
1498 __reject = __r < result_type(4.5) * __z - __k;
1500 __reject = __r < std::log(__z);
1506 const result_type __c = 1 / _M_alpha;
1510 const result_type __z = -std::log(__urng());
1511 const result_type __e = -std::log(__urng());
1513 __x = std::pow(__z, __c);
1515 __reject = __z + __e < _M_l_d + __x;
1523 template<typename _RealType, typename _CharT, typename _Traits>
1524 std::basic_ostream<_CharT, _Traits>&
1525 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1526 const gamma_distribution<_RealType>& __x)
1528 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1529 typedef typename __ostream_type::ios_base __ios_base;
1531 const typename __ios_base::fmtflags __flags = __os.flags();
1532 const _CharT __fill = __os.fill();
1533 const std::streamsize __precision = __os.precision();
1534 __os.flags(__ios_base::scientific | __ios_base::left);
1535 __os.fill(__os.widen(' '));
1536 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1538 __os << __x.alpha();
1540 __os.flags(__flags);
1542 __os.precision(__precision);
1546 _GLIBCXX_END_NAMESPACE