]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/random.tcc
87c46186b57eefb2a999d7c9dfaedc8912c036b1
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / random.tcc
1 // random number generation (out of line) -*- C++ -*-
2
3 // Copyright (C) 2009 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 * You should not attempt to use it directly.
28 */
29
30 #include <numeric>
31 #include <algorithm>
32
33 namespace std
34 {
35 /*
36 * (Further) implementation-space details.
37 */
38 namespace __detail
39 {
40 // General case for x = (ax + c) mod m -- use Schrage's algorithm to
41 // avoid integer overflow.
42 //
43 // Because a and c are compile-time integral constants the compiler
44 // kindly elides any unreachable paths.
45 //
46 // Preconditions: a > 0, m > 0.
47 //
48 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
49 struct _Mod
50 {
51 static _Tp
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
81 // Special case for m == 0 -- use unsigned integer overflow as modulo
82 // operator.
83 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
84 struct _Mod<_Tp, __a, __c, __m, true>
85 {
86 static _Tp
87 __calc(_Tp __x)
88 { return __a * __x + __c; }
89 };
90 } // namespace __detail
91
92 /**
93 * Seeds the LCR with integral value @p __s, adjusted so that the
94 * ring identity is never a member of the convergence set.
95 */
96 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
97 void
98 linear_congruential_engine<_UIntType, __a, __c, __m>::
99 seed(result_type __s)
100 {
101 if ((__detail::__mod<_UIntType, 1U, 0U, __m>(__c) == 0U)
102 && (__detail::__mod<_UIntType, 1U, 0U, __m>(__s) == 0U))
103 _M_x = __detail::__mod<_UIntType, 1U, 0U, __m>(1U);
104 else
105 _M_x = __detail::__mod<_UIntType, 1U, 0U, __m>(__s);
106 }
107
108 /**
109 * Seeds the LCR engine with a value generated by @p __q.
110 */
111 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
112 void
113 linear_congruential_engine<_UIntType, __a, __c, __m>::
114 seed(seed_seq& __q)
115 {
116 const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
117 : std::__lg(__m);
118 const _UIntType __k = (__k0 + 31) / 32;
119 _UIntType __arr[__k + 3];
120 __q.generate(__arr + 0, __arr + __k + 3);
121 _UIntType __factor = 1U;
122 _UIntType __sum = 0U;
123 for (size_t __j = 0; __j < __k; ++__j)
124 {
125 __sum += __arr[__j + 3] * __factor;
126 __factor *= __detail::_Shift<_UIntType, 32>::__value;
127 }
128 seed(__sum);
129 }
130
131 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
132 typename _CharT, typename _Traits>
133 std::basic_ostream<_CharT, _Traits>&
134 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
135 const linear_congruential_engine<_UIntType,
136 __a, __c, __m>& __lcr)
137 {
138 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
139 typedef typename __ostream_type::ios_base __ios_base;
140
141 const typename __ios_base::fmtflags __flags = __os.flags();
142 const _CharT __fill = __os.fill();
143 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
144 __os.fill(__os.widen(' '));
145
146 __os << __lcr._M_x;
147
148 __os.flags(__flags);
149 __os.fill(__fill);
150 return __os;
151 }
152
153 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
154 typename _CharT, typename _Traits>
155 std::basic_istream<_CharT, _Traits>&
156 operator>>(std::basic_istream<_CharT, _Traits>& __is,
157 linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
158 {
159 typedef std::basic_istream<_CharT, _Traits> __istream_type;
160 typedef typename __istream_type::ios_base __ios_base;
161
162 const typename __ios_base::fmtflags __flags = __is.flags();
163 __is.flags(__ios_base::dec);
164
165 __is >> __lcr._M_x;
166
167 __is.flags(__flags);
168 return __is;
169 }
170
171
172 template<typename _UIntType,
173 size_t __w, size_t __n, size_t __m, size_t __r,
174 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
175 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
176 _UIntType __f>
177 void
178 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
179 __s, __b, __t, __c, __l, __f>::
180 seed(result_type __sd)
181 {
182 _M_x[0] = __detail::__mod<_UIntType, 1, 0,
183 __detail::_Shift<_UIntType, __w>::__value>(__sd);
184
185 for (size_t __i = 1; __i < state_size; ++__i)
186 {
187 _UIntType __x = _M_x[__i - 1];
188 __x ^= __x >> (__w - 2);
189 __x *= __f;
190 __x += __i;
191 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
192 __detail::_Shift<_UIntType, __w>::__value>(__x);
193 }
194 _M_p = state_size;
195 }
196
197 template<typename _UIntType,
198 size_t __w, size_t __n, size_t __m, size_t __r,
199 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
200 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
201 _UIntType __f>
202 void
203 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
204 __s, __b, __t, __c, __l, __f>::
205 seed(seed_seq& __q)
206 {
207 const _UIntType __upper_mask = (~_UIntType()) << __r;
208 const size_t __k = (__w + 31) / 32;
209 _UIntType __arr[__k * __n];
210 __q.generate(__arr + 0, __arr + __k * __n);
211
212 bool __zero = true;
213 for (size_t __i = 0; __i < state_size; ++__i)
214 {
215 _UIntType __factor = 1U;
216 _UIntType __sum = 0U;
217 for (size_t __j = 0; __j < __k; ++__j)
218 {
219 __sum += __arr[__i * __k + __j] * __factor;
220 __factor *= __detail::_Shift<_UIntType, 32>::__value;
221 }
222 _M_x[__i] = __detail::__mod<_UIntType, 1U, 0U,
223 __detail::_Shift<_UIntType, __w>::__value>(__sum);
224
225 if (__zero)
226 {
227 if (__i == 0)
228 {
229 if ((_M_x[0] & __upper_mask) != 0U)
230 __zero = false;
231 }
232 else if (_M_x[__i] != 0U)
233 __zero = false;
234 }
235 }
236 if (__zero)
237 _M_x[0] = __detail::_Shift<_UIntType, __w - 1U>::__value;
238 }
239
240 template<typename _UIntType, size_t __w,
241 size_t __n, size_t __m, size_t __r,
242 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
243 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
244 _UIntType __f>
245 typename
246 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
247 __s, __b, __t, __c, __l, __f>::result_type
248 mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
249 __s, __b, __t, __c, __l, __f>::
250 operator()()
251 {
252 // Reload the vector - cost is O(n) amortized over n calls.
253 if (_M_p >= state_size)
254 {
255 const _UIntType __upper_mask = (~_UIntType()) << __r;
256 const _UIntType __lower_mask = ~__upper_mask;
257
258 for (size_t __k = 0; __k < (__n - __m); ++__k)
259 {
260 _UIntType __y = ((_M_x[__k] & __upper_mask)
261 | (_M_x[__k + 1] & __lower_mask));
262 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
263 ^ ((__y & 0x01) ? __a : 0));
264 }
265
266 for (size_t __k = (__n - __m); __k < (__n - 1); ++__k)
267 {
268 _UIntType __y = ((_M_x[__k] & __upper_mask)
269 | (_M_x[__k + 1] & __lower_mask));
270 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
271 ^ ((__y & 0x01) ? __a : 0));
272 }
273
274 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
275 | (_M_x[0] & __lower_mask));
276 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
277 ^ ((__y & 0x01) ? __a : 0));
278 _M_p = 0;
279 }
280
281 // Calculate o(x(i)).
282 result_type __z = _M_x[_M_p++];
283 __z ^= (__z >> __u) & __d;
284 __z ^= (__z << __s) & __b;
285 __z ^= (__z << __t) & __c;
286 __z ^= (__z >> __l);
287
288 return __z;
289 }
290
291 template<typename _UIntType, size_t __w,
292 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, typename _CharT, typename _Traits>
296 std::basic_ostream<_CharT, _Traits>&
297 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
298 const mersenne_twister_engine<_UIntType, __w, __n, __m,
299 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
300 {
301 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
302 typedef typename __ostream_type::ios_base __ios_base;
303
304 const typename __ios_base::fmtflags __flags = __os.flags();
305 const _CharT __fill = __os.fill();
306 const _CharT __space = __os.widen(' ');
307 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
308 __os.fill(__space);
309
310 for (size_t __i = 0; __i < __n - 1; ++__i)
311 __os << __x._M_x[__i] << __space;
312 __os << __x._M_x[__n - 1];
313
314 __os.flags(__flags);
315 __os.fill(__fill);
316 return __os;
317 }
318
319 template<typename _UIntType, size_t __w,
320 size_t __n, size_t __m, size_t __r,
321 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
322 _UIntType __b, size_t __t, _UIntType __c, size_t __l,
323 _UIntType __f, typename _CharT, typename _Traits>
324 std::basic_istream<_CharT, _Traits>&
325 operator>>(std::basic_istream<_CharT, _Traits>& __is,
326 mersenne_twister_engine<_UIntType, __w, __n, __m,
327 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
328 {
329 typedef std::basic_istream<_CharT, _Traits> __istream_type;
330 typedef typename __istream_type::ios_base __ios_base;
331
332 const typename __ios_base::fmtflags __flags = __is.flags();
333 __is.flags(__ios_base::dec | __ios_base::skipws);
334
335 for (size_t __i = 0; __i < __n; ++__i)
336 __is >> __x._M_x[__i];
337
338 __is.flags(__flags);
339 return __is;
340 }
341
342
343 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
344 void
345 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
346 seed(result_type __value)
347 {
348 if (__value == 0)
349 __value = default_seed;
350
351 std::linear_congruential_engine<result_type, 40014U, 0U, 2147483563U>
352 __lcg(__value);
353
354 // I hope this is right. The "10000" tests work for the ranluxen.
355 const size_t __n = (word_size + 31) / 32;
356
357 for (size_t __i = 0; __i < long_lag; ++__i)
358 {
359 _UIntType __sum = 0U;
360 _UIntType __factor = 1U;
361 for (size_t __j = 0; __j < __n; ++__j)
362 {
363 __sum += __detail::__mod<__detail::_UInt32Type, 1, 0, 0>
364 (__lcg()) * __factor;
365 __factor *= __detail::_Shift<_UIntType, 32>::__value;
366 }
367 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
368 __detail::_Shift<_UIntType, __w>::__value>(__sum);
369 }
370 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
371 _M_p = 0;
372 }
373
374 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
375 void
376 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
377 seed(seed_seq& __q)
378 {
379 const size_t __n = (word_size + 31) / 32;
380 _UIntType __arr[long_lag + __n];
381 __q.generate(__arr + 0, __arr + long_lag + __n);
382
383 for (size_t __i = 0; __i < long_lag; ++__i)
384 {
385 _UIntType __sum = 0U;
386 _UIntType __factor = 1U;
387 for (size_t __j = 0; __j < __n; ++__j)
388 {
389 __sum += __detail::__mod<__detail::_UInt32Type, 1, 0, 0>
390 (__arr[__i * __n + __j]) * __factor;
391 __factor *= __detail::_Shift<_UIntType, 32>::__value;
392 }
393 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
394 __detail::_Shift<_UIntType, __w>::__value>(__sum);
395 }
396 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
397 _M_p = 0;
398 }
399
400 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
401 typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::
402 result_type
403 subtract_with_carry_engine<_UIntType, __w, __s, __r>::
404 operator()()
405 {
406 // Derive short lag index from current index.
407 long __ps = _M_p - short_lag;
408 if (__ps < 0)
409 __ps += long_lag;
410
411 // Calculate new x(i) without overflow or division.
412 // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry
413 // cannot overflow.
414 _UIntType __xi;
415 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
416 {
417 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
418 _M_carry = 0;
419 }
420 else
421 {
422 __xi = (__detail::_Shift<_UIntType, __w>::__value
423 - _M_x[_M_p] - _M_carry + _M_x[__ps]);
424 _M_carry = 1;
425 }
426 _M_x[_M_p] = __xi;
427
428 // Adjust current index to loop around in ring buffer.
429 if (++_M_p >= long_lag)
430 _M_p = 0;
431
432 return __xi;
433 }
434
435 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
436 typename _CharT, typename _Traits>
437 std::basic_ostream<_CharT, _Traits>&
438 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
439 const subtract_with_carry_engine<_UIntType,
440 __w, __s, __r>& __x)
441 {
442 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
443 typedef typename __ostream_type::ios_base __ios_base;
444
445 const typename __ios_base::fmtflags __flags = __os.flags();
446 const _CharT __fill = __os.fill();
447 const _CharT __space = __os.widen(' ');
448 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
449 __os.fill(__space);
450
451 for (size_t __i = 0; __i < __r; ++__i)
452 __os << __x._M_x[__i] << __space;
453 __os << __x._M_carry;
454
455 __os.flags(__flags);
456 __os.fill(__fill);
457 return __os;
458 }
459
460 template<typename _UIntType, size_t __w, size_t __s, size_t __r,
461 typename _CharT, typename _Traits>
462 std::basic_istream<_CharT, _Traits>&
463 operator>>(std::basic_istream<_CharT, _Traits>& __is,
464 subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
465 {
466 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
467 typedef typename __istream_type::ios_base __ios_base;
468
469 const typename __ios_base::fmtflags __flags = __is.flags();
470 __is.flags(__ios_base::dec | __ios_base::skipws);
471
472 for (size_t __i = 0; __i < __r; ++__i)
473 __is >> __x._M_x[__i];
474 __is >> __x._M_carry;
475
476 __is.flags(__flags);
477 return __is;
478 }
479
480
481 template<typename _RandomNumberEngine, size_t __p, size_t __r>
482 typename discard_block_engine<_RandomNumberEngine,
483 __p, __r>::result_type
484 discard_block_engine<_RandomNumberEngine, __p, __r>::
485 operator()()
486 {
487 if (_M_n >= used_block)
488 {
489 _M_b.discard(block_size - _M_n);
490 _M_n = 0;
491 }
492 ++_M_n;
493 return _M_b();
494 }
495
496 template<typename _RandomNumberEngine, size_t __p, size_t __r,
497 typename _CharT, typename _Traits>
498 std::basic_ostream<_CharT, _Traits>&
499 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
500 const discard_block_engine<_RandomNumberEngine,
501 __p, __r>& __x)
502 {
503 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
504 typedef typename __ostream_type::ios_base __ios_base;
505
506 const typename __ios_base::fmtflags __flags = __os.flags();
507 const _CharT __fill = __os.fill();
508 const _CharT __space = __os.widen(' ');
509 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
510 __os.fill(__space);
511
512 __os << __x.base() << __space << __x._M_n;
513
514 __os.flags(__flags);
515 __os.fill(__fill);
516 return __os;
517 }
518
519 template<typename _RandomNumberEngine, size_t __p, size_t __r,
520 typename _CharT, typename _Traits>
521 std::basic_istream<_CharT, _Traits>&
522 operator>>(std::basic_istream<_CharT, _Traits>& __is,
523 discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
524 {
525 typedef std::basic_istream<_CharT, _Traits> __istream_type;
526 typedef typename __istream_type::ios_base __ios_base;
527
528 const typename __ios_base::fmtflags __flags = __is.flags();
529 __is.flags(__ios_base::dec | __ios_base::skipws);
530
531 __is >> __x._M_b >> __x._M_n;
532
533 __is.flags(__flags);
534 return __is;
535 }
536
537
538 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
539 typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
540 result_type
541 independent_bits_engine<_RandomNumberEngine, __w, _UIntType>::
542 operator()()
543 {
544 const long double __r = static_cast<long double>(_M_b.max())
545 - static_cast<long double>(_M_b.min()) + 1.0L;
546 const result_type __m = std::log(__r) / std::log(2.0L);
547 result_type __n, __n0, __y0, __y1, __s0, __s1;
548 for (size_t __i = 0; __i < 2; ++__i)
549 {
550 __n = (__w + __m - 1) / __m + __i;
551 __n0 = __n - __w % __n;
552 const result_type __w0 = __w / __n;
553 const result_type __w1 = __w0 + 1;
554 __s0 = result_type(1) << __w0;
555 __s1 = result_type(1) << __w1;
556 __y0 = __s0 * (__r / __s0);
557 __y1 = __s1 * (__r / __s1);
558 if (__r - __y0 <= __y0 / __n)
559 break;
560 }
561
562 result_type __sum = 0;
563 for (size_t __k = 0; __k < __n0; ++__k)
564 {
565 result_type __u;
566 do
567 __u = _M_b() - _M_b.min();
568 while (__u >= __y0);
569 __sum = __s0 * __sum + __u % __s0;
570 }
571 for (size_t __k = __n0; __k < __n; ++__k)
572 {
573 result_type __u;
574 do
575 __u = _M_b() - _M_b.min();
576 while (__u >= __y1);
577 __sum = __s1 * __sum + __u % __s1;
578 }
579 return __sum;
580 }
581
582
583 template<typename _RandomNumberEngine, size_t __k>
584 typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
585 shuffle_order_engine<_RandomNumberEngine, __k>::
586 operator()()
587 {
588 size_t __j = __k * ((_M_y - _M_b.min())
589 / (_M_b.max() - _M_b.min() + 1.0L));
590 _M_y = _M_v[__j];
591 _M_v[__j] = _M_b();
592
593 return _M_y;
594 }
595
596 template<typename _RandomNumberEngine, size_t __k,
597 typename _CharT, typename _Traits>
598 std::basic_ostream<_CharT, _Traits>&
599 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
600 const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
601 {
602 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
603 typedef typename __ostream_type::ios_base __ios_base;
604
605 const typename __ios_base::fmtflags __flags = __os.flags();
606 const _CharT __fill = __os.fill();
607 const _CharT __space = __os.widen(' ');
608 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
609 __os.fill(__space);
610
611 __os << __x.base();
612 for (size_t __i = 0; __i < __k; ++__i)
613 __os << __space << __x._M_v[__i];
614 __os << __space << __x._M_y;
615
616 __os.flags(__flags);
617 __os.fill(__fill);
618 return __os;
619 }
620
621 template<typename _RandomNumberEngine, size_t __k,
622 typename _CharT, typename _Traits>
623 std::basic_istream<_CharT, _Traits>&
624 operator>>(std::basic_istream<_CharT, _Traits>& __is,
625 shuffle_order_engine<_RandomNumberEngine, __k>& __x)
626 {
627 typedef std::basic_istream<_CharT, _Traits> __istream_type;
628 typedef typename __istream_type::ios_base __ios_base;
629
630 const typename __ios_base::fmtflags __flags = __is.flags();
631 __is.flags(__ios_base::dec | __ios_base::skipws);
632
633 __is >> __x._M_b;
634 for (size_t __i = 0; __i < __k; ++__i)
635 __is >> __x._M_v[__i];
636 __is >> __x._M_y;
637
638 __is.flags(__flags);
639 return __is;
640 }
641
642
643 template<typename _IntType>
644 template<typename _UniformRandomNumberGenerator>
645 typename uniform_int_distribution<_IntType>::result_type
646 uniform_int_distribution<_IntType>::
647 operator()(_UniformRandomNumberGenerator& __urng,
648 const param_type& __param)
649 {
650 // XXX Must be fixed to work well for *arbitrary* __urng.max(),
651 // __urng.min(), __param.b(), __param.a(). Currently works fine only
652 // in the most common case __urng.max() - __urng.min() >=
653 // __param.b() - __param.a(), with __urng.max() > __urng.min() >= 0.
654 typedef typename __gnu_cxx::__add_unsigned<typename
655 _UniformRandomNumberGenerator::result_type>::__type __urntype;
656 typedef typename __gnu_cxx::__add_unsigned<result_type>::__type
657 __utype;
658 typedef typename __gnu_cxx::__conditional_type<(sizeof(__urntype)
659 > sizeof(__utype)),
660 __urntype, __utype>::__type __uctype;
661
662 result_type __ret;
663
664 const __urntype __urnmin = __urng.min();
665 const __urntype __urnmax = __urng.max();
666 const __urntype __urnrange = __urnmax - __urnmin;
667 const __uctype __urange = __param.b() - __param.a();
668 const __uctype __udenom = (__urnrange <= __urange
669 ? 1 : __urnrange / (__urange + 1));
670 do
671 __ret = (__urntype(__urng()) - __urnmin) / __udenom;
672 while (__ret > __param.b() - __param.a());
673
674 return __ret + __param.a();
675 }
676
677 template<typename _IntType, typename _CharT, typename _Traits>
678 std::basic_ostream<_CharT, _Traits>&
679 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
680 const uniform_int_distribution<_IntType>& __x)
681 {
682 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
683 typedef typename __ostream_type::ios_base __ios_base;
684
685 const typename __ios_base::fmtflags __flags = __os.flags();
686 const _CharT __fill = __os.fill();
687 const _CharT __space = __os.widen(' ');
688 __os.flags(__ios_base::scientific | __ios_base::left);
689 __os.fill(__space);
690
691 __os << __x.a() << __space << __x.b();
692
693 __os.flags(__flags);
694 __os.fill(__fill);
695 return __os;
696 }
697
698 template<typename _IntType, typename _CharT, typename _Traits>
699 std::basic_istream<_CharT, _Traits>&
700 operator>>(std::basic_istream<_CharT, _Traits>& __is,
701 uniform_int_distribution<_IntType>& __x)
702 {
703 typedef std::basic_istream<_CharT, _Traits> __istream_type;
704 typedef typename __istream_type::ios_base __ios_base;
705
706 const typename __ios_base::fmtflags __flags = __is.flags();
707 __is.flags(__ios_base::dec | __ios_base::skipws);
708
709 _IntType __a, __b;
710 __is >> __a >> __b;
711 __x.param(typename uniform_int_distribution<_IntType>::
712 param_type(__a, __b));
713
714 __is.flags(__flags);
715 return __is;
716 }
717
718
719 template<typename _RealType, typename _CharT, typename _Traits>
720 std::basic_ostream<_CharT, _Traits>&
721 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
722 const uniform_real_distribution<_RealType>& __x)
723 {
724 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
725 typedef typename __ostream_type::ios_base __ios_base;
726
727 const typename __ios_base::fmtflags __flags = __os.flags();
728 const _CharT __fill = __os.fill();
729 const std::streamsize __precision = __os.precision();
730 const _CharT __space = __os.widen(' ');
731 __os.flags(__ios_base::scientific | __ios_base::left);
732 __os.fill(__space);
733 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
734
735 __os << __x.a() << __space << __x.b();
736
737 __os.flags(__flags);
738 __os.fill(__fill);
739 __os.precision(__precision);
740 return __os;
741 }
742
743 template<typename _RealType, typename _CharT, typename _Traits>
744 std::basic_istream<_CharT, _Traits>&
745 operator>>(std::basic_istream<_CharT, _Traits>& __is,
746 uniform_real_distribution<_RealType>& __x)
747 {
748 typedef std::basic_istream<_CharT, _Traits> __istream_type;
749 typedef typename __istream_type::ios_base __ios_base;
750
751 const typename __ios_base::fmtflags __flags = __is.flags();
752 __is.flags(__ios_base::skipws);
753
754 _RealType __a, __b;
755 __is >> __a >> __b;
756 __x.param(typename uniform_real_distribution<_RealType>::
757 param_type(__a, __b));
758
759 __is.flags(__flags);
760 return __is;
761 }
762
763
764 template<typename _CharT, typename _Traits>
765 std::basic_ostream<_CharT, _Traits>&
766 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
767 const bernoulli_distribution& __x)
768 {
769 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
770 typedef typename __ostream_type::ios_base __ios_base;
771
772 const typename __ios_base::fmtflags __flags = __os.flags();
773 const _CharT __fill = __os.fill();
774 const std::streamsize __precision = __os.precision();
775 __os.flags(__ios_base::scientific | __ios_base::left);
776 __os.fill(__os.widen(' '));
777 __os.precision(std::numeric_limits<double>::digits10 + 1);
778
779 __os << __x.p();
780
781 __os.flags(__flags);
782 __os.fill(__fill);
783 __os.precision(__precision);
784 return __os;
785 }
786
787
788 template<typename _IntType>
789 template<typename _UniformRandomNumberGenerator>
790 typename geometric_distribution<_IntType>::result_type
791 geometric_distribution<_IntType>::
792 operator()(_UniformRandomNumberGenerator& __urng,
793 const param_type& __param)
794 {
795 // About the epsilon thing see this thread:
796 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
797 const double __naf =
798 (1 - std::numeric_limits<double>::epsilon()) / 2;
799 // The largest _RealType convertible to _IntType.
800 const double __thr =
801 std::numeric_limits<_IntType>::max() + __naf;
802 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
803 __aurng(__urng);
804
805 double __cand;
806 do
807 __cand = std::ceil(std::log(__aurng()) / __param._M_log_p);
808 while (__cand >= __thr);
809
810 return result_type(__cand + __naf);
811 }
812
813 template<typename _IntType,
814 typename _CharT, typename _Traits>
815 std::basic_ostream<_CharT, _Traits>&
816 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
817 const geometric_distribution<_IntType>& __x)
818 {
819 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
820 typedef typename __ostream_type::ios_base __ios_base;
821
822 const typename __ios_base::fmtflags __flags = __os.flags();
823 const _CharT __fill = __os.fill();
824 const std::streamsize __precision = __os.precision();
825 __os.flags(__ios_base::scientific | __ios_base::left);
826 __os.fill(__os.widen(' '));
827 __os.precision(std::numeric_limits<double>::digits10 + 1);
828
829 __os << __x.p();
830
831 __os.flags(__flags);
832 __os.fill(__fill);
833 __os.precision(__precision);
834 return __os;
835 }
836
837 template<typename _IntType,
838 typename _CharT, typename _Traits>
839 std::basic_istream<_CharT, _Traits>&
840 operator>>(std::basic_istream<_CharT, _Traits>& __is,
841 geometric_distribution<_IntType>& __x)
842 {
843 typedef std::basic_istream<_CharT, _Traits> __istream_type;
844 typedef typename __istream_type::ios_base __ios_base;
845
846 const typename __ios_base::fmtflags __flags = __is.flags();
847 __is.flags(__ios_base::skipws);
848
849 double __p;
850 __is >> __p;
851 __x.param(typename geometric_distribution<_IntType>::param_type(__p));
852
853 __is.flags(__flags);
854 return __is;
855 }
856
857
858 template<typename _IntType>
859 template<typename _UniformRandomNumberGenerator>
860 typename negative_binomial_distribution<_IntType>::result_type
861 negative_binomial_distribution<_IntType>::
862 operator()(_UniformRandomNumberGenerator& __urng)
863 {
864 const double __y = _M_gd(__urng);
865
866 // XXX Is the constructor too slow?
867 std::poisson_distribution<result_type> __poisson(__y);
868 return __poisson(__urng);
869 }
870
871 template<typename _IntType>
872 template<typename _UniformRandomNumberGenerator>
873 typename negative_binomial_distribution<_IntType>::result_type
874 negative_binomial_distribution<_IntType>::
875 operator()(_UniformRandomNumberGenerator& __urng,
876 const param_type& __p)
877 {
878 typedef typename std::gamma_distribution<result_type>::param_type
879 param_type;
880
881 const double __y =
882 _M_gd(__urng, param_type(__p.k(), __p.p() / (1.0 - __p.p())));
883
884 std::poisson_distribution<result_type> __poisson(__y);
885 return __poisson(__urng);
886 }
887
888 template<typename _IntType, typename _CharT, typename _Traits>
889 std::basic_ostream<_CharT, _Traits>&
890 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
891 const negative_binomial_distribution<_IntType>& __x)
892 {
893 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
894 typedef typename __ostream_type::ios_base __ios_base;
895
896 const typename __ios_base::fmtflags __flags = __os.flags();
897 const _CharT __fill = __os.fill();
898 const std::streamsize __precision = __os.precision();
899 const _CharT __space = __os.widen(' ');
900 __os.flags(__ios_base::scientific | __ios_base::left);
901 __os.fill(__os.widen(' '));
902 __os.precision(std::numeric_limits<double>::digits10 + 1);
903
904 __os << __x.k() << __space << __x.p()
905 << __space << __x._M_gd;
906
907 __os.flags(__flags);
908 __os.fill(__fill);
909 __os.precision(__precision);
910 return __os;
911 }
912
913 template<typename _IntType, typename _CharT, typename _Traits>
914 std::basic_istream<_CharT, _Traits>&
915 operator>>(std::basic_istream<_CharT, _Traits>& __is,
916 negative_binomial_distribution<_IntType>& __x)
917 {
918 typedef std::basic_istream<_CharT, _Traits> __istream_type;
919 typedef typename __istream_type::ios_base __ios_base;
920
921 const typename __ios_base::fmtflags __flags = __is.flags();
922 __is.flags(__ios_base::skipws);
923
924 _IntType __k;
925 double __p;
926 __is >> __k >> __p >> __x._M_gd;
927 __x.param(typename negative_binomial_distribution<_IntType>::
928 param_type(__k, __p));
929
930 __is.flags(__flags);
931 return __is;
932 }
933
934
935 template<typename _IntType>
936 void
937 poisson_distribution<_IntType>::param_type::
938 _M_initialize()
939 {
940 #if _GLIBCXX_USE_C99_MATH_TR1
941 if (_M_mean >= 12)
942 {
943 const double __m = std::floor(_M_mean);
944 _M_lm_thr = std::log(_M_mean);
945 _M_lfm = std::lgamma(__m + 1);
946 _M_sm = std::sqrt(__m);
947
948 const double __pi_4 = 0.7853981633974483096156608458198757L;
949 const double __dx = std::sqrt(2 * __m * std::log(32 * __m
950 / __pi_4));
951 _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
952 const double __cx = 2 * __m + _M_d;
953 _M_scx = std::sqrt(__cx / 2);
954 _M_1cx = 1 / __cx;
955
956 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
957 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2))
958 / _M_d;
959 }
960 else
961 #endif
962 _M_lm_thr = std::exp(-_M_mean);
963 }
964
965 /**
966 * A rejection algorithm when mean >= 12 and a simple method based
967 * upon the multiplication of uniform random variates otherwise.
968 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
969 * is defined.
970 *
971 * Reference:
972 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
973 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
974 */
975 template<typename _IntType>
976 template<typename _UniformRandomNumberGenerator>
977 typename poisson_distribution<_IntType>::result_type
978 poisson_distribution<_IntType>::
979 operator()(_UniformRandomNumberGenerator& __urng,
980 const param_type& __param)
981 {
982 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
983 __aurng(__urng);
984 #if _GLIBCXX_USE_C99_MATH_TR1
985 if (__param.mean() >= 12)
986 {
987 double __x;
988
989 // See comments above...
990 const double __naf =
991 (1 - std::numeric_limits<double>::epsilon()) / 2;
992 const double __thr =
993 std::numeric_limits<_IntType>::max() + __naf;
994
995 const double __m = std::floor(__param.mean());
996 // sqrt(pi / 2)
997 const double __spi_2 = 1.2533141373155002512078826424055226L;
998 const double __c1 = __param._M_sm * __spi_2;
999 const double __c2 = __param._M_c2b + __c1;
1000 const double __c3 = __c2 + 1;
1001 const double __c4 = __c3 + 1;
1002 // e^(1 / 78)
1003 const double __e178 = 1.0129030479320018583185514777512983L;
1004 const double __c5 = __c4 + __e178;
1005 const double __c = __param._M_cb + __c5;
1006 const double __2cx = 2 * (2 * __m + __param._M_d);
1007
1008 bool __reject = true;
1009 do
1010 {
1011 const double __u = __c * __aurng();
1012 const double __e = -std::log(__aurng());
1013
1014 double __w = 0.0;
1015
1016 if (__u <= __c1)
1017 {
1018 const double __n = _M_nd(__urng);
1019 const double __y = -std::abs(__n) * __param._M_sm - 1;
1020 __x = std::floor(__y);
1021 __w = -__n * __n / 2;
1022 if (__x < -__m)
1023 continue;
1024 }
1025 else if (__u <= __c2)
1026 {
1027 const double __n = _M_nd(__urng);
1028 const double __y = 1 + std::abs(__n) * __param._M_scx;
1029 __x = std::ceil(__y);
1030 __w = __y * (2 - __y) * __param._M_1cx;
1031 if (__x > __param._M_d)
1032 continue;
1033 }
1034 else if (__u <= __c3)
1035 // NB: This case not in the book, nor in the Errata,
1036 // but should be ok...
1037 __x = -1;
1038 else if (__u <= __c4)
1039 __x = 0;
1040 else if (__u <= __c5)
1041 __x = 1;
1042 else
1043 {
1044 const double __v = -std::log(__aurng());
1045 const double __y = __param._M_d
1046 + __v * __2cx / __param._M_d;
1047 __x = std::ceil(__y);
1048 __w = -__param._M_d * __param._M_1cx * (1 + __y / 2);
1049 }
1050
1051 __reject = (__w - __e - __x * __param._M_lm_thr
1052 > __param._M_lfm - std::lgamma(__x + __m + 1));
1053
1054 __reject |= __x + __m >= __thr;
1055
1056 } while (__reject);
1057
1058 return result_type(__x + __m + __naf);
1059 }
1060 else
1061 #endif
1062 {
1063 _IntType __x = 0;
1064 double __prod = 1.0;
1065
1066 do
1067 {
1068 __prod *= __aurng();
1069 __x += 1;
1070 }
1071 while (__prod > __param._M_lm_thr);
1072
1073 return __x - 1;
1074 }
1075 }
1076
1077 template<typename _IntType,
1078 typename _CharT, typename _Traits>
1079 std::basic_ostream<_CharT, _Traits>&
1080 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1081 const poisson_distribution<_IntType>& __x)
1082 {
1083 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1084 typedef typename __ostream_type::ios_base __ios_base;
1085
1086 const typename __ios_base::fmtflags __flags = __os.flags();
1087 const _CharT __fill = __os.fill();
1088 const std::streamsize __precision = __os.precision();
1089 const _CharT __space = __os.widen(' ');
1090 __os.flags(__ios_base::scientific | __ios_base::left);
1091 __os.fill(__space);
1092 __os.precision(std::numeric_limits<double>::digits10 + 1);
1093
1094 __os << __x.mean() << __space << __x._M_nd;
1095
1096 __os.flags(__flags);
1097 __os.fill(__fill);
1098 __os.precision(__precision);
1099 return __os;
1100 }
1101
1102 template<typename _IntType,
1103 typename _CharT, typename _Traits>
1104 std::basic_istream<_CharT, _Traits>&
1105 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1106 poisson_distribution<_IntType>& __x)
1107 {
1108 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1109 typedef typename __istream_type::ios_base __ios_base;
1110
1111 const typename __ios_base::fmtflags __flags = __is.flags();
1112 __is.flags(__ios_base::skipws);
1113
1114 double __mean;
1115 __is >> __mean >> __x._M_nd;
1116 __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
1117
1118 __is.flags(__flags);
1119 return __is;
1120 }
1121
1122
1123 template<typename _IntType>
1124 void
1125 binomial_distribution<_IntType>::param_type::
1126 _M_initialize()
1127 {
1128 const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1129
1130 _M_easy = true;
1131
1132 #if _GLIBCXX_USE_C99_MATH_TR1
1133 if (_M_t * __p12 >= 8)
1134 {
1135 _M_easy = false;
1136 const double __np = std::floor(_M_t * __p12);
1137 const double __pa = __np / _M_t;
1138 const double __1p = 1 - __pa;
1139
1140 const double __pi_4 = 0.7853981633974483096156608458198757L;
1141 const double __d1x =
1142 std::sqrt(__np * __1p * std::log(32 * __np
1143 / (81 * __pi_4 * __1p)));
1144 _M_d1 = std::round(std::max(1.0, __d1x));
1145 const double __d2x =
1146 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1147 / (__pi_4 * __pa)));
1148 _M_d2 = std::round(std::max(1.0, __d2x));
1149
1150 // sqrt(pi / 2)
1151 const double __spi_2 = 1.2533141373155002512078826424055226L;
1152 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1153 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1154 _M_c = 2 * _M_d1 / __np;
1155 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1156 const double __a12 = _M_a1 + _M_s2 * __spi_2;
1157 const double __s1s = _M_s1 * _M_s1;
1158 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1159 * 2 * __s1s / _M_d1
1160 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1161 const double __s2s = _M_s2 * _M_s2;
1162 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1163 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1164 _M_lf = (std::lgamma(__np + 1)
1165 + std::lgamma(_M_t - __np + 1));
1166 _M_lp1p = std::log(__pa / __1p);
1167
1168 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1169 }
1170 else
1171 #endif
1172 _M_q = -std::log(1 - __p12);
1173 }
1174
1175 template<typename _IntType>
1176 template<typename _UniformRandomNumberGenerator>
1177 typename binomial_distribution<_IntType>::result_type
1178 binomial_distribution<_IntType>::
1179 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1180 {
1181 _IntType __x = 0;
1182 double __sum = 0.0;
1183 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1184 __aurng(__urng);
1185
1186 do
1187 {
1188 const double __e = -std::log(__aurng());
1189 __sum += __e / (__t - __x);
1190 __x += 1;
1191 }
1192 while (__sum <= _M_param._M_q);
1193
1194 return __x - 1;
1195 }
1196
1197 /**
1198 * A rejection algorithm when t * p >= 8 and a simple waiting time
1199 * method - the second in the referenced book - otherwise.
1200 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1201 * is defined.
1202 *
1203 * Reference:
1204 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1205 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1206 */
1207 template<typename _IntType>
1208 template<typename _UniformRandomNumberGenerator>
1209 typename binomial_distribution<_IntType>::result_type
1210 binomial_distribution<_IntType>::
1211 operator()(_UniformRandomNumberGenerator& __urng,
1212 const param_type& __param)
1213 {
1214 result_type __ret;
1215 const _IntType __t = __param.t();
1216 const _IntType __p = __param.p();
1217 const double __p12 = __p <= 0.5 ? __p : 1.0 - __p;
1218 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
1219 __aurng(__urng);
1220
1221 #if _GLIBCXX_USE_C99_MATH_TR1
1222 if (!__param._M_easy)
1223 {
1224 double __x;
1225
1226 // See comments above...
1227 const double __naf =
1228 (1 - std::numeric_limits<double>::epsilon()) / 2;
1229 const double __thr =
1230 std::numeric_limits<_IntType>::max() + __naf;
1231
1232 const double __np = std::floor(__t * __p12);
1233
1234 // sqrt(pi / 2)
1235 const double __spi_2 = 1.2533141373155002512078826424055226L;
1236 const double __a1 = __param._M_a1;
1237 const double __a12 = __a1 + __param._M_s2 * __spi_2;
1238 const double __a123 = __param._M_a123;
1239 const double __s1s = __param._M_s1 * __param._M_s1;
1240 const double __s2s = __param._M_s2 * __param._M_s2;
1241
1242 bool __reject;
1243 do
1244 {
1245 const double __u = __param._M_s * __aurng();
1246
1247 double __v;
1248
1249 if (__u <= __a1)
1250 {
1251 const double __n = _M_nd(__urng);
1252 const double __y = __param._M_s1 * std::abs(__n);
1253 __reject = __y >= __param._M_d1;
1254 if (!__reject)
1255 {
1256 const double __e = -std::log(__aurng());
1257 __x = std::floor(__y);
1258 __v = -__e - __n * __n / 2 + __param._M_c;
1259 }
1260 }
1261 else if (__u <= __a12)
1262 {
1263 const double __n = _M_nd(__urng);
1264 const double __y = __param._M_s2 * std::abs(__n);
1265 __reject = __y >= __param._M_d2;
1266 if (!__reject)
1267 {
1268 const double __e = -std::log(__aurng());
1269 __x = std::floor(-__y);
1270 __v = -__e - __n * __n / 2;
1271 }
1272 }
1273 else if (__u <= __a123)
1274 {
1275 const double __e1 = -std::log(__aurng());
1276 const double __e2 = -std::log(__aurng());
1277
1278 const double __y = __param._M_d1
1279 + 2 * __s1s * __e1 / __param._M_d1;
1280 __x = std::floor(__y);
1281 __v = (-__e2 + __param._M_d1 * (1 / (__t - __np)
1282 -__y / (2 * __s1s)));
1283 __reject = false;
1284 }
1285 else
1286 {
1287 const double __e1 = -std::log(__aurng());
1288 const double __e2 = -std::log(__aurng());
1289
1290 const double __y = __param._M_d2
1291 + 2 * __s2s * __e1 / __param._M_d2;
1292 __x = std::floor(-__y);
1293 __v = -__e2 - __param._M_d2 * __y / (2 * __s2s);
1294 __reject = false;
1295 }
1296
1297 __reject = __reject || __x < -__np || __x > __t - __np;
1298 if (!__reject)
1299 {
1300 const double __lfx =
1301 std::lgamma(__np + __x + 1)
1302 + std::lgamma(__t - (__np + __x) + 1);
1303 __reject = __v > __param._M_lf - __lfx
1304 + __x * __param._M_lp1p;
1305 }
1306
1307 __reject |= __x + __np >= __thr;
1308 }
1309 while (__reject);
1310
1311 __x += __np + __naf;
1312
1313 const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
1314 __ret = _IntType(__x) + __z;
1315 }
1316 else
1317 #endif
1318 __ret = _M_waiting(__urng, __t);
1319
1320 if (__p12 != __p)
1321 __ret = __t - __ret;
1322 return __ret;
1323 }
1324
1325 template<typename _IntType,
1326 typename _CharT, typename _Traits>
1327 std::basic_ostream<_CharT, _Traits>&
1328 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1329 const binomial_distribution<_IntType>& __x)
1330 {
1331 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1332 typedef typename __ostream_type::ios_base __ios_base;
1333
1334 const typename __ios_base::fmtflags __flags = __os.flags();
1335 const _CharT __fill = __os.fill();
1336 const std::streamsize __precision = __os.precision();
1337 const _CharT __space = __os.widen(' ');
1338 __os.flags(__ios_base::scientific | __ios_base::left);
1339 __os.fill(__space);
1340 __os.precision(std::numeric_limits<double>::digits10 + 1);
1341
1342 __os << __x.t() << __space << __x.p()
1343 << __space << __x._M_nd;
1344
1345 __os.flags(__flags);
1346 __os.fill(__fill);
1347 __os.precision(__precision);
1348 return __os;
1349 }
1350
1351 template<typename _IntType,
1352 typename _CharT, typename _Traits>
1353 std::basic_istream<_CharT, _Traits>&
1354 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1355 binomial_distribution<_IntType>& __x)
1356 {
1357 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1358 typedef typename __istream_type::ios_base __ios_base;
1359
1360 const typename __ios_base::fmtflags __flags = __is.flags();
1361 __is.flags(__ios_base::dec | __ios_base::skipws);
1362
1363 _IntType __t;
1364 double __p;
1365 __is >> __t >> __p >> __x._M_nd;
1366 __x.param(typename binomial_distribution<_IntType>::
1367 param_type(__t, __p));
1368
1369 __is.flags(__flags);
1370 return __is;
1371 }
1372
1373
1374 template<typename _RealType, typename _CharT, typename _Traits>
1375 std::basic_ostream<_CharT, _Traits>&
1376 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1377 const exponential_distribution<_RealType>& __x)
1378 {
1379 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1380 typedef typename __ostream_type::ios_base __ios_base;
1381
1382 const typename __ios_base::fmtflags __flags = __os.flags();
1383 const _CharT __fill = __os.fill();
1384 const std::streamsize __precision = __os.precision();
1385 __os.flags(__ios_base::scientific | __ios_base::left);
1386 __os.fill(__os.widen(' '));
1387 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1388
1389 __os << __x.lambda();
1390
1391 __os.flags(__flags);
1392 __os.fill(__fill);
1393 __os.precision(__precision);
1394 return __os;
1395 }
1396
1397 template<typename _RealType, typename _CharT, typename _Traits>
1398 std::basic_istream<_CharT, _Traits>&
1399 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1400 exponential_distribution<_RealType>& __x)
1401 {
1402 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1403 typedef typename __istream_type::ios_base __ios_base;
1404
1405 const typename __ios_base::fmtflags __flags = __is.flags();
1406 __is.flags(__ios_base::dec | __ios_base::skipws);
1407
1408 _RealType __lambda;
1409 __is >> __lambda;
1410 __x.param(typename exponential_distribution<_RealType>::
1411 param_type(__lambda));
1412
1413 __is.flags(__flags);
1414 return __is;
1415 }
1416
1417
1418 /**
1419 * Polar method due to Marsaglia.
1420 *
1421 * Devroye, L. "Non-Uniform Random Variates Generation." Springer-Verlag,
1422 * New York, 1986, Ch. V, Sect. 4.4.
1423 */
1424 template<typename _RealType>
1425 template<typename _UniformRandomNumberGenerator>
1426 typename normal_distribution<_RealType>::result_type
1427 normal_distribution<_RealType>::
1428 operator()(_UniformRandomNumberGenerator& __urng,
1429 const param_type& __param)
1430 {
1431 result_type __ret;
1432 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1433 __aurng(__urng);
1434
1435 if (_M_saved_available)
1436 {
1437 _M_saved_available = false;
1438 __ret = _M_saved;
1439 }
1440 else
1441 {
1442 result_type __x, __y, __r2;
1443 do
1444 {
1445 __x = result_type(2.0) * __aurng() - 1.0;
1446 __y = result_type(2.0) * __aurng() - 1.0;
1447 __r2 = __x * __x + __y * __y;
1448 }
1449 while (__r2 > 1.0 || __r2 == 0.0);
1450
1451 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1452 _M_saved = __x * __mult;
1453 _M_saved_available = true;
1454 __ret = __y * __mult;
1455 }
1456
1457 __ret = __ret * __param.stddev() + __param.mean();
1458 return __ret;
1459 }
1460
1461 template<typename _RealType, typename _CharT, typename _Traits>
1462 std::basic_ostream<_CharT, _Traits>&
1463 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1464 const normal_distribution<_RealType>& __x)
1465 {
1466 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1467 typedef typename __ostream_type::ios_base __ios_base;
1468
1469 const typename __ios_base::fmtflags __flags = __os.flags();
1470 const _CharT __fill = __os.fill();
1471 const std::streamsize __precision = __os.precision();
1472 const _CharT __space = __os.widen(' ');
1473 __os.flags(__ios_base::scientific | __ios_base::left);
1474 __os.fill(__space);
1475 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1476
1477 __os << __x.mean() << __space << __x.stddev()
1478 << __space << __x._M_saved_available;
1479 if (__x._M_saved_available)
1480 __os << __space << __x._M_saved;
1481
1482 __os.flags(__flags);
1483 __os.fill(__fill);
1484 __os.precision(__precision);
1485 return __os;
1486 }
1487
1488 template<typename _RealType, typename _CharT, typename _Traits>
1489 std::basic_istream<_CharT, _Traits>&
1490 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1491 normal_distribution<_RealType>& __x)
1492 {
1493 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1494 typedef typename __istream_type::ios_base __ios_base;
1495
1496 const typename __ios_base::fmtflags __flags = __is.flags();
1497 __is.flags(__ios_base::dec | __ios_base::skipws);
1498
1499 double __mean, __stddev;
1500 __is >> __mean >> __stddev
1501 >> __x._M_saved_available;
1502 if (__x._M_saved_available)
1503 __is >> __x._M_saved;
1504 __x.param(typename normal_distribution<_RealType>::
1505 param_type(__mean, __stddev));
1506
1507 __is.flags(__flags);
1508 return __is;
1509 }
1510
1511
1512 template<typename _RealType, typename _CharT, typename _Traits>
1513 std::basic_ostream<_CharT, _Traits>&
1514 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1515 const lognormal_distribution<_RealType>& __x)
1516 {
1517 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1518 typedef typename __ostream_type::ios_base __ios_base;
1519
1520 const typename __ios_base::fmtflags __flags = __os.flags();
1521 const _CharT __fill = __os.fill();
1522 const std::streamsize __precision = __os.precision();
1523 const _CharT __space = __os.widen(' ');
1524 __os.flags(__ios_base::scientific | __ios_base::left);
1525 __os.fill(__space);
1526 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1527
1528 __os << __x.m() << __space << __x.s()
1529 << __space << __x._M_nd;
1530
1531 __os.flags(__flags);
1532 __os.fill(__fill);
1533 __os.precision(__precision);
1534 return __os;
1535 }
1536
1537 template<typename _RealType, typename _CharT, typename _Traits>
1538 std::basic_istream<_CharT, _Traits>&
1539 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1540 lognormal_distribution<_RealType>& __x)
1541 {
1542 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1543 typedef typename __istream_type::ios_base __ios_base;
1544
1545 const typename __ios_base::fmtflags __flags = __is.flags();
1546 __is.flags(__ios_base::dec | __ios_base::skipws);
1547
1548 _RealType __m, __s;
1549 __is >> __m >> __s >> __x._M_nd;
1550 __x.param(typename lognormal_distribution<_RealType>::
1551 param_type(__m, __s));
1552
1553 __is.flags(__flags);
1554 return __is;
1555 }
1556
1557
1558 template<typename _RealType, typename _CharT, typename _Traits>
1559 std::basic_ostream<_CharT, _Traits>&
1560 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1561 const chi_squared_distribution<_RealType>& __x)
1562 {
1563 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1564 typedef typename __ostream_type::ios_base __ios_base;
1565
1566 const typename __ios_base::fmtflags __flags = __os.flags();
1567 const _CharT __fill = __os.fill();
1568 const std::streamsize __precision = __os.precision();
1569 const _CharT __space = __os.widen(' ');
1570 __os.flags(__ios_base::scientific | __ios_base::left);
1571 __os.fill(__space);
1572 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1573
1574 __os << __x.n() << __space << __x._M_gd;
1575
1576 __os.flags(__flags);
1577 __os.fill(__fill);
1578 __os.precision(__precision);
1579 return __os;
1580 }
1581
1582 template<typename _RealType, typename _CharT, typename _Traits>
1583 std::basic_istream<_CharT, _Traits>&
1584 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1585 chi_squared_distribution<_RealType>& __x)
1586 {
1587 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1588 typedef typename __istream_type::ios_base __ios_base;
1589
1590 const typename __ios_base::fmtflags __flags = __is.flags();
1591 __is.flags(__ios_base::dec | __ios_base::skipws);
1592
1593 _RealType __n;
1594 __is >> __n >> __x._M_gd;
1595 __x.param(typename chi_squared_distribution<_RealType>::
1596 param_type(__n));
1597
1598 __is.flags(__flags);
1599 return __is;
1600 }
1601
1602
1603 template<typename _RealType>
1604 template<typename _UniformRandomNumberGenerator>
1605 typename cauchy_distribution<_RealType>::result_type
1606 cauchy_distribution<_RealType>::
1607 operator()(_UniformRandomNumberGenerator& __urng,
1608 const param_type& __p)
1609 {
1610 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1611 __aurng(__urng);
1612 _RealType __u;
1613 do
1614 __u = __aurng();
1615 while (__u == 0.5);
1616
1617 const _RealType __pi = 3.1415926535897932384626433832795029L;
1618 return __p.a() + __p.b() * std::tan(__pi * __u);
1619 }
1620
1621 template<typename _RealType, typename _CharT, typename _Traits>
1622 std::basic_ostream<_CharT, _Traits>&
1623 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1624 const cauchy_distribution<_RealType>& __x)
1625 {
1626 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1627 typedef typename __ostream_type::ios_base __ios_base;
1628
1629 const typename __ios_base::fmtflags __flags = __os.flags();
1630 const _CharT __fill = __os.fill();
1631 const std::streamsize __precision = __os.precision();
1632 const _CharT __space = __os.widen(' ');
1633 __os.flags(__ios_base::scientific | __ios_base::left);
1634 __os.fill(__space);
1635 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1636
1637 __os << __x.a() << __space << __x.b();
1638
1639 __os.flags(__flags);
1640 __os.fill(__fill);
1641 __os.precision(__precision);
1642 return __os;
1643 }
1644
1645 template<typename _RealType, typename _CharT, typename _Traits>
1646 std::basic_istream<_CharT, _Traits>&
1647 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1648 cauchy_distribution<_RealType>& __x)
1649 {
1650 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1651 typedef typename __istream_type::ios_base __ios_base;
1652
1653 const typename __ios_base::fmtflags __flags = __is.flags();
1654 __is.flags(__ios_base::dec | __ios_base::skipws);
1655
1656 _RealType __a, __b;
1657 __is >> __a >> __b;
1658 __x.param(typename cauchy_distribution<_RealType>::
1659 param_type(__a, __b));
1660
1661 __is.flags(__flags);
1662 return __is;
1663 }
1664
1665
1666 template<typename _RealType, typename _CharT, typename _Traits>
1667 std::basic_ostream<_CharT, _Traits>&
1668 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1669 const fisher_f_distribution<_RealType>& __x)
1670 {
1671 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1672 typedef typename __ostream_type::ios_base __ios_base;
1673
1674 const typename __ios_base::fmtflags __flags = __os.flags();
1675 const _CharT __fill = __os.fill();
1676 const std::streamsize __precision = __os.precision();
1677 const _CharT __space = __os.widen(' ');
1678 __os.flags(__ios_base::scientific | __ios_base::left);
1679 __os.fill(__space);
1680 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1681
1682 __os << __x.m() << __space << __x.n()
1683 << __space << __x._M_gd_x << __space << __x._M_gd_y;
1684
1685 __os.flags(__flags);
1686 __os.fill(__fill);
1687 __os.precision(__precision);
1688 return __os;
1689 }
1690
1691 template<typename _RealType, typename _CharT, typename _Traits>
1692 std::basic_istream<_CharT, _Traits>&
1693 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1694 fisher_f_distribution<_RealType>& __x)
1695 {
1696 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1697 typedef typename __istream_type::ios_base __ios_base;
1698
1699 const typename __ios_base::fmtflags __flags = __is.flags();
1700 __is.flags(__ios_base::dec | __ios_base::skipws);
1701
1702 _RealType __m, __n;
1703 __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
1704 __x.param(typename fisher_f_distribution<_RealType>::
1705 param_type(__m, __n));
1706
1707 __is.flags(__flags);
1708 return __is;
1709 }
1710
1711
1712 template<typename _RealType, typename _CharT, typename _Traits>
1713 std::basic_ostream<_CharT, _Traits>&
1714 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1715 const student_t_distribution<_RealType>& __x)
1716 {
1717 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1718 typedef typename __ostream_type::ios_base __ios_base;
1719
1720 const typename __ios_base::fmtflags __flags = __os.flags();
1721 const _CharT __fill = __os.fill();
1722 const std::streamsize __precision = __os.precision();
1723 const _CharT __space = __os.widen(' ');
1724 __os.flags(__ios_base::scientific | __ios_base::left);
1725 __os.fill(__space);
1726 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1727
1728 __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd;
1729
1730 __os.flags(__flags);
1731 __os.fill(__fill);
1732 __os.precision(__precision);
1733 return __os;
1734 }
1735
1736 template<typename _RealType, typename _CharT, typename _Traits>
1737 std::basic_istream<_CharT, _Traits>&
1738 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1739 student_t_distribution<_RealType>& __x)
1740 {
1741 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1742 typedef typename __istream_type::ios_base __ios_base;
1743
1744 const typename __ios_base::fmtflags __flags = __is.flags();
1745 __is.flags(__ios_base::dec | __ios_base::skipws);
1746
1747 _RealType __n;
1748 __is >> __n >> __x._M_nd >> __x._M_gd;
1749 __x.param(typename student_t_distribution<_RealType>::param_type(__n));
1750
1751 __is.flags(__flags);
1752 return __is;
1753 }
1754
1755
1756 template<typename _RealType>
1757 void
1758 gamma_distribution<_RealType>::param_type::
1759 _M_initialize()
1760 {
1761 _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha;
1762
1763 const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0);
1764 _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1);
1765 }
1766
1767 /**
1768 * Marsaglia, G. and Tsang, W. W.
1769 * "A Simple Method for Generating Gamma Variables"
1770 * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000.
1771 */
1772 template<typename _RealType>
1773 template<typename _UniformRandomNumberGenerator>
1774 typename gamma_distribution<_RealType>::result_type
1775 gamma_distribution<_RealType>::
1776 operator()(_UniformRandomNumberGenerator& __urng,
1777 const param_type& __param)
1778 {
1779 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1780 __aurng(__urng);
1781
1782 result_type __u, __v, __n;
1783 const result_type __a1 = (__param._M_malpha
1784 - _RealType(1.0) / _RealType(3.0));
1785
1786 do
1787 {
1788 do
1789 {
1790 __n = _M_nd(__urng);
1791 __v = result_type(1.0) + __param._M_a2 * __n;
1792 }
1793 while (__v <= 0.0);
1794
1795 __v = __v * __v * __v;
1796 __u = __aurng();
1797 }
1798 while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
1799 && (std::log(__u) > (0.5 * __n * __n + __a1
1800 * (1.0 - __v + std::log(__v)))));
1801
1802 if (__param.alpha() == __param._M_malpha)
1803 return __a1 * __v * __param.beta();
1804 else
1805 {
1806 do
1807 __u = __aurng();
1808 while (__u == 0.0);
1809
1810 return (std::pow(__u, result_type(1.0) / __param.alpha())
1811 * __a1 * __v * __param.beta());
1812 }
1813 }
1814
1815 template<typename _RealType, typename _CharT, typename _Traits>
1816 std::basic_ostream<_CharT, _Traits>&
1817 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1818 const gamma_distribution<_RealType>& __x)
1819 {
1820 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1821 typedef typename __ostream_type::ios_base __ios_base;
1822
1823 const typename __ios_base::fmtflags __flags = __os.flags();
1824 const _CharT __fill = __os.fill();
1825 const std::streamsize __precision = __os.precision();
1826 const _CharT __space = __os.widen(' ');
1827 __os.flags(__ios_base::scientific | __ios_base::left);
1828 __os.fill(__space);
1829 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1830
1831 __os << __x.alpha() << __space << __x.beta()
1832 << __space << __x._M_nd;
1833
1834 __os.flags(__flags);
1835 __os.fill(__fill);
1836 __os.precision(__precision);
1837 return __os;
1838 }
1839
1840 template<typename _RealType, typename _CharT, typename _Traits>
1841 std::basic_istream<_CharT, _Traits>&
1842 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1843 gamma_distribution<_RealType>& __x)
1844 {
1845 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1846 typedef typename __istream_type::ios_base __ios_base;
1847
1848 const typename __ios_base::fmtflags __flags = __is.flags();
1849 __is.flags(__ios_base::dec | __ios_base::skipws);
1850
1851 _RealType __alpha_val, __beta_val;
1852 __is >> __alpha_val >> __beta_val >> __x._M_nd;
1853 __x.param(typename gamma_distribution<_RealType>::
1854 param_type(__alpha_val, __beta_val));
1855
1856 __is.flags(__flags);
1857 return __is;
1858 }
1859
1860
1861 template<typename _RealType>
1862 template<typename _UniformRandomNumberGenerator>
1863 typename weibull_distribution<_RealType>::result_type
1864 weibull_distribution<_RealType>::
1865 operator()(_UniformRandomNumberGenerator& __urng,
1866 const param_type& __p)
1867 {
1868 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1869 __aurng(__urng);
1870 return __p.b() * std::pow(-std::log(__aurng()),
1871 result_type(1) / __p.a());
1872 }
1873
1874 template<typename _RealType, typename _CharT, typename _Traits>
1875 std::basic_ostream<_CharT, _Traits>&
1876 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1877 const weibull_distribution<_RealType>& __x)
1878 {
1879 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1880 typedef typename __ostream_type::ios_base __ios_base;
1881
1882 const typename __ios_base::fmtflags __flags = __os.flags();
1883 const _CharT __fill = __os.fill();
1884 const std::streamsize __precision = __os.precision();
1885 const _CharT __space = __os.widen(' ');
1886 __os.flags(__ios_base::scientific | __ios_base::left);
1887 __os.fill(__space);
1888 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1889
1890 __os << __x.a() << __space << __x.b();
1891
1892 __os.flags(__flags);
1893 __os.fill(__fill);
1894 __os.precision(__precision);
1895 return __os;
1896 }
1897
1898 template<typename _RealType, typename _CharT, typename _Traits>
1899 std::basic_istream<_CharT, _Traits>&
1900 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1901 weibull_distribution<_RealType>& __x)
1902 {
1903 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1904 typedef typename __istream_type::ios_base __ios_base;
1905
1906 const typename __ios_base::fmtflags __flags = __is.flags();
1907 __is.flags(__ios_base::dec | __ios_base::skipws);
1908
1909 _RealType __a, __b;
1910 __is >> __a >> __b;
1911 __x.param(typename weibull_distribution<_RealType>::
1912 param_type(__a, __b));
1913
1914 __is.flags(__flags);
1915 return __is;
1916 }
1917
1918
1919 template<typename _RealType>
1920 template<typename _UniformRandomNumberGenerator>
1921 typename extreme_value_distribution<_RealType>::result_type
1922 extreme_value_distribution<_RealType>::
1923 operator()(_UniformRandomNumberGenerator& __urng,
1924 const param_type& __p)
1925 {
1926 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1927 __aurng(__urng);
1928 return __p.a() - __p.b() * std::log(-std::log(__aurng()));
1929 }
1930
1931 template<typename _RealType, typename _CharT, typename _Traits>
1932 std::basic_ostream<_CharT, _Traits>&
1933 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1934 const extreme_value_distribution<_RealType>& __x)
1935 {
1936 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1937 typedef typename __ostream_type::ios_base __ios_base;
1938
1939 const typename __ios_base::fmtflags __flags = __os.flags();
1940 const _CharT __fill = __os.fill();
1941 const std::streamsize __precision = __os.precision();
1942 const _CharT __space = __os.widen(' ');
1943 __os.flags(__ios_base::scientific | __ios_base::left);
1944 __os.fill(__space);
1945 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
1946
1947 __os << __x.a() << __space << __x.b();
1948
1949 __os.flags(__flags);
1950 __os.fill(__fill);
1951 __os.precision(__precision);
1952 return __os;
1953 }
1954
1955 template<typename _RealType, typename _CharT, typename _Traits>
1956 std::basic_istream<_CharT, _Traits>&
1957 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1958 extreme_value_distribution<_RealType>& __x)
1959 {
1960 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1961 typedef typename __istream_type::ios_base __ios_base;
1962
1963 const typename __ios_base::fmtflags __flags = __is.flags();
1964 __is.flags(__ios_base::dec | __ios_base::skipws);
1965
1966 _RealType __a, __b;
1967 __is >> __a >> __b;
1968 __x.param(typename extreme_value_distribution<_RealType>::
1969 param_type(__a, __b));
1970
1971 __is.flags(__flags);
1972 return __is;
1973 }
1974
1975
1976 template<typename _IntType>
1977 void
1978 discrete_distribution<_IntType>::param_type::
1979 _M_initialize()
1980 {
1981 if (_M_prob.size() < 2)
1982 {
1983 _M_prob.clear();
1984 _M_prob.push_back(1.0);
1985 return;
1986 }
1987
1988 const double __sum = std::accumulate(_M_prob.begin(),
1989 _M_prob.end(), 0.0);
1990 // Now normalize the probabilites.
1991 std::transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
1992 std::bind2nd(std::divides<double>(), __sum));
1993 // Accumulate partial sums.
1994 _M_cp.reserve(_M_prob.size());
1995 std::partial_sum(_M_prob.begin(), _M_prob.end(),
1996 std::back_inserter(_M_cp));
1997 // Make sure the last cumulative probability is one.
1998 _M_cp[_M_cp.size() - 1] = 1.0;
1999 }
2000
2001 template<typename _IntType>
2002 template<typename _Func>
2003 discrete_distribution<_IntType>::param_type::
2004 param_type(size_t __nw, double __xmin, double __xmax, _Func __fw)
2005 : _M_prob(), _M_cp()
2006 {
2007 const size_t __n = __nw == 0 ? 1 : __nw;
2008 const double __delta = (__xmax - __xmin) / __n;
2009
2010 _M_prob.reserve(__n);
2011 for (size_t __k = 0; __k < __nw; ++__k)
2012 _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta));
2013
2014 _M_initialize();
2015 }
2016
2017 template<typename _IntType>
2018 template<typename _UniformRandomNumberGenerator>
2019 typename discrete_distribution<_IntType>::result_type
2020 discrete_distribution<_IntType>::
2021 operator()(_UniformRandomNumberGenerator& __urng,
2022 const param_type& __param)
2023 {
2024 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2025 __aurng(__urng);
2026
2027 const double __p = __aurng();
2028 auto __pos = std::lower_bound(__param._M_cp.begin(),
2029 __param._M_cp.end(), __p);
2030
2031 return __pos - __param._M_cp.begin();
2032 }
2033
2034 template<typename _IntType, typename _CharT, typename _Traits>
2035 std::basic_ostream<_CharT, _Traits>&
2036 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2037 const discrete_distribution<_IntType>& __x)
2038 {
2039 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2040 typedef typename __ostream_type::ios_base __ios_base;
2041
2042 const typename __ios_base::fmtflags __flags = __os.flags();
2043 const _CharT __fill = __os.fill();
2044 const std::streamsize __precision = __os.precision();
2045 const _CharT __space = __os.widen(' ');
2046 __os.flags(__ios_base::scientific | __ios_base::left);
2047 __os.fill(__space);
2048 __os.precision(std::numeric_limits<double>::digits10 + 1);
2049
2050 std::vector<double> __prob = __x.probabilities();
2051 __os << __prob.size();
2052 for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit)
2053 __os << __space << *__dit;
2054
2055 __os.flags(__flags);
2056 __os.fill(__fill);
2057 __os.precision(__precision);
2058 return __os;
2059 }
2060
2061 template<typename _IntType, typename _CharT, typename _Traits>
2062 std::basic_istream<_CharT, _Traits>&
2063 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2064 discrete_distribution<_IntType>& __x)
2065 {
2066 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2067 typedef typename __istream_type::ios_base __ios_base;
2068
2069 const typename __ios_base::fmtflags __flags = __is.flags();
2070 __is.flags(__ios_base::dec | __ios_base::skipws);
2071
2072 size_t __n;
2073 __is >> __n;
2074
2075 std::vector<double> __prob_vec;
2076 __prob_vec.reserve(__n);
2077 for (; __n != 0; --__n)
2078 {
2079 double __prob;
2080 __is >> __prob;
2081 __prob_vec.push_back(__prob);
2082 }
2083
2084 __x.param(typename discrete_distribution<_IntType>::
2085 param_type(__prob_vec.begin(), __prob_vec.end()));
2086
2087 __is.flags(__flags);
2088 return __is;
2089 }
2090
2091
2092 template<typename _RealType>
2093 void
2094 piecewise_constant_distribution<_RealType>::param_type::
2095 _M_initialize()
2096 {
2097 if (_M_int.size() < 2)
2098 {
2099 _M_int.clear();
2100 _M_int.reserve(2);
2101 _M_int.push_back(_RealType(0));
2102 _M_int.push_back(_RealType(1));
2103
2104 _M_den.clear();
2105 _M_den.push_back(1.0);
2106
2107 return;
2108 }
2109
2110 const double __sum = std::accumulate(_M_den.begin(),
2111 _M_den.end(), 0.0);
2112
2113 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2114 std::bind2nd(std::divides<double>(), __sum));
2115
2116 _M_cp.reserve(_M_den.size());
2117 std::partial_sum(_M_den.begin(), _M_den.end(),
2118 std::back_inserter(_M_cp));
2119
2120 // Make sure the last cumulative probability is one.
2121 _M_cp[_M_cp.size() - 1] = 1.0;
2122
2123 for (size_t __k = 0; __k < _M_den.size(); ++__k)
2124 _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];
2125 }
2126
2127 template<typename _RealType>
2128 template<typename _InputIteratorB, typename _InputIteratorW>
2129 piecewise_constant_distribution<_RealType>::param_type::
2130 param_type(_InputIteratorB __bbegin,
2131 _InputIteratorB __bend,
2132 _InputIteratorW __wbegin)
2133 : _M_int(), _M_den(), _M_cp()
2134 {
2135 if (__bbegin != __bend)
2136 {
2137 for (;;)
2138 {
2139 _M_int.push_back(*__bbegin);
2140 ++__bbegin;
2141 if (__bbegin == __bend)
2142 break;
2143
2144 _M_den.push_back(*__wbegin);
2145 ++__wbegin;
2146 }
2147 }
2148
2149 _M_initialize();
2150 }
2151
2152 template<typename _RealType>
2153 template<typename _Func>
2154 piecewise_constant_distribution<_RealType>::param_type::
2155 param_type(initializer_list<_RealType> __bl, _Func __fw)
2156 : _M_int(), _M_den(), _M_cp()
2157 {
2158 _M_int.reserve(__bl.size());
2159 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2160 _M_int.push_back(*__biter);
2161
2162 _M_den.reserve(_M_int.size() - 1);
2163 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2164 _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
2165
2166 _M_initialize();
2167 }
2168
2169 template<typename _RealType>
2170 template<typename _Func>
2171 piecewise_constant_distribution<_RealType>::param_type::
2172 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2173 : _M_int(), _M_den(), _M_cp()
2174 {
2175 const size_t __n = __nw == 0 ? 1 : __nw;
2176 const _RealType __delta = (__xmax - __xmin) / __n;
2177
2178 _M_int.reserve(__n + 1);
2179 for (size_t __k = 0; __k <= __nw; ++__k)
2180 _M_int.push_back(__xmin + __k * __delta);
2181
2182 _M_den.reserve(__n);
2183 for (size_t __k = 0; __k < __nw; ++__k)
2184 _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));
2185
2186 _M_initialize();
2187 }
2188
2189 template<typename _RealType>
2190 template<typename _UniformRandomNumberGenerator>
2191 typename piecewise_constant_distribution<_RealType>::result_type
2192 piecewise_constant_distribution<_RealType>::
2193 operator()(_UniformRandomNumberGenerator& __urng,
2194 const param_type& __param)
2195 {
2196 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2197 __aurng(__urng);
2198
2199 const double __p = __aurng();
2200 auto __pos = std::lower_bound(__param._M_cp.begin(),
2201 __param._M_cp.end(), __p);
2202 const size_t __i = __pos - __param._M_cp.begin();
2203
2204 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2205
2206 return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i];
2207 }
2208
2209 template<typename _RealType, typename _CharT, typename _Traits>
2210 std::basic_ostream<_CharT, _Traits>&
2211 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2212 const piecewise_constant_distribution<_RealType>& __x)
2213 {
2214 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2215 typedef typename __ostream_type::ios_base __ios_base;
2216
2217 const typename __ios_base::fmtflags __flags = __os.flags();
2218 const _CharT __fill = __os.fill();
2219 const std::streamsize __precision = __os.precision();
2220 const _CharT __space = __os.widen(' ');
2221 __os.flags(__ios_base::scientific | __ios_base::left);
2222 __os.fill(__space);
2223 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2224
2225 std::vector<_RealType> __int = __x.intervals();
2226 __os << __int.size() - 1;
2227
2228 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2229 __os << __space << *__xit;
2230
2231 std::vector<double> __den = __x.densities();
2232 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2233 __os << __space << *__dit;
2234
2235 __os.flags(__flags);
2236 __os.fill(__fill);
2237 __os.precision(__precision);
2238 return __os;
2239 }
2240
2241 template<typename _RealType, typename _CharT, typename _Traits>
2242 std::basic_istream<_CharT, _Traits>&
2243 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2244 piecewise_constant_distribution<_RealType>& __x)
2245 {
2246 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2247 typedef typename __istream_type::ios_base __ios_base;
2248
2249 const typename __ios_base::fmtflags __flags = __is.flags();
2250 __is.flags(__ios_base::dec | __ios_base::skipws);
2251
2252 size_t __n;
2253 __is >> __n;
2254
2255 std::vector<_RealType> __int_vec;
2256 __int_vec.reserve(__n + 1);
2257 for (size_t __i = 0; __i <= __n; ++__i)
2258 {
2259 _RealType __int;
2260 __is >> __int;
2261 __int_vec.push_back(__int);
2262 }
2263
2264 std::vector<double> __den_vec;
2265 __den_vec.reserve(__n);
2266 for (size_t __i = 0; __i < __n; ++__i)
2267 {
2268 double __den;
2269 __is >> __den;
2270 __den_vec.push_back(__den);
2271 }
2272
2273 __x.param(typename piecewise_constant_distribution<_RealType>::
2274 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2275
2276 __is.flags(__flags);
2277 return __is;
2278 }
2279
2280
2281 template<typename _RealType>
2282 void
2283 piecewise_linear_distribution<_RealType>::param_type::
2284 _M_initialize()
2285 {
2286 if (_M_int.size() < 2)
2287 {
2288 _M_int.clear();
2289 _M_int.reserve(2);
2290 _M_int.push_back(_RealType(0));
2291 _M_int.push_back(_RealType(1));
2292
2293 _M_den.clear();
2294 _M_den.reserve(2);
2295 _M_den.push_back(1.0);
2296 _M_den.push_back(1.0);
2297
2298 return;
2299 }
2300
2301 double __sum = 0.0;
2302 _M_cp.reserve(_M_int.size() - 1);
2303 _M_m.reserve(_M_int.size() - 1);
2304 for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
2305 {
2306 const _RealType __delta = _M_int[__k + 1] - _M_int[__k];
2307 __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta;
2308 _M_cp.push_back(__sum);
2309 _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
2310 }
2311
2312 // Now normalize the densities...
2313 std::transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
2314 std::bind2nd(std::divides<double>(), __sum));
2315 // ... and partial sums...
2316 std::transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
2317 std::bind2nd(std::divides<double>(), __sum));
2318 // ... and slopes.
2319 std::transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
2320 std::bind2nd(std::divides<double>(), __sum));
2321 // Make sure the last cumulative probablility is one.
2322 _M_cp[_M_cp.size() - 1] = 1.0;
2323 }
2324
2325 template<typename _RealType>
2326 template<typename _InputIteratorB, typename _InputIteratorW>
2327 piecewise_linear_distribution<_RealType>::param_type::
2328 param_type(_InputIteratorB __bbegin,
2329 _InputIteratorB __bend,
2330 _InputIteratorW __wbegin)
2331 : _M_int(), _M_den(), _M_cp(), _M_m()
2332 {
2333 for (; __bbegin != __bend; ++__bbegin, ++__wbegin)
2334 {
2335 _M_int.push_back(*__bbegin);
2336 _M_den.push_back(*__wbegin);
2337 }
2338
2339 _M_initialize();
2340 }
2341
2342 template<typename _RealType>
2343 template<typename _Func>
2344 piecewise_linear_distribution<_RealType>::param_type::
2345 param_type(initializer_list<_RealType> __bl, _Func __fw)
2346 : _M_int(), _M_den(), _M_cp(), _M_m()
2347 {
2348 _M_int.reserve(__bl.size());
2349 _M_den.reserve(__bl.size());
2350 for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
2351 {
2352 _M_int.push_back(*__biter);
2353 _M_den.push_back(__fw(*__biter));
2354 }
2355
2356 _M_initialize();
2357 }
2358
2359 template<typename _RealType>
2360 template<typename _Func>
2361 piecewise_linear_distribution<_RealType>::param_type::
2362 param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw)
2363 : _M_int(), _M_den(), _M_cp(), _M_m()
2364 {
2365 const size_t __n = __nw == 0 ? 1 : __nw;
2366 const _RealType __delta = (__xmax - __xmin) / __n;
2367
2368 _M_int.reserve(__n + 1);
2369 _M_den.reserve(__n + 1);
2370 for (size_t __k = 0; __k <= __nw; ++__k)
2371 {
2372 _M_int.push_back(__xmin + __k * __delta);
2373 _M_den.push_back(__fw(_M_int[__k] + __delta));
2374 }
2375
2376 _M_initialize();
2377 }
2378
2379 template<typename _RealType>
2380 template<typename _UniformRandomNumberGenerator>
2381 typename piecewise_linear_distribution<_RealType>::result_type
2382 piecewise_linear_distribution<_RealType>::
2383 operator()(_UniformRandomNumberGenerator& __urng,
2384 const param_type& __param)
2385 {
2386 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2387 __aurng(__urng);
2388
2389 const double __p = __aurng();
2390 auto __pos = std::lower_bound(__param._M_cp.begin(),
2391 __param._M_cp.end(), __p);
2392 const size_t __i = __pos - __param._M_cp.begin();
2393
2394 const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0;
2395
2396 const double __a = 0.5 * __param._M_m[__i];
2397 const double __b = __param._M_den[__i];
2398 const double __cm = __p - __pref;
2399
2400 _RealType __x = __param._M_int[__i];
2401 if (__a == 0)
2402 __x += __cm / __b;
2403 else
2404 {
2405 const double __d = __b * __b + 4.0 * __a * __cm;
2406 __x += 0.5 * (std::sqrt(__d) - __b) / __a;
2407 }
2408
2409 return __x;
2410 }
2411
2412 template<typename _RealType, typename _CharT, typename _Traits>
2413 std::basic_ostream<_CharT, _Traits>&
2414 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2415 const piecewise_linear_distribution<_RealType>& __x)
2416 {
2417 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
2418 typedef typename __ostream_type::ios_base __ios_base;
2419
2420 const typename __ios_base::fmtflags __flags = __os.flags();
2421 const _CharT __fill = __os.fill();
2422 const std::streamsize __precision = __os.precision();
2423 const _CharT __space = __os.widen(' ');
2424 __os.flags(__ios_base::scientific | __ios_base::left);
2425 __os.fill(__space);
2426 __os.precision(std::numeric_limits<_RealType>::digits10 + 1);
2427
2428 std::vector<_RealType> __int = __x.intervals();
2429 __os << __int.size() - 1;
2430
2431 for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit)
2432 __os << __space << *__xit;
2433
2434 std::vector<double> __den = __x.densities();
2435 for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit)
2436 __os << __space << *__dit;
2437
2438 __os.flags(__flags);
2439 __os.fill(__fill);
2440 __os.precision(__precision);
2441 return __os;
2442 }
2443
2444 template<typename _RealType, typename _CharT, typename _Traits>
2445 std::basic_istream<_CharT, _Traits>&
2446 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2447 piecewise_linear_distribution<_RealType>& __x)
2448 {
2449 typedef std::basic_istream<_CharT, _Traits> __istream_type;
2450 typedef typename __istream_type::ios_base __ios_base;
2451
2452 const typename __ios_base::fmtflags __flags = __is.flags();
2453 __is.flags(__ios_base::dec | __ios_base::skipws);
2454
2455 size_t __n;
2456 __is >> __n;
2457
2458 std::vector<_RealType> __int_vec;
2459 __int_vec.reserve(__n + 1);
2460 for (size_t __i = 0; __i <= __n; ++__i)
2461 {
2462 _RealType __int;
2463 __is >> __int;
2464 __int_vec.push_back(__int);
2465 }
2466
2467 std::vector<double> __den_vec;
2468 __den_vec.reserve(__n + 1);
2469 for (size_t __i = 0; __i <= __n; ++__i)
2470 {
2471 double __den;
2472 __is >> __den;
2473 __den_vec.push_back(__den);
2474 }
2475
2476 __x.param(typename piecewise_linear_distribution<_RealType>::
2477 param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
2478
2479 __is.flags(__flags);
2480 return __is;
2481 }
2482
2483
2484 template<typename _IntType>
2485 seed_seq::seed_seq(std::initializer_list<_IntType> __il)
2486 {
2487 for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
2488 _M_v.push_back(__detail::__mod<result_type, 1, 0,
2489 __detail::_Shift<result_type, 32>::__value>(*__iter));
2490 }
2491
2492 template<typename _InputIterator>
2493 seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
2494 {
2495 for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
2496 _M_v.push_back(__detail::__mod<result_type, 1, 0,
2497 __detail::_Shift<result_type, 32>::__value>(*__iter));
2498 }
2499
2500 template<typename _RandomAccessIterator>
2501 void
2502 seed_seq::generate(_RandomAccessIterator __begin,
2503 _RandomAccessIterator __end)
2504 {
2505 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2506 _Type;
2507
2508 if (__begin == __end)
2509 return;
2510
2511 std::fill(__begin, __end, _Type(0x8b8b8b8bU));
2512
2513 const size_t __n = __end - __begin;
2514 const size_t __s = _M_v.size();
2515 const size_t __t = (__n >= 623) ? 11
2516 : (__n >= 68) ? 7
2517 : (__n >= 39) ? 5
2518 : (__n >= 7) ? 3
2519 : (__n - 1) / 2;
2520 const size_t __p = (__n - __t) / 2;
2521 const size_t __q = __p + __t;
2522 const size_t __m = std::max(__s + 1, __n);
2523
2524 for (size_t __k = 0; __k < __m; ++__k)
2525 {
2526 _Type __arg = (__begin[__k % __n]
2527 ^ __begin[(__k + __p) % __n]
2528 ^ __begin[(__k - 1) % __n]);
2529 _Type __r1 = __arg ^ (__arg << 27);
2530 __r1 = __detail::__mod<_Type, 1664525U, 0U,
2531 __detail::_Shift<_Type, 32>::__value>(__r1);
2532 _Type __r2 = __r1;
2533 if (__k == 0)
2534 __r2 += __s;
2535 else if (__k <= __s)
2536 __r2 += __k % __n + _M_v[__k - 1];
2537 else
2538 __r2 += __k % __n;
2539 __r2 = __detail::__mod<_Type, 1U, 0U,
2540 __detail::_Shift<_Type, 32>::__value>(__r2);
2541 __begin[(__k + __p) % __n] += __r1;
2542 __begin[(__k + __q) % __n] += __r2;
2543 __begin[__k % __n] = __r2;
2544 }
2545
2546 for (size_t __k = __m; __k < __m + __n; ++__k)
2547 {
2548 _Type __arg = (__begin[__k % __n]
2549 + __begin[(__k + __p) % __n]
2550 + __begin[(__k - 1) % __n]);
2551 _Type __r3 = __arg ^ (__arg << 27);
2552 __r3 = __detail::__mod<_Type, 1566083941U, 0U,
2553 __detail::_Shift<_Type, 32>::__value>(__r3);
2554 _Type __r4 = __r3 - __k % __n;
2555 __r4 = __detail::__mod<_Type, 1U, 0U,
2556 __detail::_Shift<_Type, 32>::__value>(__r4);
2557 __begin[(__k + __p) % __n] ^= __r4;
2558 __begin[(__k + __q) % __n] ^= __r3;
2559 __begin[__k % __n] = __r4;
2560 }
2561 }
2562
2563 template<typename _RealType, size_t __bits,
2564 typename _UniformRandomNumberGenerator>
2565 _RealType
2566 generate_canonical(_UniformRandomNumberGenerator& __urng)
2567 {
2568 const size_t __b
2569 = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
2570 __bits);
2571 const long double __r = static_cast<long double>(__urng.max())
2572 - static_cast<long double>(__urng.min()) + 1.0L;
2573 const size_t __log2r = std::log(__r) / std::log(2.0L);
2574 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
2575 _RealType __sum = _RealType(0);
2576 _RealType __tmp = _RealType(1);
2577 for (; __k != 0; --__k)
2578 {
2579 __sum += _RealType(__urng() - __urng.min()) * __tmp;
2580 __tmp *= __r;
2581 }
2582 return __sum / __tmp;
2583 }
2584 }