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();
940 template<typename _IntType>
941 template<typename _ForwardIterator,
942 typename _UniformRandomNumberGenerator>
944 uniform_int_distribution<_IntType>::
945 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
946 _UniformRandomNumberGenerator& __urng,
947 const param_type& __param)
949 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
950 typedef typename _UniformRandomNumberGenerator::result_type
952 typedef typename std::make_unsigned<result_type>::type __utype;
953 typedef typename std::common_type<_Gresult_type, __utype>::type
956 const __uctype __urngmin = __urng.min();
957 const __uctype __urngmax = __urng.max();
958 const __uctype __urngrange = __urngmax - __urngmin;
959 const __uctype __urange
960 = __uctype(__param.b()) - __uctype(__param.a());
964 if (__urngrange > __urange)
966 if (__detail::_Power_of_2(__urngrange + 1)
967 && __detail::_Power_of_2(__urange + 1))
971 __ret = __uctype(__urng()) - __urngmin;
972 *__f++ = (__ret & __urange) + __param.a();
978 const __uctype __uerange = __urange + 1; // __urange can be zero
979 const __uctype __scaling = __urngrange / __uerange;
980 const __uctype __past = __uerange * __scaling;
984 __ret = __uctype(__urng()) - __urngmin;
985 while (__ret >= __past);
986 *__f++ = __ret / __scaling + __param.a();
990 else if (__urngrange < __urange)
994 Note that every value in [0, urange]
995 can be written uniquely as
997 (urngrange + 1) * high + low
1001 high in [0, urange / (urngrange + 1)]
1005 low in [0, urngrange].
1007 __uctype __tmp; // wraparound control
1012 const __uctype __uerngrange = __urngrange + 1;
1013 __tmp = (__uerngrange * operator()
1014 (__urng, param_type(0, __urange / __uerngrange)));
1015 __ret = __tmp + (__uctype(__urng()) - __urngmin);
1017 while (__ret > __urange || __ret < __tmp);
1023 *__f++ = __uctype(__urng()) - __urngmin + __param.a();
1026 template<typename _IntType, typename _CharT, typename _Traits>
1027 std::basic_ostream<_CharT, _Traits>&
1028 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1029 const uniform_int_distribution<_IntType>& __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 _CharT __space = __os.widen(' ');
1037 __os.flags(__ios_base::scientific | __ios_base::left);
1040 __os << __x.a() << __space << __x.b();
1042 __os.flags(__flags);
1047 template<typename _IntType, typename _CharT, typename _Traits>
1048 std::basic_istream<_CharT, _Traits>&
1049 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1050 uniform_int_distribution<_IntType>& __x)
1052 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1053 typedef typename __istream_type::ios_base __ios_base;
1055 const typename __ios_base::fmtflags __flags = __is.flags();
1056 __is.flags(__ios_base::dec | __ios_base::skipws);
1060 __x.param(typename uniform_int_distribution<_IntType>::
1061 param_type(__a, __b));
1063 __is.flags(__flags);
1068 template<typename _RealType>
1069 template<typename _ForwardIterator,
1070 typename _UniformRandomNumberGenerator>
1072 uniform_real_distribution<_RealType>::
1073 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1074 _UniformRandomNumberGenerator& __urng,
1075 const param_type& __p)
1077 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1078 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1080 auto __range = __p.b() - __p.a();
1082 *__f++ = __aurng() * __range + __p.a();
1085 template<typename _RealType, typename _CharT, typename _Traits>
1086 std::basic_ostream<_CharT, _Traits>&
1087 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1088 const uniform_real_distribution<_RealType>& __x)
1090 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1091 typedef typename __ostream_type::ios_base __ios_base;
1093 const typename __ios_base::fmtflags __flags = __os.flags();
1094 const _CharT __fill = __os.fill();
1095 const std::streamsize __precision = __os.precision();
1096 const _CharT __space = __os.widen(' ');
1097 __os.flags(__ios_base::scientific | __ios_base::left);
1099 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1101 __os << __x.a() << __space << __x.b();
1103 __os.flags(__flags);
1105 __os.precision(__precision);
1109 template<typename _RealType, typename _CharT, typename _Traits>
1110 std::basic_istream<_CharT, _Traits>&
1111 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1112 uniform_real_distribution<_RealType>& __x)
1114 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1115 typedef typename __istream_type::ios_base __ios_base;
1117 const typename __ios_base::fmtflags __flags = __is.flags();
1118 __is.flags(__ios_base::skipws);
1122 __x.param(typename uniform_real_distribution<_RealType>::
1123 param_type(__a, __b));
1125 __is.flags(__flags);
1130 template<typename _ForwardIterator,
1131 typename _UniformRandomNumberGenerator>
1133 std::bernoulli_distribution::
1134 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1135 _UniformRandomNumberGenerator& __urng,
1136 const param_type& __p)
1138 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1139 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1141 auto __limit = __p.p() * (__aurng.max() - __aurng.min());
1144 *__f++ = (__aurng() - __aurng.min()) < __limit;
1147 template<typename _CharT, typename _Traits>
1148 std::basic_ostream<_CharT, _Traits>&
1149 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1150 const bernoulli_distribution& __x)
1152 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1153 typedef typename __ostream_type::ios_base __ios_base;
1155 const typename __ios_base::fmtflags __flags = __os.flags();
1156 const _CharT __fill = __os.fill();
1157 const std::streamsize __precision = __os.precision();
1158 __os.flags(__ios_base::scientific | __ios_base::left);
1159 __os.fill(__os.widen(' '));
1160 __os.precision(std::numeric_limits<double>::max_digits10);
1164 __os.flags(__flags);
1166 __os.precision(__precision);
1171 template<typename _IntType>
1172 template<typename _UniformRandomNumberGenerator>
1173 typename geometric_distribution<_IntType>::result_type
1174 geometric_distribution<_IntType>::
1175 operator()(_UniformRandomNumberGenerator& __urng,
1176 const param_type& __param)
1178 // About the epsilon thing see this thread:
1179 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1180 const double __naf =
1181 (1 - std::numeric_limits<double>::epsilon()) / 2;
1182 // The largest _RealType convertible to _IntType.
1183 const double __thr =
1184 std::numeric_limits<_IntType>::max() + __naf;
1185 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1190 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1191 while (__cand >= __thr);
1193 return result_type(__cand + __naf);
1196 template<typename _IntType>
1197 template<typename _ForwardIterator,
1198 typename _UniformRandomNumberGenerator>
1200 geometric_distribution<_IntType>::
1201 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1202 _UniformRandomNumberGenerator& __urng,
1203 const param_type& __param)
1205 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1206 // About the epsilon thing see this thread:
1207 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1208 const double __naf =
1209 (1 - std::numeric_limits<double>::epsilon()) / 2;
1210 // The largest _RealType convertible to _IntType.
1211 const double __thr =
1212 std::numeric_limits<_IntType>::max() + __naf;
1213 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1220 __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
1221 while (__cand >= __thr);
1223 *__f++ = __cand + __naf;
1227 template<typename _IntType,
1228 typename _CharT, typename _Traits>
1229 std::basic_ostream<_CharT, _Traits>&
1230 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1231 const geometric_distribution<_IntType>& __x)
1233 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1234 typedef typename __ostream_type::ios_base __ios_base;
1236 const typename __ios_base::fmtflags __flags = __os.flags();
1237 const _CharT __fill = __os.fill();
1238 const std::streamsize __precision = __os.precision();
1239 __os.flags(__ios_base::scientific | __ios_base::left);
1240 __os.fill(__os.widen(' '));
1241 __os.precision(std::numeric_limits<double>::max_digits10);
1245 __os.flags(__flags);
1247 __os.precision(__precision);
1251 template<typename _IntType,
1252 typename _CharT, typename _Traits>
1253 std::basic_istream<_CharT, _Traits>&
1254 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1255 geometric_distribution<_IntType>& __x)
1257 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1258 typedef typename __istream_type::ios_base __ios_base;
1260 const typename __ios_base::fmtflags __flags = __is.flags();
1261 __is.flags(__ios_base::skipws);
1265 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1267 __is.flags(__flags);
1271 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1272 template<typename _IntType>
1273 template<typename _UniformRandomNumberGenerator>
1274 typename negative_binomial_distribution<_IntType>::result_type
1275 negative_binomial_distribution<_IntType>::
1276 operator()(_UniformRandomNumberGenerator& __urng)
1278 const double __y = _M_gd(__urng);
1280 // XXX Is the constructor too slow?
1281 std::poisson_distribution<result_type> __poisson(__y);
1282 return __poisson(__urng);
1285 template<typename _IntType>
1286 template<typename _UniformRandomNumberGenerator>
1287 typename negative_binomial_distribution<_IntType>::result_type
1288 negative_binomial_distribution<_IntType>::
1289 operator()(_UniformRandomNumberGenerator& __urng,
1290 const param_type& __p)
1292 typedef typename std::gamma_distribution<result_type>::param_type
1296 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1298 std::poisson_distribution<result_type> __poisson(__y);
1299 return __poisson(__urng);
1302 template<typename _IntType>
1303 template<typename _ForwardIterator,
1304 typename _UniformRandomNumberGenerator>
1306 negative_binomial_distribution<_IntType>::
1307 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1308 _UniformRandomNumberGenerator& __urng)
1310 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1313 const double __y = _M_gd(__urng);
1315 // XXX Is the constructor too slow?
1316 std::poisson_distribution<result_type> __poisson(__y);
1317 *__f++ = __poisson(__urng);
1321 template<typename _IntType>
1322 template<typename _ForwardIterator,
1323 typename _UniformRandomNumberGenerator>
1325 negative_binomial_distribution<_IntType>::
1326 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1327 _UniformRandomNumberGenerator& __urng,
1328 const param_type& __p)
1330 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1331 typename std::gamma_distribution<result_type>::param_type
1332 __p2(__p.k(), (1.0 - __p.p()) / __p.p());
1336 const double __y = _M_gd(__urng, __p2);
1338 std::poisson_distribution<result_type> __poisson(__y);
1339 *__f++ = __poisson(__urng);
1343 template<typename _IntType, typename _CharT, typename _Traits>
1344 std::basic_ostream<_CharT, _Traits>&
1345 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1346 const negative_binomial_distribution<_IntType>& __x)
1348 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1349 typedef typename __ostream_type::ios_base __ios_base;
1351 const typename __ios_base::fmtflags __flags = __os.flags();
1352 const _CharT __fill = __os.fill();
1353 const std::streamsize __precision = __os.precision();
1354 const _CharT __space = __os.widen(' ');
1355 __os.flags(__ios_base::scientific | __ios_base::left);
1356 __os.fill(__os.widen(' '));
1357 __os.precision(std::numeric_limits<double>::max_digits10);
1359 __os << __x.k() << __space << __x.p()
1360 << __space << __x._M_gd;
1362 __os.flags(__flags);
1364 __os.precision(__precision);
1368 template<typename _IntType, typename _CharT, typename _Traits>
1369 std::basic_istream<_CharT, _Traits>&
1370 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1371 negative_binomial_distribution<_IntType>& __x)
1373 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1374 typedef typename __istream_type::ios_base __ios_base;
1376 const typename __ios_base::fmtflags __flags = __is.flags();
1377 __is.flags(__ios_base::skipws);
1381 __is >> __k >> __p >> __x._M_gd;
1382 __x.param(typename negative_binomial_distribution<_IntType>::
1383 param_type(__k, __p));
1385 __is.flags(__flags);
1390 template<typename _IntType>
1392 poisson_distribution<_IntType>::param_type::
1395 #if _GLIBCXX_USE_C99_MATH_TR1
1398 const double __m = std::floor(_M_mean);
1399 _M_lm_thr = std::log(_M_mean);
1400 _M_lfm = std::lgamma(__m + 1);
1401 _M_sm = std::sqrt(__m);
1403 const double __pi_4 = 0.7853981633974483096156608458198757L;
1404 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1406 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
1407 const double __cx = 2 * __m + _M_d;
1408 _M_scx = std::sqrt(__cx / 2);
1411 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1412 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1417 _M_lm_thr = std::exp(-_M_mean);
1421 * A rejection algorithm when mean >= 12 and a simple method based
1422 * upon the multiplication of uniform random variates otherwise.
1423 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1427 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1428 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1430 template<typename _IntType>
1431 template<typename _UniformRandomNumberGenerator>
1432 typename poisson_distribution<_IntType>::result_type
1433 poisson_distribution<_IntType>::
1434 operator()(_UniformRandomNumberGenerator& __urng,
1435 const param_type& __param)
1437 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1439 #if _GLIBCXX_USE_C99_MATH_TR1
1440 if (__param.mean() >= 12)
1444 // See comments above...
1445 const double __naf =
1446 (1 - std::numeric_limits<double>::epsilon()) / 2;
1447 const double __thr =
1448 std::numeric_limits<_IntType>::max() + __naf;
1450 const double __m = std::floor(__param.mean());
1452 const double __spi_2 = 1.2533141373155002512078826424055226L;
1453 const double __c1 = __param._M_sm * __spi_2;
1454 const double __c2 = __param._M_c2b + __c1;
1455 const double __c3 = __c2 + 1;
1456 const double __c4 = __c3 + 1;
1458 const double __e178 = 1.0129030479320018583185514777512983L;
1459 const double __c5 = __c4 + __e178;
1460 const double __c = __param._M_cb + __c5;
1461 const double __2cx = 2 * (2 * __m + __param._M_d);
1463 bool __reject = true;
1466 const double __u = __c * __aurng();
1467 const double __e = -std::log(__aurng());
1473 const double __n = _M_nd(__urng);
1474 const double __y = -std::abs(__n) * __param._M_sm - 1;
1475 __x = std::floor(__y);
1476 __w = -__n * __n / 2;
1480 else if (__u <= __c2)
1482 const double __n = _M_nd(__urng);
1483 const double __y = 1 + std::abs(__n) * __param._M_scx;
1484 __x = std::ceil(__y);
1485 __w = __y * (2 - __y) * __param._M_1cx;
1486 if (__x > __param._M_d)
1489 else if (__u <= __c3)
1490 // NB: This case not in the book, nor in the Errata,
1491 // but should be ok...
1493 else if (__u <= __c4)
1495 else if (__u <= __c5)
1499 const double __v = -std::log(__aurng());
1500 const double __y = __param._M_d
1501 + __v * __2cx / __param._M_d;
1502 __x = std::ceil(__y);
1503 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1506 __reject = (__w - __e - __x * __param._M_lm_thr
1507 > __param._M_lfm - std::lgamma(__x + __m + 1));
1509 __reject |= __x + __m >= __thr;
1513 return result_type(__x + __m + __naf);
1519 double __prod = 1.0;
1523 __prod *= __aurng();
1526 while (__prod > __param._M_lm_thr);
1532 template<typename _IntType>
1533 template<typename _ForwardIterator,
1534 typename _UniformRandomNumberGenerator>
1536 poisson_distribution<_IntType>::
1537 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1538 _UniformRandomNumberGenerator& __urng,
1539 const param_type& __param)
1541 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1542 // We could duplicate everything from operator()...
1544 *__f++ = this->operator()(__urng, __param);
1547 template<typename _IntType,
1548 typename _CharT, typename _Traits>
1549 std::basic_ostream<_CharT, _Traits>&
1550 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1551 const poisson_distribution<_IntType>& __x)
1553 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1554 typedef typename __ostream_type::ios_base __ios_base;
1556 const typename __ios_base::fmtflags __flags = __os.flags();
1557 const _CharT __fill = __os.fill();
1558 const std::streamsize __precision = __os.precision();
1559 const _CharT __space = __os.widen(' ');
1560 __os.flags(__ios_base::scientific | __ios_base::left);
1562 __os.precision(std::numeric_limits<double>::max_digits10);
1564 __os << __x.mean() << __space << __x._M_nd;
1566 __os.flags(__flags);
1568 __os.precision(__precision);
1572 template<typename _IntType,
1573 typename _CharT, typename _Traits>
1574 std::basic_istream<_CharT, _Traits>&
1575 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1576 poisson_distribution<_IntType>& __x)
1578 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1579 typedef typename __istream_type::ios_base __ios_base;
1581 const typename __ios_base::fmtflags __flags = __is.flags();
1582 __is.flags(__ios_base::skipws);
1585 __is >> __mean >> __x._M_nd;
1586 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1588 __is.flags(__flags);
1593 template<typename _IntType>
1595 binomial_distribution<_IntType>::param_type::
1598 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1602 #if _GLIBCXX_USE_C99_MATH_TR1
1603 if (_M_t * __p12 >= 8)
1606 const double __np = std::floor(_M_t * __p12);
1607 const double __pa = __np / _M_t;
1608 const double __1p = 1 - __pa;
1610 const double __pi_4 = 0.7853981633974483096156608458198757L;
1611 const double __d1x =
1612 std::sqrt(__np * __1p * std::log(32 * __np
1613 / (81 * __pi_4 * __1p)));
1614 _M_d1 = std::round(std::max(1.0, __d1x));
1615 const double __d2x =
1616 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1617 / (__pi_4 * __pa)));
1618 _M_d2 = std::round(std::max(1.0, __d2x));
1621 const double __spi_2 = 1.2533141373155002512078826424055226L;
1622 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1623 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1624 _M_c = 2 * _M_d1 / __np;
1625 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1626 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1627 const double __s1s = _M_s1 * _M_s1;
1628 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1630 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1631 const double __s2s = _M_s2 * _M_s2;
1632 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1633 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1634 _M_lf = (std::lgamma(__np + 1)
1635 + std::lgamma(_M_t - __np + 1));
1636 _M_lp1p = std::log(__pa / __1p);
1638 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1642 _M_q = -std::log(1 - __p12);
1645 template<typename _IntType>
1646 template<typename _UniformRandomNumberGenerator>
1647 typename binomial_distribution<_IntType>::result_type
1648 binomial_distribution<_IntType>::
1649 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1653 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1658 const double __e = -std::log(__aurng());
1659 __sum += __e / (__t - __x);
1662 while (__sum <= _M_param._M_q);
1668 * A rejection algorithm when t * p >= 8 and a simple waiting time
1669 * method - the second in the referenced book - otherwise.
1670 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1674 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1675 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1677 template<typename _IntType>
1678 template<typename _UniformRandomNumberGenerator>
1679 typename binomial_distribution<_IntType>::result_type
1680 binomial_distribution<_IntType>::
1681 operator()(_UniformRandomNumberGenerator& __urng,
1682 const param_type& __param)
1685 const _IntType __t = __param.t();
1686 const double __p = __param.p();
1687 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1688 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1691 #if _GLIBCXX_USE_C99_MATH_TR1
1692 if (!__param._M_easy)
1696 // See comments above...
1697 const double __naf =
1698 (1 - std::numeric_limits<double>::epsilon()) / 2;
1699 const double __thr =
1700 std::numeric_limits<_IntType>::max() + __naf;
1702 const double __np = std::floor(__t * __p12);
1705 const double __spi_2 = 1.2533141373155002512078826424055226L;
1706 const double __a1 = __param._M_a1;
1707 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1708 const double __a123 = __param._M_a123;
1709 const double __s1s = __param._M_s1 * __param._M_s1;
1710 const double __s2s = __param._M_s2 * __param._M_s2;
1715 const double __u = __param._M_s * __aurng();
1721 const double __n = _M_nd(__urng);
1722 const double __y = __param._M_s1 * std::abs(__n);
1723 __reject = __y >= __param._M_d1;
1726 const double __e = -std::log(__aurng());
1727 __x = std::floor(__y);
1728 __v = -__e - __n * __n / 2 + __param._M_c;
1731 else if (__u <= __a12)
1733 const double __n = _M_nd(__urng);
1734 const double __y = __param._M_s2 * std::abs(__n);
1735 __reject = __y >= __param._M_d2;
1738 const double __e = -std::log(__aurng());
1739 __x = std::floor(-__y);
1740 __v = -__e - __n * __n / 2;
1743 else if (__u <= __a123)
1745 const double __e1 = -std::log(__aurng());
1746 const double __e2 = -std::log(__aurng());
1748 const double __y = __param._M_d1
1749 + 2 * __s1s * __e1 / __param._M_d1;
1750 __x = std::floor(__y);
1751 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1752 -__y / (2 * __s1s)));
1757 const double __e1 = -std::log(__aurng());
1758 const double __e2 = -std::log(__aurng());
1760 const double __y = __param._M_d2
1761 + 2 * __s2s * __e1 / __param._M_d2;
1762 __x = std::floor(-__y);
1763 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1767 __reject = __reject || __x < -__np || __x > __t - __np;
1770 const double __lfx =
1771 std::lgamma(__np + __x + 1)
1772 + std::lgamma(__t - (__np + __x) + 1);
1773 __reject = __v > __param._M_lf - __lfx
1774 + __x * __param._M_lp1p;
1777 __reject |= __x + __np >= __thr;
1781 __x += __np + __naf;
1783 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1784 __ret = _IntType(__x) + __z;
1788 __ret = _M_waiting(__urng, __t);
1791 __ret = __t - __ret;
1795 template<typename _IntType>
1796 template<typename _ForwardIterator,
1797 typename _UniformRandomNumberGenerator>
1799 binomial_distribution<_IntType>::
1800 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1801 _UniformRandomNumberGenerator& __urng,
1802 const param_type& __param)
1804 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1805 // We could duplicate everything from operator()...
1807 *__f++ = this->operator()(__urng, __param);
1810 template<typename _IntType,
1811 typename _CharT, typename _Traits>
1812 std::basic_ostream<_CharT, _Traits>&
1813 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1814 const binomial_distribution<_IntType>& __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<double>::max_digits10);
1827 __os << __x.t() << __space << __x.p()
1828 << __space << __x._M_nd;
1830 __os.flags(__flags);
1832 __os.precision(__precision);
1836 template<typename _IntType,
1837 typename _CharT, typename _Traits>
1838 std::basic_istream<_CharT, _Traits>&
1839 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1840 binomial_distribution<_IntType>& __x)
1842 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1843 typedef typename __istream_type::ios_base __ios_base;
1845 const typename __ios_base::fmtflags __flags = __is.flags();
1846 __is.flags(__ios_base::dec | __ios_base::skipws);
1850 __is >> __t >> __p >> __x._M_nd;
1851 __x.param(typename binomial_distribution<_IntType>::
1852 param_type(__t, __p));
1854 __is.flags(__flags);
1859 template<typename _RealType>
1860 template<typename _ForwardIterator,
1861 typename _UniformRandomNumberGenerator>
1863 std::exponential_distribution<_RealType>::
1864 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1865 _UniformRandomNumberGenerator& __urng,
1866 const param_type& __p)
1868 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1869 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1872 *__f++ = -std::log(__aurng()) / __p.lambda();
1875 template<typename _RealType, typename _CharT, typename _Traits>
1876 std::basic_ostream<_CharT, _Traits>&
1877 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1878 const exponential_distribution<_RealType>& __x)
1880 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1881 typedef typename __ostream_type::ios_base __ios_base;
1883 const typename __ios_base::fmtflags __flags = __os.flags();
1884 const _CharT __fill = __os.fill();
1885 const std::streamsize __precision = __os.precision();
1886 __os.flags(__ios_base::scientific | __ios_base::left);
1887 __os.fill(__os.widen(' '));
1888 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1890 __os << __x.lambda();
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 exponential_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 exponential_distribution<_RealType>::
1912 param_type(__lambda));
1914 __is.flags(__flags);
1920 * Polar method due to Marsaglia.
1922 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1923 * New York, 1986, Ch. V, Sect. 4.4.
1925 template<typename _RealType>
1926 template<typename _UniformRandomNumberGenerator>
1927 typename normal_distribution<_RealType>::result_type
1928 normal_distribution<_RealType>::
1929 operator()(_UniformRandomNumberGenerator& __urng,
1930 const param_type& __param)
1933 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1936 if (_M_saved_available)
1938 _M_saved_available = false;
1943 result_type __x, __y, __r2;
1946 __x = result_type(2.0) * __aurng() - 1.0;
1947 __y = result_type(2.0) * __aurng() - 1.0;
1948 __r2 = __x * __x + __y * __y;
1950 while (__r2 > 1.0 || __r2 == 0.0);
1952 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1953 _M_saved = __x * __mult;
1954 _M_saved_available = true;
1955 __ret = __y * __mult;
1958 __ret = __ret * __param.stddev() + __param.mean();
1962 template<typename _RealType>
1963 template<typename _ForwardIterator,
1964 typename _UniformRandomNumberGenerator>
1966 normal_distribution<_RealType>::
1967 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1968 _UniformRandomNumberGenerator& __urng,
1969 const param_type& __param)
1971 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1976 if (_M_saved_available)
1978 _M_saved_available = false;
1979 *__f++ = _M_saved * __param.stddev() + __param.mean();
1985 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1988 while (__f + 1 < __t)
1990 result_type __x, __y, __r2;
1993 __x = result_type(2.0) * __aurng() - 1.0;
1994 __y = result_type(2.0) * __aurng() - 1.0;
1995 __r2 = __x * __x + __y * __y;
1997 while (__r2 > 1.0 || __r2 == 0.0);
1999 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
2000 *__f++ = __y * __mult * __param.stddev() + __param.mean();
2001 *__f++ = __x * __mult * __param.stddev() + __param.mean();
2006 result_type __x, __y, __r2;
2009 __x = result_type(2.0) * __aurng() - 1.0;
2010 __y = result_type(2.0) * __aurng() - 1.0;
2011 __r2 = __x * __x + __y * __y;
2013 while (__r2 > 1.0 || __r2 == 0.0);
2015 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
2016 _M_saved = __x * __mult;
2017 _M_saved_available = true;
2018 *__f = __y * __mult * __param.stddev() + __param.mean();
2022 template<typename _RealType>
2024 operator==(const std::normal_distribution<_RealType>& __d1,
2025 const std::normal_distribution<_RealType>& __d2)
2027 if (__d1._M_param == __d2._M_param
2028 && __d1._M_saved_available == __d2._M_saved_available)
2030 if (__d1._M_saved_available
2031 && __d1._M_saved == __d2._M_saved)
2033 else if(!__d1._M_saved_available)
2042 template<typename _RealType, typename _CharT, typename _Traits>
2043 std::basic_ostream<_CharT, _Traits>&
2044 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2045 const normal_distribution<_RealType>& __x)
2047 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2048 typedef typename __ostream_type::ios_base __ios_base;
2050 const typename __ios_base::fmtflags __flags = __os.flags();
2051 const _CharT __fill = __os.fill();
2052 const std::streamsize __precision = __os.precision();
2053 const _CharT __space = __os.widen(' ');
2054 __os.flags(__ios_base::scientific | __ios_base::left);
2056 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2058 __os << __x.mean() << __space << __x.stddev()
2059 << __space << __x._M_saved_available;
2060 if (__x._M_saved_available)
2061 __os << __space << __x._M_saved;
2063 __os.flags(__flags);
2065 __os.precision(__precision);
2069 template<typename _RealType, typename _CharT, typename _Traits>
2070 std::basic_istream<_CharT, _Traits>&
2071 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2072 normal_distribution<_RealType>& __x)
2074 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2075 typedef typename __istream_type::ios_base __ios_base;
2077 const typename __ios_base::fmtflags __flags = __is.flags();
2078 __is.flags(__ios_base::dec | __ios_base::skipws);
2080 double __mean, __stddev;
2081 __is >> __mean >> __stddev
2082 >> __x._M_saved_available;
2083 if (__x._M_saved_available)
2084 __is >> __x._M_saved;
2085 __x.param(typename normal_distribution<_RealType>::
2086 param_type(__mean, __stddev));
2088 __is.flags(__flags);
2093 template<typename _RealType>
2094 template<typename _ForwardIterator,
2095 typename _UniformRandomNumberGenerator>
2097 lognormal_distribution<_RealType>::
2098 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2099 _UniformRandomNumberGenerator& __urng,
2100 const param_type& __p)
2102 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2104 *__f++ = std::exp(__p.s() * _M_nd(__urng) + __p.m());
2107 template<typename _RealType, typename _CharT, typename _Traits>
2108 std::basic_ostream<_CharT, _Traits>&
2109 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2110 const lognormal_distribution<_RealType>& __x)
2112 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2113 typedef typename __ostream_type::ios_base __ios_base;
2115 const typename __ios_base::fmtflags __flags = __os.flags();
2116 const _CharT __fill = __os.fill();
2117 const std::streamsize __precision = __os.precision();
2118 const _CharT __space = __os.widen(' ');
2119 __os.flags(__ios_base::scientific | __ios_base::left);
2121 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2123 __os << __x.m() << __space << __x.s()
2124 << __space << __x._M_nd;
2126 __os.flags(__flags);
2128 __os.precision(__precision);
2132 template<typename _RealType, typename _CharT, typename _Traits>
2133 std::basic_istream<_CharT, _Traits>&
2134 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2135 lognormal_distribution<_RealType>& __x)
2137 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2138 typedef typename __istream_type::ios_base __ios_base;
2140 const typename __ios_base::fmtflags __flags = __is.flags();
2141 __is.flags(__ios_base::dec | __ios_base::skipws);
2144 __is >> __m >> __s >> __x._M_nd;
2145 __x.param(typename lognormal_distribution<_RealType>::
2146 param_type(__m, __s));
2148 __is.flags(__flags);
2153 template<typename _RealType>
2154 template<typename _ForwardIterator,
2155 typename _UniformRandomNumberGenerator>
2157 std::chi_squared_distribution<_RealType>::
2158 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2159 _UniformRandomNumberGenerator& __urng,
2160 typename std::gamma_distribution<result_type>::param_type&
2163 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2165 *__f++ = 2 * _M_gd(__urng, __p);
2168 template<typename _RealType, typename _CharT, typename _Traits>
2169 std::basic_ostream<_CharT, _Traits>&
2170 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2171 const chi_squared_distribution<_RealType>& __x)
2173 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2174 typedef typename __ostream_type::ios_base __ios_base;
2176 const typename __ios_base::fmtflags __flags = __os.flags();
2177 const _CharT __fill = __os.fill();
2178 const std::streamsize __precision = __os.precision();
2179 const _CharT __space = __os.widen(' ');
2180 __os.flags(__ios_base::scientific | __ios_base::left);
2182 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2184 __os << __x.n() << __space << __x._M_gd;
2186 __os.flags(__flags);
2188 __os.precision(__precision);
2192 template<typename _RealType, typename _CharT, typename _Traits>
2193 std::basic_istream<_CharT, _Traits>&
2194 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2195 chi_squared_distribution<_RealType>& __x)
2197 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2198 typedef typename __istream_type::ios_base __ios_base;
2200 const typename __ios_base::fmtflags __flags = __is.flags();
2201 __is.flags(__ios_base::dec | __ios_base::skipws);
2204 __is >> __n >> __x._M_gd;
2205 __x.param(typename chi_squared_distribution<_RealType>::
2208 __is.flags(__flags);
2213 template<typename _RealType>
2214 template<typename _UniformRandomNumberGenerator>
2215 typename cauchy_distribution<_RealType>::result_type
2216 cauchy_distribution<_RealType>::
2217 operator()(_UniformRandomNumberGenerator& __urng,
2218 const param_type& __p)
2220 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2227 const _RealType __pi = 3.1415926535897932384626433832795029L;
2228 return __p.a() + __p.b() * std::tan(__pi * __u);
2231 template<typename _RealType>
2232 template<typename _ForwardIterator,
2233 typename _UniformRandomNumberGenerator>
2235 cauchy_distribution<_RealType>::
2236 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2237 _UniformRandomNumberGenerator& __urng,
2238 const param_type& __p)
2240 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2241 const _RealType __pi = 3.1415926535897932384626433832795029L;
2242 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2251 *__f++ = __p.a() + __p.b() * std::tan(__pi * __u);
2255 template<typename _RealType, typename _CharT, typename _Traits>
2256 std::basic_ostream<_CharT, _Traits>&
2257 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2258 const cauchy_distribution<_RealType>& __x)
2260 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2261 typedef typename __ostream_type::ios_base __ios_base;
2263 const typename __ios_base::fmtflags __flags = __os.flags();
2264 const _CharT __fill = __os.fill();
2265 const std::streamsize __precision = __os.precision();
2266 const _CharT __space = __os.widen(' ');
2267 __os.flags(__ios_base::scientific | __ios_base::left);
2269 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2271 __os << __x.a() << __space << __x.b();
2273 __os.flags(__flags);
2275 __os.precision(__precision);
2279 template<typename _RealType, typename _CharT, typename _Traits>
2280 std::basic_istream<_CharT, _Traits>&
2281 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2282 cauchy_distribution<_RealType>& __x)
2284 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2285 typedef typename __istream_type::ios_base __ios_base;
2287 const typename __ios_base::fmtflags __flags = __is.flags();
2288 __is.flags(__ios_base::dec | __ios_base::skipws);
2292 __x.param(typename cauchy_distribution<_RealType>::
2293 param_type(__a, __b));
2295 __is.flags(__flags);
2300 template<typename _RealType>
2301 template<typename _ForwardIterator,
2302 typename _UniformRandomNumberGenerator>
2304 std::fisher_f_distribution<_RealType>::
2305 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2306 _UniformRandomNumberGenerator& __urng)
2308 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2310 *__f++ = ((_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()));
2313 template<typename _RealType>
2314 template<typename _ForwardIterator,
2315 typename _UniformRandomNumberGenerator>
2317 std::fisher_f_distribution<_RealType>::
2318 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2319 _UniformRandomNumberGenerator& __urng,
2320 const param_type& __p)
2322 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2323 typedef typename std::gamma_distribution<result_type>::param_type
2325 param_type __p1(__p.m() / 2);
2326 param_type __p2(__p.n() / 2);
2328 *__f++ = ((_M_gd_x(__urng, __p1) * n())
2329 / (_M_gd_y(__urng, __p2) * m()));
2332 template<typename _RealType, typename _CharT, typename _Traits>
2333 std::basic_ostream<_CharT, _Traits>&
2334 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2335 const fisher_f_distribution<_RealType>& __x)
2337 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2338 typedef typename __ostream_type::ios_base __ios_base;
2340 const typename __ios_base::fmtflags __flags = __os.flags();
2341 const _CharT __fill = __os.fill();
2342 const std::streamsize __precision = __os.precision();
2343 const _CharT __space = __os.widen(' ');
2344 __os.flags(__ios_base::scientific | __ios_base::left);
2346 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2348 __os << __x.m() << __space << __x.n()
2349 << __space << __x._M_gd_x << __space << __x._M_gd_y;
2351 __os.flags(__flags);
2353 __os.precision(__precision);
2357 template<typename _RealType, typename _CharT, typename _Traits>
2358 std::basic_istream<_CharT, _Traits>&
2359 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2360 fisher_f_distribution<_RealType>& __x)
2362 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2363 typedef typename __istream_type::ios_base __ios_base;
2365 const typename __ios_base::fmtflags __flags = __is.flags();
2366 __is.flags(__ios_base::dec | __ios_base::skipws);
2369 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
2370 __x.param(typename fisher_f_distribution<_RealType>::
2371 param_type(__m, __n));
2373 __is.flags(__flags);
2378 template<typename _RealType>
2379 template<typename _ForwardIterator,
2380 typename _UniformRandomNumberGenerator>
2382 std::student_t_distribution<_RealType>::
2383 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2384 _UniformRandomNumberGenerator& __urng)
2386 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2388 *__f++ = _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng));
2391 template<typename _RealType>
2392 template<typename _ForwardIterator,
2393 typename _UniformRandomNumberGenerator>
2395 std::student_t_distribution<_RealType>::
2396 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2397 _UniformRandomNumberGenerator& __urng,
2398 const param_type& __p)
2400 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2401 typename std::gamma_distribution<result_type>::param_type
2402 __p2(__p.n() / 2, 2);
2404 *__f++ = _M_nd(__urng) * std::sqrt(__p.n() / _M_gd(__urng, __p2));
2407 template<typename _RealType, typename _CharT, typename _Traits>
2408 std::basic_ostream<_CharT, _Traits>&
2409 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2410 const student_t_distribution<_RealType>& __x)
2412 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2413 typedef typename __ostream_type::ios_base __ios_base;
2415 const typename __ios_base::fmtflags __flags = __os.flags();
2416 const _CharT __fill = __os.fill();
2417 const std::streamsize __precision = __os.precision();
2418 const _CharT __space = __os.widen(' ');
2419 __os.flags(__ios_base::scientific | __ios_base::left);
2421 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2423 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
2425 __os.flags(__flags);
2427 __os.precision(__precision);
2431 template<typename _RealType, typename _CharT, typename _Traits>
2432 std::basic_istream<_CharT, _Traits>&
2433 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2434 student_t_distribution<_RealType>& __x)
2436 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2437 typedef typename __istream_type::ios_base __ios_base;
2439 const typename __ios_base::fmtflags __flags = __is.flags();
2440 __is.flags(__ios_base::dec | __ios_base::skipws);
2443 __is >> __n >> __x._M_nd >> __x._M_gd;
2444 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
2446 __is.flags(__flags);
2451 template<typename _RealType>
2453 gamma_distribution<_RealType>::param_type::
2456 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2458 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2459 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2463 * Marsaglia, G. and Tsang, W. W.
2464 * "A Simple Method for Generating Gamma Variables"
2465 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2467 template<typename _RealType>
2468 template<typename _UniformRandomNumberGenerator>
2469 typename gamma_distribution<_RealType>::result_type
2470 gamma_distribution<_RealType>::
2471 operator()(_UniformRandomNumberGenerator& __urng,
2472 const param_type& __param)
2474 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2477 result_type __u, __v, __n;
2478 const result_type __a1 = (__param._M_malpha
2479 - _RealType(1.0) / _RealType(3.0));
2485 __n = _M_nd(__urng);
2486 __v = result_type(1.0) + __param._M_a2 * __n;
2490 __v = __v * __v * __v;
2493 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2494 && (std::log(__u) > (0.5 * __n * __n + __a1
2495 * (1.0 - __v + std::log(__v)))));
2497 if (__param.alpha() == __param._M_malpha)
2498 return __a1 * __v * __param.beta();
2505 return (std::pow(__u, result_type(1.0) / __param.alpha())
2506 * __a1 * __v * __param.beta());
2510 template<typename _RealType>
2511 template<typename _ForwardIterator,
2512 typename _UniformRandomNumberGenerator>
2514 gamma_distribution<_RealType>::
2515 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2516 _UniformRandomNumberGenerator& __urng,
2517 const param_type& __param)
2519 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2520 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2523 result_type __u, __v, __n;
2524 const result_type __a1 = (__param._M_malpha
2525 - _RealType(1.0) / _RealType(3.0));
2527 if (__param.alpha() == __param._M_malpha)
2534 __n = _M_nd(__urng);
2535 __v = result_type(1.0) + __param._M_a2 * __n;
2539 __v = __v * __v * __v;
2542 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2543 && (std::log(__u) > (0.5 * __n * __n + __a1
2544 * (1.0 - __v + std::log(__v)))));
2546 *__f++ = __a1 * __v * __param.beta();
2555 __n = _M_nd(__urng);
2556 __v = result_type(1.0) + __param._M_a2 * __n;
2560 __v = __v * __v * __v;
2563 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2564 && (std::log(__u) > (0.5 * __n * __n + __a1
2565 * (1.0 - __v + std::log(__v)))));
2571 *__f++ = (std::pow(__u, result_type(1.0) / __param.alpha())
2572 * __a1 * __v * __param.beta());
2576 template<typename _RealType, typename _CharT, typename _Traits>
2577 std::basic_ostream<_CharT, _Traits>&
2578 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2579 const gamma_distribution<_RealType>& __x)
2581 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2582 typedef typename __ostream_type::ios_base __ios_base;
2584 const typename __ios_base::fmtflags __flags = __os.flags();
2585 const _CharT __fill = __os.fill();
2586 const std::streamsize __precision = __os.precision();
2587 const _CharT __space = __os.widen(' ');
2588 __os.flags(__ios_base::scientific | __ios_base::left);
2590 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2592 __os << __x.alpha() << __space << __x.beta()
2593 << __space << __x._M_nd;
2595 __os.flags(__flags);
2597 __os.precision(__precision);
2601 template<typename _RealType, typename _CharT, typename _Traits>
2602 std::basic_istream<_CharT, _Traits>&
2603 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2604 gamma_distribution<_RealType>& __x)
2606 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2607 typedef typename __istream_type::ios_base __ios_base;
2609 const typename __ios_base::fmtflags __flags = __is.flags();
2610 __is.flags(__ios_base::dec | __ios_base::skipws);
2612 _RealType __alpha_val, __beta_val;
2613 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2614 __x.param(typename gamma_distribution<_RealType>::
2615 param_type(__alpha_val, __beta_val));
2617 __is.flags(__flags);
2622 template<typename _RealType>
2623 template<typename _UniformRandomNumberGenerator>
2624 typename weibull_distribution<_RealType>::result_type
2625 weibull_distribution<_RealType>::
2626 operator()(_UniformRandomNumberGenerator& __urng,
2627 const param_type& __p)
2629 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2631 return __p.b() * std::pow(-std::log(__aurng()),
2632 result_type(1) / __p.a());
2635 template<typename _RealType>
2636 template<typename _ForwardIterator,
2637 typename _UniformRandomNumberGenerator>
2639 weibull_distribution<_RealType>::
2640 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2641 _UniformRandomNumberGenerator& __urng,
2642 const param_type& __p)
2644 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2645 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2647 auto inv_a = result_type(1) / __p.a();
2650 *__f++ = __p.b() * std::pow(-std::log(__aurng()), inv_a);
2653 template<typename _RealType, typename _CharT, typename _Traits>
2654 std::basic_ostream<_CharT, _Traits>&
2655 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2656 const weibull_distribution<_RealType>& __x)
2658 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2659 typedef typename __ostream_type::ios_base __ios_base;
2661 const typename __ios_base::fmtflags __flags = __os.flags();
2662 const _CharT __fill = __os.fill();
2663 const std::streamsize __precision = __os.precision();
2664 const _CharT __space = __os.widen(' ');
2665 __os.flags(__ios_base::scientific | __ios_base::left);
2667 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2669 __os << __x.a() << __space << __x.b();
2671 __os.flags(__flags);
2673 __os.precision(__precision);
2677 template<typename _RealType, typename _CharT, typename _Traits>
2678 std::basic_istream<_CharT, _Traits>&
2679 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2680 weibull_distribution<_RealType>& __x)
2682 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2683 typedef typename __istream_type::ios_base __ios_base;
2685 const typename __ios_base::fmtflags __flags = __is.flags();
2686 __is.flags(__ios_base::dec | __ios_base::skipws);
2690 __x.param(typename weibull_distribution<_RealType>::
2691 param_type(__a, __b));
2693 __is.flags(__flags);
2698 template<typename _RealType>
2699 template<typename _UniformRandomNumberGenerator>
2700 typename extreme_value_distribution<_RealType>::result_type
2701 extreme_value_distribution<_RealType>::
2702 operator()(_UniformRandomNumberGenerator& __urng,
2703 const param_type& __p)
2705 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2707 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
2710 template<typename _RealType>
2711 template<typename _ForwardIterator,
2712 typename _UniformRandomNumberGenerator>
2714 extreme_value_distribution<_RealType>::
2715 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2716 _UniformRandomNumberGenerator& __urng,
2717 const param_type& __p)
2719 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2720 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2724 *__f++ = __p.a() - __p.b() * std::log(-std::log(__aurng()));
2727 template<typename _RealType, typename _CharT, typename _Traits>
2728 std::basic_ostream<_CharT, _Traits>&
2729 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2730 const extreme_value_distribution<_RealType>& __x)
2732 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2733 typedef typename __ostream_type::ios_base __ios_base;
2735 const typename __ios_base::fmtflags __flags = __os.flags();
2736 const _CharT __fill = __os.fill();
2737 const std::streamsize __precision = __os.precision();
2738 const _CharT __space = __os.widen(' ');
2739 __os.flags(__ios_base::scientific | __ios_base::left);
2741 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2743 __os << __x.a() << __space << __x.b();
2745 __os.flags(__flags);
2747 __os.precision(__precision);
2751 template<typename _RealType, typename _CharT, typename _Traits>
2752 std::basic_istream<_CharT, _Traits>&
2753 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2754 extreme_value_distribution<_RealType>& __x)
2756 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2757 typedef typename __istream_type::ios_base __ios_base;
2759 const typename __ios_base::fmtflags __flags = __is.flags();
2760 __is.flags(__ios_base::dec | __ios_base::skipws);
2764 __x.param(typename extreme_value_distribution<_RealType>::
2765 param_type(__a, __b));
2767 __is.flags(__flags);
2772 template<typename _IntType>
2774 discrete_distribution<_IntType>::param_type::
2777 if (_M_prob.size() < 2)
2783 const double __sum = std::accumulate(_M_prob.begin(),
2784 _M_prob.end(), 0.0);
2785 // Now normalize the probabilites.
2786 __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2787 std::bind2nd(std::divides<double>(), __sum));
2788 // Accumulate partial sums.
2789 _M_cp.reserve(_M_prob.size());
2790 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2791 std::back_inserter(_M_cp));
2792 // Make sure the last cumulative probability is one.
2793 _M_cp[_M_cp.size() - 1] = 1.0;
2796 template<typename _IntType>
2797 template<typename _Func>
2798 discrete_distribution<_IntType>::param_type::
2799 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2800 : _M_prob(), _M_cp()
2802 const size_t __n = __nw == 0 ? 1 : __nw;
2803 const double __delta = (__xmax - __xmin) / __n;
2805 _M_prob.reserve(__n);
2806 for (size_t __k = 0; __k < __nw; ++__k)
2807 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2812 template<typename _IntType>
2813 template<typename _UniformRandomNumberGenerator>
2814 typename discrete_distribution<_IntType>::result_type
2815 discrete_distribution<_IntType>::
2816 operator()(_UniformRandomNumberGenerator& __urng,
2817 const param_type& __param)
2819 if (__param._M_cp.empty())
2820 return result_type(0);
2822 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2825 const double __p = __aurng();
2826 auto __pos = std::lower_bound(__param._M_cp.begin(),
2827 __param._M_cp.end(), __p);
2829 return __pos - __param._M_cp.begin();
2832 template<typename _IntType>
2833 template<typename _ForwardIterator,
2834 typename _UniformRandomNumberGenerator>
2836 discrete_distribution<_IntType>::
2837 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2838 _UniformRandomNumberGenerator& __urng,
2839 const param_type& __param)
2841 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2843 if (__param._M_cp.empty())
2846 *__f++ = result_type(0);
2850 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2855 const double __p = __aurng();
2856 auto __pos = std::lower_bound(__param._M_cp.begin(),
2857 __param._M_cp.end(), __p);
2859 *__f++ = __pos - __param._M_cp.begin();
2863 template<typename _IntType, typename _CharT, typename _Traits>
2864 std::basic_ostream<_CharT, _Traits>&
2865 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2866 const discrete_distribution<_IntType>& __x)
2868 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2869 typedef typename __ostream_type::ios_base __ios_base;
2871 const typename __ios_base::fmtflags __flags = __os.flags();
2872 const _CharT __fill = __os.fill();
2873 const std::streamsize __precision = __os.precision();
2874 const _CharT __space = __os.widen(' ');
2875 __os.flags(__ios_base::scientific | __ios_base::left);
2877 __os.precision(std::numeric_limits<double>::max_digits10);
2879 std::vector<double> __prob = __x.probabilities();
2880 __os << __prob.size();
2881 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2882 __os << __space << *__dit;
2884 __os.flags(__flags);
2886 __os.precision(__precision);
2890 template<typename _IntType, typename _CharT, typename _Traits>
2891 std::basic_istream<_CharT, _Traits>&
2892 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2893 discrete_distribution<_IntType>& __x)
2895 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2896 typedef typename __istream_type::ios_base __ios_base;
2898 const typename __ios_base::fmtflags __flags = __is.flags();
2899 __is.flags(__ios_base::dec | __ios_base::skipws);
2904 std::vector<double> __prob_vec;
2905 __prob_vec.reserve(__n);
2906 for (; __n != 0; --__n)
2910 __prob_vec.push_back(__prob);
2913 __x.param(typename discrete_distribution<_IntType>::
2914 param_type(__prob_vec.begin(), __prob_vec.end()));
2916 __is.flags(__flags);
2921 template<typename _RealType>
2923 piecewise_constant_distribution<_RealType>::param_type::
2926 if (_M_int.size() < 2
2927 || (_M_int.size() == 2
2928 && _M_int[0] == _RealType(0)
2929 && _M_int[1] == _RealType(1)))
2936 const double __sum = std::accumulate(_M_den.begin(),
2939 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2940 std::bind2nd(std::divides<double>(), __sum));
2942 _M_cp.reserve(_M_den.size());
2943 std::partial_sum(_M_den.begin(), _M_den.end(),
2944 std::back_inserter(_M_cp));
2946 // Make sure the last cumulative probability is one.
2947 _M_cp[_M_cp.size() - 1] = 1.0;
2949 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2950 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2953 template<typename _RealType>
2954 template<typename _InputIteratorB, typename _InputIteratorW>
2955 piecewise_constant_distribution<_RealType>::param_type::
2956 param_type(_InputIteratorB __bbegin,
2957 _InputIteratorB __bend,
2958 _InputIteratorW __wbegin)
2959 : _M_int(), _M_den(), _M_cp()
2961 if (__bbegin != __bend)
2965 _M_int.push_back(*__bbegin);
2967 if (__bbegin == __bend)
2970 _M_den.push_back(*__wbegin);
2978 template<typename _RealType>
2979 template<typename _Func>
2980 piecewise_constant_distribution<_RealType>::param_type::
2981 param_type(initializer_list<_RealType> __bl, _Func __fw)
2982 : _M_int(), _M_den(), _M_cp()
2984 _M_int.reserve(__bl.size());
2985 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2986 _M_int.push_back(*__biter);
2988 _M_den.reserve(_M_int.size() - 1);
2989 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2990 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2995 template<typename _RealType>
2996 template<typename _Func>
2997 piecewise_constant_distribution<_RealType>::param_type::
2998 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2999 : _M_int(), _M_den(), _M_cp()
3001 const size_t __n = __nw == 0 ? 1 : __nw;
3002 const _RealType __delta = (__xmax - __xmin) / __n;
3004 _M_int.reserve(__n + 1);
3005 for (size_t __k = 0; __k <= __nw; ++__k)
3006 _M_int.push_back(__xmin + __k * __delta);
3008 _M_den.reserve(__n);
3009 for (size_t __k = 0; __k < __nw; ++__k)
3010 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
3015 template<typename _RealType>
3016 template<typename _UniformRandomNumberGenerator>
3017 typename piecewise_constant_distribution<_RealType>::result_type
3018 piecewise_constant_distribution<_RealType>::
3019 operator()(_UniformRandomNumberGenerator& __urng,
3020 const param_type& __param)
3022 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3025 const double __p = __aurng();
3026 if (__param._M_cp.empty())
3029 auto __pos = std::lower_bound(__param._M_cp.begin(),
3030 __param._M_cp.end(), __p);
3031 const size_t __i = __pos - __param._M_cp.begin();
3033 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3035 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
3038 template<typename _RealType>
3039 template<typename _ForwardIterator,
3040 typename _UniformRandomNumberGenerator>
3042 piecewise_constant_distribution<_RealType>::
3043 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3044 _UniformRandomNumberGenerator& __urng,
3045 const param_type& __param)
3047 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3048 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3051 if (__param._M_cp.empty())
3060 const double __p = __aurng();
3062 auto __pos = std::lower_bound(__param._M_cp.begin(),
3063 __param._M_cp.end(), __p);
3064 const size_t __i = __pos - __param._M_cp.begin();
3066 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3068 *__f++ = (__param._M_int[__i]
3069 + (__p - __pref) / __param._M_den[__i]);
3073 template<typename _RealType, typename _CharT, typename _Traits>
3074 std::basic_ostream<_CharT, _Traits>&
3075 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3076 const piecewise_constant_distribution<_RealType>& __x)
3078 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
3079 typedef typename __ostream_type::ios_base __ios_base;
3081 const typename __ios_base::fmtflags __flags = __os.flags();
3082 const _CharT __fill = __os.fill();
3083 const std::streamsize __precision = __os.precision();
3084 const _CharT __space = __os.widen(' ');
3085 __os.flags(__ios_base::scientific | __ios_base::left);
3087 __os.precision(std::numeric_limits<_RealType>::max_digits10);
3089 std::vector<_RealType> __int = __x.intervals();
3090 __os << __int.size() - 1;
3092 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
3093 __os << __space << *__xit;
3095 std::vector<double> __den = __x.densities();
3096 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
3097 __os << __space << *__dit;
3099 __os.flags(__flags);
3101 __os.precision(__precision);
3105 template<typename _RealType, typename _CharT, typename _Traits>
3106 std::basic_istream<_CharT, _Traits>&
3107 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3108 piecewise_constant_distribution<_RealType>& __x)
3110 typedef std::basic_istream<_CharT, _Traits> __istream_type;
3111 typedef typename __istream_type::ios_base __ios_base;
3113 const typename __ios_base::fmtflags __flags = __is.flags();
3114 __is.flags(__ios_base::dec | __ios_base::skipws);
3119 std::vector<_RealType> __int_vec;
3120 __int_vec.reserve(__n + 1);
3121 for (size_t __i = 0; __i <= __n; ++__i)
3125 __int_vec.push_back(__int);
3128 std::vector<double> __den_vec;
3129 __den_vec.reserve(__n);
3130 for (size_t __i = 0; __i < __n; ++__i)
3134 __den_vec.push_back(__den);
3137 __x.param(typename piecewise_constant_distribution<_RealType>::
3138 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
3140 __is.flags(__flags);
3145 template<typename _RealType>
3147 piecewise_linear_distribution<_RealType>::param_type::
3150 if (_M_int.size() < 2
3151 || (_M_int.size() == 2
3152 && _M_int[0] == _RealType(0)
3153 && _M_int[1] == _RealType(1)
3154 && _M_den[0] == _M_den[1]))
3162 _M_cp.reserve(_M_int.size() - 1);
3163 _M_m.reserve(_M_int.size() - 1);
3164 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
3166 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
3167 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
3168 _M_cp.push_back(__sum);
3169 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
3172 // Now normalize the densities...
3173 __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
3174 std::bind2nd(std::divides<double>(), __sum));
3175 // ... and partial sums...
3176 __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
3177 std::bind2nd(std::divides<double>(), __sum));
3179 __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
3180 std::bind2nd(std::divides<double>(), __sum));
3181 // Make sure the last cumulative probablility is one.
3182 _M_cp[_M_cp.size() - 1] = 1.0;
3185 template<typename _RealType>
3186 template<typename _InputIteratorB, typename _InputIteratorW>
3187 piecewise_linear_distribution<_RealType>::param_type::
3188 param_type(_InputIteratorB __bbegin,
3189 _InputIteratorB __bend,
3190 _InputIteratorW __wbegin)
3191 : _M_int(), _M_den(), _M_cp(), _M_m()
3193 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
3195 _M_int.push_back(*__bbegin);
3196 _M_den.push_back(*__wbegin);
3202 template<typename _RealType>
3203 template<typename _Func>
3204 piecewise_linear_distribution<_RealType>::param_type::
3205 param_type(initializer_list<_RealType> __bl, _Func __fw)
3206 : _M_int(), _M_den(), _M_cp(), _M_m()
3208 _M_int.reserve(__bl.size());
3209 _M_den.reserve(__bl.size());
3210 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
3212 _M_int.push_back(*__biter);
3213 _M_den.push_back(__fw(*__biter));
3219 template<typename _RealType>
3220 template<typename _Func>
3221 piecewise_linear_distribution<_RealType>::param_type::
3222 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
3223 : _M_int(), _M_den(), _M_cp(), _M_m()
3225 const size_t __n = __nw == 0 ? 1 : __nw;
3226 const _RealType __delta = (__xmax - __xmin) / __n;
3228 _M_int.reserve(__n + 1);
3229 _M_den.reserve(__n + 1);
3230 for (size_t __k = 0; __k <= __nw; ++__k)
3232 _M_int.push_back(__xmin + __k * __delta);
3233 _M_den.push_back(__fw(_M_int[__k] + __delta));
3239 template<typename _RealType>
3240 template<typename _UniformRandomNumberGenerator>
3241 typename piecewise_linear_distribution<_RealType>::result_type
3242 piecewise_linear_distribution<_RealType>::
3243 operator()(_UniformRandomNumberGenerator& __urng,
3244 const param_type& __param)
3246 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3249 const double __p = __aurng();
3250 if (__param._M_cp.empty())
3253 auto __pos = std::lower_bound(__param._M_cp.begin(),
3254 __param._M_cp.end(), __p);
3255 const size_t __i = __pos - __param._M_cp.begin();
3257 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3259 const double __a = 0.5 * __param._M_m[__i];
3260 const double __b = __param._M_den[__i];
3261 const double __cm = __p - __pref;
3263 _RealType __x = __param._M_int[__i];
3268 const double __d = __b * __b + 4.0 * __a * __cm;
3269 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
3275 template<typename _RealType>
3276 template<typename _ForwardIterator,
3277 typename _UniformRandomNumberGenerator>
3279 piecewise_linear_distribution<_RealType>::
3280 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3281 _UniformRandomNumberGenerator& __urng,
3282 const param_type& __param)
3284 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3285 // We could duplicate everything from operator()...
3287 *__f++ = this->operator()(__urng, __param);
3290 template<typename _RealType, typename _CharT, typename _Traits>
3291 std::basic_ostream<_CharT, _Traits>&
3292 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3293 const piecewise_linear_distribution<_RealType>& __x)
3295 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
3296 typedef typename __ostream_type::ios_base __ios_base;
3298 const typename __ios_base::fmtflags __flags = __os.flags();
3299 const _CharT __fill = __os.fill();
3300 const std::streamsize __precision = __os.precision();
3301 const _CharT __space = __os.widen(' ');
3302 __os.flags(__ios_base::scientific | __ios_base::left);
3304 __os.precision(std::numeric_limits<_RealType>::max_digits10);
3306 std::vector<_RealType> __int = __x.intervals();
3307 __os << __int.size() - 1;
3309 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
3310 __os << __space << *__xit;
3312 std::vector<double> __den = __x.densities();
3313 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
3314 __os << __space << *__dit;
3316 __os.flags(__flags);
3318 __os.precision(__precision);
3322 template<typename _RealType, typename _CharT, typename _Traits>
3323 std::basic_istream<_CharT, _Traits>&
3324 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3325 piecewise_linear_distribution<_RealType>& __x)
3327 typedef std::basic_istream<_CharT, _Traits> __istream_type;
3328 typedef typename __istream_type::ios_base __ios_base;
3330 const typename __ios_base::fmtflags __flags = __is.flags();
3331 __is.flags(__ios_base::dec | __ios_base::skipws);
3336 std::vector<_RealType> __int_vec;
3337 __int_vec.reserve(__n + 1);
3338 for (size_t __i = 0; __i <= __n; ++__i)
3342 __int_vec.push_back(__int);
3345 std::vector<double> __den_vec;
3346 __den_vec.reserve(__n + 1);
3347 for (size_t __i = 0; __i <= __n; ++__i)
3351 __den_vec.push_back(__den);
3354 __x.param(typename piecewise_linear_distribution<_RealType>::
3355 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
3357 __is.flags(__flags);
3362 template<typename _IntType>
3363 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
3365 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
3366 _M_v.push_back(__detail::__mod<result_type,
3367 __detail::_Shift<result_type, 32>::__value>(*__iter));
3370 template<typename _InputIterator>
3371 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
3373 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
3374 _M_v.push_back(__detail::__mod<result_type,
3375 __detail::_Shift<result_type, 32>::__value>(*__iter));
3378 template<typename _RandomAccessIterator>
3380 seed_seq::generate(_RandomAccessIterator __begin,
3381 _RandomAccessIterator __end)
3383 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3386 if (__begin == __end)
3389 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
3391 const size_t __n = __end - __begin;
3392 const size_t __s = _M_v.size();
3393 const size_t __t = (__n >= 623) ? 11
3398 const size_t __p = (__n - __t) / 2;
3399 const size_t __q = __p + __t;
3400 const size_t __m = std::max(size_t(__s + 1), __n);
3402 for (size_t __k = 0; __k < __m; ++__k)
3404 _Type __arg = (__begin[__k % __n]
3405 ^ __begin[(__k + __p) % __n]
3406 ^ __begin[(__k - 1) % __n]);
3407 _Type __r1 = __arg ^ (__arg >> 27);
3408 __r1 = __detail::__mod<_Type,
3409 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
3413 else if (__k <= __s)
3414 __r2 += __k % __n + _M_v[__k - 1];
3417 __r2 = __detail::__mod<_Type,
3418 __detail::_Shift<_Type, 32>::__value>(__r2);
3419 __begin[(__k + __p) % __n] += __r1;
3420 __begin[(__k + __q) % __n] += __r2;
3421 __begin[__k % __n] = __r2;
3424 for (size_t __k = __m; __k < __m + __n; ++__k)
3426 _Type __arg = (__begin[__k % __n]
3427 + __begin[(__k + __p) % __n]
3428 + __begin[(__k - 1) % __n]);
3429 _Type __r3 = __arg ^ (__arg >> 27);
3430 __r3 = __detail::__mod<_Type,
3431 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
3432 _Type __r4 = __r3 - __k % __n;
3433 __r4 = __detail::__mod<_Type,
3434 __detail::_Shift<_Type, 32>::__value>(__r4);
3435 __begin[(__k + __p) % __n] ^= __r3;
3436 __begin[(__k + __q) % __n] ^= __r4;
3437 __begin[__k % __n] = __r4;
3441 template<typename _RealType, size_t __bits,
3442 typename _UniformRandomNumberGenerator>
3444 generate_canonical(_UniformRandomNumberGenerator& __urng)
3447 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
3449 const long double __r = static_cast<long double>(__urng.max())
3450 - static_cast<long double>(__urng.min()) + 1.0L;
3451 const size_t __log2r = std::log(__r) / std::log(2.0L);
3452 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
3453 _RealType __sum = _RealType(0);
3454 _RealType __tmp = _RealType(1);
3455 for (; __k != 0; --__k)
3457 __sum += _RealType(__urng() - __urng.min()) * __tmp;
3460 return __sum / __tmp;
3463 _GLIBCXX_END_NAMESPACE_VERSION