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>::
400 const _UIntType __upper_mask = (~_UIntType()) << __r;
401 const _UIntType __lower_mask = ~__upper_mask;
403 for (size_t __k = 0; __k < (__n - __m); ++__k)
405 _UIntType __y = ((_M_x[__k] & __upper_mask)
406 | (_M_x[__k + 1] & __lower_mask));
407 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
408 ^ ((__y & 0x01) ? __a : 0));
411 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
413 _UIntType __y = ((_M_x[__k] & __upper_mask)
414 | (_M_x[__k + 1] & __lower_mask));
415 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
416 ^ ((__y & 0x01) ? __a : 0));
419 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
420 | (_M_x[0] & __lower_mask));
421 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
422 ^ ((__y & 0x01) ? __a : 0));
426 template<typename _UIntType, size_t __w,
427 size_t __n, size_t __m, size_t __r,
428 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
429 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
432 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
433 __s, __b, __t, __c, __l, __f>::
434 discard(unsigned long long __z)
436 while (__z > state_size - _M_p)
438 __z -= state_size - _M_p;
444 template<typename _UIntType, size_t __w,
445 size_t __n, size_t __m, size_t __r,
446 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
447 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
450 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
451 __s, __b, __t, __c, __l, __f>::result_type
452 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
453 __s, __b, __t, __c, __l, __f>::
456 // Reload the vector - cost is O(n) amortized over n calls.
457 if (_M_p >= state_size)
460 // Calculate o(x(i)).
461 result_type __z = _M_x[_M_p++];
462 __z ^= (__z >> __u) & __d;
463 __z ^= (__z << __s) & __b;
464 __z ^= (__z << __t) & __c;
470 template<typename _UIntType, size_t __w,
471 size_t __n, size_t __m, size_t __r,
472 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
473 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
474 _UIntType __f, typename _CharT, typename _Traits>
475 std::basic_ostream<_CharT, _Traits>&
476 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
477 const mersenne_twister_engine<_UIntType, __w, __n, __m,
478 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
480 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
481 typedef typename __ostream_type::ios_base __ios_base;
483 const typename __ios_base::fmtflags __flags = __os.flags();
484 const _CharT __fill = __os.fill();
485 const _CharT __space = __os.widen(' ');
486 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
489 for (size_t __i = 0; __i < __n; ++__i)
490 __os << __x._M_x[__i] << __space;
498 template<typename _UIntType, size_t __w,
499 size_t __n, size_t __m, size_t __r,
500 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
501 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
502 _UIntType __f, typename _CharT, typename _Traits>
503 std::basic_istream<_CharT, _Traits>&
504 operator>>(std::basic_istream<_CharT, _Traits>& __is,
505 mersenne_twister_engine<_UIntType, __w, __n, __m,
506 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
508 typedef std::basic_istream<_CharT, _Traits> __istream_type;
509 typedef typename __istream_type::ios_base __ios_base;
511 const typename __ios_base::fmtflags __flags = __is.flags();
512 __is.flags(__ios_base::dec | __ios_base::skipws);
514 for (size_t __i = 0; __i < __n; ++__i)
515 __is >> __x._M_x[__i];
523 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
525 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
527 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
529 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
531 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
533 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
535 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
537 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
539 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
541 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
542 seed(result_type __value)
544 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
545 __lcg(__value == 0u ? default_seed : __value);
547 const size_t __n = (__w + 31) / 32;
549 for (size_t __i = 0; __i < long_lag; ++__i)
551 _UIntType __sum = 0u;
552 _UIntType __factor = 1u;
553 for (size_t __j = 0; __j < __n; ++__j)
555 __sum += __detail::__mod<uint_least32_t,
556 __detail::_Shift<uint_least32_t, 32>::__value>
557 (__lcg()) * __factor;
558 __factor *= __detail::_Shift<_UIntType, 32>::__value;
560 _M_x[__i] = __detail::__mod<_UIntType,
561 __detail::_Shift<_UIntType, __w>::__value>(__sum);
563 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
567 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
568 template<typename _Sseq>
569 typename std::enable_if<std::is_class<_Sseq>::value>::type
570 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
573 const size_t __k = (__w + 31) / 32;
574 uint_least32_t __arr[__r * __k];
575 __q.generate(__arr + 0, __arr + __r * __k);
577 for (size_t __i = 0; __i < long_lag; ++__i)
579 _UIntType __sum = 0u;
580 _UIntType __factor = 1u;
581 for (size_t __j = 0; __j < __k; ++__j)
583 __sum += __arr[__k * __i + __j] * __factor;
584 __factor *= __detail::_Shift<_UIntType, 32>::__value;
586 _M_x[__i] = __detail::__mod<_UIntType,
587 __detail::_Shift<_UIntType, __w>::__value>(__sum);
589 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
593 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
594 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
596 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
599 // Derive short lag index from current index.
600 long __ps = _M_p - short_lag;
604 // Calculate new x(i) without overflow or division.
605 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
608 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
610 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
615 __xi = (__detail::_Shift<_UIntType, __w>::__value
616 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
621 // Adjust current index to loop around in ring buffer.
622 if (++_M_p >= long_lag)
628 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
629 typename _CharT, typename _Traits>
630 std::basic_ostream<_CharT, _Traits>&
631 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
632 const subtract_with_carry_engine<_UIntType,
635 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
636 typedef typename __ostream_type::ios_base __ios_base;
638 const typename __ios_base::fmtflags __flags = __os.flags();
639 const _CharT __fill = __os.fill();
640 const _CharT __space = __os.widen(' ');
641 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
644 for (size_t __i = 0; __i < __r; ++__i)
645 __os << __x._M_x[__i] << __space;
646 __os << __x._M_carry << __space << __x._M_p;
653 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
654 typename _CharT, typename _Traits>
655 std::basic_istream<_CharT, _Traits>&
656 operator>>(std::basic_istream<_CharT, _Traits>& __is,
657 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
659 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
660 typedef typename __istream_type::ios_base __ios_base;
662 const typename __ios_base::fmtflags __flags = __is.flags();
663 __is.flags(__ios_base::dec | __ios_base::skipws);
665 for (size_t __i = 0; __i < __r; ++__i)
666 __is >> __x._M_x[__i];
667 __is >> __x._M_carry;
675 template<typename _RandomNumberEngine, size_t __p, size_t __r>
677 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
679 template<typename _RandomNumberEngine, size_t __p, size_t __r>
681 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
683 template<typename _RandomNumberEngine, size_t __p, size_t __r>
684 typename discard_block_engine<_RandomNumberEngine,
685 __p, __r>::result_type
686 discard_block_engine<_RandomNumberEngine, __p, __r>::
689 if (_M_n >= used_block)
691 _M_b.discard(block_size - _M_n);
698 template<typename _RandomNumberEngine, size_t __p, size_t __r,
699 typename _CharT, typename _Traits>
700 std::basic_ostream<_CharT, _Traits>&
701 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
702 const discard_block_engine<_RandomNumberEngine,
705 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
706 typedef typename __ostream_type::ios_base __ios_base;
708 const typename __ios_base::fmtflags __flags = __os.flags();
709 const _CharT __fill = __os.fill();
710 const _CharT __space = __os.widen(' ');
711 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
714 __os << __x.base() << __space << __x._M_n;
721 template<typename _RandomNumberEngine, size_t __p, size_t __r,
722 typename _CharT, typename _Traits>
723 std::basic_istream<_CharT, _Traits>&
724 operator>>(std::basic_istream<_CharT, _Traits>& __is,
725 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
727 typedef std::basic_istream<_CharT, _Traits> __istream_type;
728 typedef typename __istream_type::ios_base __ios_base;
730 const typename __ios_base::fmtflags __flags = __is.flags();
731 __is.flags(__ios_base::dec | __ios_base::skipws);
733 __is >> __x._M_b >> __x._M_n;
740 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
741 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
743 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
746 typedef typename _RandomNumberEngine::result_type _Eresult_type;
747 const _Eresult_type __r
748 = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max()
749 ? _M_b.max() - _M_b.min() + 1 : 0);
750 const unsigned __edig = std::numeric_limits<_Eresult_type>::digits;
751 const unsigned __m = __r ? std::__lg(__r) : __edig;
753 typedef typename std::common_type<_Eresult_type, result_type>::type
755 const unsigned __cdig = std::numeric_limits<__ctype>::digits;
758 __ctype __s0, __s1, __y0, __y1;
760 for (size_t __i = 0; __i < 2; ++__i)
762 __n = (__w + __m - 1) / __m + __i;
763 __n0 = __n - __w % __n;
764 const unsigned __w0 = __w / __n; // __w0 <= __m
770 __s0 = __ctype(1) << __w0;
778 __y0 = __s0 * (__r / __s0);
780 __y1 = __s1 * (__r / __s1);
782 if (__r - __y0 <= __y0 / __n)
789 result_type __sum = 0;
790 for (size_t __k = 0; __k < __n0; ++__k)
794 __u = _M_b() - _M_b.min();
795 while (__y0 && __u >= __y0);
796 __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
798 for (size_t __k = __n0; __k < __n; ++__k)
802 __u = _M_b() - _M_b.min();
803 while (__y1 && __u >= __y1);
804 __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
810 template<typename _RandomNumberEngine, size_t __k>
812 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
814 template<typename _RandomNumberEngine, size_t __k>
815 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
816 shuffle_order_engine<_RandomNumberEngine, __k>::
819 size_t __j = __k * ((_M_y - _M_b.min())
820 / (_M_b.max() - _M_b.min() + 1.0L));
827 template<typename _RandomNumberEngine, size_t __k,
828 typename _CharT, typename _Traits>
829 std::basic_ostream<_CharT, _Traits>&
830 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
831 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
833 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
834 typedef typename __ostream_type::ios_base __ios_base;
836 const typename __ios_base::fmtflags __flags = __os.flags();
837 const _CharT __fill = __os.fill();
838 const _CharT __space = __os.widen(' ');
839 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
843 for (size_t __i = 0; __i < __k; ++__i)
844 __os << __space << __x._M_v[__i];
845 __os << __space << __x._M_y;
852 template<typename _RandomNumberEngine, size_t __k,
853 typename _CharT, typename _Traits>
854 std::basic_istream<_CharT, _Traits>&
855 operator>>(std::basic_istream<_CharT, _Traits>& __is,
856 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
858 typedef std::basic_istream<_CharT, _Traits> __istream_type;
859 typedef typename __istream_type::ios_base __ios_base;
861 const typename __ios_base::fmtflags __flags = __is.flags();
862 __is.flags(__ios_base::dec | __ios_base::skipws);
865 for (size_t __i = 0; __i < __k; ++__i)
866 __is >> __x._M_v[__i];
874 template<typename _IntType>
875 template<typename _UniformRandomNumberGenerator>
876 typename uniform_int_distribution<_IntType>::result_type
877 uniform_int_distribution<_IntType>::
878 operator()(_UniformRandomNumberGenerator& __urng,
879 const param_type& __param)
881 typedef typename _UniformRandomNumberGenerator::result_type
883 typedef typename std::make_unsigned<result_type>::type __utype;
884 typedef typename std::common_type<_Gresult_type, __utype>::type
887 const __uctype __urngmin = __urng.min();
888 const __uctype __urngmax = __urng.max();
889 const __uctype __urngrange = __urngmax - __urngmin;
890 const __uctype __urange
891 = __uctype(__param.b()) - __uctype(__param.a());
895 if (__urngrange > __urange)
898 const __uctype __uerange = __urange + 1; // __urange can be zero
899 const __uctype __scaling = __urngrange / __uerange;
900 const __uctype __past = __uerange * __scaling;
902 __ret = __uctype(__urng()) - __urngmin;
903 while (__ret >= __past);
906 else if (__urngrange < __urange)
910 Note that every value in [0, urange]
911 can be written uniquely as
913 (urngrange + 1) * high + low
917 high in [0, urange / (urngrange + 1)]
921 low in [0, urngrange].
923 __uctype __tmp; // wraparound control
926 const __uctype __uerngrange = __urngrange + 1;
927 __tmp = (__uerngrange * operator()
928 (__urng, param_type(0, __urange / __uerngrange)));
929 __ret = __tmp + (__uctype(__urng()) - __urngmin);
931 while (__ret > __urange || __ret < __tmp);
934 __ret = __uctype(__urng()) - __urngmin;
936 return __ret + __param.a();
939 template<typename _IntType, typename _CharT, typename _Traits>
940 std::basic_ostream<_CharT, _Traits>&
941 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
942 const uniform_int_distribution<_IntType>& __x)
944 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
945 typedef typename __ostream_type::ios_base __ios_base;
947 const typename __ios_base::fmtflags __flags = __os.flags();
948 const _CharT __fill = __os.fill();
949 const _CharT __space = __os.widen(' ');
950 __os.flags(__ios_base::scientific | __ios_base::left);
953 __os << __x.a() << __space << __x.b();
960 template<typename _IntType, typename _CharT, typename _Traits>
961 std::basic_istream<_CharT, _Traits>&
962 operator>>(std::basic_istream<_CharT, _Traits>& __is,
963 uniform_int_distribution<_IntType>& __x)
965 typedef std::basic_istream<_CharT, _Traits> __istream_type;
966 typedef typename __istream_type::ios_base __ios_base;
968 const typename __ios_base::fmtflags __flags = __is.flags();
969 __is.flags(__ios_base::dec | __ios_base::skipws);
973 __x.param(typename uniform_int_distribution<_IntType>::
974 param_type(__a, __b));
981 template<typename _RealType, typename _CharT, typename _Traits>
982 std::basic_ostream<_CharT, _Traits>&
983 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
984 const uniform_real_distribution<_RealType>& __x)
986 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
987 typedef typename __ostream_type::ios_base __ios_base;
989 const typename __ios_base::fmtflags __flags = __os.flags();
990 const _CharT __fill = __os.fill();
991 const std::streamsize __precision = __os.precision();
992 const _CharT __space = __os.widen(' ');
993 __os.flags(__ios_base::scientific | __ios_base::left);
995 __os.precision(std::numeric_limits<_RealType>::max_digits10);
997 __os << __x.a() << __space << __x.b();
1001 __os.precision(__precision);
1005 template<typename _RealType, typename _CharT, typename _Traits>
1006 std::basic_istream<_CharT, _Traits>&
1007 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1008 uniform_real_distribution<_RealType>& __x)
1010 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1011 typedef typename __istream_type::ios_base __ios_base;
1013 const typename __ios_base::fmtflags __flags = __is.flags();
1014 __is.flags(__ios_base::skipws);
1018 __x.param(typename uniform_real_distribution<_RealType>::
1019 param_type(__a, __b));
1021 __is.flags(__flags);
1026 template<typename _CharT, typename _Traits>
1027 std::basic_ostream<_CharT, _Traits>&
1028 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1029 const bernoulli_distribution& __x)
1031 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1032 typedef typename __ostream_type::ios_base __ios_base;
1034 const typename __ios_base::fmtflags __flags = __os.flags();
1035 const _CharT __fill = __os.fill();
1036 const std::streamsize __precision = __os.precision();
1037 __os.flags(__ios_base::scientific | __ios_base::left);
1038 __os.fill(__os.widen(' '));
1039 __os.precision(std::numeric_limits<double>::max_digits10);
1043 __os.flags(__flags);
1045 __os.precision(__precision);
1050 template<typename _IntType>
1051 template<typename _UniformRandomNumberGenerator>
1052 typename geometric_distribution<_IntType>::result_type
1053 geometric_distribution<_IntType>::
1054 operator()(_UniformRandomNumberGenerator& __urng,
1055 const param_type& __param)
1057 // About the epsilon thing see this thread:
1058 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1059 const double __naf =
1060 (1 - std::numeric_limits<double>::epsilon()) / 2;
1061 // The largest _RealType convertible to _IntType.
1062 const double __thr =
1063 std::numeric_limits<_IntType>::max() + __naf;
1064 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1069 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1070 while (__cand >= __thr);
1072 return result_type(__cand + __naf);
1075 template<typename _IntType,
1076 typename _CharT, typename _Traits>
1077 std::basic_ostream<_CharT, _Traits>&
1078 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1079 const geometric_distribution<_IntType>& __x)
1081 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1082 typedef typename __ostream_type::ios_base __ios_base;
1084 const typename __ios_base::fmtflags __flags = __os.flags();
1085 const _CharT __fill = __os.fill();
1086 const std::streamsize __precision = __os.precision();
1087 __os.flags(__ios_base::scientific | __ios_base::left);
1088 __os.fill(__os.widen(' '));
1089 __os.precision(std::numeric_limits<double>::max_digits10);
1093 __os.flags(__flags);
1095 __os.precision(__precision);
1099 template<typename _IntType,
1100 typename _CharT, typename _Traits>
1101 std::basic_istream<_CharT, _Traits>&
1102 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1103 geometric_distribution<_IntType>& __x)
1105 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1106 typedef typename __istream_type::ios_base __ios_base;
1108 const typename __ios_base::fmtflags __flags = __is.flags();
1109 __is.flags(__ios_base::skipws);
1113 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1115 __is.flags(__flags);
1119 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1120 template<typename _IntType>
1121 template<typename _UniformRandomNumberGenerator>
1122 typename negative_binomial_distribution<_IntType>::result_type
1123 negative_binomial_distribution<_IntType>::
1124 operator()(_UniformRandomNumberGenerator& __urng)
1126 const double __y = _M_gd(__urng);
1128 // XXX Is the constructor too slow?
1129 std::poisson_distribution<result_type> __poisson(__y);
1130 return __poisson(__urng);
1133 template<typename _IntType>
1134 template<typename _UniformRandomNumberGenerator>
1135 typename negative_binomial_distribution<_IntType>::result_type
1136 negative_binomial_distribution<_IntType>::
1137 operator()(_UniformRandomNumberGenerator& __urng,
1138 const param_type& __p)
1140 typedef typename std::gamma_distribution<result_type>::param_type
1144 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1146 std::poisson_distribution<result_type> __poisson(__y);
1147 return __poisson(__urng);
1150 template<typename _IntType, typename _CharT, typename _Traits>
1151 std::basic_ostream<_CharT, _Traits>&
1152 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1153 const negative_binomial_distribution<_IntType>& __x)
1155 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1156 typedef typename __ostream_type::ios_base __ios_base;
1158 const typename __ios_base::fmtflags __flags = __os.flags();
1159 const _CharT __fill = __os.fill();
1160 const std::streamsize __precision = __os.precision();
1161 const _CharT __space = __os.widen(' ');
1162 __os.flags(__ios_base::scientific | __ios_base::left);
1163 __os.fill(__os.widen(' '));
1164 __os.precision(std::numeric_limits<double>::max_digits10);
1166 __os << __x.k() << __space << __x.p()
1167 << __space << __x._M_gd;
1169 __os.flags(__flags);
1171 __os.precision(__precision);
1175 template<typename _IntType, typename _CharT, typename _Traits>
1176 std::basic_istream<_CharT, _Traits>&
1177 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1178 negative_binomial_distribution<_IntType>& __x)
1180 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1181 typedef typename __istream_type::ios_base __ios_base;
1183 const typename __ios_base::fmtflags __flags = __is.flags();
1184 __is.flags(__ios_base::skipws);
1188 __is >> __k >> __p >> __x._M_gd;
1189 __x.param(typename negative_binomial_distribution<_IntType>::
1190 param_type(__k, __p));
1192 __is.flags(__flags);
1197 template<typename _IntType>
1199 poisson_distribution<_IntType>::param_type::
1202 #if _GLIBCXX_USE_C99_MATH_TR1
1205 const double __m = std::floor(_M_mean);
1206 _M_lm_thr = std::log(_M_mean);
1207 _M_lfm = std::lgamma(__m + 1);
1208 _M_sm = std::sqrt(__m);
1210 const double __pi_4 = 0.7853981633974483096156608458198757L;
1211 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1213 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1214 const double __cx = 2 * __m + _M_d;
1215 _M_scx = std::sqrt(__cx / 2);
1218 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1219 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1224 _M_lm_thr = std::exp(-_M_mean);
1228 * A rejection algorithm when mean >= 12 and a simple method based
1229 * upon the multiplication of uniform random variates otherwise.
1230 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1234 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1235 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1237 template<typename _IntType>
1238 template<typename _UniformRandomNumberGenerator>
1239 typename poisson_distribution<_IntType>::result_type
1240 poisson_distribution<_IntType>::
1241 operator()(_UniformRandomNumberGenerator& __urng,
1242 const param_type& __param)
1244 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1246 #if _GLIBCXX_USE_C99_MATH_TR1
1247 if (__param.mean() >= 12)
1251 // See comments above...
1252 const double __naf =
1253 (1 - std::numeric_limits<double>::epsilon()) / 2;
1254 const double __thr =
1255 std::numeric_limits<_IntType>::max() + __naf;
1257 const double __m = std::floor(__param.mean());
1259 const double __spi_2 = 1.2533141373155002512078826424055226L;
1260 const double __c1 = __param._M_sm * __spi_2;
1261 const double __c2 = __param._M_c2b + __c1;
1262 const double __c3 = __c2 + 1;
1263 const double __c4 = __c3 + 1;
1265 const double __e178 = 1.0129030479320018583185514777512983L;
1266 const double __c5 = __c4 + __e178;
1267 const double __c = __param._M_cb + __c5;
1268 const double __2cx = 2 * (2 * __m + __param._M_d);
1270 bool __reject = true;
1273 const double __u = __c * __aurng();
1274 const double __e = -std::log(__aurng());
1280 const double __n = _M_nd(__urng);
1281 const double __y = -std::abs(__n) * __param._M_sm - 1;
1282 __x = std::floor(__y);
1283 __w = -__n * __n / 2;
1287 else if (__u <= __c2)
1289 const double __n = _M_nd(__urng);
1290 const double __y = 1 + std::abs(__n) * __param._M_scx;
1291 __x = std::ceil(__y);
1292 __w = __y * (2 - __y) * __param._M_1cx;
1293 if (__x > __param._M_d)
1296 else if (__u <= __c3)
1297 // NB: This case not in the book, nor in the Errata,
1298 // but should be ok...
1300 else if (__u <= __c4)
1302 else if (__u <= __c5)
1306 const double __v = -std::log(__aurng());
1307 const double __y = __param._M_d
1308 + __v * __2cx / __param._M_d;
1309 __x = std::ceil(__y);
1310 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1313 __reject = (__w - __e - __x * __param._M_lm_thr
1314 > __param._M_lfm - std::lgamma(__x + __m + 1));
1316 __reject |= __x + __m >= __thr;
1320 return result_type(__x + __m + __naf);
1326 double __prod = 1.0;
1330 __prod *= __aurng();
1333 while (__prod > __param._M_lm_thr);
1339 template<typename _IntType,
1340 typename _CharT, typename _Traits>
1341 std::basic_ostream<_CharT, _Traits>&
1342 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1343 const poisson_distribution<_IntType>& __x)
1345 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1346 typedef typename __ostream_type::ios_base __ios_base;
1348 const typename __ios_base::fmtflags __flags = __os.flags();
1349 const _CharT __fill = __os.fill();
1350 const std::streamsize __precision = __os.precision();
1351 const _CharT __space = __os.widen(' ');
1352 __os.flags(__ios_base::scientific | __ios_base::left);
1354 __os.precision(std::numeric_limits<double>::max_digits10);
1356 __os << __x.mean() << __space << __x._M_nd;
1358 __os.flags(__flags);
1360 __os.precision(__precision);
1364 template<typename _IntType,
1365 typename _CharT, typename _Traits>
1366 std::basic_istream<_CharT, _Traits>&
1367 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1368 poisson_distribution<_IntType>& __x)
1370 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1371 typedef typename __istream_type::ios_base __ios_base;
1373 const typename __ios_base::fmtflags __flags = __is.flags();
1374 __is.flags(__ios_base::skipws);
1377 __is >> __mean >> __x._M_nd;
1378 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1380 __is.flags(__flags);
1385 template<typename _IntType>
1387 binomial_distribution<_IntType>::param_type::
1390 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1394 #if _GLIBCXX_USE_C99_MATH_TR1
1395 if (_M_t * __p12 >= 8)
1398 const double __np = std::floor(_M_t * __p12);
1399 const double __pa = __np / _M_t;
1400 const double __1p = 1 - __pa;
1402 const double __pi_4 = 0.7853981633974483096156608458198757L;
1403 const double __d1x =
1404 std::sqrt(__np * __1p * std::log(32 * __np
1405 / (81 * __pi_4 * __1p)));
1406 _M_d1 = std::round(std::max(1.0, __d1x));
1407 const double __d2x =
1408 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1409 / (__pi_4 * __pa)));
1410 _M_d2 = std::round(std::max(1.0, __d2x));
1413 const double __spi_2 = 1.2533141373155002512078826424055226L;
1414 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1415 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1416 _M_c = 2 * _M_d1 / __np;
1417 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1418 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1419 const double __s1s = _M_s1 * _M_s1;
1420 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1422 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1423 const double __s2s = _M_s2 * _M_s2;
1424 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1425 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1426 _M_lf = (std::lgamma(__np + 1)
1427 + std::lgamma(_M_t - __np + 1));
1428 _M_lp1p = std::log(__pa / __1p);
1430 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1434 _M_q = -std::log(1 - __p12);
1437 template<typename _IntType>
1438 template<typename _UniformRandomNumberGenerator>
1439 typename binomial_distribution<_IntType>::result_type
1440 binomial_distribution<_IntType>::
1441 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1445 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1450 const double __e = -std::log(__aurng());
1451 __sum += __e / (__t - __x);
1454 while (__sum <= _M_param._M_q);
1460 * A rejection algorithm when t * p >= 8 and a simple waiting time
1461 * method - the second in the referenced book - otherwise.
1462 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1466 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1467 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1469 template<typename _IntType>
1470 template<typename _UniformRandomNumberGenerator>
1471 typename binomial_distribution<_IntType>::result_type
1472 binomial_distribution<_IntType>::
1473 operator()(_UniformRandomNumberGenerator& __urng,
1474 const param_type& __param)
1477 const _IntType __t = __param.t();
1478 const double __p = __param.p();
1479 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1480 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1483 #if _GLIBCXX_USE_C99_MATH_TR1
1484 if (!__param._M_easy)
1488 // See comments above...
1489 const double __naf =
1490 (1 - std::numeric_limits<double>::epsilon()) / 2;
1491 const double __thr =
1492 std::numeric_limits<_IntType>::max() + __naf;
1494 const double __np = std::floor(__t * __p12);
1497 const double __spi_2 = 1.2533141373155002512078826424055226L;
1498 const double __a1 = __param._M_a1;
1499 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1500 const double __a123 = __param._M_a123;
1501 const double __s1s = __param._M_s1 * __param._M_s1;
1502 const double __s2s = __param._M_s2 * __param._M_s2;
1507 const double __u = __param._M_s * __aurng();
1513 const double __n = _M_nd(__urng);
1514 const double __y = __param._M_s1 * std::abs(__n);
1515 __reject = __y >= __param._M_d1;
1518 const double __e = -std::log(__aurng());
1519 __x = std::floor(__y);
1520 __v = -__e - __n * __n / 2 + __param._M_c;
1523 else if (__u <= __a12)
1525 const double __n = _M_nd(__urng);
1526 const double __y = __param._M_s2 * std::abs(__n);
1527 __reject = __y >= __param._M_d2;
1530 const double __e = -std::log(__aurng());
1531 __x = std::floor(-__y);
1532 __v = -__e - __n * __n / 2;
1535 else if (__u <= __a123)
1537 const double __e1 = -std::log(__aurng());
1538 const double __e2 = -std::log(__aurng());
1540 const double __y = __param._M_d1
1541 + 2 * __s1s * __e1 / __param._M_d1;
1542 __x = std::floor(__y);
1543 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1544 -__y / (2 * __s1s)));
1549 const double __e1 = -std::log(__aurng());
1550 const double __e2 = -std::log(__aurng());
1552 const double __y = __param._M_d2
1553 + 2 * __s2s * __e1 / __param._M_d2;
1554 __x = std::floor(-__y);
1555 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1559 __reject = __reject || __x < -__np || __x > __t - __np;
1562 const double __lfx =
1563 std::lgamma(__np + __x + 1)
1564 + std::lgamma(__t - (__np + __x) + 1);
1565 __reject = __v > __param._M_lf - __lfx
1566 + __x * __param._M_lp1p;
1569 __reject |= __x + __np >= __thr;
1573 __x += __np + __naf;
1575 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1576 __ret = _IntType(__x) + __z;
1580 __ret = _M_waiting(__urng, __t);
1583 __ret = __t - __ret;
1587 template<typename _IntType,
1588 typename _CharT, typename _Traits>
1589 std::basic_ostream<_CharT, _Traits>&
1590 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1591 const binomial_distribution<_IntType>& __x)
1593 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1594 typedef typename __ostream_type::ios_base __ios_base;
1596 const typename __ios_base::fmtflags __flags = __os.flags();
1597 const _CharT __fill = __os.fill();
1598 const std::streamsize __precision = __os.precision();
1599 const _CharT __space = __os.widen(' ');
1600 __os.flags(__ios_base::scientific | __ios_base::left);
1602 __os.precision(std::numeric_limits<double>::max_digits10);
1604 __os << __x.t() << __space << __x.p()
1605 << __space << __x._M_nd;
1607 __os.flags(__flags);
1609 __os.precision(__precision);
1613 template<typename _IntType,
1614 typename _CharT, typename _Traits>
1615 std::basic_istream<_CharT, _Traits>&
1616 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1617 binomial_distribution<_IntType>& __x)
1619 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1620 typedef typename __istream_type::ios_base __ios_base;
1622 const typename __ios_base::fmtflags __flags = __is.flags();
1623 __is.flags(__ios_base::dec | __ios_base::skipws);
1627 __is >> __t >> __p >> __x._M_nd;
1628 __x.param(typename binomial_distribution<_IntType>::
1629 param_type(__t, __p));
1631 __is.flags(__flags);
1636 template<typename _RealType, typename _CharT, typename _Traits>
1637 std::basic_ostream<_CharT, _Traits>&
1638 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1639 const exponential_distribution<_RealType>& __x)
1641 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1642 typedef typename __ostream_type::ios_base __ios_base;
1644 const typename __ios_base::fmtflags __flags = __os.flags();
1645 const _CharT __fill = __os.fill();
1646 const std::streamsize __precision = __os.precision();
1647 __os.flags(__ios_base::scientific | __ios_base::left);
1648 __os.fill(__os.widen(' '));
1649 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1651 __os << __x.lambda();
1653 __os.flags(__flags);
1655 __os.precision(__precision);
1659 template<typename _RealType, typename _CharT, typename _Traits>
1660 std::basic_istream<_CharT, _Traits>&
1661 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1662 exponential_distribution<_RealType>& __x)
1664 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1665 typedef typename __istream_type::ios_base __ios_base;
1667 const typename __ios_base::fmtflags __flags = __is.flags();
1668 __is.flags(__ios_base::dec | __ios_base::skipws);
1672 __x.param(typename exponential_distribution<_RealType>::
1673 param_type(__lambda));
1675 __is.flags(__flags);
1681 * Polar method due to Marsaglia.
1683 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1684 * New York, 1986, Ch. V, Sect. 4.4.
1686 template<typename _RealType>
1687 template<typename _UniformRandomNumberGenerator>
1688 typename normal_distribution<_RealType>::result_type
1689 normal_distribution<_RealType>::
1690 operator()(_UniformRandomNumberGenerator& __urng,
1691 const param_type& __param)
1694 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1697 if (_M_saved_available)
1699 _M_saved_available = false;
1704 result_type __x, __y, __r2;
1707 __x = result_type(2.0) * __aurng() - 1.0;
1708 __y = result_type(2.0) * __aurng() - 1.0;
1709 __r2 = __x * __x + __y * __y;
1711 while (__r2 > 1.0 || __r2 == 0.0);
1713 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1714 _M_saved = __x * __mult;
1715 _M_saved_available = true;
1716 __ret = __y * __mult;
1719 __ret = __ret * __param.stddev() + __param.mean();
1723 template<typename _RealType>
1725 operator==(const std::normal_distribution<_RealType>& __d1,
1726 const std::normal_distribution<_RealType>& __d2)
1728 if (__d1._M_param == __d2._M_param
1729 && __d1._M_saved_available == __d2._M_saved_available)
1731 if (__d1._M_saved_available
1732 && __d1._M_saved == __d2._M_saved)
1734 else if(!__d1._M_saved_available)
1743 template<typename _RealType, typename _CharT, typename _Traits>
1744 std::basic_ostream<_CharT, _Traits>&
1745 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1746 const normal_distribution<_RealType>& __x)
1748 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1749 typedef typename __ostream_type::ios_base __ios_base;
1751 const typename __ios_base::fmtflags __flags = __os.flags();
1752 const _CharT __fill = __os.fill();
1753 const std::streamsize __precision = __os.precision();
1754 const _CharT __space = __os.widen(' ');
1755 __os.flags(__ios_base::scientific | __ios_base::left);
1757 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1759 __os << __x.mean() << __space << __x.stddev()
1760 << __space << __x._M_saved_available;
1761 if (__x._M_saved_available)
1762 __os << __space << __x._M_saved;
1764 __os.flags(__flags);
1766 __os.precision(__precision);
1770 template<typename _RealType, typename _CharT, typename _Traits>
1771 std::basic_istream<_CharT, _Traits>&
1772 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1773 normal_distribution<_RealType>& __x)
1775 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1776 typedef typename __istream_type::ios_base __ios_base;
1778 const typename __ios_base::fmtflags __flags = __is.flags();
1779 __is.flags(__ios_base::dec | __ios_base::skipws);
1781 double __mean, __stddev;
1782 __is >> __mean >> __stddev
1783 >> __x._M_saved_available;
1784 if (__x._M_saved_available)
1785 __is >> __x._M_saved;
1786 __x.param(typename normal_distribution<_RealType>::
1787 param_type(__mean, __stddev));
1789 __is.flags(__flags);
1794 template<typename _RealType, typename _CharT, typename _Traits>
1795 std::basic_ostream<_CharT, _Traits>&
1796 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1797 const lognormal_distribution<_RealType>& __x)
1799 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1800 typedef typename __ostream_type::ios_base __ios_base;
1802 const typename __ios_base::fmtflags __flags = __os.flags();
1803 const _CharT __fill = __os.fill();
1804 const std::streamsize __precision = __os.precision();
1805 const _CharT __space = __os.widen(' ');
1806 __os.flags(__ios_base::scientific | __ios_base::left);
1808 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1810 __os << __x.m() << __space << __x.s()
1811 << __space << __x._M_nd;
1813 __os.flags(__flags);
1815 __os.precision(__precision);
1819 template<typename _RealType, typename _CharT, typename _Traits>
1820 std::basic_istream<_CharT, _Traits>&
1821 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1822 lognormal_distribution<_RealType>& __x)
1824 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1825 typedef typename __istream_type::ios_base __ios_base;
1827 const typename __ios_base::fmtflags __flags = __is.flags();
1828 __is.flags(__ios_base::dec | __ios_base::skipws);
1831 __is >> __m >> __s >> __x._M_nd;
1832 __x.param(typename lognormal_distribution<_RealType>::
1833 param_type(__m, __s));
1835 __is.flags(__flags);
1840 template<typename _RealType, typename _CharT, typename _Traits>
1841 std::basic_ostream<_CharT, _Traits>&
1842 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1843 const chi_squared_distribution<_RealType>& __x)
1845 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1846 typedef typename __ostream_type::ios_base __ios_base;
1848 const typename __ios_base::fmtflags __flags = __os.flags();
1849 const _CharT __fill = __os.fill();
1850 const std::streamsize __precision = __os.precision();
1851 const _CharT __space = __os.widen(' ');
1852 __os.flags(__ios_base::scientific | __ios_base::left);
1854 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1856 __os << __x.n() << __space << __x._M_gd;
1858 __os.flags(__flags);
1860 __os.precision(__precision);
1864 template<typename _RealType, typename _CharT, typename _Traits>
1865 std::basic_istream<_CharT, _Traits>&
1866 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1867 chi_squared_distribution<_RealType>& __x)
1869 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1870 typedef typename __istream_type::ios_base __ios_base;
1872 const typename __ios_base::fmtflags __flags = __is.flags();
1873 __is.flags(__ios_base::dec | __ios_base::skipws);
1876 __is >> __n >> __x._M_gd;
1877 __x.param(typename chi_squared_distribution<_RealType>::
1880 __is.flags(__flags);
1885 template<typename _RealType>
1886 template<typename _UniformRandomNumberGenerator>
1887 typename cauchy_distribution<_RealType>::result_type
1888 cauchy_distribution<_RealType>::
1889 operator()(_UniformRandomNumberGenerator& __urng,
1890 const param_type& __p)
1892 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1899 const _RealType __pi = 3.1415926535897932384626433832795029L;
1900 return __p.a() + __p.b() * std::tan(__pi * __u);
1903 template<typename _RealType, typename _CharT, typename _Traits>
1904 std::basic_ostream<_CharT, _Traits>&
1905 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1906 const cauchy_distribution<_RealType>& __x)
1908 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1909 typedef typename __ostream_type::ios_base __ios_base;
1911 const typename __ios_base::fmtflags __flags = __os.flags();
1912 const _CharT __fill = __os.fill();
1913 const std::streamsize __precision = __os.precision();
1914 const _CharT __space = __os.widen(' ');
1915 __os.flags(__ios_base::scientific | __ios_base::left);
1917 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1919 __os << __x.a() << __space << __x.b();
1921 __os.flags(__flags);
1923 __os.precision(__precision);
1927 template<typename _RealType, typename _CharT, typename _Traits>
1928 std::basic_istream<_CharT, _Traits>&
1929 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1930 cauchy_distribution<_RealType>& __x)
1932 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1933 typedef typename __istream_type::ios_base __ios_base;
1935 const typename __ios_base::fmtflags __flags = __is.flags();
1936 __is.flags(__ios_base::dec | __ios_base::skipws);
1940 __x.param(typename cauchy_distribution<_RealType>::
1941 param_type(__a, __b));
1943 __is.flags(__flags);
1948 template<typename _RealType, typename _CharT, typename _Traits>
1949 std::basic_ostream<_CharT, _Traits>&
1950 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1951 const fisher_f_distribution<_RealType>& __x)
1953 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1954 typedef typename __ostream_type::ios_base __ios_base;
1956 const typename __ios_base::fmtflags __flags = __os.flags();
1957 const _CharT __fill = __os.fill();
1958 const std::streamsize __precision = __os.precision();
1959 const _CharT __space = __os.widen(' ');
1960 __os.flags(__ios_base::scientific | __ios_base::left);
1962 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1964 __os << __x.m() << __space << __x.n()
1965 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1967 __os.flags(__flags);
1969 __os.precision(__precision);
1973 template<typename _RealType, typename _CharT, typename _Traits>
1974 std::basic_istream<_CharT, _Traits>&
1975 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1976 fisher_f_distribution<_RealType>& __x)
1978 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1979 typedef typename __istream_type::ios_base __ios_base;
1981 const typename __ios_base::fmtflags __flags = __is.flags();
1982 __is.flags(__ios_base::dec | __ios_base::skipws);
1985 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1986 __x.param(typename fisher_f_distribution<_RealType>::
1987 param_type(__m, __n));
1989 __is.flags(__flags);
1994 template<typename _RealType, typename _CharT, typename _Traits>
1995 std::basic_ostream<_CharT, _Traits>&
1996 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1997 const student_t_distribution<_RealType>& __x)
1999 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2000 typedef typename __ostream_type::ios_base __ios_base;
2002 const typename __ios_base::fmtflags __flags = __os.flags();
2003 const _CharT __fill = __os.fill();
2004 const std::streamsize __precision = __os.precision();
2005 const _CharT __space = __os.widen(' ');
2006 __os.flags(__ios_base::scientific | __ios_base::left);
2008 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2010 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
2012 __os.flags(__flags);
2014 __os.precision(__precision);
2018 template<typename _RealType, typename _CharT, typename _Traits>
2019 std::basic_istream<_CharT, _Traits>&
2020 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2021 student_t_distribution<_RealType>& __x)
2023 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2024 typedef typename __istream_type::ios_base __ios_base;
2026 const typename __ios_base::fmtflags __flags = __is.flags();
2027 __is.flags(__ios_base::dec | __ios_base::skipws);
2030 __is >> __n >> __x._M_nd >> __x._M_gd;
2031 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
2033 __is.flags(__flags);
2038 template<typename _RealType>
2040 gamma_distribution<_RealType>::param_type::
2043 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2045 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2046 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2050 * Marsaglia, G. and Tsang, W. W.
2051 * "A Simple Method for Generating Gamma Variables"
2052 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2054 template<typename _RealType>
2055 template<typename _UniformRandomNumberGenerator>
2056 typename gamma_distribution<_RealType>::result_type
2057 gamma_distribution<_RealType>::
2058 operator()(_UniformRandomNumberGenerator& __urng,
2059 const param_type& __param)
2061 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2064 result_type __u, __v, __n;
2065 const result_type __a1 = (__param._M_malpha
2066 - _RealType(1.0) / _RealType(3.0));
2072 __n = _M_nd(__urng);
2073 __v = result_type(1.0) + __param._M_a2 * __n;
2077 __v = __v * __v * __v;
2080 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2081 && (std::log(__u) > (0.5 * __n * __n + __a1
2082 * (1.0 - __v + std::log(__v)))));
2084 if (__param.alpha() == __param._M_malpha)
2085 return __a1 * __v * __param.beta();
2092 return (std::pow(__u, result_type(1.0) / __param.alpha())
2093 * __a1 * __v * __param.beta());
2097 template<typename _RealType, typename _CharT, typename _Traits>
2098 std::basic_ostream<_CharT, _Traits>&
2099 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2100 const gamma_distribution<_RealType>& __x)
2102 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2103 typedef typename __ostream_type::ios_base __ios_base;
2105 const typename __ios_base::fmtflags __flags = __os.flags();
2106 const _CharT __fill = __os.fill();
2107 const std::streamsize __precision = __os.precision();
2108 const _CharT __space = __os.widen(' ');
2109 __os.flags(__ios_base::scientific | __ios_base::left);
2111 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2113 __os << __x.alpha() << __space << __x.beta()
2114 << __space << __x._M_nd;
2116 __os.flags(__flags);
2118 __os.precision(__precision);
2122 template<typename _RealType, typename _CharT, typename _Traits>
2123 std::basic_istream<_CharT, _Traits>&
2124 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2125 gamma_distribution<_RealType>& __x)
2127 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2128 typedef typename __istream_type::ios_base __ios_base;
2130 const typename __ios_base::fmtflags __flags = __is.flags();
2131 __is.flags(__ios_base::dec | __ios_base::skipws);
2133 _RealType __alpha_val, __beta_val;
2134 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2135 __x.param(typename gamma_distribution<_RealType>::
2136 param_type(__alpha_val, __beta_val));
2138 __is.flags(__flags);
2143 template<typename _RealType>
2144 template<typename _UniformRandomNumberGenerator>
2145 typename weibull_distribution<_RealType>::result_type
2146 weibull_distribution<_RealType>::
2147 operator()(_UniformRandomNumberGenerator& __urng,
2148 const param_type& __p)
2150 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2152 return __p.b() * std::pow(-std::log(__aurng()),
2153 result_type(1) / __p.a());
2156 template<typename _RealType, typename _CharT, typename _Traits>
2157 std::basic_ostream<_CharT, _Traits>&
2158 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2159 const weibull_distribution<_RealType>& __x)
2161 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2162 typedef typename __ostream_type::ios_base __ios_base;
2164 const typename __ios_base::fmtflags __flags = __os.flags();
2165 const _CharT __fill = __os.fill();
2166 const std::streamsize __precision = __os.precision();
2167 const _CharT __space = __os.widen(' ');
2168 __os.flags(__ios_base::scientific | __ios_base::left);
2170 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2172 __os << __x.a() << __space << __x.b();
2174 __os.flags(__flags);
2176 __os.precision(__precision);
2180 template<typename _RealType, typename _CharT, typename _Traits>
2181 std::basic_istream<_CharT, _Traits>&
2182 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2183 weibull_distribution<_RealType>& __x)
2185 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2186 typedef typename __istream_type::ios_base __ios_base;
2188 const typename __ios_base::fmtflags __flags = __is.flags();
2189 __is.flags(__ios_base::dec | __ios_base::skipws);
2193 __x.param(typename weibull_distribution<_RealType>::
2194 param_type(__a, __b));
2196 __is.flags(__flags);
2201 template<typename _RealType>
2202 template<typename _UniformRandomNumberGenerator>
2203 typename extreme_value_distribution<_RealType>::result_type
2204 extreme_value_distribution<_RealType>::
2205 operator()(_UniformRandomNumberGenerator& __urng,
2206 const param_type& __p)
2208 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2210 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2213 template<typename _RealType, typename _CharT, typename _Traits>
2214 std::basic_ostream<_CharT, _Traits>&
2215 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2216 const extreme_value_distribution<_RealType>& __x)
2218 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2219 typedef typename __ostream_type::ios_base __ios_base;
2221 const typename __ios_base::fmtflags __flags = __os.flags();
2222 const _CharT __fill = __os.fill();
2223 const std::streamsize __precision = __os.precision();
2224 const _CharT __space = __os.widen(' ');
2225 __os.flags(__ios_base::scientific | __ios_base::left);
2227 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2229 __os << __x.a() << __space << __x.b();
2231 __os.flags(__flags);
2233 __os.precision(__precision);
2237 template<typename _RealType, typename _CharT, typename _Traits>
2238 std::basic_istream<_CharT, _Traits>&
2239 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2240 extreme_value_distribution<_RealType>& __x)
2242 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2243 typedef typename __istream_type::ios_base __ios_base;
2245 const typename __ios_base::fmtflags __flags = __is.flags();
2246 __is.flags(__ios_base::dec | __ios_base::skipws);
2250 __x.param(typename extreme_value_distribution<_RealType>::
2251 param_type(__a, __b));
2253 __is.flags(__flags);
2258 template<typename _IntType>
2260 discrete_distribution<_IntType>::param_type::
2263 if (_M_prob.size() < 2)
2269 const double __sum = std::accumulate(_M_prob.begin(),
2270 _M_prob.end(), 0.0);
2271 // Now normalize the probabilites.
2272 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2273 std::bind2nd(std::divides<double>(), __sum));
2274 // Accumulate partial sums.
2275 _M_cp.reserve(_M_prob.size());
2276 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2277 std::back_inserter(_M_cp));
2278 // Make sure the last cumulative probability is one.
2279 _M_cp[_M_cp.size() - 1] = 1.0;
2282 template<typename _IntType>
2283 template<typename _Func>
2284 discrete_distribution<_IntType>::param_type::
2285 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2286 : _M_prob(), _M_cp()
2288 const size_t __n = __nw == 0 ? 1 : __nw;
2289 const double __delta = (__xmax - __xmin) / __n;
2291 _M_prob.reserve(__n);
2292 for (size_t __k = 0; __k < __nw; ++__k)
2293 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2298 template<typename _IntType>
2299 template<typename _UniformRandomNumberGenerator>
2300 typename discrete_distribution<_IntType>::result_type
2301 discrete_distribution<_IntType>::
2302 operator()(_UniformRandomNumberGenerator& __urng,
2303 const param_type& __param)
2305 if (__param._M_cp.empty())
2306 return result_type(0);
2308 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2311 const double __p = __aurng();
2312 auto __pos = std::lower_bound(__param._M_cp.begin(),
2313 __param._M_cp.end(), __p);
2315 return __pos - __param._M_cp.begin();
2318 template<typename _IntType, typename _CharT, typename _Traits>
2319 std::basic_ostream<_CharT, _Traits>&
2320 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2321 const discrete_distribution<_IntType>& __x)
2323 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2324 typedef typename __ostream_type::ios_base __ios_base;
2326 const typename __ios_base::fmtflags __flags = __os.flags();
2327 const _CharT __fill = __os.fill();
2328 const std::streamsize __precision = __os.precision();
2329 const _CharT __space = __os.widen(' ');
2330 __os.flags(__ios_base::scientific | __ios_base::left);
2332 __os.precision(std::numeric_limits<double>::max_digits10);
2334 std::vector<double> __prob = __x.probabilities();
2335 __os << __prob.size();
2336 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2337 __os << __space << *__dit;
2339 __os.flags(__flags);
2341 __os.precision(__precision);
2345 template<typename _IntType, typename _CharT, typename _Traits>
2346 std::basic_istream<_CharT, _Traits>&
2347 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2348 discrete_distribution<_IntType>& __x)
2350 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2351 typedef typename __istream_type::ios_base __ios_base;
2353 const typename __ios_base::fmtflags __flags = __is.flags();
2354 __is.flags(__ios_base::dec | __ios_base::skipws);
2359 std::vector<double> __prob_vec;
2360 __prob_vec.reserve(__n);
2361 for (; __n != 0; --__n)
2365 __prob_vec.push_back(__prob);
2368 __x.param(typename discrete_distribution<_IntType>::
2369 param_type(__prob_vec.begin(), __prob_vec.end()));
2371 __is.flags(__flags);
2376 template<typename _RealType>
2378 piecewise_constant_distribution<_RealType>::param_type::
2381 if (_M_int.size() < 2
2382 || (_M_int.size() == 2
2383 && _M_int[0] == _RealType(0)
2384 && _M_int[1] == _RealType(1)))
2391 const double __sum = std::accumulate(_M_den.begin(),
2394 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2395 std::bind2nd(std::divides<double>(), __sum));
2397 _M_cp.reserve(_M_den.size());
2398 std::partial_sum(_M_den.begin(), _M_den.end(),
2399 std::back_inserter(_M_cp));
2401 // Make sure the last cumulative probability is one.
2402 _M_cp[_M_cp.size() - 1] = 1.0;
2404 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2405 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2408 template<typename _RealType>
2409 template<typename _InputIteratorB, typename _InputIteratorW>
2410 piecewise_constant_distribution<_RealType>::param_type::
2411 param_type(_InputIteratorB __bbegin,
2412 _InputIteratorB __bend,
2413 _InputIteratorW __wbegin)
2414 : _M_int(), _M_den(), _M_cp()
2416 if (__bbegin != __bend)
2420 _M_int.push_back(*__bbegin);
2422 if (__bbegin == __bend)
2425 _M_den.push_back(*__wbegin);
2433 template<typename _RealType>
2434 template<typename _Func>
2435 piecewise_constant_distribution<_RealType>::param_type::
2436 param_type(initializer_list<_RealType> __bl, _Func __fw)
2437 : _M_int(), _M_den(), _M_cp()
2439 _M_int.reserve(__bl.size());
2440 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2441 _M_int.push_back(*__biter);
2443 _M_den.reserve(_M_int.size() - 1);
2444 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2445 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2450 template<typename _RealType>
2451 template<typename _Func>
2452 piecewise_constant_distribution<_RealType>::param_type::
2453 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2454 : _M_int(), _M_den(), _M_cp()
2456 const size_t __n = __nw == 0 ? 1 : __nw;
2457 const _RealType __delta = (__xmax - __xmin) / __n;
2459 _M_int.reserve(__n + 1);
2460 for (size_t __k = 0; __k <= __nw; ++__k)
2461 _M_int.push_back(__xmin + __k * __delta);
2463 _M_den.reserve(__n);
2464 for (size_t __k = 0; __k < __nw; ++__k)
2465 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2470 template<typename _RealType>
2471 template<typename _UniformRandomNumberGenerator>
2472 typename piecewise_constant_distribution<_RealType>::result_type
2473 piecewise_constant_distribution<_RealType>::
2474 operator()(_UniformRandomNumberGenerator& __urng,
2475 const param_type& __param)
2477 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2480 const double __p = __aurng();
2481 if (__param._M_cp.empty())
2484 auto __pos = std::lower_bound(__param._M_cp.begin(),
2485 __param._M_cp.end(), __p);
2486 const size_t __i = __pos - __param._M_cp.begin();
2488 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2490 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2493 template<typename _RealType, typename _CharT, typename _Traits>
2494 std::basic_ostream<_CharT, _Traits>&
2495 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2496 const piecewise_constant_distribution<_RealType>& __x)
2498 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2499 typedef typename __ostream_type::ios_base __ios_base;
2501 const typename __ios_base::fmtflags __flags = __os.flags();
2502 const _CharT __fill = __os.fill();
2503 const std::streamsize __precision = __os.precision();
2504 const _CharT __space = __os.widen(' ');
2505 __os.flags(__ios_base::scientific | __ios_base::left);
2507 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2509 std::vector<_RealType> __int = __x.intervals();
2510 __os << __int.size() - 1;
2512 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2513 __os << __space << *__xit;
2515 std::vector<double> __den = __x.densities();
2516 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2517 __os << __space << *__dit;
2519 __os.flags(__flags);
2521 __os.precision(__precision);
2525 template<typename _RealType, typename _CharT, typename _Traits>
2526 std::basic_istream<_CharT, _Traits>&
2527 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2528 piecewise_constant_distribution<_RealType>& __x)
2530 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2531 typedef typename __istream_type::ios_base __ios_base;
2533 const typename __ios_base::fmtflags __flags = __is.flags();
2534 __is.flags(__ios_base::dec | __ios_base::skipws);
2539 std::vector<_RealType> __int_vec;
2540 __int_vec.reserve(__n + 1);
2541 for (size_t __i = 0; __i <= __n; ++__i)
2545 __int_vec.push_back(__int);
2548 std::vector<double> __den_vec;
2549 __den_vec.reserve(__n);
2550 for (size_t __i = 0; __i < __n; ++__i)
2554 __den_vec.push_back(__den);
2557 __x.param(typename piecewise_constant_distribution<_RealType>::
2558 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2560 __is.flags(__flags);
2565 template<typename _RealType>
2567 piecewise_linear_distribution<_RealType>::param_type::
2570 if (_M_int.size() < 2
2571 || (_M_int.size() == 2
2572 && _M_int[0] == _RealType(0)
2573 && _M_int[1] == _RealType(1)
2574 && _M_den[0] == _M_den[1]))
2582 _M_cp.reserve(_M_int.size() - 1);
2583 _M_m.reserve(_M_int.size() - 1);
2584 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2586 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2587 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2588 _M_cp.push_back(__sum);
2589 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2592 // Now normalize the densities...
2593 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2594 std::bind2nd(std::divides<double>(), __sum));
2595 // ... and partial sums...
2596 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2597 std::bind2nd(std::divides<double>(), __sum));
2599 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2600 std::bind2nd(std::divides<double>(), __sum));
2601 // Make sure the last cumulative probablility is one.
2602 _M_cp[_M_cp.size() - 1] = 1.0;
2605 template<typename _RealType>
2606 template<typename _InputIteratorB, typename _InputIteratorW>
2607 piecewise_linear_distribution<_RealType>::param_type::
2608 param_type(_InputIteratorB __bbegin,
2609 _InputIteratorB __bend,
2610 _InputIteratorW __wbegin)
2611 : _M_int(), _M_den(), _M_cp(), _M_m()
2613 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2615 _M_int.push_back(*__bbegin);
2616 _M_den.push_back(*__wbegin);
2622 template<typename _RealType>
2623 template<typename _Func>
2624 piecewise_linear_distribution<_RealType>::param_type::
2625 param_type(initializer_list<_RealType> __bl, _Func __fw)
2626 : _M_int(), _M_den(), _M_cp(), _M_m()
2628 _M_int.reserve(__bl.size());
2629 _M_den.reserve(__bl.size());
2630 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2632 _M_int.push_back(*__biter);
2633 _M_den.push_back(__fw(*__biter));
2639 template<typename _RealType>
2640 template<typename _Func>
2641 piecewise_linear_distribution<_RealType>::param_type::
2642 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2643 : _M_int(), _M_den(), _M_cp(), _M_m()
2645 const size_t __n = __nw == 0 ? 1 : __nw;
2646 const _RealType __delta = (__xmax - __xmin) / __n;
2648 _M_int.reserve(__n + 1);
2649 _M_den.reserve(__n + 1);
2650 for (size_t __k = 0; __k <= __nw; ++__k)
2652 _M_int.push_back(__xmin + __k * __delta);
2653 _M_den.push_back(__fw(_M_int[__k] + __delta));
2659 template<typename _RealType>
2660 template<typename _UniformRandomNumberGenerator>
2661 typename piecewise_linear_distribution<_RealType>::result_type
2662 piecewise_linear_distribution<_RealType>::
2663 operator()(_UniformRandomNumberGenerator& __urng,
2664 const param_type& __param)
2666 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2669 const double __p = __aurng();
2670 if (__param._M_cp.empty())
2673 auto __pos = std::lower_bound(__param._M_cp.begin(),
2674 __param._M_cp.end(), __p);
2675 const size_t __i = __pos - __param._M_cp.begin();
2677 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2679 const double __a = 0.5 * __param._M_m[__i];
2680 const double __b = __param._M_den[__i];
2681 const double __cm = __p - __pref;
2683 _RealType __x = __param._M_int[__i];
2688 const double __d = __b * __b + 4.0 * __a * __cm;
2689 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2695 template<typename _RealType, typename _CharT, typename _Traits>
2696 std::basic_ostream<_CharT, _Traits>&
2697 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2698 const piecewise_linear_distribution<_RealType>& __x)
2700 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2701 typedef typename __ostream_type::ios_base __ios_base;
2703 const typename __ios_base::fmtflags __flags = __os.flags();
2704 const _CharT __fill = __os.fill();
2705 const std::streamsize __precision = __os.precision();
2706 const _CharT __space = __os.widen(' ');
2707 __os.flags(__ios_base::scientific | __ios_base::left);
2709 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2711 std::vector<_RealType> __int = __x.intervals();
2712 __os << __int.size() - 1;
2714 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2715 __os << __space << *__xit;
2717 std::vector<double> __den = __x.densities();
2718 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2719 __os << __space << *__dit;
2721 __os.flags(__flags);
2723 __os.precision(__precision);
2727 template<typename _RealType, typename _CharT, typename _Traits>
2728 std::basic_istream<_CharT, _Traits>&
2729 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2730 piecewise_linear_distribution<_RealType>& __x)
2732 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2733 typedef typename __istream_type::ios_base __ios_base;
2735 const typename __ios_base::fmtflags __flags = __is.flags();
2736 __is.flags(__ios_base::dec | __ios_base::skipws);
2741 std::vector<_RealType> __int_vec;
2742 __int_vec.reserve(__n + 1);
2743 for (size_t __i = 0; __i <= __n; ++__i)
2747 __int_vec.push_back(__int);
2750 std::vector<double> __den_vec;
2751 __den_vec.reserve(__n + 1);
2752 for (size_t __i = 0; __i <= __n; ++__i)
2756 __den_vec.push_back(__den);
2759 __x.param(typename piecewise_linear_distribution<_RealType>::
2760 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2762 __is.flags(__flags);
2767 template<typename _IntType>
2768 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2770 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2771 _M_v.push_back(__detail::__mod<result_type,
2772 __detail::_Shift<result_type, 32>::__value>(*__iter));
2775 template<typename _InputIterator>
2776 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2778 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2779 _M_v.push_back(__detail::__mod<result_type,
2780 __detail::_Shift<result_type, 32>::__value>(*__iter));
2783 template<typename _RandomAccessIterator>
2785 seed_seq::generate(_RandomAccessIterator __begin,
2786 _RandomAccessIterator __end)
2788 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2791 if (__begin == __end)
2794 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
2796 const size_t __n = __end - __begin;
2797 const size_t __s = _M_v.size();
2798 const size_t __t = (__n >= 623) ? 11
2803 const size_t __p = (__n - __t) / 2;
2804 const size_t __q = __p + __t;
2805 const size_t __m = std::max(size_t(__s + 1), __n);
2807 for (size_t __k = 0; __k < __m; ++__k)
2809 _Type __arg = (__begin[__k % __n]
2810 ^ __begin[(__k + __p) % __n]
2811 ^ __begin[(__k - 1) % __n]);
2812 _Type __r1 = __arg ^ (__arg >> 27);
2813 __r1 = __detail::__mod<_Type,
2814 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
2818 else if (__k <= __s)
2819 __r2 += __k % __n + _M_v[__k - 1];
2822 __r2 = __detail::__mod<_Type,
2823 __detail::_Shift<_Type, 32>::__value>(__r2);
2824 __begin[(__k + __p) % __n] += __r1;
2825 __begin[(__k + __q) % __n] += __r2;
2826 __begin[__k % __n] = __r2;
2829 for (size_t __k = __m; __k < __m + __n; ++__k)
2831 _Type __arg = (__begin[__k % __n]
2832 + __begin[(__k + __p) % __n]
2833 + __begin[(__k - 1) % __n]);
2834 _Type __r3 = __arg ^ (__arg >> 27);
2835 __r3 = __detail::__mod<_Type,
2836 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
2837 _Type __r4 = __r3 - __k % __n;
2838 __r4 = __detail::__mod<_Type,
2839 __detail::_Shift<_Type, 32>::__value>(__r4);
2840 __begin[(__k + __p) % __n] ^= __r3;
2841 __begin[(__k + __q) % __n] ^= __r4;
2842 __begin[__k % __n] = __r4;
2846 template<typename _RealType, size_t __bits,
2847 typename _UniformRandomNumberGenerator>
2849 generate_canonical(_UniformRandomNumberGenerator& __urng)
2852 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2854 const long double __r = static_cast<long double>(__urng.max())
2855 - static_cast<long double>(__urng.min()) + 1.0L;
2856 const size_t __log2r = std::log(__r) / std::log(2.0L);
2857 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2858 _RealType __sum = _RealType(0);
2859 _RealType __tmp = _RealType(1);
2860 for (; __k != 0; --__k)
2862 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2865 return __sum / __tmp;
2868 _GLIBCXX_END_NAMESPACE_VERSION