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
45 // to avoid integer overflow.
47 // Preconditions: a > 0, m > 0.
49 // Note: only works correctly for __m % __a < __m / __a.
50 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
52 _Mod<_Tp, __m, __a, __c, false, true>::
59 static const _Tp __q = __m / __a;
60 static const _Tp __r = __m % __a;
62 _Tp __t1 = __a * (__x % __q);
63 _Tp __t2 = __r * (__x / __q);
67 __x = __m - __t2 + __t1;
72 const _Tp __d = __m - __x;
81 template<typename _InputIterator, typename _OutputIterator,
82 typename _UnaryOperation>
84 __transform(_InputIterator __first, _InputIterator __last,
85 _OutputIterator __result, _UnaryOperation __unary_op)
87 for (; __first != __last; ++__first, ++__result)
88 *__result = __unary_op(*__first);
92 _GLIBCXX_END_NAMESPACE_VERSION
93 } // namespace __detail
95 _GLIBCXX_BEGIN_NAMESPACE_VERSION
97 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
99 linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
101 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
103 linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
105 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
107 linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
109 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
111 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
114 * Seeds the LCR with integral value @p __s, adjusted so that the
115 * ring identity is never a member of the convergence set.
117 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
119 linear_congruential_engine<_UIntType, __a, __c, __m>::
120 seed(result_type __s)
122 if ((__detail::__mod<_UIntType, __m>(__c) == 0)
123 && (__detail::__mod<_UIntType, __m>(__s) == 0))
126 _M_x = __detail::__mod<_UIntType, __m>(__s);
130 * Seeds the LCR engine with a value generated by @p __q.
132 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
133 template<typename _Sseq>
134 typename std::enable_if<std::is_class<_Sseq>::value>::type
135 linear_congruential_engine<_UIntType, __a, __c, __m>::
138 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
140 const _UIntType __k = (__k0 + 31) / 32;
141 uint_least32_t __arr[__k + 3];
142 __q.generate(__arr + 0, __arr + __k + 3);
143 _UIntType __factor = 1u;
144 _UIntType __sum = 0u;
145 for (size_t __j = 0; __j < __k; ++__j)
147 __sum += __arr[__j + 3] * __factor;
148 __factor *= __detail::_Shift<_UIntType, 32>::__value;
153 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
154 typename _CharT, typename _Traits>
155 std::basic_ostream<_CharT, _Traits>&
156 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
157 const linear_congruential_engine<_UIntType,
158 __a, __c, __m>& __lcr)
160 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
161 typedef typename __ostream_type::ios_base __ios_base;
163 const typename __ios_base::fmtflags __flags = __os.flags();
164 const _CharT __fill = __os.fill();
165 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
166 __os.fill(__os.widen(' '));
175 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
176 typename _CharT, typename _Traits>
177 std::basic_istream<_CharT, _Traits>&
178 operator>>(std::basic_istream<_CharT, _Traits>& __is,
179 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
181 typedef std::basic_istream<_CharT, _Traits> __istream_type;
182 typedef typename __istream_type::ios_base __ios_base;
184 const typename __ios_base::fmtflags __flags = __is.flags();
185 __is.flags(__ios_base::dec);
194 template<typename _UIntType,
195 size_t __w, size_t __n, size_t __m, size_t __r,
196 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
197 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
200 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
201 __s, __b, __t, __c, __l, __f>::word_size;
203 template<typename _UIntType,
204 size_t __w, size_t __n, size_t __m, size_t __r,
205 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
206 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
209 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
210 __s, __b, __t, __c, __l, __f>::state_size;
212 template<typename _UIntType,
213 size_t __w, size_t __n, size_t __m, size_t __r,
214 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
215 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
218 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
219 __s, __b, __t, __c, __l, __f>::shift_size;
221 template<typename _UIntType,
222 size_t __w, size_t __n, size_t __m, size_t __r,
223 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
224 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
227 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
228 __s, __b, __t, __c, __l, __f>::mask_bits;
230 template<typename _UIntType,
231 size_t __w, size_t __n, size_t __m, size_t __r,
232 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
233 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
236 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
237 __s, __b, __t, __c, __l, __f>::xor_mask;
239 template<typename _UIntType,
240 size_t __w, size_t __n, size_t __m, size_t __r,
241 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
242 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
245 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
246 __s, __b, __t, __c, __l, __f>::tempering_u;
248 template<typename _UIntType,
249 size_t __w, size_t __n, size_t __m, size_t __r,
250 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
251 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
254 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
255 __s, __b, __t, __c, __l, __f>::tempering_d;
257 template<typename _UIntType,
258 size_t __w, size_t __n, size_t __m, size_t __r,
259 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
260 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
263 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
264 __s, __b, __t, __c, __l, __f>::tempering_s;
266 template<typename _UIntType,
267 size_t __w, size_t __n, size_t __m, size_t __r,
268 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
269 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
272 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
273 __s, __b, __t, __c, __l, __f>::tempering_b;
275 template<typename _UIntType,
276 size_t __w, size_t __n, size_t __m, size_t __r,
277 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
278 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
281 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
282 __s, __b, __t, __c, __l, __f>::tempering_t;
284 template<typename _UIntType,
285 size_t __w, size_t __n, size_t __m, size_t __r,
286 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
287 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
290 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
291 __s, __b, __t, __c, __l, __f>::tempering_c;
293 template<typename _UIntType,
294 size_t __w, size_t __n, size_t __m, size_t __r,
295 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
296 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
299 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
300 __s, __b, __t, __c, __l, __f>::tempering_l;
302 template<typename _UIntType,
303 size_t __w, size_t __n, size_t __m, size_t __r,
304 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
305 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
308 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
309 __s, __b, __t, __c, __l, __f>::
310 initialization_multiplier;
312 template<typename _UIntType,
313 size_t __w, size_t __n, size_t __m, size_t __r,
314 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
315 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
318 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
319 __s, __b, __t, __c, __l, __f>::default_seed;
321 template<typename _UIntType,
322 size_t __w, size_t __n, size_t __m, size_t __r,
323 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
324 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
327 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
328 __s, __b, __t, __c, __l, __f>::
329 seed(result_type __sd)
331 _M_x[0] = __detail::__mod<_UIntType,
332 __detail::_Shift<_UIntType, __w>::__value>(__sd);
334 for (size_t __i = 1; __i < state_size; ++__i)
336 _UIntType __x = _M_x[__i - 1];
337 __x ^= __x >> (__w - 2);
339 __x += __detail::__mod<_UIntType, __n>(__i);
340 _M_x[__i] = __detail::__mod<_UIntType,
341 __detail::_Shift<_UIntType, __w>::__value>(__x);
346 template<typename _UIntType,
347 size_t __w, size_t __n, size_t __m, size_t __r,
348 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
349 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
351 template<typename _Sseq>
352 typename std::enable_if<std::is_class<_Sseq>::value>::type
353 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
354 __s, __b, __t, __c, __l, __f>::
357 const _UIntType __upper_mask = (~_UIntType()) << __r;
358 const size_t __k = (__w + 31) / 32;
359 uint_least32_t __arr[__n * __k];
360 __q.generate(__arr + 0, __arr + __n * __k);
363 for (size_t __i = 0; __i < state_size; ++__i)
365 _UIntType __factor = 1u;
366 _UIntType __sum = 0u;
367 for (size_t __j = 0; __j < __k; ++__j)
369 __sum += __arr[__k * __i + __j] * __factor;
370 __factor *= __detail::_Shift<_UIntType, 32>::__value;
372 _M_x[__i] = __detail::__mod<_UIntType,
373 __detail::_Shift<_UIntType, __w>::__value>(__sum);
379 if ((_M_x[0] & __upper_mask) != 0u)
382 else if (_M_x[__i] != 0u)
387 _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
390 template<typename _UIntType, size_t __w,
391 size_t __n, size_t __m, size_t __r,
392 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
393 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
396 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
397 __s, __b, __t, __c, __l, __f>::result_type
398 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
399 __s, __b, __t, __c, __l, __f>::
402 // Reload the vector - cost is O(n) amortized over n calls.
403 if (_M_p >= state_size)
405 const _UIntType __upper_mask = (~_UIntType()) << __r;
406 const _UIntType __lower_mask = ~__upper_mask;
408 for (size_t __k = 0; __k < (__n - __m); ++__k)
410 _UIntType __y = ((_M_x[__k] & __upper_mask)
411 | (_M_x[__k + 1] & __lower_mask));
412 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
413 ^ ((__y & 0x01) ? __a : 0));
416 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
418 _UIntType __y = ((_M_x[__k] & __upper_mask)
419 | (_M_x[__k + 1] & __lower_mask));
420 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
421 ^ ((__y & 0x01) ? __a : 0));
424 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
425 | (_M_x[0] & __lower_mask));
426 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
427 ^ ((__y & 0x01) ? __a : 0));
431 // Calculate o(x(i)).
432 result_type __z = _M_x[_M_p++];
433 __z ^= (__z >> __u) & __d;
434 __z ^= (__z << __s) & __b;
435 __z ^= (__z << __t) & __c;
441 template<typename _UIntType, size_t __w,
442 size_t __n, size_t __m, size_t __r,
443 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
444 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
445 _UIntType __f, typename _CharT, typename _Traits>
446 std::basic_ostream<_CharT, _Traits>&
447 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
448 const mersenne_twister_engine<_UIntType, __w, __n, __m,
449 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
451 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
452 typedef typename __ostream_type::ios_base __ios_base;
454 const typename __ios_base::fmtflags __flags = __os.flags();
455 const _CharT __fill = __os.fill();
456 const _CharT __space = __os.widen(' ');
457 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
460 for (size_t __i = 0; __i < __n; ++__i)
461 __os << __x._M_x[__i] << __space;
469 template<typename _UIntType, size_t __w,
470 size_t __n, size_t __m, size_t __r,
471 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
472 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
473 _UIntType __f, typename _CharT, typename _Traits>
474 std::basic_istream<_CharT, _Traits>&
475 operator>>(std::basic_istream<_CharT, _Traits>& __is,
476 mersenne_twister_engine<_UIntType, __w, __n, __m,
477 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
479 typedef std::basic_istream<_CharT, _Traits> __istream_type;
480 typedef typename __istream_type::ios_base __ios_base;
482 const typename __ios_base::fmtflags __flags = __is.flags();
483 __is.flags(__ios_base::dec | __ios_base::skipws);
485 for (size_t __i = 0; __i < __n; ++__i)
486 __is >> __x._M_x[__i];
494 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
496 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
498 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
500 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
502 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
504 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
506 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
508 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
510 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
512 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
513 seed(result_type __value)
515 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
516 __lcg(__value == 0u ? default_seed : __value);
518 const size_t __n = (__w + 31) / 32;
520 for (size_t __i = 0; __i < long_lag; ++__i)
522 _UIntType __sum = 0u;
523 _UIntType __factor = 1u;
524 for (size_t __j = 0; __j < __n; ++__j)
526 __sum += __detail::__mod<uint_least32_t,
527 __detail::_Shift<uint_least32_t, 32>::__value>
528 (__lcg()) * __factor;
529 __factor *= __detail::_Shift<_UIntType, 32>::__value;
531 _M_x[__i] = __detail::__mod<_UIntType,
532 __detail::_Shift<_UIntType, __w>::__value>(__sum);
534 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
538 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
539 template<typename _Sseq>
540 typename std::enable_if<std::is_class<_Sseq>::value>::type
541 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
544 const size_t __k = (__w + 31) / 32;
545 uint_least32_t __arr[__r * __k];
546 __q.generate(__arr + 0, __arr + __r * __k);
548 for (size_t __i = 0; __i < long_lag; ++__i)
550 _UIntType __sum = 0u;
551 _UIntType __factor = 1u;
552 for (size_t __j = 0; __j < __k; ++__j)
554 __sum += __arr[__k * __i + __j] * __factor;
555 __factor *= __detail::_Shift<_UIntType, 32>::__value;
557 _M_x[__i] = __detail::__mod<_UIntType,
558 __detail::_Shift<_UIntType, __w>::__value>(__sum);
560 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
564 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
565 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
567 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
570 // Derive short lag index from current index.
571 long __ps = _M_p - short_lag;
575 // Calculate new x(i) without overflow or division.
576 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
579 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
581 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
586 __xi = (__detail::_Shift<_UIntType, __w>::__value
587 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
592 // Adjust current index to loop around in ring buffer.
593 if (++_M_p >= long_lag)
599 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
600 typename _CharT, typename _Traits>
601 std::basic_ostream<_CharT, _Traits>&
602 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
603 const subtract_with_carry_engine<_UIntType,
606 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
607 typedef typename __ostream_type::ios_base __ios_base;
609 const typename __ios_base::fmtflags __flags = __os.flags();
610 const _CharT __fill = __os.fill();
611 const _CharT __space = __os.widen(' ');
612 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
615 for (size_t __i = 0; __i < __r; ++__i)
616 __os << __x._M_x[__i] << __space;
617 __os << __x._M_carry << __space << __x._M_p;
624 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
625 typename _CharT, typename _Traits>
626 std::basic_istream<_CharT, _Traits>&
627 operator>>(std::basic_istream<_CharT, _Traits>& __is,
628 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
630 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
631 typedef typename __istream_type::ios_base __ios_base;
633 const typename __ios_base::fmtflags __flags = __is.flags();
634 __is.flags(__ios_base::dec | __ios_base::skipws);
636 for (size_t __i = 0; __i < __r; ++__i)
637 __is >> __x._M_x[__i];
638 __is >> __x._M_carry;
646 template<typename _RandomNumberEngine, size_t __p, size_t __r>
648 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
650 template<typename _RandomNumberEngine, size_t __p, size_t __r>
652 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
654 template<typename _RandomNumberEngine, size_t __p, size_t __r>
655 typename discard_block_engine<_RandomNumberEngine,
656 __p, __r>::result_type
657 discard_block_engine<_RandomNumberEngine, __p, __r>::
660 if (_M_n >= used_block)
662 _M_b.discard(block_size - _M_n);
669 template<typename _RandomNumberEngine, size_t __p, size_t __r,
670 typename _CharT, typename _Traits>
671 std::basic_ostream<_CharT, _Traits>&
672 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
673 const discard_block_engine<_RandomNumberEngine,
676 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
677 typedef typename __ostream_type::ios_base __ios_base;
679 const typename __ios_base::fmtflags __flags = __os.flags();
680 const _CharT __fill = __os.fill();
681 const _CharT __space = __os.widen(' ');
682 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
685 __os << __x.base() << __space << __x._M_n;
692 template<typename _RandomNumberEngine, size_t __p, size_t __r,
693 typename _CharT, typename _Traits>
694 std::basic_istream<_CharT, _Traits>&
695 operator>>(std::basic_istream<_CharT, _Traits>& __is,
696 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
698 typedef std::basic_istream<_CharT, _Traits> __istream_type;
699 typedef typename __istream_type::ios_base __ios_base;
701 const typename __ios_base::fmtflags __flags = __is.flags();
702 __is.flags(__ios_base::dec | __ios_base::skipws);
704 __is >> __x._M_b >> __x._M_n;
711 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
712 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
714 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
717 typedef typename _RandomNumberEngine::result_type _Eresult_type;
718 const _Eresult_type __r
719 = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max()
720 ? _M_b.max() - _M_b.min() + 1 : 0);
721 const unsigned __edig = std::numeric_limits<_Eresult_type>::digits;
722 const unsigned __m = __r ? std::__lg(__r) : __edig;
724 typedef typename std::common_type<_Eresult_type, result_type>::type
726 const unsigned __cdig = std::numeric_limits<__ctype>::digits;
729 __ctype __s0, __s1, __y0, __y1;
731 for (size_t __i = 0; __i < 2; ++__i)
733 __n = (__w + __m - 1) / __m + __i;
734 __n0 = __n - __w % __n;
735 const unsigned __w0 = __w / __n; // __w0 <= __m
741 __s0 = __ctype(1) << __w0;
749 __y0 = __s0 * (__r / __s0);
751 __y1 = __s1 * (__r / __s1);
753 if (__r - __y0 <= __y0 / __n)
760 result_type __sum = 0;
761 for (size_t __k = 0; __k < __n0; ++__k)
765 __u = _M_b() - _M_b.min();
766 while (__y0 && __u >= __y0);
767 __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
769 for (size_t __k = __n0; __k < __n; ++__k)
773 __u = _M_b() - _M_b.min();
774 while (__y1 && __u >= __y1);
775 __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
781 template<typename _RandomNumberEngine, size_t __k>
783 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
785 template<typename _RandomNumberEngine, size_t __k>
786 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
787 shuffle_order_engine<_RandomNumberEngine, __k>::
790 size_t __j = __k * ((_M_y - _M_b.min())
791 / (_M_b.max() - _M_b.min() + 1.0L));
798 template<typename _RandomNumberEngine, size_t __k,
799 typename _CharT, typename _Traits>
800 std::basic_ostream<_CharT, _Traits>&
801 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
802 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
804 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
805 typedef typename __ostream_type::ios_base __ios_base;
807 const typename __ios_base::fmtflags __flags = __os.flags();
808 const _CharT __fill = __os.fill();
809 const _CharT __space = __os.widen(' ');
810 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
814 for (size_t __i = 0; __i < __k; ++__i)
815 __os << __space << __x._M_v[__i];
816 __os << __space << __x._M_y;
823 template<typename _RandomNumberEngine, size_t __k,
824 typename _CharT, typename _Traits>
825 std::basic_istream<_CharT, _Traits>&
826 operator>>(std::basic_istream<_CharT, _Traits>& __is,
827 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
829 typedef std::basic_istream<_CharT, _Traits> __istream_type;
830 typedef typename __istream_type::ios_base __ios_base;
832 const typename __ios_base::fmtflags __flags = __is.flags();
833 __is.flags(__ios_base::dec | __ios_base::skipws);
836 for (size_t __i = 0; __i < __k; ++__i)
837 __is >> __x._M_v[__i];
845 template<typename _IntType>
846 template<typename _UniformRandomNumberGenerator>
847 typename uniform_int_distribution<_IntType>::result_type
848 uniform_int_distribution<_IntType>::
849 operator()(_UniformRandomNumberGenerator& __urng,
850 const param_type& __param)
852 typedef typename _UniformRandomNumberGenerator::result_type
854 typedef typename std::make_unsigned<result_type>::type __utype;
855 typedef typename std::common_type<_Gresult_type, __utype>::type
858 const __uctype __urngmin = __urng.min();
859 const __uctype __urngmax = __urng.max();
860 const __uctype __urngrange = __urngmax - __urngmin;
861 const __uctype __urange
862 = __uctype(__param.b()) - __uctype(__param.a());
866 if (__urngrange > __urange)
869 const __uctype __uerange = __urange + 1; // __urange can be zero
870 const __uctype __scaling = __urngrange / __uerange;
871 const __uctype __past = __uerange * __scaling;
873 __ret = __uctype(__urng()) - __urngmin;
874 while (__ret >= __past);
877 else if (__urngrange < __urange)
881 Note that every value in [0, urange]
882 can be written uniquely as
884 (urngrange + 1) * high + low
888 high in [0, urange / (urngrange + 1)]
892 low in [0, urngrange].
894 __uctype __tmp; // wraparound control
897 const __uctype __uerngrange = __urngrange + 1;
898 __tmp = (__uerngrange * operator()
899 (__urng, param_type(0, __urange / __uerngrange)));
900 __ret = __tmp + (__uctype(__urng()) - __urngmin);
902 while (__ret > __urange || __ret < __tmp);
905 __ret = __uctype(__urng()) - __urngmin;
907 return __ret + __param.a();
910 template<typename _IntType, typename _CharT, typename _Traits>
911 std::basic_ostream<_CharT, _Traits>&
912 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
913 const uniform_int_distribution<_IntType>& __x)
915 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
916 typedef typename __ostream_type::ios_base __ios_base;
918 const typename __ios_base::fmtflags __flags = __os.flags();
919 const _CharT __fill = __os.fill();
920 const _CharT __space = __os.widen(' ');
921 __os.flags(__ios_base::scientific | __ios_base::left);
924 __os << __x.a() << __space << __x.b();
931 template<typename _IntType, typename _CharT, typename _Traits>
932 std::basic_istream<_CharT, _Traits>&
933 operator>>(std::basic_istream<_CharT, _Traits>& __is,
934 uniform_int_distribution<_IntType>& __x)
936 typedef std::basic_istream<_CharT, _Traits> __istream_type;
937 typedef typename __istream_type::ios_base __ios_base;
939 const typename __ios_base::fmtflags __flags = __is.flags();
940 __is.flags(__ios_base::dec | __ios_base::skipws);
944 __x.param(typename uniform_int_distribution<_IntType>::
945 param_type(__a, __b));
952 template<typename _RealType, typename _CharT, typename _Traits>
953 std::basic_ostream<_CharT, _Traits>&
954 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
955 const uniform_real_distribution<_RealType>& __x)
957 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
958 typedef typename __ostream_type::ios_base __ios_base;
960 const typename __ios_base::fmtflags __flags = __os.flags();
961 const _CharT __fill = __os.fill();
962 const std::streamsize __precision = __os.precision();
963 const _CharT __space = __os.widen(' ');
964 __os.flags(__ios_base::scientific | __ios_base::left);
966 __os.precision(std::numeric_limits<_RealType>::max_digits10);
968 __os << __x.a() << __space << __x.b();
972 __os.precision(__precision);
976 template<typename _RealType, typename _CharT, typename _Traits>
977 std::basic_istream<_CharT, _Traits>&
978 operator>>(std::basic_istream<_CharT, _Traits>& __is,
979 uniform_real_distribution<_RealType>& __x)
981 typedef std::basic_istream<_CharT, _Traits> __istream_type;
982 typedef typename __istream_type::ios_base __ios_base;
984 const typename __ios_base::fmtflags __flags = __is.flags();
985 __is.flags(__ios_base::skipws);
989 __x.param(typename uniform_real_distribution<_RealType>::
990 param_type(__a, __b));
997 template<typename _CharT, typename _Traits>
998 std::basic_ostream<_CharT, _Traits>&
999 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1000 const bernoulli_distribution& __x)
1002 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1003 typedef typename __ostream_type::ios_base __ios_base;
1005 const typename __ios_base::fmtflags __flags = __os.flags();
1006 const _CharT __fill = __os.fill();
1007 const std::streamsize __precision = __os.precision();
1008 __os.flags(__ios_base::scientific | __ios_base::left);
1009 __os.fill(__os.widen(' '));
1010 __os.precision(std::numeric_limits<double>::max_digits10);
1014 __os.flags(__flags);
1016 __os.precision(__precision);
1021 template<typename _IntType>
1022 template<typename _UniformRandomNumberGenerator>
1023 typename geometric_distribution<_IntType>::result_type
1024 geometric_distribution<_IntType>::
1025 operator()(_UniformRandomNumberGenerator& __urng,
1026 const param_type& __param)
1028 // About the epsilon thing see this thread:
1029 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1030 const double __naf =
1031 (1 - std::numeric_limits<double>::epsilon()) / 2;
1032 // The largest _RealType convertible to _IntType.
1033 const double __thr =
1034 std::numeric_limits<_IntType>::max() + __naf;
1035 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1040 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1041 while (__cand >= __thr);
1043 return result_type(__cand + __naf);
1046 template<typename _IntType,
1047 typename _CharT, typename _Traits>
1048 std::basic_ostream<_CharT, _Traits>&
1049 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1050 const geometric_distribution<_IntType>& __x)
1052 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1053 typedef typename __ostream_type::ios_base __ios_base;
1055 const typename __ios_base::fmtflags __flags = __os.flags();
1056 const _CharT __fill = __os.fill();
1057 const std::streamsize __precision = __os.precision();
1058 __os.flags(__ios_base::scientific | __ios_base::left);
1059 __os.fill(__os.widen(' '));
1060 __os.precision(std::numeric_limits<double>::max_digits10);
1064 __os.flags(__flags);
1066 __os.precision(__precision);
1070 template<typename _IntType,
1071 typename _CharT, typename _Traits>
1072 std::basic_istream<_CharT, _Traits>&
1073 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1074 geometric_distribution<_IntType>& __x)
1076 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1077 typedef typename __istream_type::ios_base __ios_base;
1079 const typename __ios_base::fmtflags __flags = __is.flags();
1080 __is.flags(__ios_base::skipws);
1084 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1086 __is.flags(__flags);
1090 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1091 template<typename _IntType>
1092 template<typename _UniformRandomNumberGenerator>
1093 typename negative_binomial_distribution<_IntType>::result_type
1094 negative_binomial_distribution<_IntType>::
1095 operator()(_UniformRandomNumberGenerator& __urng)
1097 const double __y = _M_gd(__urng);
1099 // XXX Is the constructor too slow?
1100 std::poisson_distribution<result_type> __poisson(__y);
1101 return __poisson(__urng);
1104 template<typename _IntType>
1105 template<typename _UniformRandomNumberGenerator>
1106 typename negative_binomial_distribution<_IntType>::result_type
1107 negative_binomial_distribution<_IntType>::
1108 operator()(_UniformRandomNumberGenerator& __urng,
1109 const param_type& __p)
1111 typedef typename std::gamma_distribution<result_type>::param_type
1115 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1117 std::poisson_distribution<result_type> __poisson(__y);
1118 return __poisson(__urng);
1121 template<typename _IntType, typename _CharT, typename _Traits>
1122 std::basic_ostream<_CharT, _Traits>&
1123 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1124 const negative_binomial_distribution<_IntType>& __x)
1126 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1127 typedef typename __ostream_type::ios_base __ios_base;
1129 const typename __ios_base::fmtflags __flags = __os.flags();
1130 const _CharT __fill = __os.fill();
1131 const std::streamsize __precision = __os.precision();
1132 const _CharT __space = __os.widen(' ');
1133 __os.flags(__ios_base::scientific | __ios_base::left);
1134 __os.fill(__os.widen(' '));
1135 __os.precision(std::numeric_limits<double>::max_digits10);
1137 __os << __x.k() << __space << __x.p()
1138 << __space << __x._M_gd;
1140 __os.flags(__flags);
1142 __os.precision(__precision);
1146 template<typename _IntType, typename _CharT, typename _Traits>
1147 std::basic_istream<_CharT, _Traits>&
1148 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1149 negative_binomial_distribution<_IntType>& __x)
1151 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1152 typedef typename __istream_type::ios_base __ios_base;
1154 const typename __ios_base::fmtflags __flags = __is.flags();
1155 __is.flags(__ios_base::skipws);
1159 __is >> __k >> __p >> __x._M_gd;
1160 __x.param(typename negative_binomial_distribution<_IntType>::
1161 param_type(__k, __p));
1163 __is.flags(__flags);
1168 template<typename _IntType>
1170 poisson_distribution<_IntType>::param_type::
1173 #if _GLIBCXX_USE_C99_MATH_TR1
1176 const double __m = std::floor(_M_mean);
1177 _M_lm_thr = std::log(_M_mean);
1178 _M_lfm = std::lgamma(__m + 1);
1179 _M_sm = std::sqrt(__m);
1181 const double __pi_4 = 0.7853981633974483096156608458198757L;
1182 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1184 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1185 const double __cx = 2 * __m + _M_d;
1186 _M_scx = std::sqrt(__cx / 2);
1189 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1190 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1195 _M_lm_thr = std::exp(-_M_mean);
1199 * A rejection algorithm when mean >= 12 and a simple method based
1200 * upon the multiplication of uniform random variates otherwise.
1201 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1205 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1206 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1208 template<typename _IntType>
1209 template<typename _UniformRandomNumberGenerator>
1210 typename poisson_distribution<_IntType>::result_type
1211 poisson_distribution<_IntType>::
1212 operator()(_UniformRandomNumberGenerator& __urng,
1213 const param_type& __param)
1215 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1217 #if _GLIBCXX_USE_C99_MATH_TR1
1218 if (__param.mean() >= 12)
1222 // See comments above...
1223 const double __naf =
1224 (1 - std::numeric_limits<double>::epsilon()) / 2;
1225 const double __thr =
1226 std::numeric_limits<_IntType>::max() + __naf;
1228 const double __m = std::floor(__param.mean());
1230 const double __spi_2 = 1.2533141373155002512078826424055226L;
1231 const double __c1 = __param._M_sm * __spi_2;
1232 const double __c2 = __param._M_c2b + __c1;
1233 const double __c3 = __c2 + 1;
1234 const double __c4 = __c3 + 1;
1236 const double __e178 = 1.0129030479320018583185514777512983L;
1237 const double __c5 = __c4 + __e178;
1238 const double __c = __param._M_cb + __c5;
1239 const double __2cx = 2 * (2 * __m + __param._M_d);
1241 bool __reject = true;
1244 const double __u = __c * __aurng();
1245 const double __e = -std::log(__aurng());
1251 const double __n = _M_nd(__urng);
1252 const double __y = -std::abs(__n) * __param._M_sm - 1;
1253 __x = std::floor(__y);
1254 __w = -__n * __n / 2;
1258 else if (__u <= __c2)
1260 const double __n = _M_nd(__urng);
1261 const double __y = 1 + std::abs(__n) * __param._M_scx;
1262 __x = std::ceil(__y);
1263 __w = __y * (2 - __y) * __param._M_1cx;
1264 if (__x > __param._M_d)
1267 else if (__u <= __c3)
1268 // NB: This case not in the book, nor in the Errata,
1269 // but should be ok...
1271 else if (__u <= __c4)
1273 else if (__u <= __c5)
1277 const double __v = -std::log(__aurng());
1278 const double __y = __param._M_d
1279 + __v * __2cx / __param._M_d;
1280 __x = std::ceil(__y);
1281 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1284 __reject = (__w - __e - __x * __param._M_lm_thr
1285 > __param._M_lfm - std::lgamma(__x + __m + 1));
1287 __reject |= __x + __m >= __thr;
1291 return result_type(__x + __m + __naf);
1297 double __prod = 1.0;
1301 __prod *= __aurng();
1304 while (__prod > __param._M_lm_thr);
1310 template<typename _IntType,
1311 typename _CharT, typename _Traits>
1312 std::basic_ostream<_CharT, _Traits>&
1313 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1314 const poisson_distribution<_IntType>& __x)
1316 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1317 typedef typename __ostream_type::ios_base __ios_base;
1319 const typename __ios_base::fmtflags __flags = __os.flags();
1320 const _CharT __fill = __os.fill();
1321 const std::streamsize __precision = __os.precision();
1322 const _CharT __space = __os.widen(' ');
1323 __os.flags(__ios_base::scientific | __ios_base::left);
1325 __os.precision(std::numeric_limits<double>::max_digits10);
1327 __os << __x.mean() << __space << __x._M_nd;
1329 __os.flags(__flags);
1331 __os.precision(__precision);
1335 template<typename _IntType,
1336 typename _CharT, typename _Traits>
1337 std::basic_istream<_CharT, _Traits>&
1338 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1339 poisson_distribution<_IntType>& __x)
1341 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1342 typedef typename __istream_type::ios_base __ios_base;
1344 const typename __ios_base::fmtflags __flags = __is.flags();
1345 __is.flags(__ios_base::skipws);
1348 __is >> __mean >> __x._M_nd;
1349 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1351 __is.flags(__flags);
1356 template<typename _IntType>
1358 binomial_distribution<_IntType>::param_type::
1361 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1365 #if _GLIBCXX_USE_C99_MATH_TR1
1366 if (_M_t * __p12 >= 8)
1369 const double __np = std::floor(_M_t * __p12);
1370 const double __pa = __np / _M_t;
1371 const double __1p = 1 - __pa;
1373 const double __pi_4 = 0.7853981633974483096156608458198757L;
1374 const double __d1x =
1375 std::sqrt(__np * __1p * std::log(32 * __np
1376 / (81 * __pi_4 * __1p)));
1377 _M_d1 = std::round(std::max(1.0, __d1x));
1378 const double __d2x =
1379 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1380 / (__pi_4 * __pa)));
1381 _M_d2 = std::round(std::max(1.0, __d2x));
1384 const double __spi_2 = 1.2533141373155002512078826424055226L;
1385 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1386 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1387 _M_c = 2 * _M_d1 / __np;
1388 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1389 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1390 const double __s1s = _M_s1 * _M_s1;
1391 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1393 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1394 const double __s2s = _M_s2 * _M_s2;
1395 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1396 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1397 _M_lf = (std::lgamma(__np + 1)
1398 + std::lgamma(_M_t - __np + 1));
1399 _M_lp1p = std::log(__pa / __1p);
1401 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1405 _M_q = -std::log(1 - __p12);
1408 template<typename _IntType>
1409 template<typename _UniformRandomNumberGenerator>
1410 typename binomial_distribution<_IntType>::result_type
1411 binomial_distribution<_IntType>::
1412 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1416 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1421 const double __e = -std::log(__aurng());
1422 __sum += __e / (__t - __x);
1425 while (__sum <= _M_param._M_q);
1431 * A rejection algorithm when t * p >= 8 and a simple waiting time
1432 * method - the second in the referenced book - otherwise.
1433 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1437 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1438 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1440 template<typename _IntType>
1441 template<typename _UniformRandomNumberGenerator>
1442 typename binomial_distribution<_IntType>::result_type
1443 binomial_distribution<_IntType>::
1444 operator()(_UniformRandomNumberGenerator& __urng,
1445 const param_type& __param)
1448 const _IntType __t = __param.t();
1449 const double __p = __param.p();
1450 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1451 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1454 #if _GLIBCXX_USE_C99_MATH_TR1
1455 if (!__param._M_easy)
1459 // See comments above...
1460 const double __naf =
1461 (1 - std::numeric_limits<double>::epsilon()) / 2;
1462 const double __thr =
1463 std::numeric_limits<_IntType>::max() + __naf;
1465 const double __np = std::floor(__t * __p12);
1468 const double __spi_2 = 1.2533141373155002512078826424055226L;
1469 const double __a1 = __param._M_a1;
1470 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1471 const double __a123 = __param._M_a123;
1472 const double __s1s = __param._M_s1 * __param._M_s1;
1473 const double __s2s = __param._M_s2 * __param._M_s2;
1478 const double __u = __param._M_s * __aurng();
1484 const double __n = _M_nd(__urng);
1485 const double __y = __param._M_s1 * std::abs(__n);
1486 __reject = __y >= __param._M_d1;
1489 const double __e = -std::log(__aurng());
1490 __x = std::floor(__y);
1491 __v = -__e - __n * __n / 2 + __param._M_c;
1494 else if (__u <= __a12)
1496 const double __n = _M_nd(__urng);
1497 const double __y = __param._M_s2 * std::abs(__n);
1498 __reject = __y >= __param._M_d2;
1501 const double __e = -std::log(__aurng());
1502 __x = std::floor(-__y);
1503 __v = -__e - __n * __n / 2;
1506 else if (__u <= __a123)
1508 const double __e1 = -std::log(__aurng());
1509 const double __e2 = -std::log(__aurng());
1511 const double __y = __param._M_d1
1512 + 2 * __s1s * __e1 / __param._M_d1;
1513 __x = std::floor(__y);
1514 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1515 -__y / (2 * __s1s)));
1520 const double __e1 = -std::log(__aurng());
1521 const double __e2 = -std::log(__aurng());
1523 const double __y = __param._M_d2
1524 + 2 * __s2s * __e1 / __param._M_d2;
1525 __x = std::floor(-__y);
1526 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1530 __reject = __reject || __x < -__np || __x > __t - __np;
1533 const double __lfx =
1534 std::lgamma(__np + __x + 1)
1535 + std::lgamma(__t - (__np + __x) + 1);
1536 __reject = __v > __param._M_lf - __lfx
1537 + __x * __param._M_lp1p;
1540 __reject |= __x + __np >= __thr;
1544 __x += __np + __naf;
1546 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1547 __ret = _IntType(__x) + __z;
1551 __ret = _M_waiting(__urng, __t);
1554 __ret = __t - __ret;
1558 template<typename _IntType,
1559 typename _CharT, typename _Traits>
1560 std::basic_ostream<_CharT, _Traits>&
1561 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1562 const binomial_distribution<_IntType>& __x)
1564 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1565 typedef typename __ostream_type::ios_base __ios_base;
1567 const typename __ios_base::fmtflags __flags = __os.flags();
1568 const _CharT __fill = __os.fill();
1569 const std::streamsize __precision = __os.precision();
1570 const _CharT __space = __os.widen(' ');
1571 __os.flags(__ios_base::scientific | __ios_base::left);
1573 __os.precision(std::numeric_limits<double>::max_digits10);
1575 __os << __x.t() << __space << __x.p()
1576 << __space << __x._M_nd;
1578 __os.flags(__flags);
1580 __os.precision(__precision);
1584 template<typename _IntType,
1585 typename _CharT, typename _Traits>
1586 std::basic_istream<_CharT, _Traits>&
1587 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1588 binomial_distribution<_IntType>& __x)
1590 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1591 typedef typename __istream_type::ios_base __ios_base;
1593 const typename __ios_base::fmtflags __flags = __is.flags();
1594 __is.flags(__ios_base::dec | __ios_base::skipws);
1598 __is >> __t >> __p >> __x._M_nd;
1599 __x.param(typename binomial_distribution<_IntType>::
1600 param_type(__t, __p));
1602 __is.flags(__flags);
1607 template<typename _RealType, typename _CharT, typename _Traits>
1608 std::basic_ostream<_CharT, _Traits>&
1609 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1610 const exponential_distribution<_RealType>& __x)
1612 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1613 typedef typename __ostream_type::ios_base __ios_base;
1615 const typename __ios_base::fmtflags __flags = __os.flags();
1616 const _CharT __fill = __os.fill();
1617 const std::streamsize __precision = __os.precision();
1618 __os.flags(__ios_base::scientific | __ios_base::left);
1619 __os.fill(__os.widen(' '));
1620 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1622 __os << __x.lambda();
1624 __os.flags(__flags);
1626 __os.precision(__precision);
1630 template<typename _RealType, typename _CharT, typename _Traits>
1631 std::basic_istream<_CharT, _Traits>&
1632 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1633 exponential_distribution<_RealType>& __x)
1635 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1636 typedef typename __istream_type::ios_base __ios_base;
1638 const typename __ios_base::fmtflags __flags = __is.flags();
1639 __is.flags(__ios_base::dec | __ios_base::skipws);
1643 __x.param(typename exponential_distribution<_RealType>::
1644 param_type(__lambda));
1646 __is.flags(__flags);
1652 * Polar method due to Marsaglia.
1654 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1655 * New York, 1986, Ch. V, Sect. 4.4.
1657 template<typename _RealType>
1658 template<typename _UniformRandomNumberGenerator>
1659 typename normal_distribution<_RealType>::result_type
1660 normal_distribution<_RealType>::
1661 operator()(_UniformRandomNumberGenerator& __urng,
1662 const param_type& __param)
1665 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1668 if (_M_saved_available)
1670 _M_saved_available = false;
1675 result_type __x, __y, __r2;
1678 __x = result_type(2.0) * __aurng() - 1.0;
1679 __y = result_type(2.0) * __aurng() - 1.0;
1680 __r2 = __x * __x + __y * __y;
1682 while (__r2 > 1.0 || __r2 == 0.0);
1684 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1685 _M_saved = __x * __mult;
1686 _M_saved_available = true;
1687 __ret = __y * __mult;
1690 __ret = __ret * __param.stddev() + __param.mean();
1694 template<typename _RealType>
1696 operator==(const std::normal_distribution<_RealType>& __d1,
1697 const std::normal_distribution<_RealType>& __d2)
1699 if (__d1._M_param == __d2._M_param
1700 && __d1._M_saved_available == __d2._M_saved_available)
1702 if (__d1._M_saved_available
1703 && __d1._M_saved == __d2._M_saved)
1705 else if(!__d1._M_saved_available)
1714 template<typename _RealType, typename _CharT, typename _Traits>
1715 std::basic_ostream<_CharT, _Traits>&
1716 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1717 const normal_distribution<_RealType>& __x)
1719 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1720 typedef typename __ostream_type::ios_base __ios_base;
1722 const typename __ios_base::fmtflags __flags = __os.flags();
1723 const _CharT __fill = __os.fill();
1724 const std::streamsize __precision = __os.precision();
1725 const _CharT __space = __os.widen(' ');
1726 __os.flags(__ios_base::scientific | __ios_base::left);
1728 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1730 __os << __x.mean() << __space << __x.stddev()
1731 << __space << __x._M_saved_available;
1732 if (__x._M_saved_available)
1733 __os << __space << __x._M_saved;
1735 __os.flags(__flags);
1737 __os.precision(__precision);
1741 template<typename _RealType, typename _CharT, typename _Traits>
1742 std::basic_istream<_CharT, _Traits>&
1743 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1744 normal_distribution<_RealType>& __x)
1746 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1747 typedef typename __istream_type::ios_base __ios_base;
1749 const typename __ios_base::fmtflags __flags = __is.flags();
1750 __is.flags(__ios_base::dec | __ios_base::skipws);
1752 double __mean, __stddev;
1753 __is >> __mean >> __stddev
1754 >> __x._M_saved_available;
1755 if (__x._M_saved_available)
1756 __is >> __x._M_saved;
1757 __x.param(typename normal_distribution<_RealType>::
1758 param_type(__mean, __stddev));
1760 __is.flags(__flags);
1765 template<typename _RealType, typename _CharT, typename _Traits>
1766 std::basic_ostream<_CharT, _Traits>&
1767 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1768 const lognormal_distribution<_RealType>& __x)
1770 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1771 typedef typename __ostream_type::ios_base __ios_base;
1773 const typename __ios_base::fmtflags __flags = __os.flags();
1774 const _CharT __fill = __os.fill();
1775 const std::streamsize __precision = __os.precision();
1776 const _CharT __space = __os.widen(' ');
1777 __os.flags(__ios_base::scientific | __ios_base::left);
1779 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1781 __os << __x.m() << __space << __x.s()
1782 << __space << __x._M_nd;
1784 __os.flags(__flags);
1786 __os.precision(__precision);
1790 template<typename _RealType, typename _CharT, typename _Traits>
1791 std::basic_istream<_CharT, _Traits>&
1792 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1793 lognormal_distribution<_RealType>& __x)
1795 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1796 typedef typename __istream_type::ios_base __ios_base;
1798 const typename __ios_base::fmtflags __flags = __is.flags();
1799 __is.flags(__ios_base::dec | __ios_base::skipws);
1802 __is >> __m >> __s >> __x._M_nd;
1803 __x.param(typename lognormal_distribution<_RealType>::
1804 param_type(__m, __s));
1806 __is.flags(__flags);
1811 template<typename _RealType, typename _CharT, typename _Traits>
1812 std::basic_ostream<_CharT, _Traits>&
1813 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1814 const chi_squared_distribution<_RealType>& __x)
1816 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1817 typedef typename __ostream_type::ios_base __ios_base;
1819 const typename __ios_base::fmtflags __flags = __os.flags();
1820 const _CharT __fill = __os.fill();
1821 const std::streamsize __precision = __os.precision();
1822 const _CharT __space = __os.widen(' ');
1823 __os.flags(__ios_base::scientific | __ios_base::left);
1825 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1827 __os << __x.n() << __space << __x._M_gd;
1829 __os.flags(__flags);
1831 __os.precision(__precision);
1835 template<typename _RealType, typename _CharT, typename _Traits>
1836 std::basic_istream<_CharT, _Traits>&
1837 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1838 chi_squared_distribution<_RealType>& __x)
1840 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1841 typedef typename __istream_type::ios_base __ios_base;
1843 const typename __ios_base::fmtflags __flags = __is.flags();
1844 __is.flags(__ios_base::dec | __ios_base::skipws);
1847 __is >> __n >> __x._M_gd;
1848 __x.param(typename chi_squared_distribution<_RealType>::
1851 __is.flags(__flags);
1856 template<typename _RealType>
1857 template<typename _UniformRandomNumberGenerator>
1858 typename cauchy_distribution<_RealType>::result_type
1859 cauchy_distribution<_RealType>::
1860 operator()(_UniformRandomNumberGenerator& __urng,
1861 const param_type& __p)
1863 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1870 const _RealType __pi = 3.1415926535897932384626433832795029L;
1871 return __p.a() + __p.b() * std::tan(__pi * __u);
1874 template<typename _RealType, typename _CharT, typename _Traits>
1875 std::basic_ostream<_CharT, _Traits>&
1876 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1877 const cauchy_distribution<_RealType>& __x)
1879 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1880 typedef typename __ostream_type::ios_base __ios_base;
1882 const typename __ios_base::fmtflags __flags = __os.flags();
1883 const _CharT __fill = __os.fill();
1884 const std::streamsize __precision = __os.precision();
1885 const _CharT __space = __os.widen(' ');
1886 __os.flags(__ios_base::scientific | __ios_base::left);
1888 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1890 __os << __x.a() << __space << __x.b();
1892 __os.flags(__flags);
1894 __os.precision(__precision);
1898 template<typename _RealType, typename _CharT, typename _Traits>
1899 std::basic_istream<_CharT, _Traits>&
1900 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1901 cauchy_distribution<_RealType>& __x)
1903 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1904 typedef typename __istream_type::ios_base __ios_base;
1906 const typename __ios_base::fmtflags __flags = __is.flags();
1907 __is.flags(__ios_base::dec | __ios_base::skipws);
1911 __x.param(typename cauchy_distribution<_RealType>::
1912 param_type(__a, __b));
1914 __is.flags(__flags);
1919 template<typename _RealType, typename _CharT, typename _Traits>
1920 std::basic_ostream<_CharT, _Traits>&
1921 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1922 const fisher_f_distribution<_RealType>& __x)
1924 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1925 typedef typename __ostream_type::ios_base __ios_base;
1927 const typename __ios_base::fmtflags __flags = __os.flags();
1928 const _CharT __fill = __os.fill();
1929 const std::streamsize __precision = __os.precision();
1930 const _CharT __space = __os.widen(' ');
1931 __os.flags(__ios_base::scientific | __ios_base::left);
1933 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1935 __os << __x.m() << __space << __x.n()
1936 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1938 __os.flags(__flags);
1940 __os.precision(__precision);
1944 template<typename _RealType, typename _CharT, typename _Traits>
1945 std::basic_istream<_CharT, _Traits>&
1946 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1947 fisher_f_distribution<_RealType>& __x)
1949 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1950 typedef typename __istream_type::ios_base __ios_base;
1952 const typename __ios_base::fmtflags __flags = __is.flags();
1953 __is.flags(__ios_base::dec | __ios_base::skipws);
1956 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1957 __x.param(typename fisher_f_distribution<_RealType>::
1958 param_type(__m, __n));
1960 __is.flags(__flags);
1965 template<typename _RealType, typename _CharT, typename _Traits>
1966 std::basic_ostream<_CharT, _Traits>&
1967 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1968 const student_t_distribution<_RealType>& __x)
1970 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1971 typedef typename __ostream_type::ios_base __ios_base;
1973 const typename __ios_base::fmtflags __flags = __os.flags();
1974 const _CharT __fill = __os.fill();
1975 const std::streamsize __precision = __os.precision();
1976 const _CharT __space = __os.widen(' ');
1977 __os.flags(__ios_base::scientific | __ios_base::left);
1979 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1981 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1983 __os.flags(__flags);
1985 __os.precision(__precision);
1989 template<typename _RealType, typename _CharT, typename _Traits>
1990 std::basic_istream<_CharT, _Traits>&
1991 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1992 student_t_distribution<_RealType>& __x)
1994 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1995 typedef typename __istream_type::ios_base __ios_base;
1997 const typename __ios_base::fmtflags __flags = __is.flags();
1998 __is.flags(__ios_base::dec | __ios_base::skipws);
2001 __is >> __n >> __x._M_nd >> __x._M_gd;
2002 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
2004 __is.flags(__flags);
2009 template<typename _RealType>
2011 gamma_distribution<_RealType>::param_type::
2014 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2016 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2017 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2021 * Marsaglia, G. and Tsang, W. W.
2022 * "A Simple Method for Generating Gamma Variables"
2023 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2025 template<typename _RealType>
2026 template<typename _UniformRandomNumberGenerator>
2027 typename gamma_distribution<_RealType>::result_type
2028 gamma_distribution<_RealType>::
2029 operator()(_UniformRandomNumberGenerator& __urng,
2030 const param_type& __param)
2032 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2035 result_type __u, __v, __n;
2036 const result_type __a1 = (__param._M_malpha
2037 - _RealType(1.0) / _RealType(3.0));
2043 __n = _M_nd(__urng);
2044 __v = result_type(1.0) + __param._M_a2 * __n;
2048 __v = __v * __v * __v;
2051 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2052 && (std::log(__u) > (0.5 * __n * __n + __a1
2053 * (1.0 - __v + std::log(__v)))));
2055 if (__param.alpha() == __param._M_malpha)
2056 return __a1 * __v * __param.beta();
2063 return (std::pow(__u, result_type(1.0) / __param.alpha())
2064 * __a1 * __v * __param.beta());
2068 template<typename _RealType, typename _CharT, typename _Traits>
2069 std::basic_ostream<_CharT, _Traits>&
2070 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2071 const gamma_distribution<_RealType>& __x)
2073 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2074 typedef typename __ostream_type::ios_base __ios_base;
2076 const typename __ios_base::fmtflags __flags = __os.flags();
2077 const _CharT __fill = __os.fill();
2078 const std::streamsize __precision = __os.precision();
2079 const _CharT __space = __os.widen(' ');
2080 __os.flags(__ios_base::scientific | __ios_base::left);
2082 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2084 __os << __x.alpha() << __space << __x.beta()
2085 << __space << __x._M_nd;
2087 __os.flags(__flags);
2089 __os.precision(__precision);
2093 template<typename _RealType, typename _CharT, typename _Traits>
2094 std::basic_istream<_CharT, _Traits>&
2095 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2096 gamma_distribution<_RealType>& __x)
2098 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2099 typedef typename __istream_type::ios_base __ios_base;
2101 const typename __ios_base::fmtflags __flags = __is.flags();
2102 __is.flags(__ios_base::dec | __ios_base::skipws);
2104 _RealType __alpha_val, __beta_val;
2105 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2106 __x.param(typename gamma_distribution<_RealType>::
2107 param_type(__alpha_val, __beta_val));
2109 __is.flags(__flags);
2114 template<typename _RealType>
2115 template<typename _UniformRandomNumberGenerator>
2116 typename weibull_distribution<_RealType>::result_type
2117 weibull_distribution<_RealType>::
2118 operator()(_UniformRandomNumberGenerator& __urng,
2119 const param_type& __p)
2121 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2123 return __p.b() * std::pow(-std::log(__aurng()),
2124 result_type(1) / __p.a());
2127 template<typename _RealType, typename _CharT, typename _Traits>
2128 std::basic_ostream<_CharT, _Traits>&
2129 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2130 const weibull_distribution<_RealType>& __x)
2132 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2133 typedef typename __ostream_type::ios_base __ios_base;
2135 const typename __ios_base::fmtflags __flags = __os.flags();
2136 const _CharT __fill = __os.fill();
2137 const std::streamsize __precision = __os.precision();
2138 const _CharT __space = __os.widen(' ');
2139 __os.flags(__ios_base::scientific | __ios_base::left);
2141 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2143 __os << __x.a() << __space << __x.b();
2145 __os.flags(__flags);
2147 __os.precision(__precision);
2151 template<typename _RealType, typename _CharT, typename _Traits>
2152 std::basic_istream<_CharT, _Traits>&
2153 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2154 weibull_distribution<_RealType>& __x)
2156 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2157 typedef typename __istream_type::ios_base __ios_base;
2159 const typename __ios_base::fmtflags __flags = __is.flags();
2160 __is.flags(__ios_base::dec | __ios_base::skipws);
2164 __x.param(typename weibull_distribution<_RealType>::
2165 param_type(__a, __b));
2167 __is.flags(__flags);
2172 template<typename _RealType>
2173 template<typename _UniformRandomNumberGenerator>
2174 typename extreme_value_distribution<_RealType>::result_type
2175 extreme_value_distribution<_RealType>::
2176 operator()(_UniformRandomNumberGenerator& __urng,
2177 const param_type& __p)
2179 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2181 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2184 template<typename _RealType, typename _CharT, typename _Traits>
2185 std::basic_ostream<_CharT, _Traits>&
2186 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2187 const extreme_value_distribution<_RealType>& __x)
2189 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2190 typedef typename __ostream_type::ios_base __ios_base;
2192 const typename __ios_base::fmtflags __flags = __os.flags();
2193 const _CharT __fill = __os.fill();
2194 const std::streamsize __precision = __os.precision();
2195 const _CharT __space = __os.widen(' ');
2196 __os.flags(__ios_base::scientific | __ios_base::left);
2198 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2200 __os << __x.a() << __space << __x.b();
2202 __os.flags(__flags);
2204 __os.precision(__precision);
2208 template<typename _RealType, typename _CharT, typename _Traits>
2209 std::basic_istream<_CharT, _Traits>&
2210 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2211 extreme_value_distribution<_RealType>& __x)
2213 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2214 typedef typename __istream_type::ios_base __ios_base;
2216 const typename __ios_base::fmtflags __flags = __is.flags();
2217 __is.flags(__ios_base::dec | __ios_base::skipws);
2221 __x.param(typename extreme_value_distribution<_RealType>::
2222 param_type(__a, __b));
2224 __is.flags(__flags);
2229 template<typename _IntType>
2231 discrete_distribution<_IntType>::param_type::
2234 if (_M_prob.size() < 2)
2240 const double __sum = std::accumulate(_M_prob.begin(),
2241 _M_prob.end(), 0.0);
2242 // Now normalize the probabilites.
2243 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2244 std::bind2nd(std::divides<double>(), __sum));
2245 // Accumulate partial sums.
2246 _M_cp.reserve(_M_prob.size());
2247 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2248 std::back_inserter(_M_cp));
2249 // Make sure the last cumulative probability is one.
2250 _M_cp[_M_cp.size() - 1] = 1.0;
2253 template<typename _IntType>
2254 template<typename _Func>
2255 discrete_distribution<_IntType>::param_type::
2256 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2257 : _M_prob(), _M_cp()
2259 const size_t __n = __nw == 0 ? 1 : __nw;
2260 const double __delta = (__xmax - __xmin) / __n;
2262 _M_prob.reserve(__n);
2263 for (size_t __k = 0; __k < __nw; ++__k)
2264 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2269 template<typename _IntType>
2270 template<typename _UniformRandomNumberGenerator>
2271 typename discrete_distribution<_IntType>::result_type
2272 discrete_distribution<_IntType>::
2273 operator()(_UniformRandomNumberGenerator& __urng,
2274 const param_type& __param)
2276 if (__param._M_cp.empty())
2277 return result_type(0);
2279 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2282 const double __p = __aurng();
2283 auto __pos = std::lower_bound(__param._M_cp.begin(),
2284 __param._M_cp.end(), __p);
2286 return __pos - __param._M_cp.begin();
2289 template<typename _IntType, typename _CharT, typename _Traits>
2290 std::basic_ostream<_CharT, _Traits>&
2291 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2292 const discrete_distribution<_IntType>& __x)
2294 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2295 typedef typename __ostream_type::ios_base __ios_base;
2297 const typename __ios_base::fmtflags __flags = __os.flags();
2298 const _CharT __fill = __os.fill();
2299 const std::streamsize __precision = __os.precision();
2300 const _CharT __space = __os.widen(' ');
2301 __os.flags(__ios_base::scientific | __ios_base::left);
2303 __os.precision(std::numeric_limits<double>::max_digits10);
2305 std::vector<double> __prob = __x.probabilities();
2306 __os << __prob.size();
2307 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2308 __os << __space << *__dit;
2310 __os.flags(__flags);
2312 __os.precision(__precision);
2316 template<typename _IntType, typename _CharT, typename _Traits>
2317 std::basic_istream<_CharT, _Traits>&
2318 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2319 discrete_distribution<_IntType>& __x)
2321 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2322 typedef typename __istream_type::ios_base __ios_base;
2324 const typename __ios_base::fmtflags __flags = __is.flags();
2325 __is.flags(__ios_base::dec | __ios_base::skipws);
2330 std::vector<double> __prob_vec;
2331 __prob_vec.reserve(__n);
2332 for (; __n != 0; --__n)
2336 __prob_vec.push_back(__prob);
2339 __x.param(typename discrete_distribution<_IntType>::
2340 param_type(__prob_vec.begin(), __prob_vec.end()));
2342 __is.flags(__flags);
2347 template<typename _RealType>
2349 piecewise_constant_distribution<_RealType>::param_type::
2352 if (_M_int.size() < 2
2353 || (_M_int.size() == 2
2354 && _M_int[0] == _RealType(0)
2355 && _M_int[1] == _RealType(1)))
2362 const double __sum = std::accumulate(_M_den.begin(),
2365 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2366 std::bind2nd(std::divides<double>(), __sum));
2368 _M_cp.reserve(_M_den.size());
2369 std::partial_sum(_M_den.begin(), _M_den.end(),
2370 std::back_inserter(_M_cp));
2372 // Make sure the last cumulative probability is one.
2373 _M_cp[_M_cp.size() - 1] = 1.0;
2375 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2376 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2379 template<typename _RealType>
2380 template<typename _InputIteratorB, typename _InputIteratorW>
2381 piecewise_constant_distribution<_RealType>::param_type::
2382 param_type(_InputIteratorB __bbegin,
2383 _InputIteratorB __bend,
2384 _InputIteratorW __wbegin)
2385 : _M_int(), _M_den(), _M_cp()
2387 if (__bbegin != __bend)
2391 _M_int.push_back(*__bbegin);
2393 if (__bbegin == __bend)
2396 _M_den.push_back(*__wbegin);
2404 template<typename _RealType>
2405 template<typename _Func>
2406 piecewise_constant_distribution<_RealType>::param_type::
2407 param_type(initializer_list<_RealType> __bl, _Func __fw)
2408 : _M_int(), _M_den(), _M_cp()
2410 _M_int.reserve(__bl.size());
2411 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2412 _M_int.push_back(*__biter);
2414 _M_den.reserve(_M_int.size() - 1);
2415 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2416 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2421 template<typename _RealType>
2422 template<typename _Func>
2423 piecewise_constant_distribution<_RealType>::param_type::
2424 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2425 : _M_int(), _M_den(), _M_cp()
2427 const size_t __n = __nw == 0 ? 1 : __nw;
2428 const _RealType __delta = (__xmax - __xmin) / __n;
2430 _M_int.reserve(__n + 1);
2431 for (size_t __k = 0; __k <= __nw; ++__k)
2432 _M_int.push_back(__xmin + __k * __delta);
2434 _M_den.reserve(__n);
2435 for (size_t __k = 0; __k < __nw; ++__k)
2436 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2441 template<typename _RealType>
2442 template<typename _UniformRandomNumberGenerator>
2443 typename piecewise_constant_distribution<_RealType>::result_type
2444 piecewise_constant_distribution<_RealType>::
2445 operator()(_UniformRandomNumberGenerator& __urng,
2446 const param_type& __param)
2448 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2451 const double __p = __aurng();
2452 if (__param._M_cp.empty())
2455 auto __pos = std::lower_bound(__param._M_cp.begin(),
2456 __param._M_cp.end(), __p);
2457 const size_t __i = __pos - __param._M_cp.begin();
2459 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2461 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2464 template<typename _RealType, typename _CharT, typename _Traits>
2465 std::basic_ostream<_CharT, _Traits>&
2466 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2467 const piecewise_constant_distribution<_RealType>& __x)
2469 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2470 typedef typename __ostream_type::ios_base __ios_base;
2472 const typename __ios_base::fmtflags __flags = __os.flags();
2473 const _CharT __fill = __os.fill();
2474 const std::streamsize __precision = __os.precision();
2475 const _CharT __space = __os.widen(' ');
2476 __os.flags(__ios_base::scientific | __ios_base::left);
2478 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2480 std::vector<_RealType> __int = __x.intervals();
2481 __os << __int.size() - 1;
2483 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2484 __os << __space << *__xit;
2486 std::vector<double> __den = __x.densities();
2487 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2488 __os << __space << *__dit;
2490 __os.flags(__flags);
2492 __os.precision(__precision);
2496 template<typename _RealType, typename _CharT, typename _Traits>
2497 std::basic_istream<_CharT, _Traits>&
2498 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2499 piecewise_constant_distribution<_RealType>& __x)
2501 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2502 typedef typename __istream_type::ios_base __ios_base;
2504 const typename __ios_base::fmtflags __flags = __is.flags();
2505 __is.flags(__ios_base::dec | __ios_base::skipws);
2510 std::vector<_RealType> __int_vec;
2511 __int_vec.reserve(__n + 1);
2512 for (size_t __i = 0; __i <= __n; ++__i)
2516 __int_vec.push_back(__int);
2519 std::vector<double> __den_vec;
2520 __den_vec.reserve(__n);
2521 for (size_t __i = 0; __i < __n; ++__i)
2525 __den_vec.push_back(__den);
2528 __x.param(typename piecewise_constant_distribution<_RealType>::
2529 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2531 __is.flags(__flags);
2536 template<typename _RealType>
2538 piecewise_linear_distribution<_RealType>::param_type::
2541 if (_M_int.size() < 2
2542 || (_M_int.size() == 2
2543 && _M_int[0] == _RealType(0)
2544 && _M_int[1] == _RealType(1)
2545 && _M_den[0] == _M_den[1]))
2553 _M_cp.reserve(_M_int.size() - 1);
2554 _M_m.reserve(_M_int.size() - 1);
2555 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2557 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2558 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2559 _M_cp.push_back(__sum);
2560 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2563 // Now normalize the densities...
2564 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2565 std::bind2nd(std::divides<double>(), __sum));
2566 // ... and partial sums...
2567 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2568 std::bind2nd(std::divides<double>(), __sum));
2570 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2571 std::bind2nd(std::divides<double>(), __sum));
2572 // Make sure the last cumulative probablility is one.
2573 _M_cp[_M_cp.size() - 1] = 1.0;
2576 template<typename _RealType>
2577 template<typename _InputIteratorB, typename _InputIteratorW>
2578 piecewise_linear_distribution<_RealType>::param_type::
2579 param_type(_InputIteratorB __bbegin,
2580 _InputIteratorB __bend,
2581 _InputIteratorW __wbegin)
2582 : _M_int(), _M_den(), _M_cp(), _M_m()
2584 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2586 _M_int.push_back(*__bbegin);
2587 _M_den.push_back(*__wbegin);
2593 template<typename _RealType>
2594 template<typename _Func>
2595 piecewise_linear_distribution<_RealType>::param_type::
2596 param_type(initializer_list<_RealType> __bl, _Func __fw)
2597 : _M_int(), _M_den(), _M_cp(), _M_m()
2599 _M_int.reserve(__bl.size());
2600 _M_den.reserve(__bl.size());
2601 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2603 _M_int.push_back(*__biter);
2604 _M_den.push_back(__fw(*__biter));
2610 template<typename _RealType>
2611 template<typename _Func>
2612 piecewise_linear_distribution<_RealType>::param_type::
2613 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2614 : _M_int(), _M_den(), _M_cp(), _M_m()
2616 const size_t __n = __nw == 0 ? 1 : __nw;
2617 const _RealType __delta = (__xmax - __xmin) / __n;
2619 _M_int.reserve(__n + 1);
2620 _M_den.reserve(__n + 1);
2621 for (size_t __k = 0; __k <= __nw; ++__k)
2623 _M_int.push_back(__xmin + __k * __delta);
2624 _M_den.push_back(__fw(_M_int[__k] + __delta));
2630 template<typename _RealType>
2631 template<typename _UniformRandomNumberGenerator>
2632 typename piecewise_linear_distribution<_RealType>::result_type
2633 piecewise_linear_distribution<_RealType>::
2634 operator()(_UniformRandomNumberGenerator& __urng,
2635 const param_type& __param)
2637 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2640 const double __p = __aurng();
2641 if (__param._M_cp.empty())
2644 auto __pos = std::lower_bound(__param._M_cp.begin(),
2645 __param._M_cp.end(), __p);
2646 const size_t __i = __pos - __param._M_cp.begin();
2648 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2650 const double __a = 0.5 * __param._M_m[__i];
2651 const double __b = __param._M_den[__i];
2652 const double __cm = __p - __pref;
2654 _RealType __x = __param._M_int[__i];
2659 const double __d = __b * __b + 4.0 * __a * __cm;
2660 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2666 template<typename _RealType, typename _CharT, typename _Traits>
2667 std::basic_ostream<_CharT, _Traits>&
2668 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2669 const piecewise_linear_distribution<_RealType>& __x)
2671 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2672 typedef typename __ostream_type::ios_base __ios_base;
2674 const typename __ios_base::fmtflags __flags = __os.flags();
2675 const _CharT __fill = __os.fill();
2676 const std::streamsize __precision = __os.precision();
2677 const _CharT __space = __os.widen(' ');
2678 __os.flags(__ios_base::scientific | __ios_base::left);
2680 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2682 std::vector<_RealType> __int = __x.intervals();
2683 __os << __int.size() - 1;
2685 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2686 __os << __space << *__xit;
2688 std::vector<double> __den = __x.densities();
2689 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2690 __os << __space << *__dit;
2692 __os.flags(__flags);
2694 __os.precision(__precision);
2698 template<typename _RealType, typename _CharT, typename _Traits>
2699 std::basic_istream<_CharT, _Traits>&
2700 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2701 piecewise_linear_distribution<_RealType>& __x)
2703 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2704 typedef typename __istream_type::ios_base __ios_base;
2706 const typename __ios_base::fmtflags __flags = __is.flags();
2707 __is.flags(__ios_base::dec | __ios_base::skipws);
2712 std::vector<_RealType> __int_vec;
2713 __int_vec.reserve(__n + 1);
2714 for (size_t __i = 0; __i <= __n; ++__i)
2718 __int_vec.push_back(__int);
2721 std::vector<double> __den_vec;
2722 __den_vec.reserve(__n + 1);
2723 for (size_t __i = 0; __i <= __n; ++__i)
2727 __den_vec.push_back(__den);
2730 __x.param(typename piecewise_linear_distribution<_RealType>::
2731 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2733 __is.flags(__flags);
2738 template<typename _IntType>
2739 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2741 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2742 _M_v.push_back(__detail::__mod<result_type,
2743 __detail::_Shift<result_type, 32>::__value>(*__iter));
2746 template<typename _InputIterator>
2747 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2749 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2750 _M_v.push_back(__detail::__mod<result_type,
2751 __detail::_Shift<result_type, 32>::__value>(*__iter));
2754 template<typename _RandomAccessIterator>
2756 seed_seq::generate(_RandomAccessIterator __begin,
2757 _RandomAccessIterator __end)
2759 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2762 if (__begin == __end)
2765 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2767 const size_t __n = __end - __begin;
2768 const size_t __s = _M_v.size();
2769 const size_t __t = (__n >= 623) ? 11
2774 const size_t __p = (__n - __t) / 2;
2775 const size_t __q = __p + __t;
2776 const size_t __m = std::max(__s + 1, __n);
2778 for (size_t __k = 0; __k < __m; ++__k)
2780 _Type __arg = (__begin[__k % __n]
2781 ^ __begin[(__k + __p) % __n]
2782 ^ __begin[(__k - 1) % __n]);
2783 _Type __r1 = __arg ^ (__arg >> 27);
2784 __r1 = __detail::__mod<_Type,
2785 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
2789 else if (__k <= __s)
2790 __r2 += __k % __n + _M_v[__k - 1];
2793 __r2 = __detail::__mod<_Type,
2794 __detail::_Shift<_Type, 32>::__value>(__r2);
2795 __begin[(__k + __p) % __n] += __r1;
2796 __begin[(__k + __q) % __n] += __r2;
2797 __begin[__k % __n] = __r2;
2800 for (size_t __k = __m; __k < __m + __n; ++__k)
2802 _Type __arg = (__begin[__k % __n]
2803 + __begin[(__k + __p) % __n]
2804 + __begin[(__k - 1) % __n]);
2805 _Type __r3 = __arg ^ (__arg >> 27);
2806 __r3 = __detail::__mod<_Type,
2807 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
2808 _Type __r4 = __r3 - __k % __n;
2809 __r4 = __detail::__mod<_Type,
2810 __detail::_Shift<_Type, 32>::__value>(__r4);
2811 __begin[(__k + __p) % __n] ^= __r3;
2812 __begin[(__k + __q) % __n] ^= __r4;
2813 __begin[__k % __n] = __r4;
2817 template<typename _RealType, size_t __bits,
2818 typename _UniformRandomNumberGenerator>
2820 generate_canonical(_UniformRandomNumberGenerator& __urng)
2823 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2825 const long double __r = static_cast<long double>(__urng.max())
2826 - static_cast<long double>(__urng.min()) + 1.0L;
2827 const size_t __log2r = std::log(__r) / std::log(2.0L);
2828 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2829 _RealType __sum = _RealType(0);
2830 _RealType __tmp = _RealType(1);
2831 for (; __k != 0; --__k)
2833 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2836 return __sum / __tmp;
2839 _GLIBCXX_END_NAMESPACE_VERSION