1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/random.tcc
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
33 #include <numeric> // std::accumulate and std::partial_sum
35 _GLIBCXX_BEGIN_NAMESPACE(std)
38 * (Further) implementation-space details.
42 // General case for x = (ax + c) mod m -- use Schrage's algorithm to
43 // avoid integer overflow.
45 // Because a and c are compile-time integral constants the compiler
46 // kindly elides any unreachable paths.
48 // Preconditions: a > 0, m > 0.
50 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
60 static const _Tp __q = __m / __a;
61 static const _Tp __r = __m % __a;
63 _Tp __t1 = __a * (__x % __q);
64 _Tp __t2 = __r * (__x / __q);
68 __x = __m - __t2 + __t1;
73 const _Tp __d = __m - __x;
83 // Special case for m == 0 -- use unsigned integer overflow as modulo
85 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
86 struct _Mod<_Tp, __m, __a, __c, true>
90 { return __a * __x + __c; }
93 template<typename _InputIterator, typename _OutputIterator,
94 typename _UnaryOperation>
96 __transform(_InputIterator __first, _InputIterator __last,
97 _OutputIterator __result, _UnaryOperation __unary_op)
99 for (; __first != __last; ++__first, ++__result)
100 *__result = __unary_op(*__first);
103 } // namespace __detail
106 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
108 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
110 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
112 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
114 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
116 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
118 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
120 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
123 * Seeds the LCR with integral value @p __s, adjusted so that the
124 * ring identity is never a member of the convergence set.
126 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
128 linear_congruential_engine<_UIntType, __a, __c, __m>::
129 seed(result_type __s)
131 if ((__detail::__mod<_UIntType, __m>(__c) == 0)
132 && (__detail::__mod<_UIntType, __m>(__s) == 0))
135 _M_x = __detail::__mod<_UIntType, __m>(__s);
139 * Seeds the LCR engine with a value generated by @p __q.
141 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
142 template<typename _Sseq>
143 typename std::enable_if<std::is_class<_Sseq>::value>::type
144 linear_congruential_engine<_UIntType, __a, __c, __m>::
147 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
149 const _UIntType __k = (__k0 + 31) / 32;
150 uint_least32_t __arr[__k + 3];
151 __q.generate(__arr + 0, __arr + __k + 3);
152 _UIntType __factor = 1u;
153 _UIntType __sum = 0u;
154 for (size_t __j = 0; __j < __k; ++__j)
156 __sum += __arr[__j + 3] * __factor;
157 __factor *= __detail::_Shift<_UIntType, 32>::__value;
162 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
163 typename _CharT, typename _Traits>
164 std::basic_ostream<_CharT, _Traits>&
165 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
166 const linear_congruential_engine<_UIntType,
167 __a, __c, __m>& __lcr)
169 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
170 typedef typename __ostream_type::ios_base __ios_base;
172 const typename __ios_base::fmtflags __flags = __os.flags();
173 const _CharT __fill = __os.fill();
174 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
175 __os.fill(__os.widen(' '));
184 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
185 typename _CharT, typename _Traits>
186 std::basic_istream<_CharT, _Traits>&
187 operator>>(std::basic_istream<_CharT, _Traits>& __is,
188 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
190 typedef std::basic_istream<_CharT, _Traits> __istream_type;
191 typedef typename __istream_type::ios_base __ios_base;
193 const typename __ios_base::fmtflags __flags = __is.flags();
194 __is.flags(__ios_base::dec);
203 template<typename _UIntType,
204 size_t __w, size_t __n, size_t __m, size_t __r,
205 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
206 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
209 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
210 __s, __b, __t, __c, __l, __f>::word_size;
212 template<typename _UIntType,
213 size_t __w, size_t __n, size_t __m, size_t __r,
214 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
215 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
218 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
219 __s, __b, __t, __c, __l, __f>::state_size;
221 template<typename _UIntType,
222 size_t __w, size_t __n, size_t __m, size_t __r,
223 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
224 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
227 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
228 __s, __b, __t, __c, __l, __f>::shift_size;
230 template<typename _UIntType,
231 size_t __w, size_t __n, size_t __m, size_t __r,
232 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
233 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
236 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
237 __s, __b, __t, __c, __l, __f>::mask_bits;
239 template<typename _UIntType,
240 size_t __w, size_t __n, size_t __m, size_t __r,
241 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
242 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
245 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
246 __s, __b, __t, __c, __l, __f>::xor_mask;
248 template<typename _UIntType,
249 size_t __w, size_t __n, size_t __m, size_t __r,
250 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
251 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
254 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
255 __s, __b, __t, __c, __l, __f>::tempering_u;
257 template<typename _UIntType,
258 size_t __w, size_t __n, size_t __m, size_t __r,
259 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
260 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
263 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
264 __s, __b, __t, __c, __l, __f>::tempering_d;
266 template<typename _UIntType,
267 size_t __w, size_t __n, size_t __m, size_t __r,
268 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
269 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
272 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
273 __s, __b, __t, __c, __l, __f>::tempering_s;
275 template<typename _UIntType,
276 size_t __w, size_t __n, size_t __m, size_t __r,
277 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
278 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
281 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
282 __s, __b, __t, __c, __l, __f>::tempering_b;
284 template<typename _UIntType,
285 size_t __w, size_t __n, size_t __m, size_t __r,
286 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
287 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
290 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
291 __s, __b, __t, __c, __l, __f>::tempering_t;
293 template<typename _UIntType,
294 size_t __w, size_t __n, size_t __m, size_t __r,
295 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
296 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
299 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
300 __s, __b, __t, __c, __l, __f>::tempering_c;
302 template<typename _UIntType,
303 size_t __w, size_t __n, size_t __m, size_t __r,
304 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
305 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
308 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
309 __s, __b, __t, __c, __l, __f>::tempering_l;
311 template<typename _UIntType,
312 size_t __w, size_t __n, size_t __m, size_t __r,
313 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
314 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
317 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
318 __s, __b, __t, __c, __l, __f>::
319 initialization_multiplier;
321 template<typename _UIntType,
322 size_t __w, size_t __n, size_t __m, size_t __r,
323 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
324 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
327 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
328 __s, __b, __t, __c, __l, __f>::default_seed;
330 template<typename _UIntType,
331 size_t __w, size_t __n, size_t __m, size_t __r,
332 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
333 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
336 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
337 __s, __b, __t, __c, __l, __f>::
338 seed(result_type __sd)
340 _M_x[0] = __detail::__mod<_UIntType,
341 __detail::_Shift<_UIntType, __w>::__value>(__sd);
343 for (size_t __i = 1; __i < state_size; ++__i)
345 _UIntType __x = _M_x[__i - 1];
346 __x ^= __x >> (__w - 2);
348 __x += __detail::__mod<_UIntType, __n>(__i);
349 _M_x[__i] = __detail::__mod<_UIntType,
350 __detail::_Shift<_UIntType, __w>::__value>(__x);
355 template<typename _UIntType,
356 size_t __w, size_t __n, size_t __m, size_t __r,
357 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
358 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
360 template<typename _Sseq>
361 typename std::enable_if<std::is_class<_Sseq>::value>::type
362 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
363 __s, __b, __t, __c, __l, __f>::
366 const _UIntType __upper_mask = (~_UIntType()) << __r;
367 const size_t __k = (__w + 31) / 32;
368 uint_least32_t __arr[__n * __k];
369 __q.generate(__arr + 0, __arr + __n * __k);
372 for (size_t __i = 0; __i < state_size; ++__i)
374 _UIntType __factor = 1u;
375 _UIntType __sum = 0u;
376 for (size_t __j = 0; __j < __k; ++__j)
378 __sum += __arr[__k * __i + __j] * __factor;
379 __factor *= __detail::_Shift<_UIntType, 32>::__value;
381 _M_x[__i] = __detail::__mod<_UIntType,
382 __detail::_Shift<_UIntType, __w>::__value>(__sum);
388 if ((_M_x[0] & __upper_mask) != 0u)
391 else if (_M_x[__i] != 0u)
396 _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
399 template<typename _UIntType, size_t __w,
400 size_t __n, size_t __m, size_t __r,
401 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
402 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
405 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
406 __s, __b, __t, __c, __l, __f>::result_type
407 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
408 __s, __b, __t, __c, __l, __f>::
411 // Reload the vector - cost is O(n) amortized over n calls.
412 if (_M_p >= state_size)
414 const _UIntType __upper_mask = (~_UIntType()) << __r;
415 const _UIntType __lower_mask = ~__upper_mask;
417 for (size_t __k = 0; __k < (__n - __m); ++__k)
419 _UIntType __y = ((_M_x[__k] & __upper_mask)
420 | (_M_x[__k + 1] & __lower_mask));
421 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
422 ^ ((__y & 0x01) ? __a : 0));
425 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
427 _UIntType __y = ((_M_x[__k] & __upper_mask)
428 | (_M_x[__k + 1] & __lower_mask));
429 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
430 ^ ((__y & 0x01) ? __a : 0));
433 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
434 | (_M_x[0] & __lower_mask));
435 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
436 ^ ((__y & 0x01) ? __a : 0));
440 // Calculate o(x(i)).
441 result_type __z = _M_x[_M_p++];
442 __z ^= (__z >> __u) & __d;
443 __z ^= (__z << __s) & __b;
444 __z ^= (__z << __t) & __c;
450 template<typename _UIntType, size_t __w,
451 size_t __n, size_t __m, size_t __r,
452 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
453 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
454 _UIntType __f, typename _CharT, typename _Traits>
455 std::basic_ostream<_CharT, _Traits>&
456 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
457 const mersenne_twister_engine<_UIntType, __w, __n, __m,
458 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
460 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
461 typedef typename __ostream_type::ios_base __ios_base;
463 const typename __ios_base::fmtflags __flags = __os.flags();
464 const _CharT __fill = __os.fill();
465 const _CharT __space = __os.widen(' ');
466 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
469 for (size_t __i = 0; __i < __n - 1; ++__i)
470 __os << __x._M_x[__i] << __space;
471 __os << __x._M_x[__n - 1];
478 template<typename _UIntType, size_t __w,
479 size_t __n, size_t __m, size_t __r,
480 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
481 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
482 _UIntType __f, typename _CharT, typename _Traits>
483 std::basic_istream<_CharT, _Traits>&
484 operator>>(std::basic_istream<_CharT, _Traits>& __is,
485 mersenne_twister_engine<_UIntType, __w, __n, __m,
486 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
488 typedef std::basic_istream<_CharT, _Traits> __istream_type;
489 typedef typename __istream_type::ios_base __ios_base;
491 const typename __ios_base::fmtflags __flags = __is.flags();
492 __is.flags(__ios_base::dec | __ios_base::skipws);
494 for (size_t __i = 0; __i < __n; ++__i)
495 __is >> __x._M_x[__i];
502 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
504 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
506 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
508 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
510 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
512 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
514 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
516 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
518 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
520 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
521 seed(result_type __value)
523 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
524 __lcg(__value == 0u ? default_seed : __value);
526 const size_t __n = (__w + 31) / 32;
528 for (size_t __i = 0; __i < long_lag; ++__i)
530 _UIntType __sum = 0u;
531 _UIntType __factor = 1u;
532 for (size_t __j = 0; __j < __n; ++__j)
534 __sum += __detail::__mod<uint_least32_t,
535 __detail::_Shift<uint_least32_t, 32>::__value>
536 (__lcg()) * __factor;
537 __factor *= __detail::_Shift<_UIntType, 32>::__value;
539 _M_x[__i] = __detail::__mod<_UIntType,
540 __detail::_Shift<_UIntType, __w>::__value>(__sum);
542 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
546 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
547 template<typename _Sseq>
548 typename std::enable_if<std::is_class<_Sseq>::value>::type
549 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
552 const size_t __k = (__w + 31) / 32;
553 uint_least32_t __arr[__r * __k];
554 __q.generate(__arr + 0, __arr + __r * __k);
556 for (size_t __i = 0; __i < long_lag; ++__i)
558 _UIntType __sum = 0u;
559 _UIntType __factor = 1u;
560 for (size_t __j = 0; __j < __k; ++__j)
562 __sum += __arr[__k * __i + __j] * __factor;
563 __factor *= __detail::_Shift<_UIntType, 32>::__value;
565 _M_x[__i] = __detail::__mod<_UIntType,
566 __detail::_Shift<_UIntType, __w>::__value>(__sum);
568 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
572 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
573 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
575 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
578 // Derive short lag index from current index.
579 long __ps = _M_p - short_lag;
583 // Calculate new x(i) without overflow or division.
584 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
587 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
589 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
594 __xi = (__detail::_Shift<_UIntType, __w>::__value
595 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
600 // Adjust current index to loop around in ring buffer.
601 if (++_M_p >= long_lag)
607 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
608 typename _CharT, typename _Traits>
609 std::basic_ostream<_CharT, _Traits>&
610 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
611 const subtract_with_carry_engine<_UIntType,
614 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
615 typedef typename __ostream_type::ios_base __ios_base;
617 const typename __ios_base::fmtflags __flags = __os.flags();
618 const _CharT __fill = __os.fill();
619 const _CharT __space = __os.widen(' ');
620 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
623 for (size_t __i = 0; __i < __r; ++__i)
624 __os << __x._M_x[__i] << __space;
625 __os << __x._M_carry;
632 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
633 typename _CharT, typename _Traits>
634 std::basic_istream<_CharT, _Traits>&
635 operator>>(std::basic_istream<_CharT, _Traits>& __is,
636 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
638 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
639 typedef typename __istream_type::ios_base __ios_base;
641 const typename __ios_base::fmtflags __flags = __is.flags();
642 __is.flags(__ios_base::dec | __ios_base::skipws);
644 for (size_t __i = 0; __i < __r; ++__i)
645 __is >> __x._M_x[__i];
646 __is >> __x._M_carry;
653 template<typename _RandomNumberEngine, size_t __p, size_t __r>
655 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
657 template<typename _RandomNumberEngine, size_t __p, size_t __r>
659 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
661 template<typename _RandomNumberEngine, size_t __p, size_t __r>
662 typename discard_block_engine<_RandomNumberEngine,
663 __p, __r>::result_type
664 discard_block_engine<_RandomNumberEngine, __p, __r>::
667 if (_M_n >= used_block)
669 _M_b.discard(block_size - _M_n);
676 template<typename _RandomNumberEngine, size_t __p, size_t __r,
677 typename _CharT, typename _Traits>
678 std::basic_ostream<_CharT, _Traits>&
679 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
680 const discard_block_engine<_RandomNumberEngine,
683 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
684 typedef typename __ostream_type::ios_base __ios_base;
686 const typename __ios_base::fmtflags __flags = __os.flags();
687 const _CharT __fill = __os.fill();
688 const _CharT __space = __os.widen(' ');
689 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
692 __os << __x.base() << __space << __x._M_n;
699 template<typename _RandomNumberEngine, size_t __p, size_t __r,
700 typename _CharT, typename _Traits>
701 std::basic_istream<_CharT, _Traits>&
702 operator>>(std::basic_istream<_CharT, _Traits>& __is,
703 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
705 typedef std::basic_istream<_CharT, _Traits> __istream_type;
706 typedef typename __istream_type::ios_base __ios_base;
708 const typename __ios_base::fmtflags __flags = __is.flags();
709 __is.flags(__ios_base::dec | __ios_base::skipws);
711 __is >> __x._M_b >> __x._M_n;
718 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
719 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
721 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
724 const long double __r = static_cast<long double>(_M_b.max())
725 - static_cast<long double>(_M_b.min()) + 1.0L;
726 const result_type __m = std::log(__r) / std::log(2.0L);
727 result_type __n, __n0, __y0, __y1, __s0, __s1;
728 for (size_t __i = 0; __i < 2; ++__i)
730 __n = (__w + __m - 1) / __m + __i;
731 __n0 = __n - __w % __n;
732 const result_type __w0 = __w / __n;
733 const result_type __w1 = __w0 + 1;
734 __s0 = result_type(1) << __w0;
735 __s1 = result_type(1) << __w1;
736 __y0 = __s0 * (__r / __s0);
737 __y1 = __s1 * (__r / __s1);
738 if (__r - __y0 <= __y0 / __n)
742 result_type __sum = 0;
743 for (size_t __k = 0; __k < __n0; ++__k)
747 __u = _M_b() - _M_b.min();
749 __sum = __s0 * __sum + __u % __s0;
751 for (size_t __k = __n0; __k < __n; ++__k)
755 __u = _M_b() - _M_b.min();
757 __sum = __s1 * __sum + __u % __s1;
763 template<typename _RandomNumberEngine, size_t __k>
765 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
767 template<typename _RandomNumberEngine, size_t __k>
768 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
769 shuffle_order_engine<_RandomNumberEngine, __k>::
772 size_t __j = __k * ((_M_y - _M_b.min())
773 / (_M_b.max() - _M_b.min() + 1.0L));
780 template<typename _RandomNumberEngine, size_t __k,
781 typename _CharT, typename _Traits>
782 std::basic_ostream<_CharT, _Traits>&
783 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
784 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
786 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
787 typedef typename __ostream_type::ios_base __ios_base;
789 const typename __ios_base::fmtflags __flags = __os.flags();
790 const _CharT __fill = __os.fill();
791 const _CharT __space = __os.widen(' ');
792 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
796 for (size_t __i = 0; __i < __k; ++__i)
797 __os << __space << __x._M_v[__i];
798 __os << __space << __x._M_y;
805 template<typename _RandomNumberEngine, size_t __k,
806 typename _CharT, typename _Traits>
807 std::basic_istream<_CharT, _Traits>&
808 operator>>(std::basic_istream<_CharT, _Traits>& __is,
809 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
811 typedef std::basic_istream<_CharT, _Traits> __istream_type;
812 typedef typename __istream_type::ios_base __ios_base;
814 const typename __ios_base::fmtflags __flags = __is.flags();
815 __is.flags(__ios_base::dec | __ios_base::skipws);
818 for (size_t __i = 0; __i < __k; ++__i)
819 __is >> __x._M_v[__i];
827 template<typename _IntType>
828 template<typename _UniformRandomNumberGenerator>
829 typename uniform_int_distribution<_IntType>::result_type
830 uniform_int_distribution<_IntType>::
831 operator()(_UniformRandomNumberGenerator& __urng,
832 const param_type& __param)
834 typedef typename std::make_unsigned<typename
835 _UniformRandomNumberGenerator::result_type>::type __urngtype;
836 typedef typename std::make_unsigned<result_type>::type __utype;
837 typedef typename std::conditional<(sizeof(__urngtype)
839 __urngtype, __utype>::type __uctype;
841 const __uctype __urngmin = __urng.min();
842 const __uctype __urngmax = __urng.max();
843 const __uctype __urngrange = __urngmax - __urngmin;
844 const __uctype __urange
845 = __uctype(__param.b()) - __uctype(__param.a());
849 if (__urngrange > __urange)
852 const __uctype __uerange = __urange + 1; // __urange can be zero
853 const __uctype __scaling = __urngrange / __uerange;
854 const __uctype __past = __uerange * __scaling;
856 __ret = __uctype(__urng()) - __urngmin;
857 while (__ret >= __past);
860 else if (__urngrange < __urange)
864 Note that every value in [0, urange]
865 can be written uniquely as
867 (urngrange + 1) * high + low
871 high in [0, urange / (urngrange + 1)]
875 low in [0, urngrange].
877 __uctype __tmp; // wraparound control
880 const __uctype __uerngrange = __urngrange + 1;
881 __tmp = (__uerngrange * operator()
882 (__urng, param_type(0, __urange / __uerngrange)));
883 __ret = __tmp + (__uctype(__urng()) - __urngmin);
885 while (__ret > __urange || __ret < __tmp);
888 __ret = __uctype(__urng()) - __urngmin;
890 return __ret + __param.a();
893 template<typename _IntType, typename _CharT, typename _Traits>
894 std::basic_ostream<_CharT, _Traits>&
895 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
896 const uniform_int_distribution<_IntType>& __x)
898 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
899 typedef typename __ostream_type::ios_base __ios_base;
901 const typename __ios_base::fmtflags __flags = __os.flags();
902 const _CharT __fill = __os.fill();
903 const _CharT __space = __os.widen(' ');
904 __os.flags(__ios_base::scientific | __ios_base::left);
907 __os << __x.a() << __space << __x.b();
914 template<typename _IntType, typename _CharT, typename _Traits>
915 std::basic_istream<_CharT, _Traits>&
916 operator>>(std::basic_istream<_CharT, _Traits>& __is,
917 uniform_int_distribution<_IntType>& __x)
919 typedef std::basic_istream<_CharT, _Traits> __istream_type;
920 typedef typename __istream_type::ios_base __ios_base;
922 const typename __ios_base::fmtflags __flags = __is.flags();
923 __is.flags(__ios_base::dec | __ios_base::skipws);
927 __x.param(typename uniform_int_distribution<_IntType>::
928 param_type(__a, __b));
935 template<typename _RealType, typename _CharT, typename _Traits>
936 std::basic_ostream<_CharT, _Traits>&
937 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
938 const uniform_real_distribution<_RealType>& __x)
940 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
941 typedef typename __ostream_type::ios_base __ios_base;
943 const typename __ios_base::fmtflags __flags = __os.flags();
944 const _CharT __fill = __os.fill();
945 const std::streamsize __precision = __os.precision();
946 const _CharT __space = __os.widen(' ');
947 __os.flags(__ios_base::scientific | __ios_base::left);
949 __os.precision(std::numeric_limits<_RealType>::max_digits10);
951 __os << __x.a() << __space << __x.b();
955 __os.precision(__precision);
959 template<typename _RealType, typename _CharT, typename _Traits>
960 std::basic_istream<_CharT, _Traits>&
961 operator>>(std::basic_istream<_CharT, _Traits>& __is,
962 uniform_real_distribution<_RealType>& __x)
964 typedef std::basic_istream<_CharT, _Traits> __istream_type;
965 typedef typename __istream_type::ios_base __ios_base;
967 const typename __ios_base::fmtflags __flags = __is.flags();
968 __is.flags(__ios_base::skipws);
972 __x.param(typename uniform_real_distribution<_RealType>::
973 param_type(__a, __b));
980 template<typename _CharT, typename _Traits>
981 std::basic_ostream<_CharT, _Traits>&
982 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
983 const bernoulli_distribution& __x)
985 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
986 typedef typename __ostream_type::ios_base __ios_base;
988 const typename __ios_base::fmtflags __flags = __os.flags();
989 const _CharT __fill = __os.fill();
990 const std::streamsize __precision = __os.precision();
991 __os.flags(__ios_base::scientific | __ios_base::left);
992 __os.fill(__os.widen(' '));
993 __os.precision(std::numeric_limits<double>::max_digits10);
999 __os.precision(__precision);
1004 template<typename _IntType>
1005 template<typename _UniformRandomNumberGenerator>
1006 typename geometric_distribution<_IntType>::result_type
1007 geometric_distribution<_IntType>::
1008 operator()(_UniformRandomNumberGenerator& __urng,
1009 const param_type& __param)
1011 // About the epsilon thing see this thread:
1012 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1013 const double __naf =
1014 (1 - std::numeric_limits<double>::epsilon()) / 2;
1015 // The largest _RealType convertible to _IntType.
1016 const double __thr =
1017 std::numeric_limits<_IntType>::max() + __naf;
1018 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1023 __cand = std::ceil(std::log(__aurng()) / __param._M_log_p);
1024 while (__cand >= __thr);
1026 return result_type(__cand + __naf);
1029 template<typename _IntType,
1030 typename _CharT, typename _Traits>
1031 std::basic_ostream<_CharT, _Traits>&
1032 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1033 const geometric_distribution<_IntType>& __x)
1035 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1036 typedef typename __ostream_type::ios_base __ios_base;
1038 const typename __ios_base::fmtflags __flags = __os.flags();
1039 const _CharT __fill = __os.fill();
1040 const std::streamsize __precision = __os.precision();
1041 __os.flags(__ios_base::scientific | __ios_base::left);
1042 __os.fill(__os.widen(' '));
1043 __os.precision(std::numeric_limits<double>::max_digits10);
1047 __os.flags(__flags);
1049 __os.precision(__precision);
1053 template<typename _IntType,
1054 typename _CharT, typename _Traits>
1055 std::basic_istream<_CharT, _Traits>&
1056 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1057 geometric_distribution<_IntType>& __x)
1059 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1060 typedef typename __istream_type::ios_base __ios_base;
1062 const typename __ios_base::fmtflags __flags = __is.flags();
1063 __is.flags(__ios_base::skipws);
1067 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1069 __is.flags(__flags);
1074 template<typename _IntType>
1075 template<typename _UniformRandomNumberGenerator>
1076 typename negative_binomial_distribution<_IntType>::result_type
1077 negative_binomial_distribution<_IntType>::
1078 operator()(_UniformRandomNumberGenerator& __urng)
1080 const double __y = _M_gd(__urng);
1082 // XXX Is the constructor too slow?
1083 std::poisson_distribution<result_type> __poisson(__y);
1084 return __poisson(__urng);
1087 template<typename _IntType>
1088 template<typename _UniformRandomNumberGenerator>
1089 typename negative_binomial_distribution<_IntType>::result_type
1090 negative_binomial_distribution<_IntType>::
1091 operator()(_UniformRandomNumberGenerator& __urng,
1092 const param_type& __p)
1094 typedef typename std::gamma_distribution<result_type>::param_type
1098 _M_gd(__urng, param_type(__p.k(), __p.p() / (1.0 - __p.p())));
1100 std::poisson_distribution<result_type> __poisson(__y);
1101 return __poisson(__urng);
1104 template<typename _IntType, typename _CharT, typename _Traits>
1105 std::basic_ostream<_CharT, _Traits>&
1106 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1107 const negative_binomial_distribution<_IntType>& __x)
1109 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1110 typedef typename __ostream_type::ios_base __ios_base;
1112 const typename __ios_base::fmtflags __flags = __os.flags();
1113 const _CharT __fill = __os.fill();
1114 const std::streamsize __precision = __os.precision();
1115 const _CharT __space = __os.widen(' ');
1116 __os.flags(__ios_base::scientific | __ios_base::left);
1117 __os.fill(__os.widen(' '));
1118 __os.precision(std::numeric_limits<double>::max_digits10);
1120 __os << __x.k() << __space << __x.p()
1121 << __space << __x._M_gd;
1123 __os.flags(__flags);
1125 __os.precision(__precision);
1129 template<typename _IntType, typename _CharT, typename _Traits>
1130 std::basic_istream<_CharT, _Traits>&
1131 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1132 negative_binomial_distribution<_IntType>& __x)
1134 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1135 typedef typename __istream_type::ios_base __ios_base;
1137 const typename __ios_base::fmtflags __flags = __is.flags();
1138 __is.flags(__ios_base::skipws);
1142 __is >> __k >> __p >> __x._M_gd;
1143 __x.param(typename negative_binomial_distribution<_IntType>::
1144 param_type(__k, __p));
1146 __is.flags(__flags);
1151 template<typename _IntType>
1153 poisson_distribution<_IntType>::param_type::
1156 #if _GLIBCXX_USE_C99_MATH_TR1
1159 const double __m = std::floor(_M_mean);
1160 _M_lm_thr = std::log(_M_mean);
1161 _M_lfm = std::lgamma(__m + 1);
1162 _M_sm = std::sqrt(__m);
1164 const double __pi_4 = 0.7853981633974483096156608458198757L;
1165 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1167 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1168 const double __cx = 2 * __m + _M_d;
1169 _M_scx = std::sqrt(__cx / 2);
1172 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1173 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1178 _M_lm_thr = std::exp(-_M_mean);
1182 * A rejection algorithm when mean >= 12 and a simple method based
1183 * upon the multiplication of uniform random variates otherwise.
1184 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1188 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1189 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1191 template<typename _IntType>
1192 template<typename _UniformRandomNumberGenerator>
1193 typename poisson_distribution<_IntType>::result_type
1194 poisson_distribution<_IntType>::
1195 operator()(_UniformRandomNumberGenerator& __urng,
1196 const param_type& __param)
1198 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1200 #if _GLIBCXX_USE_C99_MATH_TR1
1201 if (__param.mean() >= 12)
1205 // See comments above...
1206 const double __naf =
1207 (1 - std::numeric_limits<double>::epsilon()) / 2;
1208 const double __thr =
1209 std::numeric_limits<_IntType>::max() + __naf;
1211 const double __m = std::floor(__param.mean());
1213 const double __spi_2 = 1.2533141373155002512078826424055226L;
1214 const double __c1 = __param._M_sm * __spi_2;
1215 const double __c2 = __param._M_c2b + __c1;
1216 const double __c3 = __c2 + 1;
1217 const double __c4 = __c3 + 1;
1219 const double __e178 = 1.0129030479320018583185514777512983L;
1220 const double __c5 = __c4 + __e178;
1221 const double __c = __param._M_cb + __c5;
1222 const double __2cx = 2 * (2 * __m + __param._M_d);
1224 bool __reject = true;
1227 const double __u = __c * __aurng();
1228 const double __e = -std::log(__aurng());
1234 const double __n = _M_nd(__urng);
1235 const double __y = -std::abs(__n) * __param._M_sm - 1;
1236 __x = std::floor(__y);
1237 __w = -__n * __n / 2;
1241 else if (__u <= __c2)
1243 const double __n = _M_nd(__urng);
1244 const double __y = 1 + std::abs(__n) * __param._M_scx;
1245 __x = std::ceil(__y);
1246 __w = __y * (2 - __y) * __param._M_1cx;
1247 if (__x > __param._M_d)
1250 else if (__u <= __c3)
1251 // NB: This case not in the book, nor in the Errata,
1252 // but should be ok...
1254 else if (__u <= __c4)
1256 else if (__u <= __c5)
1260 const double __v = -std::log(__aurng());
1261 const double __y = __param._M_d
1262 + __v * __2cx / __param._M_d;
1263 __x = std::ceil(__y);
1264 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1267 __reject = (__w - __e - __x * __param._M_lm_thr
1268 > __param._M_lfm - std::lgamma(__x + __m + 1));
1270 __reject |= __x + __m >= __thr;
1274 return result_type(__x + __m + __naf);
1280 double __prod = 1.0;
1284 __prod *= __aurng();
1287 while (__prod > __param._M_lm_thr);
1293 template<typename _IntType,
1294 typename _CharT, typename _Traits>
1295 std::basic_ostream<_CharT, _Traits>&
1296 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1297 const poisson_distribution<_IntType>& __x)
1299 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1300 typedef typename __ostream_type::ios_base __ios_base;
1302 const typename __ios_base::fmtflags __flags = __os.flags();
1303 const _CharT __fill = __os.fill();
1304 const std::streamsize __precision = __os.precision();
1305 const _CharT __space = __os.widen(' ');
1306 __os.flags(__ios_base::scientific | __ios_base::left);
1308 __os.precision(std::numeric_limits<double>::max_digits10);
1310 __os << __x.mean() << __space << __x._M_nd;
1312 __os.flags(__flags);
1314 __os.precision(__precision);
1318 template<typename _IntType,
1319 typename _CharT, typename _Traits>
1320 std::basic_istream<_CharT, _Traits>&
1321 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1322 poisson_distribution<_IntType>& __x)
1324 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1325 typedef typename __istream_type::ios_base __ios_base;
1327 const typename __ios_base::fmtflags __flags = __is.flags();
1328 __is.flags(__ios_base::skipws);
1331 __is >> __mean >> __x._M_nd;
1332 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1334 __is.flags(__flags);
1339 template<typename _IntType>
1341 binomial_distribution<_IntType>::param_type::
1344 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1348 #if _GLIBCXX_USE_C99_MATH_TR1
1349 if (_M_t * __p12 >= 8)
1352 const double __np = std::floor(_M_t * __p12);
1353 const double __pa = __np / _M_t;
1354 const double __1p = 1 - __pa;
1356 const double __pi_4 = 0.7853981633974483096156608458198757L;
1357 const double __d1x =
1358 std::sqrt(__np * __1p * std::log(32 * __np
1359 / (81 * __pi_4 * __1p)));
1360 _M_d1 = std::round(std::max(1.0, __d1x));
1361 const double __d2x =
1362 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1363 / (__pi_4 * __pa)));
1364 _M_d2 = std::round(std::max(1.0, __d2x));
1367 const double __spi_2 = 1.2533141373155002512078826424055226L;
1368 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1369 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1370 _M_c = 2 * _M_d1 / __np;
1371 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1372 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1373 const double __s1s = _M_s1 * _M_s1;
1374 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1376 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1377 const double __s2s = _M_s2 * _M_s2;
1378 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1379 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1380 _M_lf = (std::lgamma(__np + 1)
1381 + std::lgamma(_M_t - __np + 1));
1382 _M_lp1p = std::log(__pa / __1p);
1384 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1388 _M_q = -std::log(1 - __p12);
1391 template<typename _IntType>
1392 template<typename _UniformRandomNumberGenerator>
1393 typename binomial_distribution<_IntType>::result_type
1394 binomial_distribution<_IntType>::
1395 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1399 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1404 const double __e = -std::log(__aurng());
1405 __sum += __e / (__t - __x);
1408 while (__sum <= _M_param._M_q);
1414 * A rejection algorithm when t * p >= 8 and a simple waiting time
1415 * method - the second in the referenced book - otherwise.
1416 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1420 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1421 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1423 template<typename _IntType>
1424 template<typename _UniformRandomNumberGenerator>
1425 typename binomial_distribution<_IntType>::result_type
1426 binomial_distribution<_IntType>::
1427 operator()(_UniformRandomNumberGenerator& __urng,
1428 const param_type& __param)
1431 const _IntType __t = __param.t();
1432 const _IntType __p = __param.p();
1433 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1434 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1437 #if _GLIBCXX_USE_C99_MATH_TR1
1438 if (!__param._M_easy)
1442 // See comments above...
1443 const double __naf =
1444 (1 - std::numeric_limits<double>::epsilon()) / 2;
1445 const double __thr =
1446 std::numeric_limits<_IntType>::max() + __naf;
1448 const double __np = std::floor(__t * __p12);
1451 const double __spi_2 = 1.2533141373155002512078826424055226L;
1452 const double __a1 = __param._M_a1;
1453 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1454 const double __a123 = __param._M_a123;
1455 const double __s1s = __param._M_s1 * __param._M_s1;
1456 const double __s2s = __param._M_s2 * __param._M_s2;
1461 const double __u = __param._M_s * __aurng();
1467 const double __n = _M_nd(__urng);
1468 const double __y = __param._M_s1 * std::abs(__n);
1469 __reject = __y >= __param._M_d1;
1472 const double __e = -std::log(__aurng());
1473 __x = std::floor(__y);
1474 __v = -__e - __n * __n / 2 + __param._M_c;
1477 else if (__u <= __a12)
1479 const double __n = _M_nd(__urng);
1480 const double __y = __param._M_s2 * std::abs(__n);
1481 __reject = __y >= __param._M_d2;
1484 const double __e = -std::log(__aurng());
1485 __x = std::floor(-__y);
1486 __v = -__e - __n * __n / 2;
1489 else if (__u <= __a123)
1491 const double __e1 = -std::log(__aurng());
1492 const double __e2 = -std::log(__aurng());
1494 const double __y = __param._M_d1
1495 + 2 * __s1s * __e1 / __param._M_d1;
1496 __x = std::floor(__y);
1497 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1498 -__y / (2 * __s1s)));
1503 const double __e1 = -std::log(__aurng());
1504 const double __e2 = -std::log(__aurng());
1506 const double __y = __param._M_d2
1507 + 2 * __s2s * __e1 / __param._M_d2;
1508 __x = std::floor(-__y);
1509 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1513 __reject = __reject || __x < -__np || __x > __t - __np;
1516 const double __lfx =
1517 std::lgamma(__np + __x + 1)
1518 + std::lgamma(__t - (__np + __x) + 1);
1519 __reject = __v > __param._M_lf - __lfx
1520 + __x * __param._M_lp1p;
1523 __reject |= __x + __np >= __thr;
1527 __x += __np + __naf;
1529 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1530 __ret = _IntType(__x) + __z;
1534 __ret = _M_waiting(__urng, __t);
1537 __ret = __t - __ret;
1541 template<typename _IntType,
1542 typename _CharT, typename _Traits>
1543 std::basic_ostream<_CharT, _Traits>&
1544 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1545 const binomial_distribution<_IntType>& __x)
1547 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1548 typedef typename __ostream_type::ios_base __ios_base;
1550 const typename __ios_base::fmtflags __flags = __os.flags();
1551 const _CharT __fill = __os.fill();
1552 const std::streamsize __precision = __os.precision();
1553 const _CharT __space = __os.widen(' ');
1554 __os.flags(__ios_base::scientific | __ios_base::left);
1556 __os.precision(std::numeric_limits<double>::max_digits10);
1558 __os << __x.t() << __space << __x.p()
1559 << __space << __x._M_nd;
1561 __os.flags(__flags);
1563 __os.precision(__precision);
1567 template<typename _IntType,
1568 typename _CharT, typename _Traits>
1569 std::basic_istream<_CharT, _Traits>&
1570 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1571 binomial_distribution<_IntType>& __x)
1573 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1574 typedef typename __istream_type::ios_base __ios_base;
1576 const typename __ios_base::fmtflags __flags = __is.flags();
1577 __is.flags(__ios_base::dec | __ios_base::skipws);
1581 __is >> __t >> __p >> __x._M_nd;
1582 __x.param(typename binomial_distribution<_IntType>::
1583 param_type(__t, __p));
1585 __is.flags(__flags);
1590 template<typename _RealType, typename _CharT, typename _Traits>
1591 std::basic_ostream<_CharT, _Traits>&
1592 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1593 const exponential_distribution<_RealType>& __x)
1595 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1596 typedef typename __ostream_type::ios_base __ios_base;
1598 const typename __ios_base::fmtflags __flags = __os.flags();
1599 const _CharT __fill = __os.fill();
1600 const std::streamsize __precision = __os.precision();
1601 __os.flags(__ios_base::scientific | __ios_base::left);
1602 __os.fill(__os.widen(' '));
1603 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1605 __os << __x.lambda();
1607 __os.flags(__flags);
1609 __os.precision(__precision);
1613 template<typename _RealType, typename _CharT, typename _Traits>
1614 std::basic_istream<_CharT, _Traits>&
1615 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1616 exponential_distribution<_RealType>& __x)
1618 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1619 typedef typename __istream_type::ios_base __ios_base;
1621 const typename __ios_base::fmtflags __flags = __is.flags();
1622 __is.flags(__ios_base::dec | __ios_base::skipws);
1626 __x.param(typename exponential_distribution<_RealType>::
1627 param_type(__lambda));
1629 __is.flags(__flags);
1635 * Polar method due to Marsaglia.
1637 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1638 * New York, 1986, Ch. V, Sect. 4.4.
1640 template<typename _RealType>
1641 template<typename _UniformRandomNumberGenerator>
1642 typename normal_distribution<_RealType>::result_type
1643 normal_distribution<_RealType>::
1644 operator()(_UniformRandomNumberGenerator& __urng,
1645 const param_type& __param)
1648 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1651 if (_M_saved_available)
1653 _M_saved_available = false;
1658 result_type __x, __y, __r2;
1661 __x = result_type(2.0) * __aurng() - 1.0;
1662 __y = result_type(2.0) * __aurng() - 1.0;
1663 __r2 = __x * __x + __y * __y;
1665 while (__r2 > 1.0 || __r2 == 0.0);
1667 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1668 _M_saved = __x * __mult;
1669 _M_saved_available = true;
1670 __ret = __y * __mult;
1673 __ret = __ret * __param.stddev() + __param.mean();
1677 template<typename _RealType>
1679 operator==(const std::normal_distribution<_RealType>& __d1,
1680 const std::normal_distribution<_RealType>& __d2)
1682 if (__d1._M_param == __d2._M_param
1683 && __d1._M_saved_available == __d2._M_saved_available)
1685 if (__d1._M_saved_available
1686 && __d1._M_saved == __d2._M_saved)
1688 else if(!__d1._M_saved_available)
1697 template<typename _RealType, typename _CharT, typename _Traits>
1698 std::basic_ostream<_CharT, _Traits>&
1699 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1700 const normal_distribution<_RealType>& __x)
1702 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1703 typedef typename __ostream_type::ios_base __ios_base;
1705 const typename __ios_base::fmtflags __flags = __os.flags();
1706 const _CharT __fill = __os.fill();
1707 const std::streamsize __precision = __os.precision();
1708 const _CharT __space = __os.widen(' ');
1709 __os.flags(__ios_base::scientific | __ios_base::left);
1711 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1713 __os << __x.mean() << __space << __x.stddev()
1714 << __space << __x._M_saved_available;
1715 if (__x._M_saved_available)
1716 __os << __space << __x._M_saved;
1718 __os.flags(__flags);
1720 __os.precision(__precision);
1724 template<typename _RealType, typename _CharT, typename _Traits>
1725 std::basic_istream<_CharT, _Traits>&
1726 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1727 normal_distribution<_RealType>& __x)
1729 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1730 typedef typename __istream_type::ios_base __ios_base;
1732 const typename __ios_base::fmtflags __flags = __is.flags();
1733 __is.flags(__ios_base::dec | __ios_base::skipws);
1735 double __mean, __stddev;
1736 __is >> __mean >> __stddev
1737 >> __x._M_saved_available;
1738 if (__x._M_saved_available)
1739 __is >> __x._M_saved;
1740 __x.param(typename normal_distribution<_RealType>::
1741 param_type(__mean, __stddev));
1743 __is.flags(__flags);
1748 template<typename _RealType, typename _CharT, typename _Traits>
1749 std::basic_ostream<_CharT, _Traits>&
1750 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1751 const lognormal_distribution<_RealType>& __x)
1753 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1754 typedef typename __ostream_type::ios_base __ios_base;
1756 const typename __ios_base::fmtflags __flags = __os.flags();
1757 const _CharT __fill = __os.fill();
1758 const std::streamsize __precision = __os.precision();
1759 const _CharT __space = __os.widen(' ');
1760 __os.flags(__ios_base::scientific | __ios_base::left);
1762 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1764 __os << __x.m() << __space << __x.s()
1765 << __space << __x._M_nd;
1767 __os.flags(__flags);
1769 __os.precision(__precision);
1773 template<typename _RealType, typename _CharT, typename _Traits>
1774 std::basic_istream<_CharT, _Traits>&
1775 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1776 lognormal_distribution<_RealType>& __x)
1778 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1779 typedef typename __istream_type::ios_base __ios_base;
1781 const typename __ios_base::fmtflags __flags = __is.flags();
1782 __is.flags(__ios_base::dec | __ios_base::skipws);
1785 __is >> __m >> __s >> __x._M_nd;
1786 __x.param(typename lognormal_distribution<_RealType>::
1787 param_type(__m, __s));
1789 __is.flags(__flags);
1794 template<typename _RealType, typename _CharT, typename _Traits>
1795 std::basic_ostream<_CharT, _Traits>&
1796 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1797 const chi_squared_distribution<_RealType>& __x)
1799 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1800 typedef typename __ostream_type::ios_base __ios_base;
1802 const typename __ios_base::fmtflags __flags = __os.flags();
1803 const _CharT __fill = __os.fill();
1804 const std::streamsize __precision = __os.precision();
1805 const _CharT __space = __os.widen(' ');
1806 __os.flags(__ios_base::scientific | __ios_base::left);
1808 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1810 __os << __x.n() << __space << __x._M_gd;
1812 __os.flags(__flags);
1814 __os.precision(__precision);
1818 template<typename _RealType, typename _CharT, typename _Traits>
1819 std::basic_istream<_CharT, _Traits>&
1820 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1821 chi_squared_distribution<_RealType>& __x)
1823 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1824 typedef typename __istream_type::ios_base __ios_base;
1826 const typename __ios_base::fmtflags __flags = __is.flags();
1827 __is.flags(__ios_base::dec | __ios_base::skipws);
1830 __is >> __n >> __x._M_gd;
1831 __x.param(typename chi_squared_distribution<_RealType>::
1834 __is.flags(__flags);
1839 template<typename _RealType>
1840 template<typename _UniformRandomNumberGenerator>
1841 typename cauchy_distribution<_RealType>::result_type
1842 cauchy_distribution<_RealType>::
1843 operator()(_UniformRandomNumberGenerator& __urng,
1844 const param_type& __p)
1846 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1853 const _RealType __pi = 3.1415926535897932384626433832795029L;
1854 return __p.a() + __p.b() * std::tan(__pi * __u);
1857 template<typename _RealType, typename _CharT, typename _Traits>
1858 std::basic_ostream<_CharT, _Traits>&
1859 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1860 const cauchy_distribution<_RealType>& __x)
1862 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1863 typedef typename __ostream_type::ios_base __ios_base;
1865 const typename __ios_base::fmtflags __flags = __os.flags();
1866 const _CharT __fill = __os.fill();
1867 const std::streamsize __precision = __os.precision();
1868 const _CharT __space = __os.widen(' ');
1869 __os.flags(__ios_base::scientific | __ios_base::left);
1871 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1873 __os << __x.a() << __space << __x.b();
1875 __os.flags(__flags);
1877 __os.precision(__precision);
1881 template<typename _RealType, typename _CharT, typename _Traits>
1882 std::basic_istream<_CharT, _Traits>&
1883 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1884 cauchy_distribution<_RealType>& __x)
1886 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1887 typedef typename __istream_type::ios_base __ios_base;
1889 const typename __ios_base::fmtflags __flags = __is.flags();
1890 __is.flags(__ios_base::dec | __ios_base::skipws);
1894 __x.param(typename cauchy_distribution<_RealType>::
1895 param_type(__a, __b));
1897 __is.flags(__flags);
1902 template<typename _RealType, typename _CharT, typename _Traits>
1903 std::basic_ostream<_CharT, _Traits>&
1904 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1905 const fisher_f_distribution<_RealType>& __x)
1907 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1908 typedef typename __ostream_type::ios_base __ios_base;
1910 const typename __ios_base::fmtflags __flags = __os.flags();
1911 const _CharT __fill = __os.fill();
1912 const std::streamsize __precision = __os.precision();
1913 const _CharT __space = __os.widen(' ');
1914 __os.flags(__ios_base::scientific | __ios_base::left);
1916 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1918 __os << __x.m() << __space << __x.n()
1919 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1921 __os.flags(__flags);
1923 __os.precision(__precision);
1927 template<typename _RealType, typename _CharT, typename _Traits>
1928 std::basic_istream<_CharT, _Traits>&
1929 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1930 fisher_f_distribution<_RealType>& __x)
1932 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1933 typedef typename __istream_type::ios_base __ios_base;
1935 const typename __ios_base::fmtflags __flags = __is.flags();
1936 __is.flags(__ios_base::dec | __ios_base::skipws);
1939 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1940 __x.param(typename fisher_f_distribution<_RealType>::
1941 param_type(__m, __n));
1943 __is.flags(__flags);
1948 template<typename _RealType, typename _CharT, typename _Traits>
1949 std::basic_ostream<_CharT, _Traits>&
1950 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1951 const student_t_distribution<_RealType>& __x)
1953 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1954 typedef typename __ostream_type::ios_base __ios_base;
1956 const typename __ios_base::fmtflags __flags = __os.flags();
1957 const _CharT __fill = __os.fill();
1958 const std::streamsize __precision = __os.precision();
1959 const _CharT __space = __os.widen(' ');
1960 __os.flags(__ios_base::scientific | __ios_base::left);
1962 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1964 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1966 __os.flags(__flags);
1968 __os.precision(__precision);
1972 template<typename _RealType, typename _CharT, typename _Traits>
1973 std::basic_istream<_CharT, _Traits>&
1974 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1975 student_t_distribution<_RealType>& __x)
1977 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1978 typedef typename __istream_type::ios_base __ios_base;
1980 const typename __ios_base::fmtflags __flags = __is.flags();
1981 __is.flags(__ios_base::dec | __ios_base::skipws);
1984 __is >> __n >> __x._M_nd >> __x._M_gd;
1985 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1987 __is.flags(__flags);
1992 template<typename _RealType>
1994 gamma_distribution<_RealType>::param_type::
1997 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
1999 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2000 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2004 * Marsaglia, G. and Tsang, W. W.
2005 * "A Simple Method for Generating Gamma Variables"
2006 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2008 template<typename _RealType>
2009 template<typename _UniformRandomNumberGenerator>
2010 typename gamma_distribution<_RealType>::result_type
2011 gamma_distribution<_RealType>::
2012 operator()(_UniformRandomNumberGenerator& __urng,
2013 const param_type& __param)
2015 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2018 result_type __u, __v, __n;
2019 const result_type __a1 = (__param._M_malpha
2020 - _RealType(1.0) / _RealType(3.0));
2026 __n = _M_nd(__urng);
2027 __v = result_type(1.0) + __param._M_a2 * __n;
2031 __v = __v * __v * __v;
2034 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2035 && (std::log(__u) > (0.5 * __n * __n + __a1
2036 * (1.0 - __v + std::log(__v)))));
2038 if (__param.alpha() == __param._M_malpha)
2039 return __a1 * __v * __param.beta();
2046 return (std::pow(__u, result_type(1.0) / __param.alpha())
2047 * __a1 * __v * __param.beta());
2051 template<typename _RealType, typename _CharT, typename _Traits>
2052 std::basic_ostream<_CharT, _Traits>&
2053 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2054 const gamma_distribution<_RealType>& __x)
2056 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2057 typedef typename __ostream_type::ios_base __ios_base;
2059 const typename __ios_base::fmtflags __flags = __os.flags();
2060 const _CharT __fill = __os.fill();
2061 const std::streamsize __precision = __os.precision();
2062 const _CharT __space = __os.widen(' ');
2063 __os.flags(__ios_base::scientific | __ios_base::left);
2065 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2067 __os << __x.alpha() << __space << __x.beta()
2068 << __space << __x._M_nd;
2070 __os.flags(__flags);
2072 __os.precision(__precision);
2076 template<typename _RealType, typename _CharT, typename _Traits>
2077 std::basic_istream<_CharT, _Traits>&
2078 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2079 gamma_distribution<_RealType>& __x)
2081 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2082 typedef typename __istream_type::ios_base __ios_base;
2084 const typename __ios_base::fmtflags __flags = __is.flags();
2085 __is.flags(__ios_base::dec | __ios_base::skipws);
2087 _RealType __alpha_val, __beta_val;
2088 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2089 __x.param(typename gamma_distribution<_RealType>::
2090 param_type(__alpha_val, __beta_val));
2092 __is.flags(__flags);
2097 template<typename _RealType>
2098 template<typename _UniformRandomNumberGenerator>
2099 typename weibull_distribution<_RealType>::result_type
2100 weibull_distribution<_RealType>::
2101 operator()(_UniformRandomNumberGenerator& __urng,
2102 const param_type& __p)
2104 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2106 return __p.b() * std::pow(-std::log(__aurng()),
2107 result_type(1) / __p.a());
2110 template<typename _RealType, typename _CharT, typename _Traits>
2111 std::basic_ostream<_CharT, _Traits>&
2112 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2113 const weibull_distribution<_RealType>& __x)
2115 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2116 typedef typename __ostream_type::ios_base __ios_base;
2118 const typename __ios_base::fmtflags __flags = __os.flags();
2119 const _CharT __fill = __os.fill();
2120 const std::streamsize __precision = __os.precision();
2121 const _CharT __space = __os.widen(' ');
2122 __os.flags(__ios_base::scientific | __ios_base::left);
2124 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2126 __os << __x.a() << __space << __x.b();
2128 __os.flags(__flags);
2130 __os.precision(__precision);
2134 template<typename _RealType, typename _CharT, typename _Traits>
2135 std::basic_istream<_CharT, _Traits>&
2136 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2137 weibull_distribution<_RealType>& __x)
2139 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2140 typedef typename __istream_type::ios_base __ios_base;
2142 const typename __ios_base::fmtflags __flags = __is.flags();
2143 __is.flags(__ios_base::dec | __ios_base::skipws);
2147 __x.param(typename weibull_distribution<_RealType>::
2148 param_type(__a, __b));
2150 __is.flags(__flags);
2155 template<typename _RealType>
2156 template<typename _UniformRandomNumberGenerator>
2157 typename extreme_value_distribution<_RealType>::result_type
2158 extreme_value_distribution<_RealType>::
2159 operator()(_UniformRandomNumberGenerator& __urng,
2160 const param_type& __p)
2162 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2164 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2167 template<typename _RealType, typename _CharT, typename _Traits>
2168 std::basic_ostream<_CharT, _Traits>&
2169 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2170 const extreme_value_distribution<_RealType>& __x)
2172 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2173 typedef typename __ostream_type::ios_base __ios_base;
2175 const typename __ios_base::fmtflags __flags = __os.flags();
2176 const _CharT __fill = __os.fill();
2177 const std::streamsize __precision = __os.precision();
2178 const _CharT __space = __os.widen(' ');
2179 __os.flags(__ios_base::scientific | __ios_base::left);
2181 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2183 __os << __x.a() << __space << __x.b();
2185 __os.flags(__flags);
2187 __os.precision(__precision);
2191 template<typename _RealType, typename _CharT, typename _Traits>
2192 std::basic_istream<_CharT, _Traits>&
2193 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2194 extreme_value_distribution<_RealType>& __x)
2196 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2197 typedef typename __istream_type::ios_base __ios_base;
2199 const typename __ios_base::fmtflags __flags = __is.flags();
2200 __is.flags(__ios_base::dec | __ios_base::skipws);
2204 __x.param(typename extreme_value_distribution<_RealType>::
2205 param_type(__a, __b));
2207 __is.flags(__flags);
2212 template<typename _IntType>
2214 discrete_distribution<_IntType>::param_type::
2217 if (_M_prob.size() < 2)
2223 const double __sum = std::accumulate(_M_prob.begin(),
2224 _M_prob.end(), 0.0);
2225 // Now normalize the probabilites.
2226 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2227 std::bind2nd(std::divides<double>(), __sum));
2228 // Accumulate partial sums.
2229 _M_cp.reserve(_M_prob.size());
2230 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2231 std::back_inserter(_M_cp));
2232 // Make sure the last cumulative probability is one.
2233 _M_cp[_M_cp.size() - 1] = 1.0;
2236 template<typename _IntType>
2237 template<typename _Func>
2238 discrete_distribution<_IntType>::param_type::
2239 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2240 : _M_prob(), _M_cp()
2242 const size_t __n = __nw == 0 ? 1 : __nw;
2243 const double __delta = (__xmax - __xmin) / __n;
2245 _M_prob.reserve(__n);
2246 for (size_t __k = 0; __k < __nw; ++__k)
2247 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2252 template<typename _IntType>
2253 template<typename _UniformRandomNumberGenerator>
2254 typename discrete_distribution<_IntType>::result_type
2255 discrete_distribution<_IntType>::
2256 operator()(_UniformRandomNumberGenerator& __urng,
2257 const param_type& __param)
2259 if (__param._M_cp.empty())
2260 return result_type(0);
2262 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2265 const double __p = __aurng();
2266 auto __pos = std::lower_bound(__param._M_cp.begin(),
2267 __param._M_cp.end(), __p);
2269 return __pos - __param._M_cp.begin();
2272 template<typename _IntType, typename _CharT, typename _Traits>
2273 std::basic_ostream<_CharT, _Traits>&
2274 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2275 const discrete_distribution<_IntType>& __x)
2277 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2278 typedef typename __ostream_type::ios_base __ios_base;
2280 const typename __ios_base::fmtflags __flags = __os.flags();
2281 const _CharT __fill = __os.fill();
2282 const std::streamsize __precision = __os.precision();
2283 const _CharT __space = __os.widen(' ');
2284 __os.flags(__ios_base::scientific | __ios_base::left);
2286 __os.precision(std::numeric_limits<double>::max_digits10);
2288 std::vector<double> __prob = __x.probabilities();
2289 __os << __prob.size();
2290 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2291 __os << __space << *__dit;
2293 __os.flags(__flags);
2295 __os.precision(__precision);
2299 template<typename _IntType, typename _CharT, typename _Traits>
2300 std::basic_istream<_CharT, _Traits>&
2301 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2302 discrete_distribution<_IntType>& __x)
2304 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2305 typedef typename __istream_type::ios_base __ios_base;
2307 const typename __ios_base::fmtflags __flags = __is.flags();
2308 __is.flags(__ios_base::dec | __ios_base::skipws);
2313 std::vector<double> __prob_vec;
2314 __prob_vec.reserve(__n);
2315 for (; __n != 0; --__n)
2319 __prob_vec.push_back(__prob);
2322 __x.param(typename discrete_distribution<_IntType>::
2323 param_type(__prob_vec.begin(), __prob_vec.end()));
2325 __is.flags(__flags);
2330 template<typename _RealType>
2332 piecewise_constant_distribution<_RealType>::param_type::
2335 if (_M_int.size() < 2
2336 || (_M_int.size() == 2
2337 && _M_int[0] == _RealType(0)
2338 && _M_int[1] == _RealType(1)))
2345 const double __sum = std::accumulate(_M_den.begin(),
2348 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2349 std::bind2nd(std::divides<double>(), __sum));
2351 _M_cp.reserve(_M_den.size());
2352 std::partial_sum(_M_den.begin(), _M_den.end(),
2353 std::back_inserter(_M_cp));
2355 // Make sure the last cumulative probability is one.
2356 _M_cp[_M_cp.size() - 1] = 1.0;
2358 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2359 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2362 template<typename _RealType>
2363 template<typename _InputIteratorB, typename _InputIteratorW>
2364 piecewise_constant_distribution<_RealType>::param_type::
2365 param_type(_InputIteratorB __bbegin,
2366 _InputIteratorB __bend,
2367 _InputIteratorW __wbegin)
2368 : _M_int(), _M_den(), _M_cp()
2370 if (__bbegin != __bend)
2374 _M_int.push_back(*__bbegin);
2376 if (__bbegin == __bend)
2379 _M_den.push_back(*__wbegin);
2387 template<typename _RealType>
2388 template<typename _Func>
2389 piecewise_constant_distribution<_RealType>::param_type::
2390 param_type(initializer_list<_RealType> __bl, _Func __fw)
2391 : _M_int(), _M_den(), _M_cp()
2393 _M_int.reserve(__bl.size());
2394 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2395 _M_int.push_back(*__biter);
2397 _M_den.reserve(_M_int.size() - 1);
2398 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2399 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2404 template<typename _RealType>
2405 template<typename _Func>
2406 piecewise_constant_distribution<_RealType>::param_type::
2407 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2408 : _M_int(), _M_den(), _M_cp()
2410 const size_t __n = __nw == 0 ? 1 : __nw;
2411 const _RealType __delta = (__xmax - __xmin) / __n;
2413 _M_int.reserve(__n + 1);
2414 for (size_t __k = 0; __k <= __nw; ++__k)
2415 _M_int.push_back(__xmin + __k * __delta);
2417 _M_den.reserve(__n);
2418 for (size_t __k = 0; __k < __nw; ++__k)
2419 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2424 template<typename _RealType>
2425 template<typename _UniformRandomNumberGenerator>
2426 typename piecewise_constant_distribution<_RealType>::result_type
2427 piecewise_constant_distribution<_RealType>::
2428 operator()(_UniformRandomNumberGenerator& __urng,
2429 const param_type& __param)
2431 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2434 const double __p = __aurng();
2435 if (__param._M_cp.empty())
2438 auto __pos = std::lower_bound(__param._M_cp.begin(),
2439 __param._M_cp.end(), __p);
2440 const size_t __i = __pos - __param._M_cp.begin();
2442 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2444 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2447 template<typename _RealType, typename _CharT, typename _Traits>
2448 std::basic_ostream<_CharT, _Traits>&
2449 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2450 const piecewise_constant_distribution<_RealType>& __x)
2452 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2453 typedef typename __ostream_type::ios_base __ios_base;
2455 const typename __ios_base::fmtflags __flags = __os.flags();
2456 const _CharT __fill = __os.fill();
2457 const std::streamsize __precision = __os.precision();
2458 const _CharT __space = __os.widen(' ');
2459 __os.flags(__ios_base::scientific | __ios_base::left);
2461 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2463 std::vector<_RealType> __int = __x.intervals();
2464 __os << __int.size() - 1;
2466 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2467 __os << __space << *__xit;
2469 std::vector<double> __den = __x.densities();
2470 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2471 __os << __space << *__dit;
2473 __os.flags(__flags);
2475 __os.precision(__precision);
2479 template<typename _RealType, typename _CharT, typename _Traits>
2480 std::basic_istream<_CharT, _Traits>&
2481 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2482 piecewise_constant_distribution<_RealType>& __x)
2484 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2485 typedef typename __istream_type::ios_base __ios_base;
2487 const typename __ios_base::fmtflags __flags = __is.flags();
2488 __is.flags(__ios_base::dec | __ios_base::skipws);
2493 std::vector<_RealType> __int_vec;
2494 __int_vec.reserve(__n + 1);
2495 for (size_t __i = 0; __i <= __n; ++__i)
2499 __int_vec.push_back(__int);
2502 std::vector<double> __den_vec;
2503 __den_vec.reserve(__n);
2504 for (size_t __i = 0; __i < __n; ++__i)
2508 __den_vec.push_back(__den);
2511 __x.param(typename piecewise_constant_distribution<_RealType>::
2512 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2514 __is.flags(__flags);
2519 template<typename _RealType>
2521 piecewise_linear_distribution<_RealType>::param_type::
2524 if (_M_int.size() < 2
2525 || (_M_int.size() == 2
2526 && _M_int[0] == _RealType(0)
2527 && _M_int[1] == _RealType(1)
2528 && _M_den[0] == _M_den[1]))
2536 _M_cp.reserve(_M_int.size() - 1);
2537 _M_m.reserve(_M_int.size() - 1);
2538 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2540 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2541 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2542 _M_cp.push_back(__sum);
2543 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2546 // Now normalize the densities...
2547 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2548 std::bind2nd(std::divides<double>(), __sum));
2549 // ... and partial sums...
2550 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2551 std::bind2nd(std::divides<double>(), __sum));
2553 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2554 std::bind2nd(std::divides<double>(), __sum));
2555 // Make sure the last cumulative probablility is one.
2556 _M_cp[_M_cp.size() - 1] = 1.0;
2559 template<typename _RealType>
2560 template<typename _InputIteratorB, typename _InputIteratorW>
2561 piecewise_linear_distribution<_RealType>::param_type::
2562 param_type(_InputIteratorB __bbegin,
2563 _InputIteratorB __bend,
2564 _InputIteratorW __wbegin)
2565 : _M_int(), _M_den(), _M_cp(), _M_m()
2567 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2569 _M_int.push_back(*__bbegin);
2570 _M_den.push_back(*__wbegin);
2576 template<typename _RealType>
2577 template<typename _Func>
2578 piecewise_linear_distribution<_RealType>::param_type::
2579 param_type(initializer_list<_RealType> __bl, _Func __fw)
2580 : _M_int(), _M_den(), _M_cp(), _M_m()
2582 _M_int.reserve(__bl.size());
2583 _M_den.reserve(__bl.size());
2584 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2586 _M_int.push_back(*__biter);
2587 _M_den.push_back(__fw(*__biter));
2593 template<typename _RealType>
2594 template<typename _Func>
2595 piecewise_linear_distribution<_RealType>::param_type::
2596 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2597 : _M_int(), _M_den(), _M_cp(), _M_m()
2599 const size_t __n = __nw == 0 ? 1 : __nw;
2600 const _RealType __delta = (__xmax - __xmin) / __n;
2602 _M_int.reserve(__n + 1);
2603 _M_den.reserve(__n + 1);
2604 for (size_t __k = 0; __k <= __nw; ++__k)
2606 _M_int.push_back(__xmin + __k * __delta);
2607 _M_den.push_back(__fw(_M_int[__k] + __delta));
2613 template<typename _RealType>
2614 template<typename _UniformRandomNumberGenerator>
2615 typename piecewise_linear_distribution<_RealType>::result_type
2616 piecewise_linear_distribution<_RealType>::
2617 operator()(_UniformRandomNumberGenerator& __urng,
2618 const param_type& __param)
2620 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2623 const double __p = __aurng();
2624 if (__param._M_cp.empty())
2627 auto __pos = std::lower_bound(__param._M_cp.begin(),
2628 __param._M_cp.end(), __p);
2629 const size_t __i = __pos - __param._M_cp.begin();
2631 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2633 const double __a = 0.5 * __param._M_m[__i];
2634 const double __b = __param._M_den[__i];
2635 const double __cm = __p - __pref;
2637 _RealType __x = __param._M_int[__i];
2642 const double __d = __b * __b + 4.0 * __a * __cm;
2643 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2649 template<typename _RealType, typename _CharT, typename _Traits>
2650 std::basic_ostream<_CharT, _Traits>&
2651 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2652 const piecewise_linear_distribution<_RealType>& __x)
2654 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2655 typedef typename __ostream_type::ios_base __ios_base;
2657 const typename __ios_base::fmtflags __flags = __os.flags();
2658 const _CharT __fill = __os.fill();
2659 const std::streamsize __precision = __os.precision();
2660 const _CharT __space = __os.widen(' ');
2661 __os.flags(__ios_base::scientific | __ios_base::left);
2663 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2665 std::vector<_RealType> __int = __x.intervals();
2666 __os << __int.size() - 1;
2668 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2669 __os << __space << *__xit;
2671 std::vector<double> __den = __x.densities();
2672 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2673 __os << __space << *__dit;
2675 __os.flags(__flags);
2677 __os.precision(__precision);
2681 template<typename _RealType, typename _CharT, typename _Traits>
2682 std::basic_istream<_CharT, _Traits>&
2683 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2684 piecewise_linear_distribution<_RealType>& __x)
2686 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2687 typedef typename __istream_type::ios_base __ios_base;
2689 const typename __ios_base::fmtflags __flags = __is.flags();
2690 __is.flags(__ios_base::dec | __ios_base::skipws);
2695 std::vector<_RealType> __int_vec;
2696 __int_vec.reserve(__n + 1);
2697 for (size_t __i = 0; __i <= __n; ++__i)
2701 __int_vec.push_back(__int);
2704 std::vector<double> __den_vec;
2705 __den_vec.reserve(__n + 1);
2706 for (size_t __i = 0; __i <= __n; ++__i)
2710 __den_vec.push_back(__den);
2713 __x.param(typename piecewise_linear_distribution<_RealType>::
2714 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2716 __is.flags(__flags);
2721 template<typename _IntType>
2722 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2724 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2725 _M_v.push_back(__detail::__mod<result_type,
2726 __detail::_Shift<result_type, 32>::__value>(*__iter));
2729 template<typename _InputIterator>
2730 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2732 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2733 _M_v.push_back(__detail::__mod<result_type,
2734 __detail::_Shift<result_type, 32>::__value>(*__iter));
2737 template<typename _RandomAccessIterator>
2739 seed_seq::generate(_RandomAccessIterator __begin,
2740 _RandomAccessIterator __end)
2742 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2745 if (__begin == __end)
2748 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2750 const size_t __n = __end - __begin;
2751 const size_t __s = _M_v.size();
2752 const size_t __t = (__n >= 623) ? 11
2757 const size_t __p = (__n - __t) / 2;
2758 const size_t __q = __p + __t;
2759 const size_t __m = std::max(__s + 1, __n);
2761 for (size_t __k = 0; __k < __m; ++__k)
2763 _Type __arg = (__begin[__k % __n]
2764 ^ __begin[(__k + __p) % __n]
2765 ^ __begin[(__k - 1) % __n]);
2766 _Type __r1 = __arg ^ (__arg << 27);
2767 __r1 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
2768 1664525u, 0u>(__r1);
2772 else if (__k <= __s)
2773 __r2 += __k % __n + _M_v[__k - 1];
2776 __r2 = __detail::__mod<_Type,
2777 __detail::_Shift<_Type, 32>::__value>(__r2);
2778 __begin[(__k + __p) % __n] += __r1;
2779 __begin[(__k + __q) % __n] += __r2;
2780 __begin[__k % __n] = __r2;
2783 for (size_t __k = __m; __k < __m + __n; ++__k)
2785 _Type __arg = (__begin[__k % __n]
2786 + __begin[(__k + __p) % __n]
2787 + __begin[(__k - 1) % __n]);
2788 _Type __r3 = __arg ^ (__arg << 27);
2789 __r3 = __detail::__mod<_Type, __detail::_Shift<_Type, 32>::__value,
2790 1566083941u, 0u>(__r3);
2791 _Type __r4 = __r3 - __k % __n;
2792 __r4 = __detail::__mod<_Type,
2793 __detail::_Shift<_Type, 32>::__value>(__r4);
2794 __begin[(__k + __p) % __n] ^= __r4;
2795 __begin[(__k + __q) % __n] ^= __r3;
2796 __begin[__k % __n] = __r4;
2800 template<typename _RealType, size_t __bits,
2801 typename _UniformRandomNumberGenerator>
2803 generate_canonical(_UniformRandomNumberGenerator& __urng)
2806 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2808 const long double __r = static_cast<long double>(__urng.max())
2809 - static_cast<long double>(__urng.min()) + 1.0L;
2810 const size_t __log2r = std::log(__r) / std::log(2.0L);
2811 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2812 _RealType __sum = _RealType(0);
2813 _RealType __tmp = _RealType(1);
2814 for (; __k != 0; --__k)
2816 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2819 return __sum / __tmp;
2821 _GLIBCXX_END_NAMESPACE