1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2009-2020 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)
37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 * (Further) implementation-space details.
44 // General case for x = (ax + c) mod m -- use Schrage's algorithm
45 // to avoid integer overflow.
47 // Preconditions: a > 0, m > 0.
49 // Note: only works correctly for __m % __a < __m / __a.
50 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
52 _Mod<_Tp, __m, __a, __c, false, true>::
59 static const _Tp __q = __m / __a;
60 static const _Tp __r = __m % __a;
62 _Tp __t1 = __a * (__x % __q);
63 _Tp __t2 = __r * (__x / __q);
67 __x = __m - __t2 + __t1;
72 const _Tp __d = __m - __x;
81 template<typename _InputIterator, typename _OutputIterator,
84 __normalize(_InputIterator __first, _InputIterator __last,
85 _OutputIterator __result, const _Tp& __factor)
87 for (; __first != __last; ++__first, ++__result)
88 *__result = *__first / __factor;
92 } // namespace __detail
94 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
96 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
98 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
100 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
102 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
104 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
106 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
108 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
111 * Seeds the LCR with integral value @p __s, adjusted so that the
112 * ring identity is never a member of the convergence set.
114 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
116 linear_congruential_engine<_UIntType, __a, __c, __m>::
117 seed(result_type __s)
119 if ((__detail::__mod<_UIntType, __m>(__c) == 0)
120 && (__detail::__mod<_UIntType, __m>(__s) == 0))
123 _M_x = __detail::__mod<_UIntType, __m>(__s);
127 * Seeds the LCR engine with a value generated by @p __q.
129 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
130 template<typename _Sseq>
132 linear_congruential_engine<_UIntType, __a, __c, __m>::
134 -> _If_seed_seq<_Sseq>
136 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
138 const _UIntType __k = (__k0 + 31) / 32;
139 uint_least32_t __arr[__k + 3];
140 __q.generate(__arr + 0, __arr + __k + 3);
141 _UIntType __factor = 1u;
142 _UIntType __sum = 0u;
143 for (size_t __j = 0; __j < __k; ++__j)
145 __sum += __arr[__j + 3] * __factor;
146 __factor *= __detail::_Shift<_UIntType, 32>::__value;
151 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
152 typename _CharT, typename _Traits>
153 std::basic_ostream<_CharT, _Traits>&
154 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
155 const linear_congruential_engine<_UIntType,
156 __a, __c, __m>& __lcr)
158 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
160 const typename __ios_base::fmtflags __flags = __os.flags();
161 const _CharT __fill = __os.fill();
162 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
163 __os.fill(__os.widen(' '));
172 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
173 typename _CharT, typename _Traits>
174 std::basic_istream<_CharT, _Traits>&
175 operator>>(std::basic_istream<_CharT, _Traits>& __is,
176 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
178 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
180 const typename __ios_base::fmtflags __flags = __is.flags();
181 __is.flags(__ios_base::dec);
190 template<typename _UIntType,
191 size_t __w, size_t __n, size_t __m, size_t __r,
192 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
193 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
196 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
197 __s, __b, __t, __c, __l, __f>::word_size;
199 template<typename _UIntType,
200 size_t __w, size_t __n, size_t __m, size_t __r,
201 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
202 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
205 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
206 __s, __b, __t, __c, __l, __f>::state_size;
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>::shift_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>::mask_bits;
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>::xor_mask;
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>::tempering_u;
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>::tempering_d;
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_s;
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_b;
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_t;
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_c;
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_l;
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>::
306 initialization_multiplier;
308 template<typename _UIntType,
309 size_t __w, size_t __n, size_t __m, size_t __r,
310 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
311 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
314 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
315 __s, __b, __t, __c, __l, __f>::default_seed;
317 template<typename _UIntType,
318 size_t __w, size_t __n, size_t __m, size_t __r,
319 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
320 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
323 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
324 __s, __b, __t, __c, __l, __f>::
325 seed(result_type __sd)
327 _M_x[0] = __detail::__mod<_UIntType,
328 __detail::_Shift<_UIntType, __w>::__value>(__sd);
330 for (size_t __i = 1; __i < state_size; ++__i)
332 _UIntType __x = _M_x[__i - 1];
333 __x ^= __x >> (__w - 2);
335 __x += __detail::__mod<_UIntType, __n>(__i);
336 _M_x[__i] = __detail::__mod<_UIntType,
337 __detail::_Shift<_UIntType, __w>::__value>(__x);
342 template<typename _UIntType,
343 size_t __w, size_t __n, size_t __m, size_t __r,
344 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
345 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
347 template<typename _Sseq>
349 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
350 __s, __b, __t, __c, __l, __f>::
352 -> _If_seed_seq<_Sseq>
354 const _UIntType __upper_mask = (~_UIntType()) << __r;
355 const size_t __k = (__w + 31) / 32;
356 uint_least32_t __arr[__n * __k];
357 __q.generate(__arr + 0, __arr + __n * __k);
360 for (size_t __i = 0; __i < state_size; ++__i)
362 _UIntType __factor = 1u;
363 _UIntType __sum = 0u;
364 for (size_t __j = 0; __j < __k; ++__j)
366 __sum += __arr[__k * __i + __j] * __factor;
367 __factor *= __detail::_Shift<_UIntType, 32>::__value;
369 _M_x[__i] = __detail::__mod<_UIntType,
370 __detail::_Shift<_UIntType, __w>::__value>(__sum);
376 if ((_M_x[0] & __upper_mask) != 0u)
379 else if (_M_x[__i] != 0u)
384 _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
388 template<typename _UIntType, size_t __w,
389 size_t __n, size_t __m, size_t __r,
390 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
391 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
394 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
395 __s, __b, __t, __c, __l, __f>::
398 const _UIntType __upper_mask = (~_UIntType()) << __r;
399 const _UIntType __lower_mask = ~__upper_mask;
401 for (size_t __k = 0; __k < (__n - __m); ++__k)
403 _UIntType __y = ((_M_x[__k] & __upper_mask)
404 | (_M_x[__k + 1] & __lower_mask));
405 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
406 ^ ((__y & 0x01) ? __a : 0));
409 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
411 _UIntType __y = ((_M_x[__k] & __upper_mask)
412 | (_M_x[__k + 1] & __lower_mask));
413 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
414 ^ ((__y & 0x01) ? __a : 0));
417 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
418 | (_M_x[0] & __lower_mask));
419 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
420 ^ ((__y & 0x01) ? __a : 0));
424 template<typename _UIntType, size_t __w,
425 size_t __n, size_t __m, size_t __r,
426 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
427 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
430 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
431 __s, __b, __t, __c, __l, __f>::
432 discard(unsigned long long __z)
434 while (__z > state_size - _M_p)
436 __z -= state_size - _M_p;
442 template<typename _UIntType, size_t __w,
443 size_t __n, size_t __m, size_t __r,
444 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
445 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
448 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
449 __s, __b, __t, __c, __l, __f>::result_type
450 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
451 __s, __b, __t, __c, __l, __f>::
454 // Reload the vector - cost is O(n) amortized over n calls.
455 if (_M_p >= state_size)
458 // Calculate o(x(i)).
459 result_type __z = _M_x[_M_p++];
460 __z ^= (__z >> __u) & __d;
461 __z ^= (__z << __s) & __b;
462 __z ^= (__z << __t) & __c;
468 template<typename _UIntType, size_t __w,
469 size_t __n, size_t __m, size_t __r,
470 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
471 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
472 _UIntType __f, typename _CharT, typename _Traits>
473 std::basic_ostream<_CharT, _Traits>&
474 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
475 const mersenne_twister_engine<_UIntType, __w, __n, __m,
476 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
478 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
480 const typename __ios_base::fmtflags __flags = __os.flags();
481 const _CharT __fill = __os.fill();
482 const _CharT __space = __os.widen(' ');
483 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
486 for (size_t __i = 0; __i < __n; ++__i)
487 __os << __x._M_x[__i] << __space;
495 template<typename _UIntType, size_t __w,
496 size_t __n, size_t __m, size_t __r,
497 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
498 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
499 _UIntType __f, typename _CharT, typename _Traits>
500 std::basic_istream<_CharT, _Traits>&
501 operator>>(std::basic_istream<_CharT, _Traits>& __is,
502 mersenne_twister_engine<_UIntType, __w, __n, __m,
503 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
505 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
507 const typename __ios_base::fmtflags __flags = __is.flags();
508 __is.flags(__ios_base::dec | __ios_base::skipws);
510 for (size_t __i = 0; __i < __n; ++__i)
511 __is >> __x._M_x[__i];
519 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
521 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
523 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
525 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
527 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
529 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
531 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
533 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
535 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
537 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
538 seed(result_type __value)
540 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
541 __lcg(__value == 0u ? default_seed : __value);
543 const size_t __n = (__w + 31) / 32;
545 for (size_t __i = 0; __i < long_lag; ++__i)
547 _UIntType __sum = 0u;
548 _UIntType __factor = 1u;
549 for (size_t __j = 0; __j < __n; ++__j)
551 __sum += __detail::__mod<uint_least32_t,
552 __detail::_Shift<uint_least32_t, 32>::__value>
553 (__lcg()) * __factor;
554 __factor *= __detail::_Shift<_UIntType, 32>::__value;
556 _M_x[__i] = __detail::__mod<_UIntType,
557 __detail::_Shift<_UIntType, __w>::__value>(__sum);
559 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
563 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
564 template<typename _Sseq>
566 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
568 -> _If_seed_seq<_Sseq>
570 const size_t __k = (__w + 31) / 32;
571 uint_least32_t __arr[__r * __k];
572 __q.generate(__arr + 0, __arr + __r * __k);
574 for (size_t __i = 0; __i < long_lag; ++__i)
576 _UIntType __sum = 0u;
577 _UIntType __factor = 1u;
578 for (size_t __j = 0; __j < __k; ++__j)
580 __sum += __arr[__k * __i + __j] * __factor;
581 __factor *= __detail::_Shift<_UIntType, 32>::__value;
583 _M_x[__i] = __detail::__mod<_UIntType,
584 __detail::_Shift<_UIntType, __w>::__value>(__sum);
586 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
590 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
591 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
593 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
596 // Derive short lag index from current index.
597 long __ps = _M_p - short_lag;
601 // Calculate new x(i) without overflow or division.
602 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
605 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
607 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
612 __xi = (__detail::_Shift<_UIntType, __w>::__value
613 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
618 // Adjust current index to loop around in ring buffer.
619 if (++_M_p >= long_lag)
625 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
626 typename _CharT, typename _Traits>
627 std::basic_ostream<_CharT, _Traits>&
628 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
629 const subtract_with_carry_engine<_UIntType,
632 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
634 const typename __ios_base::fmtflags __flags = __os.flags();
635 const _CharT __fill = __os.fill();
636 const _CharT __space = __os.widen(' ');
637 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
640 for (size_t __i = 0; __i < __r; ++__i)
641 __os << __x._M_x[__i] << __space;
642 __os << __x._M_carry << __space << __x._M_p;
649 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
650 typename _CharT, typename _Traits>
651 std::basic_istream<_CharT, _Traits>&
652 operator>>(std::basic_istream<_CharT, _Traits>& __is,
653 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
655 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
657 const typename __ios_base::fmtflags __flags = __is.flags();
658 __is.flags(__ios_base::dec | __ios_base::skipws);
660 for (size_t __i = 0; __i < __r; ++__i)
661 __is >> __x._M_x[__i];
662 __is >> __x._M_carry;
670 template<typename _RandomNumberEngine, size_t __p, size_t __r>
672 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
674 template<typename _RandomNumberEngine, size_t __p, size_t __r>
676 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
678 template<typename _RandomNumberEngine, size_t __p, size_t __r>
679 typename discard_block_engine<_RandomNumberEngine,
680 __p, __r>::result_type
681 discard_block_engine<_RandomNumberEngine, __p, __r>::
684 if (_M_n >= used_block)
686 _M_b.discard(block_size - _M_n);
693 template<typename _RandomNumberEngine, size_t __p, size_t __r,
694 typename _CharT, typename _Traits>
695 std::basic_ostream<_CharT, _Traits>&
696 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
697 const discard_block_engine<_RandomNumberEngine,
700 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
702 const typename __ios_base::fmtflags __flags = __os.flags();
703 const _CharT __fill = __os.fill();
704 const _CharT __space = __os.widen(' ');
705 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
708 __os << __x.base() << __space << __x._M_n;
715 template<typename _RandomNumberEngine, size_t __p, size_t __r,
716 typename _CharT, typename _Traits>
717 std::basic_istream<_CharT, _Traits>&
718 operator>>(std::basic_istream<_CharT, _Traits>& __is,
719 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
721 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
723 const typename __ios_base::fmtflags __flags = __is.flags();
724 __is.flags(__ios_base::dec | __ios_base::skipws);
726 __is >> __x._M_b >> __x._M_n;
733 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
734 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
736 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
739 typedef typename _RandomNumberEngine::result_type _Eresult_type;
740 const _Eresult_type __r
741 = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max()
742 ? _M_b.max() - _M_b.min() + 1 : 0);
743 const unsigned __edig = std::numeric_limits<_Eresult_type>::digits;
744 const unsigned __m = __r ? std::__lg(__r) : __edig;
746 typedef typename std::common_type<_Eresult_type, result_type>::type
748 const unsigned __cdig = std::numeric_limits<__ctype>::digits;
751 __ctype __s0, __s1, __y0, __y1;
753 for (size_t __i = 0; __i < 2; ++__i)
755 __n = (__w + __m - 1) / __m + __i;
756 __n0 = __n - __w % __n;
757 const unsigned __w0 = __w / __n; // __w0 <= __m
763 __s0 = __ctype(1) << __w0;
771 __y0 = __s0 * (__r / __s0);
773 __y1 = __s1 * (__r / __s1);
775 if (__r - __y0 <= __y0 / __n)
782 result_type __sum = 0;
783 for (size_t __k = 0; __k < __n0; ++__k)
787 __u = _M_b() - _M_b.min();
788 while (__y0 && __u >= __y0);
789 __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
791 for (size_t __k = __n0; __k < __n; ++__k)
795 __u = _M_b() - _M_b.min();
796 while (__y1 && __u >= __y1);
797 __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
803 template<typename _RandomNumberEngine, size_t __k>
805 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
807 template<typename _RandomNumberEngine, size_t __k>
808 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
809 shuffle_order_engine<_RandomNumberEngine, __k>::
812 size_t __j = __k * ((_M_y - _M_b.min())
813 / (_M_b.max() - _M_b.min() + 1.0L));
820 template<typename _RandomNumberEngine, size_t __k,
821 typename _CharT, typename _Traits>
822 std::basic_ostream<_CharT, _Traits>&
823 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
824 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
826 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
828 const typename __ios_base::fmtflags __flags = __os.flags();
829 const _CharT __fill = __os.fill();
830 const _CharT __space = __os.widen(' ');
831 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
835 for (size_t __i = 0; __i < __k; ++__i)
836 __os << __space << __x._M_v[__i];
837 __os << __space << __x._M_y;
844 template<typename _RandomNumberEngine, size_t __k,
845 typename _CharT, typename _Traits>
846 std::basic_istream<_CharT, _Traits>&
847 operator>>(std::basic_istream<_CharT, _Traits>& __is,
848 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
850 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
852 const typename __ios_base::fmtflags __flags = __is.flags();
853 __is.flags(__ios_base::dec | __ios_base::skipws);
856 for (size_t __i = 0; __i < __k; ++__i)
857 __is >> __x._M_v[__i];
865 template<typename _IntType, typename _CharT, typename _Traits>
866 std::basic_ostream<_CharT, _Traits>&
867 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
868 const uniform_int_distribution<_IntType>& __x)
870 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
872 const typename __ios_base::fmtflags __flags = __os.flags();
873 const _CharT __fill = __os.fill();
874 const _CharT __space = __os.widen(' ');
875 __os.flags(__ios_base::scientific | __ios_base::left);
878 __os << __x.a() << __space << __x.b();
885 template<typename _IntType, typename _CharT, typename _Traits>
886 std::basic_istream<_CharT, _Traits>&
887 operator>>(std::basic_istream<_CharT, _Traits>& __is,
888 uniform_int_distribution<_IntType>& __x)
891 = typename uniform_int_distribution<_IntType>::param_type;
892 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
894 const typename __ios_base::fmtflags __flags = __is.flags();
895 __is.flags(__ios_base::dec | __ios_base::skipws);
898 if (__is >> __a >> __b)
899 __x.param(param_type(__a, __b));
906 template<typename _RealType>
907 template<typename _ForwardIterator,
908 typename _UniformRandomNumberGenerator>
910 uniform_real_distribution<_RealType>::
911 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
912 _UniformRandomNumberGenerator& __urng,
913 const param_type& __p)
915 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
916 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
918 auto __range = __p.b() - __p.a();
920 *__f++ = __aurng() * __range + __p.a();
923 template<typename _RealType, typename _CharT, typename _Traits>
924 std::basic_ostream<_CharT, _Traits>&
925 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
926 const uniform_real_distribution<_RealType>& __x)
928 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
930 const typename __ios_base::fmtflags __flags = __os.flags();
931 const _CharT __fill = __os.fill();
932 const std::streamsize __precision = __os.precision();
933 const _CharT __space = __os.widen(' ');
934 __os.flags(__ios_base::scientific | __ios_base::left);
936 __os.precision(std::numeric_limits<_RealType>::max_digits10);
938 __os << __x.a() << __space << __x.b();
942 __os.precision(__precision);
946 template<typename _RealType, typename _CharT, typename _Traits>
947 std::basic_istream<_CharT, _Traits>&
948 operator>>(std::basic_istream<_CharT, _Traits>& __is,
949 uniform_real_distribution<_RealType>& __x)
952 = typename uniform_real_distribution<_RealType>::param_type;
953 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
955 const typename __ios_base::fmtflags __flags = __is.flags();
956 __is.flags(__ios_base::skipws);
959 if (__is >> __a >> __b)
960 __x.param(param_type(__a, __b));
967 template<typename _ForwardIterator,
968 typename _UniformRandomNumberGenerator>
970 std::bernoulli_distribution::
971 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
972 _UniformRandomNumberGenerator& __urng,
973 const param_type& __p)
975 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
976 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
978 auto __limit = __p.p() * (__aurng.max() - __aurng.min());
981 *__f++ = (__aurng() - __aurng.min()) < __limit;
984 template<typename _CharT, typename _Traits>
985 std::basic_ostream<_CharT, _Traits>&
986 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
987 const bernoulli_distribution& __x)
989 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
991 const typename __ios_base::fmtflags __flags = __os.flags();
992 const _CharT __fill = __os.fill();
993 const std::streamsize __precision = __os.precision();
994 __os.flags(__ios_base::scientific | __ios_base::left);
995 __os.fill(__os.widen(' '));
996 __os.precision(std::numeric_limits<double>::max_digits10);
1000 __os.flags(__flags);
1002 __os.precision(__precision);
1007 template<typename _IntType>
1008 template<typename _UniformRandomNumberGenerator>
1009 typename geometric_distribution<_IntType>::result_type
1010 geometric_distribution<_IntType>::
1011 operator()(_UniformRandomNumberGenerator& __urng,
1012 const param_type& __param)
1014 // About the epsilon thing see this thread:
1015 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1016 const double __naf =
1017 (1 - std::numeric_limits<double>::epsilon()) / 2;
1018 // The largest _RealType convertible to _IntType.
1019 const double __thr =
1020 std::numeric_limits<_IntType>::max() + __naf;
1021 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1026 __cand = std::floor(std::log(1.0 - __aurng()) / __param._M_log_1_p);
1027 while (__cand >= __thr);
1029 return result_type(__cand + __naf);
1032 template<typename _IntType>
1033 template<typename _ForwardIterator,
1034 typename _UniformRandomNumberGenerator>
1036 geometric_distribution<_IntType>::
1037 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1038 _UniformRandomNumberGenerator& __urng,
1039 const param_type& __param)
1041 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1042 // About the epsilon thing see this thread:
1043 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1044 const double __naf =
1045 (1 - std::numeric_limits<double>::epsilon()) / 2;
1046 // The largest _RealType convertible to _IntType.
1047 const double __thr =
1048 std::numeric_limits<_IntType>::max() + __naf;
1049 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1056 __cand = std::floor(std::log(1.0 - __aurng())
1057 / __param._M_log_1_p);
1058 while (__cand >= __thr);
1060 *__f++ = __cand + __naf;
1064 template<typename _IntType,
1065 typename _CharT, typename _Traits>
1066 std::basic_ostream<_CharT, _Traits>&
1067 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1068 const geometric_distribution<_IntType>& __x)
1070 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1072 const typename __ios_base::fmtflags __flags = __os.flags();
1073 const _CharT __fill = __os.fill();
1074 const std::streamsize __precision = __os.precision();
1075 __os.flags(__ios_base::scientific | __ios_base::left);
1076 __os.fill(__os.widen(' '));
1077 __os.precision(std::numeric_limits<double>::max_digits10);
1081 __os.flags(__flags);
1083 __os.precision(__precision);
1087 template<typename _IntType,
1088 typename _CharT, typename _Traits>
1089 std::basic_istream<_CharT, _Traits>&
1090 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1091 geometric_distribution<_IntType>& __x)
1093 using param_type = typename geometric_distribution<_IntType>::param_type;
1094 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1096 const typename __ios_base::fmtflags __flags = __is.flags();
1097 __is.flags(__ios_base::skipws);
1101 __x.param(param_type(__p));
1103 __is.flags(__flags);
1107 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1108 template<typename _IntType>
1109 template<typename _UniformRandomNumberGenerator>
1110 typename negative_binomial_distribution<_IntType>::result_type
1111 negative_binomial_distribution<_IntType>::
1112 operator()(_UniformRandomNumberGenerator& __urng)
1114 const double __y = _M_gd(__urng);
1116 // XXX Is the constructor too slow?
1117 std::poisson_distribution<result_type> __poisson(__y);
1118 return __poisson(__urng);
1121 template<typename _IntType>
1122 template<typename _UniformRandomNumberGenerator>
1123 typename negative_binomial_distribution<_IntType>::result_type
1124 negative_binomial_distribution<_IntType>::
1125 operator()(_UniformRandomNumberGenerator& __urng,
1126 const param_type& __p)
1128 typedef typename std::gamma_distribution<double>::param_type
1132 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1134 std::poisson_distribution<result_type> __poisson(__y);
1135 return __poisson(__urng);
1138 template<typename _IntType>
1139 template<typename _ForwardIterator,
1140 typename _UniformRandomNumberGenerator>
1142 negative_binomial_distribution<_IntType>::
1143 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1144 _UniformRandomNumberGenerator& __urng)
1146 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1149 const double __y = _M_gd(__urng);
1151 // XXX Is the constructor too slow?
1152 std::poisson_distribution<result_type> __poisson(__y);
1153 *__f++ = __poisson(__urng);
1157 template<typename _IntType>
1158 template<typename _ForwardIterator,
1159 typename _UniformRandomNumberGenerator>
1161 negative_binomial_distribution<_IntType>::
1162 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1163 _UniformRandomNumberGenerator& __urng,
1164 const param_type& __p)
1166 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1167 typename std::gamma_distribution<result_type>::param_type
1168 __p2(__p.k(), (1.0 - __p.p()) / __p.p());
1172 const double __y = _M_gd(__urng, __p2);
1174 std::poisson_distribution<result_type> __poisson(__y);
1175 *__f++ = __poisson(__urng);
1179 template<typename _IntType, typename _CharT, typename _Traits>
1180 std::basic_ostream<_CharT, _Traits>&
1181 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1182 const negative_binomial_distribution<_IntType>& __x)
1184 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1186 const typename __ios_base::fmtflags __flags = __os.flags();
1187 const _CharT __fill = __os.fill();
1188 const std::streamsize __precision = __os.precision();
1189 const _CharT __space = __os.widen(' ');
1190 __os.flags(__ios_base::scientific | __ios_base::left);
1191 __os.fill(__os.widen(' '));
1192 __os.precision(std::numeric_limits<double>::max_digits10);
1194 __os << __x.k() << __space << __x.p()
1195 << __space << __x._M_gd;
1197 __os.flags(__flags);
1199 __os.precision(__precision);
1203 template<typename _IntType, typename _CharT, typename _Traits>
1204 std::basic_istream<_CharT, _Traits>&
1205 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1206 negative_binomial_distribution<_IntType>& __x)
1209 = typename negative_binomial_distribution<_IntType>::param_type;
1210 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1212 const typename __ios_base::fmtflags __flags = __is.flags();
1213 __is.flags(__ios_base::skipws);
1217 if (__is >> __k >> __p >> __x._M_gd)
1218 __x.param(param_type(__k, __p));
1220 __is.flags(__flags);
1225 template<typename _IntType>
1227 poisson_distribution<_IntType>::param_type::
1230 #if _GLIBCXX_USE_C99_MATH_TR1
1233 const double __m = std::floor(_M_mean);
1234 _M_lm_thr = std::log(_M_mean);
1235 _M_lfm = std::lgamma(__m + 1);
1236 _M_sm = std::sqrt(__m);
1238 const double __pi_4 = 0.7853981633974483096156608458198757L;
1239 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1241 _M_d = std::round(std::max<double>(6.0, std::min(__m, __dx)));
1242 const double __cx = 2 * __m + _M_d;
1243 _M_scx = std::sqrt(__cx / 2);
1246 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1247 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1252 _M_lm_thr = std::exp(-_M_mean);
1256 * A rejection algorithm when mean >= 12 and a simple method based
1257 * upon the multiplication of uniform random variates otherwise.
1258 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1262 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1263 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1265 template<typename _IntType>
1266 template<typename _UniformRandomNumberGenerator>
1267 typename poisson_distribution<_IntType>::result_type
1268 poisson_distribution<_IntType>::
1269 operator()(_UniformRandomNumberGenerator& __urng,
1270 const param_type& __param)
1272 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1274 #if _GLIBCXX_USE_C99_MATH_TR1
1275 if (__param.mean() >= 12)
1279 // See comments above...
1280 const double __naf =
1281 (1 - std::numeric_limits<double>::epsilon()) / 2;
1282 const double __thr =
1283 std::numeric_limits<_IntType>::max() + __naf;
1285 const double __m = std::floor(__param.mean());
1287 const double __spi_2 = 1.2533141373155002512078826424055226L;
1288 const double __c1 = __param._M_sm * __spi_2;
1289 const double __c2 = __param._M_c2b + __c1;
1290 const double __c3 = __c2 + 1;
1291 const double __c4 = __c3 + 1;
1293 const double __178 = 0.0128205128205128205128205128205128L;
1295 const double __e178 = 1.0129030479320018583185514777512983L;
1296 const double __c5 = __c4 + __e178;
1297 const double __c = __param._M_cb + __c5;
1298 const double __2cx = 2 * (2 * __m + __param._M_d);
1300 bool __reject = true;
1303 const double __u = __c * __aurng();
1304 const double __e = -std::log(1.0 - __aurng());
1310 const double __n = _M_nd(__urng);
1311 const double __y = -std::abs(__n) * __param._M_sm - 1;
1312 __x = std::floor(__y);
1313 __w = -__n * __n / 2;
1317 else if (__u <= __c2)
1319 const double __n = _M_nd(__urng);
1320 const double __y = 1 + std::abs(__n) * __param._M_scx;
1321 __x = std::ceil(__y);
1322 __w = __y * (2 - __y) * __param._M_1cx;
1323 if (__x > __param._M_d)
1326 else if (__u <= __c3)
1327 // NB: This case not in the book, nor in the Errata,
1328 // but should be ok...
1330 else if (__u <= __c4)
1332 else if (__u <= __c5)
1335 // Only in the Errata, see libstdc++/83237.
1340 const double __v = -std::log(1.0 - __aurng());
1341 const double __y = __param._M_d
1342 + __v * __2cx / __param._M_d;
1343 __x = std::ceil(__y);
1344 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1347 __reject = (__w - __e - __x * __param._M_lm_thr
1348 > __param._M_lfm - std::lgamma(__x + __m + 1));
1350 __reject |= __x + __m >= __thr;
1354 return result_type(__x + __m + __naf);
1360 double __prod = 1.0;
1364 __prod *= __aurng();
1367 while (__prod > __param._M_lm_thr);
1373 template<typename _IntType>
1374 template<typename _ForwardIterator,
1375 typename _UniformRandomNumberGenerator>
1377 poisson_distribution<_IntType>::
1378 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1379 _UniformRandomNumberGenerator& __urng,
1380 const param_type& __param)
1382 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1383 // We could duplicate everything from operator()...
1385 *__f++ = this->operator()(__urng, __param);
1388 template<typename _IntType,
1389 typename _CharT, typename _Traits>
1390 std::basic_ostream<_CharT, _Traits>&
1391 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1392 const poisson_distribution<_IntType>& __x)
1394 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1396 const typename __ios_base::fmtflags __flags = __os.flags();
1397 const _CharT __fill = __os.fill();
1398 const std::streamsize __precision = __os.precision();
1399 const _CharT __space = __os.widen(' ');
1400 __os.flags(__ios_base::scientific | __ios_base::left);
1402 __os.precision(std::numeric_limits<double>::max_digits10);
1404 __os << __x.mean() << __space << __x._M_nd;
1406 __os.flags(__flags);
1408 __os.precision(__precision);
1412 template<typename _IntType,
1413 typename _CharT, typename _Traits>
1414 std::basic_istream<_CharT, _Traits>&
1415 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1416 poisson_distribution<_IntType>& __x)
1418 using param_type = typename poisson_distribution<_IntType>::param_type;
1419 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1421 const typename __ios_base::fmtflags __flags = __is.flags();
1422 __is.flags(__ios_base::skipws);
1425 if (__is >> __mean >> __x._M_nd)
1426 __x.param(param_type(__mean));
1428 __is.flags(__flags);
1433 template<typename _IntType>
1435 binomial_distribution<_IntType>::param_type::
1438 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1442 #if _GLIBCXX_USE_C99_MATH_TR1
1443 if (_M_t * __p12 >= 8)
1446 const double __np = std::floor(_M_t * __p12);
1447 const double __pa = __np / _M_t;
1448 const double __1p = 1 - __pa;
1450 const double __pi_4 = 0.7853981633974483096156608458198757L;
1451 const double __d1x =
1452 std::sqrt(__np * __1p * std::log(32 * __np
1453 / (81 * __pi_4 * __1p)));
1454 _M_d1 = std::round(std::max<double>(1.0, __d1x));
1455 const double __d2x =
1456 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1457 / (__pi_4 * __pa)));
1458 _M_d2 = std::round(std::max<double>(1.0, __d2x));
1461 const double __spi_2 = 1.2533141373155002512078826424055226L;
1462 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1463 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1464 _M_c = 2 * _M_d1 / __np;
1465 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1466 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1467 const double __s1s = _M_s1 * _M_s1;
1468 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1470 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1471 const double __s2s = _M_s2 * _M_s2;
1472 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1473 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1474 _M_lf = (std::lgamma(__np + 1)
1475 + std::lgamma(_M_t - __np + 1));
1476 _M_lp1p = std::log(__pa / __1p);
1478 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1482 _M_q = -std::log(1 - __p12);
1485 template<typename _IntType>
1486 template<typename _UniformRandomNumberGenerator>
1487 typename binomial_distribution<_IntType>::result_type
1488 binomial_distribution<_IntType>::
1489 _M_waiting(_UniformRandomNumberGenerator& __urng,
1490 _IntType __t, double __q)
1494 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1501 const double __e = -std::log(1.0 - __aurng());
1502 __sum += __e / (__t - __x);
1505 while (__sum <= __q);
1511 * A rejection algorithm when t * p >= 8 and a simple waiting time
1512 * method - the second in the referenced book - otherwise.
1513 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1517 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1518 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1520 template<typename _IntType>
1521 template<typename _UniformRandomNumberGenerator>
1522 typename binomial_distribution<_IntType>::result_type
1523 binomial_distribution<_IntType>::
1524 operator()(_UniformRandomNumberGenerator& __urng,
1525 const param_type& __param)
1528 const _IntType __t = __param.t();
1529 const double __p = __param.p();
1530 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1531 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1534 #if _GLIBCXX_USE_C99_MATH_TR1
1535 if (!__param._M_easy)
1539 // See comments above...
1540 const double __naf =
1541 (1 - std::numeric_limits<double>::epsilon()) / 2;
1542 const double __thr =
1543 std::numeric_limits<_IntType>::max() + __naf;
1545 const double __np = std::floor(__t * __p12);
1548 const double __spi_2 = 1.2533141373155002512078826424055226L;
1549 const double __a1 = __param._M_a1;
1550 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1551 const double __a123 = __param._M_a123;
1552 const double __s1s = __param._M_s1 * __param._M_s1;
1553 const double __s2s = __param._M_s2 * __param._M_s2;
1558 const double __u = __param._M_s * __aurng();
1564 const double __n = _M_nd(__urng);
1565 const double __y = __param._M_s1 * std::abs(__n);
1566 __reject = __y >= __param._M_d1;
1569 const double __e = -std::log(1.0 - __aurng());
1570 __x = std::floor(__y);
1571 __v = -__e - __n * __n / 2 + __param._M_c;
1574 else if (__u <= __a12)
1576 const double __n = _M_nd(__urng);
1577 const double __y = __param._M_s2 * std::abs(__n);
1578 __reject = __y >= __param._M_d2;
1581 const double __e = -std::log(1.0 - __aurng());
1582 __x = std::floor(-__y);
1583 __v = -__e - __n * __n / 2;
1586 else if (__u <= __a123)
1588 const double __e1 = -std::log(1.0 - __aurng());
1589 const double __e2 = -std::log(1.0 - __aurng());
1591 const double __y = __param._M_d1
1592 + 2 * __s1s * __e1 / __param._M_d1;
1593 __x = std::floor(__y);
1594 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1595 -__y / (2 * __s1s)));
1600 const double __e1 = -std::log(1.0 - __aurng());
1601 const double __e2 = -std::log(1.0 - __aurng());
1603 const double __y = __param._M_d2
1604 + 2 * __s2s * __e1 / __param._M_d2;
1605 __x = std::floor(-__y);
1606 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1610 __reject = __reject || __x < -__np || __x > __t - __np;
1613 const double __lfx =
1614 std::lgamma(__np + __x + 1)
1615 + std::lgamma(__t - (__np + __x) + 1);
1616 __reject = __v > __param._M_lf - __lfx
1617 + __x * __param._M_lp1p;
1620 __reject |= __x + __np >= __thr;
1624 __x += __np + __naf;
1626 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x),
1628 __ret = _IntType(__x) + __z;
1632 __ret = _M_waiting(__urng, __t, __param._M_q);
1635 __ret = __t - __ret;
1639 template<typename _IntType>
1640 template<typename _ForwardIterator,
1641 typename _UniformRandomNumberGenerator>
1643 binomial_distribution<_IntType>::
1644 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1645 _UniformRandomNumberGenerator& __urng,
1646 const param_type& __param)
1648 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1649 // We could duplicate everything from operator()...
1651 *__f++ = this->operator()(__urng, __param);
1654 template<typename _IntType,
1655 typename _CharT, typename _Traits>
1656 std::basic_ostream<_CharT, _Traits>&
1657 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1658 const binomial_distribution<_IntType>& __x)
1660 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1662 const typename __ios_base::fmtflags __flags = __os.flags();
1663 const _CharT __fill = __os.fill();
1664 const std::streamsize __precision = __os.precision();
1665 const _CharT __space = __os.widen(' ');
1666 __os.flags(__ios_base::scientific | __ios_base::left);
1668 __os.precision(std::numeric_limits<double>::max_digits10);
1670 __os << __x.t() << __space << __x.p()
1671 << __space << __x._M_nd;
1673 __os.flags(__flags);
1675 __os.precision(__precision);
1679 template<typename _IntType,
1680 typename _CharT, typename _Traits>
1681 std::basic_istream<_CharT, _Traits>&
1682 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1683 binomial_distribution<_IntType>& __x)
1685 using param_type = typename binomial_distribution<_IntType>::param_type;
1686 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1688 const typename __ios_base::fmtflags __flags = __is.flags();
1689 __is.flags(__ios_base::dec | __ios_base::skipws);
1693 if (__is >> __t >> __p >> __x._M_nd)
1694 __x.param(param_type(__t, __p));
1696 __is.flags(__flags);
1701 template<typename _RealType>
1702 template<typename _ForwardIterator,
1703 typename _UniformRandomNumberGenerator>
1705 std::exponential_distribution<_RealType>::
1706 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1707 _UniformRandomNumberGenerator& __urng,
1708 const param_type& __p)
1710 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1711 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1714 *__f++ = -std::log(result_type(1) - __aurng()) / __p.lambda();
1717 template<typename _RealType, typename _CharT, typename _Traits>
1718 std::basic_ostream<_CharT, _Traits>&
1719 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1720 const exponential_distribution<_RealType>& __x)
1722 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1724 const typename __ios_base::fmtflags __flags = __os.flags();
1725 const _CharT __fill = __os.fill();
1726 const std::streamsize __precision = __os.precision();
1727 __os.flags(__ios_base::scientific | __ios_base::left);
1728 __os.fill(__os.widen(' '));
1729 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1731 __os << __x.lambda();
1733 __os.flags(__flags);
1735 __os.precision(__precision);
1739 template<typename _RealType, typename _CharT, typename _Traits>
1740 std::basic_istream<_CharT, _Traits>&
1741 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1742 exponential_distribution<_RealType>& __x)
1745 = typename exponential_distribution<_RealType>::param_type;
1746 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1748 const typename __ios_base::fmtflags __flags = __is.flags();
1749 __is.flags(__ios_base::dec | __ios_base::skipws);
1752 if (__is >> __lambda)
1753 __x.param(param_type(__lambda));
1755 __is.flags(__flags);
1761 * Polar method due to Marsaglia.
1763 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1764 * New York, 1986, Ch. V, Sect. 4.4.
1766 template<typename _RealType>
1767 template<typename _UniformRandomNumberGenerator>
1768 typename normal_distribution<_RealType>::result_type
1769 normal_distribution<_RealType>::
1770 operator()(_UniformRandomNumberGenerator& __urng,
1771 const param_type& __param)
1774 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1777 if (_M_saved_available)
1779 _M_saved_available = false;
1784 result_type __x, __y, __r2;
1787 __x = result_type(2.0) * __aurng() - 1.0;
1788 __y = result_type(2.0) * __aurng() - 1.0;
1789 __r2 = __x * __x + __y * __y;
1791 while (__r2 > 1.0 || __r2 == 0.0);
1793 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1794 _M_saved = __x * __mult;
1795 _M_saved_available = true;
1796 __ret = __y * __mult;
1799 __ret = __ret * __param.stddev() + __param.mean();
1803 template<typename _RealType>
1804 template<typename _ForwardIterator,
1805 typename _UniformRandomNumberGenerator>
1807 normal_distribution<_RealType>::
1808 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1809 _UniformRandomNumberGenerator& __urng,
1810 const param_type& __param)
1812 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1817 if (_M_saved_available)
1819 _M_saved_available = false;
1820 *__f++ = _M_saved * __param.stddev() + __param.mean();
1826 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1829 while (__f + 1 < __t)
1831 result_type __x, __y, __r2;
1834 __x = result_type(2.0) * __aurng() - 1.0;
1835 __y = result_type(2.0) * __aurng() - 1.0;
1836 __r2 = __x * __x + __y * __y;
1838 while (__r2 > 1.0 || __r2 == 0.0);
1840 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1841 *__f++ = __y * __mult * __param.stddev() + __param.mean();
1842 *__f++ = __x * __mult * __param.stddev() + __param.mean();
1847 result_type __x, __y, __r2;
1850 __x = result_type(2.0) * __aurng() - 1.0;
1851 __y = result_type(2.0) * __aurng() - 1.0;
1852 __r2 = __x * __x + __y * __y;
1854 while (__r2 > 1.0 || __r2 == 0.0);
1856 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1857 _M_saved = __x * __mult;
1858 _M_saved_available = true;
1859 *__f = __y * __mult * __param.stddev() + __param.mean();
1863 template<typename _RealType>
1865 operator==(const std::normal_distribution<_RealType>& __d1,
1866 const std::normal_distribution<_RealType>& __d2)
1868 if (__d1._M_param == __d2._M_param
1869 && __d1._M_saved_available == __d2._M_saved_available)
1871 if (__d1._M_saved_available
1872 && __d1._M_saved == __d2._M_saved)
1874 else if(!__d1._M_saved_available)
1883 template<typename _RealType, typename _CharT, typename _Traits>
1884 std::basic_ostream<_CharT, _Traits>&
1885 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1886 const normal_distribution<_RealType>& __x)
1888 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1890 const typename __ios_base::fmtflags __flags = __os.flags();
1891 const _CharT __fill = __os.fill();
1892 const std::streamsize __precision = __os.precision();
1893 const _CharT __space = __os.widen(' ');
1894 __os.flags(__ios_base::scientific | __ios_base::left);
1896 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1898 __os << __x.mean() << __space << __x.stddev()
1899 << __space << __x._M_saved_available;
1900 if (__x._M_saved_available)
1901 __os << __space << __x._M_saved;
1903 __os.flags(__flags);
1905 __os.precision(__precision);
1909 template<typename _RealType, typename _CharT, typename _Traits>
1910 std::basic_istream<_CharT, _Traits>&
1911 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1912 normal_distribution<_RealType>& __x)
1914 using param_type = typename normal_distribution<_RealType>::param_type;
1915 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1917 const typename __ios_base::fmtflags __flags = __is.flags();
1918 __is.flags(__ios_base::dec | __ios_base::skipws);
1920 double __mean, __stddev;
1922 if (__is >> __mean >> __stddev >> __saved_avail)
1924 if (__saved_avail && (__is >> __x._M_saved))
1926 __x._M_saved_available = __saved_avail;
1927 __x.param(param_type(__mean, __stddev));
1931 __is.flags(__flags);
1936 template<typename _RealType>
1937 template<typename _ForwardIterator,
1938 typename _UniformRandomNumberGenerator>
1940 lognormal_distribution<_RealType>::
1941 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1942 _UniformRandomNumberGenerator& __urng,
1943 const param_type& __p)
1945 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1947 *__f++ = std::exp(__p.s() * _M_nd(__urng) + __p.m());
1950 template<typename _RealType, typename _CharT, typename _Traits>
1951 std::basic_ostream<_CharT, _Traits>&
1952 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1953 const lognormal_distribution<_RealType>& __x)
1955 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1957 const typename __ios_base::fmtflags __flags = __os.flags();
1958 const _CharT __fill = __os.fill();
1959 const std::streamsize __precision = __os.precision();
1960 const _CharT __space = __os.widen(' ');
1961 __os.flags(__ios_base::scientific | __ios_base::left);
1963 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1965 __os << __x.m() << __space << __x.s()
1966 << __space << __x._M_nd;
1968 __os.flags(__flags);
1970 __os.precision(__precision);
1974 template<typename _RealType, typename _CharT, typename _Traits>
1975 std::basic_istream<_CharT, _Traits>&
1976 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1977 lognormal_distribution<_RealType>& __x)
1980 = typename lognormal_distribution<_RealType>::param_type;
1981 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1983 const typename __ios_base::fmtflags __flags = __is.flags();
1984 __is.flags(__ios_base::dec | __ios_base::skipws);
1987 if (__is >> __m >> __s >> __x._M_nd)
1988 __x.param(param_type(__m, __s));
1990 __is.flags(__flags);
1994 template<typename _RealType>
1995 template<typename _ForwardIterator,
1996 typename _UniformRandomNumberGenerator>
1998 std::chi_squared_distribution<_RealType>::
1999 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2000 _UniformRandomNumberGenerator& __urng)
2002 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2004 *__f++ = 2 * _M_gd(__urng);
2007 template<typename _RealType>
2008 template<typename _ForwardIterator,
2009 typename _UniformRandomNumberGenerator>
2011 std::chi_squared_distribution<_RealType>::
2012 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2013 _UniformRandomNumberGenerator& __urng,
2015 std::gamma_distribution<result_type>::param_type& __p)
2017 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2019 *__f++ = 2 * _M_gd(__urng, __p);
2022 template<typename _RealType, typename _CharT, typename _Traits>
2023 std::basic_ostream<_CharT, _Traits>&
2024 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2025 const chi_squared_distribution<_RealType>& __x)
2027 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2029 const typename __ios_base::fmtflags __flags = __os.flags();
2030 const _CharT __fill = __os.fill();
2031 const std::streamsize __precision = __os.precision();
2032 const _CharT __space = __os.widen(' ');
2033 __os.flags(__ios_base::scientific | __ios_base::left);
2035 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2037 __os << __x.n() << __space << __x._M_gd;
2039 __os.flags(__flags);
2041 __os.precision(__precision);
2045 template<typename _RealType, typename _CharT, typename _Traits>
2046 std::basic_istream<_CharT, _Traits>&
2047 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2048 chi_squared_distribution<_RealType>& __x)
2051 = typename chi_squared_distribution<_RealType>::param_type;
2052 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2054 const typename __ios_base::fmtflags __flags = __is.flags();
2055 __is.flags(__ios_base::dec | __ios_base::skipws);
2058 if (__is >> __n >> __x._M_gd)
2059 __x.param(param_type(__n));
2061 __is.flags(__flags);
2066 template<typename _RealType>
2067 template<typename _UniformRandomNumberGenerator>
2068 typename cauchy_distribution<_RealType>::result_type
2069 cauchy_distribution<_RealType>::
2070 operator()(_UniformRandomNumberGenerator& __urng,
2071 const param_type& __p)
2073 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2080 const _RealType __pi = 3.1415926535897932384626433832795029L;
2081 return __p.a() + __p.b() * std::tan(__pi * __u);
2084 template<typename _RealType>
2085 template<typename _ForwardIterator,
2086 typename _UniformRandomNumberGenerator>
2088 cauchy_distribution<_RealType>::
2089 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2090 _UniformRandomNumberGenerator& __urng,
2091 const param_type& __p)
2093 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2094 const _RealType __pi = 3.1415926535897932384626433832795029L;
2095 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2104 *__f++ = __p.a() + __p.b() * std::tan(__pi * __u);
2108 template<typename _RealType, typename _CharT, typename _Traits>
2109 std::basic_ostream<_CharT, _Traits>&
2110 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2111 const cauchy_distribution<_RealType>& __x)
2113 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2115 const typename __ios_base::fmtflags __flags = __os.flags();
2116 const _CharT __fill = __os.fill();
2117 const std::streamsize __precision = __os.precision();
2118 const _CharT __space = __os.widen(' ');
2119 __os.flags(__ios_base::scientific | __ios_base::left);
2121 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2123 __os << __x.a() << __space << __x.b();
2125 __os.flags(__flags);
2127 __os.precision(__precision);
2131 template<typename _RealType, typename _CharT, typename _Traits>
2132 std::basic_istream<_CharT, _Traits>&
2133 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2134 cauchy_distribution<_RealType>& __x)
2136 using param_type = typename cauchy_distribution<_RealType>::param_type;
2137 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2139 const typename __ios_base::fmtflags __flags = __is.flags();
2140 __is.flags(__ios_base::dec | __ios_base::skipws);
2143 if (__is >> __a >> __b)
2144 __x.param(param_type(__a, __b));
2146 __is.flags(__flags);
2151 template<typename _RealType>
2152 template<typename _ForwardIterator,
2153 typename _UniformRandomNumberGenerator>
2155 std::fisher_f_distribution<_RealType>::
2156 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2157 _UniformRandomNumberGenerator& __urng)
2159 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2161 *__f++ = ((_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()));
2164 template<typename _RealType>
2165 template<typename _ForwardIterator,
2166 typename _UniformRandomNumberGenerator>
2168 std::fisher_f_distribution<_RealType>::
2169 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2170 _UniformRandomNumberGenerator& __urng,
2171 const param_type& __p)
2173 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2174 typedef typename std::gamma_distribution<result_type>::param_type
2176 param_type __p1(__p.m() / 2);
2177 param_type __p2(__p.n() / 2);
2179 *__f++ = ((_M_gd_x(__urng, __p1) * n())
2180 / (_M_gd_y(__urng, __p2) * m()));
2183 template<typename _RealType, typename _CharT, typename _Traits>
2184 std::basic_ostream<_CharT, _Traits>&
2185 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2186 const fisher_f_distribution<_RealType>& __x)
2188 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2190 const typename __ios_base::fmtflags __flags = __os.flags();
2191 const _CharT __fill = __os.fill();
2192 const std::streamsize __precision = __os.precision();
2193 const _CharT __space = __os.widen(' ');
2194 __os.flags(__ios_base::scientific | __ios_base::left);
2196 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2198 __os << __x.m() << __space << __x.n()
2199 << __space << __x._M_gd_x << __space << __x._M_gd_y;
2201 __os.flags(__flags);
2203 __os.precision(__precision);
2207 template<typename _RealType, typename _CharT, typename _Traits>
2208 std::basic_istream<_CharT, _Traits>&
2209 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2210 fisher_f_distribution<_RealType>& __x)
2213 = typename fisher_f_distribution<_RealType>::param_type;
2214 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2216 const typename __ios_base::fmtflags __flags = __is.flags();
2217 __is.flags(__ios_base::dec | __ios_base::skipws);
2220 if (__is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y)
2221 __x.param(param_type(__m, __n));
2223 __is.flags(__flags);
2228 template<typename _RealType>
2229 template<typename _ForwardIterator,
2230 typename _UniformRandomNumberGenerator>
2232 std::student_t_distribution<_RealType>::
2233 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2234 _UniformRandomNumberGenerator& __urng)
2236 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2238 *__f++ = _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng));
2241 template<typename _RealType>
2242 template<typename _ForwardIterator,
2243 typename _UniformRandomNumberGenerator>
2245 std::student_t_distribution<_RealType>::
2246 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2247 _UniformRandomNumberGenerator& __urng,
2248 const param_type& __p)
2250 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2251 typename std::gamma_distribution<result_type>::param_type
2252 __p2(__p.n() / 2, 2);
2254 *__f++ = _M_nd(__urng) * std::sqrt(__p.n() / _M_gd(__urng, __p2));
2257 template<typename _RealType, typename _CharT, typename _Traits>
2258 std::basic_ostream<_CharT, _Traits>&
2259 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2260 const student_t_distribution<_RealType>& __x)
2262 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2264 const typename __ios_base::fmtflags __flags = __os.flags();
2265 const _CharT __fill = __os.fill();
2266 const std::streamsize __precision = __os.precision();
2267 const _CharT __space = __os.widen(' ');
2268 __os.flags(__ios_base::scientific | __ios_base::left);
2270 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2272 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
2274 __os.flags(__flags);
2276 __os.precision(__precision);
2280 template<typename _RealType, typename _CharT, typename _Traits>
2281 std::basic_istream<_CharT, _Traits>&
2282 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2283 student_t_distribution<_RealType>& __x)
2286 = typename student_t_distribution<_RealType>::param_type;
2287 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2289 const typename __ios_base::fmtflags __flags = __is.flags();
2290 __is.flags(__ios_base::dec | __ios_base::skipws);
2293 if (__is >> __n >> __x._M_nd >> __x._M_gd)
2294 __x.param(param_type(__n));
2296 __is.flags(__flags);
2301 template<typename _RealType>
2303 gamma_distribution<_RealType>::param_type::
2306 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2308 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2309 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2313 * Marsaglia, G. and Tsang, W. W.
2314 * "A Simple Method for Generating Gamma Variables"
2315 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2317 template<typename _RealType>
2318 template<typename _UniformRandomNumberGenerator>
2319 typename gamma_distribution<_RealType>::result_type
2320 gamma_distribution<_RealType>::
2321 operator()(_UniformRandomNumberGenerator& __urng,
2322 const param_type& __param)
2324 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2327 result_type __u, __v, __n;
2328 const result_type __a1 = (__param._M_malpha
2329 - _RealType(1.0) / _RealType(3.0));
2335 __n = _M_nd(__urng);
2336 __v = result_type(1.0) + __param._M_a2 * __n;
2340 __v = __v * __v * __v;
2343 while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
2344 && (std::log(__u) > (0.5 * __n * __n + __a1
2345 * (1.0 - __v + std::log(__v)))));
2347 if (__param.alpha() == __param._M_malpha)
2348 return __a1 * __v * __param.beta();
2355 return (std::pow(__u, result_type(1.0) / __param.alpha())
2356 * __a1 * __v * __param.beta());
2360 template<typename _RealType>
2361 template<typename _ForwardIterator,
2362 typename _UniformRandomNumberGenerator>
2364 gamma_distribution<_RealType>::
2365 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2366 _UniformRandomNumberGenerator& __urng,
2367 const param_type& __param)
2369 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2370 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2373 result_type __u, __v, __n;
2374 const result_type __a1 = (__param._M_malpha
2375 - _RealType(1.0) / _RealType(3.0));
2377 if (__param.alpha() == __param._M_malpha)
2384 __n = _M_nd(__urng);
2385 __v = result_type(1.0) + __param._M_a2 * __n;
2389 __v = __v * __v * __v;
2392 while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
2393 && (std::log(__u) > (0.5 * __n * __n + __a1
2394 * (1.0 - __v + std::log(__v)))));
2396 *__f++ = __a1 * __v * __param.beta();
2405 __n = _M_nd(__urng);
2406 __v = result_type(1.0) + __param._M_a2 * __n;
2410 __v = __v * __v * __v;
2413 while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
2414 && (std::log(__u) > (0.5 * __n * __n + __a1
2415 * (1.0 - __v + std::log(__v)))));
2421 *__f++ = (std::pow(__u, result_type(1.0) / __param.alpha())
2422 * __a1 * __v * __param.beta());
2426 template<typename _RealType, typename _CharT, typename _Traits>
2427 std::basic_ostream<_CharT, _Traits>&
2428 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2429 const gamma_distribution<_RealType>& __x)
2431 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2433 const typename __ios_base::fmtflags __flags = __os.flags();
2434 const _CharT __fill = __os.fill();
2435 const std::streamsize __precision = __os.precision();
2436 const _CharT __space = __os.widen(' ');
2437 __os.flags(__ios_base::scientific | __ios_base::left);
2439 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2441 __os << __x.alpha() << __space << __x.beta()
2442 << __space << __x._M_nd;
2444 __os.flags(__flags);
2446 __os.precision(__precision);
2450 template<typename _RealType, typename _CharT, typename _Traits>
2451 std::basic_istream<_CharT, _Traits>&
2452 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2453 gamma_distribution<_RealType>& __x)
2455 using param_type = typename gamma_distribution<_RealType>::param_type;
2456 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2458 const typename __ios_base::fmtflags __flags = __is.flags();
2459 __is.flags(__ios_base::dec | __ios_base::skipws);
2461 _RealType __alpha_val, __beta_val;
2462 if (__is >> __alpha_val >> __beta_val >> __x._M_nd)
2463 __x.param(param_type(__alpha_val, __beta_val));
2465 __is.flags(__flags);
2470 template<typename _RealType>
2471 template<typename _UniformRandomNumberGenerator>
2472 typename weibull_distribution<_RealType>::result_type
2473 weibull_distribution<_RealType>::
2474 operator()(_UniformRandomNumberGenerator& __urng,
2475 const param_type& __p)
2477 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2479 return __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
2480 result_type(1) / __p.a());
2483 template<typename _RealType>
2484 template<typename _ForwardIterator,
2485 typename _UniformRandomNumberGenerator>
2487 weibull_distribution<_RealType>::
2488 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2489 _UniformRandomNumberGenerator& __urng,
2490 const param_type& __p)
2492 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2493 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2495 auto __inv_a = result_type(1) / __p.a();
2498 *__f++ = __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
2502 template<typename _RealType, typename _CharT, typename _Traits>
2503 std::basic_ostream<_CharT, _Traits>&
2504 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2505 const weibull_distribution<_RealType>& __x)
2507 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2509 const typename __ios_base::fmtflags __flags = __os.flags();
2510 const _CharT __fill = __os.fill();
2511 const std::streamsize __precision = __os.precision();
2512 const _CharT __space = __os.widen(' ');
2513 __os.flags(__ios_base::scientific | __ios_base::left);
2515 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2517 __os << __x.a() << __space << __x.b();
2519 __os.flags(__flags);
2521 __os.precision(__precision);
2525 template<typename _RealType, typename _CharT, typename _Traits>
2526 std::basic_istream<_CharT, _Traits>&
2527 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2528 weibull_distribution<_RealType>& __x)
2530 using param_type = typename weibull_distribution<_RealType>::param_type;
2531 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2533 const typename __ios_base::fmtflags __flags = __is.flags();
2534 __is.flags(__ios_base::dec | __ios_base::skipws);
2537 if (__is >> __a >> __b)
2538 __x.param(param_type(__a, __b));
2540 __is.flags(__flags);
2545 template<typename _RealType>
2546 template<typename _UniformRandomNumberGenerator>
2547 typename extreme_value_distribution<_RealType>::result_type
2548 extreme_value_distribution<_RealType>::
2549 operator()(_UniformRandomNumberGenerator& __urng,
2550 const param_type& __p)
2552 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2554 return __p.a() - __p.b() * std::log(-std::log(result_type(1)
2558 template<typename _RealType>
2559 template<typename _ForwardIterator,
2560 typename _UniformRandomNumberGenerator>
2562 extreme_value_distribution<_RealType>::
2563 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2564 _UniformRandomNumberGenerator& __urng,
2565 const param_type& __p)
2567 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2568 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2572 *__f++ = __p.a() - __p.b() * std::log(-std::log(result_type(1)
2576 template<typename _RealType, typename _CharT, typename _Traits>
2577 std::basic_ostream<_CharT, _Traits>&
2578 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2579 const extreme_value_distribution<_RealType>& __x)
2581 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2583 const typename __ios_base::fmtflags __flags = __os.flags();
2584 const _CharT __fill = __os.fill();
2585 const std::streamsize __precision = __os.precision();
2586 const _CharT __space = __os.widen(' ');
2587 __os.flags(__ios_base::scientific | __ios_base::left);
2589 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2591 __os << __x.a() << __space << __x.b();
2593 __os.flags(__flags);
2595 __os.precision(__precision);
2599 template<typename _RealType, typename _CharT, typename _Traits>
2600 std::basic_istream<_CharT, _Traits>&
2601 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2602 extreme_value_distribution<_RealType>& __x)
2605 = typename extreme_value_distribution<_RealType>::param_type;
2606 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2608 const typename __ios_base::fmtflags __flags = __is.flags();
2609 __is.flags(__ios_base::dec | __ios_base::skipws);
2612 if (__is >> __a >> __b)
2613 __x.param(param_type(__a, __b));
2615 __is.flags(__flags);
2620 template<typename _IntType>
2622 discrete_distribution<_IntType>::param_type::
2625 if (_M_prob.size() < 2)
2631 const double __sum = std::accumulate(_M_prob.begin(),
2632 _M_prob.end(), 0.0);
2633 __glibcxx_assert(__sum > 0);
2634 // Now normalize the probabilites.
2635 __detail::__normalize(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2637 // Accumulate partial sums.
2638 _M_cp.reserve(_M_prob.size());
2639 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2640 std::back_inserter(_M_cp));
2641 // Make sure the last cumulative probability is one.
2642 _M_cp[_M_cp.size() - 1] = 1.0;
2645 template<typename _IntType>
2646 template<typename _Func>
2647 discrete_distribution<_IntType>::param_type::
2648 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2649 : _M_prob(), _M_cp()
2651 const size_t __n = __nw == 0 ? 1 : __nw;
2652 const double __delta = (__xmax - __xmin) / __n;
2654 _M_prob.reserve(__n);
2655 for (size_t __k = 0; __k < __nw; ++__k)
2656 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2661 template<typename _IntType>
2662 template<typename _UniformRandomNumberGenerator>
2663 typename discrete_distribution<_IntType>::result_type
2664 discrete_distribution<_IntType>::
2665 operator()(_UniformRandomNumberGenerator& __urng,
2666 const param_type& __param)
2668 if (__param._M_cp.empty())
2669 return result_type(0);
2671 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2674 const double __p = __aurng();
2675 auto __pos = std::lower_bound(__param._M_cp.begin(),
2676 __param._M_cp.end(), __p);
2678 return __pos - __param._M_cp.begin();
2681 template<typename _IntType>
2682 template<typename _ForwardIterator,
2683 typename _UniformRandomNumberGenerator>
2685 discrete_distribution<_IntType>::
2686 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2687 _UniformRandomNumberGenerator& __urng,
2688 const param_type& __param)
2690 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2692 if (__param._M_cp.empty())
2695 *__f++ = result_type(0);
2699 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2704 const double __p = __aurng();
2705 auto __pos = std::lower_bound(__param._M_cp.begin(),
2706 __param._M_cp.end(), __p);
2708 *__f++ = __pos - __param._M_cp.begin();
2712 template<typename _IntType, typename _CharT, typename _Traits>
2713 std::basic_ostream<_CharT, _Traits>&
2714 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2715 const discrete_distribution<_IntType>& __x)
2717 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2719 const typename __ios_base::fmtflags __flags = __os.flags();
2720 const _CharT __fill = __os.fill();
2721 const std::streamsize __precision = __os.precision();
2722 const _CharT __space = __os.widen(' ');
2723 __os.flags(__ios_base::scientific | __ios_base::left);
2725 __os.precision(std::numeric_limits<double>::max_digits10);
2727 std::vector<double> __prob = __x.probabilities();
2728 __os << __prob.size();
2729 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2730 __os << __space << *__dit;
2732 __os.flags(__flags);
2734 __os.precision(__precision);
2740 template<typename _ValT, typename _CharT, typename _Traits>
2741 basic_istream<_CharT, _Traits>&
2742 __extract_params(basic_istream<_CharT, _Traits>& __is,
2743 vector<_ValT>& __vals, size_t __n)
2745 __vals.reserve(__n);
2750 __vals.push_back(__val);
2756 } // namespace __detail
2758 template<typename _IntType, typename _CharT, typename _Traits>
2759 std::basic_istream<_CharT, _Traits>&
2760 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2761 discrete_distribution<_IntType>& __x)
2763 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2765 const typename __ios_base::fmtflags __flags = __is.flags();
2766 __is.flags(__ios_base::dec | __ios_base::skipws);
2771 std::vector<double> __prob_vec;
2772 if (__detail::__extract_params(__is, __prob_vec, __n))
2773 __x.param({__prob_vec.begin(), __prob_vec.end()});
2776 __is.flags(__flags);
2781 template<typename _RealType>
2783 piecewise_constant_distribution<_RealType>::param_type::
2786 if (_M_int.size() < 2
2787 || (_M_int.size() == 2
2788 && _M_int[0] == _RealType(0)
2789 && _M_int[1] == _RealType(1)))
2796 const double __sum = std::accumulate(_M_den.begin(),
2798 __glibcxx_assert(__sum > 0);
2800 __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
2803 _M_cp.reserve(_M_den.size());
2804 std::partial_sum(_M_den.begin(), _M_den.end(),
2805 std::back_inserter(_M_cp));
2807 // Make sure the last cumulative probability is one.
2808 _M_cp[_M_cp.size() - 1] = 1.0;
2810 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2811 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2814 template<typename _RealType>
2815 template<typename _InputIteratorB, typename _InputIteratorW>
2816 piecewise_constant_distribution<_RealType>::param_type::
2817 param_type(_InputIteratorB __bbegin,
2818 _InputIteratorB __bend,
2819 _InputIteratorW __wbegin)
2820 : _M_int(), _M_den(), _M_cp()
2822 if (__bbegin != __bend)
2826 _M_int.push_back(*__bbegin);
2828 if (__bbegin == __bend)
2831 _M_den.push_back(*__wbegin);
2839 template<typename _RealType>
2840 template<typename _Func>
2841 piecewise_constant_distribution<_RealType>::param_type::
2842 param_type(initializer_list<_RealType> __bl, _Func __fw)
2843 : _M_int(), _M_den(), _M_cp()
2845 _M_int.reserve(__bl.size());
2846 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2847 _M_int.push_back(*__biter);
2849 _M_den.reserve(_M_int.size() - 1);
2850 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2851 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2856 template<typename _RealType>
2857 template<typename _Func>
2858 piecewise_constant_distribution<_RealType>::param_type::
2859 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2860 : _M_int(), _M_den(), _M_cp()
2862 const size_t __n = __nw == 0 ? 1 : __nw;
2863 const _RealType __delta = (__xmax - __xmin) / __n;
2865 _M_int.reserve(__n + 1);
2866 for (size_t __k = 0; __k <= __nw; ++__k)
2867 _M_int.push_back(__xmin + __k * __delta);
2869 _M_den.reserve(__n);
2870 for (size_t __k = 0; __k < __nw; ++__k)
2871 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2876 template<typename _RealType>
2877 template<typename _UniformRandomNumberGenerator>
2878 typename piecewise_constant_distribution<_RealType>::result_type
2879 piecewise_constant_distribution<_RealType>::
2880 operator()(_UniformRandomNumberGenerator& __urng,
2881 const param_type& __param)
2883 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2886 const double __p = __aurng();
2887 if (__param._M_cp.empty())
2890 auto __pos = std::lower_bound(__param._M_cp.begin(),
2891 __param._M_cp.end(), __p);
2892 const size_t __i = __pos - __param._M_cp.begin();
2894 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2896 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2899 template<typename _RealType>
2900 template<typename _ForwardIterator,
2901 typename _UniformRandomNumberGenerator>
2903 piecewise_constant_distribution<_RealType>::
2904 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2905 _UniformRandomNumberGenerator& __urng,
2906 const param_type& __param)
2908 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2909 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2912 if (__param._M_cp.empty())
2921 const double __p = __aurng();
2923 auto __pos = std::lower_bound(__param._M_cp.begin(),
2924 __param._M_cp.end(), __p);
2925 const size_t __i = __pos - __param._M_cp.begin();
2927 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2929 *__f++ = (__param._M_int[__i]
2930 + (__p - __pref) / __param._M_den[__i]);
2934 template<typename _RealType, typename _CharT, typename _Traits>
2935 std::basic_ostream<_CharT, _Traits>&
2936 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2937 const piecewise_constant_distribution<_RealType>& __x)
2939 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2941 const typename __ios_base::fmtflags __flags = __os.flags();
2942 const _CharT __fill = __os.fill();
2943 const std::streamsize __precision = __os.precision();
2944 const _CharT __space = __os.widen(' ');
2945 __os.flags(__ios_base::scientific | __ios_base::left);
2947 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2949 std::vector<_RealType> __int = __x.intervals();
2950 __os << __int.size() - 1;
2952 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2953 __os << __space << *__xit;
2955 std::vector<double> __den = __x.densities();
2956 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2957 __os << __space << *__dit;
2959 __os.flags(__flags);
2961 __os.precision(__precision);
2965 template<typename _RealType, typename _CharT, typename _Traits>
2966 std::basic_istream<_CharT, _Traits>&
2967 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2968 piecewise_constant_distribution<_RealType>& __x)
2970 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2972 const typename __ios_base::fmtflags __flags = __is.flags();
2973 __is.flags(__ios_base::dec | __ios_base::skipws);
2978 std::vector<_RealType> __int_vec;
2979 if (__detail::__extract_params(__is, __int_vec, __n + 1))
2981 std::vector<double> __den_vec;
2982 if (__detail::__extract_params(__is, __den_vec, __n))
2984 __x.param({ __int_vec.begin(), __int_vec.end(),
2985 __den_vec.begin() });
2990 __is.flags(__flags);
2995 template<typename _RealType>
2997 piecewise_linear_distribution<_RealType>::param_type::
3000 if (_M_int.size() < 2
3001 || (_M_int.size() == 2
3002 && _M_int[0] == _RealType(0)
3003 && _M_int[1] == _RealType(1)
3004 && _M_den[0] == _M_den[1]))
3012 _M_cp.reserve(_M_int.size() - 1);
3013 _M_m.reserve(_M_int.size() - 1);
3014 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
3016 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
3017 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
3018 _M_cp.push_back(__sum);
3019 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
3021 __glibcxx_assert(__sum > 0);
3023 // Now normalize the densities...
3024 __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
3026 // ... and partial sums...
3027 __detail::__normalize(_M_cp.begin(), _M_cp.end(), _M_cp.begin(), __sum);
3029 __detail::__normalize(_M_m.begin(), _M_m.end(), _M_m.begin(), __sum);
3031 // Make sure the last cumulative probablility is one.
3032 _M_cp[_M_cp.size() - 1] = 1.0;
3035 template<typename _RealType>
3036 template<typename _InputIteratorB, typename _InputIteratorW>
3037 piecewise_linear_distribution<_RealType>::param_type::
3038 param_type(_InputIteratorB __bbegin,
3039 _InputIteratorB __bend,
3040 _InputIteratorW __wbegin)
3041 : _M_int(), _M_den(), _M_cp(), _M_m()
3043 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
3045 _M_int.push_back(*__bbegin);
3046 _M_den.push_back(*__wbegin);
3052 template<typename _RealType>
3053 template<typename _Func>
3054 piecewise_linear_distribution<_RealType>::param_type::
3055 param_type(initializer_list<_RealType> __bl, _Func __fw)
3056 : _M_int(), _M_den(), _M_cp(), _M_m()
3058 _M_int.reserve(__bl.size());
3059 _M_den.reserve(__bl.size());
3060 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
3062 _M_int.push_back(*__biter);
3063 _M_den.push_back(__fw(*__biter));
3069 template<typename _RealType>
3070 template<typename _Func>
3071 piecewise_linear_distribution<_RealType>::param_type::
3072 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
3073 : _M_int(), _M_den(), _M_cp(), _M_m()
3075 const size_t __n = __nw == 0 ? 1 : __nw;
3076 const _RealType __delta = (__xmax - __xmin) / __n;
3078 _M_int.reserve(__n + 1);
3079 _M_den.reserve(__n + 1);
3080 for (size_t __k = 0; __k <= __nw; ++__k)
3082 _M_int.push_back(__xmin + __k * __delta);
3083 _M_den.push_back(__fw(_M_int[__k] + __delta));
3089 template<typename _RealType>
3090 template<typename _UniformRandomNumberGenerator>
3091 typename piecewise_linear_distribution<_RealType>::result_type
3092 piecewise_linear_distribution<_RealType>::
3093 operator()(_UniformRandomNumberGenerator& __urng,
3094 const param_type& __param)
3096 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3099 const double __p = __aurng();
3100 if (__param._M_cp.empty())
3103 auto __pos = std::lower_bound(__param._M_cp.begin(),
3104 __param._M_cp.end(), __p);
3105 const size_t __i = __pos - __param._M_cp.begin();
3107 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3109 const double __a = 0.5 * __param._M_m[__i];
3110 const double __b = __param._M_den[__i];
3111 const double __cm = __p - __pref;
3113 _RealType __x = __param._M_int[__i];
3118 const double __d = __b * __b + 4.0 * __a * __cm;
3119 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
3125 template<typename _RealType>
3126 template<typename _ForwardIterator,
3127 typename _UniformRandomNumberGenerator>
3129 piecewise_linear_distribution<_RealType>::
3130 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3131 _UniformRandomNumberGenerator& __urng,
3132 const param_type& __param)
3134 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3135 // We could duplicate everything from operator()...
3137 *__f++ = this->operator()(__urng, __param);
3140 template<typename _RealType, typename _CharT, typename _Traits>
3141 std::basic_ostream<_CharT, _Traits>&
3142 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3143 const piecewise_linear_distribution<_RealType>& __x)
3145 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
3147 const typename __ios_base::fmtflags __flags = __os.flags();
3148 const _CharT __fill = __os.fill();
3149 const std::streamsize __precision = __os.precision();
3150 const _CharT __space = __os.widen(' ');
3151 __os.flags(__ios_base::scientific | __ios_base::left);
3153 __os.precision(std::numeric_limits<_RealType>::max_digits10);
3155 std::vector<_RealType> __int = __x.intervals();
3156 __os << __int.size() - 1;
3158 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
3159 __os << __space << *__xit;
3161 std::vector<double> __den = __x.densities();
3162 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
3163 __os << __space << *__dit;
3165 __os.flags(__flags);
3167 __os.precision(__precision);
3171 template<typename _RealType, typename _CharT, typename _Traits>
3172 std::basic_istream<_CharT, _Traits>&
3173 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3174 piecewise_linear_distribution<_RealType>& __x)
3176 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
3178 const typename __ios_base::fmtflags __flags = __is.flags();
3179 __is.flags(__ios_base::dec | __ios_base::skipws);
3184 vector<_RealType> __int_vec;
3185 if (__detail::__extract_params(__is, __int_vec, __n + 1))
3187 vector<double> __den_vec;
3188 if (__detail::__extract_params(__is, __den_vec, __n + 1))
3190 __x.param({ __int_vec.begin(), __int_vec.end(),
3191 __den_vec.begin() });
3195 __is.flags(__flags);
3200 template<typename _IntType>
3201 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
3203 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
3204 _M_v.push_back(__detail::__mod<result_type,
3205 __detail::_Shift<result_type, 32>::__value>(*__iter));
3208 template<typename _InputIterator>
3209 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
3211 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
3212 _M_v.push_back(__detail::__mod<result_type,
3213 __detail::_Shift<result_type, 32>::__value>(*__iter));
3216 template<typename _RandomAccessIterator>
3218 seed_seq::generate(_RandomAccessIterator __begin,
3219 _RandomAccessIterator __end)
3221 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3224 if (__begin == __end)
3227 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
3229 const size_t __n = __end - __begin;
3230 const size_t __s = _M_v.size();
3231 const size_t __t = (__n >= 623) ? 11
3236 const size_t __p = (__n - __t) / 2;
3237 const size_t __q = __p + __t;
3238 const size_t __m = std::max(size_t(__s + 1), __n);
3240 #ifndef __UINT32_TYPE__
3243 _Up(uint_least32_t v) : _M_v(v & 0xffffffffu) { }
3245 operator uint_least32_t() const { return _M_v; }
3247 uint_least32_t _M_v;
3249 using uint32_t = _Up;
3252 // k == 0, every element in [begin,end) equals 0x8b8b8b8bu
3254 uint32_t __r1 = 1371501266u;
3255 uint32_t __r2 = __r1 + __s;
3256 __begin[__p] += __r1;
3257 __begin[__q] = (uint32_t)__begin[__q] + __r2;
3261 for (size_t __k = 1; __k <= __s; ++__k)
3263 const size_t __kn = __k % __n;
3264 const size_t __kpn = (__k + __p) % __n;
3265 const size_t __kqn = (__k + __q) % __n;
3266 uint32_t __arg = (__begin[__kn]
3268 ^ __begin[(__k - 1) % __n]);
3269 uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
3270 uint32_t __r2 = __r1 + (uint32_t)__kn + _M_v[__k - 1];
3271 __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
3272 __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
3273 __begin[__kn] = __r2;
3276 for (size_t __k = __s + 1; __k < __m; ++__k)
3278 const size_t __kn = __k % __n;
3279 const size_t __kpn = (__k + __p) % __n;
3280 const size_t __kqn = (__k + __q) % __n;
3281 uint32_t __arg = (__begin[__kn]
3283 ^ __begin[(__k - 1) % __n]);
3284 uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
3285 uint32_t __r2 = __r1 + (uint32_t)__kn;
3286 __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
3287 __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
3288 __begin[__kn] = __r2;
3291 for (size_t __k = __m; __k < __m + __n; ++__k)
3293 const size_t __kn = __k % __n;
3294 const size_t __kpn = (__k + __p) % __n;
3295 const size_t __kqn = (__k + __q) % __n;
3296 uint32_t __arg = (__begin[__kn]
3298 + __begin[(__k - 1) % __n]);
3299 uint32_t __r3 = 1566083941u * (__arg ^ (__arg >> 27));
3300 uint32_t __r4 = __r3 - __kn;
3301 __begin[__kpn] ^= __r3;
3302 __begin[__kqn] ^= __r4;
3303 __begin[__kn] = __r4;
3307 template<typename _RealType, size_t __bits,
3308 typename _UniformRandomNumberGenerator>
3310 generate_canonical(_UniformRandomNumberGenerator& __urng)
3312 static_assert(std::is_floating_point<_RealType>::value,
3313 "template argument must be a floating point type");
3316 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
3318 const long double __r = static_cast<long double>(__urng.max())
3319 - static_cast<long double>(__urng.min()) + 1.0L;
3320 const size_t __log2r = std::log(__r) / std::log(2.0L);
3321 const size_t __m = std::max<size_t>(1UL,
3322 (__b + __log2r - 1UL) / __log2r);
3324 _RealType __sum = _RealType(0);
3325 _RealType __tmp = _RealType(1);
3326 for (size_t __k = __m; __k != 0; --__k)
3328 __sum += _RealType(__urng() - __urng.min()) * __tmp;
3331 __ret = __sum / __tmp;
3332 if (__builtin_expect(__ret >= _RealType(1), 0))
3334 #if _GLIBCXX_USE_C99_MATH_TR1
3335 __ret = std::nextafter(_RealType(1), _RealType(0));
3337 __ret = _RealType(1)
3338 - std::numeric_limits<_RealType>::epsilon() / _RealType(2);
3344 _GLIBCXX_END_NAMESPACE_VERSION