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 __m, _Tp __a, _Tp __c, 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 __m, _Tp __a, _Tp __c>
84 struct _Mod<_Tp, __m, __a, __c, 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, __m>(__c) == 0)
102 && (__detail::__mod<_UIntType, __m>(__s) == 0))
105 _M_x = __detail::__mod<_UIntType, __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 uint_least32_t __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,
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);
190 __x += __detail::__mod<_UIntType, __n>(__i);
191 _M_x[__i] = __detail::__mod<_UIntType,
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 uint_least32_t __arr[__n * __k];
210 __q.generate(__arr + 0, __arr + __n * __k);
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[__k * __i + __j] * __factor;
220 __factor *= __detail::_Shift<_UIntType, 32>::__value;
222 _M_x[__i] = __detail::__mod<_UIntType,
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 - 1>::__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)
348 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
349 __lcg(__value == 0u ? default_seed : __value);
351 const size_t __n = (__w + 31) / 32;
353 for (size_t __i = 0; __i < long_lag; ++__i)
355 _UIntType __sum = 0u;
356 _UIntType __factor = 1u;
357 for (size_t __j = 0; __j < __n; ++__j)
359 __sum += __detail::__mod<uint_least32_t,
360 __detail::_Shift<uint_least32_t, 32>::__value>
361 (__lcg()) * __factor;
362 __factor *= __detail::_Shift<_UIntType, 32>::__value;
364 _M_x[__i] = __detail::__mod<_UIntType,
365 __detail::_Shift<_UIntType, __w>::__value>(__sum);
367 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
371 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
373 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
376 const size_t __k = (__w + 31) / 32;
377 uint_least32_t __arr[__r * __k];
378 __q.generate(__arr + 0, __arr + __r * __k);
380 for (size_t __i = 0; __i < long_lag; ++__i)
382 _UIntType __sum = 0u;
383 _UIntType __factor = 1u;
384 for (size_t __j = 0; __j < __k; ++__j)
386 __sum += __arr[__k * __i + __j] * __factor;
387 __factor *= __detail::_Shift<_UIntType, 32>::__value;
389 _M_x[__i] = __detail::__mod<_UIntType,
390 __detail::_Shift<_UIntType, __w>::__value>(__sum);
392 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
396 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
397 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
399 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
402 // Derive short lag index from current index.
403 long __ps = _M_p - short_lag;
407 // Calculate new x(i) without overflow or division.
408 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
411 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
413 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
418 __xi = (__detail::_Shift<_UIntType, __w>::__value
419 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
424 // Adjust current index to loop around in ring buffer.
425 if (++_M_p >= long_lag)
431 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
432 typename _CharT, typename _Traits>
433 std::basic_ostream<_CharT, _Traits>&
434 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
435 const subtract_with_carry_engine<_UIntType,
438 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
439 typedef typename __ostream_type::ios_base __ios_base;
441 const typename __ios_base::fmtflags __flags = __os.flags();
442 const _CharT __fill = __os.fill();
443 const _CharT __space = __os.widen(' ');
444 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
447 for (size_t __i = 0; __i < __r; ++__i)
448 __os << __x._M_x[__i] << __space;
449 __os << __x._M_carry;
456 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
457 typename _CharT, typename _Traits>
458 std::basic_istream<_CharT, _Traits>&
459 operator>>(std::basic_istream<_CharT, _Traits>& __is,
460 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
462 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
463 typedef typename __istream_type::ios_base __ios_base;
465 const typename __ios_base::fmtflags __flags = __is.flags();
466 __is.flags(__ios_base::dec | __ios_base::skipws);
468 for (size_t __i = 0; __i < __r; ++__i)
469 __is >> __x._M_x[__i];
470 __is >> __x._M_carry;
477 template<typename _RandomNumberEngine, size_t __p, size_t __r>
478 typename discard_block_engine<_RandomNumberEngine,
479 __p, __r>::result_type
480 discard_block_engine<_RandomNumberEngine, __p, __r>::
483 if (_M_n >= used_block)
485 _M_b.discard(block_size - _M_n);
492 template<typename _RandomNumberEngine, size_t __p, size_t __r,
493 typename _CharT, typename _Traits>
494 std::basic_ostream<_CharT, _Traits>&
495 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
496 const discard_block_engine<_RandomNumberEngine,
499 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
500 typedef typename __ostream_type::ios_base __ios_base;
502 const typename __ios_base::fmtflags __flags = __os.flags();
503 const _CharT __fill = __os.fill();
504 const _CharT __space = __os.widen(' ');
505 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
508 __os << __x.base() << __space << __x._M_n;
515 template<typename _RandomNumberEngine, size_t __p, size_t __r,
516 typename _CharT, typename _Traits>
517 std::basic_istream<_CharT, _Traits>&
518 operator>>(std::basic_istream<_CharT, _Traits>& __is,
519 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
521 typedef std::basic_istream<_CharT, _Traits> __istream_type;
522 typedef typename __istream_type::ios_base __ios_base;
524 const typename __ios_base::fmtflags __flags = __is.flags();
525 __is.flags(__ios_base::dec | __ios_base::skipws);
527 __is >> __x._M_b >> __x._M_n;
534 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
535 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
537 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
540 const long double __r = static_cast<long double>(_M_b.max())
541 - static_cast<long double>(_M_b.min()) + 1.0L;
542 const result_type __m = std::log(__r) / std::log(2.0L);
543 result_type __n, __n0, __y0, __y1, __s0, __s1;
544 for (size_t __i = 0; __i < 2; ++__i)
546 __n = (__w + __m - 1) / __m + __i;
547 __n0 = __n - __w % __n;
548 const result_type __w0 = __w / __n;
549 const result_type __w1 = __w0 + 1;
550 __s0 = result_type(1) << __w0;
551 __s1 = result_type(1) << __w1;
552 __y0 = __s0 * (__r / __s0);
553 __y1 = __s1 * (__r / __s1);
554 if (__r - __y0 <= __y0 / __n)
558 result_type __sum = 0;
559 for (size_t __k = 0; __k < __n0; ++__k)
563 __u = _M_b() - _M_b.min();
565 __sum = __s0 * __sum + __u % __s0;
567 for (size_t __k = __n0; __k < __n; ++__k)
571 __u = _M_b() - _M_b.min();
573 __sum = __s1 * __sum + __u % __s1;
579 template<typename _RandomNumberEngine, size_t __k>
580 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
581 shuffle_order_engine<_RandomNumberEngine, __k>::
584 size_t __j = __k * ((_M_y - _M_b.min())
585 / (_M_b.max() - _M_b.min() + 1.0L));
592 template<typename _RandomNumberEngine, size_t __k,
593 typename _CharT, typename _Traits>
594 std::basic_ostream<_CharT, _Traits>&
595 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
596 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
598 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
599 typedef typename __ostream_type::ios_base __ios_base;
601 const typename __ios_base::fmtflags __flags = __os.flags();
602 const _CharT __fill = __os.fill();
603 const _CharT __space = __os.widen(' ');
604 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
608 for (size_t __i = 0; __i < __k; ++__i)
609 __os << __space << __x._M_v[__i];
610 __os << __space << __x._M_y;
617 template<typename _RandomNumberEngine, size_t __k,
618 typename _CharT, typename _Traits>
619 std::basic_istream<_CharT, _Traits>&
620 operator>>(std::basic_istream<_CharT, _Traits>& __is,
621 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
623 typedef std::basic_istream<_CharT, _Traits> __istream_type;
624 typedef typename __istream_type::ios_base __ios_base;
626 const typename __ios_base::fmtflags __flags = __is.flags();
627 __is.flags(__ios_base::dec | __ios_base::skipws);
630 for (size_t __i = 0; __i < __k; ++__i)
631 __is >> __x._M_v[__i];
639 template<typename _IntType>
640 template<typename _UniformRandomNumberGenerator>
641 typename uniform_int_distribution<_IntType>::result_type
642 uniform_int_distribution<_IntType>::
643 operator()(_UniformRandomNumberGenerator& __urng,
644 const param_type& __param)
646 // XXX Must be fixed to work well for *arbitrary* __urng.max(),
647 // __urng.min(), __param.b(), __param.a(). Currently works fine only
648 // in the most common case __urng.max() - __urng.min() >=
649 // __param.b() - __param.a(), with __urng.max() > __urng.min() >= 0.
650 typedef typename __gnu_cxx::__add_unsigned<typename
651 _UniformRandomNumberGenerator::result_type>::__type __urntype;
652 typedef typename __gnu_cxx::__add_unsigned<result_type>::__type
654 typedef typename __gnu_cxx::__conditional_type<(sizeof(__urntype)
656 __urntype, __utype>::__type __uctype;
660 const __urntype __urnmin = __urng.min();
661 const __urntype __urnmax = __urng.max();
662 const __urntype __urnrange = __urnmax - __urnmin;
663 const __uctype __urange = __param.b() - __param.a();
664 const __uctype __udenom = (__urnrange <= __urange
665 ? 1 : __urnrange / (__urange + 1));
667 __ret = (__urntype(__urng()) - __urnmin) / __udenom;
668 while (__ret > __param.b() - __param.a());
670 return __ret + __param.a();
673 template<typename _IntType, typename _CharT, typename _Traits>
674 std::basic_ostream<_CharT, _Traits>&
675 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
676 const uniform_int_distribution<_IntType>& __x)
678 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
679 typedef typename __ostream_type::ios_base __ios_base;
681 const typename __ios_base::fmtflags __flags = __os.flags();
682 const _CharT __fill = __os.fill();
683 const _CharT __space = __os.widen(' ');
684 __os.flags(__ios_base::scientific | __ios_base::left);
687 __os << __x.a() << __space << __x.b();
694 template<typename _IntType, typename _CharT, typename _Traits>
695 std::basic_istream<_CharT, _Traits>&
696 operator>>(std::basic_istream<_CharT, _Traits>& __is,
697 uniform_int_distribution<_IntType>& __x)
699 typedef std::basic_istream<_CharT, _Traits> __istream_type;
700 typedef typename __istream_type::ios_base __ios_base;
702 const typename __ios_base::fmtflags __flags = __is.flags();
703 __is.flags(__ios_base::dec | __ios_base::skipws);
707 __x.param(typename uniform_int_distribution<_IntType>::
708 param_type(__a, __b));
715 template<typename _RealType, typename _CharT, typename _Traits>
716 std::basic_ostream<_CharT, _Traits>&
717 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
718 const uniform_real_distribution<_RealType>& __x)
720 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
721 typedef typename __ostream_type::ios_base __ios_base;
723 const typename __ios_base::fmtflags __flags = __os.flags();
724 const _CharT __fill = __os.fill();
725 const std::streamsize __precision = __os.precision();
726 const _CharT __space = __os.widen(' ');
727 __os.flags(__ios_base::scientific | __ios_base::left);
729 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
731 __os << __x.a() << __space << __x.b();
735 __os.precision(__precision);
739 template<typename _RealType, typename _CharT, typename _Traits>
740 std::basic_istream<_CharT, _Traits>&
741 operator>>(std::basic_istream<_CharT, _Traits>& __is,
742 uniform_real_distribution<_RealType>& __x)
744 typedef std::basic_istream<_CharT, _Traits> __istream_type;
745 typedef typename __istream_type::ios_base __ios_base;
747 const typename __ios_base::fmtflags __flags = __is.flags();
748 __is.flags(__ios_base::skipws);
752 __x.param(typename uniform_real_distribution<_RealType>::
753 param_type(__a, __b));
760 template<typename _CharT, typename _Traits>
761 std::basic_ostream<_CharT, _Traits>&
762 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
763 const bernoulli_distribution& __x)
765 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
766 typedef typename __ostream_type::ios_base __ios_base;
768 const typename __ios_base::fmtflags __flags = __os.flags();
769 const _CharT __fill = __os.fill();
770 const std::streamsize __precision = __os.precision();
771 __os.flags(__ios_base::scientific | __ios_base::left);
772 __os.fill(__os.widen(' '));
773 __os.precision(std::numeric_limits<double>::digits10 + 1);
779 __os.precision(__precision);
784 template<typename _IntType>
785 template<typename _UniformRandomNumberGenerator>
786 typename geometric_distribution<_IntType>::result_type
787 geometric_distribution<_IntType>::
788 operator()(_UniformRandomNumberGenerator& __urng,
789 const param_type& __param)
791 // About the epsilon thing see this thread:
792 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
794 (1 - std::numeric_limits<double>::epsilon()) / 2;
795 // The largest _RealType convertible to _IntType.
797 std::numeric_limits<_IntType>::max() + __naf;
798 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
803 __cand = std::ceil(std::log(__aurng()) / __param._M_log_p);
804 while (__cand >= __thr);
806 return result_type(__cand + __naf);
809 template<typename _IntType,
810 typename _CharT, typename _Traits>
811 std::basic_ostream<_CharT, _Traits>&
812 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
813 const geometric_distribution<_IntType>& __x)
815 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
816 typedef typename __ostream_type::ios_base __ios_base;
818 const typename __ios_base::fmtflags __flags = __os.flags();
819 const _CharT __fill = __os.fill();
820 const std::streamsize __precision = __os.precision();
821 __os.flags(__ios_base::scientific | __ios_base::left);
822 __os.fill(__os.widen(' '));
823 __os.precision(std::numeric_limits<double>::digits10 + 1);
829 __os.precision(__precision);
833 template<typename _IntType,
834 typename _CharT, typename _Traits>
835 std::basic_istream<_CharT, _Traits>&
836 operator>>(std::basic_istream<_CharT, _Traits>& __is,
837 geometric_distribution<_IntType>& __x)
839 typedef std::basic_istream<_CharT, _Traits> __istream_type;
840 typedef typename __istream_type::ios_base __ios_base;
842 const typename __ios_base::fmtflags __flags = __is.flags();
843 __is.flags(__ios_base::skipws);
847 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
854 template<typename _IntType>
855 template<typename _UniformRandomNumberGenerator>
856 typename negative_binomial_distribution<_IntType>::result_type
857 negative_binomial_distribution<_IntType>::
858 operator()(_UniformRandomNumberGenerator& __urng)
860 const double __y = _M_gd(__urng);
862 // XXX Is the constructor too slow?
863 std::poisson_distribution<result_type> __poisson(__y);
864 return __poisson(__urng);
867 template<typename _IntType>
868 template<typename _UniformRandomNumberGenerator>
869 typename negative_binomial_distribution<_IntType>::result_type
870 negative_binomial_distribution<_IntType>::
871 operator()(_UniformRandomNumberGenerator& __urng,
872 const param_type& __p)
874 typedef typename std::gamma_distribution<result_type>::param_type
878 _M_gd(__urng, param_type(__p.k(), __p.p() / (1.0 - __p.p())));
880 std::poisson_distribution<result_type> __poisson(__y);
881 return __poisson(__urng);
884 template<typename _IntType, typename _CharT, typename _Traits>
885 std::basic_ostream<_CharT, _Traits>&
886 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
887 const negative_binomial_distribution<_IntType>& __x)
889 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
890 typedef typename __ostream_type::ios_base __ios_base;
892 const typename __ios_base::fmtflags __flags = __os.flags();
893 const _CharT __fill = __os.fill();
894 const std::streamsize __precision = __os.precision();
895 const _CharT __space = __os.widen(' ');
896 __os.flags(__ios_base::scientific | __ios_base::left);
897 __os.fill(__os.widen(' '));
898 __os.precision(std::numeric_limits<double>::digits10 + 1);
900 __os << __x.k() << __space << __x.p()
901 << __space << __x._M_gd;
905 __os.precision(__precision);
909 template<typename _IntType, typename _CharT, typename _Traits>
910 std::basic_istream<_CharT, _Traits>&
911 operator>>(std::basic_istream<_CharT, _Traits>& __is,
912 negative_binomial_distribution<_IntType>& __x)
914 typedef std::basic_istream<_CharT, _Traits> __istream_type;
915 typedef typename __istream_type::ios_base __ios_base;
917 const typename __ios_base::fmtflags __flags = __is.flags();
918 __is.flags(__ios_base::skipws);
922 __is >> __k >> __p >> __x._M_gd;
923 __x.param(typename negative_binomial_distribution<_IntType>::
924 param_type(__k, __p));
931 template<typename _IntType>
933 poisson_distribution<_IntType>::param_type::
936 #if _GLIBCXX_USE_C99_MATH_TR1
939 const double __m = std::floor(_M_mean);
940 _M_lm_thr = std::log(_M_mean);
941 _M_lfm = std::lgamma(__m + 1);
942 _M_sm = std::sqrt(__m);
944 const double __pi_4 = 0.7853981633974483096156608458198757L;
945 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
947 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
948 const double __cx = 2 * __m + _M_d;
949 _M_scx = std::sqrt(__cx / 2);
952 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
953 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
958 _M_lm_thr = std::exp(-_M_mean);
962 * A rejection algorithm when mean >= 12 and a simple method based
963 * upon the multiplication of uniform random variates otherwise.
964 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
968 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
969 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
971 template<typename _IntType>
972 template<typename _UniformRandomNumberGenerator>
973 typename poisson_distribution<_IntType>::result_type
974 poisson_distribution<_IntType>::
975 operator()(_UniformRandomNumberGenerator& __urng,
976 const param_type& __param)
978 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
980 #if _GLIBCXX_USE_C99_MATH_TR1
981 if (__param.mean() >= 12)
985 // See comments above...
987 (1 - std::numeric_limits<double>::epsilon()) / 2;
989 std::numeric_limits<_IntType>::max() + __naf;
991 const double __m = std::floor(__param.mean());
993 const double __spi_2 = 1.2533141373155002512078826424055226L;
994 const double __c1 = __param._M_sm * __spi_2;
995 const double __c2 = __param._M_c2b + __c1;
996 const double __c3 = __c2 + 1;
997 const double __c4 = __c3 + 1;
999 const double __e178 = 1.0129030479320018583185514777512983L;
1000 const double __c5 = __c4 + __e178;
1001 const double __c = __param._M_cb + __c5;
1002 const double __2cx = 2 * (2 * __m + __param._M_d);
1004 bool __reject = true;
1007 const double __u = __c * __aurng();
1008 const double __e = -std::log(__aurng());
1014 const double __n = _M_nd(__urng);
1015 const double __y = -std::abs(__n) * __param._M_sm - 1;
1016 __x = std::floor(__y);
1017 __w = -__n * __n / 2;
1021 else if (__u <= __c2)
1023 const double __n = _M_nd(__urng);
1024 const double __y = 1 + std::abs(__n) * __param._M_scx;
1025 __x = std::ceil(__y);
1026 __w = __y * (2 - __y) * __param._M_1cx;
1027 if (__x > __param._M_d)
1030 else if (__u <= __c3)
1031 // NB: This case not in the book, nor in the Errata,
1032 // but should be ok...
1034 else if (__u <= __c4)
1036 else if (__u <= __c5)
1040 const double __v = -std::log(__aurng());
1041 const double __y = __param._M_d
1042 + __v * __2cx / __param._M_d;
1043 __x = std::ceil(__y);
1044 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1047 __reject = (__w - __e - __x * __param._M_lm_thr
1048 > __param._M_lfm - std::lgamma(__x + __m + 1));
1050 __reject |= __x + __m >= __thr;
1054 return result_type(__x + __m + __naf);
1060 double __prod = 1.0;
1064 __prod *= __aurng();
1067 while (__prod > __param._M_lm_thr);
1073 template<typename _IntType,
1074 typename _CharT, typename _Traits>
1075 std::basic_ostream<_CharT, _Traits>&
1076 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1077 const poisson_distribution<_IntType>& __x)
1079 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1080 typedef typename __ostream_type::ios_base __ios_base;
1082 const typename __ios_base::fmtflags __flags = __os.flags();
1083 const _CharT __fill = __os.fill();
1084 const std::streamsize __precision = __os.precision();
1085 const _CharT __space = __os.widen(' ');
1086 __os.flags(__ios_base::scientific | __ios_base::left);
1088 __os.precision(std::numeric_limits<double>::digits10 + 1);
1090 __os << __x.mean() << __space << __x._M_nd;
1092 __os.flags(__flags);
1094 __os.precision(__precision);
1098 template<typename _IntType,
1099 typename _CharT, typename _Traits>
1100 std::basic_istream<_CharT, _Traits>&
1101 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1102 poisson_distribution<_IntType>& __x)
1104 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1105 typedef typename __istream_type::ios_base __ios_base;
1107 const typename __ios_base::fmtflags __flags = __is.flags();
1108 __is.flags(__ios_base::skipws);
1111 __is >> __mean >> __x._M_nd;
1112 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1114 __is.flags(__flags);
1119 template<typename _IntType>
1121 binomial_distribution<_IntType>::param_type::
1124 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1128 #if _GLIBCXX_USE_C99_MATH_TR1
1129 if (_M_t * __p12 >= 8)
1132 const double __np = std::floor(_M_t * __p12);
1133 const double __pa = __np / _M_t;
1134 const double __1p = 1 - __pa;
1136 const double __pi_4 = 0.7853981633974483096156608458198757L;
1137 const double __d1x =
1138 std::sqrt(__np * __1p * std::log(32 * __np
1139 / (81 * __pi_4 * __1p)));
1140 _M_d1 = std::round(std::max(1.0, __d1x));
1141 const double __d2x =
1142 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1143 / (__pi_4 * __pa)));
1144 _M_d2 = std::round(std::max(1.0, __d2x));
1147 const double __spi_2 = 1.2533141373155002512078826424055226L;
1148 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1149 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1150 _M_c = 2 * _M_d1 / __np;
1151 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1152 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1153 const double __s1s = _M_s1 * _M_s1;
1154 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1156 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1157 const double __s2s = _M_s2 * _M_s2;
1158 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1159 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1160 _M_lf = (std::lgamma(__np + 1)
1161 + std::lgamma(_M_t - __np + 1));
1162 _M_lp1p = std::log(__pa / __1p);
1164 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1168 _M_q = -std::log(1 - __p12);
1171 template<typename _IntType>
1172 template<typename _UniformRandomNumberGenerator>
1173 typename binomial_distribution<_IntType>::result_type
1174 binomial_distribution<_IntType>::
1175 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1179 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1184 const double __e = -std::log(__aurng());
1185 __sum += __e / (__t - __x);
1188 while (__sum <= _M_param._M_q);
1194 * A rejection algorithm when t * p >= 8 and a simple waiting time
1195 * method - the second in the referenced book - otherwise.
1196 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1200 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1201 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1203 template<typename _IntType>
1204 template<typename _UniformRandomNumberGenerator>
1205 typename binomial_distribution<_IntType>::result_type
1206 binomial_distribution<_IntType>::
1207 operator()(_UniformRandomNumberGenerator& __urng,
1208 const param_type& __param)
1211 const _IntType __t = __param.t();
1212 const _IntType __p = __param.p();
1213 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1214 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1217 #if _GLIBCXX_USE_C99_MATH_TR1
1218 if (!__param._M_easy)
1222 // See comments above...
1223 const double __naf =
1224 (1 - std::numeric_limits<double>::epsilon()) / 2;
1225 const double __thr =
1226 std::numeric_limits<_IntType>::max() + __naf;
1228 const double __np = std::floor(__t * __p12);
1231 const double __spi_2 = 1.2533141373155002512078826424055226L;
1232 const double __a1 = __param._M_a1;
1233 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1234 const double __a123 = __param._M_a123;
1235 const double __s1s = __param._M_s1 * __param._M_s1;
1236 const double __s2s = __param._M_s2 * __param._M_s2;
1241 const double __u = __param._M_s * __aurng();
1247 const double __n = _M_nd(__urng);
1248 const double __y = __param._M_s1 * std::abs(__n);
1249 __reject = __y >= __param._M_d1;
1252 const double __e = -std::log(__aurng());
1253 __x = std::floor(__y);
1254 __v = -__e - __n * __n / 2 + __param._M_c;
1257 else if (__u <= __a12)
1259 const double __n = _M_nd(__urng);
1260 const double __y = __param._M_s2 * std::abs(__n);
1261 __reject = __y >= __param._M_d2;
1264 const double __e = -std::log(__aurng());
1265 __x = std::floor(-__y);
1266 __v = -__e - __n * __n / 2;
1269 else if (__u <= __a123)
1271 const double __e1 = -std::log(__aurng());
1272 const double __e2 = -std::log(__aurng());
1274 const double __y = __param._M_d1
1275 + 2 * __s1s * __e1 / __param._M_d1;
1276 __x = std::floor(__y);
1277 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1278 -__y / (2 * __s1s)));
1283 const double __e1 = -std::log(__aurng());
1284 const double __e2 = -std::log(__aurng());
1286 const double __y = __param._M_d2
1287 + 2 * __s2s * __e1 / __param._M_d2;
1288 __x = std::floor(-__y);
1289 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1293 __reject = __reject || __x < -__np || __x > __t - __np;
1296 const double __lfx =
1297 std::lgamma(__np + __x + 1)
1298 + std::lgamma(__t - (__np + __x) + 1);
1299 __reject = __v > __param._M_lf - __lfx
1300 + __x * __param._M_lp1p;
1303 __reject |= __x + __np >= __thr;
1307 __x += __np + __naf;
1309 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1310 __ret = _IntType(__x) + __z;
1314 __ret = _M_waiting(__urng, __t);
1317 __ret = __t - __ret;
1321 template<typename _IntType,
1322 typename _CharT, typename _Traits>
1323 std::basic_ostream<_CharT, _Traits>&
1324 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1325 const binomial_distribution<_IntType>& __x)
1327 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1328 typedef typename __ostream_type::ios_base __ios_base;
1330 const typename __ios_base::fmtflags __flags = __os.flags();
1331 const _CharT __fill = __os.fill();
1332 const std::streamsize __precision = __os.precision();
1333 const _CharT __space = __os.widen(' ');
1334 __os.flags(__ios_base::scientific | __ios_base::left);
1336 __os.precision(std::numeric_limits<double>::digits10 + 1);
1338 __os << __x.t() << __space << __x.p()
1339 << __space << __x._M_nd;
1341 __os.flags(__flags);
1343 __os.precision(__precision);
1347 template<typename _IntType,
1348 typename _CharT, typename _Traits>
1349 std::basic_istream<_CharT, _Traits>&
1350 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1351 binomial_distribution<_IntType>& __x)
1353 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1354 typedef typename __istream_type::ios_base __ios_base;
1356 const typename __ios_base::fmtflags __flags = __is.flags();
1357 __is.flags(__ios_base::dec | __ios_base::skipws);
1361 __is >> __t >> __p >> __x._M_nd;
1362 __x.param(typename binomial_distribution<_IntType>::
1363 param_type(__t, __p));
1365 __is.flags(__flags);
1370 template<typename _RealType, typename _CharT, typename _Traits>
1371 std::basic_ostream<_CharT, _Traits>&
1372 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1373 const exponential_distribution<_RealType>& __x)
1375 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1376 typedef typename __ostream_type::ios_base __ios_base;
1378 const typename __ios_base::fmtflags __flags = __os.flags();
1379 const _CharT __fill = __os.fill();
1380 const std::streamsize __precision = __os.precision();
1381 __os.flags(__ios_base::scientific | __ios_base::left);
1382 __os.fill(__os.widen(' '));
1383 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1385 __os << __x.lambda();
1387 __os.flags(__flags);
1389 __os.precision(__precision);
1393 template<typename _RealType, typename _CharT, typename _Traits>
1394 std::basic_istream<_CharT, _Traits>&
1395 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1396 exponential_distribution<_RealType>& __x)
1398 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1399 typedef typename __istream_type::ios_base __ios_base;
1401 const typename __ios_base::fmtflags __flags = __is.flags();
1402 __is.flags(__ios_base::dec | __ios_base::skipws);
1406 __x.param(typename exponential_distribution<_RealType>::
1407 param_type(__lambda));
1409 __is.flags(__flags);
1415 * Polar method due to Marsaglia.
1417 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1418 * New York, 1986, Ch. V, Sect. 4.4.
1420 template<typename _RealType>
1421 template<typename _UniformRandomNumberGenerator>
1422 typename normal_distribution<_RealType>::result_type
1423 normal_distribution<_RealType>::
1424 operator()(_UniformRandomNumberGenerator& __urng,
1425 const param_type& __param)
1428 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1431 if (_M_saved_available)
1433 _M_saved_available = false;
1438 result_type __x, __y, __r2;
1441 __x = result_type(2.0) * __aurng() - 1.0;
1442 __y = result_type(2.0) * __aurng() - 1.0;
1443 __r2 = __x * __x + __y * __y;
1445 while (__r2 > 1.0 || __r2 == 0.0);
1447 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1448 _M_saved = __x * __mult;
1449 _M_saved_available = true;
1450 __ret = __y * __mult;
1453 __ret = __ret * __param.stddev() + __param.mean();
1457 template<typename _RealType, typename _CharT, typename _Traits>
1458 std::basic_ostream<_CharT, _Traits>&
1459 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1460 const normal_distribution<_RealType>& __x)
1462 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1463 typedef typename __ostream_type::ios_base __ios_base;
1465 const typename __ios_base::fmtflags __flags = __os.flags();
1466 const _CharT __fill = __os.fill();
1467 const std::streamsize __precision = __os.precision();
1468 const _CharT __space = __os.widen(' ');
1469 __os.flags(__ios_base::scientific | __ios_base::left);
1471 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1473 __os << __x.mean() << __space << __x.stddev()
1474 << __space << __x._M_saved_available;
1475 if (__x._M_saved_available)
1476 __os << __space << __x._M_saved;
1478 __os.flags(__flags);
1480 __os.precision(__precision);
1484 template<typename _RealType, typename _CharT, typename _Traits>
1485 std::basic_istream<_CharT, _Traits>&
1486 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1487 normal_distribution<_RealType>& __x)
1489 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1490 typedef typename __istream_type::ios_base __ios_base;
1492 const typename __ios_base::fmtflags __flags = __is.flags();
1493 __is.flags(__ios_base::dec | __ios_base::skipws);
1495 double __mean, __stddev;
1496 __is >> __mean >> __stddev
1497 >> __x._M_saved_available;
1498 if (__x._M_saved_available)
1499 __is >> __x._M_saved;
1500 __x.param(typename normal_distribution<_RealType>::
1501 param_type(__mean, __stddev));
1503 __is.flags(__flags);
1508 template<typename _RealType, typename _CharT, typename _Traits>
1509 std::basic_ostream<_CharT, _Traits>&
1510 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1511 const lognormal_distribution<_RealType>& __x)
1513 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1514 typedef typename __ostream_type::ios_base __ios_base;
1516 const typename __ios_base::fmtflags __flags = __os.flags();
1517 const _CharT __fill = __os.fill();
1518 const std::streamsize __precision = __os.precision();
1519 const _CharT __space = __os.widen(' ');
1520 __os.flags(__ios_base::scientific | __ios_base::left);
1522 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1524 __os << __x.m() << __space << __x.s()
1525 << __space << __x._M_nd;
1527 __os.flags(__flags);
1529 __os.precision(__precision);
1533 template<typename _RealType, typename _CharT, typename _Traits>
1534 std::basic_istream<_CharT, _Traits>&
1535 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1536 lognormal_distribution<_RealType>& __x)
1538 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1539 typedef typename __istream_type::ios_base __ios_base;
1541 const typename __ios_base::fmtflags __flags = __is.flags();
1542 __is.flags(__ios_base::dec | __ios_base::skipws);
1545 __is >> __m >> __s >> __x._M_nd;
1546 __x.param(typename lognormal_distribution<_RealType>::
1547 param_type(__m, __s));
1549 __is.flags(__flags);
1554 template<typename _RealType, typename _CharT, typename _Traits>
1555 std::basic_ostream<_CharT, _Traits>&
1556 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1557 const chi_squared_distribution<_RealType>& __x)
1559 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1560 typedef typename __ostream_type::ios_base __ios_base;
1562 const typename __ios_base::fmtflags __flags = __os.flags();
1563 const _CharT __fill = __os.fill();
1564 const std::streamsize __precision = __os.precision();
1565 const _CharT __space = __os.widen(' ');
1566 __os.flags(__ios_base::scientific | __ios_base::left);
1568 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1570 __os << __x.n() << __space << __x._M_gd;
1572 __os.flags(__flags);
1574 __os.precision(__precision);
1578 template<typename _RealType, typename _CharT, typename _Traits>
1579 std::basic_istream<_CharT, _Traits>&
1580 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1581 chi_squared_distribution<_RealType>& __x)
1583 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1584 typedef typename __istream_type::ios_base __ios_base;
1586 const typename __ios_base::fmtflags __flags = __is.flags();
1587 __is.flags(__ios_base::dec | __ios_base::skipws);
1590 __is >> __n >> __x._M_gd;
1591 __x.param(typename chi_squared_distribution<_RealType>::
1594 __is.flags(__flags);
1599 template<typename _RealType>
1600 template<typename _UniformRandomNumberGenerator>
1601 typename cauchy_distribution<_RealType>::result_type
1602 cauchy_distribution<_RealType>::
1603 operator()(_UniformRandomNumberGenerator& __urng,
1604 const param_type& __p)
1606 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1613 const _RealType __pi = 3.1415926535897932384626433832795029L;
1614 return __p.a() + __p.b() * std::tan(__pi * __u);
1617 template<typename _RealType, typename _CharT, typename _Traits>
1618 std::basic_ostream<_CharT, _Traits>&
1619 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1620 const cauchy_distribution<_RealType>& __x)
1622 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1623 typedef typename __ostream_type::ios_base __ios_base;
1625 const typename __ios_base::fmtflags __flags = __os.flags();
1626 const _CharT __fill = __os.fill();
1627 const std::streamsize __precision = __os.precision();
1628 const _CharT __space = __os.widen(' ');
1629 __os.flags(__ios_base::scientific | __ios_base::left);
1631 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1633 __os << __x.a() << __space << __x.b();
1635 __os.flags(__flags);
1637 __os.precision(__precision);
1641 template<typename _RealType, typename _CharT, typename _Traits>
1642 std::basic_istream<_CharT, _Traits>&
1643 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1644 cauchy_distribution<_RealType>& __x)
1646 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1647 typedef typename __istream_type::ios_base __ios_base;
1649 const typename __ios_base::fmtflags __flags = __is.flags();
1650 __is.flags(__ios_base::dec | __ios_base::skipws);
1654 __x.param(typename cauchy_distribution<_RealType>::
1655 param_type(__a, __b));
1657 __is.flags(__flags);
1662 template<typename _RealType, typename _CharT, typename _Traits>
1663 std::basic_ostream<_CharT, _Traits>&
1664 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1665 const fisher_f_distribution<_RealType>& __x)
1667 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1668 typedef typename __ostream_type::ios_base __ios_base;
1670 const typename __ios_base::fmtflags __flags = __os.flags();
1671 const _CharT __fill = __os.fill();
1672 const std::streamsize __precision = __os.precision();
1673 const _CharT __space = __os.widen(' ');
1674 __os.flags(__ios_base::scientific | __ios_base::left);
1676 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1678 __os << __x.m() << __space << __x.n()
1679 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1681 __os.flags(__flags);
1683 __os.precision(__precision);
1687 template<typename _RealType, typename _CharT, typename _Traits>
1688 std::basic_istream<_CharT, _Traits>&
1689 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1690 fisher_f_distribution<_RealType>& __x)
1692 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1693 typedef typename __istream_type::ios_base __ios_base;
1695 const typename __ios_base::fmtflags __flags = __is.flags();
1696 __is.flags(__ios_base::dec | __ios_base::skipws);
1699 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1700 __x.param(typename fisher_f_distribution<_RealType>::
1701 param_type(__m, __n));
1703 __is.flags(__flags);
1708 template<typename _RealType, typename _CharT, typename _Traits>
1709 std::basic_ostream<_CharT, _Traits>&
1710 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1711 const student_t_distribution<_RealType>& __x)
1713 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1714 typedef typename __ostream_type::ios_base __ios_base;
1716 const typename __ios_base::fmtflags __flags = __os.flags();
1717 const _CharT __fill = __os.fill();
1718 const std::streamsize __precision = __os.precision();
1719 const _CharT __space = __os.widen(' ');
1720 __os.flags(__ios_base::scientific | __ios_base::left);
1722 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1724 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1726 __os.flags(__flags);
1728 __os.precision(__precision);
1732 template<typename _RealType, typename _CharT, typename _Traits>
1733 std::basic_istream<_CharT, _Traits>&
1734 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1735 student_t_distribution<_RealType>& __x)
1737 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1738 typedef typename __istream_type::ios_base __ios_base;
1740 const typename __ios_base::fmtflags __flags = __is.flags();
1741 __is.flags(__ios_base::dec | __ios_base::skipws);
1744 __is >> __n >> __x._M_nd >> __x._M_gd;
1745 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1747 __is.flags(__flags);
1752 template<typename _RealType>
1754 gamma_distribution<_RealType>::param_type::
1757 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
1759 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
1760 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
1764 * Marsaglia, G. and Tsang, W. W.
1765 * "A Simple Method for Generating Gamma Variables"
1766 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
1768 template<typename _RealType>
1769 template<typename _UniformRandomNumberGenerator>
1770 typename gamma_distribution<_RealType>::result_type
1771 gamma_distribution<_RealType>::
1772 operator()(_UniformRandomNumberGenerator& __urng,
1773 const param_type& __param)
1775 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1778 result_type __u, __v, __n;
1779 const result_type __a1 = (__param._M_malpha
1780 - _RealType(1.0) / _RealType(3.0));
1786 __n = _M_nd(__urng);
1787 __v = result_type(1.0) + __param._M_a2 * __n;
1791 __v = __v * __v * __v;
1794 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
1795 && (std::log(__u) > (0.5 * __n * __n + __a1
1796 * (1.0 - __v + std::log(__v)))));
1798 if (__param.alpha() == __param._M_malpha)
1799 return __a1 * __v * __param.beta();
1806 return (std::pow(__u, result_type(1.0) / __param.alpha())
1807 * __a1 * __v * __param.beta());
1811 template<typename _RealType, typename _CharT, typename _Traits>
1812 std::basic_ostream<_CharT, _Traits>&
1813 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1814 const gamma_distribution<_RealType>& __x)
1816 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1817 typedef typename __ostream_type::ios_base __ios_base;
1819 const typename __ios_base::fmtflags __flags = __os.flags();
1820 const _CharT __fill = __os.fill();
1821 const std::streamsize __precision = __os.precision();
1822 const _CharT __space = __os.widen(' ');
1823 __os.flags(__ios_base::scientific | __ios_base::left);
1825 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1827 __os << __x.alpha() << __space << __x.beta()
1828 << __space << __x._M_nd;
1830 __os.flags(__flags);
1832 __os.precision(__precision);
1836 template<typename _RealType, typename _CharT, typename _Traits>
1837 std::basic_istream<_CharT, _Traits>&
1838 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1839 gamma_distribution<_RealType>& __x)
1841 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1842 typedef typename __istream_type::ios_base __ios_base;
1844 const typename __ios_base::fmtflags __flags = __is.flags();
1845 __is.flags(__ios_base::dec | __ios_base::skipws);
1847 _RealType __alpha_val, __beta_val;
1848 __is >> __alpha_val >> __beta_val >> __x._M_nd;
1849 __x.param(typename gamma_distribution<_RealType>::
1850 param_type(__alpha_val, __beta_val));
1852 __is.flags(__flags);
1857 template<typename _RealType>
1858 template<typename _UniformRandomNumberGenerator>
1859 typename weibull_distribution<_RealType>::result_type
1860 weibull_distribution<_RealType>::
1861 operator()(_UniformRandomNumberGenerator& __urng,
1862 const param_type& __p)
1864 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1866 return __p.b() * std::pow(-std::log(__aurng()),
1867 result_type(1) / __p.a());
1870 template<typename _RealType, typename _CharT, typename _Traits>
1871 std::basic_ostream<_CharT, _Traits>&
1872 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1873 const weibull_distribution<_RealType>& __x)
1875 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1876 typedef typename __ostream_type::ios_base __ios_base;
1878 const typename __ios_base::fmtflags __flags = __os.flags();
1879 const _CharT __fill = __os.fill();
1880 const std::streamsize __precision = __os.precision();
1881 const _CharT __space = __os.widen(' ');
1882 __os.flags(__ios_base::scientific | __ios_base::left);
1884 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1886 __os << __x.a() << __space << __x.b();
1888 __os.flags(__flags);
1890 __os.precision(__precision);
1894 template<typename _RealType, typename _CharT, typename _Traits>
1895 std::basic_istream<_CharT, _Traits>&
1896 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1897 weibull_distribution<_RealType>& __x)
1899 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1900 typedef typename __istream_type::ios_base __ios_base;
1902 const typename __ios_base::fmtflags __flags = __is.flags();
1903 __is.flags(__ios_base::dec | __ios_base::skipws);
1907 __x.param(typename weibull_distribution<_RealType>::
1908 param_type(__a, __b));
1910 __is.flags(__flags);
1915 template<typename _RealType>
1916 template<typename _UniformRandomNumberGenerator>
1917 typename extreme_value_distribution<_RealType>::result_type
1918 extreme_value_distribution<_RealType>::
1919 operator()(_UniformRandomNumberGenerator& __urng,
1920 const param_type& __p)
1922 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1924 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
1927 template<typename _RealType, typename _CharT, typename _Traits>
1928 std::basic_ostream<_CharT, _Traits>&
1929 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1930 const extreme_value_distribution<_RealType>& __x)
1932 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1933 typedef typename __ostream_type::ios_base __ios_base;
1935 const typename __ios_base::fmtflags __flags = __os.flags();
1936 const _CharT __fill = __os.fill();
1937 const std::streamsize __precision = __os.precision();
1938 const _CharT __space = __os.widen(' ');
1939 __os.flags(__ios_base::scientific | __ios_base::left);
1941 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1943 __os << __x.a() << __space << __x.b();
1945 __os.flags(__flags);
1947 __os.precision(__precision);
1951 template<typename _RealType, typename _CharT, typename _Traits>
1952 std::basic_istream<_CharT, _Traits>&
1953 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1954 extreme_value_distribution<_RealType>& __x)
1956 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1957 typedef typename __istream_type::ios_base __ios_base;
1959 const typename __ios_base::fmtflags __flags = __is.flags();
1960 __is.flags(__ios_base::dec | __ios_base::skipws);
1964 __x.param(typename extreme_value_distribution<_RealType>::
1965 param_type(__a, __b));
1967 __is.flags(__flags);
1972 template<typename _IntType>
1974 discrete_distribution<_IntType>::param_type::
1977 if (_M_prob.size() < 2)
1980 _M_prob.push_back(1.0);
1984 const double __sum = std::accumulate(_M_prob.begin(),
1985 _M_prob.end(), 0.0);
1986 // Now normalize the probabilites.
1987 std::transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
1988 std::bind2nd(std::divides<double>(), __sum));
1989 // Accumulate partial sums.
1990 _M_cp.reserve(_M_prob.size());
1991 std::partial_sum(_M_prob.begin(), _M_prob.end(),
1992 std::back_inserter(_M_cp));
1993 // Make sure the last cumulative probability is one.
1994 _M_cp[_M_cp.size() - 1] = 1.0;
1997 template<typename _IntType>
1998 template<typename _Func>
1999 discrete_distribution<_IntType>::param_type::
2000 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2001 : _M_prob(), _M_cp()
2003 const size_t __n = __nw == 0 ? 1 : __nw;
2004 const double __delta = (__xmax - __xmin) / __n;
2006 _M_prob.reserve(__n);
2007 for (size_t __k = 0; __k < __nw; ++__k)
2008 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2013 template<typename _IntType>
2014 template<typename _UniformRandomNumberGenerator>
2015 typename discrete_distribution<_IntType>::result_type
2016 discrete_distribution<_IntType>::
2017 operator()(_UniformRandomNumberGenerator& __urng,
2018 const param_type& __param)
2020 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2023 const double __p = __aurng();
2024 auto __pos = std::lower_bound(__param._M_cp.begin(),
2025 __param._M_cp.end(), __p);
2027 return __pos - __param._M_cp.begin();
2030 template<typename _IntType, typename _CharT, typename _Traits>
2031 std::basic_ostream<_CharT, _Traits>&
2032 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2033 const discrete_distribution<_IntType>& __x)
2035 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2036 typedef typename __ostream_type::ios_base __ios_base;
2038 const typename __ios_base::fmtflags __flags = __os.flags();
2039 const _CharT __fill = __os.fill();
2040 const std::streamsize __precision = __os.precision();
2041 const _CharT __space = __os.widen(' ');
2042 __os.flags(__ios_base::scientific | __ios_base::left);
2044 __os.precision(std::numeric_limits<double>::digits10 + 1);
2046 std::vector<double> __prob = __x.probabilities();
2047 __os << __prob.size();
2048 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2049 __os << __space << *__dit;
2051 __os.flags(__flags);
2053 __os.precision(__precision);
2057 template<typename _IntType, typename _CharT, typename _Traits>
2058 std::basic_istream<_CharT, _Traits>&
2059 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2060 discrete_distribution<_IntType>& __x)
2062 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2063 typedef typename __istream_type::ios_base __ios_base;
2065 const typename __ios_base::fmtflags __flags = __is.flags();
2066 __is.flags(__ios_base::dec | __ios_base::skipws);
2071 std::vector<double> __prob_vec;
2072 __prob_vec.reserve(__n);
2073 for (; __n != 0; --__n)
2077 __prob_vec.push_back(__prob);
2080 __x.param(typename discrete_distribution<_IntType>::
2081 param_type(__prob_vec.begin(), __prob_vec.end()));
2083 __is.flags(__flags);
2088 template<typename _RealType>
2090 piecewise_constant_distribution<_RealType>::param_type::
2093 if (_M_int.size() < 2)
2097 _M_int.push_back(_RealType(0));
2098 _M_int.push_back(_RealType(1));
2101 _M_den.push_back(1.0);
2106 const double __sum = std::accumulate(_M_den.begin(),
2109 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2110 std::bind2nd(std::divides<double>(), __sum));
2112 _M_cp.reserve(_M_den.size());
2113 std::partial_sum(_M_den.begin(), _M_den.end(),
2114 std::back_inserter(_M_cp));
2116 // Make sure the last cumulative probability is one.
2117 _M_cp[_M_cp.size() - 1] = 1.0;
2119 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2120 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2123 template<typename _RealType>
2124 template<typename _InputIteratorB, typename _InputIteratorW>
2125 piecewise_constant_distribution<_RealType>::param_type::
2126 param_type(_InputIteratorB __bbegin,
2127 _InputIteratorB __bend,
2128 _InputIteratorW __wbegin)
2129 : _M_int(), _M_den(), _M_cp()
2131 if (__bbegin != __bend)
2135 _M_int.push_back(*__bbegin);
2137 if (__bbegin == __bend)
2140 _M_den.push_back(*__wbegin);
2148 template<typename _RealType>
2149 template<typename _Func>
2150 piecewise_constant_distribution<_RealType>::param_type::
2151 param_type(initializer_list<_RealType> __bl, _Func __fw)
2152 : _M_int(), _M_den(), _M_cp()
2154 _M_int.reserve(__bl.size());
2155 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2156 _M_int.push_back(*__biter);
2158 _M_den.reserve(_M_int.size() - 1);
2159 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2160 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2165 template<typename _RealType>
2166 template<typename _Func>
2167 piecewise_constant_distribution<_RealType>::param_type::
2168 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2169 : _M_int(), _M_den(), _M_cp()
2171 const size_t __n = __nw == 0 ? 1 : __nw;
2172 const _RealType __delta = (__xmax - __xmin) / __n;
2174 _M_int.reserve(__n + 1);
2175 for (size_t __k = 0; __k <= __nw; ++__k)
2176 _M_int.push_back(__xmin + __k * __delta);
2178 _M_den.reserve(__n);
2179 for (size_t __k = 0; __k < __nw; ++__k)
2180 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2185 template<typename _RealType>
2186 template<typename _UniformRandomNumberGenerator>
2187 typename piecewise_constant_distribution<_RealType>::result_type
2188 piecewise_constant_distribution<_RealType>::
2189 operator()(_UniformRandomNumberGenerator& __urng,
2190 const param_type& __param)
2192 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2195 const double __p = __aurng();
2196 auto __pos = std::lower_bound(__param._M_cp.begin(),
2197 __param._M_cp.end(), __p);
2198 const size_t __i = __pos - __param._M_cp.begin();
2200 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2202 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2205 template<typename _RealType, typename _CharT, typename _Traits>
2206 std::basic_ostream<_CharT, _Traits>&
2207 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2208 const piecewise_constant_distribution<_RealType>& __x)
2210 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2211 typedef typename __ostream_type::ios_base __ios_base;
2213 const typename __ios_base::fmtflags __flags = __os.flags();
2214 const _CharT __fill = __os.fill();
2215 const std::streamsize __precision = __os.precision();
2216 const _CharT __space = __os.widen(' ');
2217 __os.flags(__ios_base::scientific | __ios_base::left);
2219 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2221 std::vector<_RealType> __int = __x.intervals();
2222 __os << __int.size() - 1;
2224 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2225 __os << __space << *__xit;
2227 std::vector<double> __den = __x.densities();
2228 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2229 __os << __space << *__dit;
2231 __os.flags(__flags);
2233 __os.precision(__precision);
2237 template<typename _RealType, typename _CharT, typename _Traits>
2238 std::basic_istream<_CharT, _Traits>&
2239 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2240 piecewise_constant_distribution<_RealType>& __x)
2242 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2243 typedef typename __istream_type::ios_base __ios_base;
2245 const typename __ios_base::fmtflags __flags = __is.flags();
2246 __is.flags(__ios_base::dec | __ios_base::skipws);
2251 std::vector<_RealType> __int_vec;
2252 __int_vec.reserve(__n + 1);
2253 for (size_t __i = 0; __i <= __n; ++__i)
2257 __int_vec.push_back(__int);
2260 std::vector<double> __den_vec;
2261 __den_vec.reserve(__n);
2262 for (size_t __i = 0; __i < __n; ++__i)
2266 __den_vec.push_back(__den);
2269 __x.param(typename piecewise_constant_distribution<_RealType>::
2270 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2272 __is.flags(__flags);
2277 template<typename _RealType>
2279 piecewise_linear_distribution<_RealType>::param_type::
2282 if (_M_int.size() < 2)
2286 _M_int.push_back(_RealType(0));
2287 _M_int.push_back(_RealType(1));
2291 _M_den.push_back(1.0);
2292 _M_den.push_back(1.0);
2298 _M_cp.reserve(_M_int.size() - 1);
2299 _M_m.reserve(_M_int.size() - 1);
2300 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2302 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2303 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2304 _M_cp.push_back(__sum);
2305 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2308 // Now normalize the densities...
2309 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2310 std::bind2nd(std::divides<double>(), __sum));
2311 // ... and partial sums...
2312 std::transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2313 std::bind2nd(std::divides<double>(), __sum));
2315 std::transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2316 std::bind2nd(std::divides<double>(), __sum));
2317 // Make sure the last cumulative probablility is one.
2318 _M_cp[_M_cp.size() - 1] = 1.0;
2321 template<typename _RealType>
2322 template<typename _InputIteratorB, typename _InputIteratorW>
2323 piecewise_linear_distribution<_RealType>::param_type::
2324 param_type(_InputIteratorB __bbegin,
2325 _InputIteratorB __bend,
2326 _InputIteratorW __wbegin)
2327 : _M_int(), _M_den(), _M_cp(), _M_m()
2329 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2331 _M_int.push_back(*__bbegin);
2332 _M_den.push_back(*__wbegin);
2338 template<typename _RealType>
2339 template<typename _Func>
2340 piecewise_linear_distribution<_RealType>::param_type::
2341 param_type(initializer_list<_RealType> __bl, _Func __fw)
2342 : _M_int(), _M_den(), _M_cp(), _M_m()
2344 _M_int.reserve(__bl.size());
2345 _M_den.reserve(__bl.size());
2346 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2348 _M_int.push_back(*__biter);
2349 _M_den.push_back(__fw(*__biter));
2355 template<typename _RealType>
2356 template<typename _Func>
2357 piecewise_linear_distribution<_RealType>::param_type::
2358 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2359 : _M_int(), _M_den(), _M_cp(), _M_m()
2361 const size_t __n = __nw == 0 ? 1 : __nw;
2362 const _RealType __delta = (__xmax - __xmin) / __n;
2364 _M_int.reserve(__n + 1);
2365 _M_den.reserve(__n + 1);
2366 for (size_t __k = 0; __k <= __nw; ++__k)
2368 _M_int.push_back(__xmin + __k * __delta);
2369 _M_den.push_back(__fw(_M_int[__k] + __delta));
2375 template<typename _RealType>
2376 template<typename _UniformRandomNumberGenerator>
2377 typename piecewise_linear_distribution<_RealType>::result_type
2378 piecewise_linear_distribution<_RealType>::
2379 operator()(_UniformRandomNumberGenerator& __urng,
2380 const param_type& __param)
2382 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2385 const double __p = __aurng();
2386 auto __pos = std::lower_bound(__param._M_cp.begin(),
2387 __param._M_cp.end(), __p);
2388 const size_t __i = __pos - __param._M_cp.begin();
2390 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2392 const double __a = 0.5 * __param._M_m[__i];
2393 const double __b = __param._M_den[__i];
2394 const double __cm = __p - __pref;
2396 _RealType __x = __param._M_int[__i];
2401 const double __d = __b * __b + 4.0 * __a * __cm;
2402 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2408 template<typename _RealType, typename _CharT, typename _Traits>
2409 std::basic_ostream<_CharT, _Traits>&
2410 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2411 const piecewise_linear_distribution<_RealType>& __x)
2413 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2414 typedef typename __ostream_type::ios_base __ios_base;
2416 const typename __ios_base::fmtflags __flags = __os.flags();
2417 const _CharT __fill = __os.fill();
2418 const std::streamsize __precision = __os.precision();
2419 const _CharT __space = __os.widen(' ');
2420 __os.flags(__ios_base::scientific | __ios_base::left);
2422 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2424 std::vector<_RealType> __int = __x.intervals();
2425 __os << __int.size() - 1;
2427 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2428 __os << __space << *__xit;
2430 std::vector<double> __den = __x.densities();
2431 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2432 __os << __space << *__dit;
2434 __os.flags(__flags);
2436 __os.precision(__precision);
2440 template<typename _RealType, typename _CharT, typename _Traits>
2441 std::basic_istream<_CharT, _Traits>&
2442 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2443 piecewise_linear_distribution<_RealType>& __x)
2445 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2446 typedef typename __istream_type::ios_base __ios_base;
2448 const typename __ios_base::fmtflags __flags = __is.flags();
2449 __is.flags(__ios_base::dec | __ios_base::skipws);
2454 std::vector<_RealType> __int_vec;
2455 __int_vec.reserve(__n + 1);
2456 for (size_t __i = 0; __i <= __n; ++__i)
2460 __int_vec.push_back(__int);
2463 std::vector<double> __den_vec;
2464 __den_vec.reserve(__n + 1);
2465 for (size_t __i = 0; __i <= __n; ++__i)
2469 __den_vec.push_back(__den);
2472 __x.param(typename piecewise_linear_distribution<_RealType>::
2473 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2475 __is.flags(__flags);
2480 template<typename _IntType>
2481 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2483 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2484 _M_v.push_back(__detail::__mod<result_type,
2485 __detail::_Shift<result_type, 32>::__value>(*__iter));
2488 template<typename _InputIterator>
2489 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2491 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2492 _M_v.push_back(__detail::__mod<result_type,
2493 __detail::_Shift<result_type, 32>::__value>(*__iter));
2496 template<typename _RandomAccessIterator>
2498 seed_seq::generate(_RandomAccessIterator __begin,
2499 _RandomAccessIterator __end)
2501 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2504 if (__begin == __end)
2507 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2509 const size_t __n = __end - __begin;
2510 const size_t __s = _M_v.size();
2511 const size_t __t = (__n >= 623) ? 11
2516 const size_t __p = (__n - __t) / 2;
2517 const size_t __q = __p + __t;
2518 const size_t __m = std::max(__s + 1, __n);
2520 for (size_t __k = 0; __k < __m; ++__k)
2522 _Type __arg = (__begin[__k % __n]
2523 ^ __begin[(__k + __p) % __n]
2524 ^ __begin[(__k - 1) % __n]);
2525 _Type __r1 = __arg ^ (__arg << 27);
2526 __r1 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
2527 1664525u, 0u>(__r1);
2531 else if (__k <= __s)
2532 __r2 += __k % __n + _M_v[__k - 1];
2535 __r2 = __detail::__mod<_Type,
2536 __detail::_Shift<_Type, 32>::__value>(__r2);
2537 __begin[(__k + __p) % __n] += __r1;
2538 __begin[(__k + __q) % __n] += __r2;
2539 __begin[__k % __n] = __r2;
2542 for (size_t __k = __m; __k < __m + __n; ++__k)
2544 _Type __arg = (__begin[__k % __n]
2545 + __begin[(__k + __p) % __n]
2546 + __begin[(__k - 1) % __n]);
2547 _Type __r3 = __arg ^ (__arg << 27);
2548 __r3 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
2549 1566083941u, 0u>(__r3);
2550 _Type __r4 = __r3 - __k % __n;
2551 __r4 = __detail::__mod<_Type,
2552 __detail::_Shift<_Type, 32>::__value>(__r4);
2553 __begin[(__k + __p) % __n] ^= __r4;
2554 __begin[(__k + __q) % __n] ^= __r3;
2555 __begin[__k % __n] = __r4;
2559 template<typename _RealType, size_t __bits,
2560 typename _UniformRandomNumberGenerator>
2562 generate_canonical(_UniformRandomNumberGenerator& __urng)
2565 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2567 const long double __r = static_cast<long double>(__urng.max())
2568 - static_cast<long double>(__urng.min()) + 1.0L;
2569 const size_t __log2r = std::log(__r) / std::log(2.0L);
2570 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2571 _RealType __sum = _RealType(0);
2572 _RealType __tmp = _RealType(1);
2573 for (; __k != 0; --__k)
2575 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2578 return __sum / __tmp;