1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009-2017 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 remove_reference
<_Engine
>::type _BEngine
;
85 typedef typename
_BEngine::result_type _Engine_result_type
;
86 typedef typename
_Distribution::input_type result_type
;
89 _Adaptor(const _Engine
& __g
)
95 result_type __return_value
;
96 if (is_integral
<_Engine_result_type
>::value
97 && is_integral
<result_type
>::value
)
98 __return_value
= _M_g
.min();
100 __return_value
= result_type(0);
101 return __return_value
;
107 result_type __return_value
;
108 if (is_integral
<_Engine_result_type
>::value
109 && is_integral
<result_type
>::value
)
110 __return_value
= _M_g
.max();
111 else if (!is_integral
<result_type
>::value
)
112 __return_value
= result_type(1);
114 __return_value
= std::numeric_limits
<result_type
>::max() - 1;
115 return __return_value
;
119 * Converts a value generated by the adapted random number generator
120 * into a value in the input domain for the dependent random number
123 * Because the type traits are compile time constants only the
124 * appropriate clause of the if statements will actually be emitted
130 result_type __return_value
;
131 if (is_integral
<_Engine_result_type
>::value
132 && is_integral
<result_type
>::value
)
133 __return_value
= _M_g();
134 else if (!is_integral
<_Engine_result_type
>::value
135 && !is_integral
<result_type
>::value
)
136 __return_value
= result_type(_M_g() - _M_g
.min())
137 / result_type(_M_g
.max() - _M_g
.min());
138 else if (is_integral
<_Engine_result_type
>::value
139 && !is_integral
<result_type
>::value
)
140 __return_value
= result_type(_M_g() - _M_g
.min())
141 / result_type(_M_g
.max() - _M_g
.min() + result_type(1));
143 __return_value
= (((_M_g() - _M_g
.min())
144 / (_M_g
.max() - _M_g
.min()))
145 * std::numeric_limits
<result_type
>::max());
146 return __return_value
;
153 // Specialization for _Engine*.
154 template<typename _Engine
, typename _Distribution
>
155 struct _Adaptor
<_Engine
*, _Distribution
>
157 typedef typename
_Engine::result_type _Engine_result_type
;
158 typedef typename
_Distribution::input_type result_type
;
161 _Adaptor(_Engine
* __g
)
167 result_type __return_value
;
168 if (is_integral
<_Engine_result_type
>::value
169 && is_integral
<result_type
>::value
)
170 __return_value
= _M_g
->min();
172 __return_value
= result_type(0);
173 return __return_value
;
179 result_type __return_value
;
180 if (is_integral
<_Engine_result_type
>::value
181 && is_integral
<result_type
>::value
)
182 __return_value
= _M_g
->max();
183 else if (!is_integral
<result_type
>::value
)
184 __return_value
= result_type(1);
186 __return_value
= std::numeric_limits
<result_type
>::max() - 1;
187 return __return_value
;
193 result_type __return_value
;
194 if (is_integral
<_Engine_result_type
>::value
195 && is_integral
<result_type
>::value
)
196 __return_value
= (*_M_g
)();
197 else if (!is_integral
<_Engine_result_type
>::value
198 && !is_integral
<result_type
>::value
)
199 __return_value
= result_type((*_M_g
)() - _M_g
->min())
200 / result_type(_M_g
->max() - _M_g
->min());
201 else if (is_integral
<_Engine_result_type
>::value
202 && !is_integral
<result_type
>::value
)
203 __return_value
= result_type((*_M_g
)() - _M_g
->min())
204 / result_type(_M_g
->max() - _M_g
->min() + result_type(1));
206 __return_value
= ((((*_M_g
)() - _M_g
->min())
207 / (_M_g
->max() - _M_g
->min()))
208 * std::numeric_limits
<result_type
>::max());
209 return __return_value
;
215 } // namespace __detail
218 * Produces random numbers on a given distribution function using a
219 * non-uniform random number generation engine.
221 * @todo the engine_value_type needs to be studied more carefully.
223 template<typename _Engine
, typename _Dist
>
224 class variate_generator
226 // Concept requirements.
227 __glibcxx_class_requires(_Engine
, _CopyConstructibleConcept
)
228 // __glibcxx_class_requires(_Engine, _EngineConcept)
229 // __glibcxx_class_requires(_Dist, _EngineConcept)
232 typedef _Engine engine_type
;
233 typedef __detail::_Adaptor
<_Engine
, _Dist
> engine_value_type
;
234 typedef _Dist distribution_type
;
235 typedef typename
_Dist::result_type result_type
;
237 // tr1:5.1.1 table 5.1 requirement
238 typedef typename
__gnu_cxx::__enable_if
<
239 is_arithmetic
<result_type
>::value
, result_type
>::__type _IsValidType
;
242 * Constructs a variate generator with the uniform random number
243 * generator @p __eng for the random distribution @p __dist.
245 * @throws Any exceptions which may thrown by the copy constructors of
246 * the @p _Engine or @p _Dist objects.
248 variate_generator(engine_type __eng
, distribution_type __dist
)
249 : _M_engine(__eng
), _M_dist(__dist
) { }
252 * Gets the next generated value on the distribution.
256 { return _M_dist(_M_engine
); }
261 template<typename _Tp
>
263 operator()(_Tp __value
)
264 { return _M_dist(_M_engine
, __value
); }
267 * Gets a reference to the underlying uniform random number generator
272 { return _M_engine
; }
275 * Gets a const reference to the underlying uniform random number
278 const engine_value_type
&
280 { return _M_engine
; }
283 * Gets a reference to the underlying random distribution.
290 * Gets a const reference to the underlying random distribution.
292 const distribution_type
&
297 * Gets the closed lower bound of the distribution interval.
301 { return this->distribution().min(); }
304 * Gets the closed upper bound of the distribution interval.
308 { return this->distribution().max(); }
311 engine_value_type _M_engine
;
312 distribution_type _M_dist
;
317 * @addtogroup tr1_random_generators Random Number Generators
318 * @ingroup tr1_random
320 * These classes define objects which provide random or pseudorandom
321 * numbers, either from a discrete or a continuous interval. The
322 * random number generator supplied as a part of this library are
323 * all uniform random number generators which provide a sequence of
324 * random number uniformly distributed over their range.
326 * A number generator is a function object with an operator() that
327 * takes zero arguments and returns a number.
329 * A compliant random number generator must satisfy the following
330 * requirements. <table border=1 cellpadding=10 cellspacing=0>
331 * <caption align=top>Random Number Generator Requirements</caption>
332 * <tr><td>To be documented.</td></tr> </table>
338 * @brief A model of a linear congruential random number generator.
340 * A random number generator that produces pseudorandom numbers using the
341 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
343 * The template parameter @p _UIntType must be an unsigned integral type
344 * large enough to store values up to (__m-1). If the template parameter
345 * @p __m is 0, the modulus @p __m used is
346 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
347 * parameters @p __a and @p __c must be less than @p __m.
349 * The size of the state is @f$ 1 @f$.
351 template<class _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
352 class linear_congruential
354 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
355 // __glibcpp_class_requires(__a < __m && __c < __m)
358 /** The type of the generated random value. */
359 typedef _UIntType result_type
;
361 /** The multiplier. */
362 static const _UIntType multiplier
= __a
;
364 static const _UIntType increment
= __c
;
366 static const _UIntType modulus
= __m
;
369 * Constructs a %linear_congruential random number generator engine with
370 * seed @p __s. The default seed value is 1.
372 * @param __s The initial seed value.
375 linear_congruential(unsigned long __x0
= 1)
376 { this->seed(__x0
); }
379 * Constructs a %linear_congruential random number generator engine
380 * seeded from the generator function @p __g.
382 * @param __g The seed generator function.
385 linear_congruential(_Gen
& __g
)
389 * Reseeds the %linear_congruential random number generator engine
390 * sequence to the seed @g __s.
392 * @param __s The new seed.
395 seed(unsigned long __s
= 1);
398 * Reseeds the %linear_congruential random number generator engine
399 * sequence using values from the generator function @p __g.
401 * @param __g the seed generator function.
406 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
409 * Gets the smallest possible value in the output range.
411 * The minimum depends on the @p __c parameter: if it is zero, the
412 * minimum generated must be > 0, otherwise 0 is allowed.
416 { return (__detail::__mod
<_UIntType
, 1, 0, __m
>(__c
) == 0) ? 1 : 0; }
419 * Gets the largest possible value in the output range.
426 * Gets the next random number in the sequence.
432 * Compares two linear congruential random number generator
433 * objects of the same type for equality.
435 * @param __lhs A linear congruential random number generator object.
436 * @param __rhs Another linear congruential random number generator obj.
438 * @returns true if the two objects are equal, false otherwise.
441 operator==(const linear_congruential
& __lhs
,
442 const linear_congruential
& __rhs
)
443 { return __lhs
._M_x
== __rhs
._M_x
; }
446 * Compares two linear congruential random number generator
447 * objects of the same type for inequality.
449 * @param __lhs A linear congruential random number generator object.
450 * @param __rhs Another linear congruential random number generator obj.
452 * @returns true if the two objects are not equal, false otherwise.
455 operator!=(const linear_congruential
& __lhs
,
456 const linear_congruential
& __rhs
)
457 { return !(__lhs
== __rhs
); }
460 * Writes the textual representation of the state x(i) of x to @p __os.
462 * @param __os The output stream.
463 * @param __lcr A % linear_congruential random number generator.
466 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
468 typename _CharT
, typename _Traits
>
469 friend std::basic_ostream
<_CharT
, _Traits
>&
470 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
471 const linear_congruential
<_UIntType1
, __a1
, __c1
,
475 * Sets the state of the engine by reading its textual
476 * representation from @p __is.
478 * The textual representation must have been previously written using an
479 * output stream whose imbued locale and whose type's template
480 * specialization arguments _CharT and _Traits were the same as those of
483 * @param __is The input stream.
484 * @param __lcr A % linear_congruential random number generator.
487 template<class _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
489 typename _CharT
, typename _Traits
>
490 friend std::basic_istream
<_CharT
, _Traits
>&
491 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
492 linear_congruential
<_UIntType1
, __a1
, __c1
, __m1
>& __lcr
);
497 seed(_Gen
& __g
, true_type
)
498 { return seed(static_cast<unsigned long>(__g
)); }
502 seed(_Gen
& __g
, false_type
);
508 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
510 typedef linear_congruential
<unsigned long, 16807, 0, 2147483647> minstd_rand0
;
513 * An alternative LCR (Lehmer Generator function) .
515 typedef linear_congruential
<unsigned long, 48271, 0, 2147483647> minstd_rand
;
519 * A generalized feedback shift register discrete random number generator.
521 * This algorithm avoids multiplication and division and is designed to be
522 * friendly to a pipelined architecture. If the parameters are chosen
523 * correctly, this generator will produce numbers with a very long period and
524 * fairly good apparent entropy, although still not cryptographically strong.
526 * The best way to use this generator is with the predefined mt19937 class.
528 * This algorithm was originally invented by Makoto Matsumoto and
531 * @var word_size The number of bits in each element of the state vector.
532 * @var state_size The degree of recursion.
533 * @var shift_size The period parameter.
534 * @var mask_bits The separation point bit index.
535 * @var parameter_a The last row of the twist matrix.
536 * @var output_u The first right-shift tempering matrix parameter.
537 * @var output_s The first left-shift tempering matrix parameter.
538 * @var output_b The first left-shift tempering matrix mask.
539 * @var output_t The second left-shift tempering matrix parameter.
540 * @var output_c The second left-shift tempering matrix mask.
541 * @var output_l The second right-shift tempering matrix parameter.
543 template<class _UIntType
, int __w
, int __n
, int __m
, int __r
,
544 _UIntType __a
, int __u
, int __s
, _UIntType __b
, int __t
,
545 _UIntType __c
, int __l
>
546 class mersenne_twister
548 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
552 typedef _UIntType result_type
;
555 static const int word_size
= __w
;
556 static const int state_size
= __n
;
557 static const int shift_size
= __m
;
558 static const int mask_bits
= __r
;
559 static const _UIntType parameter_a
= __a
;
560 static const int output_u
= __u
;
561 static const int output_s
= __s
;
562 static const _UIntType output_b
= __b
;
563 static const int output_t
= __t
;
564 static const _UIntType output_c
= __c
;
565 static const int output_l
= __l
;
567 // constructors and member function
572 mersenne_twister(unsigned long __value
)
576 mersenne_twister(_Gen
& __g
)
584 seed(unsigned long __value
);
589 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
597 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
603 * Compares two % mersenne_twister random number generator objects of
604 * the same type for equality.
606 * @param __lhs A % mersenne_twister random number generator object.
607 * @param __rhs Another % mersenne_twister random number generator
610 * @returns true if the two objects are equal, false otherwise.
613 operator==(const mersenne_twister
& __lhs
,
614 const mersenne_twister
& __rhs
)
615 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
618 * Compares two % mersenne_twister random number generator objects of
619 * the same type for inequality.
621 * @param __lhs A % mersenne_twister random number generator object.
622 * @param __rhs Another % mersenne_twister random number generator
625 * @returns true if the two objects are not equal, false otherwise.
628 operator!=(const mersenne_twister
& __lhs
,
629 const mersenne_twister
& __rhs
)
630 { return !(__lhs
== __rhs
); }
633 * Inserts the current state of a % mersenne_twister random number
634 * generator engine @p __x into the output stream @p __os.
636 * @param __os An output stream.
637 * @param __x A % mersenne_twister random number generator engine.
639 * @returns The output stream with the state of @p __x inserted or in
642 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
643 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
644 _UIntType1 __c1
, int __l1
,
645 typename _CharT
, typename _Traits
>
646 friend std::basic_ostream
<_CharT
, _Traits
>&
647 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
648 const mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
649 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
652 * Extracts the current state of a % mersenne_twister random number
653 * generator engine @p __x from the input stream @p __is.
655 * @param __is An input stream.
656 * @param __x A % mersenne_twister random number generator engine.
658 * @returns The input stream with the state of @p __x extracted or in
661 template<class _UIntType1
, int __w1
, int __n1
, int __m1
, int __r1
,
662 _UIntType1 __a1
, int __u1
, int __s1
, _UIntType1 __b1
, int __t1
,
663 _UIntType1 __c1
, int __l1
,
664 typename _CharT
, typename _Traits
>
665 friend std::basic_istream
<_CharT
, _Traits
>&
666 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
667 mersenne_twister
<_UIntType1
, __w1
, __n1
, __m1
, __r1
,
668 __a1
, __u1
, __s1
, __b1
, __t1
, __c1
, __l1
>& __x
);
673 seed(_Gen
& __g
, true_type
)
674 { return seed(static_cast<unsigned long>(__g
)); }
678 seed(_Gen
& __g
, false_type
);
680 _UIntType _M_x
[state_size
];
685 * The classic Mersenne Twister.
688 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
689 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
690 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
692 typedef mersenne_twister
<
693 unsigned long, 32, 624, 397, 31,
701 * @brief The Marsaglia-Zaman generator.
703 * This is a model of a Generalized Fibonacci discrete random number
704 * generator, sometimes referred to as the SWC generator.
706 * A discrete random number generator that produces pseudorandom
707 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
708 * carry_{i-1}) \bmod m @f$.
710 * The size of the state is @f$ r @f$
711 * and the maximum period of the generator is @f$ m^r - m^s -1 @f$.
713 * N1688[4.13] says <em>the template parameter _IntType shall denote
714 * an integral type large enough to store values up to m</em>.
716 * @var _M_x The state of the generator. This is a ring buffer.
717 * @var _M_carry The carry.
718 * @var _M_p Current index of x(i - r).
720 template<typename _IntType
, _IntType __m
, int __s
, int __r
>
721 class subtract_with_carry
723 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
726 /** The type of the generated random value. */
727 typedef _IntType result_type
;
730 static const _IntType modulus
= __m
;
731 static const int long_lag
= __r
;
732 static const int short_lag
= __s
;
735 * Constructs a default-initialized % subtract_with_carry random number
738 subtract_with_carry()
742 * Constructs an explicitly seeded % subtract_with_carry random number
746 subtract_with_carry(unsigned long __value
)
747 { this->seed(__value
); }
750 * Constructs a %subtract_with_carry random number generator engine
751 * seeded from the generator function @p __g.
753 * @param __g The seed generator function.
756 subtract_with_carry(_Gen
& __g
)
760 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
762 * N1688[4.19] modifies this as follows. If @p __value == 0,
763 * sets value to 19780503. In any case, with a linear
764 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
765 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
766 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
767 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
768 * set carry to 1, otherwise sets carry to 0.
771 seed(unsigned long __value
= 19780503);
774 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry
775 * random number generator.
780 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
783 * Gets the inclusive minimum value of the range of random integers
784 * returned by this generator.
791 * Gets the inclusive maximum value of the range of random integers
792 * returned by this generator.
796 { return this->modulus
- 1; }
799 * Gets the next random number in the sequence.
805 * Compares two % subtract_with_carry random number generator objects of
806 * the same type for equality.
808 * @param __lhs A % subtract_with_carry random number generator object.
809 * @param __rhs Another % subtract_with_carry random number generator
812 * @returns true if the two objects are equal, false otherwise.
815 operator==(const subtract_with_carry
& __lhs
,
816 const subtract_with_carry
& __rhs
)
817 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
820 * Compares two % subtract_with_carry random number generator objects of
821 * the same type for inequality.
823 * @param __lhs A % subtract_with_carry random number generator object.
824 * @param __rhs Another % subtract_with_carry random number generator
827 * @returns true if the two objects are not equal, false otherwise.
830 operator!=(const subtract_with_carry
& __lhs
,
831 const subtract_with_carry
& __rhs
)
832 { return !(__lhs
== __rhs
); }
835 * Inserts the current state of a % subtract_with_carry random number
836 * generator engine @p __x into the output stream @p __os.
838 * @param __os An output stream.
839 * @param __x A % subtract_with_carry random number generator engine.
841 * @returns The output stream with the state of @p __x inserted or in
844 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
845 typename _CharT
, typename _Traits
>
846 friend std::basic_ostream
<_CharT
, _Traits
>&
847 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
848 const subtract_with_carry
<_IntType1
, __m1
, __s1
,
852 * Extracts the current state of a % subtract_with_carry random number
853 * generator engine @p __x from the input stream @p __is.
855 * @param __is An input stream.
856 * @param __x A % subtract_with_carry random number generator engine.
858 * @returns The input stream with the state of @p __x extracted or in
861 template<typename _IntType1
, _IntType1 __m1
, int __s1
, int __r1
,
862 typename _CharT
, typename _Traits
>
863 friend std::basic_istream
<_CharT
, _Traits
>&
864 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
865 subtract_with_carry
<_IntType1
, __m1
, __s1
, __r1
>& __x
);
870 seed(_Gen
& __g
, true_type
)
871 { return seed(static_cast<unsigned long>(__g
)); }
875 seed(_Gen
& __g
, false_type
);
877 typedef typename
__gnu_cxx::__add_unsigned
<_IntType
>::__type _UIntType
;
879 _UIntType _M_x
[long_lag
];
886 * @brief The Marsaglia-Zaman generator (floats version).
888 * @var _M_x The state of the generator. This is a ring buffer.
889 * @var _M_carry The carry.
890 * @var _M_p Current index of x(i - r).
891 * @var _M_npows Precomputed negative powers of 2.
893 template<typename _RealType
, int __w
, int __s
, int __r
>
894 class subtract_with_carry_01
897 /** The type of the generated random value. */
898 typedef _RealType result_type
;
901 static const int word_size
= __w
;
902 static const int long_lag
= __r
;
903 static const int short_lag
= __s
;
906 * Constructs a default-initialized % subtract_with_carry_01 random
909 subtract_with_carry_01()
912 _M_initialize_npows();
916 * Constructs an explicitly seeded % subtract_with_carry_01 random number
920 subtract_with_carry_01(unsigned long __value
)
923 _M_initialize_npows();
927 * Constructs a % subtract_with_carry_01 random number generator engine
928 * seeded from the generator function @p __g.
930 * @param __g The seed generator function.
933 subtract_with_carry_01(_Gen
& __g
)
936 _M_initialize_npows();
940 * Seeds the initial state @f$ x_0 @f$ of the random number generator.
943 seed(unsigned long __value
= 19780503);
946 * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01
947 * random number generator.
952 { seed(__g
, typename is_fundamental
<_Gen
>::type()); }
955 * Gets the minimum value of the range of random floats
956 * returned by this generator.
963 * Gets the maximum value of the range of random floats
964 * returned by this generator.
971 * Gets the next random number in the sequence.
977 * Compares two % subtract_with_carry_01 random number generator objects
978 * of the same type for equality.
980 * @param __lhs A % subtract_with_carry_01 random number
982 * @param __rhs Another % subtract_with_carry_01 random number generator
985 * @returns true if the two objects are equal, false otherwise.
988 operator==(const subtract_with_carry_01
& __lhs
,
989 const subtract_with_carry_01
& __rhs
)
991 for (int __i
= 0; __i
< long_lag
; ++__i
)
992 if (!std::equal(__lhs
._M_x
[__i
], __lhs
._M_x
[__i
] + __n
,
999 * Compares two % subtract_with_carry_01 random number generator objects
1000 * of the same type for inequality.
1002 * @param __lhs A % subtract_with_carry_01 random number
1005 * @param __rhs Another % subtract_with_carry_01 random number generator
1008 * @returns true if the two objects are not equal, false otherwise.
1011 operator!=(const subtract_with_carry_01
& __lhs
,
1012 const subtract_with_carry_01
& __rhs
)
1013 { return !(__lhs
== __rhs
); }
1016 * Inserts the current state of a % subtract_with_carry_01 random number
1017 * generator engine @p __x into the output stream @p __os.
1019 * @param __os An output stream.
1020 * @param __x A % subtract_with_carry_01 random number generator engine.
1022 * @returns The output stream with the state of @p __x inserted or in
1025 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
1026 typename _CharT
, typename _Traits
>
1027 friend std::basic_ostream
<_CharT
, _Traits
>&
1028 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1029 const subtract_with_carry_01
<_RealType1
, __w1
, __s1
,
1033 * Extracts the current state of a % subtract_with_carry_01 random number
1034 * generator engine @p __x from the input stream @p __is.
1036 * @param __is An input stream.
1037 * @param __x A % subtract_with_carry_01 random number generator engine.
1039 * @returns The input stream with the state of @p __x extracted or in
1042 template<typename _RealType1
, int __w1
, int __s1
, int __r1
,
1043 typename _CharT
, typename _Traits
>
1044 friend std::basic_istream
<_CharT
, _Traits
>&
1045 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1046 subtract_with_carry_01
<_RealType1
, __w1
, __s1
, __r1
>& __x
);
1049 template<class _Gen
>
1051 seed(_Gen
& __g
, true_type
)
1052 { return seed(static_cast<unsigned long>(__g
)); }
1054 template<class _Gen
>
1056 seed(_Gen
& __g
, false_type
);
1059 _M_initialize_npows();
1061 static const int __n
= (__w
+ 31) / 32;
1063 typedef __detail::_UInt32Type _UInt32Type
;
1064 _UInt32Type _M_x
[long_lag
][__n
];
1065 _RealType _M_npows
[__n
];
1066 _UInt32Type _M_carry
;
1070 typedef subtract_with_carry_01
<float, 24, 10, 24> ranlux_base_01
;
1072 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1073 // 508. Bad parameters for ranlux64_base_01.
1074 typedef subtract_with_carry_01
<double, 48, 5, 12> ranlux64_base_01
;
1078 * Produces random numbers from some base engine by discarding blocks of
1081 * 0 <= @p __r <= @p __p
1083 template<class _UniformRandomNumberGenerator
, int __p
, int __r
>
1086 // __glibcxx_class_requires(typename base_type::result_type,
1087 // ArithmeticTypeConcept)
1090 /** The type of the underlying generator engine. */
1091 typedef _UniformRandomNumberGenerator base_type
;
1092 /** The type of the generated random value. */
1093 typedef typename
base_type::result_type result_type
;
1096 static const int block_size
= __p
;
1097 static const int used_block
= __r
;
1100 * Constructs a default %discard_block engine.
1102 * The underlying engine is default constructed as well.
1108 * Copy constructs a %discard_block engine.
1110 * Copies an existing base class random number generator.
1111 * @param rng An existing (base class) engine object.
1114 discard_block(const base_type
& __rng
)
1115 : _M_b(__rng
), _M_n(0) { }
1118 * Seed constructs a %discard_block engine.
1120 * Constructs the underlying generator engine seeded with @p __s.
1121 * @param __s A seed value for the base class engine.
1124 discard_block(unsigned long __s
)
1125 : _M_b(__s
), _M_n(0) { }
1128 * Generator construct a %discard_block engine.
1130 * @param __g A seed generator function.
1132 template<class _Gen
>
1133 discard_block(_Gen
& __g
)
1134 : _M_b(__g
), _M_n(0) { }
1137 * Reseeds the %discard_block object with the default seed for the
1138 * underlying base class generator engine.
1147 * Reseeds the %discard_block object with the given seed generator
1149 * @param __g A seed generator function.
1151 template<class _Gen
>
1152 void seed(_Gen
& __g
)
1159 * Gets a const reference to the underlying generator engine object.
1166 * Gets the minimum value in the generated random number range.
1170 { return _M_b
.min(); }
1173 * Gets the maximum value in the generated random number range.
1177 { return _M_b
.max(); }
1180 * Gets the next value in the generated random number sequence.
1186 * Compares two %discard_block random number generator objects of
1187 * the same type for equality.
1189 * @param __lhs A %discard_block random number generator object.
1190 * @param __rhs Another %discard_block random number generator
1193 * @returns true if the two objects are equal, false otherwise.
1196 operator==(const discard_block
& __lhs
, const discard_block
& __rhs
)
1197 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
1200 * Compares two %discard_block random number generator objects of
1201 * the same type for inequality.
1203 * @param __lhs A %discard_block random number generator object.
1204 * @param __rhs Another %discard_block random number generator
1207 * @returns true if the two objects are not equal, false otherwise.
1210 operator!=(const discard_block
& __lhs
, const discard_block
& __rhs
)
1211 { return !(__lhs
== __rhs
); }
1214 * Inserts the current state of a %discard_block random number
1215 * generator engine @p __x into the output stream @p __os.
1217 * @param __os An output stream.
1218 * @param __x A %discard_block random number generator engine.
1220 * @returns The output stream with the state of @p __x inserted or in
1223 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1224 typename _CharT
, typename _Traits
>
1225 friend std::basic_ostream
<_CharT
, _Traits
>&
1226 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1227 const discard_block
<_UniformRandomNumberGenerator1
,
1231 * Extracts the current state of a % subtract_with_carry random number
1232 * generator engine @p __x from the input stream @p __is.
1234 * @param __is An input stream.
1235 * @param __x A %discard_block random number generator engine.
1237 * @returns The input stream with the state of @p __x extracted or in
1240 template<class _UniformRandomNumberGenerator1
, int __p1
, int __r1
,
1241 typename _CharT
, typename _Traits
>
1242 friend std::basic_istream
<_CharT
, _Traits
>&
1243 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1244 discard_block
<_UniformRandomNumberGenerator1
,
1254 * James's luxury-level-3 integer adaptation of Luescher's generator.
1256 typedef discard_block
<
1257 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1263 * James's luxury-level-4 integer adaptation of Luescher's generator.
1265 typedef discard_block
<
1266 subtract_with_carry
<unsigned long, (1UL << 24), 10, 24>,
1271 typedef discard_block
<
1272 subtract_with_carry_01
<float, 24, 10, 24>,
1277 typedef discard_block
<
1278 subtract_with_carry_01
<float, 24, 10, 24>,
1285 * A random number generator adaptor class that combines two random number
1286 * generator engines into a single output sequence.
1288 template<class _UniformRandomNumberGenerator1
, int __s1
,
1289 class _UniformRandomNumberGenerator2
, int __s2
>
1292 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1::
1293 // result_type, ArithmeticTypeConcept)
1294 // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2::
1295 // result_type, ArithmeticTypeConcept)
1298 /** The type of the first underlying generator engine. */
1299 typedef _UniformRandomNumberGenerator1 base1_type
;
1300 /** The type of the second underlying generator engine. */
1301 typedef _UniformRandomNumberGenerator2 base2_type
;
1304 typedef typename
base1_type::result_type _Result_type1
;
1305 typedef typename
base2_type::result_type _Result_type2
;
1308 /** The type of the generated random value. */
1309 typedef typename
__gnu_cxx::__conditional_type
<(sizeof(_Result_type1
)
1310 > sizeof(_Result_type2
)),
1311 _Result_type1
, _Result_type2
>::__type result_type
;
1314 static const int shift1
= __s1
;
1315 static const int shift2
= __s2
;
1317 // constructors and member function
1320 { _M_initialize_max(); }
1322 xor_combine(const base1_type
& __rng1
, const base2_type
& __rng2
)
1323 : _M_b1(__rng1
), _M_b2(__rng2
)
1324 { _M_initialize_max(); }
1326 xor_combine(unsigned long __s
)
1327 : _M_b1(__s
), _M_b2(__s
+ 1)
1328 { _M_initialize_max(); }
1330 template<class _Gen
>
1331 xor_combine(_Gen
& __g
)
1332 : _M_b1(__g
), _M_b2(__g
)
1333 { _M_initialize_max(); }
1342 template<class _Gen
>
1367 * Gets the next random number in the sequence.
1369 // NB: Not exactly the TR1 formula, per N2079 instead.
1373 return ((result_type(_M_b1() - _M_b1
.min()) << shift1
)
1374 ^ (result_type(_M_b2() - _M_b2
.min()) << shift2
));
1378 * Compares two %xor_combine random number generator objects of
1379 * the same type for equality.
1381 * @param __lhs A %xor_combine random number generator object.
1382 * @param __rhs Another %xor_combine random number generator
1385 * @returns true if the two objects are equal, false otherwise.
1388 operator==(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1390 return (__lhs
.base1() == __rhs
.base1())
1391 && (__lhs
.base2() == __rhs
.base2());
1395 * Compares two %xor_combine random number generator objects of
1396 * the same type for inequality.
1398 * @param __lhs A %xor_combine random number generator object.
1399 * @param __rhs Another %xor_combine random number generator
1402 * @returns true if the two objects are not equal, false otherwise.
1405 operator!=(const xor_combine
& __lhs
, const xor_combine
& __rhs
)
1406 { return !(__lhs
== __rhs
); }
1409 * Inserts the current state of a %xor_combine random number
1410 * generator engine @p __x into the output stream @p __os.
1412 * @param __os An output stream.
1413 * @param __x A %xor_combine random number generator engine.
1415 * @returns The output stream with the state of @p __x inserted or in
1418 template<class _UniformRandomNumberGenerator11
, int __s11
,
1419 class _UniformRandomNumberGenerator21
, int __s21
,
1420 typename _CharT
, typename _Traits
>
1421 friend std::basic_ostream
<_CharT
, _Traits
>&
1422 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1423 const xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1424 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1427 * Extracts the current state of a %xor_combine random number
1428 * generator engine @p __x from the input stream @p __is.
1430 * @param __is An input stream.
1431 * @param __x A %xor_combine random number generator engine.
1433 * @returns The input stream with the state of @p __x extracted or in
1436 template<class _UniformRandomNumberGenerator11
, int __s11
,
1437 class _UniformRandomNumberGenerator21
, int __s21
,
1438 typename _CharT
, typename _Traits
>
1439 friend std::basic_istream
<_CharT
, _Traits
>&
1440 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1441 xor_combine
<_UniformRandomNumberGenerator11
, __s11
,
1442 _UniformRandomNumberGenerator21
, __s21
>& __x
);
1446 _M_initialize_max();
1449 _M_initialize_max_aux(result_type
, result_type
, int);
1458 * A standard interface to a platform-specific non-deterministic
1459 * random number generator (if any are available).
1465 typedef unsigned int result_type
;
1467 // constructors, destructors and member functions
1469 #ifdef _GLIBCXX_USE_RANDOM_TR1
1472 random_device(const std::string
& __token
= "/dev/urandom")
1474 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1475 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1476 std::__throw_runtime_error(__N("random_device::"
1477 "random_device(const std::string&)"));
1481 { std::fclose(_M_file
); }
1486 random_device(const std::string
& __token
= "mt19937")
1487 : _M_mt(_M_strtoul(__token
)) { }
1490 static unsigned long
1491 _M_strtoul(const std::string
& __str
)
1493 unsigned long __ret
= 5489UL;
1494 if (__str
!= "mt19937")
1496 const char* __nptr
= __str
.c_str();
1498 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1499 if (*__nptr
== '\0' || *__endptr
!= '\0')
1500 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1501 "(const std::string&)"));
1512 { return std::numeric_limits
<result_type
>::min(); }
1516 { return std::numeric_limits
<result_type
>::max(); }
1525 #ifdef _GLIBCXX_USE_RANDOM_TR1
1527 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1536 random_device(const random_device
&);
1537 void operator=(const random_device
&);
1539 #ifdef _GLIBCXX_USE_RANDOM_TR1
1546 /* @} */ // group tr1_random_generators
1549 * @addtogroup tr1_random_distributions Random Number Distributions
1550 * @ingroup tr1_random
1555 * @addtogroup tr1_random_distributions_discrete Discrete Distributions
1556 * @ingroup tr1_random_distributions
1561 * @brief Uniform discrete distribution for random numbers.
1562 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1563 * probability throughout the range.
1565 template<typename _IntType
= int>
1568 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
1571 /** The type of the parameters of the distribution. */
1572 typedef _IntType input_type
;
1573 /** The type of the range of the distribution. */
1574 typedef _IntType result_type
;
1578 * Constructs a uniform distribution object.
1581 uniform_int(_IntType __min
= 0, _IntType __max
= 9)
1582 : _M_min(__min
), _M_max(__max
)
1584 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
1588 * Gets the inclusive lower bound of the distribution range.
1595 * Gets the inclusive upper bound of the distribution range.
1602 * Resets the distribution state.
1604 * Does nothing for the uniform integer distribution.
1610 * Gets a uniformly distributed random number in the range
1613 template<typename _UniformRandomNumberGenerator
>
1615 operator()(_UniformRandomNumberGenerator
& __urng
)
1617 typedef typename
_UniformRandomNumberGenerator::result_type
1619 return _M_call(__urng
, _M_min
, _M_max
,
1620 typename is_integral
<_UResult_type
>::type());
1624 * Gets a uniform random number in the range @f$[0, n)@f$.
1626 * This function is aimed at use with std::random_shuffle.
1628 template<typename _UniformRandomNumberGenerator
>
1630 operator()(_UniformRandomNumberGenerator
& __urng
, result_type __n
)
1632 typedef typename
_UniformRandomNumberGenerator::result_type
1634 return _M_call(__urng
, 0, __n
- 1,
1635 typename is_integral
<_UResult_type
>::type());
1639 * Inserts a %uniform_int random number distribution @p __x into the
1640 * output stream @p os.
1642 * @param __os An output stream.
1643 * @param __x A %uniform_int random number distribution.
1645 * @returns The output stream with the state of @p __x inserted or in
1648 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1649 friend std::basic_ostream
<_CharT
, _Traits
>&
1650 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1651 const uniform_int
<_IntType1
>& __x
);
1654 * Extracts a %uniform_int random number distribution
1655 * @p __x from the input stream @p __is.
1657 * @param __is An input stream.
1658 * @param __x A %uniform_int random number generator engine.
1660 * @returns The input stream with @p __x extracted or in an error state.
1662 template<typename _IntType1
, typename _CharT
, typename _Traits
>
1663 friend std::basic_istream
<_CharT
, _Traits
>&
1664 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1665 uniform_int
<_IntType1
>& __x
);
1668 template<typename _UniformRandomNumberGenerator
>
1670 _M_call(_UniformRandomNumberGenerator
& __urng
,
1671 result_type __min
, result_type __max
, true_type
);
1673 template<typename _UniformRandomNumberGenerator
>
1675 _M_call(_UniformRandomNumberGenerator
& __urng
,
1676 result_type __min
, result_type __max
, false_type
)
1678 return result_type((__urng() - __urng
.min())
1679 / (__urng
.max() - __urng
.min())
1680 * (__max
- __min
+ 1)) + __min
;
1689 * @brief A Bernoulli random number distribution.
1691 * Generates a sequence of true and false values with likelihood @f$ p @f$
1692 * that true will come up and @f$ (1 - p) @f$ that false will appear.
1694 class bernoulli_distribution
1697 typedef int input_type
;
1698 typedef bool result_type
;
1702 * Constructs a Bernoulli distribution with likelihood @p p.
1704 * @param __p [IN] The likelihood of a true result being returned. Must
1705 * be in the interval @f$ [0, 1] @f$.
1708 bernoulli_distribution(double __p
= 0.5)
1711 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
1715 * Gets the @p p parameter of the distribution.
1722 * Resets the distribution state.
1724 * Does nothing for a Bernoulli distribution.
1730 * Gets the next value in the Bernoullian sequence.
1732 template<class _UniformRandomNumberGenerator
>
1734 operator()(_UniformRandomNumberGenerator
& __urng
)
1736 if ((__urng() - __urng
.min()) < _M_p
* (__urng
.max() - __urng
.min()))
1742 * Inserts a %bernoulli_distribution random number distribution
1743 * @p __x into the output stream @p __os.
1745 * @param __os An output stream.
1746 * @param __x A %bernoulli_distribution random number distribution.
1748 * @returns The output stream with the state of @p __x inserted or in
1751 template<typename _CharT
, typename _Traits
>
1752 friend std::basic_ostream
<_CharT
, _Traits
>&
1753 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1754 const bernoulli_distribution
& __x
);
1757 * Extracts a %bernoulli_distribution random number distribution
1758 * @p __x from the input stream @p __is.
1760 * @param __is An input stream.
1761 * @param __x A %bernoulli_distribution random number generator engine.
1763 * @returns The input stream with @p __x extracted or in an error state.
1765 template<typename _CharT
, typename _Traits
>
1766 friend std::basic_istream
<_CharT
, _Traits
>&
1767 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1768 bernoulli_distribution
& __x
)
1769 { return __is
>> __x
._M_p
; }
1777 * @brief A discrete geometric random number distribution.
1779 * The formula for the geometric probability mass function is
1780 * @f$ p(i) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
1783 template<typename _IntType
= int, typename _RealType
= double>
1784 class geometric_distribution
1788 typedef _RealType input_type
;
1789 typedef _IntType result_type
;
1791 // constructors and member function
1793 geometric_distribution(const _RealType
& __p
= _RealType(0.5))
1796 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
1801 * Gets the distribution parameter @p p.
1810 template<class _UniformRandomNumberGenerator
>
1812 operator()(_UniformRandomNumberGenerator
& __urng
);
1815 * Inserts a %geometric_distribution random number distribution
1816 * @p __x into the output stream @p __os.
1818 * @param __os An output stream.
1819 * @param __x A %geometric_distribution random number distribution.
1821 * @returns The output stream with the state of @p __x inserted or in
1824 template<typename _IntType1
, typename _RealType1
,
1825 typename _CharT
, typename _Traits
>
1826 friend std::basic_ostream
<_CharT
, _Traits
>&
1827 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1828 const geometric_distribution
<_IntType1
, _RealType1
>& __x
);
1831 * Extracts a %geometric_distribution random number distribution
1832 * @p __x from the input stream @p __is.
1834 * @param __is An input stream.
1835 * @param __x A %geometric_distribution random number generator engine.
1837 * @returns The input stream with @p __x extracted or in an error state.
1839 template<typename _CharT
, typename _Traits
>
1840 friend std::basic_istream
<_CharT
, _Traits
>&
1841 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1842 geometric_distribution
& __x
)
1845 __x
._M_initialize();
1852 { _M_log_p
= std::log(_M_p
); }
1859 template<typename _RealType
>
1860 class normal_distribution
;
1863 * @brief A discrete Poisson random number distribution.
1865 * The formula for the Poisson probability mass function is
1866 * @f$ p(i) = \frac{mean^i}{i!} e^{-mean} @f$ where @f$ mean @f$ is the
1867 * parameter of the distribution.
1869 template<typename _IntType
= int, typename _RealType
= double>
1870 class poisson_distribution
1874 typedef _RealType input_type
;
1875 typedef _IntType result_type
;
1877 // constructors and member function
1879 poisson_distribution(const _RealType
& __mean
= _RealType(1))
1880 : _M_mean(__mean
), _M_nd()
1882 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
1887 * Gets the distribution parameter @p mean.
1897 template<class _UniformRandomNumberGenerator
>
1899 operator()(_UniformRandomNumberGenerator
& __urng
);
1902 * Inserts a %poisson_distribution random number distribution
1903 * @p __x into the output stream @p __os.
1905 * @param __os An output stream.
1906 * @param __x A %poisson_distribution random number distribution.
1908 * @returns The output stream with the state of @p __x inserted or in
1911 template<typename _IntType1
, typename _RealType1
,
1912 typename _CharT
, typename _Traits
>
1913 friend std::basic_ostream
<_CharT
, _Traits
>&
1914 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1915 const poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1918 * Extracts a %poisson_distribution random number distribution
1919 * @p __x from the input stream @p __is.
1921 * @param __is An input stream.
1922 * @param __x A %poisson_distribution random number generator engine.
1924 * @returns The input stream with @p __x extracted or in an error state.
1926 template<typename _IntType1
, typename _RealType1
,
1927 typename _CharT
, typename _Traits
>
1928 friend std::basic_istream
<_CharT
, _Traits
>&
1929 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1930 poisson_distribution
<_IntType1
, _RealType1
>& __x
);
1936 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
1937 normal_distribution
<_RealType
> _M_nd
;
1941 // Hosts either log(mean) or the threshold of the simple method.
1942 _RealType _M_lm_thr
;
1943 #if _GLIBCXX_USE_C99_MATH_TR1
1944 _RealType _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
1950 * @brief A discrete binomial random number distribution.
1952 * The formula for the binomial probability mass function is
1953 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
1954 * and @f$ p @f$ are the parameters of the distribution.
1956 template<typename _IntType
= int, typename _RealType
= double>
1957 class binomial_distribution
1961 typedef _RealType input_type
;
1962 typedef _IntType result_type
;
1964 // constructors and member function
1966 binomial_distribution(_IntType __t
= 1,
1967 const _RealType
& __p
= _RealType(0.5))
1968 : _M_t(__t
), _M_p(__p
), _M_nd()
1970 _GLIBCXX_DEBUG_ASSERT((_M_t
>= 0) && (_M_p
>= 0.0) && (_M_p
<= 1.0));
1975 * Gets the distribution @p t parameter.
1982 * Gets the distribution @p p parameter.
1992 template<class _UniformRandomNumberGenerator
>
1994 operator()(_UniformRandomNumberGenerator
& __urng
);
1997 * Inserts a %binomial_distribution random number distribution
1998 * @p __x into the output stream @p __os.
2000 * @param __os An output stream.
2001 * @param __x A %binomial_distribution random number distribution.
2003 * @returns The output stream with the state of @p __x inserted or in
2006 template<typename _IntType1
, typename _RealType1
,
2007 typename _CharT
, typename _Traits
>
2008 friend std::basic_ostream
<_CharT
, _Traits
>&
2009 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2010 const binomial_distribution
<_IntType1
, _RealType1
>& __x
);
2013 * Extracts a %binomial_distribution random number distribution
2014 * @p __x from the input stream @p __is.
2016 * @param __is An input stream.
2017 * @param __x A %binomial_distribution random number generator engine.
2019 * @returns The input stream with @p __x extracted or in an error state.
2021 template<typename _IntType1
, typename _RealType1
,
2022 typename _CharT
, typename _Traits
>
2023 friend std::basic_istream
<_CharT
, _Traits
>&
2024 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2025 binomial_distribution
<_IntType1
, _RealType1
>& __x
);
2031 template<class _UniformRandomNumberGenerator
>
2033 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
2035 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
2036 normal_distribution
<_RealType
> _M_nd
;
2039 #if _GLIBCXX_USE_C99_MATH_TR1
2040 _RealType _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
2041 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
2049 /* @} */ // group tr1_random_distributions_discrete
2052 * @addtogroup tr1_random_distributions_continuous Continuous Distributions
2053 * @ingroup tr1_random_distributions
2058 * @brief Uniform continuous distribution for random numbers.
2060 * A continuous random distribution on the range [min, max) with equal
2061 * probability throughout the range. The URNG should be real-valued and
2062 * deliver number in the range [0, 1).
2064 template<typename _RealType
= double>
2069 typedef _RealType input_type
;
2070 typedef _RealType result_type
;
2074 * Constructs a uniform_real object.
2076 * @param __min [IN] The lower bound of the distribution.
2077 * @param __max [IN] The upper bound of the distribution.
2080 uniform_real(_RealType __min
= _RealType(0),
2081 _RealType __max
= _RealType(1))
2082 : _M_min(__min
), _M_max(__max
)
2084 _GLIBCXX_DEBUG_ASSERT(_M_min
<= _M_max
);
2098 template<class _UniformRandomNumberGenerator
>
2100 operator()(_UniformRandomNumberGenerator
& __urng
)
2101 { return (__urng() * (_M_max
- _M_min
)) + _M_min
; }
2104 * Inserts a %uniform_real random number distribution @p __x into the
2105 * output stream @p __os.
2107 * @param __os An output stream.
2108 * @param __x A %uniform_real random number distribution.
2110 * @returns The output stream with the state of @p __x inserted or in
2113 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2114 friend std::basic_ostream
<_CharT
, _Traits
>&
2115 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2116 const uniform_real
<_RealType1
>& __x
);
2119 * Extracts a %uniform_real random number distribution
2120 * @p __x from the input stream @p __is.
2122 * @param __is An input stream.
2123 * @param __x A %uniform_real random number generator engine.
2125 * @returns The input stream with @p __x extracted or in an error state.
2127 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2128 friend std::basic_istream
<_CharT
, _Traits
>&
2129 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2130 uniform_real
<_RealType1
>& __x
);
2139 * @brief An exponential continuous distribution for random numbers.
2141 * The formula for the exponential probability mass function is
2142 * @f$ p(x) = \lambda e^{-\lambda x} @f$.
2144 * <table border=1 cellpadding=10 cellspacing=0>
2145 * <caption align=top>Distribution Statistics</caption>
2146 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2147 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
2148 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
2149 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
2150 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
2153 template<typename _RealType
= double>
2154 class exponential_distribution
2158 typedef _RealType input_type
;
2159 typedef _RealType result_type
;
2163 * Constructs an exponential distribution with inverse scale parameter
2167 exponential_distribution(const result_type
& __lambda
= result_type(1))
2168 : _M_lambda(__lambda
)
2170 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> 0);
2174 * Gets the inverse scale parameter of the distribution.
2178 { return _M_lambda
; }
2181 * Resets the distribution.
2183 * Has no effect on exponential distributions.
2188 template<class _UniformRandomNumberGenerator
>
2190 operator()(_UniformRandomNumberGenerator
& __urng
)
2191 { return -std::log(__urng()) / _M_lambda
; }
2194 * Inserts a %exponential_distribution random number distribution
2195 * @p __x into the output stream @p __os.
2197 * @param __os An output stream.
2198 * @param __x A %exponential_distribution random number distribution.
2200 * @returns The output stream with the state of @p __x inserted or in
2203 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2204 friend std::basic_ostream
<_CharT
, _Traits
>&
2205 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2206 const exponential_distribution
<_RealType1
>& __x
);
2209 * Extracts a %exponential_distribution random number distribution
2210 * @p __x from the input stream @p __is.
2212 * @param __is An input stream.
2213 * @param __x A %exponential_distribution random number
2216 * @returns The input stream with @p __x extracted or in an error state.
2218 template<typename _CharT
, typename _Traits
>
2219 friend std::basic_istream
<_CharT
, _Traits
>&
2220 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2221 exponential_distribution
& __x
)
2222 { return __is
>> __x
._M_lambda
; }
2225 result_type _M_lambda
;
2230 * @brief A normal continuous distribution for random numbers.
2232 * The formula for the normal probability mass function is
2233 * @f$ p(x) = \frac{1}{\sigma \sqrt{2 \pi}}
2234 * e^{- \frac{{x - mean}^ {2}}{2 \sigma ^ {2}} } @f$.
2236 template<typename _RealType
= double>
2237 class normal_distribution
2241 typedef _RealType input_type
;
2242 typedef _RealType result_type
;
2246 * Constructs a normal distribution with parameters @f$ mean @f$ and
2250 normal_distribution(const result_type
& __mean
= result_type(0),
2251 const result_type
& __sigma
= result_type(1))
2252 : _M_mean(__mean
), _M_sigma(__sigma
), _M_saved_available(false)
2254 _GLIBCXX_DEBUG_ASSERT(_M_sigma
> 0);
2258 * Gets the mean of the distribution.
2265 * Gets the @f$ \sigma @f$ of the distribution.
2269 { return _M_sigma
; }
2272 * Resets the distribution.
2276 { _M_saved_available
= false; }
2278 template<class _UniformRandomNumberGenerator
>
2280 operator()(_UniformRandomNumberGenerator
& __urng
);
2283 * Inserts a %normal_distribution random number distribution
2284 * @p __x into the output stream @p __os.
2286 * @param __os An output stream.
2287 * @param __x A %normal_distribution random number distribution.
2289 * @returns The output stream with the state of @p __x inserted or in
2292 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2293 friend std::basic_ostream
<_CharT
, _Traits
>&
2294 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2295 const normal_distribution
<_RealType1
>& __x
);
2298 * Extracts a %normal_distribution random number distribution
2299 * @p __x from the input stream @p __is.
2301 * @param __is An input stream.
2302 * @param __x A %normal_distribution random number generator engine.
2304 * @returns The input stream with @p __x extracted or in an error state.
2306 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2307 friend std::basic_istream
<_CharT
, _Traits
>&
2308 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2309 normal_distribution
<_RealType1
>& __x
);
2312 result_type _M_mean
;
2313 result_type _M_sigma
;
2314 result_type _M_saved
;
2315 bool _M_saved_available
;
2320 * @brief A gamma continuous distribution for random numbers.
2322 * The formula for the gamma probability mass function is
2323 * @f$ p(x) = \frac{1}{\Gamma(\alpha)} x^{\alpha - 1} e^{-x} @f$.
2325 template<typename _RealType
= double>
2326 class gamma_distribution
2330 typedef _RealType input_type
;
2331 typedef _RealType result_type
;
2335 * Constructs a gamma distribution with parameters @f$ \alpha @f$.
2338 gamma_distribution(const result_type
& __alpha_val
= result_type(1))
2339 : _M_alpha(__alpha_val
)
2341 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> 0);
2346 * Gets the @f$ \alpha @f$ of the distribution.
2350 { return _M_alpha
; }
2353 * Resets the distribution.
2358 template<class _UniformRandomNumberGenerator
>
2360 operator()(_UniformRandomNumberGenerator
& __urng
);
2363 * Inserts a %gamma_distribution random number distribution
2364 * @p __x into the output stream @p __os.
2366 * @param __os An output stream.
2367 * @param __x A %gamma_distribution random number distribution.
2369 * @returns The output stream with the state of @p __x inserted or in
2372 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2373 friend std::basic_ostream
<_CharT
, _Traits
>&
2374 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
2375 const gamma_distribution
<_RealType1
>& __x
);
2378 * Extracts a %gamma_distribution random number distribution
2379 * @p __x from the input stream @p __is.
2381 * @param __is An input stream.
2382 * @param __x A %gamma_distribution random number generator engine.
2384 * @returns The input stream with @p __x extracted or in an error state.
2386 template<typename _CharT
, typename _Traits
>
2387 friend std::basic_istream
<_CharT
, _Traits
>&
2388 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2389 gamma_distribution
& __x
)
2391 __is
>> __x
._M_alpha
;
2392 __x
._M_initialize();
2400 result_type _M_alpha
;
2402 // Hosts either lambda of GB or d of modified Vaduva's.
2406 /* @} */ // group tr1_random_distributions_continuous
2407 /* @} */ // group tr1_random_distributions
2408 /* @} */ // group tr1_random
2411 _GLIBCXX_END_NAMESPACE_VERSION
2414 #endif // _GLIBCXX_TR1_RANDOM_H