1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2025 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 #ifdef _GLIBCXX_SYSHDR
35 #pragma GCC system_header
38 namespace std
_GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 // [5.1] Random number generation
47 * @addtogroup tr1_random Random Number Generation
48 * A facility for generating random numbers on selected distributions.
53 * Implementation-space details.
57 template<typename _UIntType
, int __w
,
58 bool = __w
< std::numeric_limits
<_UIntType
>::digits
>
60 { static const _UIntType __value
= 0; };
62 template<typename _UIntType
, int __w
>
63 struct _Shift
<_UIntType
, __w
, true>
64 { static const _UIntType __value
= _UIntType(1) << __w
; };
66 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
, bool>
69 // Dispatch based on modulus value to prevent divide-by-zero compile-time
70 // errors when m == 0.
71 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
>
74 { return _Mod
<_Tp
, __a
, __c
, __m
, __m
== 0>::__calc(__x
); }
76 typedef __gnu_cxx::__conditional_type
<(sizeof(unsigned) == 4),
77 unsigned, unsigned long>::__type _UInt32Type
;
80 * An adaptor class for converting the output of any Generator into
81 * the input for a specific Distribution.
83 template<typename _Engine
, typename _Distribution
>
86 typedef typename
_Engine::result_type _Engine_result_type
;
87 typedef typename
_Distribution::input_type result_type
;
90 _Adaptor(const _Engine
& __g
)
96 result_type __return_value
;
97 if (is_integral
<_Engine_result_type
>::value
98 && is_integral
<result_type
>::value
)
99 __return_value
= _M_g
.min();
101 __return_value
= result_type(0);
102 return __return_value
;
108 result_type __return_value
;
109 if (is_integral
<_Engine_result_type
>::value
110 && is_integral
<result_type
>::value
)
111 __return_value
= _M_g
.max();
112 else if (!is_integral
<result_type
>::value
)
113 __return_value
= result_type(1);
115 __return_value
= std::numeric_limits
<result_type
>::max() - 1;
116 return __return_value
;
120 * Converts a value generated by the adapted random number generator
121 * into a value in the input domain for the dependent random number
124 * Because the type traits are compile time constants only the
125 * appropriate clause of the if statements will actually be emitted
131 result_type __return_value
;
132 if (is_integral
<_Engine_result_type
>::value
133 && is_integral
<result_type
>::value
)
134 __return_value
= _M_g();
135 else if (!is_integral
<_Engine_result_type
>::value
136 && !is_integral
<result_type
>::value
)
137 __return_value
= result_type(_M_g() - _M_g
.min())
138 / result_type(_M_g
.max() - _M_g
.min());
139 else if (is_integral
<_Engine_result_type
>::value
140 && !is_integral
<result_type
>::value
)
141 __return_value
= result_type(_M_g() - _M_g
.min())
142 / result_type(_M_g
.max() - _M_g
.min() + result_type(1));
144 __return_value
= (((_M_g() - _M_g
.min())
145 / (_M_g
.max() - _M_g
.min()))
146 * std::numeric_limits
<result_type
>::max());
147 return __return_value
;
152 } // namespace __detail
155 * Produces random numbers on a given distribution function using a
156 * non-uniform random number generation engine.
158 * @todo the engine_value_type needs to be studied more carefully.
160 template<typename _Engine
, typename _Dist
>
161 class variate_generator
163 template<typename _Eng
>
169 _S_ref(const _Eng
& __e
) { return __e
; }
172 template<typename _Eng
>
177 __attribute__((__nonnull__
))
179 _S_ref(const _Eng
* __e
) { return *__e
; }
182 template<typename _Eng
>
188 _S_ref(const _Eng
& __e
) { return __e
; }
192 typedef _Engine engine_type
;
193 typedef typename _Value
<_Engine
>::type engine_value_type
;
194 typedef _Dist distribution_type
;
195 typedef typename
_Dist::result_type result_type
;
197 // Concept requirements.
198 __glibcxx_class_requires(engine_value_type
, _CopyConstructibleConcept
)
199 // __glibcxx_class_requires(_Engine, _EngineConcept)
200 // __glibcxx_class_requires(_Dist, _EngineConcept)
202 // tr1:5.1.1 table 5.1 requirement
203 typedef typename
__gnu_cxx::__enable_if
<
204 is_arithmetic
<result_type
>::value
, result_type
>::__type _IsValidType
;
207 * Constructs a variate generator with the uniform random number
208 * generator @p __eng for the random distribution @p __dist.
210 * @throws Any exceptions which may thrown by the copy constructors of
211 * the @p _Engine or @p _Dist objects.
213 variate_generator(engine_type __eng
, distribution_type __dist
)
214 : _M_engine(_Value
<_Engine
>::_S_ref(__eng
)), _M_dist(__dist
) { }
217 * Gets the next generated value on the distribution.
221 { return _M_dist(_M_engine
); }
226 template<typename _Tp
>
228 operator()(_Tp __value
)
229 { return _M_dist(_M_engine
, __value
); }
232 * Gets a reference to the underlying uniform random number generator
237 { return _M_engine
._M_g
; }
240 * Gets a const reference to the underlying uniform random number
243 const engine_value_type
&
245 { return _M_engine
._M_g
; }
248 * Gets a reference to the underlying random distribution.
255 * Gets a const reference to the underlying random distribution.
257 const distribution_type
&
262 * Gets the closed lower bound of the distribution interval.
266 { return this->distribution().min(); }
269 * Gets the closed upper bound of the distribution interval.
273 { return this->distribution().max(); }
276 __detail::_Adaptor
<engine_value_type
, _Dist
> _M_engine
;
277 distribution_type _M_dist
;
282 * @addtogroup tr1_random_generators Random Number Generators
283 * @ingroup tr1_random
285 * These classes define objects which provide random or pseudorandom
286 * numbers, either from a discrete or a continuous interval. The
287 * random number generator supplied as a part of this library are
288 * all uniform random number generators which provide a sequence of
289 * random number uniformly distributed over their range.
291 * A number generator is a function object with an operator() that
292 * takes zero arguments and returns a number.
294 * A compliant random number generator must satisfy the following
295 * requirements. <table border=1 cellpadding=10 cellspacing=0>
296 * <caption align=top>Random Number Generator Requirements</caption>
297 * <tr><td>To be documented.</td></tr> </table>
303 * @brief A model of a linear congruential random number generator.
305 * A random number generator that produces pseudorandom numbers using the
306 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
308 * The template parameter @p _UIntType must be an unsigned integral type
309 * large enough to store values up to (__m-1). If the template parameter
310 * @p __m is 0, the modulus @p __m used is
311 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
312 * parameters @p __a and @p __c must be less than @p __m.
314 * The size of the state is @f$ 1 @f$.
316 template<class _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
317 class linear_congruential
319 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
320 // __glibcpp_class_requires(__a < __m && __c < __m)
323 /** The type of the generated random value. */
324 typedef _UIntType result_type
;
326 /** The multiplier. */
327 static const _UIntType multiplier
= __a
;
329 static const _UIntType increment
= __c
;
331 static const _UIntType modulus
= __m
;
334 * Constructs a %linear_congruential random number generator engine with
335 * seed @p __s. The default seed value is 1.
337 * @param __s The initial seed value.
340 linear_congruential(unsigned long __x0
= 1)
341 { this->seed(__x0
); }
344 * Constructs a %linear_congruential random number generator engine
345 * seeded from the generator function @p __g.
347 * @param __g The seed generator function.
350 linear_congruential(_Gen
& __g
)
354 * Reseeds the %linear_congruential random number generator engine
355 * sequence to the seed @g __s.
357 * @param __s The new seed.
360 seed(unsigned long __s
= 1);
363 * Reseeds the %linear_congruential random number generator engine
364 * sequence using values from the generator function @p __g.
366 * @param __g the seed generator function.
371 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
374 * Gets the smallest possible value in the output range.
376 * The minimum depends on the @p __c parameter: if it is zero, the
377 * minimum generated must be > 0, otherwise 0 is allowed.
381 { return (__detail::__mod
<_UIntType
, 1, 0, __m
>(__c
) == 0) ? 1 : 0; }
384 * Gets the largest possible value in the output range.
391 * Gets the next random number in the sequence.
397 * Compares two linear congruential random number generator
398 * objects of the same type for equality.
400 * @param __lhs A linear congruential random number generator object.
401 * @param __rhs Another linear congruential random number generator obj.
403 * @returns true if the two objects are equal, false otherwise.
406 operator==(const linear_congruential
& __lhs
,
407 const linear_congruential
& __rhs
)
408 { return __lhs
._M_x
== __rhs
._M_x
; }
411 * Compares two linear congruential random number generator
412 * objects of the same type for inequality.
414 * @param __lhs A linear congruential random number generator object.
415 * @param __rhs Another linear congruential random number generator obj.
417 * @returns true if the two objects are not equal, false otherwise.
420 operator!=(const linear_congruential
& __lhs
,
421 const linear_congruential
& __rhs
)
422 { return !(__lhs
== __rhs
); }
425 * Writes the textual representation of the state x(i) of x to @p __os.
427 * @param __os The output stream.
428 * @param __lcr A % linear_congruential random number generator.
431 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
433 typename _CharT
, typename _Traits
>
434 friend std::basic_ostream
<_CharT
, _Traits
>&
435 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
436 const linear_congruential
<_UIntType1
, __a1
, __c1
,
440 * Sets the state of the engine by reading its textual
441 * representation from @p __is.
443 * The textual representation must have been previously written using an
444 * output stream whose imbued locale and whose type's template
445 * specialization arguments _CharT and _Traits were the same as those of
448 * @param __is The input stream.
449 * @param __lcr A % linear_congruential random number generator.
452 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
454 typename _CharT
, typename _Traits
>
455 friend std::basic_istream
<_CharT
, _Traits
>&
456 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
457 linear_congruential
<_UIntType1
, __a1
, __c1
, __m1
>& __lcr
);
462 seed(_Gen
& __g
, true_type
)
463 { return seed(static_cast<unsigned long>(__g
)); }
467 seed(_Gen
& __g
, false_type
);
473 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
475 typedef linear_congruential
<unsigned long, 16807, 0, 2147483647> minstd_rand0
;
478 * An alternative LCR (Lehmer Generator function) .
480 typedef linear_congruential
<unsigned long, 48271, 0, 2147483647> minstd_rand
;
484 * A generalized feedback shift register discrete random number generator.
486 * This algorithm avoids multiplication and division and is designed to be
487 * friendly to a pipelined architecture. If the parameters are chosen
488 * correctly, this generator will produce numbers with a very long period and
489 * fairly good apparent entropy, although still not cryptographically strong.
491 * The best way to use this generator is with the predefined mt19937 class.
493 * This algorithm was originally invented by Makoto Matsumoto and
496 * @var word_size The number of bits in each element of the state vector.
497 * @var state_size The degree of recursion.
498 * @var shift_size The period parameter.
499 * @var mask_bits The separation point bit index.
500 * @var parameter_a The last row of the twist matrix.
501 * @var output_u The first right-shift tempering matrix parameter.
502 * @var output_s The first left-shift tempering matrix parameter.
503 * @var output_b The first left-shift tempering matrix mask.
504 * @var output_t The second left-shift tempering matrix parameter.
505 * @var output_c The second left-shift tempering matrix mask.
506 * @var output_l The second right-shift tempering matrix parameter.
508 template<class _UIntType
, int __w
, int __n
, int __m
, int __r
,
509 _UIntType __a
, int __u
, int __s
, _UIntType __b
, int __t
,
510 _UIntType __c
, int __l
>
511 class mersenne_twister
513 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
517 typedef _UIntType result_type
;
520 static const int word_size
= __w
;
521 static const int state_size
= __n
;
522 static const int shift_size
= __m
;
523 static const int mask_bits
= __r
;
524 static const _UIntType parameter_a
= __a
;
525 static const int output_u
= __u
;
526 static const int output_s
= __s
;
527 static const _UIntType output_b
= __b
;
528 static const int output_t
= __t
;
529 static const _UIntType output_c
= __c
;
530 static const int output_l
= __l
;
532 // constructors and member function
537 mersenne_twister(unsigned long __value
)
541 mersenne_twister(_Gen
& __g
)
549 seed(unsigned long __value
);
554 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
562 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
568 * Compares two % mersenne_twister random number generator objects of
569 * the same type for equality.
571 * @param __lhs A % mersenne_twister random number generator object.
572 * @param __rhs Another % mersenne_twister random number generator
575 * @returns true if the two objects are equal, false otherwise.
578 operator==(const mersenne_twister
& __lhs
,
579 const mersenne_twister
& __rhs
)
580 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
583 * Compares two % mersenne_twister random number generator objects of
584 * the same type for inequality.
586 * @param __lhs A % mersenne_twister random number generator object.
587 * @param __rhs Another % mersenne_twister random number generator
590 * @returns true if the two objects are not equal, false otherwise.
593 operator!=(const mersenne_twister
& __lhs
,
594 const mersenne_twister
& __rhs
)
595 { return !(__lhs
== __rhs
); }
598 * Inserts the current state of a % mersenne_twister random number
599 * generator engine @p __x into the output stream @p __os.
601 * @param __os An output stream.
602 * @param __x A % mersenne_twister random number generator engine.
604 * @returns The output stream with the state of @p __x inserted or in
607 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
608 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
609 _UIntType1 __c1
, int __l1
,
610 typename _CharT
, typename _Traits
>
611 friend std::basic_ostream
<_CharT
, _Traits
>&
612 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
613 const mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
614 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
617 * Extracts the current state of a % mersenne_twister random number
618 * generator engine @p __x from the input stream @p __is.
620 * @param __is An input stream.
621 * @param __x A % mersenne_twister random number generator engine.
623 * @returns The input stream with the state of @p __x extracted or in
626 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
627 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
628 _UIntType1 __c1
, int __l1
,
629 typename _CharT
, typename _Traits
>
630 friend std::basic_istream
<_CharT
, _Traits
>&
631 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
632 mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
633 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
638 seed(_Gen
& __g
, true_type
)
639 { return seed(static_cast<unsigned long>(__g
)); }
643 seed(_Gen
& __g
, false_type
);
645 _UIntType _M_x
[state_size
];
650 * The classic Mersenne Twister.
653 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
654 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
655 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
657 typedef mersenne_twister
<
658 unsigned long, 32, 624, 397, 31,
666 * @brief The Marsaglia-Zaman generator.
668 * This is a model of a Generalized Fibonacci discrete random number
669 * generator, sometimes referred to as the SWC generator.
671 * A discrete random number generator that produces pseudorandom
672 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
673 * carry_{i-1}) \bmod m @f$.
675 * The size of the state is @f$ r @f$
676 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
678 * N1688[4.13] says <em>the template parameter _IntType shall denote
679 * an integral type large enough to store values up to m</em>.
681 * @var _M_x The state of the generator. This is a ring buffer.
682 * @var _M_carry The carry.
683 * @var _M_p Current index of x(i - r).
685 template<typename _IntType
, _IntType __m
, int __s
, int __r
>
686 class subtract_with_carry
688 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
691 /** The type of the generated random value. */
692 typedef _IntType result_type
;
695 static const _IntType modulus
= __m
;
696 static const int long_lag
= __r
;
697 static const int short_lag
= __s
;
700 * Constructs a default-initialized % subtract_with_carry random number
703 subtract_with_carry()
707 * Constructs an explicitly seeded % subtract_with_carry random number
711 subtract_with_carry(unsigned long __value
)
712 { this->seed(__value
); }
715 * Constructs a %subtract_with_carry random number generator engine
716 * seeded from the generator function @p __g.
718 * @param __g The seed generator function.
721 subtract_with_carry(_Gen
& __g
)
725 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
727 * N1688[4.19] modifies this as follows. If @p __value == 0,
728 * sets value to 19780503. In any case, with a linear
729 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
730 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
731 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
732 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
733 * set carry to 1, otherwise sets carry to 0.
736 seed(unsigned long __value
= 19780503);
739 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
740 * random number generator.
745 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
748 * Gets the inclusive minimum value of the range of random integers
749 * returned by this generator.
756 * Gets the inclusive maximum value of the range of random integers
757 * returned by this generator.
761 { return this->modulus
- 1; }
764 * Gets the next random number in the sequence.
770 * Compares two % subtract_with_carry random number generator objects of
771 * the same type for equality.
773 * @param __lhs A % subtract_with_carry random number generator object.
774 * @param __rhs Another % subtract_with_carry random number generator
777 * @returns true if the two objects are equal, false otherwise.
780 operator==(const subtract_with_carry
& __lhs
,
781 const subtract_with_carry
& __rhs
)
782 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
785 * Compares two % subtract_with_carry random number generator objects of
786 * the same type for inequality.
788 * @param __lhs A % subtract_with_carry random number generator object.
789 * @param __rhs Another % subtract_with_carry random number generator
792 * @returns true if the two objects are not equal, false otherwise.
795 operator!=(const subtract_with_carry
& __lhs
,
796 const subtract_with_carry
& __rhs
)
797 { return !(__lhs
== __rhs
); }
800 * Inserts the current state of a % subtract_with_carry random number
801 * generator engine @p __x into the output stream @p __os.
803 * @param __os An output stream.
804 * @param __x A % subtract_with_carry random number generator engine.
806 * @returns The output stream with the state of @p __x inserted or in
809 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
810 typename _CharT
, typename _Traits
>
811 friend std::basic_ostream
<_CharT
, _Traits
>&
812 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
813 const subtract_with_carry
<_IntType1
, __m1
, __s1
,
817 * Extracts the current state of a % subtract_with_carry random number
818 * generator engine @p __x from the input stream @p __is.
820 * @param __is An input stream.
821 * @param __x A % subtract_with_carry random number generator engine.
823 * @returns The input stream with the state of @p __x extracted or in
826 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
827 typename _CharT
, typename _Traits
>
828 friend std::basic_istream
<_CharT
, _Traits
>&
829 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
830 subtract_with_carry
<_IntType1
, __m1
, __s1
, __r1
>& __x
);
835 seed(_Gen
& __g
, true_type
)
836 { return seed(static_cast<unsigned long>(__g
)); }
840 seed(_Gen
& __g
, false_type
);
842 typedef typename
__gnu_cxx::__add_unsigned
<_IntType
>::__type _UIntType
;
844 _UIntType _M_x
[long_lag
];
851 * @brief The Marsaglia-Zaman generator (floats version).
853 * @var _M_x The state of the generator. This is a ring buffer.
854 * @var _M_carry The carry.
855 * @var _M_p Current index of x(i - r).
856 * @var _M_npows Precomputed negative powers of 2.
858 template<typename _RealType
, int __w
, int __s
, int __r
>
859 class subtract_with_carry_01
862 /** The type of the generated random value. */
863 typedef _RealType result_type
;
866 static const int word_size
= __w
;
867 static const int long_lag
= __r
;
868 static const int short_lag
= __s
;
871 * Constructs a default-initialized % subtract_with_carry_01 random
874 subtract_with_carry_01()
877 _M_initialize_npows();
881 * Constructs an explicitly seeded % subtract_with_carry_01 random number
885 subtract_with_carry_01(unsigned long __value
)
888 _M_initialize_npows();
892 * Constructs a % subtract_with_carry_01 random number generator engine
893 * seeded from the generator function @p __g.
895 * @param __g The seed generator function.
898 subtract_with_carry_01(_Gen
& __g
)
901 _M_initialize_npows();
905 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
908 seed(unsigned long __value
= 19780503);
911 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
912 * random number generator.
917 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
920 * Gets the minimum value of the range of random floats
921 * returned by this generator.
928 * Gets the maximum value of the range of random floats
929 * returned by this generator.
936 * Gets the next random number in the sequence.
942 * Compares two % subtract_with_carry_01 random number generator objects
943 * of the same type for equality.
945 * @param __lhs A % subtract_with_carry_01 random number
947 * @param __rhs Another % subtract_with_carry_01 random number generator
950 * @returns true if the two objects are equal, false otherwise.
953 operator==(const subtract_with_carry_01
& __lhs
,
954 const subtract_with_carry_01
& __rhs
)
956 for (int __i
= 0; __i
< long_lag
; ++__i
)
957 if (!std::equal(__lhs
._M_x
[__i
], __lhs
._M_x
[__i
] + __n
,
964 * Compares two % subtract_with_carry_01 random number generator objects
965 * of the same type for inequality.
967 * @param __lhs A % subtract_with_carry_01 random number
970 * @param __rhs Another % subtract_with_carry_01 random number generator
973 * @returns true if the two objects are not equal, false otherwise.
976 operator!=(const subtract_with_carry_01
& __lhs
,
977 const subtract_with_carry_01
& __rhs
)
978 { return !(__lhs
== __rhs
); }
981 * Inserts the current state of a % subtract_with_carry_01 random number
982 * generator engine @p __x into the output stream @p __os.
984 * @param __os An output stream.
985 * @param __x A % subtract_with_carry_01 random number generator engine.
987 * @returns The output stream with the state of @p __x inserted or in
990 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
991 typename _CharT
, typename _Traits
>
992 friend std::basic_ostream
<_CharT
, _Traits
>&
993 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
994 const subtract_with_carry_01
<_RealType1
, __w1
, __s1
,
998 * Extracts the current state of a % subtract_with_carry_01 random number
999 * generator engine @p __x from the input stream @p __is.
1001 * @param __is An input stream.
1002 * @param __x A % subtract_with_carry_01 random number generator engine.
1004 * @returns The input stream with the state of @p __x extracted or in
1007 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
1008 typename _CharT
, typename _Traits
>
1009 friend std::basic_istream
<_CharT
, _Traits
>&
1010 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1011 subtract_with_carry_01
<_RealType1
, __w1
, __s1
, __r1
>& __x
);
1014 template<class _Gen
>
1016 seed(_Gen
& __g
, true_type
)
1017 { return seed(static_cast<unsigned long>(__g
)); }
1019 template<class _Gen
>
1021 seed(_Gen
& __g
, false_type
);
1024 _M_initialize_npows();
1026 static const int __n
= (__w
+ 31) / 32;
1028 typedef __detail::_UInt32Type _UInt32Type
;
1029 _UInt32Type _M_x
[long_lag
][__n
];
1030 _RealType _M_npows
[__n
];
1031 _UInt32Type _M_carry
;
1035 typedef subtract_with_carry_01
<float, 24, 10, 24> ranlux_base_01
;
1037 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1038 // 508. Bad parameters for ranlux64_base_01.
1039 typedef subtract_with_carry_01
<double, 48, 5, 12> ranlux64_base_01
;
1043 * Produces random numbers from some base engine by discarding blocks of
1046 * 0 <= @p __r <= @p __p
1048 template<class _UniformRandomNumberGenerator
, int __p
, int __r
>
1051 // __glibcxx_class_requires(typename base_type::result_type,
1052 // ArithmeticTypeConcept)
1055 /** The type of the underlying generator engine. */
1056 typedef _UniformRandomNumberGenerator base_type
;
1057 /** The type of the generated random value. */
1058 typedef typename
base_type::result_type result_type
;
1061 static const int block_size
= __p
;
1062 static const int used_block
= __r
;
1065 * Constructs a default %discard_block engine.
1067 * The underlying engine is default constructed as well.
1073 * Copy constructs a %discard_block engine.
1075 * Copies an existing base class random number generator.
1076 * @param rng An existing (base class) engine object.
1079 discard_block(const base_type
& __rng
)
1080 : _M_b(__rng
), _M_n(0) { }
1083 * Seed constructs a %discard_block engine.
1085 * Constructs the underlying generator engine seeded with @p __s.
1086 * @param __s A seed value for the base class engine.
1089 discard_block(unsigned long __s
)
1090 : _M_b(__s
), _M_n(0) { }
1093 * Generator construct a %discard_block engine.
1095 * @param __g A seed generator function.
1097 template<class _Gen
>
1098 discard_block(_Gen
& __g
)
1099 : _M_b(__g
), _M_n(0) { }
1102 * Reseeds the %discard_block object with the default seed for the
1103 * underlying base class generator engine.
1112 * Reseeds the %discard_block object with the given seed generator
1114 * @param __g A seed generator function.
1116 template<class _Gen
>
1117 void seed(_Gen
& __g
)
1124 * Gets a const reference to the underlying generator engine object.
1131 * Gets the minimum value in the generated random number range.
1135 { return _M_b
.min(); }
1138 * Gets the maximum value in the generated random number range.
1142 { return _M_b
.max(); }
1145 * Gets the next value in the generated random number sequence.
1151 * Compares two %discard_block random number generator objects of
1152 * the same type for equality.
1154 * @param __lhs A %discard_block random number generator object.
1155 * @param __rhs Another %discard_block random number generator
1158 * @returns true if the two objects are equal, false otherwise.
1161 operator==(const discard_block
& __lhs
, const discard_block
& __rhs
)
1162 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
1165 * Compares two %discard_block random number generator objects of
1166 * the same type for inequality.
1168 * @param __lhs A %discard_block random number generator object.
1169 * @param __rhs Another %discard_block random number generator
1172 * @returns true if the two objects are not equal, false otherwise.
1175 operator!=(const discard_block
& __lhs
, const discard_block
& __rhs
)
1176 { return !(__lhs
== __rhs
); }
1179 * Inserts the current state of a %discard_block random number
1180 * generator engine @p __x into the output stream @p __os.
1182 * @param __os An output stream.
1183 * @param __x A %discard_block random number generator engine.
1185 * @returns The output stream with the state of @p __x inserted or in
1188 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1189 typename _CharT
, typename _Traits
>
1190 friend std::basic_ostream
<_CharT
, _Traits
>&
1191 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1192 const discard_block
<_UniformRandomNumberGenerator1
,
1196 * Extracts the current state of a % subtract_with_carry random number
1197 * generator engine @p __x from the input stream @p __is.
1199 * @param __is An input stream.
1200 * @param __x A %discard_block random number generator engine.
1202 * @returns The input stream with the state of @p __x extracted or in
1205 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1206 typename _CharT
, typename _Traits
>
1207 friend std::basic_istream
<_CharT
, _Traits
>&
1208 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1209 discard_block
<_UniformRandomNumberGenerator1
,
1219 * James's luxury-level-3 integer adaptation of Luescher's generator.
1221 typedef discard_block
<
1222 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1228 * James's luxury-level-4 integer adaptation of Luescher's generator.
1230 typedef discard_block
<
1231 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1236 typedef discard_block
<
1237 subtract_with_carry_01
<float, 24, 10, 24>,
1242 typedef discard_block
<
1243 subtract_with_carry_01
<float, 24, 10, 24>,
1250 * A random number generator adaptor class that combines two random number
1251 * generator engines into a single output sequence.
1253 template<class _UniformRandomNumberGenerator1
, int __s1
,
1254 class _UniformRandomNumberGenerator2
, int __s2
>
1257 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1258 // result_type, ArithmeticTypeConcept)
1259 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1260 // result_type, ArithmeticTypeConcept)
1263 /** The type of the first underlying generator engine. */
1264 typedef _UniformRandomNumberGenerator1 base1_type
;
1265 /** The type of the second underlying generator engine. */
1266 typedef _UniformRandomNumberGenerator2 base2_type
;
1269 typedef typename
base1_type::result_type _Result_type1
;
1270 typedef typename
base2_type::result_type _Result_type2
;
1273 /** The type of the generated random value. */
1274 typedef typename
__gnu_cxx::__conditional_type
<(sizeof(_Result_type1
)
1275 > sizeof(_Result_type2
)),
1276 _Result_type1
, _Result_type2
>::__type result_type
;
1279 static const int shift1
= __s1
;
1280 static const int shift2
= __s2
;
1282 // constructors and member function
1285 { _M_initialize_max(); }
1287 xor_combine(const base1_type
& __rng1
, const base2_type
& __rng2
)
1288 : _M_b1(__rng1
), _M_b2(__rng2
)
1289 { _M_initialize_max(); }
1291 xor_combine(unsigned long __s
)
1292 : _M_b1(__s
), _M_b2(__s
+ 1)
1293 { _M_initialize_max(); }
1295 template<class _Gen
>
1296 xor_combine(_Gen
& __g
)
1297 : _M_b1(__g
), _M_b2(__g
)
1298 { _M_initialize_max(); }
1307 template<class _Gen
>
1332 * Gets the next random number in the sequence.
1334 // NB: Not exactly the TR1 formula, per N2079 instead.
1338 return ((result_type(_M_b1() - _M_b1
.min()) << shift1
)
1339 ^ (result_type(_M_b2() - _M_b2
.min()) << shift2
));
1343 * Compares two %xor_combine random number generator objects of
1344 * the same type for equality.
1346 * @param __lhs A %xor_combine random number generator object.
1347 * @param __rhs Another %xor_combine random number generator
1350 * @returns true if the two objects are equal, false otherwise.
1353 operator==(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1355 return (__lhs
.base1() == __rhs
.base1())
1356 && (__lhs
.base2() == __rhs
.base2());
1360 * Compares two %xor_combine random number generator objects of
1361 * the same type for inequality.
1363 * @param __lhs A %xor_combine random number generator object.
1364 * @param __rhs Another %xor_combine random number generator
1367 * @returns true if the two objects are not equal, false otherwise.
1370 operator!=(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1371 { return !(__lhs
== __rhs
); }
1374 * Inserts the current state of a %xor_combine random number
1375 * generator engine @p __x into the output stream @p __os.
1377 * @param __os An output stream.
1378 * @param __x A %xor_combine random number generator engine.
1380 * @returns The output stream with the state of @p __x inserted or in
1383 template<class _UniformRandomNumberGenerator11
, int __s11
,
1384 class _UniformRandomNumberGenerator21
, int __s21
,
1385 typename _CharT
, typename _Traits
>
1386 friend std::basic_ostream
<_CharT
, _Traits
>&
1387 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1388 const xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1389 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1392 * Extracts the current state of a %xor_combine random number
1393 * generator engine @p __x from the input stream @p __is.
1395 * @param __is An input stream.
1396 * @param __x A %xor_combine random number generator engine.
1398 * @returns The input stream with the state of @p __x extracted or in
1401 template<class _UniformRandomNumberGenerator11
, int __s11
,
1402 class _UniformRandomNumberGenerator21
, int __s21
,
1403 typename _CharT
, typename _Traits
>
1404 friend std::basic_istream
<_CharT
, _Traits
>&
1405 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1406 xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1407 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1411 _M_initialize_max();
1414 _M_initialize_max_aux(result_type
, result_type
, int);
1423 * A standard interface to a platform-specific non-deterministic
1424 * random number generator (if any are available).
1430 typedef unsigned int result_type
;
1432 // constructors, destructors and member functions
1434 #ifdef _GLIBCXX_USE_RANDOM_TR1
1437 random_device(const std::string
& __token
= "/dev/urandom")
1439 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1440 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1441 std::__throw_runtime_error(__N("random_device::"
1442 "random_device(const std::string&)"));
1446 { std::fclose(_M_file
); }
1451 random_device(const std::string
& __token
= "mt19937")
1452 : _M_mt(_M_strtoul(__token
)) { }
1455 static unsigned long
1456 _M_strtoul(const std::string
& __str
)
1458 unsigned long __ret
= 5489UL;
1459 if (__str
!= "mt19937")
1461 const char* __nptr
= __str
.c_str();
1463 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1464 if (*__nptr
== '\0' || *__endptr
!= '\0')
1465 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1466 "(const std::string&)"));
1477 { return std::numeric_limits
<result_type
>::min(); }
1481 { return std::numeric_limits
<result_type
>::max(); }
1490 #ifdef _GLIBCXX_USE_RANDOM_TR1
1492 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1501 random_device(const random_device
&);
1502 void operator=(const random_device
&);
1504 #ifdef _GLIBCXX_USE_RANDOM_TR1
1511 /// @} group tr1_random_generators
1514 * @addtogroup tr1_random_distributions Random Number Distributions
1515 * @ingroup tr1_random
1520 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1521 * @ingroup tr1_random_distributions
1526 * @brief Uniform discrete distribution for random numbers.
1527 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1528 * probability throughout the range.
1530 template<typename _IntType
= int>
1533 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
1536 /** The type of the parameters of the distribution. */
1537 typedef _IntType input_type
;
1538 /** The type of the range of the distribution. */
1539 typedef _IntType result_type
;
1543 * Constructs a uniform distribution object.
1546 uniform_int(_IntType __min
= 0, _IntType __max
= 9)
1547 : _M_min(__min
), _M_max(__max
)
1549 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
1553 * Gets the inclusive lower bound of the distribution range.
1560 * Gets the inclusive upper bound of the distribution range.
1567 * Resets the distribution state.
1569 * Does nothing for the uniform integer distribution.
1575 * Gets a uniformly distributed random number in the range
1578 template<typename _UniformRandomNumberGenerator
>
1580 operator()(_UniformRandomNumberGenerator
& __urng
)
1582 typedef typename
_UniformRandomNumberGenerator::result_type
1584 return _M_call(__urng
, _M_min
, _M_max
,
1585 typename is_integral
<_UResult_type
>::type());
1589 * Gets a uniform random number in the range @f$[0, n)@f$.
1591 * This function is aimed at use with std::random_shuffle.
1593 template<typename _UniformRandomNumberGenerator
>
1595 operator()(_UniformRandomNumberGenerator
& __urng
, result_type __n
)
1597 typedef typename
_UniformRandomNumberGenerator::result_type
1599 return _M_call(__urng
, 0, __n
- 1,
1600 typename is_integral
<_UResult_type
>::type());
1604 * Inserts a %uniform_int random number distribution @p __x into the
1605 * output stream @p os.
1607 * @param __os An output stream.
1608 * @param __x A %uniform_int random number distribution.
1610 * @returns The output stream with the state of @p __x inserted or in
1613 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1614 friend std::basic_ostream
<_CharT
, _Traits
>&
1615 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1616 const uniform_int
<_IntType1
>& __x
);
1619 * Extracts a %uniform_int random number distribution
1620 * @p __x from the input stream @p __is.
1622 * @param __is An input stream.
1623 * @param __x A %uniform_int random number generator engine.
1625 * @returns The input stream with @p __x extracted or in an error state.
1627 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1628 friend std::basic_istream
<_CharT
, _Traits
>&
1629 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1630 uniform_int
<_IntType1
>& __x
);
1633 template<typename _UniformRandomNumberGenerator
>
1635 _M_call(_UniformRandomNumberGenerator
& __urng
,
1636 result_type __min
, result_type __max
, true_type
);
1638 template<typename _UniformRandomNumberGenerator
>
1640 _M_call(_UniformRandomNumberGenerator
& __urng
,
1641 result_type __min
, result_type __max
, false_type
)
1643 return result_type((__urng() - __urng
.min())
1644 / (__urng
.max() - __urng
.min())
1645 * (__max
- __min
+ 1)) + __min
;
1654 * @brief A Bernoulli random number distribution.
1656 * Generates a sequence of true and false values with likelihood @f$ p @f$
1657 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1659 class bernoulli_distribution
1662 typedef int input_type
;
1663 typedef bool result_type
;
1667 * Constructs a Bernoulli distribution with likelihood @p p.
1669 * @param __p [IN] The likelihood of a true result being returned. Must
1670 * be in the interval @f$ [0, 1] @f$.
1673 bernoulli_distribution(double __p
= 0.5)
1676 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
1680 * Gets the @p p parameter of the distribution.
1687 * Resets the distribution state.
1689 * Does nothing for a Bernoulli distribution.
1695 * Gets the next value in the Bernoullian sequence.
1697 template<class _UniformRandomNumberGenerator
>
1699 operator()(_UniformRandomNumberGenerator
& __urng
)
1701 if ((__urng() - __urng
.min()) < _M_p
* (__urng
.max() - __urng
.min()))
1707 * Inserts a %bernoulli_distribution random number distribution
1708 * @p __x into the output stream @p __os.
1710 * @param __os An output stream.
1711 * @param __x A %bernoulli_distribution random number distribution.
1713 * @returns The output stream with the state of @p __x inserted or in
1716 template<typename _CharT
, typename _Traits
>
1717 friend std::basic_ostream
<_CharT
, _Traits
>&
1718 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1719 const bernoulli_distribution
& __x
);
1722 * Extracts a %bernoulli_distribution random number distribution
1723 * @p __x from the input stream @p __is.
1725 * @param __is An input stream.
1726 * @param __x A %bernoulli_distribution random number generator engine.
1728 * @returns The input stream with @p __x extracted or in an error state.
1730 template<typename _CharT
, typename _Traits
>
1731 friend std::basic_istream
<_CharT
, _Traits
>&
1732 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1733 bernoulli_distribution
& __x
)
1734 { return __is
>> __x
._M_p
; }
1742 * @brief A discrete geometric random number distribution.
1744 * The formula for the geometric probability mass function is
1745 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1748 template<typename _IntType
= int, typename _RealType
= double>
1749 class geometric_distribution
1753 typedef _RealType input_type
;
1754 typedef _IntType result_type
;
1756 // constructors and member function
1758 geometric_distribution(const _RealType
& __p
= _RealType(0.5))
1761 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
1766 * Gets the distribution parameter @p p.
1775 template<class _UniformRandomNumberGenerator
>
1777 operator()(_UniformRandomNumberGenerator
& __urng
);
1780 * Inserts a %geometric_distribution random number distribution
1781 * @p __x into the output stream @p __os.
1783 * @param __os An output stream.
1784 * @param __x A %geometric_distribution random number distribution.
1786 * @returns The output stream with the state of @p __x inserted or in
1789 template<typename _IntType1
, typename _RealType1
,
1790 typename _CharT
, typename _Traits
>
1791 friend std::basic_ostream
<_CharT
, _Traits
>&
1792 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1793 const geometric_distribution
<_IntType1
, _RealType1
>& __x
);
1796 * Extracts a %geometric_distribution random number distribution
1797 * @p __x from the input stream @p __is.
1799 * @param __is An input stream.
1800 * @param __x A %geometric_distribution random number generator engine.
1802 * @returns The input stream with @p __x extracted or in an error state.
1804 template<typename _CharT
, typename _Traits
>
1805 friend std::basic_istream
<_CharT
, _Traits
>&
1806 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1807 geometric_distribution
& __x
)
1810 __x
._M_initialize();
1817 { _M_log_p
= std::log(_M_p
); }
1824 template<typename _RealType
>
1825 class normal_distribution
;
1828 * @brief A discrete Poisson random number distribution.
1830 * The formula for the Poisson probability mass function is
1831 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1832 * parameter of the distribution.
1834 template<typename _IntType
= int, typename _RealType
= double>
1835 class poisson_distribution
1839 typedef _RealType input_type
;
1840 typedef _IntType result_type
;
1842 // constructors and member function
1844 poisson_distribution(const _RealType
& __mean
= _RealType(1))
1845 : _M_mean(__mean
), _M_nd()
1847 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
1852 * Gets the distribution parameter @p mean.
1862 template<class _UniformRandomNumberGenerator
>
1864 operator()(_UniformRandomNumberGenerator
& __urng
);
1867 * Inserts a %poisson_distribution random number distribution
1868 * @p __x into the output stream @p __os.
1870 * @param __os An output stream.
1871 * @param __x A %poisson_distribution random number distribution.
1873 * @returns The output stream with the state of @p __x inserted or in
1876 template<typename _IntType1
, typename _RealType1
,
1877 typename _CharT
, typename _Traits
>
1878 friend std::basic_ostream
<_CharT
, _Traits
>&
1879 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1880 const poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1883 * Extracts a %poisson_distribution random number distribution
1884 * @p __x from the input stream @p __is.
1886 * @param __is An input stream.
1887 * @param __x A %poisson_distribution random number generator engine.
1889 * @returns The input stream with @p __x extracted or in an error state.
1891 template<typename _IntType1
, typename _RealType1
,
1892 typename _CharT
, typename _Traits
>
1893 friend std::basic_istream
<_CharT
, _Traits
>&
1894 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1895 poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1901 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1902 normal_distribution
<_RealType
> _M_nd
;
1906 // Hosts either log(mean) or the threshold of the simple method.
1907 _RealType _M_lm_thr
;
1908 #if _GLIBCXX_USE_C99_MATH_TR1
1909 _RealType _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
1915 * @brief A discrete binomial random number distribution.
1917 * The formula for the binomial probability mass function is
1918 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1919 * and @f$ p @f$ are the parameters of the distribution.
1921 template<typename _IntType
= int, typename _RealType
= double>
1922 class binomial_distribution
1926 typedef _RealType input_type
;
1927 typedef _IntType result_type
;
1929 // constructors and member function
1931 binomial_distribution(_IntType __t
= 1,
1932 const _RealType
& __p
= _RealType(0.5))
1933 : _M_t(__t
), _M_p(__p
), _M_nd()
1935 _GLIBCXX_DEBUG_ASSERT((_M_t
>= 0) && (_M_p
>= 0.0) && (_M_p
<= 1.0));
1940 * Gets the distribution @p t parameter.
1947 * Gets the distribution @p p parameter.
1957 template<class _UniformRandomNumberGenerator
>
1959 operator()(_UniformRandomNumberGenerator
& __urng
);
1962 * Inserts a %binomial_distribution random number distribution
1963 * @p __x into the output stream @p __os.
1965 * @param __os An output stream.
1966 * @param __x A %binomial_distribution random number distribution.
1968 * @returns The output stream with the state of @p __x inserted or in
1971 template<typename _IntType1
, typename _RealType1
,
1972 typename _CharT
, typename _Traits
>
1973 friend std::basic_ostream
<_CharT
, _Traits
>&
1974 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1975 const binomial_distribution
<_IntType1
, _RealType1
>& __x
);
1978 * Extracts a %binomial_distribution random number distribution
1979 * @p __x from the input stream @p __is.
1981 * @param __is An input stream.
1982 * @param __x A %binomial_distribution random number generator engine.
1984 * @returns The input stream with @p __x extracted or in an error state.
1986 template<typename _IntType1
, typename _RealType1
,
1987 typename _CharT
, typename _Traits
>
1988 friend std::basic_istream
<_CharT
, _Traits
>&
1989 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1990 binomial_distribution
<_IntType1
, _RealType1
>& __x
);
1996 template<class _UniformRandomNumberGenerator
>
1998 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
2000 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2001 normal_distribution
<_RealType
> _M_nd
;
2004 #if _GLIBCXX_USE_C99_MATH_TR1
2005 _RealType _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
2006 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
2014 /// @} group tr1_random_distributions_discrete
2017 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2018 * @ingroup tr1_random_distributions
2023 * @brief Uniform continuous distribution for random numbers.
2025 * A continuous random distribution on the range [min, max) with equal
2026 * probability throughout the range. The URNG should be real-valued and
2027 * deliver number in the range [0, 1).
2029 template<typename _RealType
= double>
2034 typedef _RealType input_type
;
2035 typedef _RealType result_type
;
2039 * Constructs a uniform_real object.
2041 * @param __min [IN] The lower bound of the distribution.
2042 * @param __max [IN] The upper bound of the distribution.
2045 uniform_real(_RealType __min
= _RealType(0),
2046 _RealType __max
= _RealType(1))
2047 : _M_min(__min
), _M_max(__max
)
2049 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
2063 template<class _UniformRandomNumberGenerator
>
2065 operator()(_UniformRandomNumberGenerator
& __urng
)
2066 { return (__urng() * (_M_max
- _M_min
)) + _M_min
; }
2069 * Inserts a %uniform_real random number distribution @p __x into the
2070 * output stream @p __os.
2072 * @param __os An output stream.
2073 * @param __x A %uniform_real random number distribution.
2075 * @returns The output stream with the state of @p __x inserted or in
2078 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2079 friend std::basic_ostream
<_CharT
, _Traits
>&
2080 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2081 const uniform_real
<_RealType1
>& __x
);
2084 * Extracts a %uniform_real random number distribution
2085 * @p __x from the input stream @p __is.
2087 * @param __is An input stream.
2088 * @param __x A %uniform_real random number generator engine.
2090 * @returns The input stream with @p __x extracted or in an error state.
2092 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2093 friend std::basic_istream
<_CharT
, _Traits
>&
2094 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2095 uniform_real
<_RealType1
>& __x
);
2104 * @brief An exponential continuous distribution for random numbers.
2106 * The formula for the exponential probability mass function is
2107 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2109 * <table border=1 cellpadding=10 cellspacing=0>
2110 * <caption align=top>Distribution Statistics</caption>
2111 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2112 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2113 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2114 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2115 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2118 template<typename _RealType
= double>
2119 class exponential_distribution
2123 typedef _RealType input_type
;
2124 typedef _RealType result_type
;
2128 * Constructs an exponential distribution with inverse scale parameter
2132 exponential_distribution(const result_type
& __lambda
= result_type(1))
2133 : _M_lambda(__lambda
)
2135 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> 0);
2139 * Gets the inverse scale parameter of the distribution.
2143 { return _M_lambda
; }
2146 * Resets the distribution.
2148 * Has no effect on exponential distributions.
2153 template<class _UniformRandomNumberGenerator
>
2155 operator()(_UniformRandomNumberGenerator
& __urng
)
2156 { return -std::log(__urng()) / _M_lambda
; }
2159 * Inserts a %exponential_distribution random number distribution
2160 * @p __x into the output stream @p __os.
2162 * @param __os An output stream.
2163 * @param __x A %exponential_distribution random number distribution.
2165 * @returns The output stream with the state of @p __x inserted or in
2168 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2169 friend std::basic_ostream
<_CharT
, _Traits
>&
2170 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2171 const exponential_distribution
<_RealType1
>& __x
);
2174 * Extracts a %exponential_distribution random number distribution
2175 * @p __x from the input stream @p __is.
2177 * @param __is An input stream.
2178 * @param __x A %exponential_distribution random number
2181 * @returns The input stream with @p __x extracted or in an error state.
2183 template<typename _CharT
, typename _Traits
>
2184 friend std::basic_istream
<_CharT
, _Traits
>&
2185 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2186 exponential_distribution
& __x
)
2187 { return __is
>> __x
._M_lambda
; }
2190 result_type _M_lambda
;
2195 * @brief A normal continuous distribution for random numbers.
2197 * The formula for the normal probability mass function is
2198 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2199 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2201 template<typename _RealType
= double>
2202 class normal_distribution
2206 typedef _RealType input_type
;
2207 typedef _RealType result_type
;
2211 * Constructs a normal distribution with parameters @f$ mean @f$ and
2215 normal_distribution(const result_type
& __mean
= result_type(0),
2216 const result_type
& __sigma
= result_type(1))
2217 : _M_mean(__mean
), _M_sigma(__sigma
), _M_saved_available(false)
2219 _GLIBCXX_DEBUG_ASSERT(_M_sigma
> 0);
2223 * Gets the mean of the distribution.
2230 * Gets the @f$ \sigma @f$ of the distribution.
2234 { return _M_sigma
; }
2237 * Resets the distribution.
2241 { _M_saved_available
= false; }
2243 template<class _UniformRandomNumberGenerator
>
2245 operator()(_UniformRandomNumberGenerator
& __urng
);
2248 * Inserts a %normal_distribution random number distribution
2249 * @p __x into the output stream @p __os.
2251 * @param __os An output stream.
2252 * @param __x A %normal_distribution random number distribution.
2254 * @returns The output stream with the state of @p __x inserted or in
2257 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2258 friend std::basic_ostream
<_CharT
, _Traits
>&
2259 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2260 const normal_distribution
<_RealType1
>& __x
);
2263 * Extracts a %normal_distribution random number distribution
2264 * @p __x from the input stream @p __is.
2266 * @param __is An input stream.
2267 * @param __x A %normal_distribution random number generator engine.
2269 * @returns The input stream with @p __x extracted or in an error state.
2271 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2272 friend std::basic_istream
<_CharT
, _Traits
>&
2273 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2274 normal_distribution
<_RealType1
>& __x
);
2277 result_type _M_mean
;
2278 result_type _M_sigma
;
2279 result_type _M_saved
;
2280 bool _M_saved_available
;
2285 * @brief A gamma continuous distribution for random numbers.
2287 * The formula for the gamma probability mass function is
2288 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2290 template<typename _RealType
= double>
2291 class gamma_distribution
2295 typedef _RealType input_type
;
2296 typedef _RealType result_type
;
2300 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2303 gamma_distribution(const result_type
& __alpha_val
= result_type(1))
2304 : _M_alpha(__alpha_val
)
2306 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> 0);
2311 * Gets the @f$ \alpha @f$ of the distribution.
2315 { return _M_alpha
; }
2318 * Resets the distribution.
2323 template<class _UniformRandomNumberGenerator
>
2325 operator()(_UniformRandomNumberGenerator
& __urng
);
2328 * Inserts a %gamma_distribution random number distribution
2329 * @p __x into the output stream @p __os.
2331 * @param __os An output stream.
2332 * @param __x A %gamma_distribution random number distribution.
2334 * @returns The output stream with the state of @p __x inserted or in
2337 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2338 friend std::basic_ostream
<_CharT
, _Traits
>&
2339 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2340 const gamma_distribution
<_RealType1
>& __x
);
2343 * Extracts a %gamma_distribution random number distribution
2344 * @p __x from the input stream @p __is.
2346 * @param __is An input stream.
2347 * @param __x A %gamma_distribution random number generator engine.
2349 * @returns The input stream with @p __x extracted or in an error state.
2351 template<typename _CharT
, typename _Traits
>
2352 friend std::basic_istream
<_CharT
, _Traits
>&
2353 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2354 gamma_distribution
& __x
)
2356 __is
>> __x
._M_alpha
;
2357 __x
._M_initialize();
2365 result_type _M_alpha
;
2367 // Hosts either lambda of GB or d of modified Vaduva's.
2371 /// @} group tr1_random_distributions_continuous
2372 /// @} group tr1_random_distributions
2373 /// @} group tr1_random
2376 _GLIBCXX_END_NAMESPACE_VERSION
2379 #endif // _GLIBCXX_TR1_RANDOM_H