1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2009-2017 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,
84 __normalize(_InputIterator __first, _InputIterator __last,
85 _OutputIterator __result, const _Tp& __factor)
87 for (; __first != __last; ++__first, ++__result)
88 *__result = *__first / __factor;
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;
391 template<typename _UIntType, size_t __w,
392 size_t __n, size_t __m, size_t __r,
393 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
394 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
397 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
398 __s, __b, __t, __c, __l, __f>::
401 const _UIntType __upper_mask = (~_UIntType()) << __r;
402 const _UIntType __lower_mask = ~__upper_mask;
404 for (size_t __k = 0; __k < (__n - __m); ++__k)
406 _UIntType __y = ((_M_x[__k] & __upper_mask)
407 | (_M_x[__k + 1] & __lower_mask));
408 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
409 ^ ((__y & 0x01) ? __a : 0));
412 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
414 _UIntType __y = ((_M_x[__k] & __upper_mask)
415 | (_M_x[__k + 1] & __lower_mask));
416 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
417 ^ ((__y & 0x01) ? __a : 0));
420 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
421 | (_M_x[0] & __lower_mask));
422 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
423 ^ ((__y & 0x01) ? __a : 0));
427 template<typename _UIntType, size_t __w,
428 size_t __n, size_t __m, size_t __r,
429 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
430 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
433 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
434 __s, __b, __t, __c, __l, __f>::
435 discard(unsigned long long __z)
437 while (__z > state_size - _M_p)
439 __z -= state_size - _M_p;
445 template<typename _UIntType, size_t __w,
446 size_t __n, size_t __m, size_t __r,
447 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
448 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
451 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
452 __s, __b, __t, __c, __l, __f>::result_type
453 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
454 __s, __b, __t, __c, __l, __f>::
457 // Reload the vector - cost is O(n) amortized over n calls.
458 if (_M_p >= state_size)
461 // Calculate o(x(i)).
462 result_type __z = _M_x[_M_p++];
463 __z ^= (__z >> __u) & __d;
464 __z ^= (__z << __s) & __b;
465 __z ^= (__z << __t) & __c;
471 template<typename _UIntType, size_t __w,
472 size_t __n, size_t __m, size_t __r,
473 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
474 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
475 _UIntType __f, typename _CharT, typename _Traits>
476 std::basic_ostream<_CharT, _Traits>&
477 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
478 const mersenne_twister_engine<_UIntType, __w, __n, __m,
479 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
481 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
482 typedef typename __ostream_type::ios_base __ios_base;
484 const typename __ios_base::fmtflags __flags = __os.flags();
485 const _CharT __fill = __os.fill();
486 const _CharT __space = __os.widen(' ');
487 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
490 for (size_t __i = 0; __i < __n; ++__i)
491 __os << __x._M_x[__i] << __space;
499 template<typename _UIntType, size_t __w,
500 size_t __n, size_t __m, size_t __r,
501 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
502 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
503 _UIntType __f, typename _CharT, typename _Traits>
504 std::basic_istream<_CharT, _Traits>&
505 operator>>(std::basic_istream<_CharT, _Traits>& __is,
506 mersenne_twister_engine<_UIntType, __w, __n, __m,
507 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
509 typedef std::basic_istream<_CharT, _Traits> __istream_type;
510 typedef typename __istream_type::ios_base __ios_base;
512 const typename __ios_base::fmtflags __flags = __is.flags();
513 __is.flags(__ios_base::dec | __ios_base::skipws);
515 for (size_t __i = 0; __i < __n; ++__i)
516 __is >> __x._M_x[__i];
524 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
526 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
528 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
530 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
532 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
534 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
536 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
538 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
540 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
542 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
543 seed(result_type __value)
545 std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
546 __lcg(__value == 0u ? default_seed : __value);
548 const size_t __n = (__w + 31) / 32;
550 for (size_t __i = 0; __i < long_lag; ++__i)
552 _UIntType __sum = 0u;
553 _UIntType __factor = 1u;
554 for (size_t __j = 0; __j < __n; ++__j)
556 __sum += __detail::__mod<uint_least32_t,
557 __detail::_Shift<uint_least32_t, 32>::__value>
558 (__lcg()) * __factor;
559 __factor *= __detail::_Shift<_UIntType, 32>::__value;
561 _M_x[__i] = __detail::__mod<_UIntType,
562 __detail::_Shift<_UIntType, __w>::__value>(__sum);
564 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
568 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
569 template<typename _Sseq>
570 typename std::enable_if<std::is_class<_Sseq>::value>::type
571 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
574 const size_t __k = (__w + 31) / 32;
575 uint_least32_t __arr[__r * __k];
576 __q.generate(__arr + 0, __arr + __r * __k);
578 for (size_t __i = 0; __i < long_lag; ++__i)
580 _UIntType __sum = 0u;
581 _UIntType __factor = 1u;
582 for (size_t __j = 0; __j < __k; ++__j)
584 __sum += __arr[__k * __i + __j] * __factor;
585 __factor *= __detail::_Shift<_UIntType, 32>::__value;
587 _M_x[__i] = __detail::__mod<_UIntType,
588 __detail::_Shift<_UIntType, __w>::__value>(__sum);
590 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
594 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
595 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
597 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
600 // Derive short lag index from current index.
601 long __ps = _M_p - short_lag;
605 // Calculate new x(i) without overflow or division.
606 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
609 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
611 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
616 __xi = (__detail::_Shift<_UIntType, __w>::__value
617 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
622 // Adjust current index to loop around in ring buffer.
623 if (++_M_p >= long_lag)
629 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
630 typename _CharT, typename _Traits>
631 std::basic_ostream<_CharT, _Traits>&
632 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
633 const subtract_with_carry_engine<_UIntType,
636 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
637 typedef typename __ostream_type::ios_base __ios_base;
639 const typename __ios_base::fmtflags __flags = __os.flags();
640 const _CharT __fill = __os.fill();
641 const _CharT __space = __os.widen(' ');
642 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
645 for (size_t __i = 0; __i < __r; ++__i)
646 __os << __x._M_x[__i] << __space;
647 __os << __x._M_carry << __space << __x._M_p;
654 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
655 typename _CharT, typename _Traits>
656 std::basic_istream<_CharT, _Traits>&
657 operator>>(std::basic_istream<_CharT, _Traits>& __is,
658 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
660 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
661 typedef typename __istream_type::ios_base __ios_base;
663 const typename __ios_base::fmtflags __flags = __is.flags();
664 __is.flags(__ios_base::dec | __ios_base::skipws);
666 for (size_t __i = 0; __i < __r; ++__i)
667 __is >> __x._M_x[__i];
668 __is >> __x._M_carry;
676 template<typename _RandomNumberEngine, size_t __p, size_t __r>
678 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
680 template<typename _RandomNumberEngine, size_t __p, size_t __r>
682 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
684 template<typename _RandomNumberEngine, size_t __p, size_t __r>
685 typename discard_block_engine<_RandomNumberEngine,
686 __p, __r>::result_type
687 discard_block_engine<_RandomNumberEngine, __p, __r>::
690 if (_M_n >= used_block)
692 _M_b.discard(block_size - _M_n);
699 template<typename _RandomNumberEngine, size_t __p, size_t __r,
700 typename _CharT, typename _Traits>
701 std::basic_ostream<_CharT, _Traits>&
702 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
703 const discard_block_engine<_RandomNumberEngine,
706 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
707 typedef typename __ostream_type::ios_base __ios_base;
709 const typename __ios_base::fmtflags __flags = __os.flags();
710 const _CharT __fill = __os.fill();
711 const _CharT __space = __os.widen(' ');
712 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
715 __os << __x.base() << __space << __x._M_n;
722 template<typename _RandomNumberEngine, size_t __p, size_t __r,
723 typename _CharT, typename _Traits>
724 std::basic_istream<_CharT, _Traits>&
725 operator>>(std::basic_istream<_CharT, _Traits>& __is,
726 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
728 typedef std::basic_istream<_CharT, _Traits> __istream_type;
729 typedef typename __istream_type::ios_base __ios_base;
731 const typename __ios_base::fmtflags __flags = __is.flags();
732 __is.flags(__ios_base::dec | __ios_base::skipws);
734 __is >> __x._M_b >> __x._M_n;
741 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
742 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
744 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
747 typedef typename _RandomNumberEngine::result_type _Eresult_type;
748 const _Eresult_type __r
749 = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max()
750 ? _M_b.max() - _M_b.min() + 1 : 0);
751 const unsigned __edig = std::numeric_limits<_Eresult_type>::digits;
752 const unsigned __m = __r ? std::__lg(__r) : __edig;
754 typedef typename std::common_type<_Eresult_type, result_type>::type
756 const unsigned __cdig = std::numeric_limits<__ctype>::digits;
759 __ctype __s0, __s1, __y0, __y1;
761 for (size_t __i = 0; __i < 2; ++__i)
763 __n = (__w + __m - 1) / __m + __i;
764 __n0 = __n - __w % __n;
765 const unsigned __w0 = __w / __n; // __w0 <= __m
771 __s0 = __ctype(1) << __w0;
779 __y0 = __s0 * (__r / __s0);
781 __y1 = __s1 * (__r / __s1);
783 if (__r - __y0 <= __y0 / __n)
790 result_type __sum = 0;
791 for (size_t __k = 0; __k < __n0; ++__k)
795 __u = _M_b() - _M_b.min();
796 while (__y0 && __u >= __y0);
797 __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
799 for (size_t __k = __n0; __k < __n; ++__k)
803 __u = _M_b() - _M_b.min();
804 while (__y1 && __u >= __y1);
805 __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
811 template<typename _RandomNumberEngine, size_t __k>
813 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
815 template<typename _RandomNumberEngine, size_t __k>
816 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
817 shuffle_order_engine<_RandomNumberEngine, __k>::
820 size_t __j = __k * ((_M_y - _M_b.min())
821 / (_M_b.max() - _M_b.min() + 1.0L));
828 template<typename _RandomNumberEngine, size_t __k,
829 typename _CharT, typename _Traits>
830 std::basic_ostream<_CharT, _Traits>&
831 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
832 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
834 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
835 typedef typename __ostream_type::ios_base __ios_base;
837 const typename __ios_base::fmtflags __flags = __os.flags();
838 const _CharT __fill = __os.fill();
839 const _CharT __space = __os.widen(' ');
840 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
844 for (size_t __i = 0; __i < __k; ++__i)
845 __os << __space << __x._M_v[__i];
846 __os << __space << __x._M_y;
853 template<typename _RandomNumberEngine, size_t __k,
854 typename _CharT, typename _Traits>
855 std::basic_istream<_CharT, _Traits>&
856 operator>>(std::basic_istream<_CharT, _Traits>& __is,
857 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
859 typedef std::basic_istream<_CharT, _Traits> __istream_type;
860 typedef typename __istream_type::ios_base __ios_base;
862 const typename __ios_base::fmtflags __flags = __is.flags();
863 __is.flags(__ios_base::dec | __ios_base::skipws);
866 for (size_t __i = 0; __i < __k; ++__i)
867 __is >> __x._M_v[__i];
875 template<typename _IntType, typename _CharT, typename _Traits>
876 std::basic_ostream<_CharT, _Traits>&
877 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
878 const uniform_int_distribution<_IntType>& __x)
880 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
881 typedef typename __ostream_type::ios_base __ios_base;
883 const typename __ios_base::fmtflags __flags = __os.flags();
884 const _CharT __fill = __os.fill();
885 const _CharT __space = __os.widen(' ');
886 __os.flags(__ios_base::scientific | __ios_base::left);
889 __os << __x.a() << __space << __x.b();
896 template<typename _IntType, typename _CharT, typename _Traits>
897 std::basic_istream<_CharT, _Traits>&
898 operator>>(std::basic_istream<_CharT, _Traits>& __is,
899 uniform_int_distribution<_IntType>& __x)
901 typedef std::basic_istream<_CharT, _Traits> __istream_type;
902 typedef typename __istream_type::ios_base __ios_base;
904 const typename __ios_base::fmtflags __flags = __is.flags();
905 __is.flags(__ios_base::dec | __ios_base::skipws);
909 __x.param(typename uniform_int_distribution<_IntType>::
910 param_type(__a, __b));
917 template<typename _RealType>
918 template<typename _ForwardIterator,
919 typename _UniformRandomNumberGenerator>
921 uniform_real_distribution<_RealType>::
922 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
923 _UniformRandomNumberGenerator& __urng,
924 const param_type& __p)
926 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
927 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
929 auto __range = __p.b() - __p.a();
931 *__f++ = __aurng() * __range + __p.a();
934 template<typename _RealType, typename _CharT, typename _Traits>
935 std::basic_ostream<_CharT, _Traits>&
936 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
937 const uniform_real_distribution<_RealType>& __x)
939 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
940 typedef typename __ostream_type::ios_base __ios_base;
942 const typename __ios_base::fmtflags __flags = __os.flags();
943 const _CharT __fill = __os.fill();
944 const std::streamsize __precision = __os.precision();
945 const _CharT __space = __os.widen(' ');
946 __os.flags(__ios_base::scientific | __ios_base::left);
948 __os.precision(std::numeric_limits<_RealType>::max_digits10);
950 __os << __x.a() << __space << __x.b();
954 __os.precision(__precision);
958 template<typename _RealType, typename _CharT, typename _Traits>
959 std::basic_istream<_CharT, _Traits>&
960 operator>>(std::basic_istream<_CharT, _Traits>& __is,
961 uniform_real_distribution<_RealType>& __x)
963 typedef std::basic_istream<_CharT, _Traits> __istream_type;
964 typedef typename __istream_type::ios_base __ios_base;
966 const typename __ios_base::fmtflags __flags = __is.flags();
967 __is.flags(__ios_base::skipws);
971 __x.param(typename uniform_real_distribution<_RealType>::
972 param_type(__a, __b));
979 template<typename _ForwardIterator,
980 typename _UniformRandomNumberGenerator>
982 std::bernoulli_distribution::
983 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
984 _UniformRandomNumberGenerator& __urng,
985 const param_type& __p)
987 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
988 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
990 auto __limit = __p.p() * (__aurng.max() - __aurng.min());
993 *__f++ = (__aurng() - __aurng.min()) < __limit;
996 template<typename _CharT, typename _Traits>
997 std::basic_ostream<_CharT, _Traits>&
998 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
999 const bernoulli_distribution& __x)
1001 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1002 typedef typename __ostream_type::ios_base __ios_base;
1004 const typename __ios_base::fmtflags __flags = __os.flags();
1005 const _CharT __fill = __os.fill();
1006 const std::streamsize __precision = __os.precision();
1007 __os.flags(__ios_base::scientific | __ios_base::left);
1008 __os.fill(__os.widen(' '));
1009 __os.precision(std::numeric_limits<double>::max_digits10);
1013 __os.flags(__flags);
1015 __os.precision(__precision);
1020 template<typename _IntType>
1021 template<typename _UniformRandomNumberGenerator>
1022 typename geometric_distribution<_IntType>::result_type
1023 geometric_distribution<_IntType>::
1024 operator()(_UniformRandomNumberGenerator& __urng,
1025 const param_type& __param)
1027 // About the epsilon thing see this thread:
1028 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1029 const double __naf =
1030 (1 - std::numeric_limits<double>::epsilon()) / 2;
1031 // The largest _RealType convertible to _IntType.
1032 const double __thr =
1033 std::numeric_limits<_IntType>::max() + __naf;
1034 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1039 __cand = std::floor(std::log(1.0 - __aurng()) / __param._M_log_1_p);
1040 while (__cand >= __thr);
1042 return result_type(__cand + __naf);
1045 template<typename _IntType>
1046 template<typename _ForwardIterator,
1047 typename _UniformRandomNumberGenerator>
1049 geometric_distribution<_IntType>::
1050 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1051 _UniformRandomNumberGenerator& __urng,
1052 const param_type& __param)
1054 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1055 // About the epsilon thing see this thread:
1056 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1057 const double __naf =
1058 (1 - std::numeric_limits<double>::epsilon()) / 2;
1059 // The largest _RealType convertible to _IntType.
1060 const double __thr =
1061 std::numeric_limits<_IntType>::max() + __naf;
1062 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1069 __cand = std::floor(std::log(1.0 - __aurng())
1070 / __param._M_log_1_p);
1071 while (__cand >= __thr);
1073 *__f++ = __cand + __naf;
1077 template<typename _IntType,
1078 typename _CharT, typename _Traits>
1079 std::basic_ostream<_CharT, _Traits>&
1080 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1081 const geometric_distribution<_IntType>& __x)
1083 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1084 typedef typename __ostream_type::ios_base __ios_base;
1086 const typename __ios_base::fmtflags __flags = __os.flags();
1087 const _CharT __fill = __os.fill();
1088 const std::streamsize __precision = __os.precision();
1089 __os.flags(__ios_base::scientific | __ios_base::left);
1090 __os.fill(__os.widen(' '));
1091 __os.precision(std::numeric_limits<double>::max_digits10);
1095 __os.flags(__flags);
1097 __os.precision(__precision);
1101 template<typename _IntType,
1102 typename _CharT, typename _Traits>
1103 std::basic_istream<_CharT, _Traits>&
1104 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1105 geometric_distribution<_IntType>& __x)
1107 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1108 typedef typename __istream_type::ios_base __ios_base;
1110 const typename __ios_base::fmtflags __flags = __is.flags();
1111 __is.flags(__ios_base::skipws);
1115 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
1117 __is.flags(__flags);
1121 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1122 template<typename _IntType>
1123 template<typename _UniformRandomNumberGenerator>
1124 typename negative_binomial_distribution<_IntType>::result_type
1125 negative_binomial_distribution<_IntType>::
1126 operator()(_UniformRandomNumberGenerator& __urng)
1128 const double __y = _M_gd(__urng);
1130 // XXX Is the constructor too slow?
1131 std::poisson_distribution<result_type> __poisson(__y);
1132 return __poisson(__urng);
1135 template<typename _IntType>
1136 template<typename _UniformRandomNumberGenerator>
1137 typename negative_binomial_distribution<_IntType>::result_type
1138 negative_binomial_distribution<_IntType>::
1139 operator()(_UniformRandomNumberGenerator& __urng,
1140 const param_type& __p)
1142 typedef typename std::gamma_distribution<double>::param_type
1146 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1148 std::poisson_distribution<result_type> __poisson(__y);
1149 return __poisson(__urng);
1152 template<typename _IntType>
1153 template<typename _ForwardIterator,
1154 typename _UniformRandomNumberGenerator>
1156 negative_binomial_distribution<_IntType>::
1157 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1158 _UniformRandomNumberGenerator& __urng)
1160 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1163 const double __y = _M_gd(__urng);
1165 // XXX Is the constructor too slow?
1166 std::poisson_distribution<result_type> __poisson(__y);
1167 *__f++ = __poisson(__urng);
1171 template<typename _IntType>
1172 template<typename _ForwardIterator,
1173 typename _UniformRandomNumberGenerator>
1175 negative_binomial_distribution<_IntType>::
1176 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1177 _UniformRandomNumberGenerator& __urng,
1178 const param_type& __p)
1180 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1181 typename std::gamma_distribution<result_type>::param_type
1182 __p2(__p.k(), (1.0 - __p.p()) / __p.p());
1186 const double __y = _M_gd(__urng, __p2);
1188 std::poisson_distribution<result_type> __poisson(__y);
1189 *__f++ = __poisson(__urng);
1193 template<typename _IntType, typename _CharT, typename _Traits>
1194 std::basic_ostream<_CharT, _Traits>&
1195 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1196 const negative_binomial_distribution<_IntType>& __x)
1198 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1199 typedef typename __ostream_type::ios_base __ios_base;
1201 const typename __ios_base::fmtflags __flags = __os.flags();
1202 const _CharT __fill = __os.fill();
1203 const std::streamsize __precision = __os.precision();
1204 const _CharT __space = __os.widen(' ');
1205 __os.flags(__ios_base::scientific | __ios_base::left);
1206 __os.fill(__os.widen(' '));
1207 __os.precision(std::numeric_limits<double>::max_digits10);
1209 __os << __x.k() << __space << __x.p()
1210 << __space << __x._M_gd;
1212 __os.flags(__flags);
1214 __os.precision(__precision);
1218 template<typename _IntType, typename _CharT, typename _Traits>
1219 std::basic_istream<_CharT, _Traits>&
1220 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1221 negative_binomial_distribution<_IntType>& __x)
1223 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1224 typedef typename __istream_type::ios_base __ios_base;
1226 const typename __ios_base::fmtflags __flags = __is.flags();
1227 __is.flags(__ios_base::skipws);
1231 __is >> __k >> __p >> __x._M_gd;
1232 __x.param(typename negative_binomial_distribution<_IntType>::
1233 param_type(__k, __p));
1235 __is.flags(__flags);
1240 template<typename _IntType>
1242 poisson_distribution<_IntType>::param_type::
1245 #if _GLIBCXX_USE_C99_MATH_TR1
1248 const double __m = std::floor(_M_mean);
1249 _M_lm_thr = std::log(_M_mean);
1250 _M_lfm = std::lgamma(__m + 1);
1251 _M_sm = std::sqrt(__m);
1253 const double __pi_4 = 0.7853981633974483096156608458198757L;
1254 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1256 _M_d = std::round(std::max<double>(6.0, std::min(__m, __dx)));
1257 const double __cx = 2 * __m + _M_d;
1258 _M_scx = std::sqrt(__cx / 2);
1261 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1262 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1267 _M_lm_thr = std::exp(-_M_mean);
1271 * A rejection algorithm when mean >= 12 and a simple method based
1272 * upon the multiplication of uniform random variates otherwise.
1273 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1277 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1278 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1280 template<typename _IntType>
1281 template<typename _UniformRandomNumberGenerator>
1282 typename poisson_distribution<_IntType>::result_type
1283 poisson_distribution<_IntType>::
1284 operator()(_UniformRandomNumberGenerator& __urng,
1285 const param_type& __param)
1287 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1289 #if _GLIBCXX_USE_C99_MATH_TR1
1290 if (__param.mean() >= 12)
1294 // See comments above...
1295 const double __naf =
1296 (1 - std::numeric_limits<double>::epsilon()) / 2;
1297 const double __thr =
1298 std::numeric_limits<_IntType>::max() + __naf;
1300 const double __m = std::floor(__param.mean());
1302 const double __spi_2 = 1.2533141373155002512078826424055226L;
1303 const double __c1 = __param._M_sm * __spi_2;
1304 const double __c2 = __param._M_c2b + __c1;
1305 const double __c3 = __c2 + 1;
1306 const double __c4 = __c3 + 1;
1308 const double __e178 = 1.0129030479320018583185514777512983L;
1309 const double __c5 = __c4 + __e178;
1310 const double __c = __param._M_cb + __c5;
1311 const double __2cx = 2 * (2 * __m + __param._M_d);
1313 bool __reject = true;
1316 const double __u = __c * __aurng();
1317 const double __e = -std::log(1.0 - __aurng());
1323 const double __n = _M_nd(__urng);
1324 const double __y = -std::abs(__n) * __param._M_sm - 1;
1325 __x = std::floor(__y);
1326 __w = -__n * __n / 2;
1330 else if (__u <= __c2)
1332 const double __n = _M_nd(__urng);
1333 const double __y = 1 + std::abs(__n) * __param._M_scx;
1334 __x = std::ceil(__y);
1335 __w = __y * (2 - __y) * __param._M_1cx;
1336 if (__x > __param._M_d)
1339 else if (__u <= __c3)
1340 // NB: This case not in the book, nor in the Errata,
1341 // but should be ok...
1343 else if (__u <= __c4)
1345 else if (__u <= __c5)
1349 const double __v = -std::log(1.0 - __aurng());
1350 const double __y = __param._M_d
1351 + __v * __2cx / __param._M_d;
1352 __x = std::ceil(__y);
1353 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1356 __reject = (__w - __e - __x * __param._M_lm_thr
1357 > __param._M_lfm - std::lgamma(__x + __m + 1));
1359 __reject |= __x + __m >= __thr;
1363 return result_type(__x + __m + __naf);
1369 double __prod = 1.0;
1373 __prod *= __aurng();
1376 while (__prod > __param._M_lm_thr);
1382 template<typename _IntType>
1383 template<typename _ForwardIterator,
1384 typename _UniformRandomNumberGenerator>
1386 poisson_distribution<_IntType>::
1387 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1388 _UniformRandomNumberGenerator& __urng,
1389 const param_type& __param)
1391 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1392 // We could duplicate everything from operator()...
1394 *__f++ = this->operator()(__urng, __param);
1397 template<typename _IntType,
1398 typename _CharT, typename _Traits>
1399 std::basic_ostream<_CharT, _Traits>&
1400 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1401 const poisson_distribution<_IntType>& __x)
1403 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1404 typedef typename __ostream_type::ios_base __ios_base;
1406 const typename __ios_base::fmtflags __flags = __os.flags();
1407 const _CharT __fill = __os.fill();
1408 const std::streamsize __precision = __os.precision();
1409 const _CharT __space = __os.widen(' ');
1410 __os.flags(__ios_base::scientific | __ios_base::left);
1412 __os.precision(std::numeric_limits<double>::max_digits10);
1414 __os << __x.mean() << __space << __x._M_nd;
1416 __os.flags(__flags);
1418 __os.precision(__precision);
1422 template<typename _IntType,
1423 typename _CharT, typename _Traits>
1424 std::basic_istream<_CharT, _Traits>&
1425 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1426 poisson_distribution<_IntType>& __x)
1428 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1429 typedef typename __istream_type::ios_base __ios_base;
1431 const typename __ios_base::fmtflags __flags = __is.flags();
1432 __is.flags(__ios_base::skipws);
1435 __is >> __mean >> __x._M_nd;
1436 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1438 __is.flags(__flags);
1443 template<typename _IntType>
1445 binomial_distribution<_IntType>::param_type::
1448 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1452 #if _GLIBCXX_USE_C99_MATH_TR1
1453 if (_M_t * __p12 >= 8)
1456 const double __np = std::floor(_M_t * __p12);
1457 const double __pa = __np / _M_t;
1458 const double __1p = 1 - __pa;
1460 const double __pi_4 = 0.7853981633974483096156608458198757L;
1461 const double __d1x =
1462 std::sqrt(__np * __1p * std::log(32 * __np
1463 / (81 * __pi_4 * __1p)));
1464 _M_d1 = std::round(std::max<double>(1.0, __d1x));
1465 const double __d2x =
1466 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1467 / (__pi_4 * __pa)));
1468 _M_d2 = std::round(std::max<double>(1.0, __d2x));
1471 const double __spi_2 = 1.2533141373155002512078826424055226L;
1472 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1473 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1474 _M_c = 2 * _M_d1 / __np;
1475 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1476 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1477 const double __s1s = _M_s1 * _M_s1;
1478 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1480 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1481 const double __s2s = _M_s2 * _M_s2;
1482 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1483 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1484 _M_lf = (std::lgamma(__np + 1)
1485 + std::lgamma(_M_t - __np + 1));
1486 _M_lp1p = std::log(__pa / __1p);
1488 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1492 _M_q = -std::log(1 - __p12);
1495 template<typename _IntType>
1496 template<typename _UniformRandomNumberGenerator>
1497 typename binomial_distribution<_IntType>::result_type
1498 binomial_distribution<_IntType>::
1499 _M_waiting(_UniformRandomNumberGenerator& __urng,
1500 _IntType __t, double __q)
1504 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1511 const double __e = -std::log(1.0 - __aurng());
1512 __sum += __e / (__t - __x);
1515 while (__sum <= __q);
1521 * A rejection algorithm when t * p >= 8 and a simple waiting time
1522 * method - the second in the referenced book - otherwise.
1523 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1527 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1528 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1530 template<typename _IntType>
1531 template<typename _UniformRandomNumberGenerator>
1532 typename binomial_distribution<_IntType>::result_type
1533 binomial_distribution<_IntType>::
1534 operator()(_UniformRandomNumberGenerator& __urng,
1535 const param_type& __param)
1538 const _IntType __t = __param.t();
1539 const double __p = __param.p();
1540 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1541 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1544 #if _GLIBCXX_USE_C99_MATH_TR1
1545 if (!__param._M_easy)
1549 // See comments above...
1550 const double __naf =
1551 (1 - std::numeric_limits<double>::epsilon()) / 2;
1552 const double __thr =
1553 std::numeric_limits<_IntType>::max() + __naf;
1555 const double __np = std::floor(__t * __p12);
1558 const double __spi_2 = 1.2533141373155002512078826424055226L;
1559 const double __a1 = __param._M_a1;
1560 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1561 const double __a123 = __param._M_a123;
1562 const double __s1s = __param._M_s1 * __param._M_s1;
1563 const double __s2s = __param._M_s2 * __param._M_s2;
1568 const double __u = __param._M_s * __aurng();
1574 const double __n = _M_nd(__urng);
1575 const double __y = __param._M_s1 * std::abs(__n);
1576 __reject = __y >= __param._M_d1;
1579 const double __e = -std::log(1.0 - __aurng());
1580 __x = std::floor(__y);
1581 __v = -__e - __n * __n / 2 + __param._M_c;
1584 else if (__u <= __a12)
1586 const double __n = _M_nd(__urng);
1587 const double __y = __param._M_s2 * std::abs(__n);
1588 __reject = __y >= __param._M_d2;
1591 const double __e = -std::log(1.0 - __aurng());
1592 __x = std::floor(-__y);
1593 __v = -__e - __n * __n / 2;
1596 else if (__u <= __a123)
1598 const double __e1 = -std::log(1.0 - __aurng());
1599 const double __e2 = -std::log(1.0 - __aurng());
1601 const double __y = __param._M_d1
1602 + 2 * __s1s * __e1 / __param._M_d1;
1603 __x = std::floor(__y);
1604 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1605 -__y / (2 * __s1s)));
1610 const double __e1 = -std::log(1.0 - __aurng());
1611 const double __e2 = -std::log(1.0 - __aurng());
1613 const double __y = __param._M_d2
1614 + 2 * __s2s * __e1 / __param._M_d2;
1615 __x = std::floor(-__y);
1616 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1620 __reject = __reject || __x < -__np || __x > __t - __np;
1623 const double __lfx =
1624 std::lgamma(__np + __x + 1)
1625 + std::lgamma(__t - (__np + __x) + 1);
1626 __reject = __v > __param._M_lf - __lfx
1627 + __x * __param._M_lp1p;
1630 __reject |= __x + __np >= __thr;
1634 __x += __np + __naf;
1636 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x),
1638 __ret = _IntType(__x) + __z;
1642 __ret = _M_waiting(__urng, __t, __param._M_q);
1645 __ret = __t - __ret;
1649 template<typename _IntType>
1650 template<typename _ForwardIterator,
1651 typename _UniformRandomNumberGenerator>
1653 binomial_distribution<_IntType>::
1654 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1655 _UniformRandomNumberGenerator& __urng,
1656 const param_type& __param)
1658 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1659 // We could duplicate everything from operator()...
1661 *__f++ = this->operator()(__urng, __param);
1664 template<typename _IntType,
1665 typename _CharT, typename _Traits>
1666 std::basic_ostream<_CharT, _Traits>&
1667 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1668 const binomial_distribution<_IntType>& __x)
1670 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1671 typedef typename __ostream_type::ios_base __ios_base;
1673 const typename __ios_base::fmtflags __flags = __os.flags();
1674 const _CharT __fill = __os.fill();
1675 const std::streamsize __precision = __os.precision();
1676 const _CharT __space = __os.widen(' ');
1677 __os.flags(__ios_base::scientific | __ios_base::left);
1679 __os.precision(std::numeric_limits<double>::max_digits10);
1681 __os << __x.t() << __space << __x.p()
1682 << __space << __x._M_nd;
1684 __os.flags(__flags);
1686 __os.precision(__precision);
1690 template<typename _IntType,
1691 typename _CharT, typename _Traits>
1692 std::basic_istream<_CharT, _Traits>&
1693 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1694 binomial_distribution<_IntType>& __x)
1696 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1697 typedef typename __istream_type::ios_base __ios_base;
1699 const typename __ios_base::fmtflags __flags = __is.flags();
1700 __is.flags(__ios_base::dec | __ios_base::skipws);
1704 __is >> __t >> __p >> __x._M_nd;
1705 __x.param(typename binomial_distribution<_IntType>::
1706 param_type(__t, __p));
1708 __is.flags(__flags);
1713 template<typename _RealType>
1714 template<typename _ForwardIterator,
1715 typename _UniformRandomNumberGenerator>
1717 std::exponential_distribution<_RealType>::
1718 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1719 _UniformRandomNumberGenerator& __urng,
1720 const param_type& __p)
1722 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1723 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1726 *__f++ = -std::log(result_type(1) - __aurng()) / __p.lambda();
1729 template<typename _RealType, typename _CharT, typename _Traits>
1730 std::basic_ostream<_CharT, _Traits>&
1731 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1732 const exponential_distribution<_RealType>& __x)
1734 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1735 typedef typename __ostream_type::ios_base __ios_base;
1737 const typename __ios_base::fmtflags __flags = __os.flags();
1738 const _CharT __fill = __os.fill();
1739 const std::streamsize __precision = __os.precision();
1740 __os.flags(__ios_base::scientific | __ios_base::left);
1741 __os.fill(__os.widen(' '));
1742 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1744 __os << __x.lambda();
1746 __os.flags(__flags);
1748 __os.precision(__precision);
1752 template<typename _RealType, typename _CharT, typename _Traits>
1753 std::basic_istream<_CharT, _Traits>&
1754 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1755 exponential_distribution<_RealType>& __x)
1757 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1758 typedef typename __istream_type::ios_base __ios_base;
1760 const typename __ios_base::fmtflags __flags = __is.flags();
1761 __is.flags(__ios_base::dec | __ios_base::skipws);
1765 __x.param(typename exponential_distribution<_RealType>::
1766 param_type(__lambda));
1768 __is.flags(__flags);
1774 * Polar method due to Marsaglia.
1776 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1777 * New York, 1986, Ch. V, Sect. 4.4.
1779 template<typename _RealType>
1780 template<typename _UniformRandomNumberGenerator>
1781 typename normal_distribution<_RealType>::result_type
1782 normal_distribution<_RealType>::
1783 operator()(_UniformRandomNumberGenerator& __urng,
1784 const param_type& __param)
1787 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1790 if (_M_saved_available)
1792 _M_saved_available = false;
1797 result_type __x, __y, __r2;
1800 __x = result_type(2.0) * __aurng() - 1.0;
1801 __y = result_type(2.0) * __aurng() - 1.0;
1802 __r2 = __x * __x + __y * __y;
1804 while (__r2 > 1.0 || __r2 == 0.0);
1806 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1807 _M_saved = __x * __mult;
1808 _M_saved_available = true;
1809 __ret = __y * __mult;
1812 __ret = __ret * __param.stddev() + __param.mean();
1816 template<typename _RealType>
1817 template<typename _ForwardIterator,
1818 typename _UniformRandomNumberGenerator>
1820 normal_distribution<_RealType>::
1821 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1822 _UniformRandomNumberGenerator& __urng,
1823 const param_type& __param)
1825 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1830 if (_M_saved_available)
1832 _M_saved_available = false;
1833 *__f++ = _M_saved * __param.stddev() + __param.mean();
1839 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1842 while (__f + 1 < __t)
1844 result_type __x, __y, __r2;
1847 __x = result_type(2.0) * __aurng() - 1.0;
1848 __y = result_type(2.0) * __aurng() - 1.0;
1849 __r2 = __x * __x + __y * __y;
1851 while (__r2 > 1.0 || __r2 == 0.0);
1853 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1854 *__f++ = __y * __mult * __param.stddev() + __param.mean();
1855 *__f++ = __x * __mult * __param.stddev() + __param.mean();
1860 result_type __x, __y, __r2;
1863 __x = result_type(2.0) * __aurng() - 1.0;
1864 __y = result_type(2.0) * __aurng() - 1.0;
1865 __r2 = __x * __x + __y * __y;
1867 while (__r2 > 1.0 || __r2 == 0.0);
1869 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1870 _M_saved = __x * __mult;
1871 _M_saved_available = true;
1872 *__f = __y * __mult * __param.stddev() + __param.mean();
1876 template<typename _RealType>
1878 operator==(const std::normal_distribution<_RealType>& __d1,
1879 const std::normal_distribution<_RealType>& __d2)
1881 if (__d1._M_param == __d2._M_param
1882 && __d1._M_saved_available == __d2._M_saved_available)
1884 if (__d1._M_saved_available
1885 && __d1._M_saved == __d2._M_saved)
1887 else if(!__d1._M_saved_available)
1896 template<typename _RealType, typename _CharT, typename _Traits>
1897 std::basic_ostream<_CharT, _Traits>&
1898 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1899 const normal_distribution<_RealType>& __x)
1901 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1902 typedef typename __ostream_type::ios_base __ios_base;
1904 const typename __ios_base::fmtflags __flags = __os.flags();
1905 const _CharT __fill = __os.fill();
1906 const std::streamsize __precision = __os.precision();
1907 const _CharT __space = __os.widen(' ');
1908 __os.flags(__ios_base::scientific | __ios_base::left);
1910 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1912 __os << __x.mean() << __space << __x.stddev()
1913 << __space << __x._M_saved_available;
1914 if (__x._M_saved_available)
1915 __os << __space << __x._M_saved;
1917 __os.flags(__flags);
1919 __os.precision(__precision);
1923 template<typename _RealType, typename _CharT, typename _Traits>
1924 std::basic_istream<_CharT, _Traits>&
1925 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1926 normal_distribution<_RealType>& __x)
1928 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1929 typedef typename __istream_type::ios_base __ios_base;
1931 const typename __ios_base::fmtflags __flags = __is.flags();
1932 __is.flags(__ios_base::dec | __ios_base::skipws);
1934 double __mean, __stddev;
1935 __is >> __mean >> __stddev
1936 >> __x._M_saved_available;
1937 if (__x._M_saved_available)
1938 __is >> __x._M_saved;
1939 __x.param(typename normal_distribution<_RealType>::
1940 param_type(__mean, __stddev));
1942 __is.flags(__flags);
1947 template<typename _RealType>
1948 template<typename _ForwardIterator,
1949 typename _UniformRandomNumberGenerator>
1951 lognormal_distribution<_RealType>::
1952 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1953 _UniformRandomNumberGenerator& __urng,
1954 const param_type& __p)
1956 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1958 *__f++ = std::exp(__p.s() * _M_nd(__urng) + __p.m());
1961 template<typename _RealType, typename _CharT, typename _Traits>
1962 std::basic_ostream<_CharT, _Traits>&
1963 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1964 const lognormal_distribution<_RealType>& __x)
1966 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1967 typedef typename __ostream_type::ios_base __ios_base;
1969 const typename __ios_base::fmtflags __flags = __os.flags();
1970 const _CharT __fill = __os.fill();
1971 const std::streamsize __precision = __os.precision();
1972 const _CharT __space = __os.widen(' ');
1973 __os.flags(__ios_base::scientific | __ios_base::left);
1975 __os.precision(std::numeric_limits<_RealType>::max_digits10);
1977 __os << __x.m() << __space << __x.s()
1978 << __space << __x._M_nd;
1980 __os.flags(__flags);
1982 __os.precision(__precision);
1986 template<typename _RealType, typename _CharT, typename _Traits>
1987 std::basic_istream<_CharT, _Traits>&
1988 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1989 lognormal_distribution<_RealType>& __x)
1991 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1992 typedef typename __istream_type::ios_base __ios_base;
1994 const typename __ios_base::fmtflags __flags = __is.flags();
1995 __is.flags(__ios_base::dec | __ios_base::skipws);
1998 __is >> __m >> __s >> __x._M_nd;
1999 __x.param(typename lognormal_distribution<_RealType>::
2000 param_type(__m, __s));
2002 __is.flags(__flags);
2006 template<typename _RealType>
2007 template<typename _ForwardIterator,
2008 typename _UniformRandomNumberGenerator>
2010 std::chi_squared_distribution<_RealType>::
2011 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2012 _UniformRandomNumberGenerator& __urng)
2014 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2016 *__f++ = 2 * _M_gd(__urng);
2019 template<typename _RealType>
2020 template<typename _ForwardIterator,
2021 typename _UniformRandomNumberGenerator>
2023 std::chi_squared_distribution<_RealType>::
2024 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2025 _UniformRandomNumberGenerator& __urng,
2027 std::gamma_distribution<result_type>::param_type& __p)
2029 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2031 *__f++ = 2 * _M_gd(__urng, __p);
2034 template<typename _RealType, typename _CharT, typename _Traits>
2035 std::basic_ostream<_CharT, _Traits>&
2036 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2037 const chi_squared_distribution<_RealType>& __x)
2039 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2040 typedef typename __ostream_type::ios_base __ios_base;
2042 const typename __ios_base::fmtflags __flags = __os.flags();
2043 const _CharT __fill = __os.fill();
2044 const std::streamsize __precision = __os.precision();
2045 const _CharT __space = __os.widen(' ');
2046 __os.flags(__ios_base::scientific | __ios_base::left);
2048 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2050 __os << __x.n() << __space << __x._M_gd;
2052 __os.flags(__flags);
2054 __os.precision(__precision);
2058 template<typename _RealType, typename _CharT, typename _Traits>
2059 std::basic_istream<_CharT, _Traits>&
2060 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2061 chi_squared_distribution<_RealType>& __x)
2063 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2064 typedef typename __istream_type::ios_base __ios_base;
2066 const typename __ios_base::fmtflags __flags = __is.flags();
2067 __is.flags(__ios_base::dec | __ios_base::skipws);
2070 __is >> __n >> __x._M_gd;
2071 __x.param(typename chi_squared_distribution<_RealType>::
2074 __is.flags(__flags);
2079 template<typename _RealType>
2080 template<typename _UniformRandomNumberGenerator>
2081 typename cauchy_distribution<_RealType>::result_type
2082 cauchy_distribution<_RealType>::
2083 operator()(_UniformRandomNumberGenerator& __urng,
2084 const param_type& __p)
2086 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2093 const _RealType __pi = 3.1415926535897932384626433832795029L;
2094 return __p.a() + __p.b() * std::tan(__pi * __u);
2097 template<typename _RealType>
2098 template<typename _ForwardIterator,
2099 typename _UniformRandomNumberGenerator>
2101 cauchy_distribution<_RealType>::
2102 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2103 _UniformRandomNumberGenerator& __urng,
2104 const param_type& __p)
2106 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2107 const _RealType __pi = 3.1415926535897932384626433832795029L;
2108 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2117 *__f++ = __p.a() + __p.b() * std::tan(__pi * __u);
2121 template<typename _RealType, typename _CharT, typename _Traits>
2122 std::basic_ostream<_CharT, _Traits>&
2123 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2124 const cauchy_distribution<_RealType>& __x)
2126 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2127 typedef typename __ostream_type::ios_base __ios_base;
2129 const typename __ios_base::fmtflags __flags = __os.flags();
2130 const _CharT __fill = __os.fill();
2131 const std::streamsize __precision = __os.precision();
2132 const _CharT __space = __os.widen(' ');
2133 __os.flags(__ios_base::scientific | __ios_base::left);
2135 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2137 __os << __x.a() << __space << __x.b();
2139 __os.flags(__flags);
2141 __os.precision(__precision);
2145 template<typename _RealType, typename _CharT, typename _Traits>
2146 std::basic_istream<_CharT, _Traits>&
2147 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2148 cauchy_distribution<_RealType>& __x)
2150 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2151 typedef typename __istream_type::ios_base __ios_base;
2153 const typename __ios_base::fmtflags __flags = __is.flags();
2154 __is.flags(__ios_base::dec | __ios_base::skipws);
2158 __x.param(typename cauchy_distribution<_RealType>::
2159 param_type(__a, __b));
2161 __is.flags(__flags);
2166 template<typename _RealType>
2167 template<typename _ForwardIterator,
2168 typename _UniformRandomNumberGenerator>
2170 std::fisher_f_distribution<_RealType>::
2171 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2172 _UniformRandomNumberGenerator& __urng)
2174 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2176 *__f++ = ((_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()));
2179 template<typename _RealType>
2180 template<typename _ForwardIterator,
2181 typename _UniformRandomNumberGenerator>
2183 std::fisher_f_distribution<_RealType>::
2184 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2185 _UniformRandomNumberGenerator& __urng,
2186 const param_type& __p)
2188 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2189 typedef typename std::gamma_distribution<result_type>::param_type
2191 param_type __p1(__p.m() / 2);
2192 param_type __p2(__p.n() / 2);
2194 *__f++ = ((_M_gd_x(__urng, __p1) * n())
2195 / (_M_gd_y(__urng, __p2) * m()));
2198 template<typename _RealType, typename _CharT, typename _Traits>
2199 std::basic_ostream<_CharT, _Traits>&
2200 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2201 const fisher_f_distribution<_RealType>& __x)
2203 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2204 typedef typename __ostream_type::ios_base __ios_base;
2206 const typename __ios_base::fmtflags __flags = __os.flags();
2207 const _CharT __fill = __os.fill();
2208 const std::streamsize __precision = __os.precision();
2209 const _CharT __space = __os.widen(' ');
2210 __os.flags(__ios_base::scientific | __ios_base::left);
2212 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2214 __os << __x.m() << __space << __x.n()
2215 << __space << __x._M_gd_x << __space << __x._M_gd_y;
2217 __os.flags(__flags);
2219 __os.precision(__precision);
2223 template<typename _RealType, typename _CharT, typename _Traits>
2224 std::basic_istream<_CharT, _Traits>&
2225 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2226 fisher_f_distribution<_RealType>& __x)
2228 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2229 typedef typename __istream_type::ios_base __ios_base;
2231 const typename __ios_base::fmtflags __flags = __is.flags();
2232 __is.flags(__ios_base::dec | __ios_base::skipws);
2235 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
2236 __x.param(typename fisher_f_distribution<_RealType>::
2237 param_type(__m, __n));
2239 __is.flags(__flags);
2244 template<typename _RealType>
2245 template<typename _ForwardIterator,
2246 typename _UniformRandomNumberGenerator>
2248 std::student_t_distribution<_RealType>::
2249 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2250 _UniformRandomNumberGenerator& __urng)
2252 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2254 *__f++ = _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng));
2257 template<typename _RealType>
2258 template<typename _ForwardIterator,
2259 typename _UniformRandomNumberGenerator>
2261 std::student_t_distribution<_RealType>::
2262 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2263 _UniformRandomNumberGenerator& __urng,
2264 const param_type& __p)
2266 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2267 typename std::gamma_distribution<result_type>::param_type
2268 __p2(__p.n() / 2, 2);
2270 *__f++ = _M_nd(__urng) * std::sqrt(__p.n() / _M_gd(__urng, __p2));
2273 template<typename _RealType, typename _CharT, typename _Traits>
2274 std::basic_ostream<_CharT, _Traits>&
2275 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2276 const student_t_distribution<_RealType>& __x)
2278 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2279 typedef typename __ostream_type::ios_base __ios_base;
2281 const typename __ios_base::fmtflags __flags = __os.flags();
2282 const _CharT __fill = __os.fill();
2283 const std::streamsize __precision = __os.precision();
2284 const _CharT __space = __os.widen(' ');
2285 __os.flags(__ios_base::scientific | __ios_base::left);
2287 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2289 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
2291 __os.flags(__flags);
2293 __os.precision(__precision);
2297 template<typename _RealType, typename _CharT, typename _Traits>
2298 std::basic_istream<_CharT, _Traits>&
2299 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2300 student_t_distribution<_RealType>& __x)
2302 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2303 typedef typename __istream_type::ios_base __ios_base;
2305 const typename __ios_base::fmtflags __flags = __is.flags();
2306 __is.flags(__ios_base::dec | __ios_base::skipws);
2309 __is >> __n >> __x._M_nd >> __x._M_gd;
2310 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
2312 __is.flags(__flags);
2317 template<typename _RealType>
2319 gamma_distribution<_RealType>::param_type::
2322 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2324 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2325 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2329 * Marsaglia, G. and Tsang, W. W.
2330 * "A Simple Method for Generating Gamma Variables"
2331 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2333 template<typename _RealType>
2334 template<typename _UniformRandomNumberGenerator>
2335 typename gamma_distribution<_RealType>::result_type
2336 gamma_distribution<_RealType>::
2337 operator()(_UniformRandomNumberGenerator& __urng,
2338 const param_type& __param)
2340 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2343 result_type __u, __v, __n;
2344 const result_type __a1 = (__param._M_malpha
2345 - _RealType(1.0) / _RealType(3.0));
2351 __n = _M_nd(__urng);
2352 __v = result_type(1.0) + __param._M_a2 * __n;
2356 __v = __v * __v * __v;
2359 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2360 && (std::log(__u) > (0.5 * __n * __n + __a1
2361 * (1.0 - __v + std::log(__v)))));
2363 if (__param.alpha() == __param._M_malpha)
2364 return __a1 * __v * __param.beta();
2371 return (std::pow(__u, result_type(1.0) / __param.alpha())
2372 * __a1 * __v * __param.beta());
2376 template<typename _RealType>
2377 template<typename _ForwardIterator,
2378 typename _UniformRandomNumberGenerator>
2380 gamma_distribution<_RealType>::
2381 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2382 _UniformRandomNumberGenerator& __urng,
2383 const param_type& __param)
2385 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2386 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2389 result_type __u, __v, __n;
2390 const result_type __a1 = (__param._M_malpha
2391 - _RealType(1.0) / _RealType(3.0));
2393 if (__param.alpha() == __param._M_malpha)
2400 __n = _M_nd(__urng);
2401 __v = result_type(1.0) + __param._M_a2 * __n;
2405 __v = __v * __v * __v;
2408 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2409 && (std::log(__u) > (0.5 * __n * __n + __a1
2410 * (1.0 - __v + std::log(__v)))));
2412 *__f++ = __a1 * __v * __param.beta();
2421 __n = _M_nd(__urng);
2422 __v = result_type(1.0) + __param._M_a2 * __n;
2426 __v = __v * __v * __v;
2429 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
2430 && (std::log(__u) > (0.5 * __n * __n + __a1
2431 * (1.0 - __v + std::log(__v)))));
2437 *__f++ = (std::pow(__u, result_type(1.0) / __param.alpha())
2438 * __a1 * __v * __param.beta());
2442 template<typename _RealType, typename _CharT, typename _Traits>
2443 std::basic_ostream<_CharT, _Traits>&
2444 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2445 const gamma_distribution<_RealType>& __x)
2447 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2448 typedef typename __ostream_type::ios_base __ios_base;
2450 const typename __ios_base::fmtflags __flags = __os.flags();
2451 const _CharT __fill = __os.fill();
2452 const std::streamsize __precision = __os.precision();
2453 const _CharT __space = __os.widen(' ');
2454 __os.flags(__ios_base::scientific | __ios_base::left);
2456 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2458 __os << __x.alpha() << __space << __x.beta()
2459 << __space << __x._M_nd;
2461 __os.flags(__flags);
2463 __os.precision(__precision);
2467 template<typename _RealType, typename _CharT, typename _Traits>
2468 std::basic_istream<_CharT, _Traits>&
2469 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2470 gamma_distribution<_RealType>& __x)
2472 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2473 typedef typename __istream_type::ios_base __ios_base;
2475 const typename __ios_base::fmtflags __flags = __is.flags();
2476 __is.flags(__ios_base::dec | __ios_base::skipws);
2478 _RealType __alpha_val, __beta_val;
2479 __is >> __alpha_val >> __beta_val >> __x._M_nd;
2480 __x.param(typename gamma_distribution<_RealType>::
2481 param_type(__alpha_val, __beta_val));
2483 __is.flags(__flags);
2488 template<typename _RealType>
2489 template<typename _UniformRandomNumberGenerator>
2490 typename weibull_distribution<_RealType>::result_type
2491 weibull_distribution<_RealType>::
2492 operator()(_UniformRandomNumberGenerator& __urng,
2493 const param_type& __p)
2495 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2497 return __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
2498 result_type(1) / __p.a());
2501 template<typename _RealType>
2502 template<typename _ForwardIterator,
2503 typename _UniformRandomNumberGenerator>
2505 weibull_distribution<_RealType>::
2506 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2507 _UniformRandomNumberGenerator& __urng,
2508 const param_type& __p)
2510 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2511 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2513 auto __inv_a = result_type(1) / __p.a();
2516 *__f++ = __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
2520 template<typename _RealType, typename _CharT, typename _Traits>
2521 std::basic_ostream<_CharT, _Traits>&
2522 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2523 const weibull_distribution<_RealType>& __x)
2525 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2526 typedef typename __ostream_type::ios_base __ios_base;
2528 const typename __ios_base::fmtflags __flags = __os.flags();
2529 const _CharT __fill = __os.fill();
2530 const std::streamsize __precision = __os.precision();
2531 const _CharT __space = __os.widen(' ');
2532 __os.flags(__ios_base::scientific | __ios_base::left);
2534 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2536 __os << __x.a() << __space << __x.b();
2538 __os.flags(__flags);
2540 __os.precision(__precision);
2544 template<typename _RealType, typename _CharT, typename _Traits>
2545 std::basic_istream<_CharT, _Traits>&
2546 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2547 weibull_distribution<_RealType>& __x)
2549 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2550 typedef typename __istream_type::ios_base __ios_base;
2552 const typename __ios_base::fmtflags __flags = __is.flags();
2553 __is.flags(__ios_base::dec | __ios_base::skipws);
2557 __x.param(typename weibull_distribution<_RealType>::
2558 param_type(__a, __b));
2560 __is.flags(__flags);
2565 template<typename _RealType>
2566 template<typename _UniformRandomNumberGenerator>
2567 typename extreme_value_distribution<_RealType>::result_type
2568 extreme_value_distribution<_RealType>::
2569 operator()(_UniformRandomNumberGenerator& __urng,
2570 const param_type& __p)
2572 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2574 return __p.a() - __p.b() * std::log(-std::log(result_type(1)
2578 template<typename _RealType>
2579 template<typename _ForwardIterator,
2580 typename _UniformRandomNumberGenerator>
2582 extreme_value_distribution<_RealType>::
2583 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2584 _UniformRandomNumberGenerator& __urng,
2585 const param_type& __p)
2587 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2588 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2592 *__f++ = __p.a() - __p.b() * std::log(-std::log(result_type(1)
2596 template<typename _RealType, typename _CharT, typename _Traits>
2597 std::basic_ostream<_CharT, _Traits>&
2598 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2599 const extreme_value_distribution<_RealType>& __x)
2601 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2602 typedef typename __ostream_type::ios_base __ios_base;
2604 const typename __ios_base::fmtflags __flags = __os.flags();
2605 const _CharT __fill = __os.fill();
2606 const std::streamsize __precision = __os.precision();
2607 const _CharT __space = __os.widen(' ');
2608 __os.flags(__ios_base::scientific | __ios_base::left);
2610 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2612 __os << __x.a() << __space << __x.b();
2614 __os.flags(__flags);
2616 __os.precision(__precision);
2620 template<typename _RealType, typename _CharT, typename _Traits>
2621 std::basic_istream<_CharT, _Traits>&
2622 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2623 extreme_value_distribution<_RealType>& __x)
2625 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2626 typedef typename __istream_type::ios_base __ios_base;
2628 const typename __ios_base::fmtflags __flags = __is.flags();
2629 __is.flags(__ios_base::dec | __ios_base::skipws);
2633 __x.param(typename extreme_value_distribution<_RealType>::
2634 param_type(__a, __b));
2636 __is.flags(__flags);
2641 template<typename _IntType>
2643 discrete_distribution<_IntType>::param_type::
2646 if (_M_prob.size() < 2)
2652 const double __sum = std::accumulate(_M_prob.begin(),
2653 _M_prob.end(), 0.0);
2654 // Now normalize the probabilites.
2655 __detail::__normalize(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2657 // Accumulate partial sums.
2658 _M_cp.reserve(_M_prob.size());
2659 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2660 std::back_inserter(_M_cp));
2661 // Make sure the last cumulative probability is one.
2662 _M_cp[_M_cp.size() - 1] = 1.0;
2665 template<typename _IntType>
2666 template<typename _Func>
2667 discrete_distribution<_IntType>::param_type::
2668 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2669 : _M_prob(), _M_cp()
2671 const size_t __n = __nw == 0 ? 1 : __nw;
2672 const double __delta = (__xmax - __xmin) / __n;
2674 _M_prob.reserve(__n);
2675 for (size_t __k = 0; __k < __nw; ++__k)
2676 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2681 template<typename _IntType>
2682 template<typename _UniformRandomNumberGenerator>
2683 typename discrete_distribution<_IntType>::result_type
2684 discrete_distribution<_IntType>::
2685 operator()(_UniformRandomNumberGenerator& __urng,
2686 const param_type& __param)
2688 if (__param._M_cp.empty())
2689 return result_type(0);
2691 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2694 const double __p = __aurng();
2695 auto __pos = std::lower_bound(__param._M_cp.begin(),
2696 __param._M_cp.end(), __p);
2698 return __pos - __param._M_cp.begin();
2701 template<typename _IntType>
2702 template<typename _ForwardIterator,
2703 typename _UniformRandomNumberGenerator>
2705 discrete_distribution<_IntType>::
2706 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2707 _UniformRandomNumberGenerator& __urng,
2708 const param_type& __param)
2710 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2712 if (__param._M_cp.empty())
2715 *__f++ = result_type(0);
2719 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2724 const double __p = __aurng();
2725 auto __pos = std::lower_bound(__param._M_cp.begin(),
2726 __param._M_cp.end(), __p);
2728 *__f++ = __pos - __param._M_cp.begin();
2732 template<typename _IntType, typename _CharT, typename _Traits>
2733 std::basic_ostream<_CharT, _Traits>&
2734 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2735 const discrete_distribution<_IntType>& __x)
2737 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2738 typedef typename __ostream_type::ios_base __ios_base;
2740 const typename __ios_base::fmtflags __flags = __os.flags();
2741 const _CharT __fill = __os.fill();
2742 const std::streamsize __precision = __os.precision();
2743 const _CharT __space = __os.widen(' ');
2744 __os.flags(__ios_base::scientific | __ios_base::left);
2746 __os.precision(std::numeric_limits<double>::max_digits10);
2748 std::vector<double> __prob = __x.probabilities();
2749 __os << __prob.size();
2750 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2751 __os << __space << *__dit;
2753 __os.flags(__flags);
2755 __os.precision(__precision);
2759 template<typename _IntType, typename _CharT, typename _Traits>
2760 std::basic_istream<_CharT, _Traits>&
2761 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2762 discrete_distribution<_IntType>& __x)
2764 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2765 typedef typename __istream_type::ios_base __ios_base;
2767 const typename __ios_base::fmtflags __flags = __is.flags();
2768 __is.flags(__ios_base::dec | __ios_base::skipws);
2773 std::vector<double> __prob_vec;
2774 __prob_vec.reserve(__n);
2775 for (; __n != 0; --__n)
2779 __prob_vec.push_back(__prob);
2782 __x.param(typename discrete_distribution<_IntType>::
2783 param_type(__prob_vec.begin(), __prob_vec.end()));
2785 __is.flags(__flags);
2790 template<typename _RealType>
2792 piecewise_constant_distribution<_RealType>::param_type::
2795 if (_M_int.size() < 2
2796 || (_M_int.size() == 2
2797 && _M_int[0] == _RealType(0)
2798 && _M_int[1] == _RealType(1)))
2805 const double __sum = std::accumulate(_M_den.begin(),
2808 __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
2811 _M_cp.reserve(_M_den.size());
2812 std::partial_sum(_M_den.begin(), _M_den.end(),
2813 std::back_inserter(_M_cp));
2815 // Make sure the last cumulative probability is one.
2816 _M_cp[_M_cp.size() - 1] = 1.0;
2818 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2819 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2822 template<typename _RealType>
2823 template<typename _InputIteratorB, typename _InputIteratorW>
2824 piecewise_constant_distribution<_RealType>::param_type::
2825 param_type(_InputIteratorB __bbegin,
2826 _InputIteratorB __bend,
2827 _InputIteratorW __wbegin)
2828 : _M_int(), _M_den(), _M_cp()
2830 if (__bbegin != __bend)
2834 _M_int.push_back(*__bbegin);
2836 if (__bbegin == __bend)
2839 _M_den.push_back(*__wbegin);
2847 template<typename _RealType>
2848 template<typename _Func>
2849 piecewise_constant_distribution<_RealType>::param_type::
2850 param_type(initializer_list<_RealType> __bl, _Func __fw)
2851 : _M_int(), _M_den(), _M_cp()
2853 _M_int.reserve(__bl.size());
2854 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2855 _M_int.push_back(*__biter);
2857 _M_den.reserve(_M_int.size() - 1);
2858 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2859 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2864 template<typename _RealType>
2865 template<typename _Func>
2866 piecewise_constant_distribution<_RealType>::param_type::
2867 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2868 : _M_int(), _M_den(), _M_cp()
2870 const size_t __n = __nw == 0 ? 1 : __nw;
2871 const _RealType __delta = (__xmax - __xmin) / __n;
2873 _M_int.reserve(__n + 1);
2874 for (size_t __k = 0; __k <= __nw; ++__k)
2875 _M_int.push_back(__xmin + __k * __delta);
2877 _M_den.reserve(__n);
2878 for (size_t __k = 0; __k < __nw; ++__k)
2879 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2884 template<typename _RealType>
2885 template<typename _UniformRandomNumberGenerator>
2886 typename piecewise_constant_distribution<_RealType>::result_type
2887 piecewise_constant_distribution<_RealType>::
2888 operator()(_UniformRandomNumberGenerator& __urng,
2889 const param_type& __param)
2891 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2894 const double __p = __aurng();
2895 if (__param._M_cp.empty())
2898 auto __pos = std::lower_bound(__param._M_cp.begin(),
2899 __param._M_cp.end(), __p);
2900 const size_t __i = __pos - __param._M_cp.begin();
2902 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2904 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2907 template<typename _RealType>
2908 template<typename _ForwardIterator,
2909 typename _UniformRandomNumberGenerator>
2911 piecewise_constant_distribution<_RealType>::
2912 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2913 _UniformRandomNumberGenerator& __urng,
2914 const param_type& __param)
2916 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2917 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2920 if (__param._M_cp.empty())
2929 const double __p = __aurng();
2931 auto __pos = std::lower_bound(__param._M_cp.begin(),
2932 __param._M_cp.end(), __p);
2933 const size_t __i = __pos - __param._M_cp.begin();
2935 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2937 *__f++ = (__param._M_int[__i]
2938 + (__p - __pref) / __param._M_den[__i]);
2942 template<typename _RealType, typename _CharT, typename _Traits>
2943 std::basic_ostream<_CharT, _Traits>&
2944 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2945 const piecewise_constant_distribution<_RealType>& __x)
2947 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2948 typedef typename __ostream_type::ios_base __ios_base;
2950 const typename __ios_base::fmtflags __flags = __os.flags();
2951 const _CharT __fill = __os.fill();
2952 const std::streamsize __precision = __os.precision();
2953 const _CharT __space = __os.widen(' ');
2954 __os.flags(__ios_base::scientific | __ios_base::left);
2956 __os.precision(std::numeric_limits<_RealType>::max_digits10);
2958 std::vector<_RealType> __int = __x.intervals();
2959 __os << __int.size() - 1;
2961 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2962 __os << __space << *__xit;
2964 std::vector<double> __den = __x.densities();
2965 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2966 __os << __space << *__dit;
2968 __os.flags(__flags);
2970 __os.precision(__precision);
2974 template<typename _RealType, typename _CharT, typename _Traits>
2975 std::basic_istream<_CharT, _Traits>&
2976 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2977 piecewise_constant_distribution<_RealType>& __x)
2979 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2980 typedef typename __istream_type::ios_base __ios_base;
2982 const typename __ios_base::fmtflags __flags = __is.flags();
2983 __is.flags(__ios_base::dec | __ios_base::skipws);
2988 std::vector<_RealType> __int_vec;
2989 __int_vec.reserve(__n + 1);
2990 for (size_t __i = 0; __i <= __n; ++__i)
2994 __int_vec.push_back(__int);
2997 std::vector<double> __den_vec;
2998 __den_vec.reserve(__n);
2999 for (size_t __i = 0; __i < __n; ++__i)
3003 __den_vec.push_back(__den);
3006 __x.param(typename piecewise_constant_distribution<_RealType>::
3007 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
3009 __is.flags(__flags);
3014 template<typename _RealType>
3016 piecewise_linear_distribution<_RealType>::param_type::
3019 if (_M_int.size() < 2
3020 || (_M_int.size() == 2
3021 && _M_int[0] == _RealType(0)
3022 && _M_int[1] == _RealType(1)
3023 && _M_den[0] == _M_den[1]))
3031 _M_cp.reserve(_M_int.size() - 1);
3032 _M_m.reserve(_M_int.size() - 1);
3033 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
3035 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
3036 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
3037 _M_cp.push_back(__sum);
3038 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
3041 // Now normalize the densities...
3042 __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
3044 // ... and partial sums...
3045 __detail::__normalize(_M_cp.begin(), _M_cp.end(), _M_cp.begin(), __sum);
3047 __detail::__normalize(_M_m.begin(), _M_m.end(), _M_m.begin(), __sum);
3049 // Make sure the last cumulative probablility is one.
3050 _M_cp[_M_cp.size() - 1] = 1.0;
3053 template<typename _RealType>
3054 template<typename _InputIteratorB, typename _InputIteratorW>
3055 piecewise_linear_distribution<_RealType>::param_type::
3056 param_type(_InputIteratorB __bbegin,
3057 _InputIteratorB __bend,
3058 _InputIteratorW __wbegin)
3059 : _M_int(), _M_den(), _M_cp(), _M_m()
3061 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
3063 _M_int.push_back(*__bbegin);
3064 _M_den.push_back(*__wbegin);
3070 template<typename _RealType>
3071 template<typename _Func>
3072 piecewise_linear_distribution<_RealType>::param_type::
3073 param_type(initializer_list<_RealType> __bl, _Func __fw)
3074 : _M_int(), _M_den(), _M_cp(), _M_m()
3076 _M_int.reserve(__bl.size());
3077 _M_den.reserve(__bl.size());
3078 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
3080 _M_int.push_back(*__biter);
3081 _M_den.push_back(__fw(*__biter));
3087 template<typename _RealType>
3088 template<typename _Func>
3089 piecewise_linear_distribution<_RealType>::param_type::
3090 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
3091 : _M_int(), _M_den(), _M_cp(), _M_m()
3093 const size_t __n = __nw == 0 ? 1 : __nw;
3094 const _RealType __delta = (__xmax - __xmin) / __n;
3096 _M_int.reserve(__n + 1);
3097 _M_den.reserve(__n + 1);
3098 for (size_t __k = 0; __k <= __nw; ++__k)
3100 _M_int.push_back(__xmin + __k * __delta);
3101 _M_den.push_back(__fw(_M_int[__k] + __delta));
3107 template<typename _RealType>
3108 template<typename _UniformRandomNumberGenerator>
3109 typename piecewise_linear_distribution<_RealType>::result_type
3110 piecewise_linear_distribution<_RealType>::
3111 operator()(_UniformRandomNumberGenerator& __urng,
3112 const param_type& __param)
3114 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3117 const double __p = __aurng();
3118 if (__param._M_cp.empty())
3121 auto __pos = std::lower_bound(__param._M_cp.begin(),
3122 __param._M_cp.end(), __p);
3123 const size_t __i = __pos - __param._M_cp.begin();
3125 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3127 const double __a = 0.5 * __param._M_m[__i];
3128 const double __b = __param._M_den[__i];
3129 const double __cm = __p - __pref;
3131 _RealType __x = __param._M_int[__i];
3136 const double __d = __b * __b + 4.0 * __a * __cm;
3137 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
3143 template<typename _RealType>
3144 template<typename _ForwardIterator,
3145 typename _UniformRandomNumberGenerator>
3147 piecewise_linear_distribution<_RealType>::
3148 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3149 _UniformRandomNumberGenerator& __urng,
3150 const param_type& __param)
3152 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3153 // We could duplicate everything from operator()...
3155 *__f++ = this->operator()(__urng, __param);
3158 template<typename _RealType, typename _CharT, typename _Traits>
3159 std::basic_ostream<_CharT, _Traits>&
3160 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3161 const piecewise_linear_distribution<_RealType>& __x)
3163 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
3164 typedef typename __ostream_type::ios_base __ios_base;
3166 const typename __ios_base::fmtflags __flags = __os.flags();
3167 const _CharT __fill = __os.fill();
3168 const std::streamsize __precision = __os.precision();
3169 const _CharT __space = __os.widen(' ');
3170 __os.flags(__ios_base::scientific | __ios_base::left);
3172 __os.precision(std::numeric_limits<_RealType>::max_digits10);
3174 std::vector<_RealType> __int = __x.intervals();
3175 __os << __int.size() - 1;
3177 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
3178 __os << __space << *__xit;
3180 std::vector<double> __den = __x.densities();
3181 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
3182 __os << __space << *__dit;
3184 __os.flags(__flags);
3186 __os.precision(__precision);
3190 template<typename _RealType, typename _CharT, typename _Traits>
3191 std::basic_istream<_CharT, _Traits>&
3192 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3193 piecewise_linear_distribution<_RealType>& __x)
3195 typedef std::basic_istream<_CharT, _Traits> __istream_type;
3196 typedef typename __istream_type::ios_base __ios_base;
3198 const typename __ios_base::fmtflags __flags = __is.flags();
3199 __is.flags(__ios_base::dec | __ios_base::skipws);
3204 std::vector<_RealType> __int_vec;
3205 __int_vec.reserve(__n + 1);
3206 for (size_t __i = 0; __i <= __n; ++__i)
3210 __int_vec.push_back(__int);
3213 std::vector<double> __den_vec;
3214 __den_vec.reserve(__n + 1);
3215 for (size_t __i = 0; __i <= __n; ++__i)
3219 __den_vec.push_back(__den);
3222 __x.param(typename piecewise_linear_distribution<_RealType>::
3223 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
3225 __is.flags(__flags);
3230 template<typename _IntType>
3231 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
3233 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
3234 _M_v.push_back(__detail::__mod<result_type,
3235 __detail::_Shift<result_type, 32>::__value>(*__iter));
3238 template<typename _InputIterator>
3239 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
3241 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
3242 _M_v.push_back(__detail::__mod<result_type,
3243 __detail::_Shift<result_type, 32>::__value>(*__iter));
3246 template<typename _RandomAccessIterator>
3248 seed_seq::generate(_RandomAccessIterator __begin,
3249 _RandomAccessIterator __end)
3251 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3254 if (__begin == __end)
3257 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
3259 const size_t __n = __end - __begin;
3260 const size_t __s = _M_v.size();
3261 const size_t __t = (__n >= 623) ? 11
3266 const size_t __p = (__n - __t) / 2;
3267 const size_t __q = __p + __t;
3268 const size_t __m = std::max(size_t(__s + 1), __n);
3270 for (size_t __k = 0; __k < __m; ++__k)
3272 _Type __arg = (__begin[__k % __n]
3273 ^ __begin[(__k + __p) % __n]
3274 ^ __begin[(__k - 1) % __n]);
3275 _Type __r1 = __arg ^ (__arg >> 27);
3276 __r1 = __detail::__mod<_Type,
3277 __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
3281 else if (__k <= __s)
3282 __r2 += __k % __n + _M_v[__k - 1];
3285 __r2 = __detail::__mod<_Type,
3286 __detail::_Shift<_Type, 32>::__value>(__r2);
3287 __begin[(__k + __p) % __n] += __r1;
3288 __begin[(__k + __q) % __n] += __r2;
3289 __begin[__k % __n] = __r2;
3292 for (size_t __k = __m; __k < __m + __n; ++__k)
3294 _Type __arg = (__begin[__k % __n]
3295 + __begin[(__k + __p) % __n]
3296 + __begin[(__k - 1) % __n]);
3297 _Type __r3 = __arg ^ (__arg >> 27);
3298 __r3 = __detail::__mod<_Type,
3299 __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
3300 _Type __r4 = __r3 - __k % __n;
3301 __r4 = __detail::__mod<_Type,
3302 __detail::_Shift<_Type, 32>::__value>(__r4);
3303 __begin[(__k + __p) % __n] ^= __r3;
3304 __begin[(__k + __q) % __n] ^= __r4;
3305 __begin[__k % __n] = __r4;
3309 template<typename _RealType, size_t __bits,
3310 typename _UniformRandomNumberGenerator>
3312 generate_canonical(_UniformRandomNumberGenerator& __urng)
3314 static_assert(std::is_floating_point<_RealType>::value,
3315 "template argument must be a floating point type");
3318 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
3320 const long double __r = static_cast<long double>(__urng.max())
3321 - static_cast<long double>(__urng.min()) + 1.0L;
3322 const size_t __log2r = std::log(__r) / std::log(2.0L);
3323 const size_t __m = std::max<size_t>(1UL,
3324 (__b + __log2r - 1UL) / __log2r);
3326 _RealType __sum = _RealType(0);
3327 _RealType __tmp = _RealType(1);
3328 for (size_t __k = __m; __k != 0; --__k)
3330 __sum += _RealType(__urng() - __urng.min()) * __tmp;
3333 __ret = __sum / __tmp;
3334 if (__builtin_expect(__ret >= _RealType(1), 0))
3336 #if _GLIBCXX_USE_C99_MATH_TR1
3337 __ret = std::nextafter(_RealType(1), _RealType(0));
3339 __ret = _RealType(1)
3340 - std::numeric_limits<_RealType>::epsilon() / _RealType(2);
3346 _GLIBCXX_END_NAMESPACE_VERSION