1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 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 * Do not attempt to use it directly. @headername{random}
33 #include <numeric> // std::accumulate and std::partial_sum
35 namespace std _GLIBCXX_VISIBILITY(default)
38 * (Further) implementation-space details.
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 // General case for x = (ax + c) mod m -- use Schrage's algorithm to
45 // avoid integer overflow.
47 // Because a and c are compile-time integral constants the compiler
48 // kindly elides any unreachable paths.
50 // Preconditions: a > 0, m > 0.
52 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
62 static const _Tp __q = __m / __a;
63 static const _Tp __r = __m % __a;
65 _Tp __t1 = __a * (__x % __q);
66 _Tp __t2 = __r * (__x / __q);
70 __x = __m - __t2 + __t1;
75 const _Tp __d = __m - __x;
85 // Special case for m == 0 -- use unsigned integer overflow as modulo
87 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
88 struct _Mod<_Tp, __m, __a, __c, true>
92 { return __a * __x + __c; }
95 template<typename _InputIterator, typename _OutputIterator,
96 typename _UnaryOperation>
98 __transform(_InputIterator __first, _InputIterator __last,
99 _OutputIterator __result, _UnaryOperation __unary_op)
101 for (; __first != __last; ++__first, ++__result)
102 *__result = __unary_op(*__first);
106 _GLIBCXX_END_NAMESPACE_VERSION
107 } // namespace __detail
109 _GLIBCXX_BEGIN_NAMESPACE_VERSION
111 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
113 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
115 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
117 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
119 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
121 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
123 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
125 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
128 * Seeds the LCR with integral value @p __s, adjusted so that the
129 * ring identity is never a member of the convergence set.
131 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
133 linear_congruential_engine<_UIntType, __a, __c, __m>::
134 seed(result_type __s)
136 if ((__detail::__mod<_UIntType, __m>(__c) == 0)
137 && (__detail::__mod<_UIntType, __m>(__s) == 0))
140 _M_x = __detail::__mod<_UIntType, __m>(__s);
144 * Seeds the LCR engine with a value generated by @p __q.
146 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
147 template<typename _Sseq>
148 typename std::enable_if<std::is_class<_Sseq>::value>::type
149 linear_congruential_engine<_UIntType, __a, __c, __m>::
152 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
154 const _UIntType __k = (__k0 + 31) / 32;
155 uint_least32_t __arr[__k + 3];
156 __q.generate(__arr + 0, __arr + __k + 3);
157 _UIntType __factor = 1u;
158 _UIntType __sum = 0u;
159 for (size_t __j = 0; __j < __k; ++__j)
161 __sum += __arr[__j + 3] * __factor;
162 __factor *= __detail::_Shift<_UIntType, 32>::__value;
167 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
168 typename _CharT, typename _Traits>
169 std::basic_ostream<_CharT, _Traits>&
170 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
171 const linear_congruential_engine<_UIntType,
172 __a, __c, __m>& __lcr)
174 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
175 typedef typename __ostream_type::ios_base __ios_base;
177 const typename __ios_base::fmtflags __flags = __os.flags();
178 const _CharT __fill = __os.fill();
179 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
180 __os.fill(__os.widen(' '));
189 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
190 typename _CharT, typename _Traits>
191 std::basic_istream<_CharT, _Traits>&
192 operator>>(std::basic_istream<_CharT, _Traits>& __is,
193 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
195 typedef std::basic_istream<_CharT, _Traits> __istream_type;
196 typedef typename __istream_type::ios_base __ios_base;
198 const typename __ios_base::fmtflags __flags = __is.flags();
199 __is.flags(__ios_base::dec);
208 template<typename _UIntType,
209 size_t __w, size_t __n, size_t __m, size_t __r,
210 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
211 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
214 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
215 __s, __b, __t, __c, __l, __f>::word_size;
217 template<typename _UIntType,
218 size_t __w, size_t __n, size_t __m, size_t __r,
219 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
220 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
223 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
224 __s, __b, __t, __c, __l, __f>::state_size;
226 template<typename _UIntType,
227 size_t __w, size_t __n, size_t __m, size_t __r,
228 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
229 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
232 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
233 __s, __b, __t, __c, __l, __f>::shift_size;
235 template<typename _UIntType,
236 size_t __w, size_t __n, size_t __m, size_t __r,
237 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
238 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
241 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
242 __s, __b, __t, __c, __l, __f>::mask_bits;
244 template<typename _UIntType,
245 size_t __w, size_t __n, size_t __m, size_t __r,
246 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
247 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
250 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
251 __s, __b, __t, __c, __l, __f>::xor_mask;
253 template<typename _UIntType,
254 size_t __w, size_t __n, size_t __m, size_t __r,
255 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
256 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
259 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
260 __s, __b, __t, __c, __l, __f>::tempering_u;
262 template<typename _UIntType,
263 size_t __w, size_t __n, size_t __m, size_t __r,
264 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
265 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
268 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
269 __s, __b, __t, __c, __l, __f>::tempering_d;
271 template<typename _UIntType,
272 size_t __w, size_t __n, size_t __m, size_t __r,
273 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
274 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
277 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
278 __s, __b, __t, __c, __l, __f>::tempering_s;
280 template<typename _UIntType,
281 size_t __w, size_t __n, size_t __m, size_t __r,
282 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
283 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
286 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
287 __s, __b, __t, __c, __l, __f>::tempering_b;
289 template<typename _UIntType,
290 size_t __w, size_t __n, size_t __m, size_t __r,
291 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
292 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
295 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
296 __s, __b, __t, __c, __l, __f>::tempering_t;
298 template<typename _UIntType,
299 size_t __w, size_t __n, size_t __m, size_t __r,
300 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
301 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
304 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
305 __s, __b, __t, __c, __l, __f>::tempering_c;
307 template<typename _UIntType,
308 size_t __w, size_t __n, size_t __m, size_t __r,
309 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
310 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
313 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
314 __s, __b, __t, __c, __l, __f>::tempering_l;
316 template<typename _UIntType,
317 size_t __w, size_t __n, size_t __m, size_t __r,
318 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
319 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
322 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
323 __s, __b, __t, __c, __l, __f>::
324 initialization_multiplier;
326 template<typename _UIntType,
327 size_t __w, size_t __n, size_t __m, size_t __r,
328 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
329 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
332 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
333 __s, __b, __t, __c, __l, __f>::default_seed;
335 template<typename _UIntType,
336 size_t __w, size_t __n, size_t __m, size_t __r,
337 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
338 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
341 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
342 __s, __b, __t, __c, __l, __f>::
343 seed(result_type __sd)
345 _M_x[0] = __detail::__mod<_UIntType,
346 __detail::_Shift<_UIntType, __w>::__value>(__sd);
348 for (size_t __i = 1; __i < state_size; ++__i)
350 _UIntType __x = _M_x[__i - 1];
351 __x ^= __x >> (__w - 2);
353 __x += __detail::__mod<_UIntType, __n>(__i);
354 _M_x[__i] = __detail::__mod<_UIntType,
355 __detail::_Shift<_UIntType, __w>::__value>(__x);
360 template<typename _UIntType,
361 size_t __w, size_t __n, size_t __m, size_t __r,
362 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
363 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
365 template<typename _Sseq>
366 typename std::enable_if<std::is_class<_Sseq>::value>::type
367 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
368 __s, __b, __t, __c, __l, __f>::
371 const _UIntType __upper_mask = (~_UIntType()) << __r;
372 const size_t __k = (__w + 31) / 32;
373 uint_least32_t __arr[__n * __k];
374 __q.generate(__arr + 0, __arr + __n * __k);
377 for (size_t __i = 0; __i < state_size; ++__i)
379 _UIntType __factor = 1u;
380 _UIntType __sum = 0u;
381 for (size_t __j = 0; __j < __k; ++__j)
383 __sum += __arr[__k * __i + __j] * __factor;
384 __factor *= __detail::_Shift<_UIntType, 32>::__value;
386 _M_x[__i] = __detail::__mod<_UIntType,
387 __detail::_Shift<_UIntType, __w>::__value>(__sum);
393 if ((_M_x[0] & __upper_mask) != 0u)
396 else if (_M_x[__i] != 0u)
401 _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
404 template<typename _UIntType, size_t __w,
405 size_t __n, size_t __m, size_t __r,
406 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
407 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
410 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
411 __s, __b, __t, __c, __l, __f>::result_type
412 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
413 __s, __b, __t, __c, __l, __f>::
416 // Reload the vector - cost is O(n) amortized over n calls.
417 if (_M_p >= state_size)
419 const _UIntType __upper_mask = (~_UIntType()) << __r;
420 const _UIntType __lower_mask = ~__upper_mask;
422 for (size_t __k = 0; __k < (__n - __m); ++__k)
424 _UIntType __y = ((_M_x[__k] & __upper_mask)
425 | (_M_x[__k + 1] & __lower_mask));
426 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
427 ^ ((__y & 0x01) ? __a : 0));
430 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
432 _UIntType __y = ((_M_x[__k] & __upper_mask)
433 | (_M_x[__k + 1] & __lower_mask));
434 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
435 ^ ((__y & 0x01) ? __a : 0));
438 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
439 | (_M_x[0] & __lower_mask));
440 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
441 ^ ((__y & 0x01) ? __a : 0));
445 // Calculate o(x(i)).
446 result_type __z = _M_x[_M_p++];
447 __z ^= (__z >> __u) & __d;
448 __z ^= (__z << __s) & __b;
449 __z ^= (__z << __t) & __c;
455 template<typename _UIntType, size_t __w,
456 size_t __n, size_t __m, size_t __r,
457 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
458 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
459 _UIntType __f, typename _CharT, typename _Traits>
460 std::basic_ostream<_CharT, _Traits>&
461 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
462 const mersenne_twister_engine<_UIntType, __w, __n, __m,
463 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
465 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
466 typedef typename __ostream_type::ios_base __ios_base;
468 const typename __ios_base::fmtflags __flags = __os.flags();
469 const _CharT __fill = __os.fill();
470 const _CharT __space = __os.widen(' ');
471 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
474 for (size_t __i = 0; __i < __n; ++__i)
475 __os << __x._M_x[__i] << __space;
483 template<typename _UIntType, size_t __w,
484 size_t __n, size_t __m, size_t __r,
485 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
486 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
487 _UIntType __f, typename _CharT, typename _Traits>
488 std::basic_istream<_CharT, _Traits>&
489 operator>>(std::basic_istream<_CharT, _Traits>& __is,
490 mersenne_twister_engine<_UIntType, __w, __n, __m,
491 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
493 typedef std::basic_istream<_CharT, _Traits> __istream_type;
494 typedef typename __istream_type::ios_base __ios_base;
496 const typename __ios_base::fmtflags __flags = __is.flags();
497 __is.flags(__ios_base::dec | __ios_base::skipws);
499 for (size_t __i = 0; __i < __n; ++__i)
500 __is >> __x._M_x[__i];
508 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
510 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
512 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
514 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
516 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
518 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
520 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
522 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
524 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
526 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
527 seed(result_type __value)
529 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
530 __lcg(__value == 0u ? default_seed : __value);
532 const size_t __n = (__w + 31) / 32;
534 for (size_t __i = 0; __i < long_lag; ++__i)
536 _UIntType __sum = 0u;
537 _UIntType __factor = 1u;
538 for (size_t __j = 0; __j < __n; ++__j)
540 __sum += __detail::__mod<uint_least32_t,
541 __detail::_Shift<uint_least32_t, 32>::__value>
542 (__lcg()) * __factor;
543 __factor *= __detail::_Shift<_UIntType, 32>::__value;
545 _M_x[__i] = __detail::__mod<_UIntType,
546 __detail::_Shift<_UIntType, __w>::__value>(__sum);
548 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
552 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
553 template<typename _Sseq>
554 typename std::enable_if<std::is_class<_Sseq>::value>::type
555 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
558 const size_t __k = (__w + 31) / 32;
559 uint_least32_t __arr[__r * __k];
560 __q.generate(__arr + 0, __arr + __r * __k);
562 for (size_t __i = 0; __i < long_lag; ++__i)
564 _UIntType __sum = 0u;
565 _UIntType __factor = 1u;
566 for (size_t __j = 0; __j < __k; ++__j)
568 __sum += __arr[__k * __i + __j] * __factor;
569 __factor *= __detail::_Shift<_UIntType, 32>::__value;
571 _M_x[__i] = __detail::__mod<_UIntType,
572 __detail::_Shift<_UIntType, __w>::__value>(__sum);
574 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
578 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
579 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
581 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
584 // Derive short lag index from current index.
585 long __ps = _M_p - short_lag;
589 // Calculate new x(i) without overflow or division.
590 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
593 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
595 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
600 __xi = (__detail::_Shift<_UIntType, __w>::__value
601 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
606 // Adjust current index to loop around in ring buffer.
607 if (++_M_p >= long_lag)
613 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
614 typename _CharT, typename _Traits>
615 std::basic_ostream<_CharT, _Traits>&
616 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
617 const subtract_with_carry_engine<_UIntType,
620 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
621 typedef typename __ostream_type::ios_base __ios_base;
623 const typename __ios_base::fmtflags __flags = __os.flags();
624 const _CharT __fill = __os.fill();
625 const _CharT __space = __os.widen(' ');
626 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
629 for (size_t __i = 0; __i < __r; ++__i)
630 __os << __x._M_x[__i] << __space;
631 __os << __x._M_carry << __space << __x._M_p;
638 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
639 typename _CharT, typename _Traits>
640 std::basic_istream<_CharT, _Traits>&
641 operator>>(std::basic_istream<_CharT, _Traits>& __is,
642 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
644 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
645 typedef typename __istream_type::ios_base __ios_base;
647 const typename __ios_base::fmtflags __flags = __is.flags();
648 __is.flags(__ios_base::dec | __ios_base::skipws);
650 for (size_t __i = 0; __i < __r; ++__i)
651 __is >> __x._M_x[__i];
652 __is >> __x._M_carry;
660 template<typename _RandomNumberEngine, size_t __p, size_t __r>
662 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
664 template<typename _RandomNumberEngine, size_t __p, size_t __r>
666 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
668 template<typename _RandomNumberEngine, size_t __p, size_t __r>
669 typename discard_block_engine<_RandomNumberEngine,
670 __p, __r>::result_type
671 discard_block_engine<_RandomNumberEngine, __p, __r>::
674 if (_M_n >= used_block)
676 _M_b.discard(block_size - _M_n);
683 template<typename _RandomNumberEngine, size_t __p, size_t __r,
684 typename _CharT, typename _Traits>
685 std::basic_ostream<_CharT, _Traits>&
686 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
687 const discard_block_engine<_RandomNumberEngine,
690 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
691 typedef typename __ostream_type::ios_base __ios_base;
693 const typename __ios_base::fmtflags __flags = __os.flags();
694 const _CharT __fill = __os.fill();
695 const _CharT __space = __os.widen(' ');
696 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
699 __os << __x.base() << __space << __x._M_n;
706 template<typename _RandomNumberEngine, size_t __p, size_t __r,
707 typename _CharT, typename _Traits>
708 std::basic_istream<_CharT, _Traits>&
709 operator>>(std::basic_istream<_CharT, _Traits>& __is,
710 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
712 typedef std::basic_istream<_CharT, _Traits> __istream_type;
713 typedef typename __istream_type::ios_base __ios_base;
715 const typename __ios_base::fmtflags __flags = __is.flags();
716 __is.flags(__ios_base::dec | __ios_base::skipws);
718 __is >> __x._M_b >> __x._M_n;
725 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
726 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
728 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
731 const long double __r = static_cast<long double>(_M_b.max())
732 - static_cast<long double>(_M_b.min()) + 1.0L;
733 const result_type __m = std::log(__r) / std::log(2.0L);
734 result_type __n, __n0, __y0, __y1, __s0, __s1;
735 for (size_t __i = 0; __i < 2; ++__i)
737 __n = (__w + __m - 1) / __m + __i;
738 __n0 = __n - __w % __n;
739 const result_type __w0 = __w / __n;
740 const result_type __w1 = __w0 + 1;
741 __s0 = result_type(1) << __w0;
742 __s1 = result_type(1) << __w1;
743 __y0 = __s0 * (__r / __s0);
744 __y1 = __s1 * (__r / __s1);
745 if (__r - __y0 <= __y0 / __n)
749 result_type __sum = 0;
750 for (size_t __k = 0; __k < __n0; ++__k)
754 __u = _M_b() - _M_b.min();
756 __sum = __s0 * __sum + __u % __s0;
758 for (size_t __k = __n0; __k < __n; ++__k)
762 __u = _M_b() - _M_b.min();
764 __sum = __s1 * __sum + __u % __s1;
770 template<typename _RandomNumberEngine, size_t __k>
772 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
774 template<typename _RandomNumberEngine, size_t __k>
775 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
776 shuffle_order_engine<_RandomNumberEngine, __k>::
779 size_t __j = __k * ((_M_y - _M_b.min())
780 / (_M_b.max() - _M_b.min() + 1.0L));
787 template<typename _RandomNumberEngine, size_t __k,
788 typename _CharT, typename _Traits>
789 std::basic_ostream<_CharT, _Traits>&
790 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
791 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
793 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
794 typedef typename __ostream_type::ios_base __ios_base;
796 const typename __ios_base::fmtflags __flags = __os.flags();
797 const _CharT __fill = __os.fill();
798 const _CharT __space = __os.widen(' ');
799 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
803 for (size_t __i = 0; __i < __k; ++__i)
804 __os << __space << __x._M_v[__i];
805 __os << __space << __x._M_y;
812 template<typename _RandomNumberEngine, size_t __k,
813 typename _CharT, typename _Traits>
814 std::basic_istream<_CharT, _Traits>&
815 operator>>(std::basic_istream<_CharT, _Traits>& __is,
816 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
818 typedef std::basic_istream<_CharT, _Traits> __istream_type;
819 typedef typename __istream_type::ios_base __ios_base;
821 const typename __ios_base::fmtflags __flags = __is.flags();
822 __is.flags(__ios_base::dec | __ios_base::skipws);
825 for (size_t __i = 0; __i < __k; ++__i)
826 __is >> __x._M_v[__i];
834 template<typename _IntType>
835 template<typename _UniformRandomNumberGenerator>
836 typename uniform_int_distribution<_IntType>::result_type
837 uniform_int_distribution<_IntType>::
838 operator()(_UniformRandomNumberGenerator& __urng,
839 const param_type& __param)
841 typedef typename std::make_unsigned<typename
842 _UniformRandomNumberGenerator::result_type>::type __urngtype;
843 typedef typename std::make_unsigned<result_type>::type __utype;
844 typedef typename std::conditional<(sizeof(__urngtype)
846 __urngtype, __utype>::type __uctype;
848 const __uctype __urngmin = __urng.min();
849 const __uctype __urngmax = __urng.max();
850 const __uctype __urngrange = __urngmax - __urngmin;
851 const __uctype __urange
852 = __uctype(__param.b()) - __uctype(__param.a());
856 if (__urngrange > __urange)
859 const __uctype __uerange = __urange + 1; // __urange can be zero
860 const __uctype __scaling = __urngrange / __uerange;
861 const __uctype __past = __uerange * __scaling;
863 __ret = __uctype(__urng()) - __urngmin;
864 while (__ret >= __past);
867 else if (__urngrange < __urange)
871 Note that every value in [0, urange]
872 can be written uniquely as
874 (urngrange + 1) * high + low
878 high in [0, urange / (urngrange + 1)]
882 low in [0, urngrange].
884 __uctype __tmp; // wraparound control
887 const __uctype __uerngrange = __urngrange + 1;
888 __tmp = (__uerngrange * operator()
889 (__urng, param_type(0, __urange / __uerngrange)));
890 __ret = __tmp + (__uctype(__urng()) - __urngmin);
892 while (__ret > __urange || __ret < __tmp);
895 __ret = __uctype(__urng()) - __urngmin;
897 return __ret + __param.a();
900 template<typename _IntType, typename _CharT, typename _Traits>
901 std::basic_ostream<_CharT, _Traits>&
902 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
903 const uniform_int_distribution<_IntType>& __x)
905 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
906 typedef typename __ostream_type::ios_base __ios_base;
908 const typename __ios_base::fmtflags __flags = __os.flags();
909 const _CharT __fill = __os.fill();
910 const _CharT __space = __os.widen(' ');
911 __os.flags(__ios_base::scientific | __ios_base::left);
914 __os << __x.a() << __space << __x.b();
921 template<typename _IntType, typename _CharT, typename _Traits>
922 std::basic_istream<_CharT, _Traits>&
923 operator>>(std::basic_istream<_CharT, _Traits>& __is,
924 uniform_int_distribution<_IntType>& __x)
926 typedef std::basic_istream<_CharT, _Traits> __istream_type;
927 typedef typename __istream_type::ios_base __ios_base;
929 const typename __ios_base::fmtflags __flags = __is.flags();
930 __is.flags(__ios_base::dec | __ios_base::skipws);
934 __x.param(typename uniform_int_distribution<_IntType>::
935 param_type(__a, __b));
942 template<typename _RealType, typename _CharT, typename _Traits>
943 std::basic_ostream<_CharT, _Traits>&
944 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
945 const uniform_real_distribution<_RealType>& __x)
947 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
948 typedef typename __ostream_type::ios_base __ios_base;
950 const typename __ios_base::fmtflags __flags = __os.flags();
951 const _CharT __fill = __os.fill();
952 const std::streamsize __precision = __os.precision();
953 const _CharT __space = __os.widen(' ');
954 __os.flags(__ios_base::scientific | __ios_base::left);
956 __os.precision(std::numeric_limits<_RealType>::max_digits10);
958 __os << __x.a() << __space << __x.b();
962 __os.precision(__precision);
966 template<typename _RealType, typename _CharT, typename _Traits>
967 std::basic_istream<_CharT, _Traits>&
968 operator>>(std::basic_istream<_CharT, _Traits>& __is,
969 uniform_real_distribution<_RealType>& __x)
971 typedef std::basic_istream<_CharT, _Traits> __istream_type;
972 typedef typename __istream_type::ios_base __ios_base;
974 const typename __ios_base::fmtflags __flags = __is.flags();
975 __is.flags(__ios_base::skipws);
979 __x.param(typename uniform_real_distribution<_RealType>::
980 param_type(__a, __b));
987 template<typename _CharT, typename _Traits>
988 std::basic_ostream<_CharT, _Traits>&
989 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
990 const bernoulli_distribution& __x)
992 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
993 typedef typename __ostream_type::ios_base __ios_base;
995 const typename __ios_base::fmtflags __flags = __os.flags();
996 const _CharT __fill = __os.fill();
997 const std::streamsize __precision = __os.precision();
998 __os.flags(__ios_base::scientific | __ios_base::left);
999 __os.fill(__os.widen(' '));
1000 __os.precision(std::numeric_limits<double>::max_digits10);
1004 __os.flags(__flags);
1006 __os.precision(__precision);
1011 template<typename _IntType>
1012 template<typename _UniformRandomNumberGenerator>
1013 typename geometric_distribution<_IntType>::result_type
1014 geometric_distribution<_IntType>::
1015 operator()(_UniformRandomNumberGenerator& __urng,
1016 const param_type& __param)
1018 // About the epsilon thing see this thread:
1019 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1020 const double __naf =
1021 (1 - std::numeric_limits<double>::epsilon()) / 2;
1022 // The largest _RealType convertible to _IntType.
1023 const double __thr =
1024 std::numeric_limits<_IntType>::max() + __naf;
1025 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1030 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1031 while (__cand >= __thr);
1033 return result_type(__cand + __naf);
1036 template<typename _IntType,
1037 typename _CharT, typename _Traits>
1038 std::basic_ostream<_CharT, _Traits>&
1039 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1040 const geometric_distribution<_IntType>& __x)
1042 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1043 typedef typename __ostream_type::ios_base __ios_base;
1045 const typename __ios_base::fmtflags __flags = __os.flags();
1046 const _CharT __fill = __os.fill();
1047 const std::streamsize __precision = __os.precision();
1048 __os.flags(__ios_base::scientific | __ios_base::left);
1049 __os.fill(__os.widen(' '));
1050 __os.precision(std::numeric_limits<double>::max_digits10);
1054 __os.flags(__flags);
1056 __os.precision(__precision);
1060 template<typename _IntType,
1061 typename _CharT, typename _Traits>
1062 std::basic_istream<_CharT, _Traits>&
1063 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1064 geometric_distribution<_IntType>& __x)
1066 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1067 typedef typename __istream_type::ios_base __ios_base;
1069 const typename __ios_base::fmtflags __flags = __is.flags();
1070 __is.flags(__ios_base::skipws);
1074 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1076 __is.flags(__flags);
1080 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1081 template<typename _IntType>
1082 template<typename _UniformRandomNumberGenerator>
1083 typename negative_binomial_distribution<_IntType>::result_type
1084 negative_binomial_distribution<_IntType>::
1085 operator()(_UniformRandomNumberGenerator& __urng)
1087 const double __y = _M_gd(__urng);
1089 // XXX Is the constructor too slow?
1090 std::poisson_distribution<result_type> __poisson(__y);
1091 return __poisson(__urng);
1094 template<typename _IntType>
1095 template<typename _UniformRandomNumberGenerator>
1096 typename negative_binomial_distribution<_IntType>::result_type
1097 negative_binomial_distribution<_IntType>::
1098 operator()(_UniformRandomNumberGenerator& __urng,
1099 const param_type& __p)
1101 typedef typename std::gamma_distribution<result_type>::param_type
1105 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1107 std::poisson_distribution<result_type> __poisson(__y);
1108 return __poisson(__urng);
1111 template<typename _IntType, typename _CharT, typename _Traits>
1112 std::basic_ostream<_CharT, _Traits>&
1113 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1114 const negative_binomial_distribution<_IntType>& __x)
1116 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1117 typedef typename __ostream_type::ios_base __ios_base;
1119 const typename __ios_base::fmtflags __flags = __os.flags();
1120 const _CharT __fill = __os.fill();
1121 const std::streamsize __precision = __os.precision();
1122 const _CharT __space = __os.widen(' ');
1123 __os.flags(__ios_base::scientific | __ios_base::left);
1124 __os.fill(__os.widen(' '));
1125 __os.precision(std::numeric_limits<double>::max_digits10);
1127 __os << __x.k() << __space << __x.p()
1128 << __space << __x._M_gd;
1130 __os.flags(__flags);
1132 __os.precision(__precision);
1136 template<typename _IntType, typename _CharT, typename _Traits>
1137 std::basic_istream<_CharT, _Traits>&
1138 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1139 negative_binomial_distribution<_IntType>& __x)
1141 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1142 typedef typename __istream_type::ios_base __ios_base;
1144 const typename __ios_base::fmtflags __flags = __is.flags();
1145 __is.flags(__ios_base::skipws);
1149 __is >> __k >> __p >> __x._M_gd;
1150 __x.param(typename negative_binomial_distribution<_IntType>::
1151 param_type(__k, __p));
1153 __is.flags(__flags);
1158 template<typename _IntType>
1160 poisson_distribution<_IntType>::param_type::
1163 #if _GLIBCXX_USE_C99_MATH_TR1
1166 const double __m = std::floor(_M_mean);
1167 _M_lm_thr = std::log(_M_mean);
1168 _M_lfm = std::lgamma(__m + 1);
1169 _M_sm = std::sqrt(__m);
1171 const double __pi_4 = 0.7853981633974483096156608458198757L;
1172 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1174 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1175 const double __cx = 2 * __m + _M_d;
1176 _M_scx = std::sqrt(__cx / 2);
1179 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1180 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1185 _M_lm_thr = std::exp(-_M_mean);
1189 * A rejection algorithm when mean >= 12 and a simple method based
1190 * upon the multiplication of uniform random variates otherwise.
1191 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1195 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1196 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1198 template<typename _IntType>
1199 template<typename _UniformRandomNumberGenerator>
1200 typename poisson_distribution<_IntType>::result_type
1201 poisson_distribution<_IntType>::
1202 operator()(_UniformRandomNumberGenerator& __urng,
1203 const param_type& __param)
1205 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1207 #if _GLIBCXX_USE_C99_MATH_TR1
1208 if (__param.mean() >= 12)
1212 // See comments above...
1213 const double __naf =
1214 (1 - std::numeric_limits<double>::epsilon()) / 2;
1215 const double __thr =
1216 std::numeric_limits<_IntType>::max() + __naf;
1218 const double __m = std::floor(__param.mean());
1220 const double __spi_2 = 1.2533141373155002512078826424055226L;
1221 const double __c1 = __param._M_sm * __spi_2;
1222 const double __c2 = __param._M_c2b + __c1;
1223 const double __c3 = __c2 + 1;
1224 const double __c4 = __c3 + 1;
1226 const double __e178 = 1.0129030479320018583185514777512983L;
1227 const double __c5 = __c4 + __e178;
1228 const double __c = __param._M_cb + __c5;
1229 const double __2cx = 2 * (2 * __m + __param._M_d);
1231 bool __reject = true;
1234 const double __u = __c * __aurng();
1235 const double __e = -std::log(__aurng());
1241 const double __n = _M_nd(__urng);
1242 const double __y = -std::abs(__n) * __param._M_sm - 1;
1243 __x = std::floor(__y);
1244 __w = -__n * __n / 2;
1248 else if (__u <= __c2)
1250 const double __n = _M_nd(__urng);
1251 const double __y = 1 + std::abs(__n) * __param._M_scx;
1252 __x = std::ceil(__y);
1253 __w = __y * (2 - __y) * __param._M_1cx;
1254 if (__x > __param._M_d)
1257 else if (__u <= __c3)
1258 // NB: This case not in the book, nor in the Errata,
1259 // but should be ok...
1261 else if (__u <= __c4)
1263 else if (__u <= __c5)
1267 const double __v = -std::log(__aurng());
1268 const double __y = __param._M_d
1269 + __v * __2cx / __param._M_d;
1270 __x = std::ceil(__y);
1271 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1274 __reject = (__w - __e - __x * __param._M_lm_thr
1275 > __param._M_lfm - std::lgamma(__x + __m + 1));
1277 __reject |= __x + __m >= __thr;
1281 return result_type(__x + __m + __naf);
1287 double __prod = 1.0;
1291 __prod *= __aurng();
1294 while (__prod > __param._M_lm_thr);
1300 template<typename _IntType,
1301 typename _CharT, typename _Traits>
1302 std::basic_ostream<_CharT, _Traits>&
1303 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1304 const poisson_distribution<_IntType>& __x)
1306 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1307 typedef typename __ostream_type::ios_base __ios_base;
1309 const typename __ios_base::fmtflags __flags = __os.flags();
1310 const _CharT __fill = __os.fill();
1311 const std::streamsize __precision = __os.precision();
1312 const _CharT __space = __os.widen(' ');
1313 __os.flags(__ios_base::scientific | __ios_base::left);
1315 __os.precision(std::numeric_limits<double>::max_digits10);
1317 __os << __x.mean() << __space << __x._M_nd;
1319 __os.flags(__flags);
1321 __os.precision(__precision);
1325 template<typename _IntType,
1326 typename _CharT, typename _Traits>
1327 std::basic_istream<_CharT, _Traits>&
1328 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1329 poisson_distribution<_IntType>& __x)
1331 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1332 typedef typename __istream_type::ios_base __ios_base;
1334 const typename __ios_base::fmtflags __flags = __is.flags();
1335 __is.flags(__ios_base::skipws);
1338 __is >> __mean >> __x._M_nd;
1339 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1341 __is.flags(__flags);
1346 template<typename _IntType>
1348 binomial_distribution<_IntType>::param_type::
1351 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1355 #if _GLIBCXX_USE_C99_MATH_TR1
1356 if (_M_t * __p12 >= 8)
1359 const double __np = std::floor(_M_t * __p12);
1360 const double __pa = __np / _M_t;
1361 const double __1p = 1 - __pa;
1363 const double __pi_4 = 0.7853981633974483096156608458198757L;
1364 const double __d1x =
1365 std::sqrt(__np * __1p * std::log(32 * __np
1366 / (81 * __pi_4 * __1p)));
1367 _M_d1 = std::round(std::max(1.0, __d1x));
1368 const double __d2x =
1369 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1370 / (__pi_4 * __pa)));
1371 _M_d2 = std::round(std::max(1.0, __d2x));
1374 const double __spi_2 = 1.2533141373155002512078826424055226L;
1375 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1376 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1377 _M_c = 2 * _M_d1 / __np;
1378 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1379 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1380 const double __s1s = _M_s1 * _M_s1;
1381 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1383 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1384 const double __s2s = _M_s2 * _M_s2;
1385 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1386 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1387 _M_lf = (std::lgamma(__np + 1)
1388 + std::lgamma(_M_t - __np + 1));
1389 _M_lp1p = std::log(__pa / __1p);
1391 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1395 _M_q = -std::log(1 - __p12);
1398 template<typename _IntType>
1399 template<typename _UniformRandomNumberGenerator>
1400 typename binomial_distribution<_IntType>::result_type
1401 binomial_distribution<_IntType>::
1402 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1406 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1411 const double __e = -std::log(__aurng());
1412 __sum += __e / (__t - __x);
1415 while (__sum <= _M_param._M_q);
1421 * A rejection algorithm when t * p >= 8 and a simple waiting time
1422 * method - the second in the referenced book - otherwise.
1423 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1427 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1428 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1430 template<typename _IntType>
1431 template<typename _UniformRandomNumberGenerator>
1432 typename binomial_distribution<_IntType>::result_type
1433 binomial_distribution<_IntType>::
1434 operator()(_UniformRandomNumberGenerator& __urng,
1435 const param_type& __param)
1438 const _IntType __t = __param.t();
1439 const double __p = __param.p();
1440 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1441 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1444 #if _GLIBCXX_USE_C99_MATH_TR1
1445 if (!__param._M_easy)
1449 // See comments above...
1450 const double __naf =
1451 (1 - std::numeric_limits<double>::epsilon()) / 2;
1452 const double __thr =
1453 std::numeric_limits<_IntType>::max() + __naf;
1455 const double __np = std::floor(__t * __p12);
1458 const double __spi_2 = 1.2533141373155002512078826424055226L;
1459 const double __a1 = __param._M_a1;
1460 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1461 const double __a123 = __param._M_a123;
1462 const double __s1s = __param._M_s1 * __param._M_s1;
1463 const double __s2s = __param._M_s2 * __param._M_s2;
1468 const double __u = __param._M_s * __aurng();
1474 const double __n = _M_nd(__urng);
1475 const double __y = __param._M_s1 * std::abs(__n);
1476 __reject = __y >= __param._M_d1;
1479 const double __e = -std::log(__aurng());
1480 __x = std::floor(__y);
1481 __v = -__e - __n * __n / 2 + __param._M_c;
1484 else if (__u <= __a12)
1486 const double __n = _M_nd(__urng);
1487 const double __y = __param._M_s2 * std::abs(__n);
1488 __reject = __y >= __param._M_d2;
1491 const double __e = -std::log(__aurng());
1492 __x = std::floor(-__y);
1493 __v = -__e - __n * __n / 2;
1496 else if (__u <= __a123)
1498 const double __e1 = -std::log(__aurng());
1499 const double __e2 = -std::log(__aurng());
1501 const double __y = __param._M_d1
1502 + 2 * __s1s * __e1 / __param._M_d1;
1503 __x = std::floor(__y);
1504 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1505 -__y / (2 * __s1s)));
1510 const double __e1 = -std::log(__aurng());
1511 const double __e2 = -std::log(__aurng());
1513 const double __y = __param._M_d2
1514 + 2 * __s2s * __e1 / __param._M_d2;
1515 __x = std::floor(-__y);
1516 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1520 __reject = __reject || __x < -__np || __x > __t - __np;
1523 const double __lfx =
1524 std::lgamma(__np + __x + 1)
1525 + std::lgamma(__t - (__np + __x) + 1);
1526 __reject = __v > __param._M_lf - __lfx
1527 + __x * __param._M_lp1p;
1530 __reject |= __x + __np >= __thr;
1534 __x += __np + __naf;
1536 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1537 __ret = _IntType(__x) + __z;
1541 __ret = _M_waiting(__urng, __t);
1544 __ret = __t - __ret;
1548 template<typename _IntType,
1549 typename _CharT, typename _Traits>
1550 std::basic_ostream<_CharT, _Traits>&
1551 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1552 const binomial_distribution<_IntType>& __x)
1554 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1555 typedef typename __ostream_type::ios_base __ios_base;
1557 const typename __ios_base::fmtflags __flags = __os.flags();
1558 const _CharT __fill = __os.fill();
1559 const std::streamsize __precision = __os.precision();
1560 const _CharT __space = __os.widen(' ');
1561 __os.flags(__ios_base::scientific | __ios_base::left);
1563 __os.precision(std::numeric_limits<double>::max_digits10);
1565 __os << __x.t() << __space << __x.p()
1566 << __space << __x._M_nd;
1568 __os.flags(__flags);
1570 __os.precision(__precision);
1574 template<typename _IntType,
1575 typename _CharT, typename _Traits>
1576 std::basic_istream<_CharT, _Traits>&
1577 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1578 binomial_distribution<_IntType>& __x)
1580 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1581 typedef typename __istream_type::ios_base __ios_base;
1583 const typename __ios_base::fmtflags __flags = __is.flags();
1584 __is.flags(__ios_base::dec | __ios_base::skipws);
1588 __is >> __t >> __p >> __x._M_nd;
1589 __x.param(typename binomial_distribution<_IntType>::
1590 param_type(__t, __p));
1592 __is.flags(__flags);
1597 template<typename _RealType, typename _CharT, typename _Traits>
1598 std::basic_ostream<_CharT, _Traits>&
1599 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1600 const exponential_distribution<_RealType>& __x)
1602 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1603 typedef typename __ostream_type::ios_base __ios_base;
1605 const typename __ios_base::fmtflags __flags = __os.flags();
1606 const _CharT __fill = __os.fill();
1607 const std::streamsize __precision = __os.precision();
1608 __os.flags(__ios_base::scientific | __ios_base::left);
1609 __os.fill(__os.widen(' '));
1610 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1612 __os << __x.lambda();
1614 __os.flags(__flags);
1616 __os.precision(__precision);
1620 template<typename _RealType, typename _CharT, typename _Traits>
1621 std::basic_istream<_CharT, _Traits>&
1622 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1623 exponential_distribution<_RealType>& __x)
1625 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1626 typedef typename __istream_type::ios_base __ios_base;
1628 const typename __ios_base::fmtflags __flags = __is.flags();
1629 __is.flags(__ios_base::dec | __ios_base::skipws);
1633 __x.param(typename exponential_distribution<_RealType>::
1634 param_type(__lambda));
1636 __is.flags(__flags);
1642 * Polar method due to Marsaglia.
1644 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1645 * New York, 1986, Ch. V, Sect. 4.4.
1647 template<typename _RealType>
1648 template<typename _UniformRandomNumberGenerator>
1649 typename normal_distribution<_RealType>::result_type
1650 normal_distribution<_RealType>::
1651 operator()(_UniformRandomNumberGenerator& __urng,
1652 const param_type& __param)
1655 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1658 if (_M_saved_available)
1660 _M_saved_available = false;
1665 result_type __x, __y, __r2;
1668 __x = result_type(2.0) * __aurng() - 1.0;
1669 __y = result_type(2.0) * __aurng() - 1.0;
1670 __r2 = __x * __x + __y * __y;
1672 while (__r2 > 1.0 || __r2 == 0.0);
1674 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1675 _M_saved = __x * __mult;
1676 _M_saved_available = true;
1677 __ret = __y * __mult;
1680 __ret = __ret * __param.stddev() + __param.mean();
1684 template<typename _RealType>
1686 operator==(const std::normal_distribution<_RealType>& __d1,
1687 const std::normal_distribution<_RealType>& __d2)
1689 if (__d1._M_param == __d2._M_param
1690 && __d1._M_saved_available == __d2._M_saved_available)
1692 if (__d1._M_saved_available
1693 && __d1._M_saved == __d2._M_saved)
1695 else if(!__d1._M_saved_available)
1704 template<typename _RealType, typename _CharT, typename _Traits>
1705 std::basic_ostream<_CharT, _Traits>&
1706 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1707 const normal_distribution<_RealType>& __x)
1709 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1710 typedef typename __ostream_type::ios_base __ios_base;
1712 const typename __ios_base::fmtflags __flags = __os.flags();
1713 const _CharT __fill = __os.fill();
1714 const std::streamsize __precision = __os.precision();
1715 const _CharT __space = __os.widen(' ');
1716 __os.flags(__ios_base::scientific | __ios_base::left);
1718 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1720 __os << __x.mean() << __space << __x.stddev()
1721 << __space << __x._M_saved_available;
1722 if (__x._M_saved_available)
1723 __os << __space << __x._M_saved;
1725 __os.flags(__flags);
1727 __os.precision(__precision);
1731 template<typename _RealType, typename _CharT, typename _Traits>
1732 std::basic_istream<_CharT, _Traits>&
1733 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1734 normal_distribution<_RealType>& __x)
1736 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1737 typedef typename __istream_type::ios_base __ios_base;
1739 const typename __ios_base::fmtflags __flags = __is.flags();
1740 __is.flags(__ios_base::dec | __ios_base::skipws);
1742 double __mean, __stddev;
1743 __is >> __mean >> __stddev
1744 >> __x._M_saved_available;
1745 if (__x._M_saved_available)
1746 __is >> __x._M_saved;
1747 __x.param(typename normal_distribution<_RealType>::
1748 param_type(__mean, __stddev));
1750 __is.flags(__flags);
1755 template<typename _RealType, typename _CharT, typename _Traits>
1756 std::basic_ostream<_CharT, _Traits>&
1757 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1758 const lognormal_distribution<_RealType>& __x)
1760 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1761 typedef typename __ostream_type::ios_base __ios_base;
1763 const typename __ios_base::fmtflags __flags = __os.flags();
1764 const _CharT __fill = __os.fill();
1765 const std::streamsize __precision = __os.precision();
1766 const _CharT __space = __os.widen(' ');
1767 __os.flags(__ios_base::scientific | __ios_base::left);
1769 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1771 __os << __x.m() << __space << __x.s()
1772 << __space << __x._M_nd;
1774 __os.flags(__flags);
1776 __os.precision(__precision);
1780 template<typename _RealType, typename _CharT, typename _Traits>
1781 std::basic_istream<_CharT, _Traits>&
1782 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1783 lognormal_distribution<_RealType>& __x)
1785 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1786 typedef typename __istream_type::ios_base __ios_base;
1788 const typename __ios_base::fmtflags __flags = __is.flags();
1789 __is.flags(__ios_base::dec | __ios_base::skipws);
1792 __is >> __m >> __s >> __x._M_nd;
1793 __x.param(typename lognormal_distribution<_RealType>::
1794 param_type(__m, __s));
1796 __is.flags(__flags);
1801 template<typename _RealType, typename _CharT, typename _Traits>
1802 std::basic_ostream<_CharT, _Traits>&
1803 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1804 const chi_squared_distribution<_RealType>& __x)
1806 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1807 typedef typename __ostream_type::ios_base __ios_base;
1809 const typename __ios_base::fmtflags __flags = __os.flags();
1810 const _CharT __fill = __os.fill();
1811 const std::streamsize __precision = __os.precision();
1812 const _CharT __space = __os.widen(' ');
1813 __os.flags(__ios_base::scientific | __ios_base::left);
1815 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1817 __os << __x.n() << __space << __x._M_gd;
1819 __os.flags(__flags);
1821 __os.precision(__precision);
1825 template<typename _RealType, typename _CharT, typename _Traits>
1826 std::basic_istream<_CharT, _Traits>&
1827 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1828 chi_squared_distribution<_RealType>& __x)
1830 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1831 typedef typename __istream_type::ios_base __ios_base;
1833 const typename __ios_base::fmtflags __flags = __is.flags();
1834 __is.flags(__ios_base::dec | __ios_base::skipws);
1837 __is >> __n >> __x._M_gd;
1838 __x.param(typename chi_squared_distribution<_RealType>::
1841 __is.flags(__flags);
1846 template<typename _RealType>
1847 template<typename _UniformRandomNumberGenerator>
1848 typename cauchy_distribution<_RealType>::result_type
1849 cauchy_distribution<_RealType>::
1850 operator()(_UniformRandomNumberGenerator& __urng,
1851 const param_type& __p)
1853 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1860 const _RealType __pi = 3.1415926535897932384626433832795029L;
1861 return __p.a() + __p.b() * std::tan(__pi * __u);
1864 template<typename _RealType, typename _CharT, typename _Traits>
1865 std::basic_ostream<_CharT, _Traits>&
1866 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1867 const cauchy_distribution<_RealType>& __x)
1869 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1870 typedef typename __ostream_type::ios_base __ios_base;
1872 const typename __ios_base::fmtflags __flags = __os.flags();
1873 const _CharT __fill = __os.fill();
1874 const std::streamsize __precision = __os.precision();
1875 const _CharT __space = __os.widen(' ');
1876 __os.flags(__ios_base::scientific | __ios_base::left);
1878 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1880 __os << __x.a() << __space << __x.b();
1882 __os.flags(__flags);
1884 __os.precision(__precision);
1888 template<typename _RealType, typename _CharT, typename _Traits>
1889 std::basic_istream<_CharT, _Traits>&
1890 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1891 cauchy_distribution<_RealType>& __x)
1893 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1894 typedef typename __istream_type::ios_base __ios_base;
1896 const typename __ios_base::fmtflags __flags = __is.flags();
1897 __is.flags(__ios_base::dec | __ios_base::skipws);
1901 __x.param(typename cauchy_distribution<_RealType>::
1902 param_type(__a, __b));
1904 __is.flags(__flags);
1909 template<typename _RealType, typename _CharT, typename _Traits>
1910 std::basic_ostream<_CharT, _Traits>&
1911 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1912 const fisher_f_distribution<_RealType>& __x)
1914 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1915 typedef typename __ostream_type::ios_base __ios_base;
1917 const typename __ios_base::fmtflags __flags = __os.flags();
1918 const _CharT __fill = __os.fill();
1919 const std::streamsize __precision = __os.precision();
1920 const _CharT __space = __os.widen(' ');
1921 __os.flags(__ios_base::scientific | __ios_base::left);
1923 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1925 __os << __x.m() << __space << __x.n()
1926 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1928 __os.flags(__flags);
1930 __os.precision(__precision);
1934 template<typename _RealType, typename _CharT, typename _Traits>
1935 std::basic_istream<_CharT, _Traits>&
1936 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1937 fisher_f_distribution<_RealType>& __x)
1939 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1940 typedef typename __istream_type::ios_base __ios_base;
1942 const typename __ios_base::fmtflags __flags = __is.flags();
1943 __is.flags(__ios_base::dec | __ios_base::skipws);
1946 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1947 __x.param(typename fisher_f_distribution<_RealType>::
1948 param_type(__m, __n));
1950 __is.flags(__flags);
1955 template<typename _RealType, typename _CharT, typename _Traits>
1956 std::basic_ostream<_CharT, _Traits>&
1957 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1958 const student_t_distribution<_RealType>& __x)
1960 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1961 typedef typename __ostream_type::ios_base __ios_base;
1963 const typename __ios_base::fmtflags __flags = __os.flags();
1964 const _CharT __fill = __os.fill();
1965 const std::streamsize __precision = __os.precision();
1966 const _CharT __space = __os.widen(' ');
1967 __os.flags(__ios_base::scientific | __ios_base::left);
1969 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1971 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1973 __os.flags(__flags);
1975 __os.precision(__precision);
1979 template<typename _RealType, typename _CharT, typename _Traits>
1980 std::basic_istream<_CharT, _Traits>&
1981 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1982 student_t_distribution<_RealType>& __x)
1984 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1985 typedef typename __istream_type::ios_base __ios_base;
1987 const typename __ios_base::fmtflags __flags = __is.flags();
1988 __is.flags(__ios_base::dec | __ios_base::skipws);
1991 __is >> __n >> __x._M_nd >> __x._M_gd;
1992 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1994 __is.flags(__flags);
1999 template<typename _RealType>
2001 gamma_distribution<_RealType>::param_type::
2004 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2006 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2007 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2011 * Marsaglia, G. and Tsang, W. W.
2012 * "A Simple Method for Generating Gamma Variables"
2013 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2015 template<typename _RealType>
2016 template<typename _UniformRandomNumberGenerator>
2017 typename gamma_distribution<_RealType>::result_type
2018 gamma_distribution<_RealType>::
2019 operator()(_UniformRandomNumberGenerator& __urng,
2020 const param_type& __param)
2022 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2025 result_type __u, __v, __n;
2026 const result_type __a1 = (__param._M_malpha
2027 - _RealType(1.0) / _RealType(3.0));
2033 __n = _M_nd(__urng);
2034 __v = result_type(1.0) + __param._M_a2 * __n;
2038 __v = __v * __v * __v;
2041 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2042 && (std::log(__u) > (0.5 * __n * __n + __a1
2043 * (1.0 - __v + std::log(__v)))));
2045 if (__param.alpha() == __param._M_malpha)
2046 return __a1 * __v * __param.beta();
2053 return (std::pow(__u, result_type(1.0) / __param.alpha())
2054 * __a1 * __v * __param.beta());
2058 template<typename _RealType, typename _CharT, typename _Traits>
2059 std::basic_ostream<_CharT, _Traits>&
2060 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2061 const gamma_distribution<_RealType>& __x)
2063 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2064 typedef typename __ostream_type::ios_base __ios_base;
2066 const typename __ios_base::fmtflags __flags = __os.flags();
2067 const _CharT __fill = __os.fill();
2068 const std::streamsize __precision = __os.precision();
2069 const _CharT __space = __os.widen(' ');
2070 __os.flags(__ios_base::scientific | __ios_base::left);
2072 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2074 __os << __x.alpha() << __space << __x.beta()
2075 << __space << __x._M_nd;
2077 __os.flags(__flags);
2079 __os.precision(__precision);
2083 template<typename _RealType, typename _CharT, typename _Traits>
2084 std::basic_istream<_CharT, _Traits>&
2085 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2086 gamma_distribution<_RealType>& __x)
2088 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2089 typedef typename __istream_type::ios_base __ios_base;
2091 const typename __ios_base::fmtflags __flags = __is.flags();
2092 __is.flags(__ios_base::dec | __ios_base::skipws);
2094 _RealType __alpha_val, __beta_val;
2095 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2096 __x.param(typename gamma_distribution<_RealType>::
2097 param_type(__alpha_val, __beta_val));
2099 __is.flags(__flags);
2104 template<typename _RealType>
2105 template<typename _UniformRandomNumberGenerator>
2106 typename weibull_distribution<_RealType>::result_type
2107 weibull_distribution<_RealType>::
2108 operator()(_UniformRandomNumberGenerator& __urng,
2109 const param_type& __p)
2111 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2113 return __p.b() * std::pow(-std::log(__aurng()),
2114 result_type(1) / __p.a());
2117 template<typename _RealType, typename _CharT, typename _Traits>
2118 std::basic_ostream<_CharT, _Traits>&
2119 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2120 const weibull_distribution<_RealType>& __x)
2122 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2123 typedef typename __ostream_type::ios_base __ios_base;
2125 const typename __ios_base::fmtflags __flags = __os.flags();
2126 const _CharT __fill = __os.fill();
2127 const std::streamsize __precision = __os.precision();
2128 const _CharT __space = __os.widen(' ');
2129 __os.flags(__ios_base::scientific | __ios_base::left);
2131 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2133 __os << __x.a() << __space << __x.b();
2135 __os.flags(__flags);
2137 __os.precision(__precision);
2141 template<typename _RealType, typename _CharT, typename _Traits>
2142 std::basic_istream<_CharT, _Traits>&
2143 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2144 weibull_distribution<_RealType>& __x)
2146 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2147 typedef typename __istream_type::ios_base __ios_base;
2149 const typename __ios_base::fmtflags __flags = __is.flags();
2150 __is.flags(__ios_base::dec | __ios_base::skipws);
2154 __x.param(typename weibull_distribution<_RealType>::
2155 param_type(__a, __b));
2157 __is.flags(__flags);
2162 template<typename _RealType>
2163 template<typename _UniformRandomNumberGenerator>
2164 typename extreme_value_distribution<_RealType>::result_type
2165 extreme_value_distribution<_RealType>::
2166 operator()(_UniformRandomNumberGenerator& __urng,
2167 const param_type& __p)
2169 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2171 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2174 template<typename _RealType, typename _CharT, typename _Traits>
2175 std::basic_ostream<_CharT, _Traits>&
2176 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2177 const extreme_value_distribution<_RealType>& __x)
2179 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2180 typedef typename __ostream_type::ios_base __ios_base;
2182 const typename __ios_base::fmtflags __flags = __os.flags();
2183 const _CharT __fill = __os.fill();
2184 const std::streamsize __precision = __os.precision();
2185 const _CharT __space = __os.widen(' ');
2186 __os.flags(__ios_base::scientific | __ios_base::left);
2188 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2190 __os << __x.a() << __space << __x.b();
2192 __os.flags(__flags);
2194 __os.precision(__precision);
2198 template<typename _RealType, typename _CharT, typename _Traits>
2199 std::basic_istream<_CharT, _Traits>&
2200 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2201 extreme_value_distribution<_RealType>& __x)
2203 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2204 typedef typename __istream_type::ios_base __ios_base;
2206 const typename __ios_base::fmtflags __flags = __is.flags();
2207 __is.flags(__ios_base::dec | __ios_base::skipws);
2211 __x.param(typename extreme_value_distribution<_RealType>::
2212 param_type(__a, __b));
2214 __is.flags(__flags);
2219 template<typename _IntType>
2221 discrete_distribution<_IntType>::param_type::
2224 if (_M_prob.size() < 2)
2230 const double __sum = std::accumulate(_M_prob.begin(),
2231 _M_prob.end(), 0.0);
2232 // Now normalize the probabilites.
2233 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2234 std::bind2nd(std::divides<double>(), __sum));
2235 // Accumulate partial sums.
2236 _M_cp.reserve(_M_prob.size());
2237 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2238 std::back_inserter(_M_cp));
2239 // Make sure the last cumulative probability is one.
2240 _M_cp[_M_cp.size() - 1] = 1.0;
2243 template<typename _IntType>
2244 template<typename _Func>
2245 discrete_distribution<_IntType>::param_type::
2246 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2247 : _M_prob(), _M_cp()
2249 const size_t __n = __nw == 0 ? 1 : __nw;
2250 const double __delta = (__xmax - __xmin) / __n;
2252 _M_prob.reserve(__n);
2253 for (size_t __k = 0; __k < __nw; ++__k)
2254 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2259 template<typename _IntType>
2260 template<typename _UniformRandomNumberGenerator>
2261 typename discrete_distribution<_IntType>::result_type
2262 discrete_distribution<_IntType>::
2263 operator()(_UniformRandomNumberGenerator& __urng,
2264 const param_type& __param)
2266 if (__param._M_cp.empty())
2267 return result_type(0);
2269 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2272 const double __p = __aurng();
2273 auto __pos = std::lower_bound(__param._M_cp.begin(),
2274 __param._M_cp.end(), __p);
2276 return __pos - __param._M_cp.begin();
2279 template<typename _IntType, typename _CharT, typename _Traits>
2280 std::basic_ostream<_CharT, _Traits>&
2281 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2282 const discrete_distribution<_IntType>& __x)
2284 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2285 typedef typename __ostream_type::ios_base __ios_base;
2287 const typename __ios_base::fmtflags __flags = __os.flags();
2288 const _CharT __fill = __os.fill();
2289 const std::streamsize __precision = __os.precision();
2290 const _CharT __space = __os.widen(' ');
2291 __os.flags(__ios_base::scientific | __ios_base::left);
2293 __os.precision(std::numeric_limits<double>::max_digits10);
2295 std::vector<double> __prob = __x.probabilities();
2296 __os << __prob.size();
2297 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2298 __os << __space << *__dit;
2300 __os.flags(__flags);
2302 __os.precision(__precision);
2306 template<typename _IntType, typename _CharT, typename _Traits>
2307 std::basic_istream<_CharT, _Traits>&
2308 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2309 discrete_distribution<_IntType>& __x)
2311 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2312 typedef typename __istream_type::ios_base __ios_base;
2314 const typename __ios_base::fmtflags __flags = __is.flags();
2315 __is.flags(__ios_base::dec | __ios_base::skipws);
2320 std::vector<double> __prob_vec;
2321 __prob_vec.reserve(__n);
2322 for (; __n != 0; --__n)
2326 __prob_vec.push_back(__prob);
2329 __x.param(typename discrete_distribution<_IntType>::
2330 param_type(__prob_vec.begin(), __prob_vec.end()));
2332 __is.flags(__flags);
2337 template<typename _RealType>
2339 piecewise_constant_distribution<_RealType>::param_type::
2342 if (_M_int.size() < 2
2343 || (_M_int.size() == 2
2344 && _M_int[0] == _RealType(0)
2345 && _M_int[1] == _RealType(1)))
2352 const double __sum = std::accumulate(_M_den.begin(),
2355 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2356 std::bind2nd(std::divides<double>(), __sum));
2358 _M_cp.reserve(_M_den.size());
2359 std::partial_sum(_M_den.begin(), _M_den.end(),
2360 std::back_inserter(_M_cp));
2362 // Make sure the last cumulative probability is one.
2363 _M_cp[_M_cp.size() - 1] = 1.0;
2365 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2366 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2369 template<typename _RealType>
2370 template<typename _InputIteratorB, typename _InputIteratorW>
2371 piecewise_constant_distribution<_RealType>::param_type::
2372 param_type(_InputIteratorB __bbegin,
2373 _InputIteratorB __bend,
2374 _InputIteratorW __wbegin)
2375 : _M_int(), _M_den(), _M_cp()
2377 if (__bbegin != __bend)
2381 _M_int.push_back(*__bbegin);
2383 if (__bbegin == __bend)
2386 _M_den.push_back(*__wbegin);
2394 template<typename _RealType>
2395 template<typename _Func>
2396 piecewise_constant_distribution<_RealType>::param_type::
2397 param_type(initializer_list<_RealType> __bl, _Func __fw)
2398 : _M_int(), _M_den(), _M_cp()
2400 _M_int.reserve(__bl.size());
2401 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2402 _M_int.push_back(*__biter);
2404 _M_den.reserve(_M_int.size() - 1);
2405 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2406 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2411 template<typename _RealType>
2412 template<typename _Func>
2413 piecewise_constant_distribution<_RealType>::param_type::
2414 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2415 : _M_int(), _M_den(), _M_cp()
2417 const size_t __n = __nw == 0 ? 1 : __nw;
2418 const _RealType __delta = (__xmax - __xmin) / __n;
2420 _M_int.reserve(__n + 1);
2421 for (size_t __k = 0; __k <= __nw; ++__k)
2422 _M_int.push_back(__xmin + __k * __delta);
2424 _M_den.reserve(__n);
2425 for (size_t __k = 0; __k < __nw; ++__k)
2426 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2431 template<typename _RealType>
2432 template<typename _UniformRandomNumberGenerator>
2433 typename piecewise_constant_distribution<_RealType>::result_type
2434 piecewise_constant_distribution<_RealType>::
2435 operator()(_UniformRandomNumberGenerator& __urng,
2436 const param_type& __param)
2438 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2441 const double __p = __aurng();
2442 if (__param._M_cp.empty())
2445 auto __pos = std::lower_bound(__param._M_cp.begin(),
2446 __param._M_cp.end(), __p);
2447 const size_t __i = __pos - __param._M_cp.begin();
2449 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2451 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2454 template<typename _RealType, typename _CharT, typename _Traits>
2455 std::basic_ostream<_CharT, _Traits>&
2456 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2457 const piecewise_constant_distribution<_RealType>& __x)
2459 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2460 typedef typename __ostream_type::ios_base __ios_base;
2462 const typename __ios_base::fmtflags __flags = __os.flags();
2463 const _CharT __fill = __os.fill();
2464 const std::streamsize __precision = __os.precision();
2465 const _CharT __space = __os.widen(' ');
2466 __os.flags(__ios_base::scientific | __ios_base::left);
2468 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2470 std::vector<_RealType> __int = __x.intervals();
2471 __os << __int.size() - 1;
2473 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2474 __os << __space << *__xit;
2476 std::vector<double> __den = __x.densities();
2477 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2478 __os << __space << *__dit;
2480 __os.flags(__flags);
2482 __os.precision(__precision);
2486 template<typename _RealType, typename _CharT, typename _Traits>
2487 std::basic_istream<_CharT, _Traits>&
2488 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2489 piecewise_constant_distribution<_RealType>& __x)
2491 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2492 typedef typename __istream_type::ios_base __ios_base;
2494 const typename __ios_base::fmtflags __flags = __is.flags();
2495 __is.flags(__ios_base::dec | __ios_base::skipws);
2500 std::vector<_RealType> __int_vec;
2501 __int_vec.reserve(__n + 1);
2502 for (size_t __i = 0; __i <= __n; ++__i)
2506 __int_vec.push_back(__int);
2509 std::vector<double> __den_vec;
2510 __den_vec.reserve(__n);
2511 for (size_t __i = 0; __i < __n; ++__i)
2515 __den_vec.push_back(__den);
2518 __x.param(typename piecewise_constant_distribution<_RealType>::
2519 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2521 __is.flags(__flags);
2526 template<typename _RealType>
2528 piecewise_linear_distribution<_RealType>::param_type::
2531 if (_M_int.size() < 2
2532 || (_M_int.size() == 2
2533 && _M_int[0] == _RealType(0)
2534 && _M_int[1] == _RealType(1)
2535 && _M_den[0] == _M_den[1]))
2543 _M_cp.reserve(_M_int.size() - 1);
2544 _M_m.reserve(_M_int.size() - 1);
2545 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2547 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2548 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2549 _M_cp.push_back(__sum);
2550 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2553 // Now normalize the densities...
2554 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2555 std::bind2nd(std::divides<double>(), __sum));
2556 // ... and partial sums...
2557 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2558 std::bind2nd(std::divides<double>(), __sum));
2560 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2561 std::bind2nd(std::divides<double>(), __sum));
2562 // Make sure the last cumulative probablility is one.
2563 _M_cp[_M_cp.size() - 1] = 1.0;
2566 template<typename _RealType>
2567 template<typename _InputIteratorB, typename _InputIteratorW>
2568 piecewise_linear_distribution<_RealType>::param_type::
2569 param_type(_InputIteratorB __bbegin,
2570 _InputIteratorB __bend,
2571 _InputIteratorW __wbegin)
2572 : _M_int(), _M_den(), _M_cp(), _M_m()
2574 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2576 _M_int.push_back(*__bbegin);
2577 _M_den.push_back(*__wbegin);
2583 template<typename _RealType>
2584 template<typename _Func>
2585 piecewise_linear_distribution<_RealType>::param_type::
2586 param_type(initializer_list<_RealType> __bl, _Func __fw)
2587 : _M_int(), _M_den(), _M_cp(), _M_m()
2589 _M_int.reserve(__bl.size());
2590 _M_den.reserve(__bl.size());
2591 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2593 _M_int.push_back(*__biter);
2594 _M_den.push_back(__fw(*__biter));
2600 template<typename _RealType>
2601 template<typename _Func>
2602 piecewise_linear_distribution<_RealType>::param_type::
2603 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2604 : _M_int(), _M_den(), _M_cp(), _M_m()
2606 const size_t __n = __nw == 0 ? 1 : __nw;
2607 const _RealType __delta = (__xmax - __xmin) / __n;
2609 _M_int.reserve(__n + 1);
2610 _M_den.reserve(__n + 1);
2611 for (size_t __k = 0; __k <= __nw; ++__k)
2613 _M_int.push_back(__xmin + __k * __delta);
2614 _M_den.push_back(__fw(_M_int[__k] + __delta));
2620 template<typename _RealType>
2621 template<typename _UniformRandomNumberGenerator>
2622 typename piecewise_linear_distribution<_RealType>::result_type
2623 piecewise_linear_distribution<_RealType>::
2624 operator()(_UniformRandomNumberGenerator& __urng,
2625 const param_type& __param)
2627 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2630 const double __p = __aurng();
2631 if (__param._M_cp.empty())
2634 auto __pos = std::lower_bound(__param._M_cp.begin(),
2635 __param._M_cp.end(), __p);
2636 const size_t __i = __pos - __param._M_cp.begin();
2638 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2640 const double __a = 0.5 * __param._M_m[__i];
2641 const double __b = __param._M_den[__i];
2642 const double __cm = __p - __pref;
2644 _RealType __x = __param._M_int[__i];
2649 const double __d = __b * __b + 4.0 * __a * __cm;
2650 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2656 template<typename _RealType, typename _CharT, typename _Traits>
2657 std::basic_ostream<_CharT, _Traits>&
2658 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2659 const piecewise_linear_distribution<_RealType>& __x)
2661 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2662 typedef typename __ostream_type::ios_base __ios_base;
2664 const typename __ios_base::fmtflags __flags = __os.flags();
2665 const _CharT __fill = __os.fill();
2666 const std::streamsize __precision = __os.precision();
2667 const _CharT __space = __os.widen(' ');
2668 __os.flags(__ios_base::scientific | __ios_base::left);
2670 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2672 std::vector<_RealType> __int = __x.intervals();
2673 __os << __int.size() - 1;
2675 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2676 __os << __space << *__xit;
2678 std::vector<double> __den = __x.densities();
2679 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2680 __os << __space << *__dit;
2682 __os.flags(__flags);
2684 __os.precision(__precision);
2688 template<typename _RealType, typename _CharT, typename _Traits>
2689 std::basic_istream<_CharT, _Traits>&
2690 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2691 piecewise_linear_distribution<_RealType>& __x)
2693 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2694 typedef typename __istream_type::ios_base __ios_base;
2696 const typename __ios_base::fmtflags __flags = __is.flags();
2697 __is.flags(__ios_base::dec | __ios_base::skipws);
2702 std::vector<_RealType> __int_vec;
2703 __int_vec.reserve(__n + 1);
2704 for (size_t __i = 0; __i <= __n; ++__i)
2708 __int_vec.push_back(__int);
2711 std::vector<double> __den_vec;
2712 __den_vec.reserve(__n + 1);
2713 for (size_t __i = 0; __i <= __n; ++__i)
2717 __den_vec.push_back(__den);
2720 __x.param(typename piecewise_linear_distribution<_RealType>::
2721 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2723 __is.flags(__flags);
2728 template<typename _IntType>
2729 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2731 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2732 _M_v.push_back(__detail::__mod<result_type,
2733 __detail::_Shift<result_type, 32>::__value>(*__iter));
2736 template<typename _InputIterator>
2737 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2739 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2740 _M_v.push_back(__detail::__mod<result_type,
2741 __detail::_Shift<result_type, 32>::__value>(*__iter));
2744 template<typename _RandomAccessIterator>
2746 seed_seq::generate(_RandomAccessIterator __begin,
2747 _RandomAccessIterator __end)
2749 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2752 if (__begin == __end)
2755 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2757 const size_t __n = __end - __begin;
2758 const size_t __s = _M_v.size();
2759 const size_t __t = (__n >= 623) ? 11
2764 const size_t __p = (__n - __t) / 2;
2765 const size_t __q = __p + __t;
2766 const size_t __m = std::max(__s + 1, __n);
2768 for (size_t __k = 0; __k < __m; ++__k)
2770 _Type __arg = (__begin[__k % __n]
2771 ^ __begin[(__k + __p) % __n]
2772 ^ __begin[(__k - 1) % __n]);
2773 _Type __r1 = __arg ^ (__arg >> 27);
2774 __r1 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
2775 1664525u, 0u>(__r1);
2779 else if (__k <= __s)
2780 __r2 += __k % __n + _M_v[__k - 1];
2783 __r2 = __detail::__mod<_Type,
2784 __detail::_Shift<_Type, 32>::__value>(__r2);
2785 __begin[(__k + __p) % __n] += __r1;
2786 __begin[(__k + __q) % __n] += __r2;
2787 __begin[__k % __n] = __r2;
2790 for (size_t __k = __m; __k < __m + __n; ++__k)
2792 _Type __arg = (__begin[__k % __n]
2793 + __begin[(__k + __p) % __n]
2794 + __begin[(__k - 1) % __n]);
2795 _Type __r3 = __arg ^ (__arg >> 27);
2796 __r3 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
2797 1566083941u, 0u>(__r3);
2798 _Type __r4 = __r3 - __k % __n;
2799 __r4 = __detail::__mod<_Type,
2800 __detail::_Shift<_Type, 32>::__value>(__r4);
2801 __begin[(__k + __p) % __n] ^= __r3;
2802 __begin[(__k + __q) % __n] ^= __r4;
2803 __begin[__k % __n] = __r4;
2807 template<typename _RealType, size_t __bits,
2808 typename _UniformRandomNumberGenerator>
2810 generate_canonical(_UniformRandomNumberGenerator& __urng)
2813 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2815 const long double __r = static_cast<long double>(__urng.max())
2816 - static_cast<long double>(__urng.min()) + 1.0L;
2817 const size_t __log2r = std::log(__r) / std::log(2.0L);
2818 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2819 _RealType __sum = _RealType(0);
2820 _RealType __tmp = _RealType(1);
2821 for (; __k != 0; --__k)
2823 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2826 return __sum / __tmp;
2829 _GLIBCXX_END_NAMESPACE_VERSION