1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 /** @file tr1/random.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{tr1/random}
31 #ifndef _GLIBCXX_TR1_RANDOM_TCC
32 #define _GLIBCXX_TR1_RANDOM_TCC 1
34 namespace std _GLIBCXX_VISIBILITY(default)
39 * (Further) implementation-space details.
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 // General case for x = (ax + c) mod m -- use Schrage's algorithm to avoid
48 // Because a and c are compile-time integral constants the compiler kindly
49 // elides any unreachable paths.
51 // Preconditions: a > 0, m > 0.
53 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m, bool>
63 static const _Tp __q = __m / __a;
64 static const _Tp __r = __m % __a;
66 _Tp __t1 = __a * (__x % __q);
67 _Tp __t2 = __r * (__x / __q);
71 __x = __m - __t2 + __t1;
76 const _Tp __d = __m - __x;
86 // Special case for m == 0 -- use unsigned integer overflow as modulo
88 template<typename _Tp, _Tp __a, _Tp __c, _Tp __m>
89 struct _Mod<_Tp, __a, __c, __m, true>
93 { return __a * __x + __c; }
95 _GLIBCXX_END_NAMESPACE_VERSION
96 } // namespace __detail
98 _GLIBCXX_BEGIN_NAMESPACE_VERSION
100 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
102 linear_congruential<_UIntType, __a, __c, __m>::multiplier;
104 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
106 linear_congruential<_UIntType, __a, __c, __m>::increment;
108 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
110 linear_congruential<_UIntType, __a, __c, __m>::modulus;
113 * Seeds the LCR with integral value @p __x0, adjusted so that the
114 * ring identity is never a member of the convergence set.
116 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
118 linear_congruential<_UIntType, __a, __c, __m>::
119 seed(unsigned long __x0)
121 if ((__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0)
122 && (__detail::__mod<_UIntType, 1, 0, __m>(__x0) == 0))
123 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(1);
125 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(__x0);
129 * Seeds the LCR engine with a value generated by @p __g.
131 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
134 linear_congruential<_UIntType, __a, __c, __m>::
135 seed(_Gen& __g, false_type)
137 _UIntType __x0 = __g();
138 if ((__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0)
139 && (__detail::__mod<_UIntType, 1, 0, __m>(__x0) == 0))
140 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(1);
142 _M_x = __detail::__mod<_UIntType, 1, 0, __m>(__x0);
146 * Gets the next generated value in sequence.
148 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
149 typename linear_congruential<_UIntType, __a, __c, __m>::result_type
150 linear_congruential<_UIntType, __a, __c, __m>::
153 _M_x = __detail::__mod<_UIntType, __a, __c, __m>(_M_x);
157 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
158 typename _CharT, typename _Traits>
159 std::basic_ostream<_CharT, _Traits>&
160 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
161 const linear_congruential<_UIntType, __a, __c, __m>& __lcr)
163 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
164 typedef typename __ostream_type::ios_base __ios_base;
166 const typename __ios_base::fmtflags __flags = __os.flags();
167 const _CharT __fill = __os.fill();
168 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
169 __os.fill(__os.widen(' '));
178 template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m,
179 typename _CharT, typename _Traits>
180 std::basic_istream<_CharT, _Traits>&
181 operator>>(std::basic_istream<_CharT, _Traits>& __is,
182 linear_congruential<_UIntType, __a, __c, __m>& __lcr)
184 typedef std::basic_istream<_CharT, _Traits> __istream_type;
185 typedef typename __istream_type::ios_base __ios_base;
187 const typename __ios_base::fmtflags __flags = __is.flags();
188 __is.flags(__ios_base::dec);
197 template<class _UIntType, int __w, int __n, int __m, int __r,
198 _UIntType __a, int __u, int __s,
199 _UIntType __b, int __t, _UIntType __c, int __l>
201 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
202 __b, __t, __c, __l>::word_size;
204 template<class _UIntType, int __w, int __n, int __m, int __r,
205 _UIntType __a, int __u, int __s,
206 _UIntType __b, int __t, _UIntType __c, int __l>
208 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
209 __b, __t, __c, __l>::state_size;
211 template<class _UIntType, int __w, int __n, int __m, int __r,
212 _UIntType __a, int __u, int __s,
213 _UIntType __b, int __t, _UIntType __c, int __l>
215 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
216 __b, __t, __c, __l>::shift_size;
218 template<class _UIntType, int __w, int __n, int __m, int __r,
219 _UIntType __a, int __u, int __s,
220 _UIntType __b, int __t, _UIntType __c, int __l>
222 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
223 __b, __t, __c, __l>::mask_bits;
225 template<class _UIntType, int __w, int __n, int __m, int __r,
226 _UIntType __a, int __u, int __s,
227 _UIntType __b, int __t, _UIntType __c, int __l>
229 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
230 __b, __t, __c, __l>::parameter_a;
232 template<class _UIntType, int __w, int __n, int __m, int __r,
233 _UIntType __a, int __u, int __s,
234 _UIntType __b, int __t, _UIntType __c, int __l>
236 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
237 __b, __t, __c, __l>::output_u;
239 template<class _UIntType, int __w, int __n, int __m, int __r,
240 _UIntType __a, int __u, int __s,
241 _UIntType __b, int __t, _UIntType __c, int __l>
243 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
244 __b, __t, __c, __l>::output_s;
246 template<class _UIntType, int __w, int __n, int __m, int __r,
247 _UIntType __a, int __u, int __s,
248 _UIntType __b, int __t, _UIntType __c, int __l>
250 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
251 __b, __t, __c, __l>::output_b;
253 template<class _UIntType, int __w, int __n, int __m, int __r,
254 _UIntType __a, int __u, int __s,
255 _UIntType __b, int __t, _UIntType __c, int __l>
257 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
258 __b, __t, __c, __l>::output_t;
260 template<class _UIntType, int __w, int __n, int __m, int __r,
261 _UIntType __a, int __u, int __s,
262 _UIntType __b, int __t, _UIntType __c, int __l>
264 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
265 __b, __t, __c, __l>::output_c;
267 template<class _UIntType, int __w, int __n, int __m, int __r,
268 _UIntType __a, int __u, int __s,
269 _UIntType __b, int __t, _UIntType __c, int __l>
271 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
272 __b, __t, __c, __l>::output_l;
274 template<class _UIntType, int __w, int __n, int __m, int __r,
275 _UIntType __a, int __u, int __s,
276 _UIntType __b, int __t, _UIntType __c, int __l>
278 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
279 __b, __t, __c, __l>::
280 seed(unsigned long __value)
282 _M_x[0] = __detail::__mod<_UIntType, 1, 0,
283 __detail::_Shift<_UIntType, __w>::__value>(__value);
285 for (int __i = 1; __i < state_size; ++__i)
287 _UIntType __x = _M_x[__i - 1];
288 __x ^= __x >> (__w - 2);
291 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
292 __detail::_Shift<_UIntType, __w>::__value>(__x);
297 template<class _UIntType, int __w, int __n, int __m, int __r,
298 _UIntType __a, int __u, int __s,
299 _UIntType __b, int __t, _UIntType __c, int __l>
302 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
303 __b, __t, __c, __l>::
304 seed(_Gen& __gen, false_type)
306 for (int __i = 0; __i < state_size; ++__i)
307 _M_x[__i] = __detail::__mod<_UIntType, 1, 0,
308 __detail::_Shift<_UIntType, __w>::__value>(__gen());
312 template<class _UIntType, int __w, int __n, int __m, int __r,
313 _UIntType __a, int __u, int __s,
314 _UIntType __b, int __t, _UIntType __c, int __l>
316 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
317 __b, __t, __c, __l>::result_type
318 mersenne_twister<_UIntType, __w, __n, __m, __r, __a, __u, __s,
319 __b, __t, __c, __l>::
322 // Reload the vector - cost is O(n) amortized over n calls.
323 if (_M_p >= state_size)
325 const _UIntType __upper_mask = (~_UIntType()) << __r;
326 const _UIntType __lower_mask = ~__upper_mask;
328 for (int __k = 0; __k < (__n - __m); ++__k)
330 _UIntType __y = ((_M_x[__k] & __upper_mask)
331 | (_M_x[__k + 1] & __lower_mask));
332 _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1)
333 ^ ((__y & 0x01) ? __a : 0));
336 for (int __k = (__n - __m); __k < (__n - 1); ++__k)
338 _UIntType __y = ((_M_x[__k] & __upper_mask)
339 | (_M_x[__k + 1] & __lower_mask));
340 _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1)
341 ^ ((__y & 0x01) ? __a : 0));
344 _UIntType __y = ((_M_x[__n - 1] & __upper_mask)
345 | (_M_x[0] & __lower_mask));
346 _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1)
347 ^ ((__y & 0x01) ? __a : 0));
351 // Calculate o(x(i)).
352 result_type __z = _M_x[_M_p++];
354 __z ^= (__z << __s) & __b;
355 __z ^= (__z << __t) & __c;
361 template<class _UIntType, int __w, int __n, int __m, int __r,
362 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
363 _UIntType __c, int __l,
364 typename _CharT, typename _Traits>
365 std::basic_ostream<_CharT, _Traits>&
366 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
367 const mersenne_twister<_UIntType, __w, __n, __m,
368 __r, __a, __u, __s, __b, __t, __c, __l>& __x)
370 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
371 typedef typename __ostream_type::ios_base __ios_base;
373 const typename __ios_base::fmtflags __flags = __os.flags();
374 const _CharT __fill = __os.fill();
375 const _CharT __space = __os.widen(' ');
376 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
379 for (int __i = 0; __i < __n - 1; ++__i)
380 __os << __x._M_x[__i] << __space;
381 __os << __x._M_x[__n - 1];
388 template<class _UIntType, int __w, int __n, int __m, int __r,
389 _UIntType __a, int __u, int __s, _UIntType __b, int __t,
390 _UIntType __c, int __l,
391 typename _CharT, typename _Traits>
392 std::basic_istream<_CharT, _Traits>&
393 operator>>(std::basic_istream<_CharT, _Traits>& __is,
394 mersenne_twister<_UIntType, __w, __n, __m,
395 __r, __a, __u, __s, __b, __t, __c, __l>& __x)
397 typedef std::basic_istream<_CharT, _Traits> __istream_type;
398 typedef typename __istream_type::ios_base __ios_base;
400 const typename __ios_base::fmtflags __flags = __is.flags();
401 __is.flags(__ios_base::dec | __ios_base::skipws);
403 for (int __i = 0; __i < __n; ++__i)
404 __is >> __x._M_x[__i];
411 template<typename _IntType, _IntType __m, int __s, int __r>
413 subtract_with_carry<_IntType, __m, __s, __r>::modulus;
415 template<typename _IntType, _IntType __m, int __s, int __r>
417 subtract_with_carry<_IntType, __m, __s, __r>::long_lag;
419 template<typename _IntType, _IntType __m, int __s, int __r>
421 subtract_with_carry<_IntType, __m, __s, __r>::short_lag;
423 template<typename _IntType, _IntType __m, int __s, int __r>
425 subtract_with_carry<_IntType, __m, __s, __r>::
426 seed(unsigned long __value)
431 std::tr1::linear_congruential<unsigned long, 40014, 0, 2147483563>
434 for (int __i = 0; __i < long_lag; ++__i)
435 _M_x[__i] = __detail::__mod<_UIntType, 1, 0, modulus>(__lcg());
437 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
441 template<typename _IntType, _IntType __m, int __s, int __r>
444 subtract_with_carry<_IntType, __m, __s, __r>::
445 seed(_Gen& __gen, false_type)
447 const int __n = (std::numeric_limits<_UIntType>::digits + 31) / 32;
449 for (int __i = 0; __i < long_lag; ++__i)
452 _UIntType __factor = 1;
453 for (int __j = 0; __j < __n; ++__j)
455 __tmp += __detail::__mod<__detail::_UInt32Type, 1, 0, 0>
456 (__gen()) * __factor;
457 __factor *= __detail::_Shift<_UIntType, 32>::__value;
459 _M_x[__i] = __detail::__mod<_UIntType, 1, 0, modulus>(__tmp);
461 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
465 template<typename _IntType, _IntType __m, int __s, int __r>
466 typename subtract_with_carry<_IntType, __m, __s, __r>::result_type
467 subtract_with_carry<_IntType, __m, __s, __r>::
470 // Derive short lag index from current index.
471 int __ps = _M_p - short_lag;
475 // Calculate new x(i) without overflow or division.
476 // NB: Thanks to the requirements for _IntType, _M_x[_M_p] + _M_carry
479 if (_M_x[__ps] >= _M_x[_M_p] + _M_carry)
481 __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry;
486 __xi = modulus - _M_x[_M_p] - _M_carry + _M_x[__ps];
491 // Adjust current index to loop around in ring buffer.
492 if (++_M_p >= long_lag)
498 template<typename _IntType, _IntType __m, int __s, int __r,
499 typename _CharT, typename _Traits>
500 std::basic_ostream<_CharT, _Traits>&
501 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
502 const subtract_with_carry<_IntType, __m, __s, __r>& __x)
504 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
505 typedef typename __ostream_type::ios_base __ios_base;
507 const typename __ios_base::fmtflags __flags = __os.flags();
508 const _CharT __fill = __os.fill();
509 const _CharT __space = __os.widen(' ');
510 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
513 for (int __i = 0; __i < __r; ++__i)
514 __os << __x._M_x[__i] << __space;
515 __os << __x._M_carry;
522 template<typename _IntType, _IntType __m, int __s, int __r,
523 typename _CharT, typename _Traits>
524 std::basic_istream<_CharT, _Traits>&
525 operator>>(std::basic_istream<_CharT, _Traits>& __is,
526 subtract_with_carry<_IntType, __m, __s, __r>& __x)
528 typedef std::basic_ostream<_CharT, _Traits> __istream_type;
529 typedef typename __istream_type::ios_base __ios_base;
531 const typename __ios_base::fmtflags __flags = __is.flags();
532 __is.flags(__ios_base::dec | __ios_base::skipws);
534 for (int __i = 0; __i < __r; ++__i)
535 __is >> __x._M_x[__i];
536 __is >> __x._M_carry;
543 template<typename _RealType, int __w, int __s, int __r>
545 subtract_with_carry_01<_RealType, __w, __s, __r>::word_size;
547 template<typename _RealType, int __w, int __s, int __r>
549 subtract_with_carry_01<_RealType, __w, __s, __r>::long_lag;
551 template<typename _RealType, int __w, int __s, int __r>
553 subtract_with_carry_01<_RealType, __w, __s, __r>::short_lag;
555 template<typename _RealType, int __w, int __s, int __r>
557 subtract_with_carry_01<_RealType, __w, __s, __r>::
558 _M_initialize_npows()
560 for (int __j = 0; __j < __n; ++__j)
561 #if _GLIBCXX_USE_C99_MATH_TR1
562 _M_npows[__j] = std::tr1::ldexp(_RealType(1), -__w + __j * 32);
564 _M_npows[__j] = std::pow(_RealType(2), -__w + __j * 32);
568 template<typename _RealType, int __w, int __s, int __r>
570 subtract_with_carry_01<_RealType, __w, __s, __r>::
571 seed(unsigned long __value)
576 // _GLIBCXX_RESOLVE_LIB_DEFECTS
577 // 512. Seeding subtract_with_carry_01 from a single unsigned long.
578 std::tr1::linear_congruential<unsigned long, 40014, 0, 2147483563>
584 template<typename _RealType, int __w, int __s, int __r>
587 subtract_with_carry_01<_RealType, __w, __s, __r>::
588 seed(_Gen& __gen, false_type)
590 for (int __i = 0; __i < long_lag; ++__i)
592 for (int __j = 0; __j < __n - 1; ++__j)
593 _M_x[__i][__j] = __detail::__mod<_UInt32Type, 1, 0, 0>(__gen());
594 _M_x[__i][__n - 1] = __detail::__mod<_UInt32Type, 1, 0,
595 __detail::_Shift<_UInt32Type, __w % 32>::__value>(__gen());
599 for (int __j = 0; __j < __n; ++__j)
600 if (_M_x[long_lag - 1][__j] != 0)
609 template<typename _RealType, int __w, int __s, int __r>
610 typename subtract_with_carry_01<_RealType, __w, __s, __r>::result_type
611 subtract_with_carry_01<_RealType, __w, __s, __r>::
614 // Derive short lag index from current index.
615 int __ps = _M_p - short_lag;
619 _UInt32Type __new_carry;
620 for (int __j = 0; __j < __n - 1; ++__j)
622 if (_M_x[__ps][__j] > _M_x[_M_p][__j]
623 || (_M_x[__ps][__j] == _M_x[_M_p][__j] && _M_carry == 0))
628 _M_x[_M_p][__j] = _M_x[__ps][__j] - _M_x[_M_p][__j] - _M_carry;
629 _M_carry = __new_carry;
632 if (_M_x[__ps][__n - 1] > _M_x[_M_p][__n - 1]
633 || (_M_x[__ps][__n - 1] == _M_x[_M_p][__n - 1] && _M_carry == 0))
638 _M_x[_M_p][__n - 1] = __detail::__mod<_UInt32Type, 1, 0,
639 __detail::_Shift<_UInt32Type, __w % 32>::__value>
640 (_M_x[__ps][__n - 1] - _M_x[_M_p][__n - 1] - _M_carry);
641 _M_carry = __new_carry;
643 result_type __ret = 0.0;
644 for (int __j = 0; __j < __n; ++__j)
645 __ret += _M_x[_M_p][__j] * _M_npows[__j];
647 // Adjust current index to loop around in ring buffer.
648 if (++_M_p >= long_lag)
654 template<typename _RealType, int __w, int __s, int __r,
655 typename _CharT, typename _Traits>
656 std::basic_ostream<_CharT, _Traits>&
657 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
658 const subtract_with_carry_01<_RealType, __w, __s, __r>& __x)
660 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
661 typedef typename __ostream_type::ios_base __ios_base;
663 const typename __ios_base::fmtflags __flags = __os.flags();
664 const _CharT __fill = __os.fill();
665 const _CharT __space = __os.widen(' ');
666 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
669 for (int __i = 0; __i < __r; ++__i)
670 for (int __j = 0; __j < __x.__n; ++__j)
671 __os << __x._M_x[__i][__j] << __space;
672 __os << __x._M_carry;
679 template<typename _RealType, int __w, int __s, int __r,
680 typename _CharT, typename _Traits>
681 std::basic_istream<_CharT, _Traits>&
682 operator>>(std::basic_istream<_CharT, _Traits>& __is,
683 subtract_with_carry_01<_RealType, __w, __s, __r>& __x)
685 typedef std::basic_istream<_CharT, _Traits> __istream_type;
686 typedef typename __istream_type::ios_base __ios_base;
688 const typename __ios_base::fmtflags __flags = __is.flags();
689 __is.flags(__ios_base::dec | __ios_base::skipws);
691 for (int __i = 0; __i < __r; ++__i)
692 for (int __j = 0; __j < __x.__n; ++__j)
693 __is >> __x._M_x[__i][__j];
694 __is >> __x._M_carry;
700 template<class _UniformRandomNumberGenerator, int __p, int __r>
702 discard_block<_UniformRandomNumberGenerator, __p, __r>::block_size;
704 template<class _UniformRandomNumberGenerator, int __p, int __r>
706 discard_block<_UniformRandomNumberGenerator, __p, __r>::used_block;
708 template<class _UniformRandomNumberGenerator, int __p, int __r>
709 typename discard_block<_UniformRandomNumberGenerator,
710 __p, __r>::result_type
711 discard_block<_UniformRandomNumberGenerator, __p, __r>::
714 if (_M_n >= used_block)
716 while (_M_n < block_size)
727 template<class _UniformRandomNumberGenerator, int __p, int __r,
728 typename _CharT, typename _Traits>
729 std::basic_ostream<_CharT, _Traits>&
730 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
731 const discard_block<_UniformRandomNumberGenerator,
734 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
735 typedef typename __ostream_type::ios_base __ios_base;
737 const typename __ios_base::fmtflags __flags = __os.flags();
738 const _CharT __fill = __os.fill();
739 const _CharT __space = __os.widen(' ');
740 __os.flags(__ios_base::dec | __ios_base::fixed
744 __os << __x._M_b << __space << __x._M_n;
751 template<class _UniformRandomNumberGenerator, int __p, int __r,
752 typename _CharT, typename _Traits>
753 std::basic_istream<_CharT, _Traits>&
754 operator>>(std::basic_istream<_CharT, _Traits>& __is,
755 discard_block<_UniformRandomNumberGenerator, __p, __r>& __x)
757 typedef std::basic_istream<_CharT, _Traits> __istream_type;
758 typedef typename __istream_type::ios_base __ios_base;
760 const typename __ios_base::fmtflags __flags = __is.flags();
761 __is.flags(__ios_base::dec | __ios_base::skipws);
763 __is >> __x._M_b >> __x._M_n;
770 template<class _UniformRandomNumberGenerator1, int __s1,
771 class _UniformRandomNumberGenerator2, int __s2>
773 xor_combine<_UniformRandomNumberGenerator1, __s1,
774 _UniformRandomNumberGenerator2, __s2>::shift1;
776 template<class _UniformRandomNumberGenerator1, int __s1,
777 class _UniformRandomNumberGenerator2, int __s2>
779 xor_combine<_UniformRandomNumberGenerator1, __s1,
780 _UniformRandomNumberGenerator2, __s2>::shift2;
782 template<class _UniformRandomNumberGenerator1, int __s1,
783 class _UniformRandomNumberGenerator2, int __s2>
785 xor_combine<_UniformRandomNumberGenerator1, __s1,
786 _UniformRandomNumberGenerator2, __s2>::
789 const int __w = std::numeric_limits<result_type>::digits;
791 const result_type __m1 =
792 std::min(result_type(_M_b1.max() - _M_b1.min()),
793 __detail::_Shift<result_type, __w - __s1>::__value - 1);
795 const result_type __m2 =
796 std::min(result_type(_M_b2.max() - _M_b2.min()),
797 __detail::_Shift<result_type, __w - __s2>::__value - 1);
799 // NB: In TR1 s1 is not required to be >= s2.
801 _M_max = _M_initialize_max_aux(__m2, __m1, __s2 - __s1) << __s1;
803 _M_max = _M_initialize_max_aux(__m1, __m2, __s1 - __s2) << __s2;
806 template<class _UniformRandomNumberGenerator1, int __s1,
807 class _UniformRandomNumberGenerator2, int __s2>
808 typename xor_combine<_UniformRandomNumberGenerator1, __s1,
809 _UniformRandomNumberGenerator2, __s2>::result_type
810 xor_combine<_UniformRandomNumberGenerator1, __s1,
811 _UniformRandomNumberGenerator2, __s2>::
812 _M_initialize_max_aux(result_type __a, result_type __b, int __d)
814 const result_type __two2d = result_type(1) << __d;
815 const result_type __c = __a * __two2d;
817 if (__a == 0 || __b < __two2d)
820 const result_type __t = std::max(__c, __b);
821 const result_type __u = std::min(__c, __b);
823 result_type __ub = __u;
825 for (__p = 0; __ub != 1; __ub >>= 1)
828 const result_type __two2p = result_type(1) << __p;
829 const result_type __k = __t / __two2p;
832 return (__k + 1) * __two2p - 1;
835 return (__k + 1) * __two2p + _M_initialize_max_aux((__t % __two2p)
839 return (__k + 1) * __two2p + _M_initialize_max_aux((__u % __two2p)
844 template<class _UniformRandomNumberGenerator1, int __s1,
845 class _UniformRandomNumberGenerator2, int __s2,
846 typename _CharT, typename _Traits>
847 std::basic_ostream<_CharT, _Traits>&
848 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
849 const xor_combine<_UniformRandomNumberGenerator1, __s1,
850 _UniformRandomNumberGenerator2, __s2>& __x)
852 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
853 typedef typename __ostream_type::ios_base __ios_base;
855 const typename __ios_base::fmtflags __flags = __os.flags();
856 const _CharT __fill = __os.fill();
857 const _CharT __space = __os.widen(' ');
858 __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
861 __os << __x.base1() << __space << __x.base2();
868 template<class _UniformRandomNumberGenerator1, int __s1,
869 class _UniformRandomNumberGenerator2, int __s2,
870 typename _CharT, typename _Traits>
871 std::basic_istream<_CharT, _Traits>&
872 operator>>(std::basic_istream<_CharT, _Traits>& __is,
873 xor_combine<_UniformRandomNumberGenerator1, __s1,
874 _UniformRandomNumberGenerator2, __s2>& __x)
876 typedef std::basic_istream<_CharT, _Traits> __istream_type;
877 typedef typename __istream_type::ios_base __ios_base;
879 const typename __ios_base::fmtflags __flags = __is.flags();
880 __is.flags(__ios_base::skipws);
882 __is >> __x._M_b1 >> __x._M_b2;
889 template<typename _IntType>
890 template<typename _UniformRandomNumberGenerator>
891 typename uniform_int<_IntType>::result_type
892 uniform_int<_IntType>::
893 _M_call(_UniformRandomNumberGenerator& __urng,
894 result_type __min, result_type __max, true_type)
896 // XXX Must be fixed to work well for *arbitrary* __urng.max(),
897 // __urng.min(), __max, __min. Currently works fine only in the
898 // most common case __urng.max() - __urng.min() >= __max - __min,
899 // with __urng.max() > __urng.min() >= 0.
900 typedef typename __gnu_cxx::__add_unsigned<typename
901 _UniformRandomNumberGenerator::result_type>::__type __urntype;
902 typedef typename __gnu_cxx::__add_unsigned<result_type>::__type
904 typedef typename __gnu_cxx::__conditional_type<(sizeof(__urntype)
906 __urntype, __utype>::__type __uctype;
910 const __urntype __urnmin = __urng.min();
911 const __urntype __urnmax = __urng.max();
912 const __urntype __urnrange = __urnmax - __urnmin;
913 const __uctype __urange = __max - __min;
914 const __uctype __udenom = (__urnrange <= __urange
915 ? 1 : __urnrange / (__urange + 1));
917 __ret = (__urntype(__urng()) - __urnmin) / __udenom;
918 while (__ret > __max - __min);
920 return __ret + __min;
923 template<typename _IntType, typename _CharT, typename _Traits>
924 std::basic_ostream<_CharT, _Traits>&
925 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
926 const uniform_int<_IntType>& __x)
928 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
929 typedef typename __ostream_type::ios_base __ios_base;
931 const typename __ios_base::fmtflags __flags = __os.flags();
932 const _CharT __fill = __os.fill();
933 const _CharT __space = __os.widen(' ');
934 __os.flags(__ios_base::scientific | __ios_base::left);
937 __os << __x.min() << __space << __x.max();
944 template<typename _IntType, typename _CharT, typename _Traits>
945 std::basic_istream<_CharT, _Traits>&
946 operator>>(std::basic_istream<_CharT, _Traits>& __is,
947 uniform_int<_IntType>& __x)
949 typedef std::basic_istream<_CharT, _Traits> __istream_type;
950 typedef typename __istream_type::ios_base __ios_base;
952 const typename __ios_base::fmtflags __flags = __is.flags();
953 __is.flags(__ios_base::dec | __ios_base::skipws);
955 __is >> __x._M_min >> __x._M_max;
962 template<typename _CharT, typename _Traits>
963 std::basic_ostream<_CharT, _Traits>&
964 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
965 const bernoulli_distribution& __x)
967 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
968 typedef typename __ostream_type::ios_base __ios_base;
970 const typename __ios_base::fmtflags __flags = __os.flags();
971 const _CharT __fill = __os.fill();
972 const std::streamsize __precision = __os.precision();
973 __os.flags(__ios_base::scientific | __ios_base::left);
974 __os.fill(__os.widen(' '));
975 __os.precision(__gnu_cxx::__numeric_traits<double>::__max_digits10);
981 __os.precision(__precision);
986 template<typename _IntType, typename _RealType>
987 template<class _UniformRandomNumberGenerator>
988 typename geometric_distribution<_IntType, _RealType>::result_type
989 geometric_distribution<_IntType, _RealType>::
990 operator()(_UniformRandomNumberGenerator& __urng)
992 // About the epsilon thing see this thread:
993 // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
994 const _RealType __naf =
995 (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
996 // The largest _RealType convertible to _IntType.
997 const _RealType __thr =
998 std::numeric_limits<_IntType>::max() + __naf;
1002 __cand = std::ceil(std::log(__urng()) / _M_log_p);
1003 while (__cand >= __thr);
1005 return result_type(__cand + __naf);
1008 template<typename _IntType, typename _RealType,
1009 typename _CharT, typename _Traits>
1010 std::basic_ostream<_CharT, _Traits>&
1011 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1012 const geometric_distribution<_IntType, _RealType>& __x)
1014 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1015 typedef typename __ostream_type::ios_base __ios_base;
1017 const typename __ios_base::fmtflags __flags = __os.flags();
1018 const _CharT __fill = __os.fill();
1019 const std::streamsize __precision = __os.precision();
1020 __os.flags(__ios_base::scientific | __ios_base::left);
1021 __os.fill(__os.widen(' '));
1022 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1026 __os.flags(__flags);
1028 __os.precision(__precision);
1033 template<typename _IntType, typename _RealType>
1035 poisson_distribution<_IntType, _RealType>::
1038 #if _GLIBCXX_USE_C99_MATH_TR1
1041 const _RealType __m = std::floor(_M_mean);
1042 _M_lm_thr = std::log(_M_mean);
1043 _M_lfm = std::tr1::lgamma(__m + 1);
1044 _M_sm = std::sqrt(__m);
1046 const _RealType __pi_4 = 0.7853981633974483096156608458198757L;
1047 const _RealType __dx = std::sqrt(2 * __m * std::log(32 * __m
1049 _M_d = std::tr1::round(std::max(_RealType(6),
1050 std::min(__m, __dx)));
1051 const _RealType __cx = 2 * __m + _M_d;
1052 _M_scx = std::sqrt(__cx / 2);
1055 _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx);
1056 _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2)) / _M_d;
1060 _M_lm_thr = std::exp(-_M_mean);
1064 * A rejection algorithm when mean >= 12 and a simple method based
1065 * upon the multiplication of uniform random variates otherwise.
1066 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1070 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1071 * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!).
1073 template<typename _IntType, typename _RealType>
1074 template<class _UniformRandomNumberGenerator>
1075 typename poisson_distribution<_IntType, _RealType>::result_type
1076 poisson_distribution<_IntType, _RealType>::
1077 operator()(_UniformRandomNumberGenerator& __urng)
1079 #if _GLIBCXX_USE_C99_MATH_TR1
1084 // See comments above...
1085 const _RealType __naf =
1086 (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
1087 const _RealType __thr =
1088 std::numeric_limits<_IntType>::max() + __naf;
1090 const _RealType __m = std::floor(_M_mean);
1092 const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
1093 const _RealType __c1 = _M_sm * __spi_2;
1094 const _RealType __c2 = _M_c2b + __c1;
1095 const _RealType __c3 = __c2 + 1;
1096 const _RealType __c4 = __c3 + 1;
1098 const _RealType __e178 = 1.0129030479320018583185514777512983L;
1099 const _RealType __c5 = __c4 + __e178;
1100 const _RealType __c = _M_cb + __c5;
1101 const _RealType __2cx = 2 * (2 * __m + _M_d);
1103 bool __reject = true;
1106 const _RealType __u = __c * __urng();
1107 const _RealType __e = -std::log(__urng());
1109 _RealType __w = 0.0;
1113 const _RealType __n = _M_nd(__urng);
1114 const _RealType __y = -std::abs(__n) * _M_sm - 1;
1115 __x = std::floor(__y);
1116 __w = -__n * __n / 2;
1120 else if (__u <= __c2)
1122 const _RealType __n = _M_nd(__urng);
1123 const _RealType __y = 1 + std::abs(__n) * _M_scx;
1124 __x = std::ceil(__y);
1125 __w = __y * (2 - __y) * _M_1cx;
1129 else if (__u <= __c3)
1130 // NB: This case not in the book, nor in the Errata,
1131 // but should be ok...
1133 else if (__u <= __c4)
1135 else if (__u <= __c5)
1139 const _RealType __v = -std::log(__urng());
1140 const _RealType __y = _M_d + __v * __2cx / _M_d;
1141 __x = std::ceil(__y);
1142 __w = -_M_d * _M_1cx * (1 + __y / 2);
1145 __reject = (__w - __e - __x * _M_lm_thr
1146 > _M_lfm - std::tr1::lgamma(__x + __m + 1));
1148 __reject |= __x + __m >= __thr;
1152 return result_type(__x + __m + __naf);
1158 _RealType __prod = 1.0;
1165 while (__prod > _M_lm_thr);
1171 template<typename _IntType, typename _RealType,
1172 typename _CharT, typename _Traits>
1173 std::basic_ostream<_CharT, _Traits>&
1174 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1175 const poisson_distribution<_IntType, _RealType>& __x)
1177 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1178 typedef typename __ostream_type::ios_base __ios_base;
1180 const typename __ios_base::fmtflags __flags = __os.flags();
1181 const _CharT __fill = __os.fill();
1182 const std::streamsize __precision = __os.precision();
1183 const _CharT __space = __os.widen(' ');
1184 __os.flags(__ios_base::scientific | __ios_base::left);
1186 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1188 __os << __x.mean() << __space << __x._M_nd;
1190 __os.flags(__flags);
1192 __os.precision(__precision);
1196 template<typename _IntType, typename _RealType,
1197 typename _CharT, typename _Traits>
1198 std::basic_istream<_CharT, _Traits>&
1199 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1200 poisson_distribution<_IntType, _RealType>& __x)
1202 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1203 typedef typename __istream_type::ios_base __ios_base;
1205 const typename __ios_base::fmtflags __flags = __is.flags();
1206 __is.flags(__ios_base::skipws);
1208 __is >> __x._M_mean >> __x._M_nd;
1209 __x._M_initialize();
1211 __is.flags(__flags);
1216 template<typename _IntType, typename _RealType>
1218 binomial_distribution<_IntType, _RealType>::
1221 const _RealType __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1225 #if _GLIBCXX_USE_C99_MATH_TR1
1226 if (_M_t * __p12 >= 8)
1229 const _RealType __np = std::floor(_M_t * __p12);
1230 const _RealType __pa = __np / _M_t;
1231 const _RealType __1p = 1 - __pa;
1233 const _RealType __pi_4 = 0.7853981633974483096156608458198757L;
1234 const _RealType __d1x =
1235 std::sqrt(__np * __1p * std::log(32 * __np
1236 / (81 * __pi_4 * __1p)));
1237 _M_d1 = std::tr1::round(std::max(_RealType(1), __d1x));
1238 const _RealType __d2x =
1239 std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
1240 / (__pi_4 * __pa)));
1241 _M_d2 = std::tr1::round(std::max(_RealType(1), __d2x));
1244 const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
1245 _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np));
1246 _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p));
1247 _M_c = 2 * _M_d1 / __np;
1248 _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2;
1249 const _RealType __a12 = _M_a1 + _M_s2 * __spi_2;
1250 const _RealType __s1s = _M_s1 * _M_s1;
1251 _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p))
1253 * std::exp(-_M_d1 * _M_d1 / (2 * __s1s)));
1254 const _RealType __s2s = _M_s2 * _M_s2;
1255 _M_s = (_M_a123 + 2 * __s2s / _M_d2
1256 * std::exp(-_M_d2 * _M_d2 / (2 * __s2s)));
1257 _M_lf = (std::tr1::lgamma(__np + 1)
1258 + std::tr1::lgamma(_M_t - __np + 1));
1259 _M_lp1p = std::log(__pa / __1p);
1261 _M_q = -std::log(1 - (__p12 - __pa) / __1p);
1265 _M_q = -std::log(1 - __p12);
1268 template<typename _IntType, typename _RealType>
1269 template<class _UniformRandomNumberGenerator>
1270 typename binomial_distribution<_IntType, _RealType>::result_type
1271 binomial_distribution<_IntType, _RealType>::
1272 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
1275 _RealType __sum = 0;
1279 const _RealType __e = -std::log(__urng());
1280 __sum += __e / (__t - __x);
1283 while (__sum <= _M_q);
1289 * A rejection algorithm when t * p >= 8 and a simple waiting time
1290 * method - the second in the referenced book - otherwise.
1291 * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1
1295 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1296 * New York, 1986, Ch. X, Sect. 4 (+ Errata!).
1298 template<typename _IntType, typename _RealType>
1299 template<class _UniformRandomNumberGenerator>
1300 typename binomial_distribution<_IntType, _RealType>::result_type
1301 binomial_distribution<_IntType, _RealType>::
1302 operator()(_UniformRandomNumberGenerator& __urng)
1305 const _RealType __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p;
1307 #if _GLIBCXX_USE_C99_MATH_TR1
1312 // See comments above...
1313 const _RealType __naf =
1314 (1 - std::numeric_limits<_RealType>::epsilon()) / 2;
1315 const _RealType __thr =
1316 std::numeric_limits<_IntType>::max() + __naf;
1318 const _RealType __np = std::floor(_M_t * __p12);
1319 const _RealType __pa = __np / _M_t;
1322 const _RealType __spi_2 = 1.2533141373155002512078826424055226L;
1323 const _RealType __a1 = _M_a1;
1324 const _RealType __a12 = __a1 + _M_s2 * __spi_2;
1325 const _RealType __a123 = _M_a123;
1326 const _RealType __s1s = _M_s1 * _M_s1;
1327 const _RealType __s2s = _M_s2 * _M_s2;
1332 const _RealType __u = _M_s * __urng();
1338 const _RealType __n = _M_nd(__urng);
1339 const _RealType __y = _M_s1 * std::abs(__n);
1340 __reject = __y >= _M_d1;
1343 const _RealType __e = -std::log(__urng());
1344 __x = std::floor(__y);
1345 __v = -__e - __n * __n / 2 + _M_c;
1348 else if (__u <= __a12)
1350 const _RealType __n = _M_nd(__urng);
1351 const _RealType __y = _M_s2 * std::abs(__n);
1352 __reject = __y >= _M_d2;
1355 const _RealType __e = -std::log(__urng());
1356 __x = std::floor(-__y);
1357 __v = -__e - __n * __n / 2;
1360 else if (__u <= __a123)
1362 const _RealType __e1 = -std::log(__urng());
1363 const _RealType __e2 = -std::log(__urng());
1365 const _RealType __y = _M_d1 + 2 * __s1s * __e1 / _M_d1;
1366 __x = std::floor(__y);
1367 __v = (-__e2 + _M_d1 * (1 / (_M_t - __np)
1368 -__y / (2 * __s1s)));
1373 const _RealType __e1 = -std::log(__urng());
1374 const _RealType __e2 = -std::log(__urng());
1376 const _RealType __y = _M_d2 + 2 * __s2s * __e1 / _M_d2;
1377 __x = std::floor(-__y);
1378 __v = -__e2 - _M_d2 * __y / (2 * __s2s);
1382 __reject = __reject || __x < -__np || __x > _M_t - __np;
1385 const _RealType __lfx =
1386 std::tr1::lgamma(__np + __x + 1)
1387 + std::tr1::lgamma(_M_t - (__np + __x) + 1);
1388 __reject = __v > _M_lf - __lfx + __x * _M_lp1p;
1391 __reject |= __x + __np >= __thr;
1395 __x += __np + __naf;
1397 const _IntType __z = _M_waiting(__urng, _M_t - _IntType(__x));
1398 __ret = _IntType(__x) + __z;
1402 __ret = _M_waiting(__urng, _M_t);
1405 __ret = _M_t - __ret;
1409 template<typename _IntType, typename _RealType,
1410 typename _CharT, typename _Traits>
1411 std::basic_ostream<_CharT, _Traits>&
1412 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1413 const binomial_distribution<_IntType, _RealType>& __x)
1415 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1416 typedef typename __ostream_type::ios_base __ios_base;
1418 const typename __ios_base::fmtflags __flags = __os.flags();
1419 const _CharT __fill = __os.fill();
1420 const std::streamsize __precision = __os.precision();
1421 const _CharT __space = __os.widen(' ');
1422 __os.flags(__ios_base::scientific | __ios_base::left);
1424 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1426 __os << __x.t() << __space << __x.p()
1427 << __space << __x._M_nd;
1429 __os.flags(__flags);
1431 __os.precision(__precision);
1435 template<typename _IntType, typename _RealType,
1436 typename _CharT, typename _Traits>
1437 std::basic_istream<_CharT, _Traits>&
1438 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1439 binomial_distribution<_IntType, _RealType>& __x)
1441 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1442 typedef typename __istream_type::ios_base __ios_base;
1444 const typename __ios_base::fmtflags __flags = __is.flags();
1445 __is.flags(__ios_base::dec | __ios_base::skipws);
1447 __is >> __x._M_t >> __x._M_p >> __x._M_nd;
1448 __x._M_initialize();
1450 __is.flags(__flags);
1455 template<typename _RealType, typename _CharT, typename _Traits>
1456 std::basic_ostream<_CharT, _Traits>&
1457 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1458 const uniform_real<_RealType>& __x)
1460 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1461 typedef typename __ostream_type::ios_base __ios_base;
1463 const typename __ios_base::fmtflags __flags = __os.flags();
1464 const _CharT __fill = __os.fill();
1465 const std::streamsize __precision = __os.precision();
1466 const _CharT __space = __os.widen(' ');
1467 __os.flags(__ios_base::scientific | __ios_base::left);
1469 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1471 __os << __x.min() << __space << __x.max();
1473 __os.flags(__flags);
1475 __os.precision(__precision);
1479 template<typename _RealType, typename _CharT, typename _Traits>
1480 std::basic_istream<_CharT, _Traits>&
1481 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1482 uniform_real<_RealType>& __x)
1484 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1485 typedef typename __istream_type::ios_base __ios_base;
1487 const typename __ios_base::fmtflags __flags = __is.flags();
1488 __is.flags(__ios_base::skipws);
1490 __is >> __x._M_min >> __x._M_max;
1492 __is.flags(__flags);
1497 template<typename _RealType, typename _CharT, typename _Traits>
1498 std::basic_ostream<_CharT, _Traits>&
1499 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1500 const exponential_distribution<_RealType>& __x)
1502 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1503 typedef typename __ostream_type::ios_base __ios_base;
1505 const typename __ios_base::fmtflags __flags = __os.flags();
1506 const _CharT __fill = __os.fill();
1507 const std::streamsize __precision = __os.precision();
1508 __os.flags(__ios_base::scientific | __ios_base::left);
1509 __os.fill(__os.widen(' '));
1510 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1512 __os << __x.lambda();
1514 __os.flags(__flags);
1516 __os.precision(__precision);
1522 * Polar method due to Marsaglia.
1524 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1525 * New York, 1986, Ch. V, Sect. 4.4.
1527 template<typename _RealType>
1528 template<class _UniformRandomNumberGenerator>
1529 typename normal_distribution<_RealType>::result_type
1530 normal_distribution<_RealType>::
1531 operator()(_UniformRandomNumberGenerator& __urng)
1535 if (_M_saved_available)
1537 _M_saved_available = false;
1542 result_type __x, __y, __r2;
1545 __x = result_type(2.0) * __urng() - 1.0;
1546 __y = result_type(2.0) * __urng() - 1.0;
1547 __r2 = __x * __x + __y * __y;
1549 while (__r2 > 1.0 || __r2 == 0.0);
1551 const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2);
1552 _M_saved = __x * __mult;
1553 _M_saved_available = true;
1554 __ret = __y * __mult;
1557 __ret = __ret * _M_sigma + _M_mean;
1561 template<typename _RealType, typename _CharT, typename _Traits>
1562 std::basic_ostream<_CharT, _Traits>&
1563 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1564 const normal_distribution<_RealType>& __x)
1566 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1567 typedef typename __ostream_type::ios_base __ios_base;
1569 const typename __ios_base::fmtflags __flags = __os.flags();
1570 const _CharT __fill = __os.fill();
1571 const std::streamsize __precision = __os.precision();
1572 const _CharT __space = __os.widen(' ');
1573 __os.flags(__ios_base::scientific | __ios_base::left);
1575 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1577 __os << __x._M_saved_available << __space
1578 << __x.mean() << __space
1580 if (__x._M_saved_available)
1581 __os << __space << __x._M_saved;
1583 __os.flags(__flags);
1585 __os.precision(__precision);
1589 template<typename _RealType, typename _CharT, typename _Traits>
1590 std::basic_istream<_CharT, _Traits>&
1591 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1592 normal_distribution<_RealType>& __x)
1594 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1595 typedef typename __istream_type::ios_base __ios_base;
1597 const typename __ios_base::fmtflags __flags = __is.flags();
1598 __is.flags(__ios_base::dec | __ios_base::skipws);
1600 __is >> __x._M_saved_available >> __x._M_mean
1602 if (__x._M_saved_available)
1603 __is >> __x._M_saved;
1605 __is.flags(__flags);
1610 template<typename _RealType>
1612 gamma_distribution<_RealType>::
1616 _M_l_d = std::sqrt(2 * _M_alpha - 1);
1618 _M_l_d = (std::pow(_M_alpha, _M_alpha / (1 - _M_alpha))
1623 * Cheng's rejection algorithm GB for alpha >= 1 and a modification
1624 * of Vaduva's rejection from Weibull algorithm due to Devroye for
1628 * Cheng, R. C. The Generation of Gamma Random Variables with Non-integral
1629 * Shape Parameter. Applied Statistics, 26, 71-75, 1977.
1631 * Vaduva, I. Computer Generation of Gamma Gandom Variables by Rejection
1632 * and Composition Procedures. Math. Operationsforschung and Statistik,
1633 * Series in Statistics, 8, 545-576, 1977.
1635 * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,
1636 * New York, 1986, Ch. IX, Sect. 3.4 (+ Errata!).
1638 template<typename _RealType>
1639 template<class _UniformRandomNumberGenerator>
1640 typename gamma_distribution<_RealType>::result_type
1641 gamma_distribution<_RealType>::
1642 operator()(_UniformRandomNumberGenerator& __urng)
1650 const result_type __b = _M_alpha
1651 - result_type(1.3862943611198906188344642429163531L);
1652 const result_type __c = _M_alpha + _M_l_d;
1653 const result_type __1l = 1 / _M_l_d;
1656 const result_type __k = 2.5040773967762740733732583523868748L;
1660 const result_type __u = __urng();
1661 const result_type __v = __urng();
1663 const result_type __y = __1l * std::log(__v / (1 - __v));
1664 __x = _M_alpha * std::exp(__y);
1666 const result_type __z = __u * __v * __v;
1667 const result_type __r = __b + __c * __y - __x;
1669 __reject = __r < result_type(4.5) * __z - __k;
1671 __reject = __r < std::log(__z);
1677 const result_type __c = 1 / _M_alpha;
1681 const result_type __z = -std::log(__urng());
1682 const result_type __e = -std::log(__urng());
1684 __x = std::pow(__z, __c);
1686 __reject = __z + __e < _M_l_d + __x;
1694 template<typename _RealType, typename _CharT, typename _Traits>
1695 std::basic_ostream<_CharT, _Traits>&
1696 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1697 const gamma_distribution<_RealType>& __x)
1699 typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
1700 typedef typename __ostream_type::ios_base __ios_base;
1702 const typename __ios_base::fmtflags __flags = __os.flags();
1703 const _CharT __fill = __os.fill();
1704 const std::streamsize __precision = __os.precision();
1705 __os.flags(__ios_base::scientific | __ios_base::left);
1706 __os.fill(__os.widen(' '));
1707 __os.precision(__gnu_cxx::__numeric_traits<_RealType>::__max_digits10);
1709 __os << __x.alpha();
1711 __os.flags(__flags);
1713 __os.precision(__precision);
1717 _GLIBCXX_END_NAMESPACE_VERSION