1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/random.tcc
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
36 * (Further) implementation-space details.
40 // General case for x = (ax + c) mod m -- use Schrage's algorithm to
41 // avoid integer overflow.
43 // Because a and c are compile-time integral constants the compiler
44 // kindly elides any unreachable paths.
46 // Preconditions: a > 0, m > 0.
48 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
58 static const _Tp __q = __m / __a;
59 static const _Tp __r = __m % __a;
61 _Tp __t1 = __a * (__x % __q);
62 _Tp __t2 = __r * (__x / __q);
66 __x = __m - __t2 + __t1;
71 const _Tp __d = __m - __x;
81 // Special case for m == 0 -- use unsigned integer overflow as modulo
83 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
84 struct _Mod<_Tp, __a, __c, __m, true>
88 { return __a * __x + __c; }
90 } // namespace __detail
93 * Seeds the LCR with integral value @p __s, adjusted so that the
94 * ring identity is never a member of the convergence set.
96 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
98 linear_congruential_engine<_UIntType, __a, __c, __m>::
101 if ((__detail::__mod<_UIntType, 1U, 0U, __m>(__c) == 0U)
102 && (__detail::__mod<_UIntType, 1U, 0U, __m>(__s) == 0U))
103 _M_x = __detail::__mod<_UIntType, 1U, 0U, __m>(1U);
105 _M_x = __detail::__mod<_UIntType, 1U, 0U, __m>(__s);
109 * Seeds the LCR engine with a value generated by @p __q.
111 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
113 linear_congruential_engine<_UIntType, __a, __c, __m>::
116 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
118 const _UIntType __k = (__k0 + 31) / 32;
119 _UIntType __arr[__k + 3];
120 __q.generate(__arr + 0, __arr + __k + 3);
121 _UIntType __factor = 1U;
122 _UIntType __sum = 0U;
123 for (size_t __j = 0; __j < __k; ++__j)
125 __sum += __arr[__j + 3] * __factor;
126 __factor *= __detail::_Shift<_UIntType, 32>::__value;
131 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
132 typename _CharT, typename _Traits>
133 std::basic_ostream<_CharT, _Traits>&
134 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
135 const linear_congruential_engine<_UIntType,
136 __a, __c, __m>& __lcr)
138 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
139 typedef typename __ostream_type::ios_base __ios_base;
141 const typename __ios_base::fmtflags __flags = __os.flags();
142 const _CharT __fill = __os.fill();
143 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
144 __os.fill(__os.widen(' '));
153 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
154 typename _CharT, typename _Traits>
155 std::basic_istream<_CharT, _Traits>&
156 operator>>(std::basic_istream<_CharT, _Traits>& __is,
157 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
159 typedef std::basic_istream<_CharT, _Traits> __istream_type;
160 typedef typename __istream_type::ios_base __ios_base;
162 const typename __ios_base::fmtflags __flags = __is.flags();
163 __is.flags(__ios_base::dec);
172 template<typename _UIntType,
173 size_t __w, size_t __n, size_t __m, size_t __r,
174 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
175 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
178 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
179 __s, __b, __t, __c, __l, __f>::
180 seed(result_type __sd)
182 _M_x[0] = __detail::__mod<_UIntType, 1, 0,
183 __detail::_Shift<_UIntType, __w>::__value>(__sd);
185 for (size_t __i = 1; __i < state_size; ++__i)
187 _UIntType __x = _M_x[__i - 1];
188 __x ^= __x >> (__w - 2);
191 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
192 __detail::_Shift<_UIntType, __w>::__value>(__x);
197 template<typename _UIntType,
198 size_t __w, size_t __n, size_t __m, size_t __r,
199 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
200 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
203 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
204 __s, __b, __t, __c, __l, __f>::
207 const _UIntType __upper_mask = (~_UIntType()) << __r;
208 const size_t __k = (__w + 31) / 32;
209 _UIntType __arr[__k * __n];
210 __q.generate(__arr + 0, __arr + __k * __n);
213 for (size_t __i = 0; __i < state_size; ++__i)
215 _UIntType __factor = 1U;
216 _UIntType __sum = 0U;
217 for (size_t __j = 0; __j < __k; ++__j)
219 __sum += __arr[__i * __k + __j] * __factor;
220 __factor *= __detail::_Shift<_UIntType, 32>::__value;
222 _M_x[__i] = __detail::__mod<_UIntType, 1U, 0U,
223 __detail::_Shift<_UIntType, __w>::__value>(__sum);
229 if ((_M_x[0] & __upper_mask) != 0U)
232 else if (_M_x[__i] != 0U)
237 _M_x[0] = __detail::_Shift<_UIntType, __w - 1U>::__value;
240 template<typename _UIntType, size_t __w,
241 size_t __n, size_t __m, size_t __r,
242 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
243 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
246 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
247 __s, __b, __t, __c, __l, __f>::result_type
248 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
249 __s, __b, __t, __c, __l, __f>::
252 // Reload the vector - cost is O(n) amortized over n calls.
253 if (_M_p >= state_size)
255 const _UIntType __upper_mask = (~_UIntType()) << __r;
256 const _UIntType __lower_mask = ~__upper_mask;
258 for (size_t __k = 0; __k < (__n - __m); ++__k)
260 _UIntType __y = ((_M_x[__k] & __upper_mask)
261 | (_M_x[__k + 1] & __lower_mask));
262 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
263 ^ ((__y & 0x01) ? __a : 0));
266 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
268 _UIntType __y = ((_M_x[__k] & __upper_mask)
269 | (_M_x[__k + 1] & __lower_mask));
270 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
271 ^ ((__y & 0x01) ? __a : 0));
274 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
275 | (_M_x[0] & __lower_mask));
276 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
277 ^ ((__y & 0x01) ? __a : 0));
281 // Calculate o(x(i)).
282 result_type __z = _M_x[_M_p++];
283 __z ^= (__z >> __u) & __d;
284 __z ^= (__z << __s) & __b;
285 __z ^= (__z << __t) & __c;
291 template<typename _UIntType, size_t __w,
292 size_t __n, size_t __m, size_t __r,
293 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
294 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
295 _UIntType __f, typename _CharT, typename _Traits>
296 std::basic_ostream<_CharT, _Traits>&
297 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
298 const mersenne_twister_engine<_UIntType, __w, __n, __m,
299 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
301 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
302 typedef typename __ostream_type::ios_base __ios_base;
304 const typename __ios_base::fmtflags __flags = __os.flags();
305 const _CharT __fill = __os.fill();
306 const _CharT __space = __os.widen(' ');
307 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
310 for (size_t __i = 0; __i < __n - 1; ++__i)
311 __os << __x._M_x[__i] << __space;
312 __os << __x._M_x[__n - 1];
319 template<typename _UIntType, size_t __w,
320 size_t __n, size_t __m, size_t __r,
321 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
322 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
323 _UIntType __f, typename _CharT, typename _Traits>
324 std::basic_istream<_CharT, _Traits>&
325 operator>>(std::basic_istream<_CharT, _Traits>& __is,
326 mersenne_twister_engine<_UIntType, __w, __n, __m,
327 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
329 typedef std::basic_istream<_CharT, _Traits> __istream_type;
330 typedef typename __istream_type::ios_base __ios_base;
332 const typename __ios_base::fmtflags __flags = __is.flags();
333 __is.flags(__ios_base::dec | __ios_base::skipws);
335 for (size_t __i = 0; __i < __n; ++__i)
336 __is >> __x._M_x[__i];
343 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
345 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
346 seed(result_type __value)
349 __value = default_seed;
351 std::linear_congruential_engine<result_type, 40014U, 0U, 2147483563U>
354 // I hope this is right. The "10000" tests work for the ranluxen.
355 const size_t __n = (word_size + 31) / 32;
357 for (size_t __i = 0; __i < long_lag; ++__i)
359 _UIntType __sum = 0U;
360 _UIntType __factor = 1U;
361 for (size_t __j = 0; __j < __n; ++__j)
363 __sum += __detail::__mod<__detail::_UInt32Type, 1, 0, 0>
364 (__lcg()) * __factor;
365 __factor *= __detail::_Shift<_UIntType, 32>::__value;
367 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
368 __detail::_Shift<_UIntType, __w>::__value>(__sum);
370 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
374 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
376 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
379 const size_t __n = (word_size + 31) / 32;
380 _UIntType __arr[long_lag + __n];
381 __q.generate(__arr + 0, __arr + long_lag + __n);
383 for (size_t __i = 0; __i < long_lag; ++__i)
385 _UIntType __sum = 0U;
386 _UIntType __factor = 1U;
387 for (size_t __j = 0; __j < __n; ++__j)
389 __sum += __detail::__mod<__detail::_UInt32Type, 1, 0, 0>
390 (__arr[__i * __n + __j]) * __factor;
391 __factor *= __detail::_Shift<_UIntType, 32>::__value;
393 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
394 __detail::_Shift<_UIntType, __w>::__value>(__sum);
396 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
400 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
401 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
403 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
406 // Derive short lag index from current index.
407 long __ps = _M_p - short_lag;
411 // Calculate new x(i) without overflow or division.
412 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
415 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
417 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
422 __xi = (__detail::_Shift<_UIntType, __w>::__value
423 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
428 // Adjust current index to loop around in ring buffer.
429 if (++_M_p >= long_lag)
435 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
436 typename _CharT, typename _Traits>
437 std::basic_ostream<_CharT, _Traits>&
438 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
439 const subtract_with_carry_engine<_UIntType,
442 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
443 typedef typename __ostream_type::ios_base __ios_base;
445 const typename __ios_base::fmtflags __flags = __os.flags();
446 const _CharT __fill = __os.fill();
447 const _CharT __space = __os.widen(' ');
448 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
451 for (size_t __i = 0; __i < __r; ++__i)
452 __os << __x._M_x[__i] << __space;
453 __os << __x._M_carry;
460 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
461 typename _CharT, typename _Traits>
462 std::basic_istream<_CharT, _Traits>&
463 operator>>(std::basic_istream<_CharT, _Traits>& __is,
464 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
466 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
467 typedef typename __istream_type::ios_base __ios_base;
469 const typename __ios_base::fmtflags __flags = __is.flags();
470 __is.flags(__ios_base::dec | __ios_base::skipws);
472 for (size_t __i = 0; __i < __r; ++__i)
473 __is >> __x._M_x[__i];
474 __is >> __x._M_carry;
481 template<typename _RandomNumberEngine, size_t __p, size_t __r>
482 typename discard_block_engine<_RandomNumberEngine,
483 __p, __r>::result_type
484 discard_block_engine<_RandomNumberEngine, __p, __r>::
487 if (_M_n >= used_block)
489 _M_b.discard(block_size - _M_n);
496 template<typename _RandomNumberEngine, size_t __p, size_t __r,
497 typename _CharT, typename _Traits>
498 std::basic_ostream<_CharT, _Traits>&
499 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
500 const discard_block_engine<_RandomNumberEngine,
503 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
504 typedef typename __ostream_type::ios_base __ios_base;
506 const typename __ios_base::fmtflags __flags = __os.flags();
507 const _CharT __fill = __os.fill();
508 const _CharT __space = __os.widen(' ');
509 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
512 __os << __x.base() << __space << __x._M_n;
519 template<typename _RandomNumberEngine, size_t __p, size_t __r,
520 typename _CharT, typename _Traits>
521 std::basic_istream<_CharT, _Traits>&
522 operator>>(std::basic_istream<_CharT, _Traits>& __is,
523 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
525 typedef std::basic_istream<_CharT, _Traits> __istream_type;
526 typedef typename __istream_type::ios_base __ios_base;
528 const typename __ios_base::fmtflags __flags = __is.flags();
529 __is.flags(__ios_base::dec | __ios_base::skipws);
531 __is >> __x._M_b >> __x._M_n;
538 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
539 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
541 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
544 const long double __r = static_cast<long double>(_M_b.max())
545 - static_cast<long double>(_M_b.min()) + 1.0L;
546 const result_type __m = std::log(__r) / std::log(2.0L);
547 result_type __n, __n0, __y0, __y1, __s0, __s1;
548 for (size_t __i = 0; __i < 2; ++__i)
550 __n = (__w + __m - 1) / __m + __i;
551 __n0 = __n - __w % __n;
552 const result_type __w0 = __w / __n;
553 const result_type __w1 = __w0 + 1;
554 __s0 = result_type(1) << __w0;
555 __s1 = result_type(1) << __w1;
556 __y0 = __s0 * (__r / __s0);
557 __y1 = __s1 * (__r / __s1);
558 if (__r - __y0 <= __y0 / __n)
562 result_type __sum = 0;
563 for (size_t __k = 0; __k < __n0; ++__k)
567 __u = _M_b() - _M_b.min();
569 __sum = __s0 * __sum + __u % __s0;
571 for (size_t __k = __n0; __k < __n; ++__k)
575 __u = _M_b() - _M_b.min();
577 __sum = __s1 * __sum + __u % __s1;
583 template<typename _RandomNumberEngine, size_t __k>
584 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
585 shuffle_order_engine<_RandomNumberEngine, __k>::
588 size_t __j = __k * ((_M_y - _M_b.min())
589 / (_M_b.max() - _M_b.min() + 1.0L));
596 template<typename _RandomNumberEngine, size_t __k,
597 typename _CharT, typename _Traits>
598 std::basic_ostream<_CharT, _Traits>&
599 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
600 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
602 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
603 typedef typename __ostream_type::ios_base __ios_base;
605 const typename __ios_base::fmtflags __flags = __os.flags();
606 const _CharT __fill = __os.fill();
607 const _CharT __space = __os.widen(' ');
608 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
612 for (size_t __i = 0; __i < __k; ++__i)
613 __os << __space << __x._M_v[__i];
614 __os << __space << __x._M_y;
621 template<typename _RandomNumberEngine, size_t __k,
622 typename _CharT, typename _Traits>
623 std::basic_istream<_CharT, _Traits>&
624 operator>>(std::basic_istream<_CharT, _Traits>& __is,
625 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
627 typedef std::basic_istream<_CharT, _Traits> __istream_type;
628 typedef typename __istream_type::ios_base __ios_base;
630 const typename __ios_base::fmtflags __flags = __is.flags();
631 __is.flags(__ios_base::dec | __ios_base::skipws);
634 for (size_t __i = 0; __i < __k; ++__i)
635 __is >> __x._M_v[__i];
643 template<typename _IntType>
644 template<typename _UniformRandomNumberGenerator>
645 typename uniform_int_distribution<_IntType>::result_type
646 uniform_int_distribution<_IntType>::
647 _M_call(_UniformRandomNumberGenerator& __urng,
648 result_type __min, result_type __max, true_type)
650 // XXX Must be fixed to work well for *arbitrary* __urng.max(),
651 // __urng.min(), __max, __min. Currently works fine only in the
652 // most common case __urng.max() - __urng.min() >= __max - __min,
653 // with __urng.max() > __urng.min() >= 0.
654 typedef typename __gnu_cxx::__add_unsigned<typename
655 _UniformRandomNumberGenerator::result_type>::__type __urntype;
656 typedef typename __gnu_cxx::__add_unsigned<result_type>::__type
658 typedef typename __gnu_cxx::__conditional_type<(sizeof(__urntype)
660 __urntype, __utype>::__type __uctype;
664 const __urntype __urnmin = __urng.min();
665 const __urntype __urnmax = __urng.max();
666 const __urntype __urnrange = __urnmax - __urnmin;
667 const __uctype __urange = __max - __min;
668 const __uctype __udenom = (__urnrange <= __urange
669 ? 1 : __urnrange / (__urange + 1));
671 __ret = (__urntype(__urng()) - __urnmin) / __udenom;
672 while (__ret > __max - __min);
674 return __ret + __min;
677 template<typename _IntType, typename _CharT, typename _Traits>
678 std::basic_ostream<_CharT, _Traits>&
679 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
680 const uniform_int_distribution<_IntType>& __x)
682 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
683 typedef typename __ostream_type::ios_base __ios_base;
685 const typename __ios_base::fmtflags __flags = __os.flags();
686 const _CharT __fill = __os.fill();
687 const _CharT __space = __os.widen(' ');
688 __os.flags(__ios_base::scientific | __ios_base::left);
691 __os << __x.a() << __space << __x.b();
698 template<typename _IntType, typename _CharT, typename _Traits>
699 std::basic_istream<_CharT, _Traits>&
700 operator>>(std::basic_istream<_CharT, _Traits>& __is,
701 uniform_int_distribution<_IntType>& __x)
703 typedef std::basic_istream<_CharT, _Traits> __istream_type;
704 typedef typename __istream_type::ios_base __ios_base;
706 const typename __ios_base::fmtflags __flags = __is.flags();
707 __is.flags(__ios_base::dec | __ios_base::skipws);
711 __x.param(typename uniform_int_distribution<_IntType>::
712 param_type(__a, __b));
719 template<typename _RealType, typename _CharT, typename _Traits>
720 std::basic_ostream<_CharT, _Traits>&
721 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
722 const uniform_real_distribution<_RealType>& __x)
724 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
725 typedef typename __ostream_type::ios_base __ios_base;
727 const typename __ios_base::fmtflags __flags = __os.flags();
728 const _CharT __fill = __os.fill();
729 const std::streamsize __precision = __os.precision();
730 const _CharT __space = __os.widen(' ');
731 __os.flags(__ios_base::scientific | __ios_base::left);
733 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
735 __os << __x.a() << __space << __x.b();
739 __os.precision(__precision);
743 template<typename _RealType, typename _CharT, typename _Traits>
744 std::basic_istream<_CharT, _Traits>&
745 operator>>(std::basic_istream<_CharT, _Traits>& __is,
746 uniform_real_distribution<_RealType>& __x)
748 typedef std::basic_istream<_CharT, _Traits> __istream_type;
749 typedef typename __istream_type::ios_base __ios_base;
751 const typename __ios_base::fmtflags __flags = __is.flags();
752 __is.flags(__ios_base::skipws);
756 __x.param(typename uniform_real_distribution<_RealType>::
757 param_type(__a, __b));
764 template<typename _CharT, typename _Traits>
765 std::basic_ostream<_CharT, _Traits>&
766 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
767 const bernoulli_distribution& __x)
769 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
770 typedef typename __ostream_type::ios_base __ios_base;
772 const typename __ios_base::fmtflags __flags = __os.flags();
773 const _CharT __fill = __os.fill();
774 const std::streamsize __precision = __os.precision();
775 __os.flags(__ios_base::scientific | __ios_base::left);
776 __os.fill(__os.widen(' '));
777 __os.precision(std::numeric_limits<double>::digits10 + 1);
783 __os.precision(__precision);
788 template<typename _IntType>
789 template<typename _UniformRandomNumberGenerator>
790 typename geometric_distribution<_IntType>::result_type
791 geometric_distribution<_IntType>::
792 operator()(_UniformRandomNumberGenerator& __urng,
793 const param_type& __param)
795 // About the epsilon thing see this thread:
796 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
798 (1 - std::numeric_limits<double>::epsilon()) / 2;
799 // The largest _RealType convertible to _IntType.
801 std::numeric_limits<_IntType>::max() + __naf;
802 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
807 __cand = std::ceil(std::log(__aurng()) / __param._M_log_p);
808 while (__cand >= __thr);
810 return result_type(__cand + __naf);
813 template<typename _IntType,
814 typename _CharT, typename _Traits>
815 std::basic_ostream<_CharT, _Traits>&
816 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
817 const geometric_distribution<_IntType>& __x)
819 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
820 typedef typename __ostream_type::ios_base __ios_base;
822 const typename __ios_base::fmtflags __flags = __os.flags();
823 const _CharT __fill = __os.fill();
824 const std::streamsize __precision = __os.precision();
825 __os.flags(__ios_base::scientific | __ios_base::left);
826 __os.fill(__os.widen(' '));
827 __os.precision(std::numeric_limits<double>::digits10 + 1);
833 __os.precision(__precision);
837 template<typename _IntType,
838 typename _CharT, typename _Traits>
839 std::basic_istream<_CharT, _Traits>&
840 operator>>(std::basic_istream<_CharT, _Traits>& __is,
841 geometric_distribution<_IntType>& __x)
843 typedef std::basic_istream<_CharT, _Traits> __istream_type;
844 typedef typename __istream_type::ios_base __ios_base;
846 const typename __ios_base::fmtflags __flags = __is.flags();
847 __is.flags(__ios_base::skipws);
851 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
857 template<typename _IntType>
858 template<typename _UniformRandomNumberGenerator>
859 typename negative_binomial_distribution<_IntType>::result_type
860 negative_binomial_distribution<_IntType>::
861 operator()(_UniformRandomNumberGenerator& __urng,
862 const param_type& __p)
864 gamma_distribution<> __gamma(__p.k(), 1.0);
865 double __x = __gamma(__urng);
867 poisson_distribution<result_type> __poisson(__x * __p.p()
869 return __poisson(__urng);
872 template<typename _IntType, typename _CharT, typename _Traits>
873 std::basic_ostream<_CharT, _Traits>&
874 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
875 const negative_binomial_distribution<_IntType>& __x)
877 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
878 typedef typename __ostream_type::ios_base __ios_base;
880 const typename __ios_base::fmtflags __flags = __os.flags();
881 const _CharT __fill = __os.fill();
882 const std::streamsize __precision = __os.precision();
883 const _CharT __space = __os.widen(' ');
884 __os.flags(__ios_base::scientific | __ios_base::left);
885 __os.fill(__os.widen(' '));
886 __os.precision(std::numeric_limits<double>::digits10 + 1);
888 __os << __x.k() << __space << __x.p();
892 __os.precision(__precision);
896 template<typename _IntType, typename _CharT, typename _Traits>
897 std::basic_istream<_CharT, _Traits>&
898 operator>>(std::basic_istream<_CharT, _Traits>& __is,
899 negative_binomial_distribution<_IntType>& __x)
901 typedef std::basic_istream<_CharT, _Traits> __istream_type;
902 typedef typename __istream_type::ios_base __ios_base;
904 const typename __ios_base::fmtflags __flags = __is.flags();
905 __is.flags(__ios_base::skipws);
910 __x.param(typename negative_binomial_distribution<_IntType>::
911 param_type(__k, __p));
918 template<typename _IntType>
920 poisson_distribution<_IntType>::param_type::
923 #if _GLIBCXX_USE_C99_MATH_TR1
926 const double __m = std::floor(_M_mean);
927 _M_lm_thr = std::log(_M_mean);
928 _M_lfm = std::lgamma(__m + 1);
929 _M_sm = std::sqrt(__m);
931 const double __pi_4 = 0.7853981633974483096156608458198757L;
932 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
934 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
935 const double __cx = 2 * __m + _M_d;
936 _M_scx = std::sqrt(__cx / 2);
939 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
940 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
945 _M_lm_thr = std::exp(-_M_mean);
949 * A rejection algorithm when mean >= 12 and a simple method based
950 * upon the multiplication of uniform random variates otherwise.
951 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
955 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
956 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
958 template<typename _IntType>
959 template<typename _UniformRandomNumberGenerator>
960 typename poisson_distribution<_IntType>::result_type
961 poisson_distribution<_IntType>::
962 operator()(_UniformRandomNumberGenerator& __urng,
963 const param_type& __param)
965 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
967 #if _GLIBCXX_USE_C99_MATH_TR1
968 if (__param.mean() >= 12)
972 // See comments above...
974 (1 - std::numeric_limits<double>::epsilon()) / 2;
976 std::numeric_limits<_IntType>::max() + __naf;
978 const double __m = std::floor(__param.mean());
980 const double __spi_2 = 1.2533141373155002512078826424055226L;
981 const double __c1 = __param._M_sm * __spi_2;
982 const double __c2 = __param._M_c2b + __c1;
983 const double __c3 = __c2 + 1;
984 const double __c4 = __c3 + 1;
986 const double __e178 = 1.0129030479320018583185514777512983L;
987 const double __c5 = __c4 + __e178;
988 const double __c = __param._M_cb + __c5;
989 const double __2cx = 2 * (2 * __m + __param._M_d);
991 bool __reject = true;
994 const double __u = __c * __aurng();
995 const double __e = -std::log(__aurng());
1001 const double __n = _M_nd(__urng);
1002 const double __y = -std::abs(__n) * __param._M_sm - 1;
1003 __x = std::floor(__y);
1004 __w = -__n * __n / 2;
1008 else if (__u <= __c2)
1010 const double __n = _M_nd(__urng);
1011 const double __y = 1 + std::abs(__n) * __param._M_scx;
1012 __x = std::ceil(__y);
1013 __w = __y * (2 - __y) * __param._M_1cx;
1014 if (__x > __param._M_d)
1017 else if (__u <= __c3)
1018 // NB: This case not in the book, nor in the Errata,
1019 // but should be ok...
1021 else if (__u <= __c4)
1023 else if (__u <= __c5)
1027 const double __v = -std::log(__aurng());
1028 const double __y = __param._M_d
1029 + __v * __2cx / __param._M_d;
1030 __x = std::ceil(__y);
1031 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1034 __reject = (__w - __e - __x * __param._M_lm_thr
1035 > __param._M_lfm - std::lgamma(__x + __m + 1));
1037 __reject |= __x + __m >= __thr;
1041 return result_type(__x + __m + __naf);
1047 double __prod = 1.0;
1051 __prod *= __aurng();
1054 while (__prod > __param._M_lm_thr);
1060 template<typename _IntType,
1061 typename _CharT, typename _Traits>
1062 std::basic_ostream<_CharT, _Traits>&
1063 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1064 const poisson_distribution<_IntType>& __x)
1066 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1067 typedef typename __ostream_type::ios_base __ios_base;
1069 const typename __ios_base::fmtflags __flags = __os.flags();
1070 const _CharT __fill = __os.fill();
1071 const std::streamsize __precision = __os.precision();
1072 const _CharT __space = __os.widen(' ');
1073 __os.flags(__ios_base::scientific | __ios_base::left);
1075 __os.precision(std::numeric_limits<double>::digits10 + 1);
1077 __os << __x.mean() << __space << __x._M_nd;
1079 __os.flags(__flags);
1081 __os.precision(__precision);
1085 template<typename _IntType,
1086 typename _CharT, typename _Traits>
1087 std::basic_istream<_CharT, _Traits>&
1088 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1089 poisson_distribution<_IntType>& __x)
1091 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1092 typedef typename __istream_type::ios_base __ios_base;
1094 const typename __ios_base::fmtflags __flags = __is.flags();
1095 __is.flags(__ios_base::skipws);
1098 __is >> __mean >> __x._M_nd;
1099 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1101 __is.flags(__flags);
1106 template<typename _IntType>
1108 binomial_distribution<_IntType>::param_type::
1111 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1115 #if _GLIBCXX_USE_C99_MATH_TR1
1116 if (_M_t * __p12 >= 8)
1119 const double __np = std::floor(_M_t * __p12);
1120 const double __pa = __np / _M_t;
1121 const double __1p = 1 - __pa;
1123 const double __pi_4 = 0.7853981633974483096156608458198757L;
1124 const double __d1x =
1125 std::sqrt(__np * __1p * std::log(32 * __np
1126 / (81 * __pi_4 * __1p)));
1127 _M_d1 = std::round(std::max(1.0, __d1x));
1128 const double __d2x =
1129 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1130 / (__pi_4 * __pa)));
1131 _M_d2 = std::round(std::max(1.0, __d2x));
1134 const double __spi_2 = 1.2533141373155002512078826424055226L;
1135 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1136 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1137 _M_c = 2 * _M_d1 / __np;
1138 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1139 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1140 const double __s1s = _M_s1 * _M_s1;
1141 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1143 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1144 const double __s2s = _M_s2 * _M_s2;
1145 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1146 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1147 _M_lf = (std::lgamma(__np + 1)
1148 + std::lgamma(_M_t - __np + 1));
1149 _M_lp1p = std::log(__pa / __1p);
1151 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1155 _M_q = -std::log(1 - __p12);
1158 template<typename _IntType>
1159 template<typename _UniformRandomNumberGenerator>
1160 typename binomial_distribution<_IntType>::result_type
1161 binomial_distribution<_IntType>::
1162 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1166 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1171 const double __e = -std::log(__aurng());
1172 __sum += __e / (__t - __x);
1175 while (__sum <= _M_param._M_q);
1181 * A rejection algorithm when t * p >= 8 and a simple waiting time
1182 * method - the second in the referenced book - otherwise.
1183 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1187 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1188 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1190 template<typename _IntType>
1191 template<typename _UniformRandomNumberGenerator>
1192 typename binomial_distribution<_IntType>::result_type
1193 binomial_distribution<_IntType>::
1194 operator()(_UniformRandomNumberGenerator& __urng,
1195 const param_type& __param)
1198 const _IntType __t = __param.t();
1199 const _IntType __p = __param.p();
1200 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1201 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1204 #if _GLIBCXX_USE_C99_MATH_TR1
1205 if (!__param._M_easy)
1209 // See comments above...
1210 const double __naf =
1211 (1 - std::numeric_limits<double>::epsilon()) / 2;
1212 const double __thr =
1213 std::numeric_limits<_IntType>::max() + __naf;
1215 const double __np = std::floor(__t * __p12);
1218 const double __spi_2 = 1.2533141373155002512078826424055226L;
1219 const double __a1 = __param._M_a1;
1220 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1221 const double __a123 = __param._M_a123;
1222 const double __s1s = __param._M_s1 * __param._M_s1;
1223 const double __s2s = __param._M_s2 * __param._M_s2;
1228 const double __u = __param._M_s * __aurng();
1234 const double __n = _M_nd(__urng);
1235 const double __y = __param._M_s1 * std::abs(__n);
1236 __reject = __y >= __param._M_d1;
1239 const double __e = -std::log(__aurng());
1240 __x = std::floor(__y);
1241 __v = -__e - __n * __n / 2 + __param._M_c;
1244 else if (__u <= __a12)
1246 const double __n = _M_nd(__urng);
1247 const double __y = __param._M_s2 * std::abs(__n);
1248 __reject = __y >= __param._M_d2;
1251 const double __e = -std::log(__aurng());
1252 __x = std::floor(-__y);
1253 __v = -__e - __n * __n / 2;
1256 else if (__u <= __a123)
1258 const double __e1 = -std::log(__aurng());
1259 const double __e2 = -std::log(__aurng());
1261 const double __y = __param._M_d1
1262 + 2 * __s1s * __e1 / __param._M_d1;
1263 __x = std::floor(__y);
1264 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1265 -__y / (2 * __s1s)));
1270 const double __e1 = -std::log(__aurng());
1271 const double __e2 = -std::log(__aurng());
1273 const double __y = __param._M_d2
1274 + 2 * __s2s * __e1 / __param._M_d2;
1275 __x = std::floor(-__y);
1276 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1280 __reject = __reject || __x < -__np || __x > __t - __np;
1283 const double __lfx =
1284 std::lgamma(__np + __x + 1)
1285 + std::lgamma(__t - (__np + __x) + 1);
1286 __reject = __v > __param._M_lf - __lfx
1287 + __x * __param._M_lp1p;
1290 __reject |= __x + __np >= __thr;
1294 __x += __np + __naf;
1296 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1297 __ret = _IntType(__x) + __z;
1301 __ret = _M_waiting(__urng, __t);
1304 __ret = __t - __ret;
1308 template<typename _IntType,
1309 typename _CharT, typename _Traits>
1310 std::basic_ostream<_CharT, _Traits>&
1311 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1312 const binomial_distribution<_IntType>& __x)
1314 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1315 typedef typename __ostream_type::ios_base __ios_base;
1317 const typename __ios_base::fmtflags __flags = __os.flags();
1318 const _CharT __fill = __os.fill();
1319 const std::streamsize __precision = __os.precision();
1320 const _CharT __space = __os.widen(' ');
1321 __os.flags(__ios_base::scientific | __ios_base::left);
1323 __os.precision(std::numeric_limits<double>::digits10 + 1);
1325 __os << __x.t() << __space << __x.p()
1326 << __space << __x._M_nd;
1328 __os.flags(__flags);
1330 __os.precision(__precision);
1334 template<typename _IntType,
1335 typename _CharT, typename _Traits>
1336 std::basic_istream<_CharT, _Traits>&
1337 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1338 binomial_distribution<_IntType>& __x)
1340 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1341 typedef typename __istream_type::ios_base __ios_base;
1343 const typename __ios_base::fmtflags __flags = __is.flags();
1344 __is.flags(__ios_base::dec | __ios_base::skipws);
1348 __is >> __t >> __p >> __x._M_nd;
1349 __x.param(typename binomial_distribution<_IntType>::
1350 param_type(__t, __p));
1352 __is.flags(__flags);
1357 template<typename _RealType, typename _CharT, typename _Traits>
1358 std::basic_ostream<_CharT, _Traits>&
1359 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1360 const exponential_distribution<_RealType>& __x)
1362 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1363 typedef typename __ostream_type::ios_base __ios_base;
1365 const typename __ios_base::fmtflags __flags = __os.flags();
1366 const _CharT __fill = __os.fill();
1367 const std::streamsize __precision = __os.precision();
1368 __os.flags(__ios_base::scientific | __ios_base::left);
1369 __os.fill(__os.widen(' '));
1370 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1372 __os << __x.lambda();
1374 __os.flags(__flags);
1376 __os.precision(__precision);
1380 template<typename _RealType, typename _CharT, typename _Traits>
1381 std::basic_istream<_CharT, _Traits>&
1382 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1383 exponential_distribution<_RealType>& __x)
1385 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1386 typedef typename __istream_type::ios_base __ios_base;
1388 const typename __ios_base::fmtflags __flags = __is.flags();
1389 __is.flags(__ios_base::dec | __ios_base::skipws);
1393 __x.param(typename exponential_distribution<_RealType>::
1394 param_type(__lambda));
1396 __is.flags(__flags);
1402 * Polar method due to Marsaglia.
1404 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1405 * New York, 1986, Ch. V, Sect. 4.4.
1407 template<typename _RealType>
1408 template<typename _UniformRandomNumberGenerator>
1409 typename normal_distribution<_RealType>::result_type
1410 normal_distribution<_RealType>::
1411 operator()(_UniformRandomNumberGenerator& __urng,
1412 const param_type& __param)
1415 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1418 if (_M_saved_available)
1420 _M_saved_available = false;
1425 result_type __x, __y, __r2;
1428 __x = result_type(2.0) * __aurng() - 1.0;
1429 __y = result_type(2.0) * __aurng() - 1.0;
1430 __r2 = __x * __x + __y * __y;
1432 while (__r2 > 1.0 || __r2 == 0.0);
1434 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1435 _M_saved = __x * __mult;
1436 _M_saved_available = true;
1437 __ret = __y * __mult;
1440 __ret = __ret * __param.stddev() + __param.mean();
1444 template<typename _RealType, typename _CharT, typename _Traits>
1445 std::basic_ostream<_CharT, _Traits>&
1446 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1447 const normal_distribution<_RealType>& __x)
1449 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1450 typedef typename __ostream_type::ios_base __ios_base;
1452 const typename __ios_base::fmtflags __flags = __os.flags();
1453 const _CharT __fill = __os.fill();
1454 const std::streamsize __precision = __os.precision();
1455 const _CharT __space = __os.widen(' ');
1456 __os.flags(__ios_base::scientific | __ios_base::left);
1458 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1460 __os << __x.mean() << __space << __x.stddev()
1461 << __space << __x._M_saved_available;
1462 if (__x._M_saved_available)
1463 __os << __space << __x._M_saved;
1465 __os.flags(__flags);
1467 __os.precision(__precision);
1471 template<typename _RealType, typename _CharT, typename _Traits>
1472 std::basic_istream<_CharT, _Traits>&
1473 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1474 normal_distribution<_RealType>& __x)
1476 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1477 typedef typename __istream_type::ios_base __ios_base;
1479 const typename __ios_base::fmtflags __flags = __is.flags();
1480 __is.flags(__ios_base::dec | __ios_base::skipws);
1482 double __mean, __stddev;
1483 __is >> __mean >> __stddev
1484 >> __x._M_saved_available;
1485 if (__x._M_saved_available)
1486 __is >> __x._M_saved;
1487 __x.param(typename normal_distribution<_RealType>::
1488 param_type(__mean, __stddev));
1490 __is.flags(__flags);
1495 template<typename _RealType, typename _CharT, typename _Traits>
1496 std::basic_ostream<_CharT, _Traits>&
1497 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1498 const lognormal_distribution<_RealType>& __x)
1500 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1501 typedef typename __ostream_type::ios_base __ios_base;
1503 const typename __ios_base::fmtflags __flags = __os.flags();
1504 const _CharT __fill = __os.fill();
1505 const std::streamsize __precision = __os.precision();
1506 const _CharT __space = __os.widen(' ');
1507 __os.flags(__ios_base::scientific | __ios_base::left);
1509 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1511 __os << __x.m() << __space << __x.s()
1512 << __space << __x._M_nd;
1514 __os.flags(__flags);
1516 __os.precision(__precision);
1520 template<typename _RealType, typename _CharT, typename _Traits>
1521 std::basic_istream<_CharT, _Traits>&
1522 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1523 lognormal_distribution<_RealType>& __x)
1525 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1526 typedef typename __istream_type::ios_base __ios_base;
1528 const typename __ios_base::fmtflags __flags = __is.flags();
1529 __is.flags(__ios_base::dec | __ios_base::skipws);
1532 __is >> __m >> __s >> __x._M_nd;
1533 __x.param(typename lognormal_distribution<_RealType>::
1534 param_type(__m, __s));
1536 __is.flags(__flags);
1541 template<typename _RealType>
1542 template<typename _UniformRandomNumberGenerator>
1543 typename chi_squared_distribution<_RealType>::result_type
1544 chi_squared_distribution<_RealType>::
1545 operator()(_UniformRandomNumberGenerator& __urng,
1546 const param_type& __p)
1548 gamma_distribution<_RealType> __gamma(__p.n() / 2, 1.0);
1549 return 2 * __gamma(__urng);
1552 template<typename _RealType, typename _CharT, typename _Traits>
1553 std::basic_ostream<_CharT, _Traits>&
1554 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1555 const chi_squared_distribution<_RealType>& __x)
1557 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1558 typedef typename __ostream_type::ios_base __ios_base;
1560 const typename __ios_base::fmtflags __flags = __os.flags();
1561 const _CharT __fill = __os.fill();
1562 const std::streamsize __precision = __os.precision();
1563 const _CharT __space = __os.widen(' ');
1564 __os.flags(__ios_base::scientific | __ios_base::left);
1566 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1570 __os.flags(__flags);
1572 __os.precision(__precision);
1576 template<typename _RealType, typename _CharT, typename _Traits>
1577 std::basic_istream<_CharT, _Traits>&
1578 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1579 chi_squared_distribution<_RealType>& __x)
1581 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1582 typedef typename __istream_type::ios_base __ios_base;
1584 const typename __ios_base::fmtflags __flags = __is.flags();
1585 __is.flags(__ios_base::dec | __ios_base::skipws);
1589 __x.param(typename chi_squared_distribution<_RealType>::
1592 __is.flags(__flags);
1597 template<typename _RealType>
1598 template<typename _UniformRandomNumberGenerator>
1599 typename cauchy_distribution<_RealType>::result_type
1600 cauchy_distribution<_RealType>::
1601 operator()(_UniformRandomNumberGenerator& __urng,
1602 const param_type& __p)
1604 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1611 const _RealType __pi = 3.1415926535897932384626433832795029L;
1612 return __p.a() + __p.b() * std::tan(__pi * __u);
1615 template<typename _RealType, typename _CharT, typename _Traits>
1616 std::basic_ostream<_CharT, _Traits>&
1617 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1618 const cauchy_distribution<_RealType>& __x)
1620 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1621 typedef typename __ostream_type::ios_base __ios_base;
1623 const typename __ios_base::fmtflags __flags = __os.flags();
1624 const _CharT __fill = __os.fill();
1625 const std::streamsize __precision = __os.precision();
1626 const _CharT __space = __os.widen(' ');
1627 __os.flags(__ios_base::scientific | __ios_base::left);
1629 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1631 __os << __x.a() << __space << __x.b();
1633 __os.flags(__flags);
1635 __os.precision(__precision);
1639 template<typename _RealType, typename _CharT, typename _Traits>
1640 std::basic_istream<_CharT, _Traits>&
1641 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1642 cauchy_distribution<_RealType>& __x)
1644 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1645 typedef typename __istream_type::ios_base __ios_base;
1647 const typename __ios_base::fmtflags __flags = __is.flags();
1648 __is.flags(__ios_base::dec | __ios_base::skipws);
1652 __x.param(typename cauchy_distribution<_RealType>::
1653 param_type(__a, __b));
1655 __is.flags(__flags);
1660 template<typename _RealType>
1661 template<typename _UniformRandomNumberGenerator>
1662 typename fisher_f_distribution<_RealType>::result_type
1663 fisher_f_distribution<_RealType>::
1664 operator()(_UniformRandomNumberGenerator& __urng,
1665 const param_type& __p)
1667 gamma_distribution<_RealType> __gamma;
1668 _RealType __ym = __gamma(__urng,
1669 typename gamma_distribution<_RealType>::param_type(__p.m() / 2, 2));
1671 _RealType __yn = __gamma(__urng,
1672 typename gamma_distribution<_RealType>::param_type(__p.n() / 2, 2));
1674 return (__ym * __p.n()) / (__yn * __p.m());
1677 template<typename _RealType, typename _CharT, typename _Traits>
1678 std::basic_ostream<_CharT, _Traits>&
1679 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1680 const fisher_f_distribution<_RealType>& __x)
1682 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1683 typedef typename __ostream_type::ios_base __ios_base;
1685 const typename __ios_base::fmtflags __flags = __os.flags();
1686 const _CharT __fill = __os.fill();
1687 const std::streamsize __precision = __os.precision();
1688 const _CharT __space = __os.widen(' ');
1689 __os.flags(__ios_base::scientific | __ios_base::left);
1691 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1693 __os << __x.m() << __space << __x.n();
1695 __os.flags(__flags);
1697 __os.precision(__precision);
1701 template<typename _RealType, typename _CharT, typename _Traits>
1702 std::basic_istream<_CharT, _Traits>&
1703 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1704 fisher_f_distribution<_RealType>& __x)
1706 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1707 typedef typename __istream_type::ios_base __ios_base;
1709 const typename __ios_base::fmtflags __flags = __is.flags();
1710 __is.flags(__ios_base::dec | __ios_base::skipws);
1714 __x.param(typename fisher_f_distribution<_RealType>::
1715 param_type(__m, __n));
1717 __is.flags(__flags);
1722 template<typename _RealType>
1723 template<typename _UniformRandomNumberGenerator>
1724 typename student_t_distribution<_RealType>::result_type
1725 student_t_distribution<_RealType>::
1726 operator()(_UniformRandomNumberGenerator& __urng,
1727 const param_type& __param)
1729 if (__param.n() <= 2.0)
1731 _RealType __y1 = _M_nd(__urng);
1732 chi_squared_distribution<_RealType> __chisq(__param.n());
1733 _RealType __y2 = __chisq(__urng);
1735 return __y1 / std::sqrt(__y2 / __param.n());
1739 _RealType __y1, __y2, __z;
1740 exponential_distribution<_RealType>
1741 __exponential(1.0 / (__param.n() / 2.0 - 1.0));
1745 __y1 = _M_nd(__urng);
1746 __y2 = __exponential(__urng);
1748 __z = __y1 * __y1 / (__param.n() - 2.0);
1750 while (1.0 - __z < 0.0 || std::exp(-__y2 - __z) > (1.0 - __z));
1752 // Note that there is a typo in Knuth's formula, the line below
1753 // is taken from the original paper of Marsaglia, Mathematics of
1754 // Computation, 34 (1980), p 234-256
1755 return __y1 / std::sqrt((1.0 - 2.0 / __param.n()) * (1.0 - __z));
1759 template<typename _RealType, typename _CharT, typename _Traits>
1760 std::basic_ostream<_CharT, _Traits>&
1761 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1762 const student_t_distribution<_RealType>& __x)
1764 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1765 typedef typename __ostream_type::ios_base __ios_base;
1767 const typename __ios_base::fmtflags __flags = __os.flags();
1768 const _CharT __fill = __os.fill();
1769 const std::streamsize __precision = __os.precision();
1770 const _CharT __space = __os.widen(' ');
1771 __os.flags(__ios_base::scientific | __ios_base::left);
1773 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1775 __os << __x.n() << __space << __x._M_nd;
1777 __os.flags(__flags);
1779 __os.precision(__precision);
1783 template<typename _RealType, typename _CharT, typename _Traits>
1784 std::basic_istream<_CharT, _Traits>&
1785 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1786 student_t_distribution<_RealType>& __x)
1788 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1789 typedef typename __istream_type::ios_base __ios_base;
1791 const typename __ios_base::fmtflags __flags = __is.flags();
1792 __is.flags(__ios_base::dec | __ios_base::skipws);
1795 __is >> __n >> __x._M_nd;
1796 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1798 __is.flags(__flags);
1803 template<typename _RealType>
1805 gamma_distribution<_RealType>::param_type::
1808 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
1810 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
1811 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
1815 * Marsaglia, G. and Tsang, W. W.
1816 * "A Simple Method for Generating Gamma Variables"
1817 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
1819 template<typename _RealType>
1820 template<typename _UniformRandomNumberGenerator>
1821 typename gamma_distribution<_RealType>::result_type
1822 gamma_distribution<_RealType>::
1823 operator()(_UniformRandomNumberGenerator& __urng,
1824 const param_type& __param)
1826 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1829 result_type __u, __v, __n;
1830 const result_type __a1 = (__param._M_malpha
1831 - _RealType(1.0) / _RealType(3.0));
1837 __n = _M_nd(__urng);
1838 __v = result_type(1.0) + __param._M_a2 * __n;
1842 __v = __v * __v * __v;
1845 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
1846 && (std::log(__u) > (0.5 * __n * __n + __a1
1847 * (1.0 - __v + std::log(__v)))));
1849 if (__param.alpha() == __param._M_malpha)
1850 return __a1 * __v * __param.beta();
1857 return (std::pow(__u, result_type(1.0) / __param.alpha())
1858 * __a1 * __v * __param.beta());
1862 template<typename _RealType, typename _CharT, typename _Traits>
1863 std::basic_ostream<_CharT, _Traits>&
1864 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1865 const gamma_distribution<_RealType>& __x)
1867 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1868 typedef typename __ostream_type::ios_base __ios_base;
1870 const typename __ios_base::fmtflags __flags = __os.flags();
1871 const _CharT __fill = __os.fill();
1872 const std::streamsize __precision = __os.precision();
1873 const _CharT __space = __os.widen(' ');
1874 __os.flags(__ios_base::scientific | __ios_base::left);
1876 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1878 __os << __x.alpha() << __space << __x.beta()
1879 << __space << __x._M_nd;
1881 __os.flags(__flags);
1883 __os.precision(__precision);
1887 template<typename _RealType, typename _CharT, typename _Traits>
1888 std::basic_istream<_CharT, _Traits>&
1889 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1890 gamma_distribution<_RealType>& __x)
1892 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1893 typedef typename __istream_type::ios_base __ios_base;
1895 const typename __ios_base::fmtflags __flags = __is.flags();
1896 __is.flags(__ios_base::dec | __ios_base::skipws);
1898 _RealType __alpha_val, __beta_val;
1899 __is >> __alpha_val >> __beta_val >> __x._M_nd;
1900 __x.param(typename gamma_distribution<_RealType>::
1901 param_type(__alpha_val, __beta_val));
1903 __is.flags(__flags);
1908 template<typename _RealType>
1909 template<typename _UniformRandomNumberGenerator>
1910 typename weibull_distribution<_RealType>::result_type
1911 weibull_distribution<_RealType>::
1912 operator()(_UniformRandomNumberGenerator& __urng,
1913 const param_type& __p)
1915 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1917 return __p.b() * std::pow(-std::log(__aurng()),
1918 result_type(1) / __p.a());
1921 template<typename _RealType, typename _CharT, typename _Traits>
1922 std::basic_ostream<_CharT, _Traits>&
1923 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1924 const weibull_distribution<_RealType>& __x)
1926 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1927 typedef typename __ostream_type::ios_base __ios_base;
1929 const typename __ios_base::fmtflags __flags = __os.flags();
1930 const _CharT __fill = __os.fill();
1931 const std::streamsize __precision = __os.precision();
1932 const _CharT __space = __os.widen(' ');
1933 __os.flags(__ios_base::scientific | __ios_base::left);
1935 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1937 __os << __x.a() << __space << __x.b();
1939 __os.flags(__flags);
1941 __os.precision(__precision);
1945 template<typename _RealType, typename _CharT, typename _Traits>
1946 std::basic_istream<_CharT, _Traits>&
1947 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1948 weibull_distribution<_RealType>& __x)
1950 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1951 typedef typename __istream_type::ios_base __ios_base;
1953 const typename __ios_base::fmtflags __flags = __is.flags();
1954 __is.flags(__ios_base::dec | __ios_base::skipws);
1958 __x.param(typename weibull_distribution<_RealType>::
1959 param_type(__a, __b));
1961 __is.flags(__flags);
1966 template<typename _RealType>
1967 template<typename _UniformRandomNumberGenerator>
1968 typename extreme_value_distribution<_RealType>::result_type
1969 extreme_value_distribution<_RealType>::
1970 operator()(_UniformRandomNumberGenerator& __urng,
1971 const param_type& __p)
1973 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1975 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
1978 template<typename _RealType, typename _CharT, typename _Traits>
1979 std::basic_ostream<_CharT, _Traits>&
1980 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1981 const extreme_value_distribution<_RealType>& __x)
1983 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1984 typedef typename __ostream_type::ios_base __ios_base;
1986 const typename __ios_base::fmtflags __flags = __os.flags();
1987 const _CharT __fill = __os.fill();
1988 const std::streamsize __precision = __os.precision();
1989 const _CharT __space = __os.widen(' ');
1990 __os.flags(__ios_base::scientific | __ios_base::left);
1992 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1994 __os << __x.a() << __space << __x.b();
1996 __os.flags(__flags);
1998 __os.precision(__precision);
2002 template<typename _RealType, typename _CharT, typename _Traits>
2003 std::basic_istream<_CharT, _Traits>&
2004 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2005 extreme_value_distribution<_RealType>& __x)
2007 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2008 typedef typename __istream_type::ios_base __ios_base;
2010 const typename __ios_base::fmtflags __flags = __is.flags();
2011 __is.flags(__ios_base::dec | __ios_base::skipws);
2015 __x.param(typename extreme_value_distribution<_RealType>::
2016 param_type(__a, __b));
2018 __is.flags(__flags);
2023 template<typename _IntType>
2025 discrete_distribution<_IntType>::param_type::
2028 if (_M_prob.size() < 2)
2031 _M_prob.push_back(1.0);
2035 double __sum = std::accumulate(_M_prob.begin(), _M_prob.end(), 0.0);
2036 // Now normalize the densities.
2037 std::transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2038 std::bind2nd(std::divides<double>(), __sum));
2039 // Accumulate partial sums.
2040 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2041 std::back_inserter(_M_cp));
2042 // Make sure the last cumulative probablility is one.
2043 _M_cp[_M_cp.size() - 1] = 1.0;
2046 template<typename _IntType>
2047 template<typename _Func>
2048 discrete_distribution<_IntType>::param_type::
2049 param_type(size_t __nw, double __xmin, double __xmax,
2051 : _M_prob(), _M_cp()
2053 for (size_t __i = 0; __i < __nw; ++__i)
2055 const double __x = ((__nw - __i - 0.5) * __xmin
2056 + (__i + 0.5) * __xmax) / __nw;
2057 _M_prob.push_back(__fw(__x));
2063 template<typename _IntType>
2064 template<typename _UniformRandomNumberGenerator>
2065 typename discrete_distribution<_IntType>::result_type
2066 discrete_distribution<_IntType>::
2067 operator()(_UniformRandomNumberGenerator& __urng,
2068 const param_type& __param)
2070 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2073 const double __p = __aurng();
2074 auto __pos = std::lower_bound(__param._M_cp.begin(),
2075 __param._M_cp.end(), __p);
2076 if (__pos == __param._M_cp.end())
2078 const size_t __i = __pos - __param._M_cp.begin();
2083 template<typename _IntType, typename _CharT, typename _Traits>
2084 std::basic_ostream<_CharT, _Traits>&
2085 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2086 const discrete_distribution<_IntType>& __x)
2088 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2089 typedef typename __ostream_type::ios_base __ios_base;
2091 const typename __ios_base::fmtflags __flags = __os.flags();
2092 const _CharT __fill = __os.fill();
2093 const std::streamsize __precision = __os.precision();
2094 const _CharT __space = __os.widen(' ');
2095 __os.flags(__ios_base::scientific | __ios_base::left);
2097 __os.precision(std::numeric_limits<double>::digits10 + 1);
2099 std::vector<double> __prob = __x.probabilities();
2100 __os << __prob.size();
2101 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2102 __os << __space << *__dit;
2104 __os.flags(__flags);
2106 __os.precision(__precision);
2110 template<typename _IntType, typename _CharT, typename _Traits>
2111 std::basic_istream<_CharT, _Traits>&
2112 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2113 discrete_distribution<_IntType>& __x)
2115 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2116 typedef typename __istream_type::ios_base __ios_base;
2118 const typename __ios_base::fmtflags __flags = __is.flags();
2119 __is.flags(__ios_base::dec | __ios_base::skipws);
2124 std::vector<double> __prob_vec;
2125 for (; __n != 0; --__n)
2129 __prob_vec.push_back(__prob);
2132 __x.param(typename discrete_distribution<_IntType>::
2133 param_type(__prob_vec.begin(), __prob_vec.end()));
2135 __is.flags(__flags);
2140 template<typename _RealType>
2142 piecewise_constant_distribution<_RealType>::param_type::
2145 if (_M_int.size() < 2)
2148 _M_int.push_back(_RealType(0));
2149 _M_int.push_back(_RealType(1));
2152 _M_den.push_back(1.0);
2158 for (size_t __i = 0; __i < _M_den.size(); ++__i)
2160 __sum += _M_den[__i] * (_M_int[__i + 1] - _M_int[__i]);
2161 _M_cp.push_back(__sum);
2164 // Now normalize the densities...
2165 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2166 std::bind2nd(std::divides<double>(), __sum));
2167 // ... and partial sums.
2168 std::transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2169 std::bind2nd(std::divides<double>(), __sum));
2170 // Make sure the last cumulative probablility is one.
2171 _M_cp[_M_cp.size() - 1] = 1.0;
2174 template<typename _RealType>
2175 template<typename _InputIteratorB, typename _InputIteratorW>
2176 piecewise_constant_distribution<_RealType>::param_type::
2177 param_type(_InputIteratorB __bbegin,
2178 _InputIteratorB __bend,
2179 _InputIteratorW __wbegin)
2180 : _M_int(), _M_den(), _M_cp()
2184 _M_int.push_back(*__bbegin);
2186 if (__bbegin != __bend)
2188 _M_den.push_back(*__wbegin);
2192 while (__bbegin != __bend);
2197 template<typename _RealType>
2198 template<typename _Func>
2199 piecewise_constant_distribution<_RealType>::param_type::
2200 param_type(initializer_list<_RealType> __bil, _Func __fw)
2201 : _M_int(), _M_den(), _M_cp()
2203 for (auto __biter = __bil.begin(); __biter != __bil.end(); ++__biter)
2204 _M_int.push_back(*__biter);
2206 for (size_t __i = 0; __i < _M_int.size() - 1; ++__i)
2208 _RealType __x = 0.5 * (_M_int[__i] + _M_int[__i + 1]);
2209 _M_den.push_back(__fw(__x));
2215 template<typename _RealType>
2216 template<typename _Func>
2217 piecewise_constant_distribution<_RealType>::param_type::
2218 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2219 : _M_int(), _M_den(), _M_cp()
2221 for (size_t __i = 0; __i <= __nw; ++__i)
2223 const _RealType __x = ((__nw - __i) * __xmin
2224 + __i * __xmax) / __nw;
2225 _M_int.push_back(__x);
2227 for (size_t __i = 0; __i < __nw; ++__i)
2229 const _RealType __x = ((__nw - __i - 0.5) * __xmin
2230 + (__i + 0.5) * __xmax) / __nw;
2231 _M_den.push_back(__fw(__x));
2237 template<typename _RealType>
2238 template<typename _UniformRandomNumberGenerator>
2239 typename piecewise_constant_distribution<_RealType>::result_type
2240 piecewise_constant_distribution<_RealType>::
2241 operator()(_UniformRandomNumberGenerator& __urng,
2242 const param_type& __param)
2244 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2247 const double __p = __aurng();
2248 auto __pos = std::lower_bound(__param._M_cp.begin(),
2249 __param._M_cp.end(), __p);
2250 const size_t __i = __pos - __param._M_cp.begin();
2252 return __param._M_int[__i]
2253 + (__p - __param._M_cp[__i]) / __param._M_den[__i];
2256 template<typename _RealType, typename _CharT, typename _Traits>
2257 std::basic_ostream<_CharT, _Traits>&
2258 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2259 const piecewise_constant_distribution<_RealType>& __x)
2261 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2262 typedef typename __ostream_type::ios_base __ios_base;
2264 const typename __ios_base::fmtflags __flags = __os.flags();
2265 const _CharT __fill = __os.fill();
2266 const std::streamsize __precision = __os.precision();
2267 const _CharT __space = __os.widen(' ');
2268 __os.flags(__ios_base::scientific | __ios_base::left);
2270 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2272 std::vector<_RealType> __int = __x.intervals();
2273 __os << __int.size() - 1;
2275 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2276 __os << __space << *__xit;
2278 std::vector<double> __den = __x.densities();
2279 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2280 __os << __space << *__dit;
2282 __os.flags(__flags);
2284 __os.precision(__precision);
2288 template<typename _RealType, typename _CharT, typename _Traits>
2289 std::basic_istream<_CharT, _Traits>&
2290 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2291 piecewise_constant_distribution<_RealType>& __x)
2293 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2294 typedef typename __istream_type::ios_base __ios_base;
2296 const typename __ios_base::fmtflags __flags = __is.flags();
2297 __is.flags(__ios_base::dec | __ios_base::skipws);
2302 std::vector<_RealType> __int_vec;
2303 for (size_t __i = 0; __i <= __n; ++__i)
2307 __int_vec.push_back(__int);
2310 std::vector<double> __den_vec;
2311 for (size_t __i = 0; __i < __n; ++__i)
2315 __den_vec.push_back(__den);
2318 __x.param(typename piecewise_constant_distribution<_RealType>::
2319 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2321 __is.flags(__flags);
2326 template<typename _RealType>
2328 piecewise_linear_distribution<_RealType>::param_type::
2331 if (_M_int.size() < 2)
2334 _M_int.push_back(_RealType(0));
2335 _M_int.push_back(_RealType(1));
2338 _M_den.push_back(1.0);
2339 _M_den.push_back(1.0);
2345 for (size_t __i = 0; __i < _M_int.size() - 1; ++__i)
2347 const _RealType __delta = _M_int[__i + 1] - _M_int[__i];
2348 __sum += 0.5 * (_M_den[__i + 1] + _M_den[__i]) * __delta;
2349 _M_cp.push_back(__sum);
2350 _M_m.push_back((_M_den[__i + 1] - _M_den[__i]) / __delta);
2353 // Now normalize the densities...
2354 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2355 std::bind2nd(std::divides<double>(),__sum));
2356 // ... and partial sums...
2357 std::transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2358 std::bind2nd(std::divides<double>(), __sum));
2360 std::transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2361 std::bind2nd(std::divides<double>(), __sum));
2362 // Make sure the last cumulative probablility is one.
2363 _M_cp[_M_cp.size() - 1] = 1.0;
2366 template<typename _RealType>
2367 piecewise_linear_distribution<_RealType>::param_type::
2369 : _M_int(), _M_den(), _M_cp(), _M_m()
2370 { _M_initialize(); }
2372 template<typename _RealType>
2373 template<typename _InputIteratorB, typename _InputIteratorW>
2374 piecewise_linear_distribution<_RealType>::param_type::
2375 param_type(_InputIteratorB __bbegin,
2376 _InputIteratorB __bend,
2377 _InputIteratorW __wbegin)
2378 : _M_int(), _M_den(), _M_cp(), _M_m()
2380 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2382 _M_int.push_back(*__bbegin);
2383 _M_den.push_back(*__wbegin);
2389 template<typename _RealType>
2390 template<typename _Func>
2391 piecewise_linear_distribution<_RealType>::param_type::
2392 param_type(initializer_list<_RealType> __bil, _Func __fw)
2393 : _M_int(), _M_den(), _M_cp(), _M_m()
2395 for (auto __biter = __bil.begin(); __biter != __bil.end(); ++__biter)
2397 _M_int.push_back(*__biter);
2398 _M_den.push_back(__fw(*__biter));
2404 template<typename _RealType>
2405 template<typename _Func>
2406 piecewise_linear_distribution<_RealType>::param_type::
2407 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
2409 : _M_int(), _M_den(), _M_cp(), _M_m()
2411 for (size_t __i = 0; __i <= __nw; ++__i)
2413 const _RealType __x = ((__nw - __i) * __xmin
2414 + __i * __xmax) / __nw;
2415 _M_int.push_back(__x);
2416 _M_den.push_back(__fw(__x));
2422 template<typename _RealType>
2423 template<typename _UniformRandomNumberGenerator>
2424 typename piecewise_linear_distribution<_RealType>::result_type
2425 piecewise_linear_distribution<_RealType>::
2426 operator()(_UniformRandomNumberGenerator& __urng,
2427 const param_type& __param)
2430 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2433 const double __p = __aurng();
2434 auto __pos = std::lower_bound(__param._M_cp.begin(),
2435 __param._M_cp.end(), __p);
2436 const size_t __i = __pos - __param._M_cp.begin();
2437 const double __a = 0.5 * __param._M_m[__i];
2438 const double __b = __param._M_den[__i];
2439 const double __c = __param._M_cp[__i];
2440 const double __q = -0.5 * (__b
2441 #if _GLIBCXX_USE_C99_MATH_TR1
2442 + std::copysign(std::sqrt(__b * __b
2443 - 4.0 * __a * __c), __b));
2445 + (__b < 0.0 ? -1.0 : 1.0)
2446 * std::sqrt(__b * __b - 4.0 * __a * __c));
2448 const double __x0 = __param._M_int[__i];
2449 const double __x1 = __q / __a;
2450 const double __x2 = __c / __q;
2451 __x = std::max(__x0 + __x1, __x0 + __x2);
2456 template<typename _RealType, typename _CharT, typename _Traits>
2457 std::basic_ostream<_CharT, _Traits>&
2458 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2459 const piecewise_linear_distribution<_RealType>& __x)
2461 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2462 typedef typename __ostream_type::ios_base __ios_base;
2464 const typename __ios_base::fmtflags __flags = __os.flags();
2465 const _CharT __fill = __os.fill();
2466 const std::streamsize __precision = __os.precision();
2467 const _CharT __space = __os.widen(' ');
2468 __os.flags(__ios_base::scientific | __ios_base::left);
2470 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2472 std::vector<_RealType> __int = __x.intervals();
2473 __os << __int.size() - 1;
2475 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2476 __os << __space << *__xit;
2478 std::vector<double> __den = __x.densities();
2479 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2480 __os << __space << *__dit;
2482 __os.flags(__flags);
2484 __os.precision(__precision);
2488 template<typename _RealType, typename _CharT, typename _Traits>
2489 std::basic_istream<_CharT, _Traits>&
2490 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2491 piecewise_linear_distribution<_RealType>& __x)
2493 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2494 typedef typename __istream_type::ios_base __ios_base;
2496 const typename __ios_base::fmtflags __flags = __is.flags();
2497 __is.flags(__ios_base::dec | __ios_base::skipws);
2502 std::vector<_RealType> __int_vec;
2503 for (size_t __i = 0; __i <= __n; ++__i)
2507 __int_vec.push_back(__int);
2510 std::vector<double> __den_vec;
2511 for (size_t __i = 0; __i <= __n; ++__i)
2515 __den_vec.push_back(__den);
2518 __x.param(typename piecewise_linear_distribution<_RealType>::
2519 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2521 __is.flags(__flags);
2526 template<typename _IntType>
2527 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2529 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2530 _M_v.push_back(__detail::__mod<result_type, 1, 0,
2531 __detail::_Shift<result_type, 32>::__value>(*__iter));
2534 template<typename _InputIterator>
2535 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2537 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2538 _M_v.push_back(__detail::__mod<result_type, 1, 0,
2539 __detail::_Shift<result_type, 32>::__value>(*__iter));
2542 template<typename _RandomAccessIterator>
2544 seed_seq::generate(_RandomAccessIterator __begin,
2545 _RandomAccessIterator __end)
2547 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2550 if (__begin == __end)
2553 std::fill(__begin, __end, _Type(0x8b8b8b8bU));
2555 const size_t __n = __end - __begin;
2556 const size_t __s = _M_v.size();
2557 const size_t __t = (__n >= 623) ? 11
2562 const size_t __p = (__n - __t) / 2;
2563 const size_t __q = __p + __t;
2564 const size_t __m = std::max(__s + 1, __n);
2566 for (size_t __k = 0; __k < __m; ++__k)
2568 _Type __arg = (__begin[__k % __n]
2569 ^ __begin[(__k + __p) % __n]
2570 ^ __begin[(__k - 1) % __n]);
2571 _Type __r1 = __arg ^ (__arg << 27);
2572 __r1 = __detail::__mod<_Type, 1664525U, 0U,
2573 __detail::_Shift<_Type, 32>::__value>(__r1);
2577 else if (__k <= __s)
2578 __r2 += __k % __n + _M_v[__k - 1];
2581 __r2 = __detail::__mod<_Type, 1U, 0U,
2582 __detail::_Shift<_Type, 32>::__value>(__r2);
2583 __begin[(__k + __p) % __n] += __r1;
2584 __begin[(__k + __q) % __n] += __r2;
2585 __begin[__k % __n] = __r2;
2588 for (size_t __k = __m; __k < __m + __n; ++__k)
2590 _Type __arg = (__begin[__k % __n]
2591 + __begin[(__k + __p) % __n]
2592 + __begin[(__k - 1) % __n]);
2593 _Type __r3 = __arg ^ (__arg << 27);
2594 __r3 = __detail::__mod<_Type, 1566083941U, 0U,
2595 __detail::_Shift<_Type, 32>::__value>(__r3);
2596 _Type __r4 = __r3 - __k % __n;
2597 __r4 = __detail::__mod<_Type, 1U, 0U,
2598 __detail::_Shift<_Type, 32>::__value>(__r4);
2599 __begin[(__k + __p) % __n] ^= __r4;
2600 __begin[(__k + __q) % __n] ^= __r3;
2601 __begin[__k % __n] = __r4;
2605 template<typename _RealType, size_t __bits,
2606 typename _UniformRandomNumberGenerator>
2608 generate_canonical(_UniformRandomNumberGenerator& __urng)
2611 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2613 const long double __r = static_cast<long double>(__urng.max())
2614 - static_cast<long double>(__urng.min()) + 1.0L;
2615 const size_t __log2r = std::log(__r) / std::log(2.0L);
2616 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2617 _RealType __sum = _RealType(0);
2618 _RealType __tmp = _RealType(1);
2619 for (; __k != 0; --__k)
2621 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2624 return __sum / __tmp;