1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/random.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{random}
33 #include <numeric> // std::accumulate and std::partial_sum
35 namespace std _GLIBCXX_VISIBILITY(default)
38 * (Further) implementation-space details.
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 // General case for x = (ax + c) mod m -- use Schrage's algorithm to
45 // avoid integer overflow.
47 // Because a and c are compile-time integral constants the compiler
48 // kindly elides any unreachable paths.
50 // Preconditions: a > 0, m > 0.
52 // XXX FIXME: as-is, only works correctly for __m % __a < __m / __a.
54 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
64 static const _Tp __q = __m / __a;
65 static const _Tp __r = __m % __a;
67 _Tp __t1 = __a * (__x % __q);
68 _Tp __t2 = __r * (__x / __q);
72 __x = __m - __t2 + __t1;
77 const _Tp __d = __m - __x;
87 // Special case for m == 0 -- use unsigned integer overflow as modulo
89 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
90 struct _Mod<_Tp, __m, __a, __c, true>
94 { return __a * __x + __c; }
97 template<typename _InputIterator, typename _OutputIterator,
98 typename _UnaryOperation>
100 __transform(_InputIterator __first, _InputIterator __last,
101 _OutputIterator __result, _UnaryOperation __unary_op)
103 for (; __first != __last; ++__first, ++__result)
104 *__result = __unary_op(*__first);
108 _GLIBCXX_END_NAMESPACE_VERSION
109 } // namespace __detail
111 _GLIBCXX_BEGIN_NAMESPACE_VERSION
113 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
115 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
117 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
119 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
121 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
123 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
125 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
127 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
130 * Seeds the LCR with integral value @p __s, adjusted so that the
131 * ring identity is never a member of the convergence set.
133 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
135 linear_congruential_engine<_UIntType, __a, __c, __m>::
136 seed(result_type __s)
138 if ((__detail::__mod<_UIntType, __m>(__c) == 0)
139 && (__detail::__mod<_UIntType, __m>(__s) == 0))
142 _M_x = __detail::__mod<_UIntType, __m>(__s);
146 * Seeds the LCR engine with a value generated by @p __q.
148 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
149 template<typename _Sseq>
150 typename std::enable_if<std::is_class<_Sseq>::value>::type
151 linear_congruential_engine<_UIntType, __a, __c, __m>::
154 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
156 const _UIntType __k = (__k0 + 31) / 32;
157 uint_least32_t __arr[__k + 3];
158 __q.generate(__arr + 0, __arr + __k + 3);
159 _UIntType __factor = 1u;
160 _UIntType __sum = 0u;
161 for (size_t __j = 0; __j < __k; ++__j)
163 __sum += __arr[__j + 3] * __factor;
164 __factor *= __detail::_Shift<_UIntType, 32>::__value;
169 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
170 typename _CharT, typename _Traits>
171 std::basic_ostream<_CharT, _Traits>&
172 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
173 const linear_congruential_engine<_UIntType,
174 __a, __c, __m>& __lcr)
176 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
177 typedef typename __ostream_type::ios_base __ios_base;
179 const typename __ios_base::fmtflags __flags = __os.flags();
180 const _CharT __fill = __os.fill();
181 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
182 __os.fill(__os.widen(' '));
191 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
192 typename _CharT, typename _Traits>
193 std::basic_istream<_CharT, _Traits>&
194 operator>>(std::basic_istream<_CharT, _Traits>& __is,
195 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
197 typedef std::basic_istream<_CharT, _Traits> __istream_type;
198 typedef typename __istream_type::ios_base __ios_base;
200 const typename __ios_base::fmtflags __flags = __is.flags();
201 __is.flags(__ios_base::dec);
210 template<typename _UIntType,
211 size_t __w, size_t __n, size_t __m, size_t __r,
212 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
213 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
216 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
217 __s, __b, __t, __c, __l, __f>::word_size;
219 template<typename _UIntType,
220 size_t __w, size_t __n, size_t __m, size_t __r,
221 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
222 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
225 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
226 __s, __b, __t, __c, __l, __f>::state_size;
228 template<typename _UIntType,
229 size_t __w, size_t __n, size_t __m, size_t __r,
230 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
231 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
234 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
235 __s, __b, __t, __c, __l, __f>::shift_size;
237 template<typename _UIntType,
238 size_t __w, size_t __n, size_t __m, size_t __r,
239 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
240 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
243 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
244 __s, __b, __t, __c, __l, __f>::mask_bits;
246 template<typename _UIntType,
247 size_t __w, size_t __n, size_t __m, size_t __r,
248 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
249 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
252 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
253 __s, __b, __t, __c, __l, __f>::xor_mask;
255 template<typename _UIntType,
256 size_t __w, size_t __n, size_t __m, size_t __r,
257 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
258 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
261 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
262 __s, __b, __t, __c, __l, __f>::tempering_u;
264 template<typename _UIntType,
265 size_t __w, size_t __n, size_t __m, size_t __r,
266 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
267 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
270 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
271 __s, __b, __t, __c, __l, __f>::tempering_d;
273 template<typename _UIntType,
274 size_t __w, size_t __n, size_t __m, size_t __r,
275 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
276 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
279 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
280 __s, __b, __t, __c, __l, __f>::tempering_s;
282 template<typename _UIntType,
283 size_t __w, size_t __n, size_t __m, size_t __r,
284 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
285 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
288 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
289 __s, __b, __t, __c, __l, __f>::tempering_b;
291 template<typename _UIntType,
292 size_t __w, size_t __n, size_t __m, size_t __r,
293 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
294 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
297 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
298 __s, __b, __t, __c, __l, __f>::tempering_t;
300 template<typename _UIntType,
301 size_t __w, size_t __n, size_t __m, size_t __r,
302 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
303 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
306 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
307 __s, __b, __t, __c, __l, __f>::tempering_c;
309 template<typename _UIntType,
310 size_t __w, size_t __n, size_t __m, size_t __r,
311 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
312 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
315 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
316 __s, __b, __t, __c, __l, __f>::tempering_l;
318 template<typename _UIntType,
319 size_t __w, size_t __n, size_t __m, size_t __r,
320 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
321 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
324 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
325 __s, __b, __t, __c, __l, __f>::
326 initialization_multiplier;
328 template<typename _UIntType,
329 size_t __w, size_t __n, size_t __m, size_t __r,
330 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
331 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
334 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
335 __s, __b, __t, __c, __l, __f>::default_seed;
337 template<typename _UIntType,
338 size_t __w, size_t __n, size_t __m, size_t __r,
339 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
340 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
343 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
344 __s, __b, __t, __c, __l, __f>::
345 seed(result_type __sd)
347 _M_x[0] = __detail::__mod<_UIntType,
348 __detail::_Shift<_UIntType, __w>::__value>(__sd);
350 for (size_t __i = 1; __i < state_size; ++__i)
352 _UIntType __x = _M_x[__i - 1];
353 __x ^= __x >> (__w - 2);
355 __x += __detail::__mod<_UIntType, __n>(__i);
356 _M_x[__i] = __detail::__mod<_UIntType,
357 __detail::_Shift<_UIntType, __w>::__value>(__x);
362 template<typename _UIntType,
363 size_t __w, size_t __n, size_t __m, size_t __r,
364 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
365 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
367 template<typename _Sseq>
368 typename std::enable_if<std::is_class<_Sseq>::value>::type
369 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
370 __s, __b, __t, __c, __l, __f>::
373 const _UIntType __upper_mask = (~_UIntType()) << __r;
374 const size_t __k = (__w + 31) / 32;
375 uint_least32_t __arr[__n * __k];
376 __q.generate(__arr + 0, __arr + __n * __k);
379 for (size_t __i = 0; __i < state_size; ++__i)
381 _UIntType __factor = 1u;
382 _UIntType __sum = 0u;
383 for (size_t __j = 0; __j < __k; ++__j)
385 __sum += __arr[__k * __i + __j] * __factor;
386 __factor *= __detail::_Shift<_UIntType, 32>::__value;
388 _M_x[__i] = __detail::__mod<_UIntType,
389 __detail::_Shift<_UIntType, __w>::__value>(__sum);
395 if ((_M_x[0] & __upper_mask) != 0u)
398 else if (_M_x[__i] != 0u)
403 _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
406 template<typename _UIntType, size_t __w,
407 size_t __n, size_t __m, size_t __r,
408 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
409 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
412 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
413 __s, __b, __t, __c, __l, __f>::result_type
414 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
415 __s, __b, __t, __c, __l, __f>::
418 // Reload the vector - cost is O(n) amortized over n calls.
419 if (_M_p >= state_size)
421 const _UIntType __upper_mask = (~_UIntType()) << __r;
422 const _UIntType __lower_mask = ~__upper_mask;
424 for (size_t __k = 0; __k < (__n - __m); ++__k)
426 _UIntType __y = ((_M_x[__k] & __upper_mask)
427 | (_M_x[__k + 1] & __lower_mask));
428 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
429 ^ ((__y & 0x01) ? __a : 0));
432 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
434 _UIntType __y = ((_M_x[__k] & __upper_mask)
435 | (_M_x[__k + 1] & __lower_mask));
436 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
437 ^ ((__y & 0x01) ? __a : 0));
440 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
441 | (_M_x[0] & __lower_mask));
442 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
443 ^ ((__y & 0x01) ? __a : 0));
447 // Calculate o(x(i)).
448 result_type __z = _M_x[_M_p++];
449 __z ^= (__z >> __u) & __d;
450 __z ^= (__z << __s) & __b;
451 __z ^= (__z << __t) & __c;
457 template<typename _UIntType, size_t __w,
458 size_t __n, size_t __m, size_t __r,
459 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
460 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
461 _UIntType __f, typename _CharT, typename _Traits>
462 std::basic_ostream<_CharT, _Traits>&
463 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
464 const mersenne_twister_engine<_UIntType, __w, __n, __m,
465 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
467 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
468 typedef typename __ostream_type::ios_base __ios_base;
470 const typename __ios_base::fmtflags __flags = __os.flags();
471 const _CharT __fill = __os.fill();
472 const _CharT __space = __os.widen(' ');
473 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
476 for (size_t __i = 0; __i < __n; ++__i)
477 __os << __x._M_x[__i] << __space;
485 template<typename _UIntType, size_t __w,
486 size_t __n, size_t __m, size_t __r,
487 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
488 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
489 _UIntType __f, typename _CharT, typename _Traits>
490 std::basic_istream<_CharT, _Traits>&
491 operator>>(std::basic_istream<_CharT, _Traits>& __is,
492 mersenne_twister_engine<_UIntType, __w, __n, __m,
493 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
495 typedef std::basic_istream<_CharT, _Traits> __istream_type;
496 typedef typename __istream_type::ios_base __ios_base;
498 const typename __ios_base::fmtflags __flags = __is.flags();
499 __is.flags(__ios_base::dec | __ios_base::skipws);
501 for (size_t __i = 0; __i < __n; ++__i)
502 __is >> __x._M_x[__i];
510 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
512 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
514 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
516 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
518 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
520 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
522 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
524 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
526 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
528 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
529 seed(result_type __value)
531 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
532 __lcg(__value == 0u ? default_seed : __value);
534 const size_t __n = (__w + 31) / 32;
536 for (size_t __i = 0; __i < long_lag; ++__i)
538 _UIntType __sum = 0u;
539 _UIntType __factor = 1u;
540 for (size_t __j = 0; __j < __n; ++__j)
542 __sum += __detail::__mod<uint_least32_t,
543 __detail::_Shift<uint_least32_t, 32>::__value>
544 (__lcg()) * __factor;
545 __factor *= __detail::_Shift<_UIntType, 32>::__value;
547 _M_x[__i] = __detail::__mod<_UIntType,
548 __detail::_Shift<_UIntType, __w>::__value>(__sum);
550 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
554 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
555 template<typename _Sseq>
556 typename std::enable_if<std::is_class<_Sseq>::value>::type
557 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
560 const size_t __k = (__w + 31) / 32;
561 uint_least32_t __arr[__r * __k];
562 __q.generate(__arr + 0, __arr + __r * __k);
564 for (size_t __i = 0; __i < long_lag; ++__i)
566 _UIntType __sum = 0u;
567 _UIntType __factor = 1u;
568 for (size_t __j = 0; __j < __k; ++__j)
570 __sum += __arr[__k * __i + __j] * __factor;
571 __factor *= __detail::_Shift<_UIntType, 32>::__value;
573 _M_x[__i] = __detail::__mod<_UIntType,
574 __detail::_Shift<_UIntType, __w>::__value>(__sum);
576 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
580 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
581 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
583 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
586 // Derive short lag index from current index.
587 long __ps = _M_p - short_lag;
591 // Calculate new x(i) without overflow or division.
592 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
595 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
597 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
602 __xi = (__detail::_Shift<_UIntType, __w>::__value
603 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
608 // Adjust current index to loop around in ring buffer.
609 if (++_M_p >= long_lag)
615 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
616 typename _CharT, typename _Traits>
617 std::basic_ostream<_CharT, _Traits>&
618 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
619 const subtract_with_carry_engine<_UIntType,
622 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
623 typedef typename __ostream_type::ios_base __ios_base;
625 const typename __ios_base::fmtflags __flags = __os.flags();
626 const _CharT __fill = __os.fill();
627 const _CharT __space = __os.widen(' ');
628 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
631 for (size_t __i = 0; __i < __r; ++__i)
632 __os << __x._M_x[__i] << __space;
633 __os << __x._M_carry << __space << __x._M_p;
640 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
641 typename _CharT, typename _Traits>
642 std::basic_istream<_CharT, _Traits>&
643 operator>>(std::basic_istream<_CharT, _Traits>& __is,
644 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
646 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
647 typedef typename __istream_type::ios_base __ios_base;
649 const typename __ios_base::fmtflags __flags = __is.flags();
650 __is.flags(__ios_base::dec | __ios_base::skipws);
652 for (size_t __i = 0; __i < __r; ++__i)
653 __is >> __x._M_x[__i];
654 __is >> __x._M_carry;
662 template<typename _RandomNumberEngine, size_t __p, size_t __r>
664 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
666 template<typename _RandomNumberEngine, size_t __p, size_t __r>
668 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
670 template<typename _RandomNumberEngine, size_t __p, size_t __r>
671 typename discard_block_engine<_RandomNumberEngine,
672 __p, __r>::result_type
673 discard_block_engine<_RandomNumberEngine, __p, __r>::
676 if (_M_n >= used_block)
678 _M_b.discard(block_size - _M_n);
685 template<typename _RandomNumberEngine, size_t __p, size_t __r,
686 typename _CharT, typename _Traits>
687 std::basic_ostream<_CharT, _Traits>&
688 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
689 const discard_block_engine<_RandomNumberEngine,
692 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
693 typedef typename __ostream_type::ios_base __ios_base;
695 const typename __ios_base::fmtflags __flags = __os.flags();
696 const _CharT __fill = __os.fill();
697 const _CharT __space = __os.widen(' ');
698 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
701 __os << __x.base() << __space << __x._M_n;
708 template<typename _RandomNumberEngine, size_t __p, size_t __r,
709 typename _CharT, typename _Traits>
710 std::basic_istream<_CharT, _Traits>&
711 operator>>(std::basic_istream<_CharT, _Traits>& __is,
712 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
714 typedef std::basic_istream<_CharT, _Traits> __istream_type;
715 typedef typename __istream_type::ios_base __ios_base;
717 const typename __ios_base::fmtflags __flags = __is.flags();
718 __is.flags(__ios_base::dec | __ios_base::skipws);
720 __is >> __x._M_b >> __x._M_n;
727 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
728 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
730 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
733 typedef typename _RandomNumberEngine::result_type _Eresult_type;
734 const _Eresult_type __r
735 = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max()
736 ? _M_b.max() - _M_b.min() + 1 : 0);
737 const unsigned __edig = std::numeric_limits<_Eresult_type>::digits;
738 const unsigned __m = __r ? std::__lg(__r) : __edig;
740 typedef typename std::common_type<_Eresult_type, result_type>::type
742 const unsigned __cdig = std::numeric_limits<__ctype>::digits;
745 __ctype __s0, __s1, __y0, __y1;
747 for (size_t __i = 0; __i < 2; ++__i)
749 __n = (__w + __m - 1) / __m + __i;
750 __n0 = __n - __w % __n;
751 const unsigned __w0 = __w / __n; // __w0 <= __m
757 __s0 = __ctype(1) << __w0;
765 __y0 = __s0 * (__r / __s0);
767 __y1 = __s1 * (__r / __s1);
769 if (__r - __y0 <= __y0 / __n)
776 result_type __sum = 0;
777 for (size_t __k = 0; __k < __n0; ++__k)
781 __u = _M_b() - _M_b.min();
782 while (__y0 && __u >= __y0);
783 __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
785 for (size_t __k = __n0; __k < __n; ++__k)
789 __u = _M_b() - _M_b.min();
790 while (__y1 && __u >= __y1);
791 __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
797 template<typename _RandomNumberEngine, size_t __k>
799 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
801 template<typename _RandomNumberEngine, size_t __k>
802 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
803 shuffle_order_engine<_RandomNumberEngine, __k>::
806 size_t __j = __k * ((_M_y - _M_b.min())
807 / (_M_b.max() - _M_b.min() + 1.0L));
814 template<typename _RandomNumberEngine, size_t __k,
815 typename _CharT, typename _Traits>
816 std::basic_ostream<_CharT, _Traits>&
817 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
818 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
820 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
821 typedef typename __ostream_type::ios_base __ios_base;
823 const typename __ios_base::fmtflags __flags = __os.flags();
824 const _CharT __fill = __os.fill();
825 const _CharT __space = __os.widen(' ');
826 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
830 for (size_t __i = 0; __i < __k; ++__i)
831 __os << __space << __x._M_v[__i];
832 __os << __space << __x._M_y;
839 template<typename _RandomNumberEngine, size_t __k,
840 typename _CharT, typename _Traits>
841 std::basic_istream<_CharT, _Traits>&
842 operator>>(std::basic_istream<_CharT, _Traits>& __is,
843 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
845 typedef std::basic_istream<_CharT, _Traits> __istream_type;
846 typedef typename __istream_type::ios_base __ios_base;
848 const typename __ios_base::fmtflags __flags = __is.flags();
849 __is.flags(__ios_base::dec | __ios_base::skipws);
852 for (size_t __i = 0; __i < __k; ++__i)
853 __is >> __x._M_v[__i];
861 template<typename _IntType>
862 template<typename _UniformRandomNumberGenerator>
863 typename uniform_int_distribution<_IntType>::result_type
864 uniform_int_distribution<_IntType>::
865 operator()(_UniformRandomNumberGenerator& __urng,
866 const param_type& __param)
868 typedef typename _UniformRandomNumberGenerator::result_type
870 typedef typename std::make_unsigned<result_type>::type __utype;
871 typedef typename std::common_type<_Gresult_type, __utype>::type
874 const __uctype __urngmin = __urng.min();
875 const __uctype __urngmax = __urng.max();
876 const __uctype __urngrange = __urngmax - __urngmin;
877 const __uctype __urange
878 = __uctype(__param.b()) - __uctype(__param.a());
882 if (__urngrange > __urange)
885 const __uctype __uerange = __urange + 1; // __urange can be zero
886 const __uctype __scaling = __urngrange / __uerange;
887 const __uctype __past = __uerange * __scaling;
889 __ret = __uctype(__urng()) - __urngmin;
890 while (__ret >= __past);
893 else if (__urngrange < __urange)
897 Note that every value in [0, urange]
898 can be written uniquely as
900 (urngrange + 1) * high + low
904 high in [0, urange / (urngrange + 1)]
908 low in [0, urngrange].
910 __uctype __tmp; // wraparound control
913 const __uctype __uerngrange = __urngrange + 1;
914 __tmp = (__uerngrange * operator()
915 (__urng, param_type(0, __urange / __uerngrange)));
916 __ret = __tmp + (__uctype(__urng()) - __urngmin);
918 while (__ret > __urange || __ret < __tmp);
921 __ret = __uctype(__urng()) - __urngmin;
923 return __ret + __param.a();
926 template<typename _IntType, typename _CharT, typename _Traits>
927 std::basic_ostream<_CharT, _Traits>&
928 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
929 const uniform_int_distribution<_IntType>& __x)
931 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
932 typedef typename __ostream_type::ios_base __ios_base;
934 const typename __ios_base::fmtflags __flags = __os.flags();
935 const _CharT __fill = __os.fill();
936 const _CharT __space = __os.widen(' ');
937 __os.flags(__ios_base::scientific | __ios_base::left);
940 __os << __x.a() << __space << __x.b();
947 template<typename _IntType, typename _CharT, typename _Traits>
948 std::basic_istream<_CharT, _Traits>&
949 operator>>(std::basic_istream<_CharT, _Traits>& __is,
950 uniform_int_distribution<_IntType>& __x)
952 typedef std::basic_istream<_CharT, _Traits> __istream_type;
953 typedef typename __istream_type::ios_base __ios_base;
955 const typename __ios_base::fmtflags __flags = __is.flags();
956 __is.flags(__ios_base::dec | __ios_base::skipws);
960 __x.param(typename uniform_int_distribution<_IntType>::
961 param_type(__a, __b));
968 template<typename _RealType, typename _CharT, typename _Traits>
969 std::basic_ostream<_CharT, _Traits>&
970 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
971 const uniform_real_distribution<_RealType>& __x)
973 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
974 typedef typename __ostream_type::ios_base __ios_base;
976 const typename __ios_base::fmtflags __flags = __os.flags();
977 const _CharT __fill = __os.fill();
978 const std::streamsize __precision = __os.precision();
979 const _CharT __space = __os.widen(' ');
980 __os.flags(__ios_base::scientific | __ios_base::left);
982 __os.precision(std::numeric_limits<_RealType>::max_digits10);
984 __os << __x.a() << __space << __x.b();
988 __os.precision(__precision);
992 template<typename _RealType, typename _CharT, typename _Traits>
993 std::basic_istream<_CharT, _Traits>&
994 operator>>(std::basic_istream<_CharT, _Traits>& __is,
995 uniform_real_distribution<_RealType>& __x)
997 typedef std::basic_istream<_CharT, _Traits> __istream_type;
998 typedef typename __istream_type::ios_base __ios_base;
1000 const typename __ios_base::fmtflags __flags = __is.flags();
1001 __is.flags(__ios_base::skipws);
1005 __x.param(typename uniform_real_distribution<_RealType>::
1006 param_type(__a, __b));
1008 __is.flags(__flags);
1013 template<typename _CharT, typename _Traits>
1014 std::basic_ostream<_CharT, _Traits>&
1015 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1016 const bernoulli_distribution& __x)
1018 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1019 typedef typename __ostream_type::ios_base __ios_base;
1021 const typename __ios_base::fmtflags __flags = __os.flags();
1022 const _CharT __fill = __os.fill();
1023 const std::streamsize __precision = __os.precision();
1024 __os.flags(__ios_base::scientific | __ios_base::left);
1025 __os.fill(__os.widen(' '));
1026 __os.precision(std::numeric_limits<double>::max_digits10);
1030 __os.flags(__flags);
1032 __os.precision(__precision);
1037 template<typename _IntType>
1038 template<typename _UniformRandomNumberGenerator>
1039 typename geometric_distribution<_IntType>::result_type
1040 geometric_distribution<_IntType>::
1041 operator()(_UniformRandomNumberGenerator& __urng,
1042 const param_type& __param)
1044 // About the epsilon thing see this thread:
1045 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1046 const double __naf =
1047 (1 - std::numeric_limits<double>::epsilon()) / 2;
1048 // The largest _RealType convertible to _IntType.
1049 const double __thr =
1050 std::numeric_limits<_IntType>::max() + __naf;
1051 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1056 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1057 while (__cand >= __thr);
1059 return result_type(__cand + __naf);
1062 template<typename _IntType,
1063 typename _CharT, typename _Traits>
1064 std::basic_ostream<_CharT, _Traits>&
1065 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1066 const geometric_distribution<_IntType>& __x)
1068 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1069 typedef typename __ostream_type::ios_base __ios_base;
1071 const typename __ios_base::fmtflags __flags = __os.flags();
1072 const _CharT __fill = __os.fill();
1073 const std::streamsize __precision = __os.precision();
1074 __os.flags(__ios_base::scientific | __ios_base::left);
1075 __os.fill(__os.widen(' '));
1076 __os.precision(std::numeric_limits<double>::max_digits10);
1080 __os.flags(__flags);
1082 __os.precision(__precision);
1086 template<typename _IntType,
1087 typename _CharT, typename _Traits>
1088 std::basic_istream<_CharT, _Traits>&
1089 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1090 geometric_distribution<_IntType>& __x)
1092 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1093 typedef typename __istream_type::ios_base __ios_base;
1095 const typename __ios_base::fmtflags __flags = __is.flags();
1096 __is.flags(__ios_base::skipws);
1100 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1102 __is.flags(__flags);
1106 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1107 template<typename _IntType>
1108 template<typename _UniformRandomNumberGenerator>
1109 typename negative_binomial_distribution<_IntType>::result_type
1110 negative_binomial_distribution<_IntType>::
1111 operator()(_UniformRandomNumberGenerator& __urng)
1113 const double __y = _M_gd(__urng);
1115 // XXX Is the constructor too slow?
1116 std::poisson_distribution<result_type> __poisson(__y);
1117 return __poisson(__urng);
1120 template<typename _IntType>
1121 template<typename _UniformRandomNumberGenerator>
1122 typename negative_binomial_distribution<_IntType>::result_type
1123 negative_binomial_distribution<_IntType>::
1124 operator()(_UniformRandomNumberGenerator& __urng,
1125 const param_type& __p)
1127 typedef typename std::gamma_distribution<result_type>::param_type
1131 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1133 std::poisson_distribution<result_type> __poisson(__y);
1134 return __poisson(__urng);
1137 template<typename _IntType, typename _CharT, typename _Traits>
1138 std::basic_ostream<_CharT, _Traits>&
1139 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1140 const negative_binomial_distribution<_IntType>& __x)
1142 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1143 typedef typename __ostream_type::ios_base __ios_base;
1145 const typename __ios_base::fmtflags __flags = __os.flags();
1146 const _CharT __fill = __os.fill();
1147 const std::streamsize __precision = __os.precision();
1148 const _CharT __space = __os.widen(' ');
1149 __os.flags(__ios_base::scientific | __ios_base::left);
1150 __os.fill(__os.widen(' '));
1151 __os.precision(std::numeric_limits<double>::max_digits10);
1153 __os << __x.k() << __space << __x.p()
1154 << __space << __x._M_gd;
1156 __os.flags(__flags);
1158 __os.precision(__precision);
1162 template<typename _IntType, typename _CharT, typename _Traits>
1163 std::basic_istream<_CharT, _Traits>&
1164 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1165 negative_binomial_distribution<_IntType>& __x)
1167 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1168 typedef typename __istream_type::ios_base __ios_base;
1170 const typename __ios_base::fmtflags __flags = __is.flags();
1171 __is.flags(__ios_base::skipws);
1175 __is >> __k >> __p >> __x._M_gd;
1176 __x.param(typename negative_binomial_distribution<_IntType>::
1177 param_type(__k, __p));
1179 __is.flags(__flags);
1184 template<typename _IntType>
1186 poisson_distribution<_IntType>::param_type::
1189 #if _GLIBCXX_USE_C99_MATH_TR1
1192 const double __m = std::floor(_M_mean);
1193 _M_lm_thr = std::log(_M_mean);
1194 _M_lfm = std::lgamma(__m + 1);
1195 _M_sm = std::sqrt(__m);
1197 const double __pi_4 = 0.7853981633974483096156608458198757L;
1198 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1200 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1201 const double __cx = 2 * __m + _M_d;
1202 _M_scx = std::sqrt(__cx / 2);
1205 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1206 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1211 _M_lm_thr = std::exp(-_M_mean);
1215 * A rejection algorithm when mean >= 12 and a simple method based
1216 * upon the multiplication of uniform random variates otherwise.
1217 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1221 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1222 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1224 template<typename _IntType>
1225 template<typename _UniformRandomNumberGenerator>
1226 typename poisson_distribution<_IntType>::result_type
1227 poisson_distribution<_IntType>::
1228 operator()(_UniformRandomNumberGenerator& __urng,
1229 const param_type& __param)
1231 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1233 #if _GLIBCXX_USE_C99_MATH_TR1
1234 if (__param.mean() >= 12)
1238 // See comments above...
1239 const double __naf =
1240 (1 - std::numeric_limits<double>::epsilon()) / 2;
1241 const double __thr =
1242 std::numeric_limits<_IntType>::max() + __naf;
1244 const double __m = std::floor(__param.mean());
1246 const double __spi_2 = 1.2533141373155002512078826424055226L;
1247 const double __c1 = __param._M_sm * __spi_2;
1248 const double __c2 = __param._M_c2b + __c1;
1249 const double __c3 = __c2 + 1;
1250 const double __c4 = __c3 + 1;
1252 const double __e178 = 1.0129030479320018583185514777512983L;
1253 const double __c5 = __c4 + __e178;
1254 const double __c = __param._M_cb + __c5;
1255 const double __2cx = 2 * (2 * __m + __param._M_d);
1257 bool __reject = true;
1260 const double __u = __c * __aurng();
1261 const double __e = -std::log(__aurng());
1267 const double __n = _M_nd(__urng);
1268 const double __y = -std::abs(__n) * __param._M_sm - 1;
1269 __x = std::floor(__y);
1270 __w = -__n * __n / 2;
1274 else if (__u <= __c2)
1276 const double __n = _M_nd(__urng);
1277 const double __y = 1 + std::abs(__n) * __param._M_scx;
1278 __x = std::ceil(__y);
1279 __w = __y * (2 - __y) * __param._M_1cx;
1280 if (__x > __param._M_d)
1283 else if (__u <= __c3)
1284 // NB: This case not in the book, nor in the Errata,
1285 // but should be ok...
1287 else if (__u <= __c4)
1289 else if (__u <= __c5)
1293 const double __v = -std::log(__aurng());
1294 const double __y = __param._M_d
1295 + __v * __2cx / __param._M_d;
1296 __x = std::ceil(__y);
1297 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1300 __reject = (__w - __e - __x * __param._M_lm_thr
1301 > __param._M_lfm - std::lgamma(__x + __m + 1));
1303 __reject |= __x + __m >= __thr;
1307 return result_type(__x + __m + __naf);
1313 double __prod = 1.0;
1317 __prod *= __aurng();
1320 while (__prod > __param._M_lm_thr);
1326 template<typename _IntType,
1327 typename _CharT, typename _Traits>
1328 std::basic_ostream<_CharT, _Traits>&
1329 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1330 const poisson_distribution<_IntType>& __x)
1332 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1333 typedef typename __ostream_type::ios_base __ios_base;
1335 const typename __ios_base::fmtflags __flags = __os.flags();
1336 const _CharT __fill = __os.fill();
1337 const std::streamsize __precision = __os.precision();
1338 const _CharT __space = __os.widen(' ');
1339 __os.flags(__ios_base::scientific | __ios_base::left);
1341 __os.precision(std::numeric_limits<double>::max_digits10);
1343 __os << __x.mean() << __space << __x._M_nd;
1345 __os.flags(__flags);
1347 __os.precision(__precision);
1351 template<typename _IntType,
1352 typename _CharT, typename _Traits>
1353 std::basic_istream<_CharT, _Traits>&
1354 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1355 poisson_distribution<_IntType>& __x)
1357 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1358 typedef typename __istream_type::ios_base __ios_base;
1360 const typename __ios_base::fmtflags __flags = __is.flags();
1361 __is.flags(__ios_base::skipws);
1364 __is >> __mean >> __x._M_nd;
1365 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1367 __is.flags(__flags);
1372 template<typename _IntType>
1374 binomial_distribution<_IntType>::param_type::
1377 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1381 #if _GLIBCXX_USE_C99_MATH_TR1
1382 if (_M_t * __p12 >= 8)
1385 const double __np = std::floor(_M_t * __p12);
1386 const double __pa = __np / _M_t;
1387 const double __1p = 1 - __pa;
1389 const double __pi_4 = 0.7853981633974483096156608458198757L;
1390 const double __d1x =
1391 std::sqrt(__np * __1p * std::log(32 * __np
1392 / (81 * __pi_4 * __1p)));
1393 _M_d1 = std::round(std::max(1.0, __d1x));
1394 const double __d2x =
1395 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1396 / (__pi_4 * __pa)));
1397 _M_d2 = std::round(std::max(1.0, __d2x));
1400 const double __spi_2 = 1.2533141373155002512078826424055226L;
1401 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1402 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1403 _M_c = 2 * _M_d1 / __np;
1404 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1405 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1406 const double __s1s = _M_s1 * _M_s1;
1407 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1409 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1410 const double __s2s = _M_s2 * _M_s2;
1411 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1412 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1413 _M_lf = (std::lgamma(__np + 1)
1414 + std::lgamma(_M_t - __np + 1));
1415 _M_lp1p = std::log(__pa / __1p);
1417 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1421 _M_q = -std::log(1 - __p12);
1424 template<typename _IntType>
1425 template<typename _UniformRandomNumberGenerator>
1426 typename binomial_distribution<_IntType>::result_type
1427 binomial_distribution<_IntType>::
1428 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1432 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1437 const double __e = -std::log(__aurng());
1438 __sum += __e / (__t - __x);
1441 while (__sum <= _M_param._M_q);
1447 * A rejection algorithm when t * p >= 8 and a simple waiting time
1448 * method - the second in the referenced book - otherwise.
1449 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1453 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1454 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1456 template<typename _IntType>
1457 template<typename _UniformRandomNumberGenerator>
1458 typename binomial_distribution<_IntType>::result_type
1459 binomial_distribution<_IntType>::
1460 operator()(_UniformRandomNumberGenerator& __urng,
1461 const param_type& __param)
1464 const _IntType __t = __param.t();
1465 const double __p = __param.p();
1466 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1467 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1470 #if _GLIBCXX_USE_C99_MATH_TR1
1471 if (!__param._M_easy)
1475 // See comments above...
1476 const double __naf =
1477 (1 - std::numeric_limits<double>::epsilon()) / 2;
1478 const double __thr =
1479 std::numeric_limits<_IntType>::max() + __naf;
1481 const double __np = std::floor(__t * __p12);
1484 const double __spi_2 = 1.2533141373155002512078826424055226L;
1485 const double __a1 = __param._M_a1;
1486 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1487 const double __a123 = __param._M_a123;
1488 const double __s1s = __param._M_s1 * __param._M_s1;
1489 const double __s2s = __param._M_s2 * __param._M_s2;
1494 const double __u = __param._M_s * __aurng();
1500 const double __n = _M_nd(__urng);
1501 const double __y = __param._M_s1 * std::abs(__n);
1502 __reject = __y >= __param._M_d1;
1505 const double __e = -std::log(__aurng());
1506 __x = std::floor(__y);
1507 __v = -__e - __n * __n / 2 + __param._M_c;
1510 else if (__u <= __a12)
1512 const double __n = _M_nd(__urng);
1513 const double __y = __param._M_s2 * std::abs(__n);
1514 __reject = __y >= __param._M_d2;
1517 const double __e = -std::log(__aurng());
1518 __x = std::floor(-__y);
1519 __v = -__e - __n * __n / 2;
1522 else if (__u <= __a123)
1524 const double __e1 = -std::log(__aurng());
1525 const double __e2 = -std::log(__aurng());
1527 const double __y = __param._M_d1
1528 + 2 * __s1s * __e1 / __param._M_d1;
1529 __x = std::floor(__y);
1530 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1531 -__y / (2 * __s1s)));
1536 const double __e1 = -std::log(__aurng());
1537 const double __e2 = -std::log(__aurng());
1539 const double __y = __param._M_d2
1540 + 2 * __s2s * __e1 / __param._M_d2;
1541 __x = std::floor(-__y);
1542 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1546 __reject = __reject || __x < -__np || __x > __t - __np;
1549 const double __lfx =
1550 std::lgamma(__np + __x + 1)
1551 + std::lgamma(__t - (__np + __x) + 1);
1552 __reject = __v > __param._M_lf - __lfx
1553 + __x * __param._M_lp1p;
1556 __reject |= __x + __np >= __thr;
1560 __x += __np + __naf;
1562 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1563 __ret = _IntType(__x) + __z;
1567 __ret = _M_waiting(__urng, __t);
1570 __ret = __t - __ret;
1574 template<typename _IntType,
1575 typename _CharT, typename _Traits>
1576 std::basic_ostream<_CharT, _Traits>&
1577 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1578 const binomial_distribution<_IntType>& __x)
1580 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1581 typedef typename __ostream_type::ios_base __ios_base;
1583 const typename __ios_base::fmtflags __flags = __os.flags();
1584 const _CharT __fill = __os.fill();
1585 const std::streamsize __precision = __os.precision();
1586 const _CharT __space = __os.widen(' ');
1587 __os.flags(__ios_base::scientific | __ios_base::left);
1589 __os.precision(std::numeric_limits<double>::max_digits10);
1591 __os << __x.t() << __space << __x.p()
1592 << __space << __x._M_nd;
1594 __os.flags(__flags);
1596 __os.precision(__precision);
1600 template<typename _IntType,
1601 typename _CharT, typename _Traits>
1602 std::basic_istream<_CharT, _Traits>&
1603 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1604 binomial_distribution<_IntType>& __x)
1606 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1607 typedef typename __istream_type::ios_base __ios_base;
1609 const typename __ios_base::fmtflags __flags = __is.flags();
1610 __is.flags(__ios_base::dec | __ios_base::skipws);
1614 __is >> __t >> __p >> __x._M_nd;
1615 __x.param(typename binomial_distribution<_IntType>::
1616 param_type(__t, __p));
1618 __is.flags(__flags);
1623 template<typename _RealType, typename _CharT, typename _Traits>
1624 std::basic_ostream<_CharT, _Traits>&
1625 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1626 const exponential_distribution<_RealType>& __x)
1628 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1629 typedef typename __ostream_type::ios_base __ios_base;
1631 const typename __ios_base::fmtflags __flags = __os.flags();
1632 const _CharT __fill = __os.fill();
1633 const std::streamsize __precision = __os.precision();
1634 __os.flags(__ios_base::scientific | __ios_base::left);
1635 __os.fill(__os.widen(' '));
1636 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1638 __os << __x.lambda();
1640 __os.flags(__flags);
1642 __os.precision(__precision);
1646 template<typename _RealType, typename _CharT, typename _Traits>
1647 std::basic_istream<_CharT, _Traits>&
1648 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1649 exponential_distribution<_RealType>& __x)
1651 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1652 typedef typename __istream_type::ios_base __ios_base;
1654 const typename __ios_base::fmtflags __flags = __is.flags();
1655 __is.flags(__ios_base::dec | __ios_base::skipws);
1659 __x.param(typename exponential_distribution<_RealType>::
1660 param_type(__lambda));
1662 __is.flags(__flags);
1668 * Polar method due to Marsaglia.
1670 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1671 * New York, 1986, Ch. V, Sect. 4.4.
1673 template<typename _RealType>
1674 template<typename _UniformRandomNumberGenerator>
1675 typename normal_distribution<_RealType>::result_type
1676 normal_distribution<_RealType>::
1677 operator()(_UniformRandomNumberGenerator& __urng,
1678 const param_type& __param)
1681 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1684 if (_M_saved_available)
1686 _M_saved_available = false;
1691 result_type __x, __y, __r2;
1694 __x = result_type(2.0) * __aurng() - 1.0;
1695 __y = result_type(2.0) * __aurng() - 1.0;
1696 __r2 = __x * __x + __y * __y;
1698 while (__r2 > 1.0 || __r2 == 0.0);
1700 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1701 _M_saved = __x * __mult;
1702 _M_saved_available = true;
1703 __ret = __y * __mult;
1706 __ret = __ret * __param.stddev() + __param.mean();
1710 template<typename _RealType>
1712 operator==(const std::normal_distribution<_RealType>& __d1,
1713 const std::normal_distribution<_RealType>& __d2)
1715 if (__d1._M_param == __d2._M_param
1716 && __d1._M_saved_available == __d2._M_saved_available)
1718 if (__d1._M_saved_available
1719 && __d1._M_saved == __d2._M_saved)
1721 else if(!__d1._M_saved_available)
1730 template<typename _RealType, typename _CharT, typename _Traits>
1731 std::basic_ostream<_CharT, _Traits>&
1732 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1733 const normal_distribution<_RealType>& __x)
1735 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1736 typedef typename __ostream_type::ios_base __ios_base;
1738 const typename __ios_base::fmtflags __flags = __os.flags();
1739 const _CharT __fill = __os.fill();
1740 const std::streamsize __precision = __os.precision();
1741 const _CharT __space = __os.widen(' ');
1742 __os.flags(__ios_base::scientific | __ios_base::left);
1744 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1746 __os << __x.mean() << __space << __x.stddev()
1747 << __space << __x._M_saved_available;
1748 if (__x._M_saved_available)
1749 __os << __space << __x._M_saved;
1751 __os.flags(__flags);
1753 __os.precision(__precision);
1757 template<typename _RealType, typename _CharT, typename _Traits>
1758 std::basic_istream<_CharT, _Traits>&
1759 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1760 normal_distribution<_RealType>& __x)
1762 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1763 typedef typename __istream_type::ios_base __ios_base;
1765 const typename __ios_base::fmtflags __flags = __is.flags();
1766 __is.flags(__ios_base::dec | __ios_base::skipws);
1768 double __mean, __stddev;
1769 __is >> __mean >> __stddev
1770 >> __x._M_saved_available;
1771 if (__x._M_saved_available)
1772 __is >> __x._M_saved;
1773 __x.param(typename normal_distribution<_RealType>::
1774 param_type(__mean, __stddev));
1776 __is.flags(__flags);
1781 template<typename _RealType, typename _CharT, typename _Traits>
1782 std::basic_ostream<_CharT, _Traits>&
1783 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1784 const lognormal_distribution<_RealType>& __x)
1786 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1787 typedef typename __ostream_type::ios_base __ios_base;
1789 const typename __ios_base::fmtflags __flags = __os.flags();
1790 const _CharT __fill = __os.fill();
1791 const std::streamsize __precision = __os.precision();
1792 const _CharT __space = __os.widen(' ');
1793 __os.flags(__ios_base::scientific | __ios_base::left);
1795 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1797 __os << __x.m() << __space << __x.s()
1798 << __space << __x._M_nd;
1800 __os.flags(__flags);
1802 __os.precision(__precision);
1806 template<typename _RealType, typename _CharT, typename _Traits>
1807 std::basic_istream<_CharT, _Traits>&
1808 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1809 lognormal_distribution<_RealType>& __x)
1811 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1812 typedef typename __istream_type::ios_base __ios_base;
1814 const typename __ios_base::fmtflags __flags = __is.flags();
1815 __is.flags(__ios_base::dec | __ios_base::skipws);
1818 __is >> __m >> __s >> __x._M_nd;
1819 __x.param(typename lognormal_distribution<_RealType>::
1820 param_type(__m, __s));
1822 __is.flags(__flags);
1827 template<typename _RealType, typename _CharT, typename _Traits>
1828 std::basic_ostream<_CharT, _Traits>&
1829 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1830 const chi_squared_distribution<_RealType>& __x)
1832 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1833 typedef typename __ostream_type::ios_base __ios_base;
1835 const typename __ios_base::fmtflags __flags = __os.flags();
1836 const _CharT __fill = __os.fill();
1837 const std::streamsize __precision = __os.precision();
1838 const _CharT __space = __os.widen(' ');
1839 __os.flags(__ios_base::scientific | __ios_base::left);
1841 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1843 __os << __x.n() << __space << __x._M_gd;
1845 __os.flags(__flags);
1847 __os.precision(__precision);
1851 template<typename _RealType, typename _CharT, typename _Traits>
1852 std::basic_istream<_CharT, _Traits>&
1853 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1854 chi_squared_distribution<_RealType>& __x)
1856 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1857 typedef typename __istream_type::ios_base __ios_base;
1859 const typename __ios_base::fmtflags __flags = __is.flags();
1860 __is.flags(__ios_base::dec | __ios_base::skipws);
1863 __is >> __n >> __x._M_gd;
1864 __x.param(typename chi_squared_distribution<_RealType>::
1867 __is.flags(__flags);
1872 template<typename _RealType>
1873 template<typename _UniformRandomNumberGenerator>
1874 typename cauchy_distribution<_RealType>::result_type
1875 cauchy_distribution<_RealType>::
1876 operator()(_UniformRandomNumberGenerator& __urng,
1877 const param_type& __p)
1879 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1886 const _RealType __pi = 3.1415926535897932384626433832795029L;
1887 return __p.a() + __p.b() * std::tan(__pi * __u);
1890 template<typename _RealType, typename _CharT, typename _Traits>
1891 std::basic_ostream<_CharT, _Traits>&
1892 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1893 const cauchy_distribution<_RealType>& __x)
1895 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1896 typedef typename __ostream_type::ios_base __ios_base;
1898 const typename __ios_base::fmtflags __flags = __os.flags();
1899 const _CharT __fill = __os.fill();
1900 const std::streamsize __precision = __os.precision();
1901 const _CharT __space = __os.widen(' ');
1902 __os.flags(__ios_base::scientific | __ios_base::left);
1904 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1906 __os << __x.a() << __space << __x.b();
1908 __os.flags(__flags);
1910 __os.precision(__precision);
1914 template<typename _RealType, typename _CharT, typename _Traits>
1915 std::basic_istream<_CharT, _Traits>&
1916 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1917 cauchy_distribution<_RealType>& __x)
1919 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1920 typedef typename __istream_type::ios_base __ios_base;
1922 const typename __ios_base::fmtflags __flags = __is.flags();
1923 __is.flags(__ios_base::dec | __ios_base::skipws);
1927 __x.param(typename cauchy_distribution<_RealType>::
1928 param_type(__a, __b));
1930 __is.flags(__flags);
1935 template<typename _RealType, typename _CharT, typename _Traits>
1936 std::basic_ostream<_CharT, _Traits>&
1937 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1938 const fisher_f_distribution<_RealType>& __x)
1940 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1941 typedef typename __ostream_type::ios_base __ios_base;
1943 const typename __ios_base::fmtflags __flags = __os.flags();
1944 const _CharT __fill = __os.fill();
1945 const std::streamsize __precision = __os.precision();
1946 const _CharT __space = __os.widen(' ');
1947 __os.flags(__ios_base::scientific | __ios_base::left);
1949 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1951 __os << __x.m() << __space << __x.n()
1952 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1954 __os.flags(__flags);
1956 __os.precision(__precision);
1960 template<typename _RealType, typename _CharT, typename _Traits>
1961 std::basic_istream<_CharT, _Traits>&
1962 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1963 fisher_f_distribution<_RealType>& __x)
1965 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1966 typedef typename __istream_type::ios_base __ios_base;
1968 const typename __ios_base::fmtflags __flags = __is.flags();
1969 __is.flags(__ios_base::dec | __ios_base::skipws);
1972 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1973 __x.param(typename fisher_f_distribution<_RealType>::
1974 param_type(__m, __n));
1976 __is.flags(__flags);
1981 template<typename _RealType, typename _CharT, typename _Traits>
1982 std::basic_ostream<_CharT, _Traits>&
1983 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1984 const student_t_distribution<_RealType>& __x)
1986 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1987 typedef typename __ostream_type::ios_base __ios_base;
1989 const typename __ios_base::fmtflags __flags = __os.flags();
1990 const _CharT __fill = __os.fill();
1991 const std::streamsize __precision = __os.precision();
1992 const _CharT __space = __os.widen(' ');
1993 __os.flags(__ios_base::scientific | __ios_base::left);
1995 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1997 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1999 __os.flags(__flags);
2001 __os.precision(__precision);
2005 template<typename _RealType, typename _CharT, typename _Traits>
2006 std::basic_istream<_CharT, _Traits>&
2007 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2008 student_t_distribution<_RealType>& __x)
2010 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2011 typedef typename __istream_type::ios_base __ios_base;
2013 const typename __ios_base::fmtflags __flags = __is.flags();
2014 __is.flags(__ios_base::dec | __ios_base::skipws);
2017 __is >> __n >> __x._M_nd >> __x._M_gd;
2018 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
2020 __is.flags(__flags);
2025 template<typename _RealType>
2027 gamma_distribution<_RealType>::param_type::
2030 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2032 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2033 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2037 * Marsaglia, G. and Tsang, W. W.
2038 * "A Simple Method for Generating Gamma Variables"
2039 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2041 template<typename _RealType>
2042 template<typename _UniformRandomNumberGenerator>
2043 typename gamma_distribution<_RealType>::result_type
2044 gamma_distribution<_RealType>::
2045 operator()(_UniformRandomNumberGenerator& __urng,
2046 const param_type& __param)
2048 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2051 result_type __u, __v, __n;
2052 const result_type __a1 = (__param._M_malpha
2053 - _RealType(1.0) / _RealType(3.0));
2059 __n = _M_nd(__urng);
2060 __v = result_type(1.0) + __param._M_a2 * __n;
2064 __v = __v * __v * __v;
2067 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2068 && (std::log(__u) > (0.5 * __n * __n + __a1
2069 * (1.0 - __v + std::log(__v)))));
2071 if (__param.alpha() == __param._M_malpha)
2072 return __a1 * __v * __param.beta();
2079 return (std::pow(__u, result_type(1.0) / __param.alpha())
2080 * __a1 * __v * __param.beta());
2084 template<typename _RealType, typename _CharT, typename _Traits>
2085 std::basic_ostream<_CharT, _Traits>&
2086 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2087 const gamma_distribution<_RealType>& __x)
2089 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2090 typedef typename __ostream_type::ios_base __ios_base;
2092 const typename __ios_base::fmtflags __flags = __os.flags();
2093 const _CharT __fill = __os.fill();
2094 const std::streamsize __precision = __os.precision();
2095 const _CharT __space = __os.widen(' ');
2096 __os.flags(__ios_base::scientific | __ios_base::left);
2098 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2100 __os << __x.alpha() << __space << __x.beta()
2101 << __space << __x._M_nd;
2103 __os.flags(__flags);
2105 __os.precision(__precision);
2109 template<typename _RealType, typename _CharT, typename _Traits>
2110 std::basic_istream<_CharT, _Traits>&
2111 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2112 gamma_distribution<_RealType>& __x)
2114 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2115 typedef typename __istream_type::ios_base __ios_base;
2117 const typename __ios_base::fmtflags __flags = __is.flags();
2118 __is.flags(__ios_base::dec | __ios_base::skipws);
2120 _RealType __alpha_val, __beta_val;
2121 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2122 __x.param(typename gamma_distribution<_RealType>::
2123 param_type(__alpha_val, __beta_val));
2125 __is.flags(__flags);
2130 template<typename _RealType>
2131 template<typename _UniformRandomNumberGenerator>
2132 typename weibull_distribution<_RealType>::result_type
2133 weibull_distribution<_RealType>::
2134 operator()(_UniformRandomNumberGenerator& __urng,
2135 const param_type& __p)
2137 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2139 return __p.b() * std::pow(-std::log(__aurng()),
2140 result_type(1) / __p.a());
2143 template<typename _RealType, typename _CharT, typename _Traits>
2144 std::basic_ostream<_CharT, _Traits>&
2145 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2146 const weibull_distribution<_RealType>& __x)
2148 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2149 typedef typename __ostream_type::ios_base __ios_base;
2151 const typename __ios_base::fmtflags __flags = __os.flags();
2152 const _CharT __fill = __os.fill();
2153 const std::streamsize __precision = __os.precision();
2154 const _CharT __space = __os.widen(' ');
2155 __os.flags(__ios_base::scientific | __ios_base::left);
2157 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2159 __os << __x.a() << __space << __x.b();
2161 __os.flags(__flags);
2163 __os.precision(__precision);
2167 template<typename _RealType, typename _CharT, typename _Traits>
2168 std::basic_istream<_CharT, _Traits>&
2169 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2170 weibull_distribution<_RealType>& __x)
2172 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2173 typedef typename __istream_type::ios_base __ios_base;
2175 const typename __ios_base::fmtflags __flags = __is.flags();
2176 __is.flags(__ios_base::dec | __ios_base::skipws);
2180 __x.param(typename weibull_distribution<_RealType>::
2181 param_type(__a, __b));
2183 __is.flags(__flags);
2188 template<typename _RealType>
2189 template<typename _UniformRandomNumberGenerator>
2190 typename extreme_value_distribution<_RealType>::result_type
2191 extreme_value_distribution<_RealType>::
2192 operator()(_UniformRandomNumberGenerator& __urng,
2193 const param_type& __p)
2195 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2197 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2200 template<typename _RealType, typename _CharT, typename _Traits>
2201 std::basic_ostream<_CharT, _Traits>&
2202 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2203 const extreme_value_distribution<_RealType>& __x)
2205 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2206 typedef typename __ostream_type::ios_base __ios_base;
2208 const typename __ios_base::fmtflags __flags = __os.flags();
2209 const _CharT __fill = __os.fill();
2210 const std::streamsize __precision = __os.precision();
2211 const _CharT __space = __os.widen(' ');
2212 __os.flags(__ios_base::scientific | __ios_base::left);
2214 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2216 __os << __x.a() << __space << __x.b();
2218 __os.flags(__flags);
2220 __os.precision(__precision);
2224 template<typename _RealType, typename _CharT, typename _Traits>
2225 std::basic_istream<_CharT, _Traits>&
2226 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2227 extreme_value_distribution<_RealType>& __x)
2229 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2230 typedef typename __istream_type::ios_base __ios_base;
2232 const typename __ios_base::fmtflags __flags = __is.flags();
2233 __is.flags(__ios_base::dec | __ios_base::skipws);
2237 __x.param(typename extreme_value_distribution<_RealType>::
2238 param_type(__a, __b));
2240 __is.flags(__flags);
2245 template<typename _IntType>
2247 discrete_distribution<_IntType>::param_type::
2250 if (_M_prob.size() < 2)
2256 const double __sum = std::accumulate(_M_prob.begin(),
2257 _M_prob.end(), 0.0);
2258 // Now normalize the probabilites.
2259 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2260 std::bind2nd(std::divides<double>(), __sum));
2261 // Accumulate partial sums.
2262 _M_cp.reserve(_M_prob.size());
2263 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2264 std::back_inserter(_M_cp));
2265 // Make sure the last cumulative probability is one.
2266 _M_cp[_M_cp.size() - 1] = 1.0;
2269 template<typename _IntType>
2270 template<typename _Func>
2271 discrete_distribution<_IntType>::param_type::
2272 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2273 : _M_prob(), _M_cp()
2275 const size_t __n = __nw == 0 ? 1 : __nw;
2276 const double __delta = (__xmax - __xmin) / __n;
2278 _M_prob.reserve(__n);
2279 for (size_t __k = 0; __k < __nw; ++__k)
2280 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2285 template<typename _IntType>
2286 template<typename _UniformRandomNumberGenerator>
2287 typename discrete_distribution<_IntType>::result_type
2288 discrete_distribution<_IntType>::
2289 operator()(_UniformRandomNumberGenerator& __urng,
2290 const param_type& __param)
2292 if (__param._M_cp.empty())
2293 return result_type(0);
2295 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2298 const double __p = __aurng();
2299 auto __pos = std::lower_bound(__param._M_cp.begin(),
2300 __param._M_cp.end(), __p);
2302 return __pos - __param._M_cp.begin();
2305 template<typename _IntType, typename _CharT, typename _Traits>
2306 std::basic_ostream<_CharT, _Traits>&
2307 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2308 const discrete_distribution<_IntType>& __x)
2310 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2311 typedef typename __ostream_type::ios_base __ios_base;
2313 const typename __ios_base::fmtflags __flags = __os.flags();
2314 const _CharT __fill = __os.fill();
2315 const std::streamsize __precision = __os.precision();
2316 const _CharT __space = __os.widen(' ');
2317 __os.flags(__ios_base::scientific | __ios_base::left);
2319 __os.precision(std::numeric_limits<double>::max_digits10);
2321 std::vector<double> __prob = __x.probabilities();
2322 __os << __prob.size();
2323 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2324 __os << __space << *__dit;
2326 __os.flags(__flags);
2328 __os.precision(__precision);
2332 template<typename _IntType, typename _CharT, typename _Traits>
2333 std::basic_istream<_CharT, _Traits>&
2334 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2335 discrete_distribution<_IntType>& __x)
2337 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2338 typedef typename __istream_type::ios_base __ios_base;
2340 const typename __ios_base::fmtflags __flags = __is.flags();
2341 __is.flags(__ios_base::dec | __ios_base::skipws);
2346 std::vector<double> __prob_vec;
2347 __prob_vec.reserve(__n);
2348 for (; __n != 0; --__n)
2352 __prob_vec.push_back(__prob);
2355 __x.param(typename discrete_distribution<_IntType>::
2356 param_type(__prob_vec.begin(), __prob_vec.end()));
2358 __is.flags(__flags);
2363 template<typename _RealType>
2365 piecewise_constant_distribution<_RealType>::param_type::
2368 if (_M_int.size() < 2
2369 || (_M_int.size() == 2
2370 && _M_int[0] == _RealType(0)
2371 && _M_int[1] == _RealType(1)))
2378 const double __sum = std::accumulate(_M_den.begin(),
2381 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2382 std::bind2nd(std::divides<double>(), __sum));
2384 _M_cp.reserve(_M_den.size());
2385 std::partial_sum(_M_den.begin(), _M_den.end(),
2386 std::back_inserter(_M_cp));
2388 // Make sure the last cumulative probability is one.
2389 _M_cp[_M_cp.size() - 1] = 1.0;
2391 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2392 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2395 template<typename _RealType>
2396 template<typename _InputIteratorB, typename _InputIteratorW>
2397 piecewise_constant_distribution<_RealType>::param_type::
2398 param_type(_InputIteratorB __bbegin,
2399 _InputIteratorB __bend,
2400 _InputIteratorW __wbegin)
2401 : _M_int(), _M_den(), _M_cp()
2403 if (__bbegin != __bend)
2407 _M_int.push_back(*__bbegin);
2409 if (__bbegin == __bend)
2412 _M_den.push_back(*__wbegin);
2420 template<typename _RealType>
2421 template<typename _Func>
2422 piecewise_constant_distribution<_RealType>::param_type::
2423 param_type(initializer_list<_RealType> __bl, _Func __fw)
2424 : _M_int(), _M_den(), _M_cp()
2426 _M_int.reserve(__bl.size());
2427 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2428 _M_int.push_back(*__biter);
2430 _M_den.reserve(_M_int.size() - 1);
2431 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2432 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2437 template<typename _RealType>
2438 template<typename _Func>
2439 piecewise_constant_distribution<_RealType>::param_type::
2440 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2441 : _M_int(), _M_den(), _M_cp()
2443 const size_t __n = __nw == 0 ? 1 : __nw;
2444 const _RealType __delta = (__xmax - __xmin) / __n;
2446 _M_int.reserve(__n + 1);
2447 for (size_t __k = 0; __k <= __nw; ++__k)
2448 _M_int.push_back(__xmin + __k * __delta);
2450 _M_den.reserve(__n);
2451 for (size_t __k = 0; __k < __nw; ++__k)
2452 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2457 template<typename _RealType>
2458 template<typename _UniformRandomNumberGenerator>
2459 typename piecewise_constant_distribution<_RealType>::result_type
2460 piecewise_constant_distribution<_RealType>::
2461 operator()(_UniformRandomNumberGenerator& __urng,
2462 const param_type& __param)
2464 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2467 const double __p = __aurng();
2468 if (__param._M_cp.empty())
2471 auto __pos = std::lower_bound(__param._M_cp.begin(),
2472 __param._M_cp.end(), __p);
2473 const size_t __i = __pos - __param._M_cp.begin();
2475 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2477 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2480 template<typename _RealType, typename _CharT, typename _Traits>
2481 std::basic_ostream<_CharT, _Traits>&
2482 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2483 const piecewise_constant_distribution<_RealType>& __x)
2485 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2486 typedef typename __ostream_type::ios_base __ios_base;
2488 const typename __ios_base::fmtflags __flags = __os.flags();
2489 const _CharT __fill = __os.fill();
2490 const std::streamsize __precision = __os.precision();
2491 const _CharT __space = __os.widen(' ');
2492 __os.flags(__ios_base::scientific | __ios_base::left);
2494 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2496 std::vector<_RealType> __int = __x.intervals();
2497 __os << __int.size() - 1;
2499 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2500 __os << __space << *__xit;
2502 std::vector<double> __den = __x.densities();
2503 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2504 __os << __space << *__dit;
2506 __os.flags(__flags);
2508 __os.precision(__precision);
2512 template<typename _RealType, typename _CharT, typename _Traits>
2513 std::basic_istream<_CharT, _Traits>&
2514 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2515 piecewise_constant_distribution<_RealType>& __x)
2517 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2518 typedef typename __istream_type::ios_base __ios_base;
2520 const typename __ios_base::fmtflags __flags = __is.flags();
2521 __is.flags(__ios_base::dec | __ios_base::skipws);
2526 std::vector<_RealType> __int_vec;
2527 __int_vec.reserve(__n + 1);
2528 for (size_t __i = 0; __i <= __n; ++__i)
2532 __int_vec.push_back(__int);
2535 std::vector<double> __den_vec;
2536 __den_vec.reserve(__n);
2537 for (size_t __i = 0; __i < __n; ++__i)
2541 __den_vec.push_back(__den);
2544 __x.param(typename piecewise_constant_distribution<_RealType>::
2545 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2547 __is.flags(__flags);
2552 template<typename _RealType>
2554 piecewise_linear_distribution<_RealType>::param_type::
2557 if (_M_int.size() < 2
2558 || (_M_int.size() == 2
2559 && _M_int[0] == _RealType(0)
2560 && _M_int[1] == _RealType(1)
2561 && _M_den[0] == _M_den[1]))
2569 _M_cp.reserve(_M_int.size() - 1);
2570 _M_m.reserve(_M_int.size() - 1);
2571 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2573 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2574 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2575 _M_cp.push_back(__sum);
2576 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2579 // Now normalize the densities...
2580 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2581 std::bind2nd(std::divides<double>(), __sum));
2582 // ... and partial sums...
2583 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2584 std::bind2nd(std::divides<double>(), __sum));
2586 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2587 std::bind2nd(std::divides<double>(), __sum));
2588 // Make sure the last cumulative probablility is one.
2589 _M_cp[_M_cp.size() - 1] = 1.0;
2592 template<typename _RealType>
2593 template<typename _InputIteratorB, typename _InputIteratorW>
2594 piecewise_linear_distribution<_RealType>::param_type::
2595 param_type(_InputIteratorB __bbegin,
2596 _InputIteratorB __bend,
2597 _InputIteratorW __wbegin)
2598 : _M_int(), _M_den(), _M_cp(), _M_m()
2600 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2602 _M_int.push_back(*__bbegin);
2603 _M_den.push_back(*__wbegin);
2609 template<typename _RealType>
2610 template<typename _Func>
2611 piecewise_linear_distribution<_RealType>::param_type::
2612 param_type(initializer_list<_RealType> __bl, _Func __fw)
2613 : _M_int(), _M_den(), _M_cp(), _M_m()
2615 _M_int.reserve(__bl.size());
2616 _M_den.reserve(__bl.size());
2617 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2619 _M_int.push_back(*__biter);
2620 _M_den.push_back(__fw(*__biter));
2626 template<typename _RealType>
2627 template<typename _Func>
2628 piecewise_linear_distribution<_RealType>::param_type::
2629 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2630 : _M_int(), _M_den(), _M_cp(), _M_m()
2632 const size_t __n = __nw == 0 ? 1 : __nw;
2633 const _RealType __delta = (__xmax - __xmin) / __n;
2635 _M_int.reserve(__n + 1);
2636 _M_den.reserve(__n + 1);
2637 for (size_t __k = 0; __k <= __nw; ++__k)
2639 _M_int.push_back(__xmin + __k * __delta);
2640 _M_den.push_back(__fw(_M_int[__k] + __delta));
2646 template<typename _RealType>
2647 template<typename _UniformRandomNumberGenerator>
2648 typename piecewise_linear_distribution<_RealType>::result_type
2649 piecewise_linear_distribution<_RealType>::
2650 operator()(_UniformRandomNumberGenerator& __urng,
2651 const param_type& __param)
2653 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2656 const double __p = __aurng();
2657 if (__param._M_cp.empty())
2660 auto __pos = std::lower_bound(__param._M_cp.begin(),
2661 __param._M_cp.end(), __p);
2662 const size_t __i = __pos - __param._M_cp.begin();
2664 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2666 const double __a = 0.5 * __param._M_m[__i];
2667 const double __b = __param._M_den[__i];
2668 const double __cm = __p - __pref;
2670 _RealType __x = __param._M_int[__i];
2675 const double __d = __b * __b + 4.0 * __a * __cm;
2676 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2682 template<typename _RealType, typename _CharT, typename _Traits>
2683 std::basic_ostream<_CharT, _Traits>&
2684 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2685 const piecewise_linear_distribution<_RealType>& __x)
2687 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2688 typedef typename __ostream_type::ios_base __ios_base;
2690 const typename __ios_base::fmtflags __flags = __os.flags();
2691 const _CharT __fill = __os.fill();
2692 const std::streamsize __precision = __os.precision();
2693 const _CharT __space = __os.widen(' ');
2694 __os.flags(__ios_base::scientific | __ios_base::left);
2696 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2698 std::vector<_RealType> __int = __x.intervals();
2699 __os << __int.size() - 1;
2701 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2702 __os << __space << *__xit;
2704 std::vector<double> __den = __x.densities();
2705 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2706 __os << __space << *__dit;
2708 __os.flags(__flags);
2710 __os.precision(__precision);
2714 template<typename _RealType, typename _CharT, typename _Traits>
2715 std::basic_istream<_CharT, _Traits>&
2716 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2717 piecewise_linear_distribution<_RealType>& __x)
2719 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2720 typedef typename __istream_type::ios_base __ios_base;
2722 const typename __ios_base::fmtflags __flags = __is.flags();
2723 __is.flags(__ios_base::dec | __ios_base::skipws);
2728 std::vector<_RealType> __int_vec;
2729 __int_vec.reserve(__n + 1);
2730 for (size_t __i = 0; __i <= __n; ++__i)
2734 __int_vec.push_back(__int);
2737 std::vector<double> __den_vec;
2738 __den_vec.reserve(__n + 1);
2739 for (size_t __i = 0; __i <= __n; ++__i)
2743 __den_vec.push_back(__den);
2746 __x.param(typename piecewise_linear_distribution<_RealType>::
2747 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2749 __is.flags(__flags);
2754 template<typename _IntType>
2755 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2757 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2758 _M_v.push_back(__detail::__mod<result_type,
2759 __detail::_Shift<result_type, 32>::__value>(*__iter));
2762 template<typename _InputIterator>
2763 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2765 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2766 _M_v.push_back(__detail::__mod<result_type,
2767 __detail::_Shift<result_type, 32>::__value>(*__iter));
2770 template<typename _RandomAccessIterator>
2772 seed_seq::generate(_RandomAccessIterator __begin,
2773 _RandomAccessIterator __end)
2775 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2778 if (__begin == __end)
2781 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2783 const size_t __n = __end - __begin;
2784 const size_t __s = _M_v.size();
2785 const size_t __t = (__n >= 623) ? 11
2790 const size_t __p = (__n - __t) / 2;
2791 const size_t __q = __p + __t;
2792 const size_t __m = std::max(__s + 1, __n);
2794 for (size_t __k = 0; __k < __m; ++__k)
2796 _Type __arg = (__begin[__k % __n]
2797 ^ __begin[(__k + __p) % __n]
2798 ^ __begin[(__k - 1) % __n]);
2799 _Type __r1 = __arg ^ (__arg >> 27);
2800 __r1 = __detail::__mod<_Type,
2801 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
2805 else if (__k <= __s)
2806 __r2 += __k % __n + _M_v[__k - 1];
2809 __r2 = __detail::__mod<_Type,
2810 __detail::_Shift<_Type, 32>::__value>(__r2);
2811 __begin[(__k + __p) % __n] += __r1;
2812 __begin[(__k + __q) % __n] += __r2;
2813 __begin[__k % __n] = __r2;
2816 for (size_t __k = __m; __k < __m + __n; ++__k)
2818 _Type __arg = (__begin[__k % __n]
2819 + __begin[(__k + __p) % __n]
2820 + __begin[(__k - 1) % __n]);
2821 _Type __r3 = __arg ^ (__arg >> 27);
2822 __r3 = __detail::__mod<_Type,
2823 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
2824 _Type __r4 = __r3 - __k % __n;
2825 __r4 = __detail::__mod<_Type,
2826 __detail::_Shift<_Type, 32>::__value>(__r4);
2827 __begin[(__k + __p) % __n] ^= __r3;
2828 __begin[(__k + __q) % __n] ^= __r4;
2829 __begin[__k % __n] = __r4;
2833 template<typename _RealType, size_t __bits,
2834 typename _UniformRandomNumberGenerator>
2836 generate_canonical(_UniformRandomNumberGenerator& __urng)
2839 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2841 const long double __r = static_cast<long double>(__urng.max())
2842 - static_cast<long double>(__urng.min()) + 1.0L;
2843 const size_t __log2r = std::log(__r) / std::log(2.0L);
2844 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2845 _RealType __sum = _RealType(0);
2846 _RealType __tmp = _RealType(1);
2847 for (; __k != 0; --__k)
2849 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2852 return __sum / __tmp;
2855 _GLIBCXX_END_NAMESPACE_VERSION