libstdc++
bits/random.tcc
Go to the documentation of this file.
1// random number generation (out of line) -*- C++ -*-
2
3// Copyright (C) 2009-2025 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
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}
28 */
29
30#ifndef _RANDOM_TCC
31#define _RANDOM_TCC 1
32
33#include <numeric> // std::accumulate and std::partial_sum
34
35namespace std _GLIBCXX_VISIBILITY(default)
36{
37_GLIBCXX_BEGIN_NAMESPACE_VERSION
38
39 /// @cond undocumented
40 // (Further) implementation-space details.
41 namespace __detail
42 {
43 // General case for x = (ax + c) mod m -- use Schrage's algorithm
44 // to avoid integer overflow.
45 //
46 // Preconditions: a > 0, m > 0.
47 //
48 // Note: only works correctly for __m % __a < __m / __a.
49 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
50 _Tp
51 _Mod<_Tp, __m, __a, __c, false, true>::
52 __calc(_Tp __x)
53 {
54 if (__a == 1)
55 __x %= __m;
56 else
57 {
58 static const _Tp __q = __m / __a;
59 static const _Tp __r = __m % __a;
60
61 _Tp __t1 = __a * (__x % __q);
62 _Tp __t2 = __r * (__x / __q);
63 if (__t1 >= __t2)
64 __x = __t1 - __t2;
65 else
66 __x = __m - __t2 + __t1;
67 }
68
69 if (__c != 0)
70 {
71 const _Tp __d = __m - __x;
72 if (__d > __c)
73 __x += __c;
74 else
75 __x = __c - __d;
76 }
77 return __x;
78 }
79
80 template<typename _InputIterator, typename _OutputIterator,
81 typename _Tp>
82 _OutputIterator
83 __normalize(_InputIterator __first, _InputIterator __last,
84 _OutputIterator __result, const _Tp& __factor)
85 {
86 for (; __first != __last; ++__first, ++__result)
87 *__result = *__first / __factor;
88 return __result;
89 }
90
91 } // namespace __detail
92 /// @endcond
93
94#if ! __cpp_inline_variables
95 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
96 constexpr _UIntType
98
99 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
100 constexpr _UIntType
102
103 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
104 constexpr _UIntType
106
107 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
108 constexpr _UIntType
109 linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
110#endif
111
112 /**
113 * Seeds the LCR with integral value @p __s, adjusted so that the
114 * ring identity is never a member of the convergence set.
115 */
116 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
117 void
120 {
121 if ((__detail::__mod<_UIntType, __m>(__c) == 0)
122 && (__detail::__mod<_UIntType, __m>(__s) == 0))
123 _M_x = 1;
124 else
125 _M_x = __detail::__mod<_UIntType, __m>(__s);
126 }
127
128 /**
129 * Seeds the LCR engine with a value generated by @p __q.
130 */
131 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
132 template<typename _Sseq>
133 auto
135 seed(_Sseq& __q)
136 -> _If_seed_seq<_Sseq>
137 {
138 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
139 : std::__lg(__m);
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)
146 {
147 __sum += __arr[__j + 3] * __factor;
148 __factor *= __detail::_Shift<_UIntType, 32>::__value;
149 }
150 seed(__sum);
151 }
152
153 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
154 typename _CharT, typename _Traits>
157 const linear_congruential_engine<_UIntType,
158 __a, __c, __m>& __lcr)
159 {
160 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
161
162 const typename __ios_base::fmtflags __flags = __os.flags();
163 const _CharT __fill = __os.fill();
164 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
165 __os.fill(__os.widen(' '));
166
167 __os << __lcr._M_x;
168
169 __os.flags(__flags);
170 __os.fill(__fill);
171 return __os;
172 }
173
174 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
175 typename _CharT, typename _Traits>
178 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
179 {
180 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
181
182 const typename __ios_base::fmtflags __flags = __is.flags();
183 __is.flags(__ios_base::dec);
184
185 __is >> __lcr._M_x;
186
187 __is.flags(__flags);
188 return __is;
189 }
190
191#if ! __cpp_inline_variables
192 template<typename _UIntType,
193 size_t __w, size_t __n, size_t __m, size_t __r,
194 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
195 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
196 _UIntType __f>
197 constexpr size_t
198 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
199 __s, __b, __t, __c, __l, __f>::word_size;
200
201 template<typename _UIntType,
202 size_t __w, size_t __n, size_t __m, size_t __r,
203 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
204 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
205 _UIntType __f>
206 constexpr size_t
207 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
208 __s, __b, __t, __c, __l, __f>::state_size;
209
210 template<typename _UIntType,
211 size_t __w, size_t __n, size_t __m, size_t __r,
212 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
213 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
214 _UIntType __f>
215 constexpr size_t
216 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
217 __s, __b, __t, __c, __l, __f>::shift_size;
218
219 template<typename _UIntType,
220 size_t __w, size_t __n, size_t __m, size_t __r,
221 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
222 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
223 _UIntType __f>
224 constexpr size_t
225 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
226 __s, __b, __t, __c, __l, __f>::mask_bits;
227
228 template<typename _UIntType,
229 size_t __w, size_t __n, size_t __m, size_t __r,
230 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
231 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
232 _UIntType __f>
233 constexpr _UIntType
234 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
235 __s, __b, __t, __c, __l, __f>::xor_mask;
236
237 template<typename _UIntType,
238 size_t __w, size_t __n, size_t __m, size_t __r,
239 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
240 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
241 _UIntType __f>
242 constexpr size_t
243 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
244 __s, __b, __t, __c, __l, __f>::tempering_u;
245
246 template<typename _UIntType,
247 size_t __w, size_t __n, size_t __m, size_t __r,
248 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
249 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
250 _UIntType __f>
251 constexpr _UIntType
252 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
253 __s, __b, __t, __c, __l, __f>::tempering_d;
254
255 template<typename _UIntType,
256 size_t __w, size_t __n, size_t __m, size_t __r,
257 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
258 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
259 _UIntType __f>
260 constexpr size_t
261 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
262 __s, __b, __t, __c, __l, __f>::tempering_s;
263
264 template<typename _UIntType,
265 size_t __w, size_t __n, size_t __m, size_t __r,
266 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
267 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
268 _UIntType __f>
269 constexpr _UIntType
270 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
271 __s, __b, __t, __c, __l, __f>::tempering_b;
272
273 template<typename _UIntType,
274 size_t __w, size_t __n, size_t __m, size_t __r,
275 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
276 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
277 _UIntType __f>
278 constexpr size_t
279 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
280 __s, __b, __t, __c, __l, __f>::tempering_t;
281
282 template<typename _UIntType,
283 size_t __w, size_t __n, size_t __m, size_t __r,
284 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
285 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
286 _UIntType __f>
287 constexpr _UIntType
288 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
289 __s, __b, __t, __c, __l, __f>::tempering_c;
290
291 template<typename _UIntType,
292 size_t __w, size_t __n, size_t __m, size_t __r,
293 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
294 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
295 _UIntType __f>
296 constexpr size_t
297 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
298 __s, __b, __t, __c, __l, __f>::tempering_l;
299
300 template<typename _UIntType,
301 size_t __w, size_t __n, size_t __m, size_t __r,
302 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
303 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
304 _UIntType __f>
305 constexpr _UIntType
306 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
307 __s, __b, __t, __c, __l, __f>::
308 initialization_multiplier;
309
310 template<typename _UIntType,
311 size_t __w, size_t __n, size_t __m, size_t __r,
312 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
313 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
314 _UIntType __f>
315 constexpr _UIntType
316 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
317 __s, __b, __t, __c, __l, __f>::default_seed;
318#endif
319
320 template<typename _UIntType,
321 size_t __w, size_t __n, size_t __m, size_t __r,
322 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
323 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
324 _UIntType __f>
325 void
326 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
327 __s, __b, __t, __c, __l, __f>::
328 seed(result_type __sd)
329 {
330 _M_x[0] = __detail::__mod<_UIntType,
331 __detail::_Shift<_UIntType, __w>::__value>(__sd);
332
333 for (size_t __i = 1; __i < state_size; ++__i)
334 {
335 _UIntType __x = _M_x[__i - 1];
336 __x ^= __x >> (__w - 2);
337 __x *= __f;
338 __x += __detail::__mod<_UIntType, __n>(__i);
339 _M_x[__i] = __detail::__mod<_UIntType,
340 __detail::_Shift<_UIntType, __w>::__value>(__x);
341 }
342 _M_p = state_size;
343 }
344
345 template<typename _UIntType,
346 size_t __w, size_t __n, size_t __m, size_t __r,
347 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
348 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
349 _UIntType __f>
350 template<typename _Sseq>
351 auto
352 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
353 __s, __b, __t, __c, __l, __f>::
354 seed(_Sseq& __q)
355 -> _If_seed_seq<_Sseq>
356 {
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);
361
362 bool __zero = true;
363 for (size_t __i = 0; __i < state_size; ++__i)
364 {
365 _UIntType __factor = 1u;
366 _UIntType __sum = 0u;
367 for (size_t __j = 0; __j < __k; ++__j)
368 {
369 __sum += __arr[__k * __i + __j] * __factor;
370 __factor *= __detail::_Shift<_UIntType, 32>::__value;
371 }
372 _M_x[__i] = __detail::__mod<_UIntType,
373 __detail::_Shift<_UIntType, __w>::__value>(__sum);
374
375 if (__zero)
376 {
377 if (__i == 0)
378 {
379 if ((_M_x[0] & __upper_mask) != 0u)
380 __zero = false;
381 }
382 else if (_M_x[__i] != 0u)
383 __zero = false;
384 }
385 }
386 if (__zero)
387 _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
388 _M_p = state_size;
389 }
390
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,
395 _UIntType __f>
396 void
397 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
398 __s, __b, __t, __c, __l, __f>::
399 _M_gen_rand(void)
400 {
401 const _UIntType __upper_mask = (~_UIntType()) << __r;
402 const _UIntType __lower_mask = ~__upper_mask;
403
404 for (size_t __k = 0; __k < (__n - __m); ++__k)
405 {
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));
410 }
411
412 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
413 {
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));
418 }
419
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));
424 _M_p = 0;
426
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,
431 _UIntType __f>
432 void
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)
436 {
437 while (__z > state_size - _M_p)
438 {
439 __z -= state_size - _M_p;
440 _M_gen_rand();
441 }
442 _M_p += __z;
443 }
444
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,
449 _UIntType __f>
450 typename
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>::
455 operator()()
456 {
457 // Reload the vector - cost is O(n) amortized over n calls.
458 if (_M_p >= state_size)
459 _M_gen_rand();
460
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;
466 __z ^= (__z >> __l);
467
468 return __z;
469 }
470
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>
478 const mersenne_twister_engine<_UIntType, __w, __n, __m,
479 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
480 {
481 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
482
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);
487 __os.fill(__space);
488
489 for (size_t __i = 0; __i < __n; ++__i)
490 __os << __x._M_x[__i] << __space;
491 __os << __x._M_p;
492
493 __os.flags(__flags);
494 __os.fill(__fill);
495 return __os;
496 }
497
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)
507 {
508 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
509
510 const typename __ios_base::fmtflags __flags = __is.flags();
511 __is.flags(__ios_base::dec | __ios_base::skipws);
512
513 for (size_t __i = 0; __i < __n; ++__i)
514 __is >> __x._M_x[__i];
515 __is >> __x._M_p;
516
517 __is.flags(__flags);
518 return __is;
519 }
520
521#if ! __cpp_inline_variables
522 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
523 constexpr size_t
524 subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
525
526 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
527 constexpr size_t
528 subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
529
530 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
531 constexpr size_t
532 subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
533
534 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
535 constexpr uint_least32_t
536 subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
537#endif
538
539 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
540 void
542 seed(result_type __value)
543 {
544 // _GLIBCXX_RESOLVE_LIB_DEFECTS
545 // 3809. Is std::subtract_with_carry_engine<uint16_t> supposed to work?
546 // 4014. LWG 3809 changes behavior of some existing code
548 __lcg(__value == 0u ? default_seed : __value % 2147483563u);
549
550 const size_t __n = (__w + 31) / 32;
551
552 for (size_t __i = 0; __i < long_lag; ++__i)
553 {
554 _UIntType __sum = 0u;
555 _UIntType __factor = 1u;
556 for (size_t __j = 0; __j < __n; ++__j)
557 {
558 __sum += __detail::__mod<uint_least32_t,
559 __detail::_Shift<uint_least32_t, 32>::__value>
560 (__lcg()) * __factor;
561 __factor *= __detail::_Shift<_UIntType, 32>::__value;
562 }
563 _M_x[__i] = __detail::__mod<_UIntType,
564 __detail::_Shift<_UIntType, __w>::__value>(__sum);
565 }
566 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
567 _M_p = 0;
568 }
569
570 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
571 template<typename _Sseq>
572 auto
574 seed(_Sseq& __q)
575 -> _If_seed_seq<_Sseq>
576 {
577 const size_t __k = (__w + 31) / 32;
578 uint_least32_t __arr[__r * __k];
579 __q.generate(__arr + 0, __arr + __r * __k);
580
581 for (size_t __i = 0; __i < long_lag; ++__i)
582 {
583 _UIntType __sum = 0u;
584 _UIntType __factor = 1u;
585 for (size_t __j = 0; __j < __k; ++__j)
586 {
587 __sum += __arr[__k * __i + __j] * __factor;
588 __factor *= __detail::_Shift<_UIntType, 32>::__value;
589 }
590 _M_x[__i] = __detail::__mod<_UIntType,
591 __detail::_Shift<_UIntType, __w>::__value>(__sum);
592 }
593 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
594 _M_p = 0;
595 }
596
597 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
599 result_type
602 {
603 // Derive short lag index from current index.
604 long __ps = _M_p - short_lag;
605 if (__ps < 0)
606 __ps += long_lag;
607
608 // Calculate new x(i) without overflow or division.
609 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
610 // cannot overflow.
611 _UIntType __xi;
612 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
613 {
614 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
615 _M_carry = 0;
616 }
617 else
618 {
619 __xi = (__detail::_Shift<_UIntType, __w>::__value
620 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
621 _M_carry = 1;
622 }
623 _M_x[_M_p] = __xi;
624
625 // Adjust current index to loop around in ring buffer.
626 if (++_M_p >= long_lag)
627 _M_p = 0;
628
629 return __xi;
630 }
631
632 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
633 typename _CharT, typename _Traits>
636 const subtract_with_carry_engine<_UIntType,
637 __w, __s, __r>& __x)
638 {
639 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
640
641 const typename __ios_base::fmtflags __flags = __os.flags();
642 const _CharT __fill = __os.fill();
643 const _CharT __space = __os.widen(' ');
644 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
645 __os.fill(__space);
646
647 for (size_t __i = 0; __i < __r; ++__i)
648 __os << __x._M_x[__i] << __space;
649 __os << __x._M_carry << __space << __x._M_p;
650
651 __os.flags(__flags);
652 __os.fill(__fill);
653 return __os;
654 }
655
656 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
657 typename _CharT, typename _Traits>
660 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
661 {
662 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
663
664 const typename __ios_base::fmtflags __flags = __is.flags();
665 __is.flags(__ios_base::dec | __ios_base::skipws);
666
667 for (size_t __i = 0; __i < __r; ++__i)
668 __is >> __x._M_x[__i];
669 __is >> __x._M_carry;
670 __is >> __x._M_p;
671
672 __is.flags(__flags);
673 return __is;
674 }
675
676#if ! __cpp_inline_variables
677 template<typename _RandomNumberEngine, size_t __p, size_t __r>
678 constexpr size_t
679 discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
680
681 template<typename _RandomNumberEngine, size_t __p, size_t __r>
682 constexpr size_t
683 discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
684#endif
686 template<typename _RandomNumberEngine, size_t __p, size_t __r>
687 typename discard_block_engine<_RandomNumberEngine,
688 __p, __r>::result_type
691 {
692 if (_M_n >= used_block)
693 {
694 _M_b.discard(block_size - _M_n);
695 _M_n = 0;
696 }
697 ++_M_n;
698 return _M_b();
699 }
700
701 template<typename _RandomNumberEngine, size_t __p, size_t __r,
702 typename _CharT, typename _Traits>
705 const discard_block_engine<_RandomNumberEngine,
706 __p, __r>& __x)
707 {
708 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
709
710 const typename __ios_base::fmtflags __flags = __os.flags();
711 const _CharT __fill = __os.fill();
712 const _CharT __space = __os.widen(' ');
713 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
714 __os.fill(__space);
715
716 __os << __x.base() << __space << __x._M_n;
717
718 __os.flags(__flags);
719 __os.fill(__fill);
720 return __os;
721 }
722
723 template<typename _RandomNumberEngine, size_t __p, size_t __r,
724 typename _CharT, typename _Traits>
727 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
728 {
729 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
730
731 const typename __ios_base::fmtflags __flags = __is.flags();
732 __is.flags(__ios_base::dec | __ios_base::skipws);
733
734 __is >> __x._M_b >> __x._M_n;
735
736 __is.flags(__flags);
737 return __is;
738 }
739
740
741 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
743 result_type
746 {
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;
753
755 __ctype;
756 const unsigned __cdig = std::numeric_limits<__ctype>::digits;
757
758 unsigned __n, __n0;
759 __ctype __s0, __s1, __y0, __y1;
760
761 for (size_t __i = 0; __i < 2; ++__i)
762 {
763 __n = (__w + __m - 1) / __m + __i;
764 __n0 = __n - __w % __n;
765 const unsigned __w0 = __w / __n; // __w0 <= __m
766
767 __s0 = 0;
768 __s1 = 0;
769 if (__w0 < __cdig)
770 {
771 __s0 = __ctype(1) << __w0;
772 __s1 = __s0 << 1;
773 }
774
775 __y0 = 0;
776 __y1 = 0;
777 if (__r)
778 {
779 __y0 = __s0 * (__r / __s0);
780 if (__s1)
781 __y1 = __s1 * (__r / __s1);
782
783 if (__r - __y0 <= __y0 / __n)
784 break;
785 }
786 else
787 break;
788 }
789
790 result_type __sum = 0;
791 for (size_t __k = 0; __k < __n0; ++__k)
792 {
793 __ctype __u;
794 do
795 __u = _M_b() - _M_b.min();
796 while (__y0 && __u >= __y0);
797 __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u);
798 }
799 for (size_t __k = __n0; __k < __n; ++__k)
800 {
801 __ctype __u;
802 do
803 __u = _M_b() - _M_b.min();
804 while (__y1 && __u >= __y1);
805 __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u);
806 }
807 return __sum;
808 }
809
810#if ! __cpp_inline_variables
811 template<typename _RandomNumberEngine, size_t __k>
812 constexpr size_t
813 shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
814#endif
815
816 namespace __detail
817 {
818 // Determine whether an integer is representable as double.
819 template<typename _Tp>
820 constexpr bool
821 __representable_as_double(_Tp __x) noexcept
822 {
823 static_assert(numeric_limits<_Tp>::is_integer, "");
824 static_assert(!numeric_limits<_Tp>::is_signed, "");
825 // All integers <= 2^53 are representable.
826 return (__x <= (1ull << __DBL_MANT_DIG__))
827 // Between 2^53 and 2^54 only even numbers are representable.
828 || (!(__x & 1) && __detail::__representable_as_double(__x >> 1));
829 }
830
831 // Determine whether x+1 is representable as double.
832 template<typename _Tp>
833 constexpr bool
834 __p1_representable_as_double(_Tp __x) noexcept
835 {
836 static_assert(numeric_limits<_Tp>::is_integer, "");
837 static_assert(!numeric_limits<_Tp>::is_signed, "");
838 return numeric_limits<_Tp>::digits < __DBL_MANT_DIG__
839 || (bool(__x + 1u) // return false if x+1 wraps around to zero
840 && __detail::__representable_as_double(__x + 1u));
841 }
842 }
843
844 template<typename _RandomNumberEngine, size_t __k>
848 {
849 constexpr result_type __range = max() - min();
850 size_t __j = __k;
851 const result_type __y = _M_y - min();
852#pragma GCC diagnostic push
853#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
854 // Avoid using slower long double arithmetic if possible.
855 if constexpr (__detail::__p1_representable_as_double(__range))
856 __j *= __y / (__range + 1.0);
857 else
858 __j *= __y / (__range + 1.0L);
859#pragma GCC diagnostic pop
860 _M_y = _M_v[__j];
861 _M_v[__j] = _M_b();
862
863 return _M_y;
864 }
865
866 template<typename _RandomNumberEngine, size_t __k,
867 typename _CharT, typename _Traits>
872 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
873
874 const typename __ios_base::fmtflags __flags = __os.flags();
875 const _CharT __fill = __os.fill();
876 const _CharT __space = __os.widen(' ');
877 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
878 __os.fill(__space);
879
880 __os << __x.base();
881 for (size_t __i = 0; __i < __k; ++__i)
882 __os << __space << __x._M_v[__i];
883 __os << __space << __x._M_y;
884
885 __os.flags(__flags);
886 __os.fill(__fill);
887 return __os;
888 }
889
890 template<typename _RandomNumberEngine, size_t __k,
891 typename _CharT, typename _Traits>
895 {
896 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
897
898 const typename __ios_base::fmtflags __flags = __is.flags();
899 __is.flags(__ios_base::dec | __ios_base::skipws);
900
901 __is >> __x._M_b;
902 for (size_t __i = 0; __i < __k; ++__i)
903 __is >> __x._M_v[__i];
904 __is >> __x._M_y;
905
906 __is.flags(__flags);
907 return __is;
908 }
909
910
911 template<typename _IntType, typename _CharT, typename _Traits>
915 {
916 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
917
918 const typename __ios_base::fmtflags __flags = __os.flags();
919 const _CharT __fill = __os.fill();
920 const _CharT __space = __os.widen(' ');
921 __os.flags(__ios_base::scientific | __ios_base::left);
922 __os.fill(__space);
923
924 __os << __x.a() << __space << __x.b();
925
926 __os.flags(__flags);
927 __os.fill(__fill);
928 return __os;
929 }
930
931 template<typename _IntType, typename _CharT, typename _Traits>
935 {
936 using param_type
938 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
939
940 const typename __ios_base::fmtflags __flags = __is.flags();
941 __is.flags(__ios_base::dec | __ios_base::skipws);
942
943 _IntType __a, __b;
944 if (__is >> __a >> __b)
945 __x.param(param_type(__a, __b));
946
947 __is.flags(__flags);
948 return __is;
949 }
950
951
952 template<typename _RealType>
953 template<typename _ForwardIterator,
954 typename _UniformRandomNumberGenerator>
955 void
957 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
958 _UniformRandomNumberGenerator& __urng,
959 const param_type& __p)
960 {
961 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
962 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
963 __aurng(__urng);
964 auto __range = __p.b() - __p.a();
965 while (__f != __t)
966 *__f++ = __aurng() * __range + __p.a();
967 }
968
969 template<typename _RealType, typename _CharT, typename _Traits>
972 const uniform_real_distribution<_RealType>& __x)
973 {
974 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
975
976 const typename __ios_base::fmtflags __flags = __os.flags();
977 const _CharT __fill = __os.fill();
978 const std::streamsize __precision = __os.precision();
979 const _CharT __space = __os.widen(' ');
980 __os.flags(__ios_base::scientific | __ios_base::left);
981 __os.fill(__space);
983
984 __os << __x.a() << __space << __x.b();
985
986 __os.flags(__flags);
987 __os.fill(__fill);
988 __os.precision(__precision);
989 return __os;
990 }
991
992 template<typename _RealType, typename _CharT, typename _Traits>
993 std::basic_istream<_CharT, _Traits>&
996 {
997 using param_type
999 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1000
1001 const typename __ios_base::fmtflags __flags = __is.flags();
1002 __is.flags(__ios_base::skipws);
1003
1004 _RealType __a, __b;
1005 if (__is >> __a >> __b)
1006 __x.param(param_type(__a, __b));
1007
1008 __is.flags(__flags);
1009 return __is;
1010 }
1011
1012
1013 template<typename _ForwardIterator,
1014 typename _UniformRandomNumberGenerator>
1015 void
1016 std::bernoulli_distribution::
1017 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1018 _UniformRandomNumberGenerator& __urng,
1019 const param_type& __p)
1020 {
1021 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1022 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1023 __aurng(__urng);
1024 auto __limit = __p.p() * (__aurng.max() - __aurng.min());
1025
1026 while (__f != __t)
1027 *__f++ = (__aurng() - __aurng.min()) < __limit;
1028 }
1029
1030 template<typename _CharT, typename _Traits>
1033 const bernoulli_distribution& __x)
1034 {
1035 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1036
1037 const typename __ios_base::fmtflags __flags = __os.flags();
1038 const _CharT __fill = __os.fill();
1039 const std::streamsize __precision = __os.precision();
1040 __os.flags(__ios_base::scientific | __ios_base::left);
1041 __os.fill(__os.widen(' '));
1043
1044 __os << __x.p();
1045
1046 __os.flags(__flags);
1047 __os.fill(__fill);
1048 __os.precision(__precision);
1049 return __os;
1050 }
1051
1052
1053 template<typename _IntType>
1054 template<typename _UniformRandomNumberGenerator>
1057 operator()(_UniformRandomNumberGenerator& __urng,
1058 const param_type& __param)
1059 {
1060 // About the epsilon thing see this thread:
1061 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1062 const double __naf =
1064 // The largest _RealType convertible to _IntType.
1065 const double __thr =
1067 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1068 __aurng(__urng);
1069
1070 double __cand;
1071 do
1072 __cand = std::floor(std::log(1.0 - __aurng()) / __param._M_log_1_p);
1073 while (__cand >= __thr);
1074
1075 return result_type(__cand + __naf);
1076 }
1077
1078 template<typename _IntType>
1079 template<typename _ForwardIterator,
1080 typename _UniformRandomNumberGenerator>
1081 void
1083 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1084 _UniformRandomNumberGenerator& __urng,
1085 const param_type& __param)
1086 {
1087 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1088 // About the epsilon thing see this thread:
1089 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
1090 const double __naf =
1092 // The largest _RealType convertible to _IntType.
1093 const double __thr =
1095 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1096 __aurng(__urng);
1097
1098 while (__f != __t)
1099 {
1100 double __cand;
1101 do
1102 __cand = std::floor(std::log(1.0 - __aurng())
1103 / __param._M_log_1_p);
1104 while (__cand >= __thr);
1105
1106 *__f++ = __cand + __naf;
1107 }
1108 }
1109
1110 template<typename _IntType,
1111 typename _CharT, typename _Traits>
1114 const geometric_distribution<_IntType>& __x)
1115 {
1116 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1117
1118 const typename __ios_base::fmtflags __flags = __os.flags();
1119 const _CharT __fill = __os.fill();
1120 const std::streamsize __precision = __os.precision();
1121 __os.flags(__ios_base::scientific | __ios_base::left);
1122 __os.fill(__os.widen(' '));
1124
1125 __os << __x.p();
1126
1127 __os.flags(__flags);
1128 __os.fill(__fill);
1129 __os.precision(__precision);
1130 return __os;
1131 }
1132
1133 template<typename _IntType,
1134 typename _CharT, typename _Traits>
1135 std::basic_istream<_CharT, _Traits>&
1138 {
1139 using param_type = typename geometric_distribution<_IntType>::param_type;
1140 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1141
1142 const typename __ios_base::fmtflags __flags = __is.flags();
1143 __is.flags(__ios_base::skipws);
1144
1145 double __p;
1146 if (__is >> __p)
1147 __x.param(param_type(__p));
1148
1149 __is.flags(__flags);
1150 return __is;
1151 }
1152
1153 // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5.
1154 template<typename _IntType>
1155 template<typename _UniformRandomNumberGenerator>
1158 operator()(_UniformRandomNumberGenerator& __urng)
1159 {
1160 const double __y = _M_gd(__urng);
1161
1162 // XXX Is the constructor too slow?
1164 return __poisson(__urng);
1165 }
1166
1167 template<typename _IntType>
1168 template<typename _UniformRandomNumberGenerator>
1171 operator()(_UniformRandomNumberGenerator& __urng,
1172 const param_type& __p)
1173 {
1175 param_type;
1176
1177 const double __y =
1178 _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p()));
1179
1181 return __poisson(__urng);
1182 }
1183
1184 template<typename _IntType>
1185 template<typename _ForwardIterator,
1186 typename _UniformRandomNumberGenerator>
1187 void
1188 negative_binomial_distribution<_IntType>::
1189 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1190 _UniformRandomNumberGenerator& __urng)
1191 {
1192 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1193 while (__f != __t)
1194 {
1195 const double __y = _M_gd(__urng);
1196
1197 // XXX Is the constructor too slow?
1199 *__f++ = __poisson(__urng);
1200 }
1201 }
1202
1203 template<typename _IntType>
1204 template<typename _ForwardIterator,
1205 typename _UniformRandomNumberGenerator>
1206 void
1208 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1209 _UniformRandomNumberGenerator& __urng,
1210 const param_type& __p)
1211 {
1212 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1213 typename std::gamma_distribution<result_type>::param_type
1214 __p2(__p.k(), (1.0 - __p.p()) / __p.p());
1215
1216 while (__f != __t)
1217 {
1218 const double __y = _M_gd(__urng, __p2);
1219
1220 std::poisson_distribution<result_type> __poisson(__y);
1221 *__f++ = __poisson(__urng);
1222 }
1223 }
1224
1225 template<typename _IntType, typename _CharT, typename _Traits>
1226 std::basic_ostream<_CharT, _Traits>&
1227 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1229 {
1230 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1231
1232 const typename __ios_base::fmtflags __flags = __os.flags();
1233 const _CharT __fill = __os.fill();
1234 const std::streamsize __precision = __os.precision();
1235 const _CharT __space = __os.widen(' ');
1236 __os.flags(__ios_base::scientific | __ios_base::left);
1237 __os.fill(__os.widen(' '));
1238 __os.precision(std::numeric_limits<double>::max_digits10);
1239
1240 __os << __x.k() << __space << __x.p()
1241 << __space << __x._M_gd;
1242
1243 __os.flags(__flags);
1244 __os.fill(__fill);
1245 __os.precision(__precision);
1246 return __os;
1247 }
1248
1249 template<typename _IntType, typename _CharT, typename _Traits>
1250 std::basic_istream<_CharT, _Traits>&
1251 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1253 {
1254 using param_type
1256 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1257
1258 const typename __ios_base::fmtflags __flags = __is.flags();
1259 __is.flags(__ios_base::skipws);
1260
1261 _IntType __k;
1262 double __p;
1263 if (__is >> __k >> __p >> __x._M_gd)
1264 __x.param(param_type(__k, __p));
1265
1266 __is.flags(__flags);
1267 return __is;
1268 }
1269
1270
1271 template<typename _IntType>
1272 void
1275 {
1276#if _GLIBCXX_USE_C99_MATH_FUNCS
1277 if (_M_mean >= 12)
1278 {
1279 const double __m = std::floor(_M_mean);
1280 _M_lm_thr = std::log(_M_mean);
1281 _M_lfm = std::lgamma(__m + 1);
1282 _M_sm = std::sqrt(__m);
1283
1284 const double __pi_4 = 0.7853981633974483096156608458198757L;
1285 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
1286 / __pi_4));
1287 _M_d = std::round(std::max<double>(6.0, std::min(__m, __dx)));
1288 const double __cx = 2 * __m + _M_d;
1289 _M_scx = std::sqrt(__cx / 2);
1290 _M_1cx = 1 / __cx;
1291
1292 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1293 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
1294 / _M_d;
1295 }
1296 else
1297#endif
1298 _M_lm_thr = std::exp(-_M_mean);
1299 }
1300
1301 /**
1302 * A rejection algorithm when mean >= 12 and a simple method based
1303 * upon the multiplication of uniform random variates otherwise.
1304 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_FUNCS
1305 * is defined.
1306 *
1307 * Reference:
1308 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1309 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1310 */
1311 template<typename _IntType>
1312 template<typename _UniformRandomNumberGenerator>
1313 typename poisson_distribution<_IntType>::result_type
1315 operator()(_UniformRandomNumberGenerator& __urng,
1316 const param_type& __param)
1317 {
1318 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1319 __aurng(__urng);
1320#if _GLIBCXX_USE_C99_MATH_FUNCS
1321 if (__param.mean() >= 12)
1322 {
1323 double __x;
1324
1325 // See comments above...
1326 const double __naf =
1328 const double __thr =
1330
1331 const double __m = std::floor(__param.mean());
1332 // sqrt(pi / 2)
1333 const double __spi_2 = 1.2533141373155002512078826424055226L;
1334 const double __c1 = __param._M_sm * __spi_2;
1335 const double __c2 = __param._M_c2b + __c1;
1336 const double __c3 = __c2 + 1;
1337 const double __c4 = __c3 + 1;
1338 // 1 / 78
1339 const double __178 = 0.0128205128205128205128205128205128L;
1340 // e^(1 / 78)
1341 const double __e178 = 1.0129030479320018583185514777512983L;
1342 const double __c5 = __c4 + __e178;
1343 const double __c = __param._M_cb + __c5;
1344 const double __2cx = 2 * (2 * __m + __param._M_d);
1345
1346 bool __reject = true;
1347 do
1348 {
1349 const double __u = __c * __aurng();
1350 const double __e = -std::log(1.0 - __aurng());
1351
1352 double __w = 0.0;
1353
1354 if (__u <= __c1)
1355 {
1356 const double __n = _M_nd(__urng);
1357 const double __y = -std::abs(__n) * __param._M_sm - 1;
1358 __x = std::floor(__y);
1359 __w = -__n * __n / 2;
1360 if (__x < -__m)
1361 continue;
1362 }
1363 else if (__u <= __c2)
1364 {
1365 const double __n = _M_nd(__urng);
1366 const double __y = 1 + std::abs(__n) * __param._M_scx;
1367 __x = std::ceil(__y);
1368 __w = __y * (2 - __y) * __param._M_1cx;
1369 if (__x > __param._M_d)
1370 continue;
1371 }
1372 else if (__u <= __c3)
1373 // NB: This case not in the book, nor in the Errata,
1374 // but should be ok...
1375 __x = -1;
1376 else if (__u <= __c4)
1377 __x = 0;
1378 else if (__u <= __c5)
1379 {
1380 __x = 1;
1381 // Only in the Errata, see libstdc++/83237.
1382 __w = __178;
1383 }
1384 else
1385 {
1386 const double __v = -std::log(1.0 - __aurng());
1387 const double __y = __param._M_d
1388 + __v * __2cx / __param._M_d;
1389 __x = std::ceil(__y);
1390 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1391 }
1392
1393 __reject = (__w - __e - __x * __param._M_lm_thr
1394 > __param._M_lfm - std::lgamma(__x + __m + 1));
1395
1396 __reject |= __x + __m >= __thr;
1397
1398 } while (__reject);
1399
1400 return result_type(__x + __m + __naf);
1401 }
1402 else
1403#endif
1404 {
1405 _IntType __x = 0;
1406 double __prod = 1.0;
1407
1408 do
1409 {
1410 __prod *= __aurng();
1411 __x += 1;
1412 }
1413 while (__prod > __param._M_lm_thr);
1414
1415 return __x - 1;
1416 }
1417 }
1418
1419 template<typename _IntType>
1420 template<typename _ForwardIterator,
1421 typename _UniformRandomNumberGenerator>
1422 void
1424 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1425 _UniformRandomNumberGenerator& __urng,
1426 const param_type& __param)
1427 {
1428 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1429 // We could duplicate everything from operator()...
1430 while (__f != __t)
1431 *__f++ = this->operator()(__urng, __param);
1432 }
1433
1434 template<typename _IntType,
1435 typename _CharT, typename _Traits>
1438 const poisson_distribution<_IntType>& __x)
1439 {
1440 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1441
1442 const typename __ios_base::fmtflags __flags = __os.flags();
1443 const _CharT __fill = __os.fill();
1444 const std::streamsize __precision = __os.precision();
1445 const _CharT __space = __os.widen(' ');
1446 __os.flags(__ios_base::scientific | __ios_base::left);
1447 __os.fill(__space);
1449
1450 __os << __x.mean() << __space << __x._M_nd;
1451
1452 __os.flags(__flags);
1453 __os.fill(__fill);
1454 __os.precision(__precision);
1455 return __os;
1456 }
1457
1458 template<typename _IntType,
1459 typename _CharT, typename _Traits>
1460 std::basic_istream<_CharT, _Traits>&
1461 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1462 poisson_distribution<_IntType>& __x)
1463 {
1464 using param_type = typename poisson_distribution<_IntType>::param_type;
1465 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1466
1467 const typename __ios_base::fmtflags __flags = __is.flags();
1468 __is.flags(__ios_base::skipws);
1469
1470 double __mean;
1471 if (__is >> __mean >> __x._M_nd)
1472 __x.param(param_type(__mean));
1473
1474 __is.flags(__flags);
1475 return __is;
1476 }
1477
1478
1479 template<typename _IntType>
1480 void
1481 binomial_distribution<_IntType>::param_type::
1482 _M_initialize()
1483 {
1484 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1485
1486 _M_easy = true;
1487
1488#if _GLIBCXX_USE_C99_MATH_FUNCS
1489 if (_M_t * __p12 >= 8)
1490 {
1491 _M_easy = false;
1492 const double __np = std::floor(_M_t * __p12);
1493 const double __pa = __np / _M_t;
1494 const double __1p = 1 - __pa;
1495
1496 const double __pi_4 = 0.7853981633974483096156608458198757L;
1497 const double __d1x =
1498 std::sqrt(__np * __1p * std::log(32 * __np
1499 / (81 * __pi_4 * __1p)));
1500 _M_d1 = std::round(std::max<double>(1.0, __d1x));
1501 const double __d2x =
1502 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1503 / (__pi_4 * __pa)));
1504 _M_d2 = std::round(std::max<double>(1.0, __d2x));
1505
1506 // sqrt(pi / 2)
1507 const double __spi_2 = 1.2533141373155002512078826424055226L;
1508 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1509 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * (_M_t * __1p)));
1510 _M_c = 2 * _M_d1 / __np;
1511 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1512 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1513 const double __s1s = _M_s1 * _M_s1;
1514 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1515 * 2 * __s1s / _M_d1
1516 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1517 const double __s2s = _M_s2 * _M_s2;
1518 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1519 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1520 _M_lf = (std::lgamma(__np + 1)
1521 + std::lgamma(_M_t - __np + 1));
1522 _M_lp1p = std::log(__pa / __1p);
1523
1524 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1525 }
1526 else
1527#endif
1528 _M_q = -std::log(1 - __p12);
1529 }
1530
1531 template<typename _IntType>
1532 template<typename _UniformRandomNumberGenerator>
1534 binomial_distribution<_IntType>::
1535 _M_waiting(_UniformRandomNumberGenerator& __urng,
1536 _IntType __t, double __q)
1537 {
1538 _IntType __x = 0;
1539 double __sum = 0.0;
1540 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1541 __aurng(__urng);
1542
1543 do
1544 {
1545 if (__t == __x)
1546 return __x;
1547 const double __e = -std::log(1.0 - __aurng());
1548 __sum += __e / (__t - __x);
1549 __x += 1;
1550 }
1551 while (__sum <= __q);
1552
1553 return __x - 1;
1554 }
1555
1556 /**
1557 * A rejection algorithm when t * p >= 8 and a simple waiting time
1558 * method - the second in the referenced book - otherwise.
1559 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_FUNCS
1560 * is defined.
1561 *
1562 * Reference:
1563 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1564 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1565 */
1566 template<typename _IntType>
1567 template<typename _UniformRandomNumberGenerator>
1570 operator()(_UniformRandomNumberGenerator& __urng,
1571 const param_type& __param)
1572 {
1573 result_type __ret;
1574 const _IntType __t = __param.t();
1575 const double __p = __param.p();
1576 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1577 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1578 __aurng(__urng);
1579
1580#if _GLIBCXX_USE_C99_MATH_FUNCS
1581 if (!__param._M_easy)
1582 {
1583 double __x;
1584
1585 // See comments above...
1586 const double __naf =
1588 const double __thr =
1590
1591 const double __np = std::floor(__t * __p12);
1592
1593 // sqrt(pi / 2)
1594 const double __spi_2 = 1.2533141373155002512078826424055226L;
1595 const double __a1 = __param._M_a1;
1596 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1597 const double __a123 = __param._M_a123;
1598 const double __s1s = __param._M_s1 * __param._M_s1;
1599 const double __s2s = __param._M_s2 * __param._M_s2;
1600
1601 bool __reject;
1602 do
1603 {
1604 const double __u = __param._M_s * __aurng();
1605
1606 double __v;
1607
1608 if (__u <= __a1)
1609 {
1610 const double __n = _M_nd(__urng);
1611 const double __y = __param._M_s1 * std::abs(__n);
1612 __reject = __y >= __param._M_d1;
1613 if (!__reject)
1614 {
1615 const double __e = -std::log(1.0 - __aurng());
1616 __x = std::floor(__y);
1617 __v = -__e - __n * __n / 2 + __param._M_c;
1618 }
1619 }
1620 else if (__u <= __a12)
1621 {
1622 const double __n = _M_nd(__urng);
1623 const double __y = __param._M_s2 * std::abs(__n);
1624 __reject = __y >= __param._M_d2;
1625 if (!__reject)
1626 {
1627 const double __e = -std::log(1.0 - __aurng());
1628 __x = std::floor(-__y);
1629 __v = -__e - __n * __n / 2;
1630 }
1631 }
1632 else if (__u <= __a123)
1633 {
1634 const double __e1 = -std::log(1.0 - __aurng());
1635 const double __e2 = -std::log(1.0 - __aurng());
1636
1637 const double __y = __param._M_d1
1638 + 2 * __s1s * __e1 / __param._M_d1;
1639 __x = std::floor(__y);
1640 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1641 -__y / (2 * __s1s)));
1642 __reject = false;
1643 }
1644 else
1645 {
1646 const double __e1 = -std::log(1.0 - __aurng());
1647 const double __e2 = -std::log(1.0 - __aurng());
1648
1649 const double __y = __param._M_d2
1650 + 2 * __s2s * __e1 / __param._M_d2;
1651 __x = std::floor(-__y);
1652 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1653 __reject = false;
1654 }
1655
1656 __reject = __reject || __x < -__np || __x > __t - __np;
1657 if (!__reject)
1658 {
1659 const double __lfx =
1660 std::lgamma(__np + __x + 1)
1661 + std::lgamma(__t - (__np + __x) + 1);
1662 __reject = __v > __param._M_lf - __lfx
1663 + __x * __param._M_lp1p;
1664 }
1665
1666 __reject |= __x + __np >= __thr;
1667 }
1668 while (__reject);
1669
1670 __x += __np + __naf;
1671
1672 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x),
1673 __param._M_q);
1674 __ret = _IntType(__x) + __z;
1675 }
1676 else
1677#endif
1678 __ret = _M_waiting(__urng, __t, __param._M_q);
1679
1680 if (__p12 != __p)
1681 __ret = __t - __ret;
1682 return __ret;
1683 }
1684
1685 template<typename _IntType>
1686 template<typename _ForwardIterator,
1687 typename _UniformRandomNumberGenerator>
1688 void
1689 binomial_distribution<_IntType>::
1690 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1691 _UniformRandomNumberGenerator& __urng,
1692 const param_type& __param)
1693 {
1694 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1695 // We could duplicate everything from operator()...
1696 while (__f != __t)
1697 *__f++ = this->operator()(__urng, __param);
1698 }
1699
1700 template<typename _IntType,
1701 typename _CharT, typename _Traits>
1702 std::basic_ostream<_CharT, _Traits>&
1703 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1704 const binomial_distribution<_IntType>& __x)
1705 {
1706 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1707
1708 const typename __ios_base::fmtflags __flags = __os.flags();
1709 const _CharT __fill = __os.fill();
1710 const std::streamsize __precision = __os.precision();
1711 const _CharT __space = __os.widen(' ');
1712 __os.flags(__ios_base::scientific | __ios_base::left);
1713 __os.fill(__space);
1714 __os.precision(std::numeric_limits<double>::max_digits10);
1715
1716 __os << __x.t() << __space << __x.p()
1717 << __space << __x._M_nd;
1718
1719 __os.flags(__flags);
1720 __os.fill(__fill);
1721 __os.precision(__precision);
1722 return __os;
1723 }
1724
1725 template<typename _IntType,
1726 typename _CharT, typename _Traits>
1727 std::basic_istream<_CharT, _Traits>&
1728 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1729 binomial_distribution<_IntType>& __x)
1730 {
1731 using param_type = typename binomial_distribution<_IntType>::param_type;
1732 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1733
1734 const typename __ios_base::fmtflags __flags = __is.flags();
1735 __is.flags(__ios_base::dec | __ios_base::skipws);
1736
1737 _IntType __t;
1738 double __p;
1739 if (__is >> __t >> __p >> __x._M_nd)
1740 __x.param(param_type(__t, __p));
1741
1742 __is.flags(__flags);
1743 return __is;
1744 }
1745
1746
1747 template<typename _RealType>
1748 template<typename _ForwardIterator,
1749 typename _UniformRandomNumberGenerator>
1750 void
1752 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1753 _UniformRandomNumberGenerator& __urng,
1754 const param_type& __p)
1755 {
1756 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1757 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1758 __aurng(__urng);
1759 while (__f != __t)
1760 *__f++ = -std::log(result_type(1) - __aurng()) / __p.lambda();
1761 }
1762
1763 template<typename _RealType, typename _CharT, typename _Traits>
1766 const exponential_distribution<_RealType>& __x)
1767 {
1768 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1769
1770 const typename __ios_base::fmtflags __flags = __os.flags();
1771 const _CharT __fill = __os.fill();
1772 const std::streamsize __precision = __os.precision();
1773 __os.flags(__ios_base::scientific | __ios_base::left);
1774 __os.fill(__os.widen(' '));
1776
1777 __os << __x.lambda();
1778
1779 __os.flags(__flags);
1780 __os.fill(__fill);
1781 __os.precision(__precision);
1782 return __os;
1783 }
1784
1785 template<typename _RealType, typename _CharT, typename _Traits>
1786 std::basic_istream<_CharT, _Traits>&
1789 {
1790 using param_type
1792 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1793
1794 const typename __ios_base::fmtflags __flags = __is.flags();
1795 __is.flags(__ios_base::dec | __ios_base::skipws);
1796
1797 _RealType __lambda;
1798 if (__is >> __lambda)
1799 __x.param(param_type(__lambda));
1800
1801 __is.flags(__flags);
1802 return __is;
1803 }
1804
1805
1806 /**
1807 * Polar method due to Marsaglia.
1808 *
1809 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1810 * New York, 1986, Ch. V, Sect. 4.4.
1811 */
1812 template<typename _RealType>
1813 template<typename _UniformRandomNumberGenerator>
1816 operator()(_UniformRandomNumberGenerator& __urng,
1817 const param_type& __param)
1818 {
1819 result_type __ret;
1820 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1821 __aurng(__urng);
1822
1823 if (_M_saved_available)
1824 {
1825 _M_saved_available = false;
1826 __ret = _M_saved;
1827 }
1828 else
1829 {
1830 result_type __x, __y, __r2;
1831 do
1832 {
1833 __x = result_type(2.0) * __aurng() - 1.0;
1834 __y = result_type(2.0) * __aurng() - 1.0;
1835 __r2 = __x * __x + __y * __y;
1836 }
1837 while (__r2 > 1.0 || __r2 == 0.0);
1838
1839 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1840 _M_saved = __x * __mult;
1841 _M_saved_available = true;
1842 __ret = __y * __mult;
1843 }
1844
1845 __ret = __ret * __param.stddev() + __param.mean();
1846 return __ret;
1847 }
1848
1849 template<typename _RealType>
1850 template<typename _ForwardIterator,
1851 typename _UniformRandomNumberGenerator>
1852 void
1854 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1855 _UniformRandomNumberGenerator& __urng,
1856 const param_type& __param)
1857 {
1858 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1859
1860 if (__f == __t)
1861 return;
1862
1863 if (_M_saved_available)
1864 {
1865 _M_saved_available = false;
1866 *__f++ = _M_saved * __param.stddev() + __param.mean();
1867
1868 if (__f == __t)
1869 return;
1870 }
1871
1872 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1873 __aurng(__urng);
1874
1875 while (__f + 1 < __t)
1876 {
1877 result_type __x, __y, __r2;
1878 do
1879 {
1880 __x = result_type(2.0) * __aurng() - 1.0;
1881 __y = result_type(2.0) * __aurng() - 1.0;
1882 __r2 = __x * __x + __y * __y;
1883 }
1884 while (__r2 > 1.0 || __r2 == 0.0);
1885
1886 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1887 *__f++ = __y * __mult * __param.stddev() + __param.mean();
1888 *__f++ = __x * __mult * __param.stddev() + __param.mean();
1889 }
1890
1891 if (__f != __t)
1892 {
1893 result_type __x, __y, __r2;
1894 do
1895 {
1896 __x = result_type(2.0) * __aurng() - 1.0;
1897 __y = result_type(2.0) * __aurng() - 1.0;
1898 __r2 = __x * __x + __y * __y;
1899 }
1900 while (__r2 > 1.0 || __r2 == 0.0);
1901
1902 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1903 _M_saved = __x * __mult;
1904 _M_saved_available = true;
1905 *__f = __y * __mult * __param.stddev() + __param.mean();
1906 }
1907 }
1908
1909 template<typename _RealType>
1910 bool
1911 operator==(const std::normal_distribution<_RealType>& __d1,
1912 const std::normal_distribution<_RealType>& __d2)
1913 {
1914 if (__d1._M_param == __d2._M_param
1915 && __d1._M_saved_available == __d2._M_saved_available)
1916 return __d1._M_saved_available ? __d1._M_saved == __d2._M_saved : true;
1917 else
1918 return false;
1919 }
1920
1921 template<typename _RealType, typename _CharT, typename _Traits>
1922 std::basic_ostream<_CharT, _Traits>&
1923 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1924 const normal_distribution<_RealType>& __x)
1925 {
1926 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1927
1928 const typename __ios_base::fmtflags __flags = __os.flags();
1929 const _CharT __fill = __os.fill();
1930 const std::streamsize __precision = __os.precision();
1931 const _CharT __space = __os.widen(' ');
1932 __os.flags(__ios_base::scientific | __ios_base::left);
1933 __os.fill(__space);
1935
1936 __os << __x.mean() << __space << __x.stddev()
1937 << __space << __x._M_saved_available;
1938 if (__x._M_saved_available)
1939 __os << __space << __x._M_saved;
1940
1941 __os.flags(__flags);
1942 __os.fill(__fill);
1943 __os.precision(__precision);
1944 return __os;
1945 }
1946
1947 template<typename _RealType, typename _CharT, typename _Traits>
1948 std::basic_istream<_CharT, _Traits>&
1949 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1950 normal_distribution<_RealType>& __x)
1951 {
1952 using param_type = typename normal_distribution<_RealType>::param_type;
1953 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
1954
1955 const typename __ios_base::fmtflags __flags = __is.flags();
1956 __is.flags(__ios_base::dec | __ios_base::skipws);
1957
1958 double __mean, __stddev;
1959 bool __saved_avail;
1960 if (__is >> __mean >> __stddev >> __saved_avail)
1961 {
1962 if (!__saved_avail || (__is >> __x._M_saved))
1963 {
1964 __x._M_saved_available = __saved_avail;
1965 __x.param(param_type(__mean, __stddev));
1966 }
1967 }
1968
1969 __is.flags(__flags);
1970 return __is;
1971 }
1972
1973
1974 template<typename _RealType>
1975 template<typename _ForwardIterator,
1976 typename _UniformRandomNumberGenerator>
1977 void
1978 lognormal_distribution<_RealType>::
1979 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1980 _UniformRandomNumberGenerator& __urng,
1981 const param_type& __p)
1982 {
1983 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1984 while (__f != __t)
1985 *__f++ = std::exp(__p.s() * _M_nd(__urng) + __p.m());
1986 }
1987
1988 template<typename _RealType, typename _CharT, typename _Traits>
1989 std::basic_ostream<_CharT, _Traits>&
1990 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1991 const lognormal_distribution<_RealType>& __x)
1992 {
1993 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
1994
1995 const typename __ios_base::fmtflags __flags = __os.flags();
1996 const _CharT __fill = __os.fill();
1997 const std::streamsize __precision = __os.precision();
1998 const _CharT __space = __os.widen(' ');
1999 __os.flags(__ios_base::scientific | __ios_base::left);
2000 __os.fill(__space);
2002
2003 __os << __x.m() << __space << __x.s()
2004 << __space << __x._M_nd;
2005
2006 __os.flags(__flags);
2007 __os.fill(__fill);
2008 __os.precision(__precision);
2009 return __os;
2010 }
2011
2012 template<typename _RealType, typename _CharT, typename _Traits>
2013 std::basic_istream<_CharT, _Traits>&
2014 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2015 lognormal_distribution<_RealType>& __x)
2016 {
2017 using param_type
2018 = typename lognormal_distribution<_RealType>::param_type;
2019 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2020
2021 const typename __ios_base::fmtflags __flags = __is.flags();
2022 __is.flags(__ios_base::dec | __ios_base::skipws);
2023
2024 _RealType __m, __s;
2025 if (__is >> __m >> __s >> __x._M_nd)
2026 __x.param(param_type(__m, __s));
2027
2028 __is.flags(__flags);
2029 return __is;
2030 }
2031
2032 template<typename _RealType>
2033 template<typename _ForwardIterator,
2034 typename _UniformRandomNumberGenerator>
2035 void
2036 std::chi_squared_distribution<_RealType>::
2037 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2038 _UniformRandomNumberGenerator& __urng)
2039 {
2040 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2041 while (__f != __t)
2042 *__f++ = 2 * _M_gd(__urng);
2043 }
2044
2045 template<typename _RealType>
2046 template<typename _ForwardIterator,
2047 typename _UniformRandomNumberGenerator>
2048 void
2049 std::chi_squared_distribution<_RealType>::
2050 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2051 _UniformRandomNumberGenerator& __urng,
2052 const typename
2053 std::gamma_distribution<result_type>::param_type& __p)
2054 {
2055 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2056 while (__f != __t)
2057 *__f++ = 2 * _M_gd(__urng, __p);
2058 }
2059
2060 template<typename _RealType, typename _CharT, typename _Traits>
2061 std::basic_ostream<_CharT, _Traits>&
2062 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2063 const chi_squared_distribution<_RealType>& __x)
2064 {
2065 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2066
2067 const typename __ios_base::fmtflags __flags = __os.flags();
2068 const _CharT __fill = __os.fill();
2069 const std::streamsize __precision = __os.precision();
2070 const _CharT __space = __os.widen(' ');
2071 __os.flags(__ios_base::scientific | __ios_base::left);
2072 __os.fill(__space);
2074
2075 __os << __x.n() << __space << __x._M_gd;
2076
2077 __os.flags(__flags);
2078 __os.fill(__fill);
2079 __os.precision(__precision);
2080 return __os;
2081 }
2082
2083 template<typename _RealType, typename _CharT, typename _Traits>
2084 std::basic_istream<_CharT, _Traits>&
2085 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2086 chi_squared_distribution<_RealType>& __x)
2087 {
2088 using param_type
2089 = typename chi_squared_distribution<_RealType>::param_type;
2090 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2091
2092 const typename __ios_base::fmtflags __flags = __is.flags();
2093 __is.flags(__ios_base::dec | __ios_base::skipws);
2094
2095 _RealType __n;
2096 if (__is >> __n >> __x._M_gd)
2097 __x.param(param_type(__n));
2098
2099 __is.flags(__flags);
2100 return __is;
2101 }
2102
2103
2104 template<typename _RealType>
2105 template<typename _UniformRandomNumberGenerator>
2108 operator()(_UniformRandomNumberGenerator& __urng,
2109 const param_type& __p)
2110 {
2111 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2112 __aurng(__urng);
2113 _RealType __u;
2114 do
2115 __u = __aurng();
2116 while (__u == 0.5);
2117
2118 const _RealType __pi = 3.1415926535897932384626433832795029L;
2119 return __p.a() + __p.b() * std::tan(__pi * __u);
2120 }
2121
2122 template<typename _RealType>
2123 template<typename _ForwardIterator,
2124 typename _UniformRandomNumberGenerator>
2125 void
2127 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2128 _UniformRandomNumberGenerator& __urng,
2129 const param_type& __p)
2130 {
2131 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2132 const _RealType __pi = 3.1415926535897932384626433832795029L;
2133 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2134 __aurng(__urng);
2135 while (__f != __t)
2136 {
2137 _RealType __u;
2138 do
2139 __u = __aurng();
2140 while (__u == 0.5);
2141
2142 *__f++ = __p.a() + __p.b() * std::tan(__pi * __u);
2143 }
2144 }
2145
2146 template<typename _RealType, typename _CharT, typename _Traits>
2149 const cauchy_distribution<_RealType>& __x)
2150 {
2151 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2152
2153 const typename __ios_base::fmtflags __flags = __os.flags();
2154 const _CharT __fill = __os.fill();
2155 const std::streamsize __precision = __os.precision();
2156 const _CharT __space = __os.widen(' ');
2157 __os.flags(__ios_base::scientific | __ios_base::left);
2158 __os.fill(__space);
2160
2161 __os << __x.a() << __space << __x.b();
2162
2163 __os.flags(__flags);
2164 __os.fill(__fill);
2165 __os.precision(__precision);
2166 return __os;
2167 }
2168
2169 template<typename _RealType, typename _CharT, typename _Traits>
2170 std::basic_istream<_CharT, _Traits>&
2173 {
2174 using param_type = typename cauchy_distribution<_RealType>::param_type;
2175 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2176
2177 const typename __ios_base::fmtflags __flags = __is.flags();
2178 __is.flags(__ios_base::dec | __ios_base::skipws);
2179
2180 _RealType __a, __b;
2181 if (__is >> __a >> __b)
2182 __x.param(param_type(__a, __b));
2183
2184 __is.flags(__flags);
2185 return __is;
2186 }
2187
2188
2189 template<typename _RealType>
2190 template<typename _ForwardIterator,
2191 typename _UniformRandomNumberGenerator>
2192 void
2194 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2195 _UniformRandomNumberGenerator& __urng)
2196 {
2197 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2198 while (__f != __t)
2199 *__f++ = ((_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()));
2200 }
2201
2202 template<typename _RealType>
2203 template<typename _ForwardIterator,
2204 typename _UniformRandomNumberGenerator>
2205 void
2206 std::fisher_f_distribution<_RealType>::
2207 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2208 _UniformRandomNumberGenerator& __urng,
2209 const param_type& __p)
2210 {
2211 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2212 typedef typename std::gamma_distribution<result_type>::param_type
2213 param_type;
2214 param_type __p1(__p.m() / 2);
2215 param_type __p2(__p.n() / 2);
2216 while (__f != __t)
2217 *__f++ = ((_M_gd_x(__urng, __p1) * n())
2218 / (_M_gd_y(__urng, __p2) * m()));
2219 }
2220
2221 template<typename _RealType, typename _CharT, typename _Traits>
2222 std::basic_ostream<_CharT, _Traits>&
2223 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2224 const fisher_f_distribution<_RealType>& __x)
2225 {
2226 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2227
2228 const typename __ios_base::fmtflags __flags = __os.flags();
2229 const _CharT __fill = __os.fill();
2230 const std::streamsize __precision = __os.precision();
2231 const _CharT __space = __os.widen(' ');
2232 __os.flags(__ios_base::scientific | __ios_base::left);
2233 __os.fill(__space);
2235
2236 __os << __x.m() << __space << __x.n()
2237 << __space << __x._M_gd_x << __space << __x._M_gd_y;
2238
2239 __os.flags(__flags);
2240 __os.fill(__fill);
2241 __os.precision(__precision);
2242 return __os;
2244
2245 template<typename _RealType, typename _CharT, typename _Traits>
2249 {
2250 using param_type
2252 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2253
2254 const typename __ios_base::fmtflags __flags = __is.flags();
2255 __is.flags(__ios_base::dec | __ios_base::skipws);
2256
2257 _RealType __m, __n;
2258 if (__is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y)
2259 __x.param(param_type(__m, __n));
2260
2261 __is.flags(__flags);
2262 return __is;
2263 }
2264
2265
2266 template<typename _RealType>
2267 template<typename _ForwardIterator,
2268 typename _UniformRandomNumberGenerator>
2269 void
2271 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2272 _UniformRandomNumberGenerator& __urng)
2273 {
2274 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2275 while (__f != __t)
2276 *__f++ = _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng));
2277 }
2278
2279 template<typename _RealType>
2280 template<typename _ForwardIterator,
2281 typename _UniformRandomNumberGenerator>
2282 void
2283 std::student_t_distribution<_RealType>::
2284 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2285 _UniformRandomNumberGenerator& __urng,
2286 const param_type& __p)
2287 {
2288 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2289 typename std::gamma_distribution<result_type>::param_type
2290 __p2(__p.n() / 2, 2);
2291 while (__f != __t)
2292 *__f++ = _M_nd(__urng) * std::sqrt(__p.n() / _M_gd(__urng, __p2));
2293 }
2294
2295 template<typename _RealType, typename _CharT, typename _Traits>
2296 std::basic_ostream<_CharT, _Traits>&
2297 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2298 const student_t_distribution<_RealType>& __x)
2299 {
2300 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2301
2302 const typename __ios_base::fmtflags __flags = __os.flags();
2303 const _CharT __fill = __os.fill();
2304 const std::streamsize __precision = __os.precision();
2305 const _CharT __space = __os.widen(' ');
2306 __os.flags(__ios_base::scientific | __ios_base::left);
2307 __os.fill(__space);
2309
2310 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
2311
2312 __os.flags(__flags);
2313 __os.fill(__fill);
2314 __os.precision(__precision);
2315 return __os;
2316 }
2317
2318 template<typename _RealType, typename _CharT, typename _Traits>
2319 std::basic_istream<_CharT, _Traits>&
2320 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2321 student_t_distribution<_RealType>& __x)
2322 {
2323 using param_type
2324 = typename student_t_distribution<_RealType>::param_type;
2325 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2326
2327 const typename __ios_base::fmtflags __flags = __is.flags();
2328 __is.flags(__ios_base::dec | __ios_base::skipws);
2329
2330 _RealType __n;
2331 if (__is >> __n >> __x._M_nd >> __x._M_gd)
2332 __x.param(param_type(__n));
2333
2334 __is.flags(__flags);
2335 return __is;
2336 }
2337
2338
2339 template<typename _RealType>
2340 void
2341 gamma_distribution<_RealType>::param_type::
2342 _M_initialize()
2343 {
2344 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
2345
2346 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
2347 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
2348 }
2349
2350 /**
2351 * Marsaglia, G. and Tsang, W. W.
2352 * "A Simple Method for Generating Gamma Variables"
2353 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
2354 */
2355 template<typename _RealType>
2356 template<typename _UniformRandomNumberGenerator>
2359 operator()(_UniformRandomNumberGenerator& __urng,
2360 const param_type& __param)
2361 {
2362 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2363 __aurng(__urng);
2364
2365 result_type __u, __v, __n;
2366 const result_type __a1 = (__param._M_malpha
2367 - _RealType(1.0) / _RealType(3.0));
2368
2369 do
2370 {
2371 do
2372 {
2373 __n = _M_nd(__urng);
2374 __v = result_type(1.0) + __param._M_a2 * __n;
2375 }
2376 while (__v <= 0.0);
2377
2378 __v = __v * __v * __v;
2379 __u = __aurng();
2380 }
2381 while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
2382 && (std::log(__u) > (0.5 * __n * __n + __a1
2383 * (1.0 - __v + std::log(__v)))));
2384
2385 if (__param.alpha() == __param._M_malpha)
2386 return __a1 * __v * __param.beta();
2387 else
2388 {
2389 do
2390 __u = __aurng();
2391 while (__u == 0.0);
2392
2393 return (std::pow(__u, result_type(1.0) / __param.alpha())
2394 * __a1 * __v * __param.beta());
2395 }
2396 }
2397
2398 template<typename _RealType>
2399 template<typename _ForwardIterator,
2400 typename _UniformRandomNumberGenerator>
2401 void
2403 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2404 _UniformRandomNumberGenerator& __urng,
2405 const param_type& __param)
2406 {
2407 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2408 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2409 __aurng(__urng);
2410
2411 result_type __u, __v, __n;
2412 const result_type __a1 = (__param._M_malpha
2413 - _RealType(1.0) / _RealType(3.0));
2414
2415 if (__param.alpha() == __param._M_malpha)
2416 while (__f != __t)
2417 {
2418 do
2419 {
2420 do
2421 {
2422 __n = _M_nd(__urng);
2423 __v = result_type(1.0) + __param._M_a2 * __n;
2424 }
2425 while (__v <= 0.0);
2426
2427 __v = __v * __v * __v;
2428 __u = __aurng();
2429 }
2430 while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
2431 && (std::log(__u) > (0.5 * __n * __n + __a1
2432 * (1.0 - __v + std::log(__v)))));
2433
2434 *__f++ = __a1 * __v * __param.beta();
2435 }
2436 else
2437 while (__f != __t)
2438 {
2439 do
2440 {
2441 do
2442 {
2443 __n = _M_nd(__urng);
2444 __v = result_type(1.0) + __param._M_a2 * __n;
2445 }
2446 while (__v <= 0.0);
2447
2448 __v = __v * __v * __v;
2449 __u = __aurng();
2450 }
2451 while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
2452 && (std::log(__u) > (0.5 * __n * __n + __a1
2453 * (1.0 - __v + std::log(__v)))));
2454
2455 do
2456 __u = __aurng();
2457 while (__u == 0.0);
2458
2459 *__f++ = (std::pow(__u, result_type(1.0) / __param.alpha())
2460 * __a1 * __v * __param.beta());
2461 }
2462 }
2463
2464 template<typename _RealType, typename _CharT, typename _Traits>
2465 std::basic_ostream<_CharT, _Traits>&
2466 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2467 const gamma_distribution<_RealType>& __x)
2468 {
2469 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2470
2471 const typename __ios_base::fmtflags __flags = __os.flags();
2472 const _CharT __fill = __os.fill();
2473 const std::streamsize __precision = __os.precision();
2474 const _CharT __space = __os.widen(' ');
2475 __os.flags(__ios_base::scientific | __ios_base::left);
2476 __os.fill(__space);
2478
2479 __os << __x.alpha() << __space << __x.beta()
2480 << __space << __x._M_nd;
2481
2482 __os.flags(__flags);
2483 __os.fill(__fill);
2484 __os.precision(__precision);
2485 return __os;
2486 }
2487
2488 template<typename _RealType, typename _CharT, typename _Traits>
2489 std::basic_istream<_CharT, _Traits>&
2490 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2491 gamma_distribution<_RealType>& __x)
2492 {
2493 using param_type = typename gamma_distribution<_RealType>::param_type;
2494 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2495
2496 const typename __ios_base::fmtflags __flags = __is.flags();
2497 __is.flags(__ios_base::dec | __ios_base::skipws);
2498
2499 _RealType __alpha_val, __beta_val;
2500 if (__is >> __alpha_val >> __beta_val >> __x._M_nd)
2501 __x.param(param_type(__alpha_val, __beta_val));
2502
2503 __is.flags(__flags);
2504 return __is;
2505 }
2506
2507
2508 template<typename _RealType>
2509 template<typename _UniformRandomNumberGenerator>
2512 operator()(_UniformRandomNumberGenerator& __urng,
2513 const param_type& __p)
2514 {
2515 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2516 __aurng(__urng);
2517 return __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
2518 result_type(1) / __p.a());
2519 }
2520
2521 template<typename _RealType>
2522 template<typename _ForwardIterator,
2523 typename _UniformRandomNumberGenerator>
2524 void
2526 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2527 _UniformRandomNumberGenerator& __urng,
2528 const param_type& __p)
2529 {
2530 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2531 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2532 __aurng(__urng);
2533 auto __inv_a = result_type(1) / __p.a();
2534
2535 while (__f != __t)
2536 *__f++ = __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
2537 __inv_a);
2538 }
2539
2540 template<typename _RealType, typename _CharT, typename _Traits>
2543 const weibull_distribution<_RealType>& __x)
2544 {
2545 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2546
2547 const typename __ios_base::fmtflags __flags = __os.flags();
2548 const _CharT __fill = __os.fill();
2549 const std::streamsize __precision = __os.precision();
2550 const _CharT __space = __os.widen(' ');
2551 __os.flags(__ios_base::scientific | __ios_base::left);
2552 __os.fill(__space);
2554
2555 __os << __x.a() << __space << __x.b();
2556
2557 __os.flags(__flags);
2558 __os.fill(__fill);
2559 __os.precision(__precision);
2560 return __os;
2561 }
2562
2563 template<typename _RealType, typename _CharT, typename _Traits>
2564 std::basic_istream<_CharT, _Traits>&
2567 {
2568 using param_type = typename weibull_distribution<_RealType>::param_type;
2569 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2570
2571 const typename __ios_base::fmtflags __flags = __is.flags();
2572 __is.flags(__ios_base::dec | __ios_base::skipws);
2573
2574 _RealType __a, __b;
2575 if (__is >> __a >> __b)
2576 __x.param(param_type(__a, __b));
2577
2578 __is.flags(__flags);
2579 return __is;
2580 }
2581
2582
2583 template<typename _RealType>
2584 template<typename _UniformRandomNumberGenerator>
2587 operator()(_UniformRandomNumberGenerator& __urng,
2588 const param_type& __p)
2589 {
2590 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2591 __aurng(__urng);
2592 return __p.a() - __p.b() * std::log(-std::log(result_type(1)
2593 - __aurng()));
2594 }
2595
2596 template<typename _RealType>
2597 template<typename _ForwardIterator,
2598 typename _UniformRandomNumberGenerator>
2599 void
2601 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2602 _UniformRandomNumberGenerator& __urng,
2603 const param_type& __p)
2604 {
2605 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2606 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2607 __aurng(__urng);
2608
2609 while (__f != __t)
2610 *__f++ = __p.a() - __p.b() * std::log(-std::log(result_type(1)
2611 - __aurng()));
2612 }
2613
2614 template<typename _RealType, typename _CharT, typename _Traits>
2617 const extreme_value_distribution<_RealType>& __x)
2618 {
2619 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2620
2621 const typename __ios_base::fmtflags __flags = __os.flags();
2622 const _CharT __fill = __os.fill();
2623 const std::streamsize __precision = __os.precision();
2624 const _CharT __space = __os.widen(' ');
2625 __os.flags(__ios_base::scientific | __ios_base::left);
2626 __os.fill(__space);
2628
2629 __os << __x.a() << __space << __x.b();
2630
2631 __os.flags(__flags);
2632 __os.fill(__fill);
2633 __os.precision(__precision);
2634 return __os;
2635 }
2636
2637 template<typename _RealType, typename _CharT, typename _Traits>
2638 std::basic_istream<_CharT, _Traits>&
2641 {
2642 using param_type
2644 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2645
2646 const typename __ios_base::fmtflags __flags = __is.flags();
2647 __is.flags(__ios_base::dec | __ios_base::skipws);
2648
2649 _RealType __a, __b;
2650 if (__is >> __a >> __b)
2651 __x.param(param_type(__a, __b));
2652
2653 __is.flags(__flags);
2654 return __is;
2655 }
2656
2657
2658 template<typename _IntType>
2659 void
2660 discrete_distribution<_IntType>::param_type::
2661 _M_initialize()
2662 {
2663 if (_M_prob.size() < 2)
2664 {
2665 _M_prob.clear();
2666 return;
2667 }
2668
2669 const double __sum = std::accumulate(_M_prob.begin(),
2670 _M_prob.end(), 0.0);
2671 __glibcxx_assert(__sum > 0);
2672 // Now normalize the probabilites.
2673 __detail::__normalize(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
2674 __sum);
2675 // Accumulate partial sums.
2676 _M_cp.reserve(_M_prob.size());
2677 std::partial_sum(_M_prob.begin(), _M_prob.end(),
2678 std::back_inserter(_M_cp));
2679 // Make sure the last cumulative probability is one.
2680 _M_cp[_M_cp.size() - 1] = 1.0;
2681 }
2682
2683 template<typename _IntType>
2684 template<typename _Func>
2685 discrete_distribution<_IntType>::param_type::
2686 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2687 : _M_prob(), _M_cp()
2688 {
2689 const size_t __n = __nw == 0 ? 1 : __nw;
2690 const double __delta = (__xmax - __xmin) / __n;
2691
2692 _M_prob.reserve(__n);
2693 for (size_t __k = 0; __k < __nw; ++__k)
2694 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2695
2696 _M_initialize();
2697 }
2698
2699 template<typename _IntType>
2700 template<typename _UniformRandomNumberGenerator>
2701 typename discrete_distribution<_IntType>::result_type
2702 discrete_distribution<_IntType>::
2703 operator()(_UniformRandomNumberGenerator& __urng,
2704 const param_type& __param)
2706 if (__param._M_cp.empty())
2707 return result_type(0);
2708
2709 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2710 __aurng(__urng);
2711
2712 const double __p = __aurng();
2713 auto __pos = std::lower_bound(__param._M_cp.begin(),
2714 __param._M_cp.end(), __p);
2715
2716 return __pos - __param._M_cp.begin();
2717 }
2718
2719 template<typename _IntType>
2720 template<typename _ForwardIterator,
2721 typename _UniformRandomNumberGenerator>
2722 void
2724 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2725 _UniformRandomNumberGenerator& __urng,
2726 const param_type& __param)
2727 {
2728 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2729
2730 if (__param._M_cp.empty())
2731 {
2732 while (__f != __t)
2733 *__f++ = result_type(0);
2734 return;
2735 }
2736
2737 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2738 __aurng(__urng);
2739
2740 while (__f != __t)
2741 {
2742 const double __p = __aurng();
2743 auto __pos = std::lower_bound(__param._M_cp.begin(),
2744 __param._M_cp.end(), __p);
2745
2746 *__f++ = __pos - __param._M_cp.begin();
2747 }
2748 }
2749
2750 template<typename _IntType, typename _CharT, typename _Traits>
2753 const discrete_distribution<_IntType>& __x)
2754 {
2755 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2756
2757 const typename __ios_base::fmtflags __flags = __os.flags();
2758 const _CharT __fill = __os.fill();
2759 const std::streamsize __precision = __os.precision();
2760 const _CharT __space = __os.widen(' ');
2761 __os.flags(__ios_base::scientific | __ios_base::left);
2762 __os.fill(__space);
2764
2765 std::vector<double> __prob = __x.probabilities();
2766 __os << __prob.size();
2767 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2768 __os << __space << *__dit;
2769
2770 __os.flags(__flags);
2771 __os.fill(__fill);
2772 __os.precision(__precision);
2773 return __os;
2774 }
2775
2776namespace __detail
2777{
2778 template<typename _ValT, typename _CharT, typename _Traits>
2779 basic_istream<_CharT, _Traits>&
2780 __extract_params(basic_istream<_CharT, _Traits>& __is,
2781 vector<_ValT>& __vals, size_t __n)
2782 {
2783 __vals.reserve(__n);
2784 while (__n--)
2785 {
2786 _ValT __val;
2787 if (__is >> __val)
2788 __vals.push_back(__val);
2789 else
2790 break;
2791 }
2792 return __is;
2793 }
2794} // namespace __detail
2795
2796 template<typename _IntType, typename _CharT, typename _Traits>
2799 discrete_distribution<_IntType>& __x)
2800 {
2801 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
2802
2803 const typename __ios_base::fmtflags __flags = __is.flags();
2804 __is.flags(__ios_base::dec | __ios_base::skipws);
2805
2806 size_t __n;
2807 if (__is >> __n)
2808 {
2809 std::vector<double> __prob_vec;
2810 if (__detail::__extract_params(__is, __prob_vec, __n))
2811 __x.param({__prob_vec.begin(), __prob_vec.end()});
2812 }
2813
2814 __is.flags(__flags);
2815 return __is;
2816 }
2817
2818
2819 template<typename _RealType>
2820 void
2821 piecewise_constant_distribution<_RealType>::param_type::
2822 _M_initialize()
2823 {
2824 if (_M_int.size() < 2
2825 || (_M_int.size() == 2
2826 && _M_int[0] == _RealType(0)
2827 && _M_int[1] == _RealType(1)))
2828 {
2829 _M_int.clear();
2830 _M_den.clear();
2831 return;
2832 }
2833
2834 const double __sum = std::accumulate(_M_den.begin(),
2835 _M_den.end(), 0.0);
2836 __glibcxx_assert(__sum > 0);
2837
2838 __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
2839 __sum);
2840
2841 _M_cp.reserve(_M_den.size());
2842 std::partial_sum(_M_den.begin(), _M_den.end(),
2843 std::back_inserter(_M_cp));
2844
2845 // Make sure the last cumulative probability is one.
2846 _M_cp[_M_cp.size() - 1] = 1.0;
2847
2848 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2849 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2850 }
2851
2852 template<typename _RealType>
2853 template<typename _InputIteratorB, typename _InputIteratorW>
2854 piecewise_constant_distribution<_RealType>::param_type::
2855 param_type(_InputIteratorB __bbegin,
2856 _InputIteratorB __bend,
2857 _InputIteratorW __wbegin)
2858 : _M_int(), _M_den(), _M_cp()
2859 {
2860 if (__bbegin != __bend)
2861 {
2862 for (;;)
2863 {
2864 _M_int.push_back(*__bbegin);
2865 ++__bbegin;
2866 if (__bbegin == __bend)
2867 break;
2868
2869 _M_den.push_back(*__wbegin);
2870 ++__wbegin;
2871 }
2872 }
2873
2874 _M_initialize();
2875 }
2876
2877 template<typename _RealType>
2878 template<typename _Func>
2881 : _M_int(), _M_den(), _M_cp()
2882 {
2883 _M_int.reserve(__bl.size());
2884 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2885 _M_int.push_back(*__biter);
2886
2887 _M_den.reserve(_M_int.size() - 1);
2888 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2889 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2890
2891 _M_initialize();
2892 }
2893
2894 template<typename _RealType>
2895 template<typename _Func>
2897 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2898 : _M_int(), _M_den(), _M_cp()
2899 {
2900 const size_t __n = __nw == 0 ? 1 : __nw;
2901 const _RealType __delta = (__xmax - __xmin) / __n;
2902
2903 _M_int.reserve(__n + 1);
2904 for (size_t __k = 0; __k <= __nw; ++__k)
2905 _M_int.push_back(__xmin + __k * __delta);
2906
2907 _M_den.reserve(__n);
2908 for (size_t __k = 0; __k < __nw; ++__k)
2909 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2910
2911 _M_initialize();
2912 }
2913
2914 template<typename _RealType>
2915 template<typename _UniformRandomNumberGenerator>
2918 operator()(_UniformRandomNumberGenerator& __urng,
2919 const param_type& __param)
2920 {
2921 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2922 __aurng(__urng);
2923
2924 const double __p = __aurng();
2925 if (__param._M_cp.empty())
2926 return __p;
2927
2928 auto __pos = std::lower_bound(__param._M_cp.begin(),
2929 __param._M_cp.end(), __p);
2930 const size_t __i = __pos - __param._M_cp.begin();
2931
2932 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2933
2934 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2935 }
2936
2937 template<typename _RealType>
2938 template<typename _ForwardIterator,
2939 typename _UniformRandomNumberGenerator>
2940 void
2942 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2943 _UniformRandomNumberGenerator& __urng,
2944 const param_type& __param)
2945 {
2946 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2947 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2948 __aurng(__urng);
2949
2950 if (__param._M_cp.empty())
2951 {
2952 while (__f != __t)
2953 *__f++ = __aurng();
2954 return;
2955 }
2956
2957 while (__f != __t)
2958 {
2959 const double __p = __aurng();
2960
2961 auto __pos = std::lower_bound(__param._M_cp.begin(),
2962 __param._M_cp.end(), __p);
2963 const size_t __i = __pos - __param._M_cp.begin();
2964
2965 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2966
2967 *__f++ = (__param._M_int[__i]
2968 + (__p - __pref) / __param._M_den[__i]);
2969 }
2970 }
2971
2972 template<typename _RealType, typename _CharT, typename _Traits>
2973 std::basic_ostream<_CharT, _Traits>&
2974 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2976 {
2977 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
2978
2979 const typename __ios_base::fmtflags __flags = __os.flags();
2980 const _CharT __fill = __os.fill();
2981 const std::streamsize __precision = __os.precision();
2982 const _CharT __space = __os.widen(' ');
2983 __os.flags(__ios_base::scientific | __ios_base::left);
2984 __os.fill(__space);
2986
2987 std::vector<_RealType> __int = __x.intervals();
2988 __os << __int.size() - 1;
2989
2990 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2991 __os << __space << *__xit;
2992
2993 std::vector<double> __den = __x.densities();
2994 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2995 __os << __space << *__dit;
2996
2997 __os.flags(__flags);
2998 __os.fill(__fill);
2999 __os.precision(__precision);
3000 return __os;
3001 }
3002
3003 template<typename _RealType, typename _CharT, typename _Traits>
3004 std::basic_istream<_CharT, _Traits>&
3005 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3007 {
3008 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
3009
3010 const typename __ios_base::fmtflags __flags = __is.flags();
3011 __is.flags(__ios_base::dec | __ios_base::skipws);
3012
3013 size_t __n;
3014 if (__is >> __n)
3015 {
3016 std::vector<_RealType> __int_vec;
3017 if (__detail::__extract_params(__is, __int_vec, __n + 1))
3018 {
3019 std::vector<double> __den_vec;
3020 if (__detail::__extract_params(__is, __den_vec, __n))
3021 {
3022 __x.param({ __int_vec.begin(), __int_vec.end(),
3023 __den_vec.begin() });
3024 }
3025 }
3026 }
3027
3028 __is.flags(__flags);
3029 return __is;
3030 }
3031
3032
3033 template<typename _RealType>
3034 void
3037 {
3038 if (_M_int.size() < 2
3039 || (_M_int.size() == 2
3040 && _M_int[0] == _RealType(0)
3041 && _M_int[1] == _RealType(1)
3042 && _M_den[0] == _M_den[1]))
3043 {
3044 _M_int.clear();
3045 _M_den.clear();
3046 return;
3047 }
3048
3049 double __sum = 0.0;
3050 _M_cp.reserve(_M_int.size() - 1);
3051 _M_m.reserve(_M_int.size() - 1);
3052 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
3053 {
3054 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
3055 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
3056 _M_cp.push_back(__sum);
3057 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
3058 }
3059 __glibcxx_assert(__sum > 0);
3060
3061 // Now normalize the densities...
3062 __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
3063 __sum);
3064 // ... and partial sums...
3065 __detail::__normalize(_M_cp.begin(), _M_cp.end(), _M_cp.begin(), __sum);
3066 // ... and slopes.
3067 __detail::__normalize(_M_m.begin(), _M_m.end(), _M_m.begin(), __sum);
3068
3069 // Make sure the last cumulative probablility is one.
3070 _M_cp[_M_cp.size() - 1] = 1.0;
3071 }
3072
3073 template<typename _RealType>
3074 template<typename _InputIteratorB, typename _InputIteratorW>
3075 piecewise_linear_distribution<_RealType>::param_type::
3076 param_type(_InputIteratorB __bbegin,
3077 _InputIteratorB __bend,
3078 _InputIteratorW __wbegin)
3079 : _M_int(), _M_den(), _M_cp(), _M_m()
3080 {
3081 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
3082 {
3083 _M_int.push_back(*__bbegin);
3084 _M_den.push_back(*__wbegin);
3085 }
3086
3087 _M_initialize();
3088 }
3089
3090 template<typename _RealType>
3091 template<typename _Func>
3094 : _M_int(), _M_den(), _M_cp(), _M_m()
3095 {
3096 _M_int.reserve(__bl.size());
3097 _M_den.reserve(__bl.size());
3098 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
3099 {
3100 _M_int.push_back(*__biter);
3101 _M_den.push_back(__fw(*__biter));
3102 }
3103
3104 _M_initialize();
3105 }
3106
3107 template<typename _RealType>
3108 template<typename _Func>
3110 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
3111 : _M_int(), _M_den(), _M_cp(), _M_m()
3112 {
3113 const size_t __n = __nw == 0 ? 1 : __nw;
3114 const _RealType __delta = (__xmax - __xmin) / __n;
3115
3116 _M_int.reserve(__n + 1);
3117 _M_den.reserve(__n + 1);
3118 for (size_t __k = 0; __k <= __nw; ++__k)
3119 {
3120 _M_int.push_back(__xmin + __k * __delta);
3121 _M_den.push_back(__fw(_M_int[__k] + __delta));
3122 }
3123
3124 _M_initialize();
3125 }
3126
3127 template<typename _RealType>
3128 template<typename _UniformRandomNumberGenerator>
3131 operator()(_UniformRandomNumberGenerator& __urng,
3132 const param_type& __param)
3133 {
3134 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3135 __aurng(__urng);
3136
3137 const double __p = __aurng();
3138 if (__param._M_cp.empty())
3139 return __p;
3140
3141 auto __pos = std::lower_bound(__param._M_cp.begin(),
3142 __param._M_cp.end(), __p);
3143 const size_t __i = __pos - __param._M_cp.begin();
3144
3145 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
3146
3147 const double __a = 0.5 * __param._M_m[__i];
3148 const double __b = __param._M_den[__i];
3149 const double __cm = __p - __pref;
3150
3151 _RealType __x = __param._M_int[__i];
3152 if (__a == 0)
3153 __x += __cm / __b;
3154 else
3155 {
3156 const double __d = __b * __b + 4.0 * __a * __cm;
3157 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
3158 }
3159
3160 return __x;
3161 }
3162
3163 template<typename _RealType>
3164 template<typename _ForwardIterator,
3165 typename _UniformRandomNumberGenerator>
3166 void
3168 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3169 _UniformRandomNumberGenerator& __urng,
3170 const param_type& __param)
3171 {
3172 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3173 // We could duplicate everything from operator()...
3174 while (__f != __t)
3175 *__f++ = this->operator()(__urng, __param);
3176 }
3177
3178 template<typename _RealType, typename _CharT, typename _Traits>
3179 std::basic_ostream<_CharT, _Traits>&
3180 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3182 {
3183 using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
3184
3185 const typename __ios_base::fmtflags __flags = __os.flags();
3186 const _CharT __fill = __os.fill();
3187 const std::streamsize __precision = __os.precision();
3188 const _CharT __space = __os.widen(' ');
3189 __os.flags(__ios_base::scientific | __ios_base::left);
3190 __os.fill(__space);
3192
3193 std::vector<_RealType> __int = __x.intervals();
3194 __os << __int.size() - 1;
3195
3196 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
3197 __os << __space << *__xit;
3198
3199 std::vector<double> __den = __x.densities();
3200 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
3201 __os << __space << *__dit;
3202
3203 __os.flags(__flags);
3204 __os.fill(__fill);
3205 __os.precision(__precision);
3206 return __os;
3207 }
3208
3209 template<typename _RealType, typename _CharT, typename _Traits>
3210 std::basic_istream<_CharT, _Traits>&
3211 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3213 {
3214 using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
3215
3216 const typename __ios_base::fmtflags __flags = __is.flags();
3217 __is.flags(__ios_base::dec | __ios_base::skipws);
3218
3219 size_t __n;
3220 if (__is >> __n)
3221 {
3222 vector<_RealType> __int_vec;
3223 if (__detail::__extract_params(__is, __int_vec, __n + 1))
3224 {
3225 vector<double> __den_vec;
3226 if (__detail::__extract_params(__is, __den_vec, __n + 1))
3227 {
3228 __x.param({ __int_vec.begin(), __int_vec.end(),
3229 __den_vec.begin() });
3230 }
3231 }
3232 }
3233 __is.flags(__flags);
3234 return __is;
3235 }
3236
3237
3238 template<typename _IntType, typename>
3239 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
3240 {
3241 _M_v.reserve(__il.size());
3242 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
3243 _M_v.push_back(__detail::__mod<result_type,
3244 __detail::_Shift<result_type, 32>::__value>(*__iter));
3245 }
3246
3247 template<typename _InputIterator>
3248 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
3249 {
3250#pragma GCC diagnostic push
3251#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
3252 if constexpr (__is_random_access_iter<_InputIterator>::value)
3253 _M_v.reserve(std::distance(__begin, __end));
3254#pragma GCC diagnostic pop
3255
3256 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
3257 _M_v.push_back(__detail::__mod<result_type,
3258 __detail::_Shift<result_type, 32>::__value>(*__iter));
3259 }
3260
3261 template<typename _RandomAccessIterator>
3262 void
3263 seed_seq::generate(_RandomAccessIterator __begin,
3264 _RandomAccessIterator __end)
3265 {
3266 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3267 _Type;
3268
3269 if (__begin == __end)
3270 return;
3271
3272 std::fill(__begin, __end, _Type(0x8b8b8b8bu));
3273
3274 const size_t __n = __end - __begin;
3275 const size_t __s = _M_v.size();
3276 const size_t __t = (__n >= 623) ? 11
3277 : (__n >= 68) ? 7
3278 : (__n >= 39) ? 5
3279 : (__n >= 7) ? 3
3280 : (__n - 1) / 2;
3281 const size_t __p = (__n - __t) / 2;
3282 const size_t __q = __p + __t;
3283 const size_t __m = std::max(size_t(__s + 1), __n);
3284
3285#ifndef __UINT32_TYPE__
3286 struct _Up
3287 {
3288 _Up(uint_least32_t v) : _M_v(v & 0xffffffffu) { }
3289
3290 operator uint_least32_t() const { return _M_v; }
3291
3292 uint_least32_t _M_v;
3293 };
3294 using uint32_t = _Up;
3295#endif
3296
3297 // k == 0, every element in [begin,end) equals 0x8b8b8b8bu
3298 {
3299 uint32_t __r1 = 1371501266u;
3300 uint32_t __r2 = __r1 + __s;
3301 __begin[__p] += __r1;
3302 __begin[__q] = (uint32_t)__begin[__q] + __r2;
3303 __begin[0] = __r2;
3304 }
3305
3306 for (size_t __k = 1; __k <= __s; ++__k)
3307 {
3308 const size_t __kn = __k % __n;
3309 const size_t __kpn = (__k + __p) % __n;
3310 const size_t __kqn = (__k + __q) % __n;
3311 uint32_t __arg = (__begin[__kn]
3312 ^ __begin[__kpn]
3313 ^ __begin[(__k - 1) % __n]);
3314 uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
3315 uint32_t __r2 = __r1 + (uint32_t)__kn + _M_v[__k - 1];
3316 __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
3317 __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
3318 __begin[__kn] = __r2;
3319 }
3320
3321 for (size_t __k = __s + 1; __k < __m; ++__k)
3322 {
3323 const size_t __kn = __k % __n;
3324 const size_t __kpn = (__k + __p) % __n;
3325 const size_t __kqn = (__k + __q) % __n;
3326 uint32_t __arg = (__begin[__kn]
3327 ^ __begin[__kpn]
3328 ^ __begin[(__k - 1) % __n]);
3329 uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
3330 uint32_t __r2 = __r1 + (uint32_t)__kn;
3331 __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
3332 __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
3333 __begin[__kn] = __r2;
3334 }
3335
3336 for (size_t __k = __m; __k < __m + __n; ++__k)
3337 {
3338 const size_t __kn = __k % __n;
3339 const size_t __kpn = (__k + __p) % __n;
3340 const size_t __kqn = (__k + __q) % __n;
3341 uint32_t __arg = (__begin[__kn]
3342 + __begin[__kpn]
3343 + __begin[(__k - 1) % __n]);
3344 uint32_t __r3 = 1566083941u * (__arg ^ (__arg >> 27));
3345 uint32_t __r4 = __r3 - __kn;
3346 __begin[__kpn] ^= __r3;
3347 __begin[__kqn] ^= __r4;
3348 __begin[__kn] = __r4;
3349 }
3350 }
3351
3352 template<typename _RealType, size_t __bits,
3353 typename _UniformRandomNumberGenerator>
3354 _RealType
3355 generate_canonical(_UniformRandomNumberGenerator& __urng)
3356 {
3358 "template argument must be a floating point type");
3359
3360 const size_t __b
3361 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
3362 __bits);
3363 const long double __r = static_cast<long double>(__urng.max())
3364 - static_cast<long double>(__urng.min()) + 1.0L;
3365 const size_t __log2r = std::log(__r) / std::log(2.0L);
3366 const size_t __m = std::max<size_t>(1UL,
3367 (__b + __log2r - 1UL) / __log2r);
3368 _RealType __ret;
3369 _RealType __sum = _RealType(0);
3370 _RealType __tmp = _RealType(1);
3371 for (size_t __k = __m; __k != 0; --__k)
3372 {
3373 __sum += _RealType(__urng() - __urng.min()) * __tmp;
3374 __tmp *= __r;
3375 }
3376 __ret = __sum / __tmp;
3377 if (__builtin_expect(__ret >= _RealType(1), 0))
3378 {
3379#if _GLIBCXX_USE_C99_MATH_FUNCS
3380 __ret = std::nextafter(_RealType(1), _RealType(0));
3381#else
3382 __ret = _RealType(1)
3383 - std::numeric_limits<_RealType>::epsilon() / _RealType(2);
3384#endif
3385 }
3386 return __ret;
3387 }
3388
3389_GLIBCXX_END_NAMESPACE_VERSION
3390} // namespace
3391
3392#endif
complex< _Tp > log(const complex< _Tp > &)
Return complex natural logarithm of z.
Definition complex:1162
complex< _Tp > tan(const complex< _Tp > &)
Return complex tangent of z.
Definition complex:1298
_Tp abs(const complex< _Tp > &)
Return magnitude of z.
Definition complex:968
complex< _Tp > exp(const complex< _Tp > &)
Return complex base e exponential of z.
Definition complex:1135
complex< _Tp > pow(const complex< _Tp > &, int)
Return x to the y'th power.
Definition complex:1357
complex< _Tp > sqrt(const complex< _Tp > &)
Return complex square root of z.
Definition complex:1271
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
_RealType generate_canonical(_UniformRandomNumberGenerator &__g)
A function template for converting the output of a (integral) uniform random number generator to a fl...
constexpr back_insert_iterator< _Container > back_inserter(_Container &__x)
constexpr _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
Accumulate values in a range.
constexpr _OutputIterator partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Return list of partial sums.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr _Tp __lg(_Tp __n)
This is a helper function for the sort routines and for random.tcc.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1637
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1733
Implementation details not part of the namespace std interface.
initializer_list
char_type widen(char __c) const
Widens characters.
Definition basic_ios.h:464
char_type fill() const
Retrieves the empty character.
Definition basic_ios.h:387
Template class basic_istream.
Definition istream:63
Template class basic_ostream.
Definition ostream.h:67
static constexpr bool is_integer
Definition limits:233
static constexpr int max_digits10
Definition limits:226
static constexpr int digits
Definition limits:218
static constexpr bool is_signed
Definition limits:230
static constexpr _Tp max() noexcept
Definition limits:328
static constexpr _Tp epsilon() noexcept
Definition limits:340
is_floating_point
Definition type_traits:545
common_type
Definition type_traits:2529
streamsize precision() const
Flags access.
Definition ios_base.h:765
fmtflags flags() const
Access to format flags.
Definition ios_base.h:694
A model of a linear congruential random number generator.
Definition random.h:367
static constexpr result_type multiplier
Definition random.h:382
static constexpr result_type modulus
Definition random.h:386
void seed(result_type __s=default_seed)
Reseeds the linear_congruential_engine random number generator engine sequence to the seed __s.
static constexpr result_type increment
Definition random.h:384
The Marsaglia-Zaman generator.
Definition random.h:814
void seed(result_type __sd=0u)
Seeds the initial state of the random number generator.
result_type operator()()
Gets the next random number in the sequence.
result_type operator()()
Gets the next value in the generated random number sequence.
result_type operator()()
Gets the next value in the generated random number sequence.
Produces random numbers by reordering random numbers from some base engine.
Definition random.h:1464
const _RandomNumberEngine & base() const noexcept
Definition random.h:1570
_RandomNumberEngine::result_type result_type
Definition random.h:1470
Uniform continuous distribution for random numbers.
Definition random.h:1882
param_type param() const
Returns the parameter set of the distribution.
Definition random.h:1971
A normal continuous distribution for random numbers.
Definition random.h:2119
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:2238
A gamma continuous distribution for random numbers.
Definition random.h:2571
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:2700
A cauchy_distribution random number distribution.
Definition random.h:3043
param_type param() const
Returns the parameter set of the distribution.
Definition random.h:3120
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:3150
A fisher_f_distribution random number distribution.
Definition random.h:3258
param_type param() const
Returns the parameter set of the distribution.
Definition random.h:3339
A student_t_distribution random number distribution.
Definition random.h:3497
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:4082
A discrete geometric random number distribution.
Definition random.h:4200
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:4311
param_type param() const
Returns the parameter set of the distribution.
Definition random.h:4281
A negative_binomial_distribution random number distribution.
Definition random.h:4417
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
A discrete Poisson random number distribution.
Definition random.h:4654
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:4767
friend bool operator==(const poisson_distribution &__d1, const poisson_distribution &__d2)
Return true if two Poisson distributions have the same parameters and the sequences that would be gen...
Definition random.h:4803
friend std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const std::poisson_distribution< _IntType1 > &__x)
Inserts a poisson_distribution random number distribution __x into the output stream __os.
friend std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, std::poisson_distribution< _IntType1 > &__x)
Extracts a poisson_distribution random number distribution __x from the input stream __is.
An exponential continuous distribution for random numbers.
Definition random.h:4886
param_type param() const
Returns the parameter set of the distribution.
Definition random.h:4966
A weibull_distribution random number distribution.
Definition random.h:5108
param_type param() const
Returns the parameter set of the distribution.
Definition random.h:5188
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:5218
A extreme_value_distribution random number distribution.
Definition random.h:5325
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:5435
param_type param() const
Returns the parameter set of the distribution.
Definition random.h:5405
A discrete_distribution random number distribution.
Definition random.h:5547
A piecewise_constant_distribution random number distribution.
Definition random.h:5795
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:5967
A piecewise_linear_distribution random number distribution.
Definition random.h:6075
result_type operator()(_UniformRandomNumberGenerator &__urng)
Generating functions.
Definition random.h:6249
seed_seq() noexcept
Definition random.h:6367
uint_least32_t result_type
Definition random.h:6364
A standard container which offers fixed time access to individual elements in any order.
Definition stl_vector.h:461
constexpr iterator end() noexcept
constexpr iterator begin() noexcept
Definition stl_vector.h:988
constexpr size_type size() const noexcept
Uniform discrete distribution for random numbers. A discrete random distribution on the range with e...
param_type param() const
Returns the parameter set of the distribution.