1 // random number generation -*- 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/>.
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_H
32 #define _GLIBCXX_TR1_RANDOM_H 1
34 #pragma GCC system_header
40 // [5.1] Random number generation
43 * @addtogroup tr1_random Random Number Generation
44 * A facility for generating random numbers on selected distributions.
49 * Implementation-space details.
53 template<typename _UIntType
, int __w
,
54 bool = __w
< std::numeric_limits
<_UIntType
>::digits
>
56 { static const _UIntType __value
= 0; };
58 template<typename _UIntType
, int __w
>
59 struct _Shift
<_UIntType
, __w
, true>
60 { static const _UIntType __value
= _UIntType(1) << __w
; };
62 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
, bool>
65 // Dispatch based on modulus value to prevent divide-by-zero compile-time
66 // errors when m == 0.
67 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
>
70 { return _Mod
<_Tp
, __a
, __c
, __m
, __m
== 0>::__calc(__x
); }
72 typedef __gnu_cxx::__conditional_type
<(sizeof(unsigned) == 4),
73 unsigned, unsigned long>::__type _UInt32Type
;
76 * An adaptor class for converting the output of any Generator into
77 * the input for a specific Distribution.
79 template<typename _Engine
, typename _Distribution
>
82 typedef typename remove_reference
<_Engine
>::type _BEngine
;
83 typedef typename
_BEngine::result_type _Engine_result_type
;
84 typedef typename
_Distribution::input_type result_type
;
87 _Adaptor(const _Engine
& __g
)
93 result_type __return_value
;
94 if (is_integral
<_Engine_result_type
>::value
95 && is_integral
<result_type
>::value
)
96 __return_value
= _M_g
.min();
98 __return_value
= result_type(0);
99 return __return_value
;
105 result_type __return_value
;
106 if (is_integral
<_Engine_result_type
>::value
107 && is_integral
<result_type
>::value
)
108 __return_value
= _M_g
.max();
109 else if (!is_integral
<result_type
>::value
)
110 __return_value
= result_type(1);
112 __return_value
= std::numeric_limits
<result_type
>::max() - 1;
113 return __return_value
;
117 * Converts a value generated by the adapted random number generator
118 * into a value in the input domain for the dependent random number
121 * Because the type traits are compile time constants only the
122 * appropriate clause of the if statements will actually be emitted
128 result_type __return_value
;
129 if (is_integral
<_Engine_result_type
>::value
130 && is_integral
<result_type
>::value
)
131 __return_value
= _M_g();
132 else if (!is_integral
<_Engine_result_type
>::value
133 && !is_integral
<result_type
>::value
)
134 __return_value
= result_type(_M_g() - _M_g
.min())
135 / result_type(_M_g
.max() - _M_g
.min());
136 else if (is_integral
<_Engine_result_type
>::value
137 && !is_integral
<result_type
>::value
)
138 __return_value
= result_type(_M_g() - _M_g
.min())
139 / result_type(_M_g
.max() - _M_g
.min() + result_type(1));
141 __return_value
= (((_M_g() - _M_g
.min())
142 / (_M_g
.max() - _M_g
.min()))
143 * std::numeric_limits
<result_type
>::max());
144 return __return_value
;
151 // Specialization for _Engine*.
152 template<typename _Engine
, typename _Distribution
>
153 struct _Adaptor
<_Engine
*, _Distribution
>
155 typedef typename
_Engine::result_type _Engine_result_type
;
156 typedef typename
_Distribution::input_type result_type
;
159 _Adaptor(_Engine
* __g
)
165 result_type __return_value
;
166 if (is_integral
<_Engine_result_type
>::value
167 && is_integral
<result_type
>::value
)
168 __return_value
= _M_g
->min();
170 __return_value
= result_type(0);
171 return __return_value
;
177 result_type __return_value
;
178 if (is_integral
<_Engine_result_type
>::value
179 && is_integral
<result_type
>::value
)
180 __return_value
= _M_g
->max();
181 else if (!is_integral
<result_type
>::value
)
182 __return_value
= result_type(1);
184 __return_value
= std::numeric_limits
<result_type
>::max() - 1;
185 return __return_value
;
191 result_type __return_value
;
192 if (is_integral
<_Engine_result_type
>::value
193 && is_integral
<result_type
>::value
)
194 __return_value
= (*_M_g
)();
195 else if (!is_integral
<_Engine_result_type
>::value
196 && !is_integral
<result_type
>::value
)
197 __return_value
= result_type((*_M_g
)() - _M_g
->min())
198 / result_type(_M_g
->max() - _M_g
->min());
199 else if (is_integral
<_Engine_result_type
>::value
200 && !is_integral
<result_type
>::value
)
201 __return_value
= result_type((*_M_g
)() - _M_g
->min())
202 / result_type(_M_g
->max() - _M_g
->min() + result_type(1));
204 __return_value
= ((((*_M_g
)() - _M_g
->min())
205 / (_M_g
->max() - _M_g
->min()))
206 * std::numeric_limits
<result_type
>::max());
207 return __return_value
;
213 } // namespace __detail
216 * Produces random numbers on a given distribution function using a
217 * non-uniform random number generation engine.
219 * @todo the engine_value_type needs to be studied more carefully.
221 template<typename _Engine
, typename _Dist
>
222 class variate_generator
224 // Concept requirements.
225 __glibcxx_class_requires(_Engine
, _CopyConstructibleConcept
)
226 // __glibcxx_class_requires(_Engine, _EngineConcept)
227 // __glibcxx_class_requires(_Dist, _EngineConcept)
230 typedef _Engine engine_type
;
231 typedef __detail::_Adaptor
<_Engine
, _Dist
> engine_value_type
;
232 typedef _Dist distribution_type
;
233 typedef typename
_Dist::result_type result_type
;
235 // tr1:5.1.1 table 5.1 requirement
236 typedef typename
__gnu_cxx::__enable_if
<
237 is_arithmetic
<result_type
>::value
, result_type
>::__type _IsValidType
;
240 * Constructs a variate generator with the uniform random number
241 * generator @p __eng for the random distribution @p __dist.
243 * @throws Any exceptions which may thrown by the copy constructors of
244 * the @p _Engine or @p _Dist objects.
246 variate_generator(engine_type __eng
, distribution_type __dist
)
247 : _M_engine(__eng
), _M_dist(__dist
) { }
250 * Gets the next generated value on the distribution.
254 { return _M_dist(_M_engine
); }
259 template<typename _Tp
>
261 operator()(_Tp __value
)
262 { return _M_dist(_M_engine
, __value
); }
265 * Gets a reference to the underlying uniform random number generator
270 { return _M_engine
; }
273 * Gets a const reference to the underlying uniform random number
276 const engine_value_type
&
278 { return _M_engine
; }
281 * Gets a reference to the underlying random distribution.
288 * Gets a const reference to the underlying random distribution.
290 const distribution_type
&
295 * Gets the closed lower bound of the distribution interval.
299 { return this->distribution().min(); }
302 * Gets the closed upper bound of the distribution interval.
306 { return this->distribution().max(); }
309 engine_value_type _M_engine
;
310 distribution_type _M_dist
;
315 * @addtogroup tr1_random_generators Random Number Generators
316 * @ingroup tr1_random
318 * These classes define objects which provide random or pseudorandom
319 * numbers, either from a discrete or a continuous interval. The
320 * random number generator supplied as a part of this library are
321 * all uniform random number generators which provide a sequence of
322 * random number uniformly distributed over their range.
324 * A number generator is a function object with an operator() that
325 * takes zero arguments and returns a number.
327 * A compliant random number generator must satisfy the following
328 * requirements. <table border=1 cellpadding=10 cellspacing=0>
329 * <caption align=top>Random Number Generator Requirements</caption>
330 * <tr><td>To be documented.</td></tr> </table>
336 * @brief A model of a linear congruential random number generator.
338 * A random number generator that produces pseudorandom numbers using the
339 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
341 * The template parameter @p _UIntType must be an unsigned integral type
342 * large enough to store values up to (__m-1). If the template parameter
343 * @p __m is 0, the modulus @p __m used is
344 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
345 * parameters @p __a and @p __c must be less than @p __m.
347 * The size of the state is @f$ 1 @f$.
349 template<class _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
350 class linear_congruential
352 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
353 // __glibcpp_class_requires(__a < __m && __c < __m)
356 /** The type of the generated random value. */
357 typedef _UIntType result_type
;
359 /** The multiplier. */
360 static const _UIntType multiplier
= __a
;
362 static const _UIntType increment
= __c
;
364 static const _UIntType modulus
= __m
;
367 * Constructs a %linear_congruential random number generator engine with
368 * seed @p __s. The default seed value is 1.
370 * @param __s The initial seed value.
373 linear_congruential(unsigned long __x0
= 1)
374 { this->seed(__x0
); }
377 * Constructs a %linear_congruential random number generator engine
378 * seeded from the generator function @p __g.
380 * @param __g The seed generator function.
383 linear_congruential(_Gen
& __g
)
387 * Reseeds the %linear_congruential random number generator engine
388 * sequence to the seed @g __s.
390 * @param __s The new seed.
393 seed(unsigned long __s
= 1);
396 * Reseeds the %linear_congruential random number generator engine
397 * sequence using values from the generator function @p __g.
399 * @param __g the seed generator function.
404 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
407 * Gets the smallest possible value in the output range.
409 * The minimum depends on the @p __c parameter: if it is zero, the
410 * minimum generated must be > 0, otherwise 0 is allowed.
414 { return (__detail::__mod
<_UIntType
, 1, 0, __m
>(__c
) == 0) ? 1 : 0; }
417 * Gets the largest possible value in the output range.
424 * Gets the next random number in the sequence.
430 * Compares two linear congruential random number generator
431 * objects of the same type for equality.
433 * @param __lhs A linear congruential random number generator object.
434 * @param __rhs Another linear congruential random number generator obj.
436 * @returns true if the two objects are equal, false otherwise.
439 operator==(const linear_congruential
& __lhs
,
440 const linear_congruential
& __rhs
)
441 { return __lhs
._M_x
== __rhs
._M_x
; }
444 * Compares two linear congruential random number generator
445 * objects of the same type for inequality.
447 * @param __lhs A linear congruential random number generator object.
448 * @param __rhs Another linear congruential random number generator obj.
450 * @returns true if the two objects are not equal, false otherwise.
453 operator!=(const linear_congruential
& __lhs
,
454 const linear_congruential
& __rhs
)
455 { return !(__lhs
== __rhs
); }
458 * Writes the textual representation of the state x(i) of x to @p __os.
460 * @param __os The output stream.
461 * @param __lcr A % linear_congruential random number generator.
464 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
466 typename _CharT
, typename _Traits
>
467 friend std::basic_ostream
<_CharT
, _Traits
>&
468 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
469 const linear_congruential
<_UIntType1
, __a1
, __c1
,
473 * Sets the state of the engine by reading its textual
474 * representation from @p __is.
476 * The textual representation must have been previously written using an
477 * output stream whose imbued locale and whose type's template
478 * specialization arguments _CharT and _Traits were the same as those of
481 * @param __is The input stream.
482 * @param __lcr A % linear_congruential random number generator.
485 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
487 typename _CharT
, typename _Traits
>
488 friend std::basic_istream
<_CharT
, _Traits
>&
489 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
490 linear_congruential
<_UIntType1
, __a1
, __c1
, __m1
>& __lcr
);
495 seed(_Gen
& __g
, true_type
)
496 { return seed(static_cast<unsigned long>(__g
)); }
500 seed(_Gen
& __g
, false_type
);
506 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
508 typedef linear_congruential
<unsigned long, 16807, 0, 2147483647> minstd_rand0
;
511 * An alternative LCR (Lehmer Generator function) .
513 typedef linear_congruential
<unsigned long, 48271, 0, 2147483647> minstd_rand
;
517 * A generalized feedback shift register discrete random number generator.
519 * This algorithm avoids multiplication and division and is designed to be
520 * friendly to a pipelined architecture. If the parameters are chosen
521 * correctly, this generator will produce numbers with a very long period and
522 * fairly good apparent entropy, although still not cryptographically strong.
524 * The best way to use this generator is with the predefined mt19937 class.
526 * This algorithm was originally invented by Makoto Matsumoto and
529 * @var word_size The number of bits in each element of the state vector.
530 * @var state_size The degree of recursion.
531 * @var shift_size The period parameter.
532 * @var mask_bits The separation point bit index.
533 * @var parameter_a The last row of the twist matrix.
534 * @var output_u The first right-shift tempering matrix parameter.
535 * @var output_s The first left-shift tempering matrix parameter.
536 * @var output_b The first left-shift tempering matrix mask.
537 * @var output_t The second left-shift tempering matrix parameter.
538 * @var output_c The second left-shift tempering matrix mask.
539 * @var output_l The second right-shift tempering matrix parameter.
541 template<class _UIntType
, int __w
, int __n
, int __m
, int __r
,
542 _UIntType __a
, int __u
, int __s
, _UIntType __b
, int __t
,
543 _UIntType __c
, int __l
>
544 class mersenne_twister
546 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
550 typedef _UIntType result_type
;
553 static const int word_size
= __w
;
554 static const int state_size
= __n
;
555 static const int shift_size
= __m
;
556 static const int mask_bits
= __r
;
557 static const _UIntType parameter_a
= __a
;
558 static const int output_u
= __u
;
559 static const int output_s
= __s
;
560 static const _UIntType output_b
= __b
;
561 static const int output_t
= __t
;
562 static const _UIntType output_c
= __c
;
563 static const int output_l
= __l
;
565 // constructors and member function
570 mersenne_twister(unsigned long __value
)
574 mersenne_twister(_Gen
& __g
)
582 seed(unsigned long __value
);
587 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
595 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
601 * Compares two % mersenne_twister random number generator objects of
602 * the same type for equality.
604 * @param __lhs A % mersenne_twister random number generator object.
605 * @param __rhs Another % mersenne_twister random number generator
608 * @returns true if the two objects are equal, false otherwise.
611 operator==(const mersenne_twister
& __lhs
,
612 const mersenne_twister
& __rhs
)
613 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
616 * Compares two % mersenne_twister random number generator objects of
617 * the same type for inequality.
619 * @param __lhs A % mersenne_twister random number generator object.
620 * @param __rhs Another % mersenne_twister random number generator
623 * @returns true if the two objects are not equal, false otherwise.
626 operator!=(const mersenne_twister
& __lhs
,
627 const mersenne_twister
& __rhs
)
628 { return !(__lhs
== __rhs
); }
631 * Inserts the current state of a % mersenne_twister random number
632 * generator engine @p __x into the output stream @p __os.
634 * @param __os An output stream.
635 * @param __x A % mersenne_twister random number generator engine.
637 * @returns The output stream with the state of @p __x inserted or in
640 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
641 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
642 _UIntType1 __c1
, int __l1
,
643 typename _CharT
, typename _Traits
>
644 friend std::basic_ostream
<_CharT
, _Traits
>&
645 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
646 const mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
647 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
650 * Extracts the current state of a % mersenne_twister random number
651 * generator engine @p __x from the input stream @p __is.
653 * @param __is An input stream.
654 * @param __x A % mersenne_twister random number generator engine.
656 * @returns The input stream with the state of @p __x extracted or in
659 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
660 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
661 _UIntType1 __c1
, int __l1
,
662 typename _CharT
, typename _Traits
>
663 friend std::basic_istream
<_CharT
, _Traits
>&
664 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
665 mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
666 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
671 seed(_Gen
& __g
, true_type
)
672 { return seed(static_cast<unsigned long>(__g
)); }
676 seed(_Gen
& __g
, false_type
);
678 _UIntType _M_x
[state_size
];
683 * The classic Mersenne Twister.
686 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
687 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
688 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
690 typedef mersenne_twister
<
691 unsigned long, 32, 624, 397, 31,
699 * @brief The Marsaglia-Zaman generator.
701 * This is a model of a Generalized Fibonacci discrete random number
702 * generator, sometimes referred to as the SWC generator.
704 * A discrete random number generator that produces pseudorandom
705 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
706 * carry_{i-1}) \bmod m @f$.
708 * The size of the state is @f$ r @f$
709 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
711 * N1688[4.13] says <em>the template parameter _IntType shall denote
712 * an integral type large enough to store values up to m</em>.
714 * @var _M_x The state of the generator. This is a ring buffer.
715 * @var _M_carry The carry.
716 * @var _M_p Current index of x(i - r).
718 template<typename _IntType
, _IntType __m
, int __s
, int __r
>
719 class subtract_with_carry
721 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
724 /** The type of the generated random value. */
725 typedef _IntType result_type
;
728 static const _IntType modulus
= __m
;
729 static const int long_lag
= __r
;
730 static const int short_lag
= __s
;
733 * Constructs a default-initialized % subtract_with_carry random number
736 subtract_with_carry()
740 * Constructs an explicitly seeded % subtract_with_carry random number
744 subtract_with_carry(unsigned long __value
)
745 { this->seed(__value
); }
748 * Constructs a %subtract_with_carry random number generator engine
749 * seeded from the generator function @p __g.
751 * @param __g The seed generator function.
754 subtract_with_carry(_Gen
& __g
)
758 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
760 * N1688[4.19] modifies this as follows. If @p __value == 0,
761 * sets value to 19780503. In any case, with a linear
762 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
763 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
764 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
765 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
766 * set carry to 1, otherwise sets carry to 0.
769 seed(unsigned long __value
= 19780503);
772 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
773 * random number generator.
778 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
781 * Gets the inclusive minimum value of the range of random integers
782 * returned by this generator.
789 * Gets the inclusive maximum value of the range of random integers
790 * returned by this generator.
794 { return this->modulus
- 1; }
797 * Gets the next random number in the sequence.
803 * Compares two % subtract_with_carry random number generator objects of
804 * the same type for equality.
806 * @param __lhs A % subtract_with_carry random number generator object.
807 * @param __rhs Another % subtract_with_carry random number generator
810 * @returns true if the two objects are equal, false otherwise.
813 operator==(const subtract_with_carry
& __lhs
,
814 const subtract_with_carry
& __rhs
)
815 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
818 * Compares two % subtract_with_carry random number generator objects of
819 * the same type for inequality.
821 * @param __lhs A % subtract_with_carry random number generator object.
822 * @param __rhs Another % subtract_with_carry random number generator
825 * @returns true if the two objects are not equal, false otherwise.
828 operator!=(const subtract_with_carry
& __lhs
,
829 const subtract_with_carry
& __rhs
)
830 { return !(__lhs
== __rhs
); }
833 * Inserts the current state of a % subtract_with_carry random number
834 * generator engine @p __x into the output stream @p __os.
836 * @param __os An output stream.
837 * @param __x A % subtract_with_carry random number generator engine.
839 * @returns The output stream with the state of @p __x inserted or in
842 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
843 typename _CharT
, typename _Traits
>
844 friend std::basic_ostream
<_CharT
, _Traits
>&
845 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
846 const subtract_with_carry
<_IntType1
, __m1
, __s1
,
850 * Extracts the current state of a % subtract_with_carry random number
851 * generator engine @p __x from the input stream @p __is.
853 * @param __is An input stream.
854 * @param __x A % subtract_with_carry random number generator engine.
856 * @returns The input stream with the state of @p __x extracted or in
859 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
860 typename _CharT
, typename _Traits
>
861 friend std::basic_istream
<_CharT
, _Traits
>&
862 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
863 subtract_with_carry
<_IntType1
, __m1
, __s1
, __r1
>& __x
);
868 seed(_Gen
& __g
, true_type
)
869 { return seed(static_cast<unsigned long>(__g
)); }
873 seed(_Gen
& __g
, false_type
);
875 typedef typename
__gnu_cxx::__add_unsigned
<_IntType
>::__type _UIntType
;
877 _UIntType _M_x
[long_lag
];
884 * @brief The Marsaglia-Zaman generator (floats version).
886 * @var _M_x The state of the generator. This is a ring buffer.
887 * @var _M_carry The carry.
888 * @var _M_p Current index of x(i - r).
889 * @var _M_npows Precomputed negative powers of 2.
891 template<typename _RealType
, int __w
, int __s
, int __r
>
892 class subtract_with_carry_01
895 /** The type of the generated random value. */
896 typedef _RealType result_type
;
899 static const int word_size
= __w
;
900 static const int long_lag
= __r
;
901 static const int short_lag
= __s
;
904 * Constructs a default-initialized % subtract_with_carry_01 random
907 subtract_with_carry_01()
910 _M_initialize_npows();
914 * Constructs an explicitly seeded % subtract_with_carry_01 random number
918 subtract_with_carry_01(unsigned long __value
)
921 _M_initialize_npows();
925 * Constructs a % subtract_with_carry_01 random number generator engine
926 * seeded from the generator function @p __g.
928 * @param __g The seed generator function.
931 subtract_with_carry_01(_Gen
& __g
)
934 _M_initialize_npows();
938 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
941 seed(unsigned long __value
= 19780503);
944 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
945 * random number generator.
950 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
953 * Gets the minimum value of the range of random floats
954 * returned by this generator.
961 * Gets the maximum value of the range of random floats
962 * returned by this generator.
969 * Gets the next random number in the sequence.
975 * Compares two % subtract_with_carry_01 random number generator objects
976 * of the same type for equality.
978 * @param __lhs A % subtract_with_carry_01 random number
980 * @param __rhs Another % subtract_with_carry_01 random number generator
983 * @returns true if the two objects are equal, false otherwise.
986 operator==(const subtract_with_carry_01
& __lhs
,
987 const subtract_with_carry_01
& __rhs
)
989 for (int __i
= 0; __i
< long_lag
; ++__i
)
990 if (!std::equal(__lhs
._M_x
[__i
], __lhs
._M_x
[__i
] + __n
,
997 * Compares two % subtract_with_carry_01 random number generator objects
998 * of the same type for inequality.
1000 * @param __lhs A % subtract_with_carry_01 random number
1003 * @param __rhs Another % subtract_with_carry_01 random number generator
1006 * @returns true if the two objects are not equal, false otherwise.
1009 operator!=(const subtract_with_carry_01
& __lhs
,
1010 const subtract_with_carry_01
& __rhs
)
1011 { return !(__lhs
== __rhs
); }
1014 * Inserts the current state of a % subtract_with_carry_01 random number
1015 * generator engine @p __x into the output stream @p __os.
1017 * @param __os An output stream.
1018 * @param __x A % subtract_with_carry_01 random number generator engine.
1020 * @returns The output stream with the state of @p __x inserted or in
1023 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
1024 typename _CharT
, typename _Traits
>
1025 friend std::basic_ostream
<_CharT
, _Traits
>&
1026 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1027 const subtract_with_carry_01
<_RealType1
, __w1
, __s1
,
1031 * Extracts the current state of a % subtract_with_carry_01 random number
1032 * generator engine @p __x from the input stream @p __is.
1034 * @param __is An input stream.
1035 * @param __x A % subtract_with_carry_01 random number generator engine.
1037 * @returns The input stream with the state of @p __x extracted or in
1040 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
1041 typename _CharT
, typename _Traits
>
1042 friend std::basic_istream
<_CharT
, _Traits
>&
1043 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1044 subtract_with_carry_01
<_RealType1
, __w1
, __s1
, __r1
>& __x
);
1047 template<class _Gen
>
1049 seed(_Gen
& __g
, true_type
)
1050 { return seed(static_cast<unsigned long>(__g
)); }
1052 template<class _Gen
>
1054 seed(_Gen
& __g
, false_type
);
1057 _M_initialize_npows();
1059 static const int __n
= (__w
+ 31) / 32;
1061 typedef __detail::_UInt32Type _UInt32Type
;
1062 _UInt32Type _M_x
[long_lag
][__n
];
1063 _RealType _M_npows
[__n
];
1064 _UInt32Type _M_carry
;
1068 typedef subtract_with_carry_01
<float, 24, 10, 24> ranlux_base_01
;
1070 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1071 // 508. Bad parameters for ranlux64_base_01.
1072 typedef subtract_with_carry_01
<double, 48, 5, 12> ranlux64_base_01
;
1076 * Produces random numbers from some base engine by discarding blocks of
1079 * 0 <= @p __r <= @p __p
1081 template<class _UniformRandomNumberGenerator
, int __p
, int __r
>
1084 // __glibcxx_class_requires(typename base_type::result_type,
1085 // ArithmeticTypeConcept)
1088 /** The type of the underlying generator engine. */
1089 typedef _UniformRandomNumberGenerator base_type
;
1090 /** The type of the generated random value. */
1091 typedef typename
base_type::result_type result_type
;
1094 static const int block_size
= __p
;
1095 static const int used_block
= __r
;
1098 * Constructs a default %discard_block engine.
1100 * The underlying engine is default constructed as well.
1106 * Copy constructs a %discard_block engine.
1108 * Copies an existing base class random number generator.
1109 * @param rng An existing (base class) engine object.
1112 discard_block(const base_type
& __rng
)
1113 : _M_b(__rng
), _M_n(0) { }
1116 * Seed constructs a %discard_block engine.
1118 * Constructs the underlying generator engine seeded with @p __s.
1119 * @param __s A seed value for the base class engine.
1122 discard_block(unsigned long __s
)
1123 : _M_b(__s
), _M_n(0) { }
1126 * Generator construct a %discard_block engine.
1128 * @param __g A seed generator function.
1130 template<class _Gen
>
1131 discard_block(_Gen
& __g
)
1132 : _M_b(__g
), _M_n(0) { }
1135 * Reseeds the %discard_block object with the default seed for the
1136 * underlying base class generator engine.
1145 * Reseeds the %discard_block object with the given seed generator
1147 * @param __g A seed generator function.
1149 template<class _Gen
>
1150 void seed(_Gen
& __g
)
1157 * Gets a const reference to the underlying generator engine object.
1164 * Gets the minimum value in the generated random number range.
1168 { return _M_b
.min(); }
1171 * Gets the maximum value in the generated random number range.
1175 { return _M_b
.max(); }
1178 * Gets the next value in the generated random number sequence.
1184 * Compares two %discard_block random number generator objects of
1185 * the same type for equality.
1187 * @param __lhs A %discard_block random number generator object.
1188 * @param __rhs Another %discard_block random number generator
1191 * @returns true if the two objects are equal, false otherwise.
1194 operator==(const discard_block
& __lhs
, const discard_block
& __rhs
)
1195 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
1198 * Compares two %discard_block random number generator objects of
1199 * the same type for inequality.
1201 * @param __lhs A %discard_block random number generator object.
1202 * @param __rhs Another %discard_block random number generator
1205 * @returns true if the two objects are not equal, false otherwise.
1208 operator!=(const discard_block
& __lhs
, const discard_block
& __rhs
)
1209 { return !(__lhs
== __rhs
); }
1212 * Inserts the current state of a %discard_block random number
1213 * generator engine @p __x into the output stream @p __os.
1215 * @param __os An output stream.
1216 * @param __x A %discard_block random number generator engine.
1218 * @returns The output stream with the state of @p __x inserted or in
1221 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1222 typename _CharT
, typename _Traits
>
1223 friend std::basic_ostream
<_CharT
, _Traits
>&
1224 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1225 const discard_block
<_UniformRandomNumberGenerator1
,
1229 * Extracts the current state of a % subtract_with_carry random number
1230 * generator engine @p __x from the input stream @p __is.
1232 * @param __is An input stream.
1233 * @param __x A %discard_block random number generator engine.
1235 * @returns The input stream with the state of @p __x extracted or in
1238 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1239 typename _CharT
, typename _Traits
>
1240 friend std::basic_istream
<_CharT
, _Traits
>&
1241 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1242 discard_block
<_UniformRandomNumberGenerator1
,
1252 * James's luxury-level-3 integer adaptation of Luescher's generator.
1254 typedef discard_block
<
1255 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1261 * James's luxury-level-4 integer adaptation of Luescher's generator.
1263 typedef discard_block
<
1264 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1269 typedef discard_block
<
1270 subtract_with_carry_01
<float, 24, 10, 24>,
1275 typedef discard_block
<
1276 subtract_with_carry_01
<float, 24, 10, 24>,
1283 * A random number generator adaptor class that combines two random number
1284 * generator engines into a single output sequence.
1286 template<class _UniformRandomNumberGenerator1
, int __s1
,
1287 class _UniformRandomNumberGenerator2
, int __s2
>
1290 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1291 // result_type, ArithmeticTypeConcept)
1292 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1293 // result_type, ArithmeticTypeConcept)
1296 /** The type of the first underlying generator engine. */
1297 typedef _UniformRandomNumberGenerator1 base1_type
;
1298 /** The type of the second underlying generator engine. */
1299 typedef _UniformRandomNumberGenerator2 base2_type
;
1302 typedef typename
base1_type::result_type _Result_type1
;
1303 typedef typename
base2_type::result_type _Result_type2
;
1306 /** The type of the generated random value. */
1307 typedef typename
__gnu_cxx::__conditional_type
<(sizeof(_Result_type1
)
1308 > sizeof(_Result_type2
)),
1309 _Result_type1
, _Result_type2
>::__type result_type
;
1312 static const int shift1
= __s1
;
1313 static const int shift2
= __s2
;
1315 // constructors and member function
1318 { _M_initialize_max(); }
1320 xor_combine(const base1_type
& __rng1
, const base2_type
& __rng2
)
1321 : _M_b1(__rng1
), _M_b2(__rng2
)
1322 { _M_initialize_max(); }
1324 xor_combine(unsigned long __s
)
1325 : _M_b1(__s
), _M_b2(__s
+ 1)
1326 { _M_initialize_max(); }
1328 template<class _Gen
>
1329 xor_combine(_Gen
& __g
)
1330 : _M_b1(__g
), _M_b2(__g
)
1331 { _M_initialize_max(); }
1340 template<class _Gen
>
1365 * Gets the next random number in the sequence.
1367 // NB: Not exactly the TR1 formula, per N2079 instead.
1371 return ((result_type(_M_b1() - _M_b1
.min()) << shift1
)
1372 ^ (result_type(_M_b2() - _M_b2
.min()) << shift2
));
1376 * Compares two %xor_combine random number generator objects of
1377 * the same type for equality.
1379 * @param __lhs A %xor_combine random number generator object.
1380 * @param __rhs Another %xor_combine random number generator
1383 * @returns true if the two objects are equal, false otherwise.
1386 operator==(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1388 return (__lhs
.base1() == __rhs
.base1())
1389 && (__lhs
.base2() == __rhs
.base2());
1393 * Compares two %xor_combine random number generator objects of
1394 * the same type for inequality.
1396 * @param __lhs A %xor_combine random number generator object.
1397 * @param __rhs Another %xor_combine random number generator
1400 * @returns true if the two objects are not equal, false otherwise.
1403 operator!=(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1404 { return !(__lhs
== __rhs
); }
1407 * Inserts the current state of a %xor_combine random number
1408 * generator engine @p __x into the output stream @p __os.
1410 * @param __os An output stream.
1411 * @param __x A %xor_combine random number generator engine.
1413 * @returns The output stream with the state of @p __x inserted or in
1416 template<class _UniformRandomNumberGenerator11
, int __s11
,
1417 class _UniformRandomNumberGenerator21
, int __s21
,
1418 typename _CharT
, typename _Traits
>
1419 friend std::basic_ostream
<_CharT
, _Traits
>&
1420 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1421 const xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1422 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1425 * Extracts the current state of a %xor_combine random number
1426 * generator engine @p __x from the input stream @p __is.
1428 * @param __is An input stream.
1429 * @param __x A %xor_combine random number generator engine.
1431 * @returns The input stream with the state of @p __x extracted or in
1434 template<class _UniformRandomNumberGenerator11
, int __s11
,
1435 class _UniformRandomNumberGenerator21
, int __s21
,
1436 typename _CharT
, typename _Traits
>
1437 friend std::basic_istream
<_CharT
, _Traits
>&
1438 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1439 xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1440 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1444 _M_initialize_max();
1447 _M_initialize_max_aux(result_type
, result_type
, int);
1456 * A standard interface to a platform-specific non-deterministic
1457 * random number generator (if any are available).
1463 typedef unsigned int result_type
;
1465 // constructors, destructors and member functions
1467 #ifdef _GLIBCXX_USE_RANDOM_TR1
1470 random_device(const std::string
& __token
= "/dev/urandom")
1472 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1473 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1474 std::__throw_runtime_error(__N("random_device::"
1475 "random_device(const std::string&)"));
1479 { std::fclose(_M_file
); }
1484 random_device(const std::string
& __token
= "mt19937")
1485 : _M_mt(_M_strtoul(__token
)) { }
1488 static unsigned long
1489 _M_strtoul(const std::string
& __str
)
1491 unsigned long __ret
= 5489UL;
1492 if (__str
!= "mt19937")
1494 const char* __nptr
= __str
.c_str();
1496 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1497 if (*__nptr
== '\0' || *__endptr
!= '\0')
1498 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1499 "(const std::string&)"));
1510 { return std::numeric_limits
<result_type
>::min(); }
1514 { return std::numeric_limits
<result_type
>::max(); }
1523 #ifdef _GLIBCXX_USE_RANDOM_TR1
1525 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1534 random_device(const random_device
&);
1535 void operator=(const random_device
&);
1537 #ifdef _GLIBCXX_USE_RANDOM_TR1
1544 /* @} */ // group tr1_random_generators
1547 * @addtogroup tr1_random_distributions Random Number Distributions
1548 * @ingroup tr1_random
1553 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1554 * @ingroup tr1_random_distributions
1559 * @brief Uniform discrete distribution for random numbers.
1560 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1561 * probability throughout the range.
1563 template<typename _IntType
= int>
1566 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
1569 /** The type of the parameters of the distribution. */
1570 typedef _IntType input_type
;
1571 /** The type of the range of the distribution. */
1572 typedef _IntType result_type
;
1576 * Constructs a uniform distribution object.
1579 uniform_int(_IntType __min
= 0, _IntType __max
= 9)
1580 : _M_min(__min
), _M_max(__max
)
1582 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
1586 * Gets the inclusive lower bound of the distribution range.
1593 * Gets the inclusive upper bound of the distribution range.
1600 * Resets the distribution state.
1602 * Does nothing for the uniform integer distribution.
1608 * Gets a uniformly distributed random number in the range
1611 template<typename _UniformRandomNumberGenerator
>
1613 operator()(_UniformRandomNumberGenerator
& __urng
)
1615 typedef typename
_UniformRandomNumberGenerator::result_type
1617 return _M_call(__urng
, _M_min
, _M_max
,
1618 typename is_integral
<_UResult_type
>::type());
1622 * Gets a uniform random number in the range @f$[0, n)@f$.
1624 * This function is aimed at use with std::random_shuffle.
1626 template<typename _UniformRandomNumberGenerator
>
1628 operator()(_UniformRandomNumberGenerator
& __urng
, result_type __n
)
1630 typedef typename
_UniformRandomNumberGenerator::result_type
1632 return _M_call(__urng
, 0, __n
- 1,
1633 typename is_integral
<_UResult_type
>::type());
1637 * Inserts a %uniform_int random number distribution @p __x into the
1638 * output stream @p os.
1640 * @param __os An output stream.
1641 * @param __x A %uniform_int random number distribution.
1643 * @returns The output stream with the state of @p __x inserted or in
1646 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1647 friend std::basic_ostream
<_CharT
, _Traits
>&
1648 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1649 const uniform_int
<_IntType1
>& __x
);
1652 * Extracts a %uniform_int random number distribution
1653 * @p __x from the input stream @p __is.
1655 * @param __is An input stream.
1656 * @param __x A %uniform_int random number generator engine.
1658 * @returns The input stream with @p __x extracted or in an error state.
1660 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1661 friend std::basic_istream
<_CharT
, _Traits
>&
1662 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1663 uniform_int
<_IntType1
>& __x
);
1666 template<typename _UniformRandomNumberGenerator
>
1668 _M_call(_UniformRandomNumberGenerator
& __urng
,
1669 result_type __min
, result_type __max
, true_type
);
1671 template<typename _UniformRandomNumberGenerator
>
1673 _M_call(_UniformRandomNumberGenerator
& __urng
,
1674 result_type __min
, result_type __max
, false_type
)
1676 return result_type((__urng() - __urng
.min())
1677 / (__urng
.max() - __urng
.min())
1678 * (__max
- __min
+ 1)) + __min
;
1687 * @brief A Bernoulli random number distribution.
1689 * Generates a sequence of true and false values with likelihood @f$ p @f$
1690 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1692 class bernoulli_distribution
1695 typedef int input_type
;
1696 typedef bool result_type
;
1700 * Constructs a Bernoulli distribution with likelihood @p p.
1702 * @param __p [IN] The likelihood of a true result being returned. Must
1703 * be in the interval @f$ [0, 1] @f$.
1706 bernoulli_distribution(double __p
= 0.5)
1709 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
1713 * Gets the @p p parameter of the distribution.
1720 * Resets the distribution state.
1722 * Does nothing for a Bernoulli distribution.
1728 * Gets the next value in the Bernoullian sequence.
1730 template<class _UniformRandomNumberGenerator
>
1732 operator()(_UniformRandomNumberGenerator
& __urng
)
1734 if ((__urng() - __urng
.min()) < _M_p
* (__urng
.max() - __urng
.min()))
1740 * Inserts a %bernoulli_distribution random number distribution
1741 * @p __x into the output stream @p __os.
1743 * @param __os An output stream.
1744 * @param __x A %bernoulli_distribution random number distribution.
1746 * @returns The output stream with the state of @p __x inserted or in
1749 template<typename _CharT
, typename _Traits
>
1750 friend std::basic_ostream
<_CharT
, _Traits
>&
1751 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1752 const bernoulli_distribution
& __x
);
1755 * Extracts a %bernoulli_distribution random number distribution
1756 * @p __x from the input stream @p __is.
1758 * @param __is An input stream.
1759 * @param __x A %bernoulli_distribution random number generator engine.
1761 * @returns The input stream with @p __x extracted or in an error state.
1763 template<typename _CharT
, typename _Traits
>
1764 friend std::basic_istream
<_CharT
, _Traits
>&
1765 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1766 bernoulli_distribution
& __x
)
1767 { return __is
>> __x
._M_p
; }
1775 * @brief A discrete geometric random number distribution.
1777 * The formula for the geometric probability mass function is
1778 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1781 template<typename _IntType
= int, typename _RealType
= double>
1782 class geometric_distribution
1786 typedef _RealType input_type
;
1787 typedef _IntType result_type
;
1789 // constructors and member function
1791 geometric_distribution(const _RealType
& __p
= _RealType(0.5))
1794 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
1799 * Gets the distribution parameter @p p.
1808 template<class _UniformRandomNumberGenerator
>
1810 operator()(_UniformRandomNumberGenerator
& __urng
);
1813 * Inserts a %geometric_distribution random number distribution
1814 * @p __x into the output stream @p __os.
1816 * @param __os An output stream.
1817 * @param __x A %geometric_distribution random number distribution.
1819 * @returns The output stream with the state of @p __x inserted or in
1822 template<typename _IntType1
, typename _RealType1
,
1823 typename _CharT
, typename _Traits
>
1824 friend std::basic_ostream
<_CharT
, _Traits
>&
1825 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1826 const geometric_distribution
<_IntType1
, _RealType1
>& __x
);
1829 * Extracts a %geometric_distribution random number distribution
1830 * @p __x from the input stream @p __is.
1832 * @param __is An input stream.
1833 * @param __x A %geometric_distribution random number generator engine.
1835 * @returns The input stream with @p __x extracted or in an error state.
1837 template<typename _CharT
, typename _Traits
>
1838 friend std::basic_istream
<_CharT
, _Traits
>&
1839 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1840 geometric_distribution
& __x
)
1843 __x
._M_initialize();
1850 { _M_log_p
= std::log(_M_p
); }
1857 template<typename _RealType
>
1858 class normal_distribution
;
1861 * @brief A discrete Poisson random number distribution.
1863 * The formula for the Poisson probability mass function is
1864 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1865 * parameter of the distribution.
1867 template<typename _IntType
= int, typename _RealType
= double>
1868 class poisson_distribution
1872 typedef _RealType input_type
;
1873 typedef _IntType result_type
;
1875 // constructors and member function
1877 poisson_distribution(const _RealType
& __mean
= _RealType(1))
1878 : _M_mean(__mean
), _M_nd()
1880 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
1885 * Gets the distribution parameter @p mean.
1895 template<class _UniformRandomNumberGenerator
>
1897 operator()(_UniformRandomNumberGenerator
& __urng
);
1900 * Inserts a %poisson_distribution random number distribution
1901 * @p __x into the output stream @p __os.
1903 * @param __os An output stream.
1904 * @param __x A %poisson_distribution random number distribution.
1906 * @returns The output stream with the state of @p __x inserted or in
1909 template<typename _IntType1
, typename _RealType1
,
1910 typename _CharT
, typename _Traits
>
1911 friend std::basic_ostream
<_CharT
, _Traits
>&
1912 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1913 const poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1916 * Extracts a %poisson_distribution random number distribution
1917 * @p __x from the input stream @p __is.
1919 * @param __is An input stream.
1920 * @param __x A %poisson_distribution random number generator engine.
1922 * @returns The input stream with @p __x extracted or in an error state.
1924 template<typename _IntType1
, typename _RealType1
,
1925 typename _CharT
, typename _Traits
>
1926 friend std::basic_istream
<_CharT
, _Traits
>&
1927 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1928 poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1934 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1935 normal_distribution
<_RealType
> _M_nd
;
1939 // Hosts either log(mean) or the threshold of the simple method.
1940 _RealType _M_lm_thr
;
1941 #if _GLIBCXX_USE_C99_MATH_TR1
1942 _RealType _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
1948 * @brief A discrete binomial random number distribution.
1950 * The formula for the binomial probability mass function is
1951 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1952 * and @f$ p @f$ are the parameters of the distribution.
1954 template<typename _IntType
= int, typename _RealType
= double>
1955 class binomial_distribution
1959 typedef _RealType input_type
;
1960 typedef _IntType result_type
;
1962 // constructors and member function
1964 binomial_distribution(_IntType __t
= 1,
1965 const _RealType
& __p
= _RealType(0.5))
1966 : _M_t(__t
), _M_p(__p
), _M_nd()
1968 _GLIBCXX_DEBUG_ASSERT((_M_t
>= 0) && (_M_p
>= 0.0) && (_M_p
<= 1.0));
1973 * Gets the distribution @p t parameter.
1980 * Gets the distribution @p p parameter.
1990 template<class _UniformRandomNumberGenerator
>
1992 operator()(_UniformRandomNumberGenerator
& __urng
);
1995 * Inserts a %binomial_distribution random number distribution
1996 * @p __x into the output stream @p __os.
1998 * @param __os An output stream.
1999 * @param __x A %binomial_distribution random number distribution.
2001 * @returns The output stream with the state of @p __x inserted or in
2004 template<typename _IntType1
, typename _RealType1
,
2005 typename _CharT
, typename _Traits
>
2006 friend std::basic_ostream
<_CharT
, _Traits
>&
2007 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2008 const binomial_distribution
<_IntType1
, _RealType1
>& __x
);
2011 * Extracts a %binomial_distribution random number distribution
2012 * @p __x from the input stream @p __is.
2014 * @param __is An input stream.
2015 * @param __x A %binomial_distribution random number generator engine.
2017 * @returns The input stream with @p __x extracted or in an error state.
2019 template<typename _IntType1
, typename _RealType1
,
2020 typename _CharT
, typename _Traits
>
2021 friend std::basic_istream
<_CharT
, _Traits
>&
2022 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2023 binomial_distribution
<_IntType1
, _RealType1
>& __x
);
2029 template<class _UniformRandomNumberGenerator
>
2031 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
2033 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2034 normal_distribution
<_RealType
> _M_nd
;
2037 #if _GLIBCXX_USE_C99_MATH_TR1
2038 _RealType _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
2039 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
2047 /* @} */ // group tr1_random_distributions_discrete
2050 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2051 * @ingroup tr1_random_distributions
2056 * @brief Uniform continuous distribution for random numbers.
2058 * A continuous random distribution on the range [min, max) with equal
2059 * probability throughout the range. The URNG should be real-valued and
2060 * deliver number in the range [0, 1).
2062 template<typename _RealType
= double>
2067 typedef _RealType input_type
;
2068 typedef _RealType result_type
;
2072 * Constructs a uniform_real object.
2074 * @param __min [IN] The lower bound of the distribution.
2075 * @param __max [IN] The upper bound of the distribution.
2078 uniform_real(_RealType __min
= _RealType(0),
2079 _RealType __max
= _RealType(1))
2080 : _M_min(__min
), _M_max(__max
)
2082 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
2096 template<class _UniformRandomNumberGenerator
>
2098 operator()(_UniformRandomNumberGenerator
& __urng
)
2099 { return (__urng() * (_M_max
- _M_min
)) + _M_min
; }
2102 * Inserts a %uniform_real random number distribution @p __x into the
2103 * output stream @p __os.
2105 * @param __os An output stream.
2106 * @param __x A %uniform_real random number distribution.
2108 * @returns The output stream with the state of @p __x inserted or in
2111 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2112 friend std::basic_ostream
<_CharT
, _Traits
>&
2113 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2114 const uniform_real
<_RealType1
>& __x
);
2117 * Extracts a %uniform_real random number distribution
2118 * @p __x from the input stream @p __is.
2120 * @param __is An input stream.
2121 * @param __x A %uniform_real random number generator engine.
2123 * @returns The input stream with @p __x extracted or in an error state.
2125 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2126 friend std::basic_istream
<_CharT
, _Traits
>&
2127 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2128 uniform_real
<_RealType1
>& __x
);
2137 * @brief An exponential continuous distribution for random numbers.
2139 * The formula for the exponential probability mass function is
2140 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2142 * <table border=1 cellpadding=10 cellspacing=0>
2143 * <caption align=top>Distribution Statistics</caption>
2144 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2145 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2146 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2147 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2148 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2151 template<typename _RealType
= double>
2152 class exponential_distribution
2156 typedef _RealType input_type
;
2157 typedef _RealType result_type
;
2161 * Constructs an exponential distribution with inverse scale parameter
2165 exponential_distribution(const result_type
& __lambda
= result_type(1))
2166 : _M_lambda(__lambda
)
2168 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> 0);
2172 * Gets the inverse scale parameter of the distribution.
2176 { return _M_lambda
; }
2179 * Resets the distribution.
2181 * Has no effect on exponential distributions.
2186 template<class _UniformRandomNumberGenerator
>
2188 operator()(_UniformRandomNumberGenerator
& __urng
)
2189 { return -std::log(__urng()) / _M_lambda
; }
2192 * Inserts a %exponential_distribution random number distribution
2193 * @p __x into the output stream @p __os.
2195 * @param __os An output stream.
2196 * @param __x A %exponential_distribution random number distribution.
2198 * @returns The output stream with the state of @p __x inserted or in
2201 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2202 friend std::basic_ostream
<_CharT
, _Traits
>&
2203 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2204 const exponential_distribution
<_RealType1
>& __x
);
2207 * Extracts a %exponential_distribution random number distribution
2208 * @p __x from the input stream @p __is.
2210 * @param __is An input stream.
2211 * @param __x A %exponential_distribution random number
2214 * @returns The input stream with @p __x extracted or in an error state.
2216 template<typename _CharT
, typename _Traits
>
2217 friend std::basic_istream
<_CharT
, _Traits
>&
2218 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2219 exponential_distribution
& __x
)
2220 { return __is
>> __x
._M_lambda
; }
2223 result_type _M_lambda
;
2228 * @brief A normal continuous distribution for random numbers.
2230 * The formula for the normal probability mass function is
2231 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2232 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2234 template<typename _RealType
= double>
2235 class normal_distribution
2239 typedef _RealType input_type
;
2240 typedef _RealType result_type
;
2244 * Constructs a normal distribution with parameters @f$ mean @f$ and
2248 normal_distribution(const result_type
& __mean
= result_type(0),
2249 const result_type
& __sigma
= result_type(1))
2250 : _M_mean(__mean
), _M_sigma(__sigma
), _M_saved_available(false)
2252 _GLIBCXX_DEBUG_ASSERT(_M_sigma
> 0);
2256 * Gets the mean of the distribution.
2263 * Gets the @f$ \sigma @f$ of the distribution.
2267 { return _M_sigma
; }
2270 * Resets the distribution.
2274 { _M_saved_available
= false; }
2276 template<class _UniformRandomNumberGenerator
>
2278 operator()(_UniformRandomNumberGenerator
& __urng
);
2281 * Inserts a %normal_distribution random number distribution
2282 * @p __x into the output stream @p __os.
2284 * @param __os An output stream.
2285 * @param __x A %normal_distribution random number distribution.
2287 * @returns The output stream with the state of @p __x inserted or in
2290 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2291 friend std::basic_ostream
<_CharT
, _Traits
>&
2292 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2293 const normal_distribution
<_RealType1
>& __x
);
2296 * Extracts a %normal_distribution random number distribution
2297 * @p __x from the input stream @p __is.
2299 * @param __is An input stream.
2300 * @param __x A %normal_distribution random number generator engine.
2302 * @returns The input stream with @p __x extracted or in an error state.
2304 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2305 friend std::basic_istream
<_CharT
, _Traits
>&
2306 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2307 normal_distribution
<_RealType1
>& __x
);
2310 result_type _M_mean
;
2311 result_type _M_sigma
;
2312 result_type _M_saved
;
2313 bool _M_saved_available
;
2318 * @brief A gamma continuous distribution for random numbers.
2320 * The formula for the gamma probability mass function is
2321 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2323 template<typename _RealType
= double>
2324 class gamma_distribution
2328 typedef _RealType input_type
;
2329 typedef _RealType result_type
;
2333 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2336 gamma_distribution(const result_type
& __alpha_val
= result_type(1))
2337 : _M_alpha(__alpha_val
)
2339 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> 0);
2344 * Gets the @f$ \alpha @f$ of the distribution.
2348 { return _M_alpha
; }
2351 * Resets the distribution.
2356 template<class _UniformRandomNumberGenerator
>
2358 operator()(_UniformRandomNumberGenerator
& __urng
);
2361 * Inserts a %gamma_distribution random number distribution
2362 * @p __x into the output stream @p __os.
2364 * @param __os An output stream.
2365 * @param __x A %gamma_distribution random number distribution.
2367 * @returns The output stream with the state of @p __x inserted or in
2370 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2371 friend std::basic_ostream
<_CharT
, _Traits
>&
2372 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2373 const gamma_distribution
<_RealType1
>& __x
);
2376 * Extracts a %gamma_distribution random number distribution
2377 * @p __x from the input stream @p __is.
2379 * @param __is An input stream.
2380 * @param __x A %gamma_distribution random number generator engine.
2382 * @returns The input stream with @p __x extracted or in an error state.
2384 template<typename _CharT
, typename _Traits
>
2385 friend std::basic_istream
<_CharT
, _Traits
>&
2386 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2387 gamma_distribution
& __x
)
2389 __is
>> __x
._M_alpha
;
2390 __x
._M_initialize();
2398 result_type _M_alpha
;
2400 // Hosts either lambda of GB or d of modified Vaduva's.
2404 /* @} */ // group tr1_random_distributions_continuous
2405 /* @} */ // group tr1_random_distributions
2406 /* @} */ // group tr1_random
2410 #endif // _GLIBCXX_TR1_RANDOM_H