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 operator()(_UniformRandomNumberGenerator& __urng,
648 const param_type& __param)
650 // XXX Must be fixed to work well for *arbitrary* __urng.max(),
651 // __urng.min(), __param.b(), __param.a(). Currently works fine only
652 // in the most common case __urng.max() - __urng.min() >=
653 // __param.b() - __param.a(), 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 = __param.b() - __param.a();
668 const __uctype __udenom = (__urnrange <= __urange
669 ? 1 : __urnrange / (__urange + 1));
671 __ret = (__urntype(__urng()) - __urnmin) / __udenom;
672 while (__ret > __param.b() - __param.a());
674 return __ret + __param.a();
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, double>
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));
858 template<typename _IntType>
859 template<typename _UniformRandomNumberGenerator>
860 typename negative_binomial_distribution<_IntType>::result_type
861 negative_binomial_distribution<_IntType>::
862 operator()(_UniformRandomNumberGenerator& __urng)
864 const double __y = _M_gd(__urng);
866 // XXX Is the constructor too slow?
867 std::poisson_distribution<result_type> __poisson(__y);
868 return __poisson(__urng);
871 template<typename _IntType>
872 template<typename _UniformRandomNumberGenerator>
873 typename negative_binomial_distribution<_IntType>::result_type
874 negative_binomial_distribution<_IntType>::
875 operator()(_UniformRandomNumberGenerator& __urng,
876 const param_type& __p)
878 typedef typename std::gamma_distribution<result_type>::param_type
882 _M_gd(__urng, param_type(__p.k(), __p.p() / (1.0 - __p.p())));
884 std::poisson_distribution<result_type> __poisson(__y);
885 return __poisson(__urng);
888 template<typename _IntType, typename _CharT, typename _Traits>
889 std::basic_ostream<_CharT, _Traits>&
890 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
891 const negative_binomial_distribution<_IntType>& __x)
893 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
894 typedef typename __ostream_type::ios_base __ios_base;
896 const typename __ios_base::fmtflags __flags = __os.flags();
897 const _CharT __fill = __os.fill();
898 const std::streamsize __precision = __os.precision();
899 const _CharT __space = __os.widen(' ');
900 __os.flags(__ios_base::scientific | __ios_base::left);
901 __os.fill(__os.widen(' '));
902 __os.precision(std::numeric_limits<double>::digits10 + 1);
904 __os << __x.k() << __space << __x.p()
905 << __space << __x._M_gd;
909 __os.precision(__precision);
913 template<typename _IntType, typename _CharT, typename _Traits>
914 std::basic_istream<_CharT, _Traits>&
915 operator>>(std::basic_istream<_CharT, _Traits>& __is,
916 negative_binomial_distribution<_IntType>& __x)
918 typedef std::basic_istream<_CharT, _Traits> __istream_type;
919 typedef typename __istream_type::ios_base __ios_base;
921 const typename __ios_base::fmtflags __flags = __is.flags();
922 __is.flags(__ios_base::skipws);
926 __is >> __k >> __p >> __x._M_gd;
927 __x.param(typename negative_binomial_distribution<_IntType>::
928 param_type(__k, __p));
935 template<typename _IntType>
937 poisson_distribution<_IntType>::param_type::
940 #if _GLIBCXX_USE_C99_MATH_TR1
943 const double __m = std::floor(_M_mean);
944 _M_lm_thr = std::log(_M_mean);
945 _M_lfm = std::lgamma(__m + 1);
946 _M_sm = std::sqrt(__m);
948 const double __pi_4 = 0.7853981633974483096156608458198757L;
949 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
951 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
952 const double __cx = 2 * __m + _M_d;
953 _M_scx = std::sqrt(__cx / 2);
956 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
957 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
962 _M_lm_thr = std::exp(-_M_mean);
966 * A rejection algorithm when mean >= 12 and a simple method based
967 * upon the multiplication of uniform random variates otherwise.
968 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
972 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
973 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
975 template<typename _IntType>
976 template<typename _UniformRandomNumberGenerator>
977 typename poisson_distribution<_IntType>::result_type
978 poisson_distribution<_IntType>::
979 operator()(_UniformRandomNumberGenerator& __urng,
980 const param_type& __param)
982 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
984 #if _GLIBCXX_USE_C99_MATH_TR1
985 if (__param.mean() >= 12)
989 // See comments above...
991 (1 - std::numeric_limits<double>::epsilon()) / 2;
993 std::numeric_limits<_IntType>::max() + __naf;
995 const double __m = std::floor(__param.mean());
997 const double __spi_2 = 1.2533141373155002512078826424055226L;
998 const double __c1 = __param._M_sm * __spi_2;
999 const double __c2 = __param._M_c2b + __c1;
1000 const double __c3 = __c2 + 1;
1001 const double __c4 = __c3 + 1;
1003 const double __e178 = 1.0129030479320018583185514777512983L;
1004 const double __c5 = __c4 + __e178;
1005 const double __c = __param._M_cb + __c5;
1006 const double __2cx = 2 * (2 * __m + __param._M_d);
1008 bool __reject = true;
1011 const double __u = __c * __aurng();
1012 const double __e = -std::log(__aurng());
1018 const double __n = _M_nd(__urng);
1019 const double __y = -std::abs(__n) * __param._M_sm - 1;
1020 __x = std::floor(__y);
1021 __w = -__n * __n / 2;
1025 else if (__u <= __c2)
1027 const double __n = _M_nd(__urng);
1028 const double __y = 1 + std::abs(__n) * __param._M_scx;
1029 __x = std::ceil(__y);
1030 __w = __y * (2 - __y) * __param._M_1cx;
1031 if (__x > __param._M_d)
1034 else if (__u <= __c3)
1035 // NB: This case not in the book, nor in the Errata,
1036 // but should be ok...
1038 else if (__u <= __c4)
1040 else if (__u <= __c5)
1044 const double __v = -std::log(__aurng());
1045 const double __y = __param._M_d
1046 + __v * __2cx / __param._M_d;
1047 __x = std::ceil(__y);
1048 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1051 __reject = (__w - __e - __x * __param._M_lm_thr
1052 > __param._M_lfm - std::lgamma(__x + __m + 1));
1054 __reject |= __x + __m >= __thr;
1058 return result_type(__x + __m + __naf);
1064 double __prod = 1.0;
1068 __prod *= __aurng();
1071 while (__prod > __param._M_lm_thr);
1077 template<typename _IntType,
1078 typename _CharT, typename _Traits>
1079 std::basic_ostream<_CharT, _Traits>&
1080 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1081 const poisson_distribution<_IntType>& __x)
1083 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1084 typedef typename __ostream_type::ios_base __ios_base;
1086 const typename __ios_base::fmtflags __flags = __os.flags();
1087 const _CharT __fill = __os.fill();
1088 const std::streamsize __precision = __os.precision();
1089 const _CharT __space = __os.widen(' ');
1090 __os.flags(__ios_base::scientific | __ios_base::left);
1092 __os.precision(std::numeric_limits<double>::digits10 + 1);
1094 __os << __x.mean() << __space << __x._M_nd;
1096 __os.flags(__flags);
1098 __os.precision(__precision);
1102 template<typename _IntType,
1103 typename _CharT, typename _Traits>
1104 std::basic_istream<_CharT, _Traits>&
1105 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1106 poisson_distribution<_IntType>& __x)
1108 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1109 typedef typename __istream_type::ios_base __ios_base;
1111 const typename __ios_base::fmtflags __flags = __is.flags();
1112 __is.flags(__ios_base::skipws);
1115 __is >> __mean >> __x._M_nd;
1116 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1118 __is.flags(__flags);
1123 template<typename _IntType>
1125 binomial_distribution<_IntType>::param_type::
1128 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1132 #if _GLIBCXX_USE_C99_MATH_TR1
1133 if (_M_t * __p12 >= 8)
1136 const double __np = std::floor(_M_t * __p12);
1137 const double __pa = __np / _M_t;
1138 const double __1p = 1 - __pa;
1140 const double __pi_4 = 0.7853981633974483096156608458198757L;
1141 const double __d1x =
1142 std::sqrt(__np * __1p * std::log(32 * __np
1143 / (81 * __pi_4 * __1p)));
1144 _M_d1 = std::round(std::max(1.0, __d1x));
1145 const double __d2x =
1146 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1147 / (__pi_4 * __pa)));
1148 _M_d2 = std::round(std::max(1.0, __d2x));
1151 const double __spi_2 = 1.2533141373155002512078826424055226L;
1152 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1153 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1154 _M_c = 2 * _M_d1 / __np;
1155 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1156 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1157 const double __s1s = _M_s1 * _M_s1;
1158 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1160 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1161 const double __s2s = _M_s2 * _M_s2;
1162 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1163 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1164 _M_lf = (std::lgamma(__np + 1)
1165 + std::lgamma(_M_t - __np + 1));
1166 _M_lp1p = std::log(__pa / __1p);
1168 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1172 _M_q = -std::log(1 - __p12);
1175 template<typename _IntType>
1176 template<typename _UniformRandomNumberGenerator>
1177 typename binomial_distribution<_IntType>::result_type
1178 binomial_distribution<_IntType>::
1179 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1183 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1188 const double __e = -std::log(__aurng());
1189 __sum += __e / (__t - __x);
1192 while (__sum <= _M_param._M_q);
1198 * A rejection algorithm when t * p >= 8 and a simple waiting time
1199 * method - the second in the referenced book - otherwise.
1200 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1204 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1205 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1207 template<typename _IntType>
1208 template<typename _UniformRandomNumberGenerator>
1209 typename binomial_distribution<_IntType>::result_type
1210 binomial_distribution<_IntType>::
1211 operator()(_UniformRandomNumberGenerator& __urng,
1212 const param_type& __param)
1215 const _IntType __t = __param.t();
1216 const _IntType __p = __param.p();
1217 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1218 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1221 #if _GLIBCXX_USE_C99_MATH_TR1
1222 if (!__param._M_easy)
1226 // See comments above...
1227 const double __naf =
1228 (1 - std::numeric_limits<double>::epsilon()) / 2;
1229 const double __thr =
1230 std::numeric_limits<_IntType>::max() + __naf;
1232 const double __np = std::floor(__t * __p12);
1235 const double __spi_2 = 1.2533141373155002512078826424055226L;
1236 const double __a1 = __param._M_a1;
1237 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1238 const double __a123 = __param._M_a123;
1239 const double __s1s = __param._M_s1 * __param._M_s1;
1240 const double __s2s = __param._M_s2 * __param._M_s2;
1245 const double __u = __param._M_s * __aurng();
1251 const double __n = _M_nd(__urng);
1252 const double __y = __param._M_s1 * std::abs(__n);
1253 __reject = __y >= __param._M_d1;
1256 const double __e = -std::log(__aurng());
1257 __x = std::floor(__y);
1258 __v = -__e - __n * __n / 2 + __param._M_c;
1261 else if (__u <= __a12)
1263 const double __n = _M_nd(__urng);
1264 const double __y = __param._M_s2 * std::abs(__n);
1265 __reject = __y >= __param._M_d2;
1268 const double __e = -std::log(__aurng());
1269 __x = std::floor(-__y);
1270 __v = -__e - __n * __n / 2;
1273 else if (__u <= __a123)
1275 const double __e1 = -std::log(__aurng());
1276 const double __e2 = -std::log(__aurng());
1278 const double __y = __param._M_d1
1279 + 2 * __s1s * __e1 / __param._M_d1;
1280 __x = std::floor(__y);
1281 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1282 -__y / (2 * __s1s)));
1287 const double __e1 = -std::log(__aurng());
1288 const double __e2 = -std::log(__aurng());
1290 const double __y = __param._M_d2
1291 + 2 * __s2s * __e1 / __param._M_d2;
1292 __x = std::floor(-__y);
1293 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1297 __reject = __reject || __x < -__np || __x > __t - __np;
1300 const double __lfx =
1301 std::lgamma(__np + __x + 1)
1302 + std::lgamma(__t - (__np + __x) + 1);
1303 __reject = __v > __param._M_lf - __lfx
1304 + __x * __param._M_lp1p;
1307 __reject |= __x + __np >= __thr;
1311 __x += __np + __naf;
1313 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1314 __ret = _IntType(__x) + __z;
1318 __ret = _M_waiting(__urng, __t);
1321 __ret = __t - __ret;
1325 template<typename _IntType,
1326 typename _CharT, typename _Traits>
1327 std::basic_ostream<_CharT, _Traits>&
1328 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1329 const binomial_distribution<_IntType>& __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 const _CharT __space = __os.widen(' ');
1338 __os.flags(__ios_base::scientific | __ios_base::left);
1340 __os.precision(std::numeric_limits<double>::digits10 + 1);
1342 __os << __x.t() << __space << __x.p()
1343 << __space << __x._M_nd;
1345 __os.flags(__flags);
1347 __os.precision(__precision);
1351 template<typename _IntType,
1352 typename _CharT, typename _Traits>
1353 std::basic_istream<_CharT, _Traits>&
1354 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1355 binomial_distribution<_IntType>& __x)
1357 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1358 typedef typename __istream_type::ios_base __ios_base;
1360 const typename __ios_base::fmtflags __flags = __is.flags();
1361 __is.flags(__ios_base::dec | __ios_base::skipws);
1365 __is >> __t >> __p >> __x._M_nd;
1366 __x.param(typename binomial_distribution<_IntType>::
1367 param_type(__t, __p));
1369 __is.flags(__flags);
1374 template<typename _RealType, typename _CharT, typename _Traits>
1375 std::basic_ostream<_CharT, _Traits>&
1376 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1377 const exponential_distribution<_RealType>& __x)
1379 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1380 typedef typename __ostream_type::ios_base __ios_base;
1382 const typename __ios_base::fmtflags __flags = __os.flags();
1383 const _CharT __fill = __os.fill();
1384 const std::streamsize __precision = __os.precision();
1385 __os.flags(__ios_base::scientific | __ios_base::left);
1386 __os.fill(__os.widen(' '));
1387 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1389 __os << __x.lambda();
1391 __os.flags(__flags);
1393 __os.precision(__precision);
1397 template<typename _RealType, typename _CharT, typename _Traits>
1398 std::basic_istream<_CharT, _Traits>&
1399 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1400 exponential_distribution<_RealType>& __x)
1402 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1403 typedef typename __istream_type::ios_base __ios_base;
1405 const typename __ios_base::fmtflags __flags = __is.flags();
1406 __is.flags(__ios_base::dec | __ios_base::skipws);
1410 __x.param(typename exponential_distribution<_RealType>::
1411 param_type(__lambda));
1413 __is.flags(__flags);
1419 * Polar method due to Marsaglia.
1421 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1422 * New York, 1986, Ch. V, Sect. 4.4.
1424 template<typename _RealType>
1425 template<typename _UniformRandomNumberGenerator>
1426 typename normal_distribution<_RealType>::result_type
1427 normal_distribution<_RealType>::
1428 operator()(_UniformRandomNumberGenerator& __urng,
1429 const param_type& __param)
1432 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1435 if (_M_saved_available)
1437 _M_saved_available = false;
1442 result_type __x, __y, __r2;
1445 __x = result_type(2.0) * __aurng() - 1.0;
1446 __y = result_type(2.0) * __aurng() - 1.0;
1447 __r2 = __x * __x + __y * __y;
1449 while (__r2 > 1.0 || __r2 == 0.0);
1451 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1452 _M_saved = __x * __mult;
1453 _M_saved_available = true;
1454 __ret = __y * __mult;
1457 __ret = __ret * __param.stddev() + __param.mean();
1461 template<typename _RealType, typename _CharT, typename _Traits>
1462 std::basic_ostream<_CharT, _Traits>&
1463 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1464 const normal_distribution<_RealType>& __x)
1466 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1467 typedef typename __ostream_type::ios_base __ios_base;
1469 const typename __ios_base::fmtflags __flags = __os.flags();
1470 const _CharT __fill = __os.fill();
1471 const std::streamsize __precision = __os.precision();
1472 const _CharT __space = __os.widen(' ');
1473 __os.flags(__ios_base::scientific | __ios_base::left);
1475 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1477 __os << __x.mean() << __space << __x.stddev()
1478 << __space << __x._M_saved_available;
1479 if (__x._M_saved_available)
1480 __os << __space << __x._M_saved;
1482 __os.flags(__flags);
1484 __os.precision(__precision);
1488 template<typename _RealType, typename _CharT, typename _Traits>
1489 std::basic_istream<_CharT, _Traits>&
1490 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1491 normal_distribution<_RealType>& __x)
1493 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1494 typedef typename __istream_type::ios_base __ios_base;
1496 const typename __ios_base::fmtflags __flags = __is.flags();
1497 __is.flags(__ios_base::dec | __ios_base::skipws);
1499 double __mean, __stddev;
1500 __is >> __mean >> __stddev
1501 >> __x._M_saved_available;
1502 if (__x._M_saved_available)
1503 __is >> __x._M_saved;
1504 __x.param(typename normal_distribution<_RealType>::
1505 param_type(__mean, __stddev));
1507 __is.flags(__flags);
1512 template<typename _RealType, typename _CharT, typename _Traits>
1513 std::basic_ostream<_CharT, _Traits>&
1514 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1515 const lognormal_distribution<_RealType>& __x)
1517 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1518 typedef typename __ostream_type::ios_base __ios_base;
1520 const typename __ios_base::fmtflags __flags = __os.flags();
1521 const _CharT __fill = __os.fill();
1522 const std::streamsize __precision = __os.precision();
1523 const _CharT __space = __os.widen(' ');
1524 __os.flags(__ios_base::scientific | __ios_base::left);
1526 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1528 __os << __x.m() << __space << __x.s()
1529 << __space << __x._M_nd;
1531 __os.flags(__flags);
1533 __os.precision(__precision);
1537 template<typename _RealType, typename _CharT, typename _Traits>
1538 std::basic_istream<_CharT, _Traits>&
1539 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1540 lognormal_distribution<_RealType>& __x)
1542 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1543 typedef typename __istream_type::ios_base __ios_base;
1545 const typename __ios_base::fmtflags __flags = __is.flags();
1546 __is.flags(__ios_base::dec | __ios_base::skipws);
1549 __is >> __m >> __s >> __x._M_nd;
1550 __x.param(typename lognormal_distribution<_RealType>::
1551 param_type(__m, __s));
1553 __is.flags(__flags);
1558 template<typename _RealType, typename _CharT, typename _Traits>
1559 std::basic_ostream<_CharT, _Traits>&
1560 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1561 const chi_squared_distribution<_RealType>& __x)
1563 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1564 typedef typename __ostream_type::ios_base __ios_base;
1566 const typename __ios_base::fmtflags __flags = __os.flags();
1567 const _CharT __fill = __os.fill();
1568 const std::streamsize __precision = __os.precision();
1569 const _CharT __space = __os.widen(' ');
1570 __os.flags(__ios_base::scientific | __ios_base::left);
1572 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1574 __os << __x.n() << __space << __x._M_gd;
1576 __os.flags(__flags);
1578 __os.precision(__precision);
1582 template<typename _RealType, typename _CharT, typename _Traits>
1583 std::basic_istream<_CharT, _Traits>&
1584 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1585 chi_squared_distribution<_RealType>& __x)
1587 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1588 typedef typename __istream_type::ios_base __ios_base;
1590 const typename __ios_base::fmtflags __flags = __is.flags();
1591 __is.flags(__ios_base::dec | __ios_base::skipws);
1594 __is >> __n >> __x._M_gd;
1595 __x.param(typename chi_squared_distribution<_RealType>::
1598 __is.flags(__flags);
1603 template<typename _RealType>
1604 template<typename _UniformRandomNumberGenerator>
1605 typename cauchy_distribution<_RealType>::result_type
1606 cauchy_distribution<_RealType>::
1607 operator()(_UniformRandomNumberGenerator& __urng,
1608 const param_type& __p)
1610 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1617 const _RealType __pi = 3.1415926535897932384626433832795029L;
1618 return __p.a() + __p.b() * std::tan(__pi * __u);
1621 template<typename _RealType, typename _CharT, typename _Traits>
1622 std::basic_ostream<_CharT, _Traits>&
1623 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1624 const cauchy_distribution<_RealType>& __x)
1626 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1627 typedef typename __ostream_type::ios_base __ios_base;
1629 const typename __ios_base::fmtflags __flags = __os.flags();
1630 const _CharT __fill = __os.fill();
1631 const std::streamsize __precision = __os.precision();
1632 const _CharT __space = __os.widen(' ');
1633 __os.flags(__ios_base::scientific | __ios_base::left);
1635 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1637 __os << __x.a() << __space << __x.b();
1639 __os.flags(__flags);
1641 __os.precision(__precision);
1645 template<typename _RealType, typename _CharT, typename _Traits>
1646 std::basic_istream<_CharT, _Traits>&
1647 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1648 cauchy_distribution<_RealType>& __x)
1650 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1651 typedef typename __istream_type::ios_base __ios_base;
1653 const typename __ios_base::fmtflags __flags = __is.flags();
1654 __is.flags(__ios_base::dec | __ios_base::skipws);
1658 __x.param(typename cauchy_distribution<_RealType>::
1659 param_type(__a, __b));
1661 __is.flags(__flags);
1666 template<typename _RealType, typename _CharT, typename _Traits>
1667 std::basic_ostream<_CharT, _Traits>&
1668 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1669 const fisher_f_distribution<_RealType>& __x)
1671 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1672 typedef typename __ostream_type::ios_base __ios_base;
1674 const typename __ios_base::fmtflags __flags = __os.flags();
1675 const _CharT __fill = __os.fill();
1676 const std::streamsize __precision = __os.precision();
1677 const _CharT __space = __os.widen(' ');
1678 __os.flags(__ios_base::scientific | __ios_base::left);
1680 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1682 __os << __x.m() << __space << __x.n()
1683 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1685 __os.flags(__flags);
1687 __os.precision(__precision);
1691 template<typename _RealType, typename _CharT, typename _Traits>
1692 std::basic_istream<_CharT, _Traits>&
1693 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1694 fisher_f_distribution<_RealType>& __x)
1696 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1697 typedef typename __istream_type::ios_base __ios_base;
1699 const typename __ios_base::fmtflags __flags = __is.flags();
1700 __is.flags(__ios_base::dec | __ios_base::skipws);
1703 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1704 __x.param(typename fisher_f_distribution<_RealType>::
1705 param_type(__m, __n));
1707 __is.flags(__flags);
1712 template<typename _RealType, typename _CharT, typename _Traits>
1713 std::basic_ostream<_CharT, _Traits>&
1714 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1715 const student_t_distribution<_RealType>& __x)
1717 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1718 typedef typename __ostream_type::ios_base __ios_base;
1720 const typename __ios_base::fmtflags __flags = __os.flags();
1721 const _CharT __fill = __os.fill();
1722 const std::streamsize __precision = __os.precision();
1723 const _CharT __space = __os.widen(' ');
1724 __os.flags(__ios_base::scientific | __ios_base::left);
1726 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1728 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1730 __os.flags(__flags);
1732 __os.precision(__precision);
1736 template<typename _RealType, typename _CharT, typename _Traits>
1737 std::basic_istream<_CharT, _Traits>&
1738 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1739 student_t_distribution<_RealType>& __x)
1741 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1742 typedef typename __istream_type::ios_base __ios_base;
1744 const typename __ios_base::fmtflags __flags = __is.flags();
1745 __is.flags(__ios_base::dec | __ios_base::skipws);
1748 __is >> __n >> __x._M_nd >> __x._M_gd;
1749 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1751 __is.flags(__flags);
1756 template<typename _RealType>
1758 gamma_distribution<_RealType>::param_type::
1761 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
1763 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
1764 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
1768 * Marsaglia, G. and Tsang, W. W.
1769 * "A Simple Method for Generating Gamma Variables"
1770 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
1772 template<typename _RealType>
1773 template<typename _UniformRandomNumberGenerator>
1774 typename gamma_distribution<_RealType>::result_type
1775 gamma_distribution<_RealType>::
1776 operator()(_UniformRandomNumberGenerator& __urng,
1777 const param_type& __param)
1779 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1782 result_type __u, __v, __n;
1783 const result_type __a1 = (__param._M_malpha
1784 - _RealType(1.0) / _RealType(3.0));
1790 __n = _M_nd(__urng);
1791 __v = result_type(1.0) + __param._M_a2 * __n;
1795 __v = __v * __v * __v;
1798 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
1799 && (std::log(__u) > (0.5 * __n * __n + __a1
1800 * (1.0 - __v + std::log(__v)))));
1802 if (__param.alpha() == __param._M_malpha)
1803 return __a1 * __v * __param.beta();
1810 return (std::pow(__u, result_type(1.0) / __param.alpha())
1811 * __a1 * __v * __param.beta());
1815 template<typename _RealType, typename _CharT, typename _Traits>
1816 std::basic_ostream<_CharT, _Traits>&
1817 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1818 const gamma_distribution<_RealType>& __x)
1820 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1821 typedef typename __ostream_type::ios_base __ios_base;
1823 const typename __ios_base::fmtflags __flags = __os.flags();
1824 const _CharT __fill = __os.fill();
1825 const std::streamsize __precision = __os.precision();
1826 const _CharT __space = __os.widen(' ');
1827 __os.flags(__ios_base::scientific | __ios_base::left);
1829 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1831 __os << __x.alpha() << __space << __x.beta()
1832 << __space << __x._M_nd;
1834 __os.flags(__flags);
1836 __os.precision(__precision);
1840 template<typename _RealType, typename _CharT, typename _Traits>
1841 std::basic_istream<_CharT, _Traits>&
1842 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1843 gamma_distribution<_RealType>& __x)
1845 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1846 typedef typename __istream_type::ios_base __ios_base;
1848 const typename __ios_base::fmtflags __flags = __is.flags();
1849 __is.flags(__ios_base::dec | __ios_base::skipws);
1851 _RealType __alpha_val, __beta_val;
1852 __is >> __alpha_val >> __beta_val >> __x._M_nd;
1853 __x.param(typename gamma_distribution<_RealType>::
1854 param_type(__alpha_val, __beta_val));
1856 __is.flags(__flags);
1861 template<typename _RealType>
1862 template<typename _UniformRandomNumberGenerator>
1863 typename weibull_distribution<_RealType>::result_type
1864 weibull_distribution<_RealType>::
1865 operator()(_UniformRandomNumberGenerator& __urng,
1866 const param_type& __p)
1868 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1870 return __p.b() * std::pow(-std::log(__aurng()),
1871 result_type(1) / __p.a());
1874 template<typename _RealType, typename _CharT, typename _Traits>
1875 std::basic_ostream<_CharT, _Traits>&
1876 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1877 const weibull_distribution<_RealType>& __x)
1879 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1880 typedef typename __ostream_type::ios_base __ios_base;
1882 const typename __ios_base::fmtflags __flags = __os.flags();
1883 const _CharT __fill = __os.fill();
1884 const std::streamsize __precision = __os.precision();
1885 const _CharT __space = __os.widen(' ');
1886 __os.flags(__ios_base::scientific | __ios_base::left);
1888 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1890 __os << __x.a() << __space << __x.b();
1892 __os.flags(__flags);
1894 __os.precision(__precision);
1898 template<typename _RealType, typename _CharT, typename _Traits>
1899 std::basic_istream<_CharT, _Traits>&
1900 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1901 weibull_distribution<_RealType>& __x)
1903 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1904 typedef typename __istream_type::ios_base __ios_base;
1906 const typename __ios_base::fmtflags __flags = __is.flags();
1907 __is.flags(__ios_base::dec | __ios_base::skipws);
1911 __x.param(typename weibull_distribution<_RealType>::
1912 param_type(__a, __b));
1914 __is.flags(__flags);
1919 template<typename _RealType>
1920 template<typename _UniformRandomNumberGenerator>
1921 typename extreme_value_distribution<_RealType>::result_type
1922 extreme_value_distribution<_RealType>::
1923 operator()(_UniformRandomNumberGenerator& __urng,
1924 const param_type& __p)
1926 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1928 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
1931 template<typename _RealType, typename _CharT, typename _Traits>
1932 std::basic_ostream<_CharT, _Traits>&
1933 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1934 const extreme_value_distribution<_RealType>& __x)
1936 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1937 typedef typename __ostream_type::ios_base __ios_base;
1939 const typename __ios_base::fmtflags __flags = __os.flags();
1940 const _CharT __fill = __os.fill();
1941 const std::streamsize __precision = __os.precision();
1942 const _CharT __space = __os.widen(' ');
1943 __os.flags(__ios_base::scientific | __ios_base::left);
1945 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1947 __os << __x.a() << __space << __x.b();
1949 __os.flags(__flags);
1951 __os.precision(__precision);
1955 template<typename _RealType, typename _CharT, typename _Traits>
1956 std::basic_istream<_CharT, _Traits>&
1957 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1958 extreme_value_distribution<_RealType>& __x)
1960 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1961 typedef typename __istream_type::ios_base __ios_base;
1963 const typename __ios_base::fmtflags __flags = __is.flags();
1964 __is.flags(__ios_base::dec | __ios_base::skipws);
1968 __x.param(typename extreme_value_distribution<_RealType>::
1969 param_type(__a, __b));
1971 __is.flags(__flags);
1976 template<typename _IntType>
1978 discrete_distribution<_IntType>::param_type::
1981 if (_M_prob.size() < 2)
1984 _M_prob.push_back(1.0);
1988 const double __sum = std::accumulate(_M_prob.begin(),
1989 _M_prob.end(), 0.0);
1990 // Now normalize the probabilites.
1991 std::transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
1992 std::bind2nd(std::divides<double>(), __sum));
1993 // Accumulate partial sums.
1994 _M_cp.reserve(_M_prob.size());
1995 std::partial_sum(_M_prob.begin(), _M_prob.end(),
1996 std::back_inserter(_M_cp));
1997 // Make sure the last cumulative probability is one.
1998 _M_cp[_M_cp.size() - 1] = 1.0;
2001 template<typename _IntType>
2002 template<typename _Func>
2003 discrete_distribution<_IntType>::param_type::
2004 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2005 : _M_prob(), _M_cp()
2007 const size_t __n = __nw == 0 ? 1 : __nw;
2008 const double __delta = (__xmax - __xmin) / __n;
2010 _M_prob.reserve(__n);
2011 for (size_t __k = 0; __k < __nw; ++__k)
2012 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2017 template<typename _IntType>
2018 template<typename _UniformRandomNumberGenerator>
2019 typename discrete_distribution<_IntType>::result_type
2020 discrete_distribution<_IntType>::
2021 operator()(_UniformRandomNumberGenerator& __urng,
2022 const param_type& __param)
2024 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2027 const double __p = __aurng();
2028 auto __pos = std::lower_bound(__param._M_cp.begin(),
2029 __param._M_cp.end(), __p);
2031 return __pos - __param._M_cp.begin();
2034 template<typename _IntType, typename _CharT, typename _Traits>
2035 std::basic_ostream<_CharT, _Traits>&
2036 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2037 const discrete_distribution<_IntType>& __x)
2039 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2040 typedef typename __ostream_type::ios_base __ios_base;
2042 const typename __ios_base::fmtflags __flags = __os.flags();
2043 const _CharT __fill = __os.fill();
2044 const std::streamsize __precision = __os.precision();
2045 const _CharT __space = __os.widen(' ');
2046 __os.flags(__ios_base::scientific | __ios_base::left);
2048 __os.precision(std::numeric_limits<double>::digits10 + 1);
2050 std::vector<double> __prob = __x.probabilities();
2051 __os << __prob.size();
2052 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2053 __os << __space << *__dit;
2055 __os.flags(__flags);
2057 __os.precision(__precision);
2061 template<typename _IntType, typename _CharT, typename _Traits>
2062 std::basic_istream<_CharT, _Traits>&
2063 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2064 discrete_distribution<_IntType>& __x)
2066 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2067 typedef typename __istream_type::ios_base __ios_base;
2069 const typename __ios_base::fmtflags __flags = __is.flags();
2070 __is.flags(__ios_base::dec | __ios_base::skipws);
2075 std::vector<double> __prob_vec;
2076 __prob_vec.reserve(__n);
2077 for (; __n != 0; --__n)
2081 __prob_vec.push_back(__prob);
2084 __x.param(typename discrete_distribution<_IntType>::
2085 param_type(__prob_vec.begin(), __prob_vec.end()));
2087 __is.flags(__flags);
2092 template<typename _RealType>
2094 piecewise_constant_distribution<_RealType>::param_type::
2097 if (_M_int.size() < 2)
2101 _M_int.push_back(_RealType(0));
2102 _M_int.push_back(_RealType(1));
2105 _M_den.push_back(1.0);
2110 const double __sum = std::accumulate(_M_den.begin(),
2113 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2114 std::bind2nd(std::divides<double>(), __sum));
2116 _M_cp.reserve(_M_den.size());
2117 std::partial_sum(_M_den.begin(), _M_den.end(),
2118 std::back_inserter(_M_cp));
2120 // Make sure the last cumulative probability is one.
2121 _M_cp[_M_cp.size() - 1] = 1.0;
2123 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2124 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2127 template<typename _RealType>
2128 template<typename _InputIteratorB, typename _InputIteratorW>
2129 piecewise_constant_distribution<_RealType>::param_type::
2130 param_type(_InputIteratorB __bbegin,
2131 _InputIteratorB __bend,
2132 _InputIteratorW __wbegin)
2133 : _M_int(), _M_den(), _M_cp()
2135 if (__bbegin != __bend)
2139 _M_int.push_back(*__bbegin);
2141 if (__bbegin == __bend)
2144 _M_den.push_back(*__wbegin);
2152 template<typename _RealType>
2153 template<typename _Func>
2154 piecewise_constant_distribution<_RealType>::param_type::
2155 param_type(initializer_list<_RealType> __bl, _Func __fw)
2156 : _M_int(), _M_den(), _M_cp()
2158 _M_int.reserve(__bl.size());
2159 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2160 _M_int.push_back(*__biter);
2162 _M_den.reserve(_M_int.size() - 1);
2163 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2164 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2169 template<typename _RealType>
2170 template<typename _Func>
2171 piecewise_constant_distribution<_RealType>::param_type::
2172 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2173 : _M_int(), _M_den(), _M_cp()
2175 const size_t __n = __nw == 0 ? 1 : __nw;
2176 const _RealType __delta = (__xmax - __xmin) / __n;
2178 _M_int.reserve(__n + 1);
2179 for (size_t __k = 0; __k <= __nw; ++__k)
2180 _M_int.push_back(__xmin + __k * __delta);
2182 _M_den.reserve(__n);
2183 for (size_t __k = 0; __k < __nw; ++__k)
2184 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2189 template<typename _RealType>
2190 template<typename _UniformRandomNumberGenerator>
2191 typename piecewise_constant_distribution<_RealType>::result_type
2192 piecewise_constant_distribution<_RealType>::
2193 operator()(_UniformRandomNumberGenerator& __urng,
2194 const param_type& __param)
2196 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2199 const double __p = __aurng();
2200 auto __pos = std::lower_bound(__param._M_cp.begin(),
2201 __param._M_cp.end(), __p);
2202 const size_t __i = __pos - __param._M_cp.begin();
2204 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2206 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2209 template<typename _RealType, typename _CharT, typename _Traits>
2210 std::basic_ostream<_CharT, _Traits>&
2211 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2212 const piecewise_constant_distribution<_RealType>& __x)
2214 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2215 typedef typename __ostream_type::ios_base __ios_base;
2217 const typename __ios_base::fmtflags __flags = __os.flags();
2218 const _CharT __fill = __os.fill();
2219 const std::streamsize __precision = __os.precision();
2220 const _CharT __space = __os.widen(' ');
2221 __os.flags(__ios_base::scientific | __ios_base::left);
2223 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2225 std::vector<_RealType> __int = __x.intervals();
2226 __os << __int.size() - 1;
2228 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2229 __os << __space << *__xit;
2231 std::vector<double> __den = __x.densities();
2232 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2233 __os << __space << *__dit;
2235 __os.flags(__flags);
2237 __os.precision(__precision);
2241 template<typename _RealType, typename _CharT, typename _Traits>
2242 std::basic_istream<_CharT, _Traits>&
2243 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2244 piecewise_constant_distribution<_RealType>& __x)
2246 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2247 typedef typename __istream_type::ios_base __ios_base;
2249 const typename __ios_base::fmtflags __flags = __is.flags();
2250 __is.flags(__ios_base::dec | __ios_base::skipws);
2255 std::vector<_RealType> __int_vec;
2256 __int_vec.reserve(__n + 1);
2257 for (size_t __i = 0; __i <= __n; ++__i)
2261 __int_vec.push_back(__int);
2264 std::vector<double> __den_vec;
2265 __den_vec.reserve(__n);
2266 for (size_t __i = 0; __i < __n; ++__i)
2270 __den_vec.push_back(__den);
2273 __x.param(typename piecewise_constant_distribution<_RealType>::
2274 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2276 __is.flags(__flags);
2281 template<typename _RealType>
2283 piecewise_linear_distribution<_RealType>::param_type::
2286 if (_M_int.size() < 2)
2290 _M_int.push_back(_RealType(0));
2291 _M_int.push_back(_RealType(1));
2295 _M_den.push_back(1.0);
2296 _M_den.push_back(1.0);
2302 _M_cp.reserve(_M_int.size() - 1);
2303 _M_m.reserve(_M_int.size() - 1);
2304 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2306 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2307 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2308 _M_cp.push_back(__sum);
2309 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2312 // Now normalize the densities...
2313 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2314 std::bind2nd(std::divides<double>(), __sum));
2315 // ... and partial sums...
2316 std::transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2317 std::bind2nd(std::divides<double>(), __sum));
2319 std::transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2320 std::bind2nd(std::divides<double>(), __sum));
2321 // Make sure the last cumulative probablility is one.
2322 _M_cp[_M_cp.size() - 1] = 1.0;
2325 template<typename _RealType>
2326 template<typename _InputIteratorB, typename _InputIteratorW>
2327 piecewise_linear_distribution<_RealType>::param_type::
2328 param_type(_InputIteratorB __bbegin,
2329 _InputIteratorB __bend,
2330 _InputIteratorW __wbegin)
2331 : _M_int(), _M_den(), _M_cp(), _M_m()
2333 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2335 _M_int.push_back(*__bbegin);
2336 _M_den.push_back(*__wbegin);
2342 template<typename _RealType>
2343 template<typename _Func>
2344 piecewise_linear_distribution<_RealType>::param_type::
2345 param_type(initializer_list<_RealType> __bl, _Func __fw)
2346 : _M_int(), _M_den(), _M_cp(), _M_m()
2348 _M_int.reserve(__bl.size());
2349 _M_den.reserve(__bl.size());
2350 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2352 _M_int.push_back(*__biter);
2353 _M_den.push_back(__fw(*__biter));
2359 template<typename _RealType>
2360 template<typename _Func>
2361 piecewise_linear_distribution<_RealType>::param_type::
2362 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2363 : _M_int(), _M_den(), _M_cp(), _M_m()
2365 const size_t __n = __nw == 0 ? 1 : __nw;
2366 const _RealType __delta = (__xmax - __xmin) / __n;
2368 _M_int.reserve(__n + 1);
2369 _M_den.reserve(__n + 1);
2370 for (size_t __k = 0; __k <= __nw; ++__k)
2372 _M_int.push_back(__xmin + __k * __delta);
2373 _M_den.push_back(__fw(_M_int[__k] + __delta));
2379 template<typename _RealType>
2380 template<typename _UniformRandomNumberGenerator>
2381 typename piecewise_linear_distribution<_RealType>::result_type
2382 piecewise_linear_distribution<_RealType>::
2383 operator()(_UniformRandomNumberGenerator& __urng,
2384 const param_type& __param)
2386 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2389 const double __p = __aurng();
2390 auto __pos = std::lower_bound(__param._M_cp.begin(),
2391 __param._M_cp.end(), __p);
2392 const size_t __i = __pos - __param._M_cp.begin();
2394 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2396 const double __a = 0.5 * __param._M_m[__i];
2397 const double __b = __param._M_den[__i];
2398 const double __cm = __p - __pref;
2400 _RealType __x = __param._M_int[__i];
2405 const double __d = __b * __b + 4.0 * __a * __cm;
2406 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2412 template<typename _RealType, typename _CharT, typename _Traits>
2413 std::basic_ostream<_CharT, _Traits>&
2414 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2415 const piecewise_linear_distribution<_RealType>& __x)
2417 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2418 typedef typename __ostream_type::ios_base __ios_base;
2420 const typename __ios_base::fmtflags __flags = __os.flags();
2421 const _CharT __fill = __os.fill();
2422 const std::streamsize __precision = __os.precision();
2423 const _CharT __space = __os.widen(' ');
2424 __os.flags(__ios_base::scientific | __ios_base::left);
2426 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2428 std::vector<_RealType> __int = __x.intervals();
2429 __os << __int.size() - 1;
2431 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2432 __os << __space << *__xit;
2434 std::vector<double> __den = __x.densities();
2435 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2436 __os << __space << *__dit;
2438 __os.flags(__flags);
2440 __os.precision(__precision);
2444 template<typename _RealType, typename _CharT, typename _Traits>
2445 std::basic_istream<_CharT, _Traits>&
2446 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2447 piecewise_linear_distribution<_RealType>& __x)
2449 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2450 typedef typename __istream_type::ios_base __ios_base;
2452 const typename __ios_base::fmtflags __flags = __is.flags();
2453 __is.flags(__ios_base::dec | __ios_base::skipws);
2458 std::vector<_RealType> __int_vec;
2459 __int_vec.reserve(__n + 1);
2460 for (size_t __i = 0; __i <= __n; ++__i)
2464 __int_vec.push_back(__int);
2467 std::vector<double> __den_vec;
2468 __den_vec.reserve(__n + 1);
2469 for (size_t __i = 0; __i <= __n; ++__i)
2473 __den_vec.push_back(__den);
2476 __x.param(typename piecewise_linear_distribution<_RealType>::
2477 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2479 __is.flags(__flags);
2484 template<typename _IntType>
2485 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2487 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2488 _M_v.push_back(__detail::__mod<result_type, 1, 0,
2489 __detail::_Shift<result_type, 32>::__value>(*__iter));
2492 template<typename _InputIterator>
2493 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2495 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2496 _M_v.push_back(__detail::__mod<result_type, 1, 0,
2497 __detail::_Shift<result_type, 32>::__value>(*__iter));
2500 template<typename _RandomAccessIterator>
2502 seed_seq::generate(_RandomAccessIterator __begin,
2503 _RandomAccessIterator __end)
2505 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2508 if (__begin == __end)
2511 std::fill(__begin, __end, _Type(0x8b8b8b8bU));
2513 const size_t __n = __end - __begin;
2514 const size_t __s = _M_v.size();
2515 const size_t __t = (__n >= 623) ? 11
2520 const size_t __p = (__n - __t) / 2;
2521 const size_t __q = __p + __t;
2522 const size_t __m = std::max(__s + 1, __n);
2524 for (size_t __k = 0; __k < __m; ++__k)
2526 _Type __arg = (__begin[__k % __n]
2527 ^ __begin[(__k + __p) % __n]
2528 ^ __begin[(__k - 1) % __n]);
2529 _Type __r1 = __arg ^ (__arg << 27);
2530 __r1 = __detail::__mod<_Type, 1664525U, 0U,
2531 __detail::_Shift<_Type, 32>::__value>(__r1);
2535 else if (__k <= __s)
2536 __r2 += __k % __n + _M_v[__k - 1];
2539 __r2 = __detail::__mod<_Type, 1U, 0U,
2540 __detail::_Shift<_Type, 32>::__value>(__r2);
2541 __begin[(__k + __p) % __n] += __r1;
2542 __begin[(__k + __q) % __n] += __r2;
2543 __begin[__k % __n] = __r2;
2546 for (size_t __k = __m; __k < __m + __n; ++__k)
2548 _Type __arg = (__begin[__k % __n]
2549 + __begin[(__k + __p) % __n]
2550 + __begin[(__k - 1) % __n]);
2551 _Type __r3 = __arg ^ (__arg << 27);
2552 __r3 = __detail::__mod<_Type, 1566083941U, 0U,
2553 __detail::_Shift<_Type, 32>::__value>(__r3);
2554 _Type __r4 = __r3 - __k % __n;
2555 __r4 = __detail::__mod<_Type, 1U, 0U,
2556 __detail::_Shift<_Type, 32>::__value>(__r4);
2557 __begin[(__k + __p) % __n] ^= __r4;
2558 __begin[(__k + __q) % __n] ^= __r3;
2559 __begin[__k % __n] = __r4;
2563 template<typename _RealType, size_t __bits,
2564 typename _UniformRandomNumberGenerator>
2566 generate_canonical(_UniformRandomNumberGenerator& __urng)
2569 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2571 const long double __r = static_cast<long double>(__urng.max())
2572 - static_cast<long double>(__urng.min()) + 1.0L;
2573 const size_t __log2r = std::log(__r) / std::log(2.0L);
2574 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2575 _RealType __sum = _RealType(0);
2576 _RealType __tmp = _RealType(1);
2577 for (; __k != 0; --__k)
2579 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2582 return __sum / __tmp;