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 const long double __r = static_cast<long double>(_M_b.max())
734 - static_cast<long double>(_M_b.min()) + 1.0L;
735 const result_type __m = std::log(__r) / std::log(2.0L);
736 result_type __n, __n0, __y0, __y1, __s0, __s1;
737 for (size_t __i = 0; __i < 2; ++__i)
739 __n = (__w + __m - 1) / __m + __i;
740 __n0 = __n - __w % __n;
741 const result_type __w0 = __w / __n;
742 const result_type __w1 = __w0 + 1;
743 __s0 = result_type(1) << __w0;
744 __s1 = result_type(1) << __w1;
745 __y0 = __s0 * (__r / __s0);
746 __y1 = __s1 * (__r / __s1);
747 if (__r - __y0 <= __y0 / __n)
751 result_type __sum = 0;
752 for (size_t __k = 0; __k < __n0; ++__k)
756 __u = _M_b() - _M_b.min();
758 __sum = __s0 * __sum + __u % __s0;
760 for (size_t __k = __n0; __k < __n; ++__k)
764 __u = _M_b() - _M_b.min();
766 __sum = __s1 * __sum + __u % __s1;
772 template<typename _RandomNumberEngine, size_t __k>
774 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
776 template<typename _RandomNumberEngine, size_t __k>
777 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
778 shuffle_order_engine<_RandomNumberEngine, __k>::
781 size_t __j = __k * ((_M_y - _M_b.min())
782 / (_M_b.max() - _M_b.min() + 1.0L));
789 template<typename _RandomNumberEngine, size_t __k,
790 typename _CharT, typename _Traits>
791 std::basic_ostream<_CharT, _Traits>&
792 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
793 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
795 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
796 typedef typename __ostream_type::ios_base __ios_base;
798 const typename __ios_base::fmtflags __flags = __os.flags();
799 const _CharT __fill = __os.fill();
800 const _CharT __space = __os.widen(' ');
801 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
805 for (size_t __i = 0; __i < __k; ++__i)
806 __os << __space << __x._M_v[__i];
807 __os << __space << __x._M_y;
814 template<typename _RandomNumberEngine, size_t __k,
815 typename _CharT, typename _Traits>
816 std::basic_istream<_CharT, _Traits>&
817 operator>>(std::basic_istream<_CharT, _Traits>& __is,
818 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
820 typedef std::basic_istream<_CharT, _Traits> __istream_type;
821 typedef typename __istream_type::ios_base __ios_base;
823 const typename __ios_base::fmtflags __flags = __is.flags();
824 __is.flags(__ios_base::dec | __ios_base::skipws);
827 for (size_t __i = 0; __i < __k; ++__i)
828 __is >> __x._M_v[__i];
836 template<typename _IntType>
837 template<typename _UniformRandomNumberGenerator>
838 typename uniform_int_distribution<_IntType>::result_type
839 uniform_int_distribution<_IntType>::
840 operator()(_UniformRandomNumberGenerator& __urng,
841 const param_type& __param)
843 typedef typename std::make_unsigned<typename
844 _UniformRandomNumberGenerator::result_type>::type __urngtype;
845 typedef typename std::make_unsigned<result_type>::type __utype;
846 typedef typename std::conditional<(sizeof(__urngtype)
848 __urngtype, __utype>::type __uctype;
850 const __uctype __urngmin = __urng.min();
851 const __uctype __urngmax = __urng.max();
852 const __uctype __urngrange = __urngmax - __urngmin;
853 const __uctype __urange
854 = __uctype(__param.b()) - __uctype(__param.a());
858 if (__urngrange > __urange)
861 const __uctype __uerange = __urange + 1; // __urange can be zero
862 const __uctype __scaling = __urngrange / __uerange;
863 const __uctype __past = __uerange * __scaling;
865 __ret = __uctype(__urng()) - __urngmin;
866 while (__ret >= __past);
869 else if (__urngrange < __urange)
873 Note that every value in [0, urange]
874 can be written uniquely as
876 (urngrange + 1) * high + low
880 high in [0, urange / (urngrange + 1)]
884 low in [0, urngrange].
886 __uctype __tmp; // wraparound control
889 const __uctype __uerngrange = __urngrange + 1;
890 __tmp = (__uerngrange * operator()
891 (__urng, param_type(0, __urange / __uerngrange)));
892 __ret = __tmp + (__uctype(__urng()) - __urngmin);
894 while (__ret > __urange || __ret < __tmp);
897 __ret = __uctype(__urng()) - __urngmin;
899 return __ret + __param.a();
902 template<typename _IntType, typename _CharT, typename _Traits>
903 std::basic_ostream<_CharT, _Traits>&
904 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
905 const uniform_int_distribution<_IntType>& __x)
907 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
908 typedef typename __ostream_type::ios_base __ios_base;
910 const typename __ios_base::fmtflags __flags = __os.flags();
911 const _CharT __fill = __os.fill();
912 const _CharT __space = __os.widen(' ');
913 __os.flags(__ios_base::scientific | __ios_base::left);
916 __os << __x.a() << __space << __x.b();
923 template<typename _IntType, typename _CharT, typename _Traits>
924 std::basic_istream<_CharT, _Traits>&
925 operator>>(std::basic_istream<_CharT, _Traits>& __is,
926 uniform_int_distribution<_IntType>& __x)
928 typedef std::basic_istream<_CharT, _Traits> __istream_type;
929 typedef typename __istream_type::ios_base __ios_base;
931 const typename __ios_base::fmtflags __flags = __is.flags();
932 __is.flags(__ios_base::dec | __ios_base::skipws);
936 __x.param(typename uniform_int_distribution<_IntType>::
937 param_type(__a, __b));
944 template<typename _RealType, typename _CharT, typename _Traits>
945 std::basic_ostream<_CharT, _Traits>&
946 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
947 const uniform_real_distribution<_RealType>& __x)
949 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
950 typedef typename __ostream_type::ios_base __ios_base;
952 const typename __ios_base::fmtflags __flags = __os.flags();
953 const _CharT __fill = __os.fill();
954 const std::streamsize __precision = __os.precision();
955 const _CharT __space = __os.widen(' ');
956 __os.flags(__ios_base::scientific | __ios_base::left);
958 __os.precision(std::numeric_limits<_RealType>::max_digits10);
960 __os << __x.a() << __space << __x.b();
964 __os.precision(__precision);
968 template<typename _RealType, typename _CharT, typename _Traits>
969 std::basic_istream<_CharT, _Traits>&
970 operator>>(std::basic_istream<_CharT, _Traits>& __is,
971 uniform_real_distribution<_RealType>& __x)
973 typedef std::basic_istream<_CharT, _Traits> __istream_type;
974 typedef typename __istream_type::ios_base __ios_base;
976 const typename __ios_base::fmtflags __flags = __is.flags();
977 __is.flags(__ios_base::skipws);
981 __x.param(typename uniform_real_distribution<_RealType>::
982 param_type(__a, __b));
989 template<typename _CharT, typename _Traits>
990 std::basic_ostream<_CharT, _Traits>&
991 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
992 const bernoulli_distribution& __x)
994 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
995 typedef typename __ostream_type::ios_base __ios_base;
997 const typename __ios_base::fmtflags __flags = __os.flags();
998 const _CharT __fill = __os.fill();
999 const std::streamsize __precision = __os.precision();
1000 __os.flags(__ios_base::scientific | __ios_base::left);
1001 __os.fill(__os.widen(' '));
1002 __os.precision(std::numeric_limits<double>::max_digits10);
1006 __os.flags(__flags);
1008 __os.precision(__precision);
1013 template<typename _IntType>
1014 template<typename _UniformRandomNumberGenerator>
1015 typename geometric_distribution<_IntType>::result_type
1016 geometric_distribution<_IntType>::
1017 operator()(_UniformRandomNumberGenerator& __urng,
1018 const param_type& __param)
1020 // About the epsilon thing see this thread:
1021 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1022 const double __naf =
1023 (1 - std::numeric_limits<double>::epsilon()) / 2;
1024 // The largest _RealType convertible to _IntType.
1025 const double __thr =
1026 std::numeric_limits<_IntType>::max() + __naf;
1027 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1032 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1033 while (__cand >= __thr);
1035 return result_type(__cand + __naf);
1038 template<typename _IntType,
1039 typename _CharT, typename _Traits>
1040 std::basic_ostream<_CharT, _Traits>&
1041 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1042 const geometric_distribution<_IntType>& __x)
1044 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1045 typedef typename __ostream_type::ios_base __ios_base;
1047 const typename __ios_base::fmtflags __flags = __os.flags();
1048 const _CharT __fill = __os.fill();
1049 const std::streamsize __precision = __os.precision();
1050 __os.flags(__ios_base::scientific | __ios_base::left);
1051 __os.fill(__os.widen(' '));
1052 __os.precision(std::numeric_limits<double>::max_digits10);
1056 __os.flags(__flags);
1058 __os.precision(__precision);
1062 template<typename _IntType,
1063 typename _CharT, typename _Traits>
1064 std::basic_istream<_CharT, _Traits>&
1065 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1066 geometric_distribution<_IntType>& __x)
1068 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1069 typedef typename __istream_type::ios_base __ios_base;
1071 const typename __ios_base::fmtflags __flags = __is.flags();
1072 __is.flags(__ios_base::skipws);
1076 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1078 __is.flags(__flags);
1082 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1083 template<typename _IntType>
1084 template<typename _UniformRandomNumberGenerator>
1085 typename negative_binomial_distribution<_IntType>::result_type
1086 negative_binomial_distribution<_IntType>::
1087 operator()(_UniformRandomNumberGenerator& __urng)
1089 const double __y = _M_gd(__urng);
1091 // XXX Is the constructor too slow?
1092 std::poisson_distribution<result_type> __poisson(__y);
1093 return __poisson(__urng);
1096 template<typename _IntType>
1097 template<typename _UniformRandomNumberGenerator>
1098 typename negative_binomial_distribution<_IntType>::result_type
1099 negative_binomial_distribution<_IntType>::
1100 operator()(_UniformRandomNumberGenerator& __urng,
1101 const param_type& __p)
1103 typedef typename std::gamma_distribution<result_type>::param_type
1107 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1109 std::poisson_distribution<result_type> __poisson(__y);
1110 return __poisson(__urng);
1113 template<typename _IntType, typename _CharT, typename _Traits>
1114 std::basic_ostream<_CharT, _Traits>&
1115 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1116 const negative_binomial_distribution<_IntType>& __x)
1118 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1119 typedef typename __ostream_type::ios_base __ios_base;
1121 const typename __ios_base::fmtflags __flags = __os.flags();
1122 const _CharT __fill = __os.fill();
1123 const std::streamsize __precision = __os.precision();
1124 const _CharT __space = __os.widen(' ');
1125 __os.flags(__ios_base::scientific | __ios_base::left);
1126 __os.fill(__os.widen(' '));
1127 __os.precision(std::numeric_limits<double>::max_digits10);
1129 __os << __x.k() << __space << __x.p()
1130 << __space << __x._M_gd;
1132 __os.flags(__flags);
1134 __os.precision(__precision);
1138 template<typename _IntType, typename _CharT, typename _Traits>
1139 std::basic_istream<_CharT, _Traits>&
1140 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1141 negative_binomial_distribution<_IntType>& __x)
1143 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1144 typedef typename __istream_type::ios_base __ios_base;
1146 const typename __ios_base::fmtflags __flags = __is.flags();
1147 __is.flags(__ios_base::skipws);
1151 __is >> __k >> __p >> __x._M_gd;
1152 __x.param(typename negative_binomial_distribution<_IntType>::
1153 param_type(__k, __p));
1155 __is.flags(__flags);
1160 template<typename _IntType>
1162 poisson_distribution<_IntType>::param_type::
1165 #if _GLIBCXX_USE_C99_MATH_TR1
1168 const double __m = std::floor(_M_mean);
1169 _M_lm_thr = std::log(_M_mean);
1170 _M_lfm = std::lgamma(__m + 1);
1171 _M_sm = std::sqrt(__m);
1173 const double __pi_4 = 0.7853981633974483096156608458198757L;
1174 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1176 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1177 const double __cx = 2 * __m + _M_d;
1178 _M_scx = std::sqrt(__cx / 2);
1181 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1182 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1187 _M_lm_thr = std::exp(-_M_mean);
1191 * A rejection algorithm when mean >= 12 and a simple method based
1192 * upon the multiplication of uniform random variates otherwise.
1193 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1197 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1198 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1200 template<typename _IntType>
1201 template<typename _UniformRandomNumberGenerator>
1202 typename poisson_distribution<_IntType>::result_type
1203 poisson_distribution<_IntType>::
1204 operator()(_UniformRandomNumberGenerator& __urng,
1205 const param_type& __param)
1207 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1209 #if _GLIBCXX_USE_C99_MATH_TR1
1210 if (__param.mean() >= 12)
1214 // See comments above...
1215 const double __naf =
1216 (1 - std::numeric_limits<double>::epsilon()) / 2;
1217 const double __thr =
1218 std::numeric_limits<_IntType>::max() + __naf;
1220 const double __m = std::floor(__param.mean());
1222 const double __spi_2 = 1.2533141373155002512078826424055226L;
1223 const double __c1 = __param._M_sm * __spi_2;
1224 const double __c2 = __param._M_c2b + __c1;
1225 const double __c3 = __c2 + 1;
1226 const double __c4 = __c3 + 1;
1228 const double __e178 = 1.0129030479320018583185514777512983L;
1229 const double __c5 = __c4 + __e178;
1230 const double __c = __param._M_cb + __c5;
1231 const double __2cx = 2 * (2 * __m + __param._M_d);
1233 bool __reject = true;
1236 const double __u = __c * __aurng();
1237 const double __e = -std::log(__aurng());
1243 const double __n = _M_nd(__urng);
1244 const double __y = -std::abs(__n) * __param._M_sm - 1;
1245 __x = std::floor(__y);
1246 __w = -__n * __n / 2;
1250 else if (__u <= __c2)
1252 const double __n = _M_nd(__urng);
1253 const double __y = 1 + std::abs(__n) * __param._M_scx;
1254 __x = std::ceil(__y);
1255 __w = __y * (2 - __y) * __param._M_1cx;
1256 if (__x > __param._M_d)
1259 else if (__u <= __c3)
1260 // NB: This case not in the book, nor in the Errata,
1261 // but should be ok...
1263 else if (__u <= __c4)
1265 else if (__u <= __c5)
1269 const double __v = -std::log(__aurng());
1270 const double __y = __param._M_d
1271 + __v * __2cx / __param._M_d;
1272 __x = std::ceil(__y);
1273 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1276 __reject = (__w - __e - __x * __param._M_lm_thr
1277 > __param._M_lfm - std::lgamma(__x + __m + 1));
1279 __reject |= __x + __m >= __thr;
1283 return result_type(__x + __m + __naf);
1289 double __prod = 1.0;
1293 __prod *= __aurng();
1296 while (__prod > __param._M_lm_thr);
1302 template<typename _IntType,
1303 typename _CharT, typename _Traits>
1304 std::basic_ostream<_CharT, _Traits>&
1305 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1306 const poisson_distribution<_IntType>& __x)
1308 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1309 typedef typename __ostream_type::ios_base __ios_base;
1311 const typename __ios_base::fmtflags __flags = __os.flags();
1312 const _CharT __fill = __os.fill();
1313 const std::streamsize __precision = __os.precision();
1314 const _CharT __space = __os.widen(' ');
1315 __os.flags(__ios_base::scientific | __ios_base::left);
1317 __os.precision(std::numeric_limits<double>::max_digits10);
1319 __os << __x.mean() << __space << __x._M_nd;
1321 __os.flags(__flags);
1323 __os.precision(__precision);
1327 template<typename _IntType,
1328 typename _CharT, typename _Traits>
1329 std::basic_istream<_CharT, _Traits>&
1330 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1331 poisson_distribution<_IntType>& __x)
1333 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1334 typedef typename __istream_type::ios_base __ios_base;
1336 const typename __ios_base::fmtflags __flags = __is.flags();
1337 __is.flags(__ios_base::skipws);
1340 __is >> __mean >> __x._M_nd;
1341 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1343 __is.flags(__flags);
1348 template<typename _IntType>
1350 binomial_distribution<_IntType>::param_type::
1353 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1357 #if _GLIBCXX_USE_C99_MATH_TR1
1358 if (_M_t * __p12 >= 8)
1361 const double __np = std::floor(_M_t * __p12);
1362 const double __pa = __np / _M_t;
1363 const double __1p = 1 - __pa;
1365 const double __pi_4 = 0.7853981633974483096156608458198757L;
1366 const double __d1x =
1367 std::sqrt(__np * __1p * std::log(32 * __np
1368 / (81 * __pi_4 * __1p)));
1369 _M_d1 = std::round(std::max(1.0, __d1x));
1370 const double __d2x =
1371 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1372 / (__pi_4 * __pa)));
1373 _M_d2 = std::round(std::max(1.0, __d2x));
1376 const double __spi_2 = 1.2533141373155002512078826424055226L;
1377 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1378 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1379 _M_c = 2 * _M_d1 / __np;
1380 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1381 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1382 const double __s1s = _M_s1 * _M_s1;
1383 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1385 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1386 const double __s2s = _M_s2 * _M_s2;
1387 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1388 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1389 _M_lf = (std::lgamma(__np + 1)
1390 + std::lgamma(_M_t - __np + 1));
1391 _M_lp1p = std::log(__pa / __1p);
1393 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1397 _M_q = -std::log(1 - __p12);
1400 template<typename _IntType>
1401 template<typename _UniformRandomNumberGenerator>
1402 typename binomial_distribution<_IntType>::result_type
1403 binomial_distribution<_IntType>::
1404 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1408 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1413 const double __e = -std::log(__aurng());
1414 __sum += __e / (__t - __x);
1417 while (__sum <= _M_param._M_q);
1423 * A rejection algorithm when t * p >= 8 and a simple waiting time
1424 * method - the second in the referenced book - otherwise.
1425 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1429 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1430 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1432 template<typename _IntType>
1433 template<typename _UniformRandomNumberGenerator>
1434 typename binomial_distribution<_IntType>::result_type
1435 binomial_distribution<_IntType>::
1436 operator()(_UniformRandomNumberGenerator& __urng,
1437 const param_type& __param)
1440 const _IntType __t = __param.t();
1441 const double __p = __param.p();
1442 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1443 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1446 #if _GLIBCXX_USE_C99_MATH_TR1
1447 if (!__param._M_easy)
1451 // See comments above...
1452 const double __naf =
1453 (1 - std::numeric_limits<double>::epsilon()) / 2;
1454 const double __thr =
1455 std::numeric_limits<_IntType>::max() + __naf;
1457 const double __np = std::floor(__t * __p12);
1460 const double __spi_2 = 1.2533141373155002512078826424055226L;
1461 const double __a1 = __param._M_a1;
1462 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1463 const double __a123 = __param._M_a123;
1464 const double __s1s = __param._M_s1 * __param._M_s1;
1465 const double __s2s = __param._M_s2 * __param._M_s2;
1470 const double __u = __param._M_s * __aurng();
1476 const double __n = _M_nd(__urng);
1477 const double __y = __param._M_s1 * std::abs(__n);
1478 __reject = __y >= __param._M_d1;
1481 const double __e = -std::log(__aurng());
1482 __x = std::floor(__y);
1483 __v = -__e - __n * __n / 2 + __param._M_c;
1486 else if (__u <= __a12)
1488 const double __n = _M_nd(__urng);
1489 const double __y = __param._M_s2 * std::abs(__n);
1490 __reject = __y >= __param._M_d2;
1493 const double __e = -std::log(__aurng());
1494 __x = std::floor(-__y);
1495 __v = -__e - __n * __n / 2;
1498 else if (__u <= __a123)
1500 const double __e1 = -std::log(__aurng());
1501 const double __e2 = -std::log(__aurng());
1503 const double __y = __param._M_d1
1504 + 2 * __s1s * __e1 / __param._M_d1;
1505 __x = std::floor(__y);
1506 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1507 -__y / (2 * __s1s)));
1512 const double __e1 = -std::log(__aurng());
1513 const double __e2 = -std::log(__aurng());
1515 const double __y = __param._M_d2
1516 + 2 * __s2s * __e1 / __param._M_d2;
1517 __x = std::floor(-__y);
1518 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1522 __reject = __reject || __x < -__np || __x > __t - __np;
1525 const double __lfx =
1526 std::lgamma(__np + __x + 1)
1527 + std::lgamma(__t - (__np + __x) + 1);
1528 __reject = __v > __param._M_lf - __lfx
1529 + __x * __param._M_lp1p;
1532 __reject |= __x + __np >= __thr;
1536 __x += __np + __naf;
1538 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1539 __ret = _IntType(__x) + __z;
1543 __ret = _M_waiting(__urng, __t);
1546 __ret = __t - __ret;
1550 template<typename _IntType,
1551 typename _CharT, typename _Traits>
1552 std::basic_ostream<_CharT, _Traits>&
1553 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1554 const binomial_distribution<_IntType>& __x)
1556 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1557 typedef typename __ostream_type::ios_base __ios_base;
1559 const typename __ios_base::fmtflags __flags = __os.flags();
1560 const _CharT __fill = __os.fill();
1561 const std::streamsize __precision = __os.precision();
1562 const _CharT __space = __os.widen(' ');
1563 __os.flags(__ios_base::scientific | __ios_base::left);
1565 __os.precision(std::numeric_limits<double>::max_digits10);
1567 __os << __x.t() << __space << __x.p()
1568 << __space << __x._M_nd;
1570 __os.flags(__flags);
1572 __os.precision(__precision);
1576 template<typename _IntType,
1577 typename _CharT, typename _Traits>
1578 std::basic_istream<_CharT, _Traits>&
1579 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1580 binomial_distribution<_IntType>& __x)
1582 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1583 typedef typename __istream_type::ios_base __ios_base;
1585 const typename __ios_base::fmtflags __flags = __is.flags();
1586 __is.flags(__ios_base::dec | __ios_base::skipws);
1590 __is >> __t >> __p >> __x._M_nd;
1591 __x.param(typename binomial_distribution<_IntType>::
1592 param_type(__t, __p));
1594 __is.flags(__flags);
1599 template<typename _RealType, typename _CharT, typename _Traits>
1600 std::basic_ostream<_CharT, _Traits>&
1601 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1602 const exponential_distribution<_RealType>& __x)
1604 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1605 typedef typename __ostream_type::ios_base __ios_base;
1607 const typename __ios_base::fmtflags __flags = __os.flags();
1608 const _CharT __fill = __os.fill();
1609 const std::streamsize __precision = __os.precision();
1610 __os.flags(__ios_base::scientific | __ios_base::left);
1611 __os.fill(__os.widen(' '));
1612 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1614 __os << __x.lambda();
1616 __os.flags(__flags);
1618 __os.precision(__precision);
1622 template<typename _RealType, typename _CharT, typename _Traits>
1623 std::basic_istream<_CharT, _Traits>&
1624 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1625 exponential_distribution<_RealType>& __x)
1627 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1628 typedef typename __istream_type::ios_base __ios_base;
1630 const typename __ios_base::fmtflags __flags = __is.flags();
1631 __is.flags(__ios_base::dec | __ios_base::skipws);
1635 __x.param(typename exponential_distribution<_RealType>::
1636 param_type(__lambda));
1638 __is.flags(__flags);
1644 * Polar method due to Marsaglia.
1646 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1647 * New York, 1986, Ch. V, Sect. 4.4.
1649 template<typename _RealType>
1650 template<typename _UniformRandomNumberGenerator>
1651 typename normal_distribution<_RealType>::result_type
1652 normal_distribution<_RealType>::
1653 operator()(_UniformRandomNumberGenerator& __urng,
1654 const param_type& __param)
1657 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1660 if (_M_saved_available)
1662 _M_saved_available = false;
1667 result_type __x, __y, __r2;
1670 __x = result_type(2.0) * __aurng() - 1.0;
1671 __y = result_type(2.0) * __aurng() - 1.0;
1672 __r2 = __x * __x + __y * __y;
1674 while (__r2 > 1.0 || __r2 == 0.0);
1676 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1677 _M_saved = __x * __mult;
1678 _M_saved_available = true;
1679 __ret = __y * __mult;
1682 __ret = __ret * __param.stddev() + __param.mean();
1686 template<typename _RealType>
1688 operator==(const std::normal_distribution<_RealType>& __d1,
1689 const std::normal_distribution<_RealType>& __d2)
1691 if (__d1._M_param == __d2._M_param
1692 && __d1._M_saved_available == __d2._M_saved_available)
1694 if (__d1._M_saved_available
1695 && __d1._M_saved == __d2._M_saved)
1697 else if(!__d1._M_saved_available)
1706 template<typename _RealType, typename _CharT, typename _Traits>
1707 std::basic_ostream<_CharT, _Traits>&
1708 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1709 const normal_distribution<_RealType>& __x)
1711 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1712 typedef typename __ostream_type::ios_base __ios_base;
1714 const typename __ios_base::fmtflags __flags = __os.flags();
1715 const _CharT __fill = __os.fill();
1716 const std::streamsize __precision = __os.precision();
1717 const _CharT __space = __os.widen(' ');
1718 __os.flags(__ios_base::scientific | __ios_base::left);
1720 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1722 __os << __x.mean() << __space << __x.stddev()
1723 << __space << __x._M_saved_available;
1724 if (__x._M_saved_available)
1725 __os << __space << __x._M_saved;
1727 __os.flags(__flags);
1729 __os.precision(__precision);
1733 template<typename _RealType, typename _CharT, typename _Traits>
1734 std::basic_istream<_CharT, _Traits>&
1735 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1736 normal_distribution<_RealType>& __x)
1738 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1739 typedef typename __istream_type::ios_base __ios_base;
1741 const typename __ios_base::fmtflags __flags = __is.flags();
1742 __is.flags(__ios_base::dec | __ios_base::skipws);
1744 double __mean, __stddev;
1745 __is >> __mean >> __stddev
1746 >> __x._M_saved_available;
1747 if (__x._M_saved_available)
1748 __is >> __x._M_saved;
1749 __x.param(typename normal_distribution<_RealType>::
1750 param_type(__mean, __stddev));
1752 __is.flags(__flags);
1757 template<typename _RealType, typename _CharT, typename _Traits>
1758 std::basic_ostream<_CharT, _Traits>&
1759 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1760 const lognormal_distribution<_RealType>& __x)
1762 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1763 typedef typename __ostream_type::ios_base __ios_base;
1765 const typename __ios_base::fmtflags __flags = __os.flags();
1766 const _CharT __fill = __os.fill();
1767 const std::streamsize __precision = __os.precision();
1768 const _CharT __space = __os.widen(' ');
1769 __os.flags(__ios_base::scientific | __ios_base::left);
1771 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1773 __os << __x.m() << __space << __x.s()
1774 << __space << __x._M_nd;
1776 __os.flags(__flags);
1778 __os.precision(__precision);
1782 template<typename _RealType, typename _CharT, typename _Traits>
1783 std::basic_istream<_CharT, _Traits>&
1784 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1785 lognormal_distribution<_RealType>& __x)
1787 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1788 typedef typename __istream_type::ios_base __ios_base;
1790 const typename __ios_base::fmtflags __flags = __is.flags();
1791 __is.flags(__ios_base::dec | __ios_base::skipws);
1794 __is >> __m >> __s >> __x._M_nd;
1795 __x.param(typename lognormal_distribution<_RealType>::
1796 param_type(__m, __s));
1798 __is.flags(__flags);
1803 template<typename _RealType, typename _CharT, typename _Traits>
1804 std::basic_ostream<_CharT, _Traits>&
1805 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1806 const chi_squared_distribution<_RealType>& __x)
1808 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1809 typedef typename __ostream_type::ios_base __ios_base;
1811 const typename __ios_base::fmtflags __flags = __os.flags();
1812 const _CharT __fill = __os.fill();
1813 const std::streamsize __precision = __os.precision();
1814 const _CharT __space = __os.widen(' ');
1815 __os.flags(__ios_base::scientific | __ios_base::left);
1817 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1819 __os << __x.n() << __space << __x._M_gd;
1821 __os.flags(__flags);
1823 __os.precision(__precision);
1827 template<typename _RealType, typename _CharT, typename _Traits>
1828 std::basic_istream<_CharT, _Traits>&
1829 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1830 chi_squared_distribution<_RealType>& __x)
1832 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1833 typedef typename __istream_type::ios_base __ios_base;
1835 const typename __ios_base::fmtflags __flags = __is.flags();
1836 __is.flags(__ios_base::dec | __ios_base::skipws);
1839 __is >> __n >> __x._M_gd;
1840 __x.param(typename chi_squared_distribution<_RealType>::
1843 __is.flags(__flags);
1848 template<typename _RealType>
1849 template<typename _UniformRandomNumberGenerator>
1850 typename cauchy_distribution<_RealType>::result_type
1851 cauchy_distribution<_RealType>::
1852 operator()(_UniformRandomNumberGenerator& __urng,
1853 const param_type& __p)
1855 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1862 const _RealType __pi = 3.1415926535897932384626433832795029L;
1863 return __p.a() + __p.b() * std::tan(__pi * __u);
1866 template<typename _RealType, typename _CharT, typename _Traits>
1867 std::basic_ostream<_CharT, _Traits>&
1868 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1869 const cauchy_distribution<_RealType>& __x)
1871 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1872 typedef typename __ostream_type::ios_base __ios_base;
1874 const typename __ios_base::fmtflags __flags = __os.flags();
1875 const _CharT __fill = __os.fill();
1876 const std::streamsize __precision = __os.precision();
1877 const _CharT __space = __os.widen(' ');
1878 __os.flags(__ios_base::scientific | __ios_base::left);
1880 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1882 __os << __x.a() << __space << __x.b();
1884 __os.flags(__flags);
1886 __os.precision(__precision);
1890 template<typename _RealType, typename _CharT, typename _Traits>
1891 std::basic_istream<_CharT, _Traits>&
1892 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1893 cauchy_distribution<_RealType>& __x)
1895 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1896 typedef typename __istream_type::ios_base __ios_base;
1898 const typename __ios_base::fmtflags __flags = __is.flags();
1899 __is.flags(__ios_base::dec | __ios_base::skipws);
1903 __x.param(typename cauchy_distribution<_RealType>::
1904 param_type(__a, __b));
1906 __is.flags(__flags);
1911 template<typename _RealType, typename _CharT, typename _Traits>
1912 std::basic_ostream<_CharT, _Traits>&
1913 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1914 const fisher_f_distribution<_RealType>& __x)
1916 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1917 typedef typename __ostream_type::ios_base __ios_base;
1919 const typename __ios_base::fmtflags __flags = __os.flags();
1920 const _CharT __fill = __os.fill();
1921 const std::streamsize __precision = __os.precision();
1922 const _CharT __space = __os.widen(' ');
1923 __os.flags(__ios_base::scientific | __ios_base::left);
1925 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1927 __os << __x.m() << __space << __x.n()
1928 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1930 __os.flags(__flags);
1932 __os.precision(__precision);
1936 template<typename _RealType, typename _CharT, typename _Traits>
1937 std::basic_istream<_CharT, _Traits>&
1938 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1939 fisher_f_distribution<_RealType>& __x)
1941 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1942 typedef typename __istream_type::ios_base __ios_base;
1944 const typename __ios_base::fmtflags __flags = __is.flags();
1945 __is.flags(__ios_base::dec | __ios_base::skipws);
1948 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1949 __x.param(typename fisher_f_distribution<_RealType>::
1950 param_type(__m, __n));
1952 __is.flags(__flags);
1957 template<typename _RealType, typename _CharT, typename _Traits>
1958 std::basic_ostream<_CharT, _Traits>&
1959 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1960 const student_t_distribution<_RealType>& __x)
1962 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1963 typedef typename __ostream_type::ios_base __ios_base;
1965 const typename __ios_base::fmtflags __flags = __os.flags();
1966 const _CharT __fill = __os.fill();
1967 const std::streamsize __precision = __os.precision();
1968 const _CharT __space = __os.widen(' ');
1969 __os.flags(__ios_base::scientific | __ios_base::left);
1971 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1973 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1975 __os.flags(__flags);
1977 __os.precision(__precision);
1981 template<typename _RealType, typename _CharT, typename _Traits>
1982 std::basic_istream<_CharT, _Traits>&
1983 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1984 student_t_distribution<_RealType>& __x)
1986 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1987 typedef typename __istream_type::ios_base __ios_base;
1989 const typename __ios_base::fmtflags __flags = __is.flags();
1990 __is.flags(__ios_base::dec | __ios_base::skipws);
1993 __is >> __n >> __x._M_nd >> __x._M_gd;
1994 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1996 __is.flags(__flags);
2001 template<typename _RealType>
2003 gamma_distribution<_RealType>::param_type::
2006 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2008 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2009 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2013 * Marsaglia, G. and Tsang, W. W.
2014 * "A Simple Method for Generating Gamma Variables"
2015 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2017 template<typename _RealType>
2018 template<typename _UniformRandomNumberGenerator>
2019 typename gamma_distribution<_RealType>::result_type
2020 gamma_distribution<_RealType>::
2021 operator()(_UniformRandomNumberGenerator& __urng,
2022 const param_type& __param)
2024 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2027 result_type __u, __v, __n;
2028 const result_type __a1 = (__param._M_malpha
2029 - _RealType(1.0) / _RealType(3.0));
2035 __n = _M_nd(__urng);
2036 __v = result_type(1.0) + __param._M_a2 * __n;
2040 __v = __v * __v * __v;
2043 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2044 && (std::log(__u) > (0.5 * __n * __n + __a1
2045 * (1.0 - __v + std::log(__v)))));
2047 if (__param.alpha() == __param._M_malpha)
2048 return __a1 * __v * __param.beta();
2055 return (std::pow(__u, result_type(1.0) / __param.alpha())
2056 * __a1 * __v * __param.beta());
2060 template<typename _RealType, typename _CharT, typename _Traits>
2061 std::basic_ostream<_CharT, _Traits>&
2062 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2063 const gamma_distribution<_RealType>& __x)
2065 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2066 typedef typename __ostream_type::ios_base __ios_base;
2068 const typename __ios_base::fmtflags __flags = __os.flags();
2069 const _CharT __fill = __os.fill();
2070 const std::streamsize __precision = __os.precision();
2071 const _CharT __space = __os.widen(' ');
2072 __os.flags(__ios_base::scientific | __ios_base::left);
2074 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2076 __os << __x.alpha() << __space << __x.beta()
2077 << __space << __x._M_nd;
2079 __os.flags(__flags);
2081 __os.precision(__precision);
2085 template<typename _RealType, typename _CharT, typename _Traits>
2086 std::basic_istream<_CharT, _Traits>&
2087 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2088 gamma_distribution<_RealType>& __x)
2090 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2091 typedef typename __istream_type::ios_base __ios_base;
2093 const typename __ios_base::fmtflags __flags = __is.flags();
2094 __is.flags(__ios_base::dec | __ios_base::skipws);
2096 _RealType __alpha_val, __beta_val;
2097 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2098 __x.param(typename gamma_distribution<_RealType>::
2099 param_type(__alpha_val, __beta_val));
2101 __is.flags(__flags);
2106 template<typename _RealType>
2107 template<typename _UniformRandomNumberGenerator>
2108 typename weibull_distribution<_RealType>::result_type
2109 weibull_distribution<_RealType>::
2110 operator()(_UniformRandomNumberGenerator& __urng,
2111 const param_type& __p)
2113 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2115 return __p.b() * std::pow(-std::log(__aurng()),
2116 result_type(1) / __p.a());
2119 template<typename _RealType, typename _CharT, typename _Traits>
2120 std::basic_ostream<_CharT, _Traits>&
2121 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2122 const weibull_distribution<_RealType>& __x)
2124 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2125 typedef typename __ostream_type::ios_base __ios_base;
2127 const typename __ios_base::fmtflags __flags = __os.flags();
2128 const _CharT __fill = __os.fill();
2129 const std::streamsize __precision = __os.precision();
2130 const _CharT __space = __os.widen(' ');
2131 __os.flags(__ios_base::scientific | __ios_base::left);
2133 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2135 __os << __x.a() << __space << __x.b();
2137 __os.flags(__flags);
2139 __os.precision(__precision);
2143 template<typename _RealType, typename _CharT, typename _Traits>
2144 std::basic_istream<_CharT, _Traits>&
2145 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2146 weibull_distribution<_RealType>& __x)
2148 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2149 typedef typename __istream_type::ios_base __ios_base;
2151 const typename __ios_base::fmtflags __flags = __is.flags();
2152 __is.flags(__ios_base::dec | __ios_base::skipws);
2156 __x.param(typename weibull_distribution<_RealType>::
2157 param_type(__a, __b));
2159 __is.flags(__flags);
2164 template<typename _RealType>
2165 template<typename _UniformRandomNumberGenerator>
2166 typename extreme_value_distribution<_RealType>::result_type
2167 extreme_value_distribution<_RealType>::
2168 operator()(_UniformRandomNumberGenerator& __urng,
2169 const param_type& __p)
2171 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2173 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2176 template<typename _RealType, typename _CharT, typename _Traits>
2177 std::basic_ostream<_CharT, _Traits>&
2178 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2179 const extreme_value_distribution<_RealType>& __x)
2181 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2182 typedef typename __ostream_type::ios_base __ios_base;
2184 const typename __ios_base::fmtflags __flags = __os.flags();
2185 const _CharT __fill = __os.fill();
2186 const std::streamsize __precision = __os.precision();
2187 const _CharT __space = __os.widen(' ');
2188 __os.flags(__ios_base::scientific | __ios_base::left);
2190 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2192 __os << __x.a() << __space << __x.b();
2194 __os.flags(__flags);
2196 __os.precision(__precision);
2200 template<typename _RealType, typename _CharT, typename _Traits>
2201 std::basic_istream<_CharT, _Traits>&
2202 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2203 extreme_value_distribution<_RealType>& __x)
2205 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2206 typedef typename __istream_type::ios_base __ios_base;
2208 const typename __ios_base::fmtflags __flags = __is.flags();
2209 __is.flags(__ios_base::dec | __ios_base::skipws);
2213 __x.param(typename extreme_value_distribution<_RealType>::
2214 param_type(__a, __b));
2216 __is.flags(__flags);
2221 template<typename _IntType>
2223 discrete_distribution<_IntType>::param_type::
2226 if (_M_prob.size() < 2)
2232 const double __sum = std::accumulate(_M_prob.begin(),
2233 _M_prob.end(), 0.0);
2234 // Now normalize the probabilites.
2235 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2236 std::bind2nd(std::divides<double>(), __sum));
2237 // Accumulate partial sums.
2238 _M_cp.reserve(_M_prob.size());
2239 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2240 std::back_inserter(_M_cp));
2241 // Make sure the last cumulative probability is one.
2242 _M_cp[_M_cp.size() - 1] = 1.0;
2245 template<typename _IntType>
2246 template<typename _Func>
2247 discrete_distribution<_IntType>::param_type::
2248 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2249 : _M_prob(), _M_cp()
2251 const size_t __n = __nw == 0 ? 1 : __nw;
2252 const double __delta = (__xmax - __xmin) / __n;
2254 _M_prob.reserve(__n);
2255 for (size_t __k = 0; __k < __nw; ++__k)
2256 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2261 template<typename _IntType>
2262 template<typename _UniformRandomNumberGenerator>
2263 typename discrete_distribution<_IntType>::result_type
2264 discrete_distribution<_IntType>::
2265 operator()(_UniformRandomNumberGenerator& __urng,
2266 const param_type& __param)
2268 if (__param._M_cp.empty())
2269 return result_type(0);
2271 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2274 const double __p = __aurng();
2275 auto __pos = std::lower_bound(__param._M_cp.begin(),
2276 __param._M_cp.end(), __p);
2278 return __pos - __param._M_cp.begin();
2281 template<typename _IntType, typename _CharT, typename _Traits>
2282 std::basic_ostream<_CharT, _Traits>&
2283 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2284 const discrete_distribution<_IntType>& __x)
2286 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2287 typedef typename __ostream_type::ios_base __ios_base;
2289 const typename __ios_base::fmtflags __flags = __os.flags();
2290 const _CharT __fill = __os.fill();
2291 const std::streamsize __precision = __os.precision();
2292 const _CharT __space = __os.widen(' ');
2293 __os.flags(__ios_base::scientific | __ios_base::left);
2295 __os.precision(std::numeric_limits<double>::max_digits10);
2297 std::vector<double> __prob = __x.probabilities();
2298 __os << __prob.size();
2299 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2300 __os << __space << *__dit;
2302 __os.flags(__flags);
2304 __os.precision(__precision);
2308 template<typename _IntType, typename _CharT, typename _Traits>
2309 std::basic_istream<_CharT, _Traits>&
2310 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2311 discrete_distribution<_IntType>& __x)
2313 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2314 typedef typename __istream_type::ios_base __ios_base;
2316 const typename __ios_base::fmtflags __flags = __is.flags();
2317 __is.flags(__ios_base::dec | __ios_base::skipws);
2322 std::vector<double> __prob_vec;
2323 __prob_vec.reserve(__n);
2324 for (; __n != 0; --__n)
2328 __prob_vec.push_back(__prob);
2331 __x.param(typename discrete_distribution<_IntType>::
2332 param_type(__prob_vec.begin(), __prob_vec.end()));
2334 __is.flags(__flags);
2339 template<typename _RealType>
2341 piecewise_constant_distribution<_RealType>::param_type::
2344 if (_M_int.size() < 2
2345 || (_M_int.size() == 2
2346 && _M_int[0] == _RealType(0)
2347 && _M_int[1] == _RealType(1)))
2354 const double __sum = std::accumulate(_M_den.begin(),
2357 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2358 std::bind2nd(std::divides<double>(), __sum));
2360 _M_cp.reserve(_M_den.size());
2361 std::partial_sum(_M_den.begin(), _M_den.end(),
2362 std::back_inserter(_M_cp));
2364 // Make sure the last cumulative probability is one.
2365 _M_cp[_M_cp.size() - 1] = 1.0;
2367 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2368 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2371 template<typename _RealType>
2372 template<typename _InputIteratorB, typename _InputIteratorW>
2373 piecewise_constant_distribution<_RealType>::param_type::
2374 param_type(_InputIteratorB __bbegin,
2375 _InputIteratorB __bend,
2376 _InputIteratorW __wbegin)
2377 : _M_int(), _M_den(), _M_cp()
2379 if (__bbegin != __bend)
2383 _M_int.push_back(*__bbegin);
2385 if (__bbegin == __bend)
2388 _M_den.push_back(*__wbegin);
2396 template<typename _RealType>
2397 template<typename _Func>
2398 piecewise_constant_distribution<_RealType>::param_type::
2399 param_type(initializer_list<_RealType> __bl, _Func __fw)
2400 : _M_int(), _M_den(), _M_cp()
2402 _M_int.reserve(__bl.size());
2403 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2404 _M_int.push_back(*__biter);
2406 _M_den.reserve(_M_int.size() - 1);
2407 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2408 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2413 template<typename _RealType>
2414 template<typename _Func>
2415 piecewise_constant_distribution<_RealType>::param_type::
2416 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2417 : _M_int(), _M_den(), _M_cp()
2419 const size_t __n = __nw == 0 ? 1 : __nw;
2420 const _RealType __delta = (__xmax - __xmin) / __n;
2422 _M_int.reserve(__n + 1);
2423 for (size_t __k = 0; __k <= __nw; ++__k)
2424 _M_int.push_back(__xmin + __k * __delta);
2426 _M_den.reserve(__n);
2427 for (size_t __k = 0; __k < __nw; ++__k)
2428 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2433 template<typename _RealType>
2434 template<typename _UniformRandomNumberGenerator>
2435 typename piecewise_constant_distribution<_RealType>::result_type
2436 piecewise_constant_distribution<_RealType>::
2437 operator()(_UniformRandomNumberGenerator& __urng,
2438 const param_type& __param)
2440 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2443 const double __p = __aurng();
2444 if (__param._M_cp.empty())
2447 auto __pos = std::lower_bound(__param._M_cp.begin(),
2448 __param._M_cp.end(), __p);
2449 const size_t __i = __pos - __param._M_cp.begin();
2451 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2453 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2456 template<typename _RealType, typename _CharT, typename _Traits>
2457 std::basic_ostream<_CharT, _Traits>&
2458 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2459 const piecewise_constant_distribution<_RealType>& __x)
2461 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2462 typedef typename __ostream_type::ios_base __ios_base;
2464 const typename __ios_base::fmtflags __flags = __os.flags();
2465 const _CharT __fill = __os.fill();
2466 const std::streamsize __precision = __os.precision();
2467 const _CharT __space = __os.widen(' ');
2468 __os.flags(__ios_base::scientific | __ios_base::left);
2470 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2472 std::vector<_RealType> __int = __x.intervals();
2473 __os << __int.size() - 1;
2475 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2476 __os << __space << *__xit;
2478 std::vector<double> __den = __x.densities();
2479 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2480 __os << __space << *__dit;
2482 __os.flags(__flags);
2484 __os.precision(__precision);
2488 template<typename _RealType, typename _CharT, typename _Traits>
2489 std::basic_istream<_CharT, _Traits>&
2490 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2491 piecewise_constant_distribution<_RealType>& __x)
2493 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2494 typedef typename __istream_type::ios_base __ios_base;
2496 const typename __ios_base::fmtflags __flags = __is.flags();
2497 __is.flags(__ios_base::dec | __ios_base::skipws);
2502 std::vector<_RealType> __int_vec;
2503 __int_vec.reserve(__n + 1);
2504 for (size_t __i = 0; __i <= __n; ++__i)
2508 __int_vec.push_back(__int);
2511 std::vector<double> __den_vec;
2512 __den_vec.reserve(__n);
2513 for (size_t __i = 0; __i < __n; ++__i)
2517 __den_vec.push_back(__den);
2520 __x.param(typename piecewise_constant_distribution<_RealType>::
2521 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2523 __is.flags(__flags);
2528 template<typename _RealType>
2530 piecewise_linear_distribution<_RealType>::param_type::
2533 if (_M_int.size() < 2
2534 || (_M_int.size() == 2
2535 && _M_int[0] == _RealType(0)
2536 && _M_int[1] == _RealType(1)
2537 && _M_den[0] == _M_den[1]))
2545 _M_cp.reserve(_M_int.size() - 1);
2546 _M_m.reserve(_M_int.size() - 1);
2547 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2549 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2550 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2551 _M_cp.push_back(__sum);
2552 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2555 // Now normalize the densities...
2556 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2557 std::bind2nd(std::divides<double>(), __sum));
2558 // ... and partial sums...
2559 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2560 std::bind2nd(std::divides<double>(), __sum));
2562 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2563 std::bind2nd(std::divides<double>(), __sum));
2564 // Make sure the last cumulative probablility is one.
2565 _M_cp[_M_cp.size() - 1] = 1.0;
2568 template<typename _RealType>
2569 template<typename _InputIteratorB, typename _InputIteratorW>
2570 piecewise_linear_distribution<_RealType>::param_type::
2571 param_type(_InputIteratorB __bbegin,
2572 _InputIteratorB __bend,
2573 _InputIteratorW __wbegin)
2574 : _M_int(), _M_den(), _M_cp(), _M_m()
2576 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2578 _M_int.push_back(*__bbegin);
2579 _M_den.push_back(*__wbegin);
2585 template<typename _RealType>
2586 template<typename _Func>
2587 piecewise_linear_distribution<_RealType>::param_type::
2588 param_type(initializer_list<_RealType> __bl, _Func __fw)
2589 : _M_int(), _M_den(), _M_cp(), _M_m()
2591 _M_int.reserve(__bl.size());
2592 _M_den.reserve(__bl.size());
2593 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2595 _M_int.push_back(*__biter);
2596 _M_den.push_back(__fw(*__biter));
2602 template<typename _RealType>
2603 template<typename _Func>
2604 piecewise_linear_distribution<_RealType>::param_type::
2605 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2606 : _M_int(), _M_den(), _M_cp(), _M_m()
2608 const size_t __n = __nw == 0 ? 1 : __nw;
2609 const _RealType __delta = (__xmax - __xmin) / __n;
2611 _M_int.reserve(__n + 1);
2612 _M_den.reserve(__n + 1);
2613 for (size_t __k = 0; __k <= __nw; ++__k)
2615 _M_int.push_back(__xmin + __k * __delta);
2616 _M_den.push_back(__fw(_M_int[__k] + __delta));
2622 template<typename _RealType>
2623 template<typename _UniformRandomNumberGenerator>
2624 typename piecewise_linear_distribution<_RealType>::result_type
2625 piecewise_linear_distribution<_RealType>::
2626 operator()(_UniformRandomNumberGenerator& __urng,
2627 const param_type& __param)
2629 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2632 const double __p = __aurng();
2633 if (__param._M_cp.empty())
2636 auto __pos = std::lower_bound(__param._M_cp.begin(),
2637 __param._M_cp.end(), __p);
2638 const size_t __i = __pos - __param._M_cp.begin();
2640 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2642 const double __a = 0.5 * __param._M_m[__i];
2643 const double __b = __param._M_den[__i];
2644 const double __cm = __p - __pref;
2646 _RealType __x = __param._M_int[__i];
2651 const double __d = __b * __b + 4.0 * __a * __cm;
2652 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2658 template<typename _RealType, typename _CharT, typename _Traits>
2659 std::basic_ostream<_CharT, _Traits>&
2660 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2661 const piecewise_linear_distribution<_RealType>& __x)
2663 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2664 typedef typename __ostream_type::ios_base __ios_base;
2666 const typename __ios_base::fmtflags __flags = __os.flags();
2667 const _CharT __fill = __os.fill();
2668 const std::streamsize __precision = __os.precision();
2669 const _CharT __space = __os.widen(' ');
2670 __os.flags(__ios_base::scientific | __ios_base::left);
2672 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2674 std::vector<_RealType> __int = __x.intervals();
2675 __os << __int.size() - 1;
2677 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2678 __os << __space << *__xit;
2680 std::vector<double> __den = __x.densities();
2681 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2682 __os << __space << *__dit;
2684 __os.flags(__flags);
2686 __os.precision(__precision);
2690 template<typename _RealType, typename _CharT, typename _Traits>
2691 std::basic_istream<_CharT, _Traits>&
2692 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2693 piecewise_linear_distribution<_RealType>& __x)
2695 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2696 typedef typename __istream_type::ios_base __ios_base;
2698 const typename __ios_base::fmtflags __flags = __is.flags();
2699 __is.flags(__ios_base::dec | __ios_base::skipws);
2704 std::vector<_RealType> __int_vec;
2705 __int_vec.reserve(__n + 1);
2706 for (size_t __i = 0; __i <= __n; ++__i)
2710 __int_vec.push_back(__int);
2713 std::vector<double> __den_vec;
2714 __den_vec.reserve(__n + 1);
2715 for (size_t __i = 0; __i <= __n; ++__i)
2719 __den_vec.push_back(__den);
2722 __x.param(typename piecewise_linear_distribution<_RealType>::
2723 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2725 __is.flags(__flags);
2730 template<typename _IntType>
2731 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2733 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2734 _M_v.push_back(__detail::__mod<result_type,
2735 __detail::_Shift<result_type, 32>::__value>(*__iter));
2738 template<typename _InputIterator>
2739 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2741 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2742 _M_v.push_back(__detail::__mod<result_type,
2743 __detail::_Shift<result_type, 32>::__value>(*__iter));
2746 template<typename _RandomAccessIterator>
2748 seed_seq::generate(_RandomAccessIterator __begin,
2749 _RandomAccessIterator __end)
2751 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2754 if (__begin == __end)
2757 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2759 const size_t __n = __end - __begin;
2760 const size_t __s = _M_v.size();
2761 const size_t __t = (__n >= 623) ? 11
2766 const size_t __p = (__n - __t) / 2;
2767 const size_t __q = __p + __t;
2768 const size_t __m = std::max(__s + 1, __n);
2770 for (size_t __k = 0; __k < __m; ++__k)
2772 _Type __arg = (__begin[__k % __n]
2773 ^ __begin[(__k + __p) % __n]
2774 ^ __begin[(__k - 1) % __n]);
2775 _Type __r1 = __arg ^ (__arg >> 27);
2776 __r1 = __detail::__mod<_Type,
2777 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
2781 else if (__k <= __s)
2782 __r2 += __k % __n + _M_v[__k - 1];
2785 __r2 = __detail::__mod<_Type,
2786 __detail::_Shift<_Type, 32>::__value>(__r2);
2787 __begin[(__k + __p) % __n] += __r1;
2788 __begin[(__k + __q) % __n] += __r2;
2789 __begin[__k % __n] = __r2;
2792 for (size_t __k = __m; __k < __m + __n; ++__k)
2794 _Type __arg = (__begin[__k % __n]
2795 + __begin[(__k + __p) % __n]
2796 + __begin[(__k - 1) % __n]);
2797 _Type __r3 = __arg ^ (__arg >> 27);
2798 __r3 = __detail::__mod<_Type,
2799 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
2800 _Type __r4 = __r3 - __k % __n;
2801 __r4 = __detail::__mod<_Type,
2802 __detail::_Shift<_Type, 32>::__value>(__r4);
2803 __begin[(__k + __p) % __n] ^= __r3;
2804 __begin[(__k + __q) % __n] ^= __r4;
2805 __begin[__k % __n] = __r4;
2809 template<typename _RealType, size_t __bits,
2810 typename _UniformRandomNumberGenerator>
2812 generate_canonical(_UniformRandomNumberGenerator& __urng)
2815 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2817 const long double __r = static_cast<long double>(__urng.max())
2818 - static_cast<long double>(__urng.min()) + 1.0L;
2819 const size_t __log2r = std::log(__r) / std::log(2.0L);
2820 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2821 _RealType __sum = _RealType(0);
2822 _RealType __tmp = _RealType(1);
2823 for (; __k != 0; --__k)
2825 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2828 return __sum / __tmp;
2831 _GLIBCXX_END_NAMESPACE_VERSION