1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2024 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
36 namespace std
_GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 // [5.1] Random number generation
45 * @addtogroup tr1_random Random Number Generation
46 * A facility for generating random numbers on selected distributions.
51 * Implementation-space details.
55 template<typename _UIntType
, int __w
,
56 bool = __w
< std::numeric_limits
<_UIntType
>::digits
>
58 { static const _UIntType __value
= 0; };
60 template<typename _UIntType
, int __w
>
61 struct _Shift
<_UIntType
, __w
, true>
62 { static const _UIntType __value
= _UIntType(1) << __w
; };
64 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
, bool>
67 // Dispatch based on modulus value to prevent divide-by-zero compile-time
68 // errors when m == 0.
69 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
>
72 { return _Mod
<_Tp
, __a
, __c
, __m
, __m
== 0>::__calc(__x
); }
74 typedef __gnu_cxx::__conditional_type
<(sizeof(unsigned) == 4),
75 unsigned, unsigned long>::__type _UInt32Type
;
78 * An adaptor class for converting the output of any Generator into
79 * the input for a specific Distribution.
81 template<typename _Engine
, typename _Distribution
>
84 typedef typename
_Engine::result_type _Engine_result_type
;
85 typedef typename
_Distribution::input_type result_type
;
88 _Adaptor(const _Engine
& __g
)
94 result_type __return_value
;
95 if (is_integral
<_Engine_result_type
>::value
96 && is_integral
<result_type
>::value
)
97 __return_value
= _M_g
.min();
99 __return_value
= result_type(0);
100 return __return_value
;
106 result_type __return_value
;
107 if (is_integral
<_Engine_result_type
>::value
108 && is_integral
<result_type
>::value
)
109 __return_value
= _M_g
.max();
110 else if (!is_integral
<result_type
>::value
)
111 __return_value
= result_type(1);
113 __return_value
= std::numeric_limits
<result_type
>::max() - 1;
114 return __return_value
;
118 * Converts a value generated by the adapted random number generator
119 * into a value in the input domain for the dependent random number
122 * Because the type traits are compile time constants only the
123 * appropriate clause of the if statements will actually be emitted
129 result_type __return_value
;
130 if (is_integral
<_Engine_result_type
>::value
131 && is_integral
<result_type
>::value
)
132 __return_value
= _M_g();
133 else if (!is_integral
<_Engine_result_type
>::value
134 && !is_integral
<result_type
>::value
)
135 __return_value
= result_type(_M_g() - _M_g
.min())
136 / result_type(_M_g
.max() - _M_g
.min());
137 else if (is_integral
<_Engine_result_type
>::value
138 && !is_integral
<result_type
>::value
)
139 __return_value
= result_type(_M_g() - _M_g
.min())
140 / result_type(_M_g
.max() - _M_g
.min() + result_type(1));
142 __return_value
= (((_M_g() - _M_g
.min())
143 / (_M_g
.max() - _M_g
.min()))
144 * std::numeric_limits
<result_type
>::max());
145 return __return_value
;
150 } // namespace __detail
153 * Produces random numbers on a given distribution function using a
154 * non-uniform random number generation engine.
156 * @todo the engine_value_type needs to be studied more carefully.
158 template<typename _Engine
, typename _Dist
>
159 class variate_generator
161 template<typename _Eng
>
167 _S_ref(const _Eng
& __e
) { return __e
; }
170 template<typename _Eng
>
175 __attribute__((__nonnull__
))
177 _S_ref(const _Eng
* __e
) { return *__e
; }
180 template<typename _Eng
>
186 _S_ref(const _Eng
& __e
) { return __e
; }
190 typedef _Engine engine_type
;
191 typedef typename _Value
<_Engine
>::type engine_value_type
;
192 typedef _Dist distribution_type
;
193 typedef typename
_Dist::result_type result_type
;
195 // Concept requirements.
196 __glibcxx_class_requires(engine_value_type
, _CopyConstructibleConcept
)
197 // __glibcxx_class_requires(_Engine, _EngineConcept)
198 // __glibcxx_class_requires(_Dist, _EngineConcept)
200 // tr1:5.1.1 table 5.1 requirement
201 typedef typename
__gnu_cxx::__enable_if
<
202 is_arithmetic
<result_type
>::value
, result_type
>::__type _IsValidType
;
205 * Constructs a variate generator with the uniform random number
206 * generator @p __eng for the random distribution @p __dist.
208 * @throws Any exceptions which may thrown by the copy constructors of
209 * the @p _Engine or @p _Dist objects.
211 variate_generator(engine_type __eng
, distribution_type __dist
)
212 : _M_engine(_Value
<_Engine
>::_S_ref(__eng
)), _M_dist(__dist
) { }
215 * Gets the next generated value on the distribution.
219 { return _M_dist(_M_engine
); }
224 template<typename _Tp
>
226 operator()(_Tp __value
)
227 { return _M_dist(_M_engine
, __value
); }
230 * Gets a reference to the underlying uniform random number generator
235 { return _M_engine
._M_g
; }
238 * Gets a const reference to the underlying uniform random number
241 const engine_value_type
&
243 { return _M_engine
._M_g
; }
246 * Gets a reference to the underlying random distribution.
253 * Gets a const reference to the underlying random distribution.
255 const distribution_type
&
260 * Gets the closed lower bound of the distribution interval.
264 { return this->distribution().min(); }
267 * Gets the closed upper bound of the distribution interval.
271 { return this->distribution().max(); }
274 __detail::_Adaptor
<engine_value_type
, _Dist
> _M_engine
;
275 distribution_type _M_dist
;
280 * @addtogroup tr1_random_generators Random Number Generators
281 * @ingroup tr1_random
283 * These classes define objects which provide random or pseudorandom
284 * numbers, either from a discrete or a continuous interval. The
285 * random number generator supplied as a part of this library are
286 * all uniform random number generators which provide a sequence of
287 * random number uniformly distributed over their range.
289 * A number generator is a function object with an operator() that
290 * takes zero arguments and returns a number.
292 * A compliant random number generator must satisfy the following
293 * requirements. <table border=1 cellpadding=10 cellspacing=0>
294 * <caption align=top>Random Number Generator Requirements</caption>
295 * <tr><td>To be documented.</td></tr> </table>
301 * @brief A model of a linear congruential random number generator.
303 * A random number generator that produces pseudorandom numbers using the
304 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
306 * The template parameter @p _UIntType must be an unsigned integral type
307 * large enough to store values up to (__m-1). If the template parameter
308 * @p __m is 0, the modulus @p __m used is
309 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
310 * parameters @p __a and @p __c must be less than @p __m.
312 * The size of the state is @f$ 1 @f$.
314 template<class _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
315 class linear_congruential
317 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
318 // __glibcpp_class_requires(__a < __m && __c < __m)
321 /** The type of the generated random value. */
322 typedef _UIntType result_type
;
324 /** The multiplier. */
325 static const _UIntType multiplier
= __a
;
327 static const _UIntType increment
= __c
;
329 static const _UIntType modulus
= __m
;
332 * Constructs a %linear_congruential random number generator engine with
333 * seed @p __s. The default seed value is 1.
335 * @param __s The initial seed value.
338 linear_congruential(unsigned long __x0
= 1)
339 { this->seed(__x0
); }
342 * Constructs a %linear_congruential random number generator engine
343 * seeded from the generator function @p __g.
345 * @param __g The seed generator function.
348 linear_congruential(_Gen
& __g
)
352 * Reseeds the %linear_congruential random number generator engine
353 * sequence to the seed @g __s.
355 * @param __s The new seed.
358 seed(unsigned long __s
= 1);
361 * Reseeds the %linear_congruential random number generator engine
362 * sequence using values from the generator function @p __g.
364 * @param __g the seed generator function.
369 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
372 * Gets the smallest possible value in the output range.
374 * The minimum depends on the @p __c parameter: if it is zero, the
375 * minimum generated must be > 0, otherwise 0 is allowed.
379 { return (__detail::__mod
<_UIntType
, 1, 0, __m
>(__c
) == 0) ? 1 : 0; }
382 * Gets the largest possible value in the output range.
389 * Gets the next random number in the sequence.
395 * Compares two linear congruential random number generator
396 * objects of the same type for equality.
398 * @param __lhs A linear congruential random number generator object.
399 * @param __rhs Another linear congruential random number generator obj.
401 * @returns true if the two objects are equal, false otherwise.
404 operator==(const linear_congruential
& __lhs
,
405 const linear_congruential
& __rhs
)
406 { return __lhs
._M_x
== __rhs
._M_x
; }
409 * Compares two linear congruential random number generator
410 * objects of the same type for inequality.
412 * @param __lhs A linear congruential random number generator object.
413 * @param __rhs Another linear congruential random number generator obj.
415 * @returns true if the two objects are not equal, false otherwise.
418 operator!=(const linear_congruential
& __lhs
,
419 const linear_congruential
& __rhs
)
420 { return !(__lhs
== __rhs
); }
423 * Writes the textual representation of the state x(i) of x to @p __os.
425 * @param __os The output stream.
426 * @param __lcr A % linear_congruential random number generator.
429 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
431 typename _CharT
, typename _Traits
>
432 friend std::basic_ostream
<_CharT
, _Traits
>&
433 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
434 const linear_congruential
<_UIntType1
, __a1
, __c1
,
438 * Sets the state of the engine by reading its textual
439 * representation from @p __is.
441 * The textual representation must have been previously written using an
442 * output stream whose imbued locale and whose type's template
443 * specialization arguments _CharT and _Traits were the same as those of
446 * @param __is The input stream.
447 * @param __lcr A % linear_congruential random number generator.
450 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
452 typename _CharT
, typename _Traits
>
453 friend std::basic_istream
<_CharT
, _Traits
>&
454 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
455 linear_congruential
<_UIntType1
, __a1
, __c1
, __m1
>& __lcr
);
460 seed(_Gen
& __g
, true_type
)
461 { return seed(static_cast<unsigned long>(__g
)); }
465 seed(_Gen
& __g
, false_type
);
471 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
473 typedef linear_congruential
<unsigned long, 16807, 0, 2147483647> minstd_rand0
;
476 * An alternative LCR (Lehmer Generator function) .
478 typedef linear_congruential
<unsigned long, 48271, 0, 2147483647> minstd_rand
;
482 * A generalized feedback shift register discrete random number generator.
484 * This algorithm avoids multiplication and division and is designed to be
485 * friendly to a pipelined architecture. If the parameters are chosen
486 * correctly, this generator will produce numbers with a very long period and
487 * fairly good apparent entropy, although still not cryptographically strong.
489 * The best way to use this generator is with the predefined mt19937 class.
491 * This algorithm was originally invented by Makoto Matsumoto and
494 * @var word_size The number of bits in each element of the state vector.
495 * @var state_size The degree of recursion.
496 * @var shift_size The period parameter.
497 * @var mask_bits The separation point bit index.
498 * @var parameter_a The last row of the twist matrix.
499 * @var output_u The first right-shift tempering matrix parameter.
500 * @var output_s The first left-shift tempering matrix parameter.
501 * @var output_b The first left-shift tempering matrix mask.
502 * @var output_t The second left-shift tempering matrix parameter.
503 * @var output_c The second left-shift tempering matrix mask.
504 * @var output_l The second right-shift tempering matrix parameter.
506 template<class _UIntType
, int __w
, int __n
, int __m
, int __r
,
507 _UIntType __a
, int __u
, int __s
, _UIntType __b
, int __t
,
508 _UIntType __c
, int __l
>
509 class mersenne_twister
511 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
515 typedef _UIntType result_type
;
518 static const int word_size
= __w
;
519 static const int state_size
= __n
;
520 static const int shift_size
= __m
;
521 static const int mask_bits
= __r
;
522 static const _UIntType parameter_a
= __a
;
523 static const int output_u
= __u
;
524 static const int output_s
= __s
;
525 static const _UIntType output_b
= __b
;
526 static const int output_t
= __t
;
527 static const _UIntType output_c
= __c
;
528 static const int output_l
= __l
;
530 // constructors and member function
535 mersenne_twister(unsigned long __value
)
539 mersenne_twister(_Gen
& __g
)
547 seed(unsigned long __value
);
552 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
560 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
566 * Compares two % mersenne_twister random number generator objects of
567 * the same type for equality.
569 * @param __lhs A % mersenne_twister random number generator object.
570 * @param __rhs Another % mersenne_twister random number generator
573 * @returns true if the two objects are equal, false otherwise.
576 operator==(const mersenne_twister
& __lhs
,
577 const mersenne_twister
& __rhs
)
578 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
581 * Compares two % mersenne_twister random number generator objects of
582 * the same type for inequality.
584 * @param __lhs A % mersenne_twister random number generator object.
585 * @param __rhs Another % mersenne_twister random number generator
588 * @returns true if the two objects are not equal, false otherwise.
591 operator!=(const mersenne_twister
& __lhs
,
592 const mersenne_twister
& __rhs
)
593 { return !(__lhs
== __rhs
); }
596 * Inserts the current state of a % mersenne_twister random number
597 * generator engine @p __x into the output stream @p __os.
599 * @param __os An output stream.
600 * @param __x A % mersenne_twister random number generator engine.
602 * @returns The output stream with the state of @p __x inserted or in
605 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
606 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
607 _UIntType1 __c1
, int __l1
,
608 typename _CharT
, typename _Traits
>
609 friend std::basic_ostream
<_CharT
, _Traits
>&
610 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
611 const mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
612 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
615 * Extracts the current state of a % mersenne_twister random number
616 * generator engine @p __x from the input stream @p __is.
618 * @param __is An input stream.
619 * @param __x A % mersenne_twister random number generator engine.
621 * @returns The input stream with the state of @p __x extracted or in
624 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
625 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
626 _UIntType1 __c1
, int __l1
,
627 typename _CharT
, typename _Traits
>
628 friend std::basic_istream
<_CharT
, _Traits
>&
629 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
630 mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
631 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
636 seed(_Gen
& __g
, true_type
)
637 { return seed(static_cast<unsigned long>(__g
)); }
641 seed(_Gen
& __g
, false_type
);
643 _UIntType _M_x
[state_size
];
648 * The classic Mersenne Twister.
651 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
652 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
653 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
655 typedef mersenne_twister
<
656 unsigned long, 32, 624, 397, 31,
664 * @brief The Marsaglia-Zaman generator.
666 * This is a model of a Generalized Fibonacci discrete random number
667 * generator, sometimes referred to as the SWC generator.
669 * A discrete random number generator that produces pseudorandom
670 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
671 * carry_{i-1}) \bmod m @f$.
673 * The size of the state is @f$ r @f$
674 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
676 * N1688[4.13] says <em>the template parameter _IntType shall denote
677 * an integral type large enough to store values up to m</em>.
679 * @var _M_x The state of the generator. This is a ring buffer.
680 * @var _M_carry The carry.
681 * @var _M_p Current index of x(i - r).
683 template<typename _IntType
, _IntType __m
, int __s
, int __r
>
684 class subtract_with_carry
686 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
689 /** The type of the generated random value. */
690 typedef _IntType result_type
;
693 static const _IntType modulus
= __m
;
694 static const int long_lag
= __r
;
695 static const int short_lag
= __s
;
698 * Constructs a default-initialized % subtract_with_carry random number
701 subtract_with_carry()
705 * Constructs an explicitly seeded % subtract_with_carry random number
709 subtract_with_carry(unsigned long __value
)
710 { this->seed(__value
); }
713 * Constructs a %subtract_with_carry random number generator engine
714 * seeded from the generator function @p __g.
716 * @param __g The seed generator function.
719 subtract_with_carry(_Gen
& __g
)
723 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
725 * N1688[4.19] modifies this as follows. If @p __value == 0,
726 * sets value to 19780503. In any case, with a linear
727 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
728 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
729 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
730 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
731 * set carry to 1, otherwise sets carry to 0.
734 seed(unsigned long __value
= 19780503);
737 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
738 * random number generator.
743 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
746 * Gets the inclusive minimum value of the range of random integers
747 * returned by this generator.
754 * Gets the inclusive maximum value of the range of random integers
755 * returned by this generator.
759 { return this->modulus
- 1; }
762 * Gets the next random number in the sequence.
768 * Compares two % subtract_with_carry random number generator objects of
769 * the same type for equality.
771 * @param __lhs A % subtract_with_carry random number generator object.
772 * @param __rhs Another % subtract_with_carry random number generator
775 * @returns true if the two objects are equal, false otherwise.
778 operator==(const subtract_with_carry
& __lhs
,
779 const subtract_with_carry
& __rhs
)
780 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
783 * Compares two % subtract_with_carry random number generator objects of
784 * the same type for inequality.
786 * @param __lhs A % subtract_with_carry random number generator object.
787 * @param __rhs Another % subtract_with_carry random number generator
790 * @returns true if the two objects are not equal, false otherwise.
793 operator!=(const subtract_with_carry
& __lhs
,
794 const subtract_with_carry
& __rhs
)
795 { return !(__lhs
== __rhs
); }
798 * Inserts the current state of a % subtract_with_carry random number
799 * generator engine @p __x into the output stream @p __os.
801 * @param __os An output stream.
802 * @param __x A % subtract_with_carry random number generator engine.
804 * @returns The output stream with the state of @p __x inserted or in
807 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
808 typename _CharT
, typename _Traits
>
809 friend std::basic_ostream
<_CharT
, _Traits
>&
810 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
811 const subtract_with_carry
<_IntType1
, __m1
, __s1
,
815 * Extracts the current state of a % subtract_with_carry random number
816 * generator engine @p __x from the input stream @p __is.
818 * @param __is An input stream.
819 * @param __x A % subtract_with_carry random number generator engine.
821 * @returns The input stream with the state of @p __x extracted or in
824 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
825 typename _CharT
, typename _Traits
>
826 friend std::basic_istream
<_CharT
, _Traits
>&
827 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
828 subtract_with_carry
<_IntType1
, __m1
, __s1
, __r1
>& __x
);
833 seed(_Gen
& __g
, true_type
)
834 { return seed(static_cast<unsigned long>(__g
)); }
838 seed(_Gen
& __g
, false_type
);
840 typedef typename
__gnu_cxx::__add_unsigned
<_IntType
>::__type _UIntType
;
842 _UIntType _M_x
[long_lag
];
849 * @brief The Marsaglia-Zaman generator (floats version).
851 * @var _M_x The state of the generator. This is a ring buffer.
852 * @var _M_carry The carry.
853 * @var _M_p Current index of x(i - r).
854 * @var _M_npows Precomputed negative powers of 2.
856 template<typename _RealType
, int __w
, int __s
, int __r
>
857 class subtract_with_carry_01
860 /** The type of the generated random value. */
861 typedef _RealType result_type
;
864 static const int word_size
= __w
;
865 static const int long_lag
= __r
;
866 static const int short_lag
= __s
;
869 * Constructs a default-initialized % subtract_with_carry_01 random
872 subtract_with_carry_01()
875 _M_initialize_npows();
879 * Constructs an explicitly seeded % subtract_with_carry_01 random number
883 subtract_with_carry_01(unsigned long __value
)
886 _M_initialize_npows();
890 * Constructs a % subtract_with_carry_01 random number generator engine
891 * seeded from the generator function @p __g.
893 * @param __g The seed generator function.
896 subtract_with_carry_01(_Gen
& __g
)
899 _M_initialize_npows();
903 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
906 seed(unsigned long __value
= 19780503);
909 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
910 * random number generator.
915 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
918 * Gets the minimum value of the range of random floats
919 * returned by this generator.
926 * Gets the maximum value of the range of random floats
927 * returned by this generator.
934 * Gets the next random number in the sequence.
940 * Compares two % subtract_with_carry_01 random number generator objects
941 * of the same type for equality.
943 * @param __lhs A % subtract_with_carry_01 random number
945 * @param __rhs Another % subtract_with_carry_01 random number generator
948 * @returns true if the two objects are equal, false otherwise.
951 operator==(const subtract_with_carry_01
& __lhs
,
952 const subtract_with_carry_01
& __rhs
)
954 for (int __i
= 0; __i
< long_lag
; ++__i
)
955 if (!std::equal(__lhs
._M_x
[__i
], __lhs
._M_x
[__i
] + __n
,
962 * Compares two % subtract_with_carry_01 random number generator objects
963 * of the same type for inequality.
965 * @param __lhs A % subtract_with_carry_01 random number
968 * @param __rhs Another % subtract_with_carry_01 random number generator
971 * @returns true if the two objects are not equal, false otherwise.
974 operator!=(const subtract_with_carry_01
& __lhs
,
975 const subtract_with_carry_01
& __rhs
)
976 { return !(__lhs
== __rhs
); }
979 * Inserts the current state of a % subtract_with_carry_01 random number
980 * generator engine @p __x into the output stream @p __os.
982 * @param __os An output stream.
983 * @param __x A % subtract_with_carry_01 random number generator engine.
985 * @returns The output stream with the state of @p __x inserted or in
988 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
989 typename _CharT
, typename _Traits
>
990 friend std::basic_ostream
<_CharT
, _Traits
>&
991 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
992 const subtract_with_carry_01
<_RealType1
, __w1
, __s1
,
996 * Extracts the current state of a % subtract_with_carry_01 random number
997 * generator engine @p __x from the input stream @p __is.
999 * @param __is An input stream.
1000 * @param __x A % subtract_with_carry_01 random number generator engine.
1002 * @returns The input stream with the state of @p __x extracted or in
1005 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
1006 typename _CharT
, typename _Traits
>
1007 friend std::basic_istream
<_CharT
, _Traits
>&
1008 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1009 subtract_with_carry_01
<_RealType1
, __w1
, __s1
, __r1
>& __x
);
1012 template<class _Gen
>
1014 seed(_Gen
& __g
, true_type
)
1015 { return seed(static_cast<unsigned long>(__g
)); }
1017 template<class _Gen
>
1019 seed(_Gen
& __g
, false_type
);
1022 _M_initialize_npows();
1024 static const int __n
= (__w
+ 31) / 32;
1026 typedef __detail::_UInt32Type _UInt32Type
;
1027 _UInt32Type _M_x
[long_lag
][__n
];
1028 _RealType _M_npows
[__n
];
1029 _UInt32Type _M_carry
;
1033 typedef subtract_with_carry_01
<float, 24, 10, 24> ranlux_base_01
;
1035 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1036 // 508. Bad parameters for ranlux64_base_01.
1037 typedef subtract_with_carry_01
<double, 48, 5, 12> ranlux64_base_01
;
1041 * Produces random numbers from some base engine by discarding blocks of
1044 * 0 <= @p __r <= @p __p
1046 template<class _UniformRandomNumberGenerator
, int __p
, int __r
>
1049 // __glibcxx_class_requires(typename base_type::result_type,
1050 // ArithmeticTypeConcept)
1053 /** The type of the underlying generator engine. */
1054 typedef _UniformRandomNumberGenerator base_type
;
1055 /** The type of the generated random value. */
1056 typedef typename
base_type::result_type result_type
;
1059 static const int block_size
= __p
;
1060 static const int used_block
= __r
;
1063 * Constructs a default %discard_block engine.
1065 * The underlying engine is default constructed as well.
1071 * Copy constructs a %discard_block engine.
1073 * Copies an existing base class random number generator.
1074 * @param rng An existing (base class) engine object.
1077 discard_block(const base_type
& __rng
)
1078 : _M_b(__rng
), _M_n(0) { }
1081 * Seed constructs a %discard_block engine.
1083 * Constructs the underlying generator engine seeded with @p __s.
1084 * @param __s A seed value for the base class engine.
1087 discard_block(unsigned long __s
)
1088 : _M_b(__s
), _M_n(0) { }
1091 * Generator construct a %discard_block engine.
1093 * @param __g A seed generator function.
1095 template<class _Gen
>
1096 discard_block(_Gen
& __g
)
1097 : _M_b(__g
), _M_n(0) { }
1100 * Reseeds the %discard_block object with the default seed for the
1101 * underlying base class generator engine.
1110 * Reseeds the %discard_block object with the given seed generator
1112 * @param __g A seed generator function.
1114 template<class _Gen
>
1115 void seed(_Gen
& __g
)
1122 * Gets a const reference to the underlying generator engine object.
1129 * Gets the minimum value in the generated random number range.
1133 { return _M_b
.min(); }
1136 * Gets the maximum value in the generated random number range.
1140 { return _M_b
.max(); }
1143 * Gets the next value in the generated random number sequence.
1149 * Compares two %discard_block random number generator objects of
1150 * the same type for equality.
1152 * @param __lhs A %discard_block random number generator object.
1153 * @param __rhs Another %discard_block random number generator
1156 * @returns true if the two objects are equal, false otherwise.
1159 operator==(const discard_block
& __lhs
, const discard_block
& __rhs
)
1160 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
1163 * Compares two %discard_block random number generator objects of
1164 * the same type for inequality.
1166 * @param __lhs A %discard_block random number generator object.
1167 * @param __rhs Another %discard_block random number generator
1170 * @returns true if the two objects are not equal, false otherwise.
1173 operator!=(const discard_block
& __lhs
, const discard_block
& __rhs
)
1174 { return !(__lhs
== __rhs
); }
1177 * Inserts the current state of a %discard_block random number
1178 * generator engine @p __x into the output stream @p __os.
1180 * @param __os An output stream.
1181 * @param __x A %discard_block random number generator engine.
1183 * @returns The output stream with the state of @p __x inserted or in
1186 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1187 typename _CharT
, typename _Traits
>
1188 friend std::basic_ostream
<_CharT
, _Traits
>&
1189 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1190 const discard_block
<_UniformRandomNumberGenerator1
,
1194 * Extracts the current state of a % subtract_with_carry random number
1195 * generator engine @p __x from the input stream @p __is.
1197 * @param __is An input stream.
1198 * @param __x A %discard_block random number generator engine.
1200 * @returns The input stream with the state of @p __x extracted or in
1203 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1204 typename _CharT
, typename _Traits
>
1205 friend std::basic_istream
<_CharT
, _Traits
>&
1206 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1207 discard_block
<_UniformRandomNumberGenerator1
,
1217 * James's luxury-level-3 integer adaptation of Luescher's generator.
1219 typedef discard_block
<
1220 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1226 * James's luxury-level-4 integer adaptation of Luescher's generator.
1228 typedef discard_block
<
1229 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1234 typedef discard_block
<
1235 subtract_with_carry_01
<float, 24, 10, 24>,
1240 typedef discard_block
<
1241 subtract_with_carry_01
<float, 24, 10, 24>,
1248 * A random number generator adaptor class that combines two random number
1249 * generator engines into a single output sequence.
1251 template<class _UniformRandomNumberGenerator1
, int __s1
,
1252 class _UniformRandomNumberGenerator2
, int __s2
>
1255 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1256 // result_type, ArithmeticTypeConcept)
1257 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1258 // result_type, ArithmeticTypeConcept)
1261 /** The type of the first underlying generator engine. */
1262 typedef _UniformRandomNumberGenerator1 base1_type
;
1263 /** The type of the second underlying generator engine. */
1264 typedef _UniformRandomNumberGenerator2 base2_type
;
1267 typedef typename
base1_type::result_type _Result_type1
;
1268 typedef typename
base2_type::result_type _Result_type2
;
1271 /** The type of the generated random value. */
1272 typedef typename
__gnu_cxx::__conditional_type
<(sizeof(_Result_type1
)
1273 > sizeof(_Result_type2
)),
1274 _Result_type1
, _Result_type2
>::__type result_type
;
1277 static const int shift1
= __s1
;
1278 static const int shift2
= __s2
;
1280 // constructors and member function
1283 { _M_initialize_max(); }
1285 xor_combine(const base1_type
& __rng1
, const base2_type
& __rng2
)
1286 : _M_b1(__rng1
), _M_b2(__rng2
)
1287 { _M_initialize_max(); }
1289 xor_combine(unsigned long __s
)
1290 : _M_b1(__s
), _M_b2(__s
+ 1)
1291 { _M_initialize_max(); }
1293 template<class _Gen
>
1294 xor_combine(_Gen
& __g
)
1295 : _M_b1(__g
), _M_b2(__g
)
1296 { _M_initialize_max(); }
1305 template<class _Gen
>
1330 * Gets the next random number in the sequence.
1332 // NB: Not exactly the TR1 formula, per N2079 instead.
1336 return ((result_type(_M_b1() - _M_b1
.min()) << shift1
)
1337 ^ (result_type(_M_b2() - _M_b2
.min()) << shift2
));
1341 * Compares two %xor_combine random number generator objects of
1342 * the same type for equality.
1344 * @param __lhs A %xor_combine random number generator object.
1345 * @param __rhs Another %xor_combine random number generator
1348 * @returns true if the two objects are equal, false otherwise.
1351 operator==(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1353 return (__lhs
.base1() == __rhs
.base1())
1354 && (__lhs
.base2() == __rhs
.base2());
1358 * Compares two %xor_combine random number generator objects of
1359 * the same type for inequality.
1361 * @param __lhs A %xor_combine random number generator object.
1362 * @param __rhs Another %xor_combine random number generator
1365 * @returns true if the two objects are not equal, false otherwise.
1368 operator!=(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1369 { return !(__lhs
== __rhs
); }
1372 * Inserts the current state of a %xor_combine random number
1373 * generator engine @p __x into the output stream @p __os.
1375 * @param __os An output stream.
1376 * @param __x A %xor_combine random number generator engine.
1378 * @returns The output stream with the state of @p __x inserted or in
1381 template<class _UniformRandomNumberGenerator11
, int __s11
,
1382 class _UniformRandomNumberGenerator21
, int __s21
,
1383 typename _CharT
, typename _Traits
>
1384 friend std::basic_ostream
<_CharT
, _Traits
>&
1385 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1386 const xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1387 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1390 * Extracts the current state of a %xor_combine random number
1391 * generator engine @p __x from the input stream @p __is.
1393 * @param __is An input stream.
1394 * @param __x A %xor_combine random number generator engine.
1396 * @returns The input stream with the state of @p __x extracted or in
1399 template<class _UniformRandomNumberGenerator11
, int __s11
,
1400 class _UniformRandomNumberGenerator21
, int __s21
,
1401 typename _CharT
, typename _Traits
>
1402 friend std::basic_istream
<_CharT
, _Traits
>&
1403 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1404 xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1405 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1409 _M_initialize_max();
1412 _M_initialize_max_aux(result_type
, result_type
, int);
1421 * A standard interface to a platform-specific non-deterministic
1422 * random number generator (if any are available).
1428 typedef unsigned int result_type
;
1430 // constructors, destructors and member functions
1432 #ifdef _GLIBCXX_USE_RANDOM_TR1
1435 random_device(const std::string
& __token
= "/dev/urandom")
1437 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1438 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1439 std::__throw_runtime_error(__N("random_device::"
1440 "random_device(const std::string&)"));
1444 { std::fclose(_M_file
); }
1449 random_device(const std::string
& __token
= "mt19937")
1450 : _M_mt(_M_strtoul(__token
)) { }
1453 static unsigned long
1454 _M_strtoul(const std::string
& __str
)
1456 unsigned long __ret
= 5489UL;
1457 if (__str
!= "mt19937")
1459 const char* __nptr
= __str
.c_str();
1461 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1462 if (*__nptr
== '\0' || *__endptr
!= '\0')
1463 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1464 "(const std::string&)"));
1475 { return std::numeric_limits
<result_type
>::min(); }
1479 { return std::numeric_limits
<result_type
>::max(); }
1488 #ifdef _GLIBCXX_USE_RANDOM_TR1
1490 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1499 random_device(const random_device
&);
1500 void operator=(const random_device
&);
1502 #ifdef _GLIBCXX_USE_RANDOM_TR1
1509 /// @} group tr1_random_generators
1512 * @addtogroup tr1_random_distributions Random Number Distributions
1513 * @ingroup tr1_random
1518 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1519 * @ingroup tr1_random_distributions
1524 * @brief Uniform discrete distribution for random numbers.
1525 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1526 * probability throughout the range.
1528 template<typename _IntType
= int>
1531 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
1534 /** The type of the parameters of the distribution. */
1535 typedef _IntType input_type
;
1536 /** The type of the range of the distribution. */
1537 typedef _IntType result_type
;
1541 * Constructs a uniform distribution object.
1544 uniform_int(_IntType __min
= 0, _IntType __max
= 9)
1545 : _M_min(__min
), _M_max(__max
)
1547 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
1551 * Gets the inclusive lower bound of the distribution range.
1558 * Gets the inclusive upper bound of the distribution range.
1565 * Resets the distribution state.
1567 * Does nothing for the uniform integer distribution.
1573 * Gets a uniformly distributed random number in the range
1576 template<typename _UniformRandomNumberGenerator
>
1578 operator()(_UniformRandomNumberGenerator
& __urng
)
1580 typedef typename
_UniformRandomNumberGenerator::result_type
1582 return _M_call(__urng
, _M_min
, _M_max
,
1583 typename is_integral
<_UResult_type
>::type());
1587 * Gets a uniform random number in the range @f$[0, n)@f$.
1589 * This function is aimed at use with std::random_shuffle.
1591 template<typename _UniformRandomNumberGenerator
>
1593 operator()(_UniformRandomNumberGenerator
& __urng
, result_type __n
)
1595 typedef typename
_UniformRandomNumberGenerator::result_type
1597 return _M_call(__urng
, 0, __n
- 1,
1598 typename is_integral
<_UResult_type
>::type());
1602 * Inserts a %uniform_int random number distribution @p __x into the
1603 * output stream @p os.
1605 * @param __os An output stream.
1606 * @param __x A %uniform_int random number distribution.
1608 * @returns The output stream with the state of @p __x inserted or in
1611 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1612 friend std::basic_ostream
<_CharT
, _Traits
>&
1613 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1614 const uniform_int
<_IntType1
>& __x
);
1617 * Extracts a %uniform_int random number distribution
1618 * @p __x from the input stream @p __is.
1620 * @param __is An input stream.
1621 * @param __x A %uniform_int random number generator engine.
1623 * @returns The input stream with @p __x extracted or in an error state.
1625 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1626 friend std::basic_istream
<_CharT
, _Traits
>&
1627 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1628 uniform_int
<_IntType1
>& __x
);
1631 template<typename _UniformRandomNumberGenerator
>
1633 _M_call(_UniformRandomNumberGenerator
& __urng
,
1634 result_type __min
, result_type __max
, true_type
);
1636 template<typename _UniformRandomNumberGenerator
>
1638 _M_call(_UniformRandomNumberGenerator
& __urng
,
1639 result_type __min
, result_type __max
, false_type
)
1641 return result_type((__urng() - __urng
.min())
1642 / (__urng
.max() - __urng
.min())
1643 * (__max
- __min
+ 1)) + __min
;
1652 * @brief A Bernoulli random number distribution.
1654 * Generates a sequence of true and false values with likelihood @f$ p @f$
1655 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1657 class bernoulli_distribution
1660 typedef int input_type
;
1661 typedef bool result_type
;
1665 * Constructs a Bernoulli distribution with likelihood @p p.
1667 * @param __p [IN] The likelihood of a true result being returned. Must
1668 * be in the interval @f$ [0, 1] @f$.
1671 bernoulli_distribution(double __p
= 0.5)
1674 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
1678 * Gets the @p p parameter of the distribution.
1685 * Resets the distribution state.
1687 * Does nothing for a Bernoulli distribution.
1693 * Gets the next value in the Bernoullian sequence.
1695 template<class _UniformRandomNumberGenerator
>
1697 operator()(_UniformRandomNumberGenerator
& __urng
)
1699 if ((__urng() - __urng
.min()) < _M_p
* (__urng
.max() - __urng
.min()))
1705 * Inserts a %bernoulli_distribution random number distribution
1706 * @p __x into the output stream @p __os.
1708 * @param __os An output stream.
1709 * @param __x A %bernoulli_distribution random number distribution.
1711 * @returns The output stream with the state of @p __x inserted or in
1714 template<typename _CharT
, typename _Traits
>
1715 friend std::basic_ostream
<_CharT
, _Traits
>&
1716 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1717 const bernoulli_distribution
& __x
);
1720 * Extracts a %bernoulli_distribution random number distribution
1721 * @p __x from the input stream @p __is.
1723 * @param __is An input stream.
1724 * @param __x A %bernoulli_distribution random number generator engine.
1726 * @returns The input stream with @p __x extracted or in an error state.
1728 template<typename _CharT
, typename _Traits
>
1729 friend std::basic_istream
<_CharT
, _Traits
>&
1730 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1731 bernoulli_distribution
& __x
)
1732 { return __is
>> __x
._M_p
; }
1740 * @brief A discrete geometric random number distribution.
1742 * The formula for the geometric probability mass function is
1743 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1746 template<typename _IntType
= int, typename _RealType
= double>
1747 class geometric_distribution
1751 typedef _RealType input_type
;
1752 typedef _IntType result_type
;
1754 // constructors and member function
1756 geometric_distribution(const _RealType
& __p
= _RealType(0.5))
1759 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
1764 * Gets the distribution parameter @p p.
1773 template<class _UniformRandomNumberGenerator
>
1775 operator()(_UniformRandomNumberGenerator
& __urng
);
1778 * Inserts a %geometric_distribution random number distribution
1779 * @p __x into the output stream @p __os.
1781 * @param __os An output stream.
1782 * @param __x A %geometric_distribution random number distribution.
1784 * @returns The output stream with the state of @p __x inserted or in
1787 template<typename _IntType1
, typename _RealType1
,
1788 typename _CharT
, typename _Traits
>
1789 friend std::basic_ostream
<_CharT
, _Traits
>&
1790 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1791 const geometric_distribution
<_IntType1
, _RealType1
>& __x
);
1794 * Extracts a %geometric_distribution random number distribution
1795 * @p __x from the input stream @p __is.
1797 * @param __is An input stream.
1798 * @param __x A %geometric_distribution random number generator engine.
1800 * @returns The input stream with @p __x extracted or in an error state.
1802 template<typename _CharT
, typename _Traits
>
1803 friend std::basic_istream
<_CharT
, _Traits
>&
1804 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1805 geometric_distribution
& __x
)
1808 __x
._M_initialize();
1815 { _M_log_p
= std::log(_M_p
); }
1822 template<typename _RealType
>
1823 class normal_distribution
;
1826 * @brief A discrete Poisson random number distribution.
1828 * The formula for the Poisson probability mass function is
1829 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1830 * parameter of the distribution.
1832 template<typename _IntType
= int, typename _RealType
= double>
1833 class poisson_distribution
1837 typedef _RealType input_type
;
1838 typedef _IntType result_type
;
1840 // constructors and member function
1842 poisson_distribution(const _RealType
& __mean
= _RealType(1))
1843 : _M_mean(__mean
), _M_nd()
1845 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
1850 * Gets the distribution parameter @p mean.
1860 template<class _UniformRandomNumberGenerator
>
1862 operator()(_UniformRandomNumberGenerator
& __urng
);
1865 * Inserts a %poisson_distribution random number distribution
1866 * @p __x into the output stream @p __os.
1868 * @param __os An output stream.
1869 * @param __x A %poisson_distribution random number distribution.
1871 * @returns The output stream with the state of @p __x inserted or in
1874 template<typename _IntType1
, typename _RealType1
,
1875 typename _CharT
, typename _Traits
>
1876 friend std::basic_ostream
<_CharT
, _Traits
>&
1877 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1878 const poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1881 * Extracts a %poisson_distribution random number distribution
1882 * @p __x from the input stream @p __is.
1884 * @param __is An input stream.
1885 * @param __x A %poisson_distribution random number generator engine.
1887 * @returns The input stream with @p __x extracted or in an error state.
1889 template<typename _IntType1
, typename _RealType1
,
1890 typename _CharT
, typename _Traits
>
1891 friend std::basic_istream
<_CharT
, _Traits
>&
1892 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1893 poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1899 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1900 normal_distribution
<_RealType
> _M_nd
;
1904 // Hosts either log(mean) or the threshold of the simple method.
1905 _RealType _M_lm_thr
;
1906 #if _GLIBCXX_USE_C99_MATH_TR1
1907 _RealType _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
1913 * @brief A discrete binomial random number distribution.
1915 * The formula for the binomial probability mass function is
1916 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1917 * and @f$ p @f$ are the parameters of the distribution.
1919 template<typename _IntType
= int, typename _RealType
= double>
1920 class binomial_distribution
1924 typedef _RealType input_type
;
1925 typedef _IntType result_type
;
1927 // constructors and member function
1929 binomial_distribution(_IntType __t
= 1,
1930 const _RealType
& __p
= _RealType(0.5))
1931 : _M_t(__t
), _M_p(__p
), _M_nd()
1933 _GLIBCXX_DEBUG_ASSERT((_M_t
>= 0) && (_M_p
>= 0.0) && (_M_p
<= 1.0));
1938 * Gets the distribution @p t parameter.
1945 * Gets the distribution @p p parameter.
1955 template<class _UniformRandomNumberGenerator
>
1957 operator()(_UniformRandomNumberGenerator
& __urng
);
1960 * Inserts a %binomial_distribution random number distribution
1961 * @p __x into the output stream @p __os.
1963 * @param __os An output stream.
1964 * @param __x A %binomial_distribution random number distribution.
1966 * @returns The output stream with the state of @p __x inserted or in
1969 template<typename _IntType1
, typename _RealType1
,
1970 typename _CharT
, typename _Traits
>
1971 friend std::basic_ostream
<_CharT
, _Traits
>&
1972 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1973 const binomial_distribution
<_IntType1
, _RealType1
>& __x
);
1976 * Extracts a %binomial_distribution random number distribution
1977 * @p __x from the input stream @p __is.
1979 * @param __is An input stream.
1980 * @param __x A %binomial_distribution random number generator engine.
1982 * @returns The input stream with @p __x extracted or in an error state.
1984 template<typename _IntType1
, typename _RealType1
,
1985 typename _CharT
, typename _Traits
>
1986 friend std::basic_istream
<_CharT
, _Traits
>&
1987 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1988 binomial_distribution
<_IntType1
, _RealType1
>& __x
);
1994 template<class _UniformRandomNumberGenerator
>
1996 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
1998 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1999 normal_distribution
<_RealType
> _M_nd
;
2002 #if _GLIBCXX_USE_C99_MATH_TR1
2003 _RealType _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
2004 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
2012 /// @} group tr1_random_distributions_discrete
2015 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2016 * @ingroup tr1_random_distributions
2021 * @brief Uniform continuous distribution for random numbers.
2023 * A continuous random distribution on the range [min, max) with equal
2024 * probability throughout the range. The URNG should be real-valued and
2025 * deliver number in the range [0, 1).
2027 template<typename _RealType
= double>
2032 typedef _RealType input_type
;
2033 typedef _RealType result_type
;
2037 * Constructs a uniform_real object.
2039 * @param __min [IN] The lower bound of the distribution.
2040 * @param __max [IN] The upper bound of the distribution.
2043 uniform_real(_RealType __min
= _RealType(0),
2044 _RealType __max
= _RealType(1))
2045 : _M_min(__min
), _M_max(__max
)
2047 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
2061 template<class _UniformRandomNumberGenerator
>
2063 operator()(_UniformRandomNumberGenerator
& __urng
)
2064 { return (__urng() * (_M_max
- _M_min
)) + _M_min
; }
2067 * Inserts a %uniform_real random number distribution @p __x into the
2068 * output stream @p __os.
2070 * @param __os An output stream.
2071 * @param __x A %uniform_real random number distribution.
2073 * @returns The output stream with the state of @p __x inserted or in
2076 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2077 friend std::basic_ostream
<_CharT
, _Traits
>&
2078 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2079 const uniform_real
<_RealType1
>& __x
);
2082 * Extracts a %uniform_real random number distribution
2083 * @p __x from the input stream @p __is.
2085 * @param __is An input stream.
2086 * @param __x A %uniform_real random number generator engine.
2088 * @returns The input stream with @p __x extracted or in an error state.
2090 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2091 friend std::basic_istream
<_CharT
, _Traits
>&
2092 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2093 uniform_real
<_RealType1
>& __x
);
2102 * @brief An exponential continuous distribution for random numbers.
2104 * The formula for the exponential probability mass function is
2105 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2107 * <table border=1 cellpadding=10 cellspacing=0>
2108 * <caption align=top>Distribution Statistics</caption>
2109 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2110 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2111 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2112 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2113 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2116 template<typename _RealType
= double>
2117 class exponential_distribution
2121 typedef _RealType input_type
;
2122 typedef _RealType result_type
;
2126 * Constructs an exponential distribution with inverse scale parameter
2130 exponential_distribution(const result_type
& __lambda
= result_type(1))
2131 : _M_lambda(__lambda
)
2133 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> 0);
2137 * Gets the inverse scale parameter of the distribution.
2141 { return _M_lambda
; }
2144 * Resets the distribution.
2146 * Has no effect on exponential distributions.
2151 template<class _UniformRandomNumberGenerator
>
2153 operator()(_UniformRandomNumberGenerator
& __urng
)
2154 { return -std::log(__urng()) / _M_lambda
; }
2157 * Inserts a %exponential_distribution random number distribution
2158 * @p __x into the output stream @p __os.
2160 * @param __os An output stream.
2161 * @param __x A %exponential_distribution random number distribution.
2163 * @returns The output stream with the state of @p __x inserted or in
2166 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2167 friend std::basic_ostream
<_CharT
, _Traits
>&
2168 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2169 const exponential_distribution
<_RealType1
>& __x
);
2172 * Extracts a %exponential_distribution random number distribution
2173 * @p __x from the input stream @p __is.
2175 * @param __is An input stream.
2176 * @param __x A %exponential_distribution random number
2179 * @returns The input stream with @p __x extracted or in an error state.
2181 template<typename _CharT
, typename _Traits
>
2182 friend std::basic_istream
<_CharT
, _Traits
>&
2183 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2184 exponential_distribution
& __x
)
2185 { return __is
>> __x
._M_lambda
; }
2188 result_type _M_lambda
;
2193 * @brief A normal continuous distribution for random numbers.
2195 * The formula for the normal probability mass function is
2196 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2197 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2199 template<typename _RealType
= double>
2200 class normal_distribution
2204 typedef _RealType input_type
;
2205 typedef _RealType result_type
;
2209 * Constructs a normal distribution with parameters @f$ mean @f$ and
2213 normal_distribution(const result_type
& __mean
= result_type(0),
2214 const result_type
& __sigma
= result_type(1))
2215 : _M_mean(__mean
), _M_sigma(__sigma
), _M_saved_available(false)
2217 _GLIBCXX_DEBUG_ASSERT(_M_sigma
> 0);
2221 * Gets the mean of the distribution.
2228 * Gets the @f$ \sigma @f$ of the distribution.
2232 { return _M_sigma
; }
2235 * Resets the distribution.
2239 { _M_saved_available
= false; }
2241 template<class _UniformRandomNumberGenerator
>
2243 operator()(_UniformRandomNumberGenerator
& __urng
);
2246 * Inserts a %normal_distribution random number distribution
2247 * @p __x into the output stream @p __os.
2249 * @param __os An output stream.
2250 * @param __x A %normal_distribution random number distribution.
2252 * @returns The output stream with the state of @p __x inserted or in
2255 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2256 friend std::basic_ostream
<_CharT
, _Traits
>&
2257 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2258 const normal_distribution
<_RealType1
>& __x
);
2261 * Extracts a %normal_distribution random number distribution
2262 * @p __x from the input stream @p __is.
2264 * @param __is An input stream.
2265 * @param __x A %normal_distribution random number generator engine.
2267 * @returns The input stream with @p __x extracted or in an error state.
2269 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2270 friend std::basic_istream
<_CharT
, _Traits
>&
2271 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2272 normal_distribution
<_RealType1
>& __x
);
2275 result_type _M_mean
;
2276 result_type _M_sigma
;
2277 result_type _M_saved
;
2278 bool _M_saved_available
;
2283 * @brief A gamma continuous distribution for random numbers.
2285 * The formula for the gamma probability mass function is
2286 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2288 template<typename _RealType
= double>
2289 class gamma_distribution
2293 typedef _RealType input_type
;
2294 typedef _RealType result_type
;
2298 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2301 gamma_distribution(const result_type
& __alpha_val
= result_type(1))
2302 : _M_alpha(__alpha_val
)
2304 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> 0);
2309 * Gets the @f$ \alpha @f$ of the distribution.
2313 { return _M_alpha
; }
2316 * Resets the distribution.
2321 template<class _UniformRandomNumberGenerator
>
2323 operator()(_UniformRandomNumberGenerator
& __urng
);
2326 * Inserts a %gamma_distribution random number distribution
2327 * @p __x into the output stream @p __os.
2329 * @param __os An output stream.
2330 * @param __x A %gamma_distribution random number distribution.
2332 * @returns The output stream with the state of @p __x inserted or in
2335 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2336 friend std::basic_ostream
<_CharT
, _Traits
>&
2337 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2338 const gamma_distribution
<_RealType1
>& __x
);
2341 * Extracts a %gamma_distribution random number distribution
2342 * @p __x from the input stream @p __is.
2344 * @param __is An input stream.
2345 * @param __x A %gamma_distribution random number generator engine.
2347 * @returns The input stream with @p __x extracted or in an error state.
2349 template<typename _CharT
, typename _Traits
>
2350 friend std::basic_istream
<_CharT
, _Traits
>&
2351 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2352 gamma_distribution
& __x
)
2354 __is
>> __x
._M_alpha
;
2355 __x
._M_initialize();
2363 result_type _M_alpha
;
2365 // Hosts either lambda of GB or d of modified Vaduva's.
2369 /// @} group tr1_random_distributions_continuous
2370 /// @} group tr1_random_distributions
2371 /// @} group tr1_random
2374 _GLIBCXX_END_NAMESPACE_VERSION
2377 #endif // _GLIBCXX_TR1_RANDOM_H