1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
41 // [26.4] Random number generation
44 * @addtogroup std_random Random Number Generation
45 * A facility for generating random numbers on selected distributions.
50 * @brief A function template for converting the output of a (integral)
51 * uniform random number generator to a floatng point result in the range
54 template<typename _RealType
, size_t __bits
,
55 typename _UniformRandomNumberGenerator
>
57 generate_canonical(_UniformRandomNumberGenerator
& __g
);
62 * Implementation-space details.
66 template<typename _UIntType
, size_t __w
,
67 bool = __w
< static_cast<size_t>
68 (std::numeric_limits
<_UIntType
>::digits
)>
70 { static const _UIntType __value
= 0; };
72 template<typename _UIntType
, size_t __w
>
73 struct _Shift
<_UIntType
, __w
, true>
74 { static const _UIntType __value
= _UIntType(1) << __w
; };
77 template<typename _UIntType
, size_t __w
,
78 bool = __w
<static_cast<size_t>
79 (std::numeric_limits
<_UIntType
>::digits
)>
82 static const _UIntType __value
=
83 __gnu_cxx::__numeric_traits
<_UIntType
>::__max
;
86 template<typename _UIntType
, size_t __w
>
87 struct _ShiftMin1
<_UIntType
, __w
, true>
89 static const _UIntType __value
=
90 (_UIntType(1) << __w
) - _UIntType(1);
93 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
, bool>
96 // Dispatch based on modulus value to prevent divide-by-zero compile-time
97 // errors when m == 0.
98 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
>
101 { return _Mod
<_Tp
, __a
, __c
, __m
, __m
== 0>::__calc(__x
); }
103 typedef __gnu_cxx::__conditional_type
<(sizeof(unsigned) == 4),
104 unsigned, unsigned long>::__type _UInt32Type
;
107 * An adaptor class for converting the output of any Generator into
108 * the input for a specific Distribution.
110 template<typename _Engine
, typename _DInputType
>
115 _Adaptor(_Engine
& __g
)
121 if (is_integral
<_DInputType
>::value
)
124 return _DInputType(0);
130 if (is_integral
<_DInputType
>::value
)
133 return _DInputType(1);
137 * Converts a value generated by the adapted random number generator
138 * into a value in the input domain for the dependent random number
141 * Because the type traits are compile time constants only the
142 * appropriate clause of the if statements will actually be emitted
148 if (is_integral
<_DInputType
>::value
)
151 return generate_canonical
<_DInputType
,
152 numeric_limits
<_DInputType
>::digits
,
159 } // namespace __detail
162 * @addtogroup std_random_generators Random Number Generators
163 * @ingroup std_random
165 * These classes define objects which provide random or pseudorandom
166 * numbers, either from a discrete or a continuous interval. The
167 * random number generator supplied as a part of this library are
168 * all uniform random number generators which provide a sequence of
169 * random number uniformly distributed over their range.
171 * A number generator is a function object with an operator() that
172 * takes zero arguments and returns a number.
174 * A compliant random number generator must satisfy the following
175 * requirements. <table border=1 cellpadding=10 cellspacing=0>
176 * <caption align=top>Random Number Generator Requirements</caption>
177 * <tr><td>To be documented.</td></tr> </table>
183 * @brief A model of a linear congruential random number generator.
185 * A random number generator that produces pseudorandom numbers using the
186 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
188 * The template parameter @p _UIntType must be an unsigned integral type
189 * large enough to store values up to (__m-1). If the template parameter
190 * @p __m is 0, the modulus @p __m used is
191 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
192 * parameters @p __a and @p __c must be less than @p __m.
194 * The size of the state is @f$ 1 @f$.
196 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
197 class linear_congruential_engine
199 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
200 static_assert(__m
== 0 || (__a
< __m
&& __c
< __m
),
201 "template arguments out of bounds"
202 " in linear_congruential_engine");
205 /** The type of the generated random value. */
206 typedef _UIntType result_type
;
208 /** The multiplier. */
209 static const result_type multiplier
= __a
;
211 static const result_type increment
= __c
;
213 static const result_type modulus
= __m
;
214 static const result_type default_seed
= 1UL;
217 * @brief Constructs a %linear_congruential_engine random number
218 * generator engine with seed @p __s. The default seed value
221 * @param __s The initial seed value.
224 linear_congruential_engine(result_type __s
= default_seed
)
228 * @brief Constructs a %linear_congruential_engine random number
229 * generator engine seeded from the seed sequence @p __q.
231 * @param __q the seed sequence.
234 linear_congruential_engine(seed_seq
& __q
)
238 * @brief Reseeds the %linear_congruential_engine random number generator
239 * engine sequence to the seed @p __s.
241 * @param __s The new seed.
244 seed(result_type __s
= default_seed
);
247 * @brief Reseeds the %linear_congruential_engine random number generator
249 * sequence using values from the seed sequence @p __q.
251 * @param __q the seed sequence.
257 * @brief Gets the smallest possible value in the output range.
259 * The minimum depends on the @p __c parameter: if it is zero, the
260 * minimum generated must be > 0, otherwise 0 is allowed.
262 * @todo This should be constexpr.
266 { return (__detail::__mod
<_UIntType
, 1, 0, __m
>(__c
) == 0) ? 1 : 0; }
269 * @brief Gets the largest possible value in the output range.
271 * @todo This should be constexpr.
278 * @brief Discard a sequence of random numbers.
280 * @todo Look for a faster way to do discard.
283 discard(unsigned long long __z
)
285 for (; __z
!= 0ULL; --__z
)
290 * @brief Gets the next random number in the sequence.
296 * @brief Compares two linear congruential random number generator
297 * objects of the same type for equality.
299 * @param __lhs A linear congruential random number generator object.
300 * @param __rhs Another linear congruential random number generator
303 * @returns true if the two objects are equal, false otherwise.
306 operator==(const linear_congruential_engine
& __lhs
,
307 const linear_congruential_engine
& __rhs
)
308 { return __lhs
._M_x
== __rhs
._M_x
; }
311 * @brief Writes the textual representation of the state x(i) of x to
314 * @param __os The output stream.
315 * @param __lcr A % linear_congruential_engine random number generator.
318 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
320 typename _CharT
, typename _Traits
>
321 friend std::basic_ostream
<_CharT
, _Traits
>&
322 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
323 const std::linear_congruential_engine
<_UIntType1
,
327 * @brief Sets the state of the engine by reading its textual
328 * representation from @p __is.
330 * The textual representation must have been previously written using
331 * an output stream whose imbued locale and whose type's template
332 * specialization arguments _CharT and _Traits were the same as those
335 * @param __is The input stream.
336 * @param __lcr A % linear_congruential_engine random number generator.
339 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
341 typename _CharT
, typename _Traits
>
342 friend std::basic_istream
<_CharT
, _Traits
>&
343 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
344 std::linear_congruential_engine
<_UIntType1
, __a1
,
348 template<typename _Gen
>
350 seed(_Gen
& __g
, true_type
)
351 { return seed(static_cast<unsigned long>(__g
)); }
353 template<typename _Gen
>
355 seed(_Gen
& __g
, false_type
);
362 * A generalized feedback shift register discrete random number generator.
364 * This algorithm avoids multiplication and division and is designed to be
365 * friendly to a pipelined architecture. If the parameters are chosen
366 * correctly, this generator will produce numbers with a very long period and
367 * fairly good apparent entropy, although still not cryptographically strong.
369 * The best way to use this generator is with the predefined mt19937 class.
371 * This algorithm was originally invented by Makoto Matsumoto and
374 * @var word_size The number of bits in each element of the state vector.
375 * @var state_size The degree of recursion.
376 * @var shift_size The period parameter.
377 * @var mask_bits The separation point bit index.
378 * @var parameter_a The last row of the twist matrix.
379 * @var output_u The first right-shift tempering matrix parameter.
380 * @var output_s The first left-shift tempering matrix parameter.
381 * @var output_b The first left-shift tempering matrix mask.
382 * @var output_t The second left-shift tempering matrix parameter.
383 * @var output_c The second left-shift tempering matrix mask.
384 * @var output_l The second right-shift tempering matrix parameter.
386 template<typename _UIntType
, size_t __w
,
387 size_t __n
, size_t __m
, size_t __r
,
388 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
389 _UIntType __b
, size_t __t
,
390 _UIntType __c
, size_t __l
, _UIntType __f
>
391 class mersenne_twister_engine
393 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
395 static_assert(__m
>= 1U,
396 "mersenne_twister_engine template arguments out of bounds");
397 static_assert(__n
>= __m
,
398 "mersenne_twister_engine template arguments out of bounds");
399 static_assert(__w
>= __r
,
400 "mersenne_twister_engine template arguments out of bounds");
401 static_assert(__w
>= __u
,
402 "mersenne_twister_engine template arguments out of bounds");
403 static_assert(__w
>= __s
,
404 "mersenne_twister_engine template arguments out of bounds");
405 static_assert(__w
>= __t
,
406 "mersenne_twister_engine template arguments out of bounds");
407 static_assert(__w
>= __l
,
408 "mersenne_twister_engine template arguments out of bounds");
410 static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
411 "mersenne_twister_engine template arguments out of bounds");
412 static_assert(__a
<= __detail::_ShiftMin1
<_UIntType
, __w
>::__value
,
413 "mersenne_twister_engine template arguments out of bounds");
414 static_assert(__b
<= __detail::_ShiftMin1
<_UIntType
, __w
>::__value
,
415 "mersenne_twister_engine template arguments out of bounds");
416 static_assert(__c
<= __detail::_ShiftMin1
<_UIntType
, __w
>::__value
,
417 "mersenne_twister_engine template arguments out of bounds");
420 /** The type of the generated random value. */
421 typedef _UIntType result_type
;
424 static const size_t word_size
= __w
;
425 static const size_t state_size
= __n
;
426 static const size_t shift_size
= __m
;
427 static const size_t mask_bits
= __r
;
428 static const result_type xor_mask
= __a
;
429 static const size_t tempering_u
= __u
;
430 static const result_type tempering_d
= __d
;
431 static const size_t tempering_s
= __s
;
432 static const result_type tempering_b
= __b
;
433 static const size_t tempering_t
= __t
;
434 static const result_type tempering_c
= __c
;
435 static const size_t tempering_l
= __l
;
436 static const size_t initialization_multiplier
= __f
;
437 static const result_type default_seed
= 5489UL;
439 // constructors and member function
441 mersenne_twister_engine(result_type __sd
= default_seed
)
445 * @brief Constructs a %mersenne_twister_engine random number generator
446 * engine seeded from the seed sequence @p __q.
448 * @param __q the seed sequence.
451 mersenne_twister_engine(seed_seq
& __q
)
455 seed(result_type __sd
= default_seed
);
461 * @brief Gets the smallest possible value in the output range.
463 * @todo This should be constexpr.
470 * @brief Gets the largest possible value in the output range.
472 * @todo This should be constexpr.
476 { return __detail::_ShiftMin1
<_UIntType
, __w
>::__value
; }
479 * @brief Discard a sequence of random numbers.
481 * @todo Look for a faster way to do discard.
484 discard(unsigned long long __z
)
486 for (; __z
!= 0ULL; --__z
)
494 * @brief Compares two % mersenne_twister_engine random number generator
495 * objects of the same type for equality.
497 * @param __lhs A % mersenne_twister_engine random number generator
499 * @param __rhs Another % mersenne_twister_engine random number
502 * @returns true if the two objects are equal, false otherwise.
505 operator==(const mersenne_twister_engine
& __lhs
,
506 const mersenne_twister_engine
& __rhs
)
507 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
510 * @brief Inserts the current state of a % mersenne_twister_engine
511 * random number generator engine @p __x into the output stream
514 * @param __os An output stream.
515 * @param __x A % mersenne_twister_engine random number generator
518 * @returns The output stream with the state of @p __x inserted or in
521 template<typename _UIntType1
,
522 size_t __w1
, size_t __n1
,
523 size_t __m1
, size_t __r1
,
524 _UIntType1 __a1
, size_t __u1
,
525 _UIntType1 __d1
, size_t __s1
,
526 _UIntType1 __b1
, size_t __t1
,
527 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
528 typename _CharT
, typename _Traits
>
529 friend std::basic_ostream
<_CharT
, _Traits
>&
530 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
531 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
532 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
536 * @brief Extracts the current state of a % mersenne_twister_engine
537 * random number generator engine @p __x from the input stream
540 * @param __is An input stream.
541 * @param __x A % mersenne_twister_engine random number generator
544 * @returns The input stream with the state of @p __x extracted or in
547 template<typename _UIntType1
,
548 size_t __w1
, size_t __n1
,
549 size_t __m1
, size_t __r1
,
550 _UIntType1 __a1
, size_t __u1
,
551 _UIntType1 __d1
, size_t __s1
,
552 _UIntType1 __b1
, size_t __t1
,
553 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
554 typename _CharT
, typename _Traits
>
555 friend std::basic_istream
<_CharT
, _Traits
>&
556 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
557 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
558 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
562 template<typename _Gen
>
564 seed(_Gen
& __g
, true_type
)
565 { return seed(static_cast<unsigned long>(__g
)); }
567 template<typename _Gen
>
569 seed(_Gen
& __g
, false_type
);
571 _UIntType _M_x
[state_size
];
576 * @brief The Marsaglia-Zaman generator.
578 * This is a model of a Generalized Fibonacci discrete random number
579 * generator, sometimes referred to as the SWC generator.
581 * A discrete random number generator that produces pseudorandom
582 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
583 * carry_{i-1}) \bmod m @f$.
585 * The size of the state is @f$ r @f$
586 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
588 * @var _M_x The state of the generator. This is a ring buffer.
589 * @var _M_carry The carry.
590 * @var _M_p Current index of x(i - r).
592 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
593 class subtract_with_carry_engine
595 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
596 static_assert(__s
> 0U && __r
> __s
598 && __w
<= static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
599 "template arguments out of bounds"
600 " in subtract_with_carry_engine");
603 /** The type of the generated random value. */
604 typedef _UIntType result_type
;
607 static const size_t word_size
= __w
;
608 static const size_t short_lag
= __s
;
609 static const size_t long_lag
= __r
;
610 static const result_type default_seed
= 19780503;
613 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
614 * random number generator.
617 subtract_with_carry_engine(result_type __sd
= default_seed
)
618 { this->seed(__sd
); }
621 * @brief Constructs a %subtract_with_carry_engine random number engine
622 * seeded from the seed sequence @p __q.
624 * @param __q the seed sequence.
627 subtract_with_carry_engine(seed_seq
& __q
)
631 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
634 * N1688[4.19] modifies this as follows. If @p __value == 0,
635 * sets value to 19780503. In any case, with a linear
636 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
637 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
638 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
639 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
640 * set carry to 1, otherwise sets carry to 0.
643 seed(result_type __sd
= default_seed
);
646 * @brief Seeds the initial state @f$ x_0 @f$ of the
647 * % subtract_with_carry_engine random number generator.
653 * @brief Gets the inclusive minimum value of the range of random
654 * integers returned by this generator.
656 * @todo This should be constexpr.
663 * @brief Gets the inclusive maximum value of the range of random
664 * integers returned by this generator.
666 * @todo This should be constexpr.
670 { return _S_modulus
- 1U; }
673 * @brief Discard a sequence of random numbers.
675 * @todo Look for a faster way to do discard.
678 discard(unsigned long long __z
)
680 for (; __z
!= 0ULL; --__z
)
685 * @brief Gets the next random number in the sequence.
691 * @brief Compares two % subtract_with_carry_engine random number
692 * generator objects of the same type for equality.
694 * @param __lhs A % subtract_with_carry_engine random number generator
696 * @param __rhs Another % subtract_with_carry_engine random number
699 * @returns true if the two objects are equal, false otherwise.
702 operator==(const subtract_with_carry_engine
& __lhs
,
703 const subtract_with_carry_engine
& __rhs
)
704 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
707 * @brief Inserts the current state of a % subtract_with_carry_engine
708 * random number generator engine @p __x into the output stream
711 * @param __os An output stream.
712 * @param __x A % subtract_with_carry_engine random number generator
715 * @returns The output stream with the state of @p __x inserted or in
718 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
719 typename _CharT
, typename _Traits
>
720 friend std::basic_ostream
<_CharT
, _Traits
>&
721 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
722 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
726 * @brief Extracts the current state of a % subtract_with_carry_engine
727 * random number generator engine @p __x from the input stream
730 * @param __is An input stream.
731 * @param __x A % subtract_with_carry_engine random number generator engine.
733 * @returns The input stream with the state of @p __x extracted or in
736 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
737 typename _CharT
, typename _Traits
>
738 friend std::basic_istream
<_CharT
, _Traits
>&
739 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
740 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
744 template<typename _Gen
>
746 seed(_Gen
& __g
, true_type
)
747 { return seed(static_cast<unsigned long>(__g
)); }
749 template<typename _Gen
>
751 seed(_Gen
& __g
, false_type
);
753 static const size_t _S_modulus
754 = __detail::_Shift
<_UIntType
, __w
>::__value
;
756 _UIntType _M_x
[long_lag
];
762 * Produces random numbers from some base engine by discarding blocks of
765 * 0 <= @p __r <= @p __p
767 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
768 class discard_block_engine
770 static_assert(__r
>= 1U && __p
>= __r
,
771 "template arguments out of bounds"
772 " in discard_block_engine");
775 /** The type of the generated random value. */
776 typedef typename
_RandomNumberEngine::result_type result_type
;
779 static const size_t block_size
= __p
;
780 static const size_t used_block
= __r
;
783 * @brief Constructs a default %discard_block_engine engine.
785 * The underlying engine is default constructed as well.
787 discard_block_engine()
788 : _M_b(), _M_n(0) { }
791 * @brief Copy constructs a %discard_block_engine engine.
793 * Copies an existing base class random number generator.
794 * @param rng An existing (base class) engine object.
797 discard_block_engine(const _RandomNumberEngine
& __rne
)
798 : _M_b(__rne
), _M_n(0) { }
801 * @brief Move constructs a %discard_block_engine engine.
803 * Copies an existing base class random number generator.
804 * @param rng An existing (base class) engine object.
807 discard_block_engine(_RandomNumberEngine
&& __rne
)
808 : _M_b(std::move(__rne
)), _M_n(0) { }
811 * @brief Seed constructs a %discard_block_engine engine.
813 * Constructs the underlying generator engine seeded with @p __s.
814 * @param __s A seed value for the base class engine.
817 discard_block_engine(result_type __s
)
818 : _M_b(__s
), _M_n(0) { }
821 * @brief Generator construct a %discard_block_engine engine.
823 * @param __q A seed sequence.
826 discard_block_engine(seed_seq
& __q
)
831 * @brief Reseeds the %discard_block_engine object with the default
832 * seed for the underlying base class generator engine.
842 * @brief Reseeds the %discard_block_engine object with the default
843 * seed for the underlying base class generator engine.
846 seed(result_type __s
)
853 * @brief Reseeds the %discard_block_engine object with the given seed
855 * @param __q A seed generator function.
865 * @brief Gets a const reference to the underlying generator engine
868 const _RandomNumberEngine
&
873 * @brief Gets the minimum value in the generated random number range.
875 * @todo This should be constexpr.
879 { return _M_b
.min(); }
882 * @brief Gets the maximum value in the generated random number range.
884 * @todo This should be constexpr.
888 { return _M_b
.max(); }
891 * @brief Discard a sequence of random numbers.
893 * @todo Look for a faster way to do discard.
896 discard(unsigned long long __z
)
898 for (; __z
!= 0ULL; --__z
)
903 * @brief Gets the next value in the generated random number sequence.
909 * @brief Compares two %discard_block_engine random number generator
910 * objects of the same type for equality.
912 * @param __lhs A %discard_block_engine random number generator object.
913 * @param __rhs Another %discard_block_engine random number generator
916 * @returns true if the two objects are equal, false otherwise.
919 operator==(const discard_block_engine
& __lhs
,
920 const discard_block_engine
& __rhs
)
921 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
924 * @brief Inserts the current state of a %discard_block_engine random
925 * number generator engine @p __x into the output stream
928 * @param __os An output stream.
929 * @param __x A %discard_block_engine random number generator engine.
931 * @returns The output stream with the state of @p __x inserted or in
934 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
935 typename _CharT
, typename _Traits
>
936 friend std::basic_ostream
<_CharT
, _Traits
>&
937 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
938 const std::discard_block_engine
<_RandomNumberEngine1
,
942 * @brief Extracts the current state of a % subtract_with_carry_engine
943 * random number generator engine @p __x from the input stream
946 * @param __is An input stream.
947 * @param __x A %discard_block_engine random number generator engine.
949 * @returns The input stream with the state of @p __x extracted or in
952 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
953 typename _CharT
, typename _Traits
>
954 friend std::basic_istream
<_CharT
, _Traits
>&
955 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
956 std::discard_block_engine
<_RandomNumberEngine1
,
960 _RandomNumberEngine _M_b
;
965 * Produces random numbers by combining random numbers from some base
966 * engine to produce random numbers with a specifies number of bits @p __w.
968 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
969 class independent_bits_engine
971 static_assert(__w
> 0U
973 static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
974 "template arguments out of bounds "
975 "in independent_bits_engine");
978 /** The type of the generated random value. */
979 typedef _UIntType result_type
;
982 * @brief Constructs a default %independent_bits_engine engine.
984 * The underlying engine is default constructed as well.
986 independent_bits_engine()
990 * @brief Copy constructs a %independent_bits_engine engine.
992 * Copies an existing base class random number generator.
993 * @param rng An existing (base class) engine object.
996 independent_bits_engine(const _RandomNumberEngine
& __rne
)
1000 * @brief Move constructs a %independent_bits_engine engine.
1002 * Copies an existing base class random number generator.
1003 * @param rng An existing (base class) engine object.
1006 independent_bits_engine(_RandomNumberEngine
&& __rne
)
1007 : _M_b(std::move(__rne
)) { }
1010 * @brief Seed constructs a %independent_bits_engine engine.
1012 * Constructs the underlying generator engine seeded with @p __s.
1013 * @param __s A seed value for the base class engine.
1016 independent_bits_engine(result_type __s
)
1020 * @brief Generator construct a %independent_bits_engine engine.
1022 * @param __q A seed sequence.
1025 independent_bits_engine(seed_seq
& __q
)
1030 * @brief Reseeds the %independent_bits_engine object with the default
1031 * seed for the underlying base class generator engine.
1038 * @brief Reseeds the %independent_bits_engine object with the default
1039 * seed for the underlying base class generator engine.
1042 seed(result_type __s
)
1046 * @brief Reseeds the %independent_bits_engine object with the given
1048 * @param __q A seed generator function.
1055 * @brief Gets a const reference to the underlying generator engine
1058 const _RandomNumberEngine
&
1063 * @brief Gets the minimum value in the generated random number range.
1065 * @todo This should be constexpr.
1072 * @brief Gets the maximum value in the generated random number range.
1074 * @todo This should be constexpr.
1078 { return __detail::_ShiftMin1
<_UIntType
, __w
>::__value
; }
1081 * @brief Discard a sequence of random numbers.
1083 * @todo Look for a faster way to do discard.
1086 discard(unsigned long long __z
)
1088 for (; __z
!= 0ULL; --__z
)
1093 * @brief Gets the next value in the generated random number sequence.
1099 * @brief Compares two %independent_bits_engine random number generator
1100 * objects of the same type for equality.
1102 * @param __lhs A %independent_bits_engine random number generator
1104 * @param __rhs Another %independent_bits_engine random number generator
1107 * @returns true if the two objects are equal, false otherwise.
1110 operator==(const independent_bits_engine
& __lhs
,
1111 const independent_bits_engine
& __rhs
)
1112 { return __lhs
._M_b
== __rhs
._M_b
; }
1115 * @brief Extracts the current state of a % subtract_with_carry_engine
1116 * random number generator engine @p __x from the input stream
1119 * @param __is An input stream.
1120 * @param __x A %independent_bits_engine random number generator
1123 * @returns The input stream with the state of @p __x extracted or in
1126 template<typename _CharT
, typename _Traits
>
1127 friend std::basic_istream
<_CharT
, _Traits
>&
1128 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1129 std::independent_bits_engine
<_RandomNumberEngine
,
1130 __w
, _UIntType
>& __x
)
1137 _RandomNumberEngine _M_b
;
1141 * @brief Inserts the current state of a %independent_bits_engine random
1142 * number generator engine @p __x into the output stream @p __os.
1144 * @param __os An output stream.
1145 * @param __x A %independent_bits_engine random number generator engine.
1147 * @returns The output stream with the state of @p __x inserted or in
1150 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1151 typename _CharT
, typename _Traits
>
1152 std::basic_ostream
<_CharT
, _Traits
>&
1153 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1154 const std::independent_bits_engine
<_RandomNumberEngine
,
1155 __w
, _UIntType
>& __x
)
1162 * @brief Produces random numbers by combining random numbers from some
1163 * base engine to produce random numbers with a specifies number of bits
1166 template<typename _RandomNumberEngine
, size_t __k
>
1167 class shuffle_order_engine
1169 static_assert(__k
>= 1U,
1170 "template arguments out of bounds"
1171 " in shuffle_order_engine");
1174 /** The type of the generated random value. */
1175 typedef typename
_RandomNumberEngine::result_type result_type
;
1177 static const size_t table_size
= __k
;
1180 * @brief Constructs a default %shuffle_order_engine engine.
1182 * The underlying engine is default constructed as well.
1184 shuffle_order_engine()
1186 { _M_initialize(); }
1189 * @brief Copy constructs a %shuffle_order_engine engine.
1191 * Copies an existing base class random number generator.
1192 * @param rng An existing (base class) engine object.
1195 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1197 { _M_initialize(); }
1200 * @brief Move constructs a %shuffle_order_engine engine.
1202 * Copies an existing base class random number generator.
1203 * @param rng An existing (base class) engine object.
1206 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1207 : _M_b(std::move(__rne
))
1208 { _M_initialize(); }
1211 * @brief Seed constructs a %shuffle_order_engine engine.
1213 * Constructs the underlying generator engine seeded with @p __s.
1214 * @param __s A seed value for the base class engine.
1217 shuffle_order_engine(result_type __s
)
1219 { _M_initialize(); }
1222 * @brief Generator construct a %shuffle_order_engine engine.
1224 * @param __q A seed sequence.
1227 shuffle_order_engine(seed_seq
& __q
)
1229 { _M_initialize(); }
1232 * @brief Reseeds the %shuffle_order_engine object with the default seed
1233 for the underlying base class generator engine.
1243 * @brief Reseeds the %shuffle_order_engine object with the default seed
1244 * for the underlying base class generator engine.
1247 seed(result_type __s
)
1254 * @brief Reseeds the %shuffle_order_engine object with the given seed
1256 * @param __q A seed generator function.
1266 * Gets a const reference to the underlying generator engine object.
1268 const _RandomNumberEngine
&
1273 * Gets the minimum value in the generated random number range.
1275 * @todo This should be constexpr.
1279 { return _M_b
.min(); }
1282 * Gets the maximum value in the generated random number range.
1284 * @todo This should be constexpr.
1288 { return _M_b
.max(); }
1291 * Discard a sequence of random numbers.
1293 * @todo Look for a faster way to do discard.
1296 discard(unsigned long long __z
)
1298 for (; __z
!= 0ULL; --__z
)
1303 * Gets the next value in the generated random number sequence.
1309 * Compares two %shuffle_order_engine random number generator objects
1310 * of the same type for equality.
1312 * @param __lhs A %shuffle_order_engine random number generator object.
1313 * @param __rhs Another %shuffle_order_engine random number generator
1316 * @returns true if the two objects are equal, false otherwise.
1319 operator==(const shuffle_order_engine
& __lhs
,
1320 const shuffle_order_engine
& __rhs
)
1321 { return __lhs
._M_b
== __rhs
._M_b
; }
1324 * @brief Inserts the current state of a %shuffle_order_engine random
1325 * number generator engine @p __x into the output stream
1328 * @param __os An output stream.
1329 * @param __x A %shuffle_order_engine random number generator engine.
1331 * @returns The output stream with the state of @p __x inserted or in
1334 template<typename _RandomNumberEngine1
, size_t __k1
,
1335 typename _CharT
, typename _Traits
>
1336 friend std::basic_ostream
<_CharT
, _Traits
>&
1337 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1338 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1342 * @brief Extracts the current state of a % subtract_with_carry_engine
1343 * random number generator engine @p __x from the input stream
1346 * @param __is An input stream.
1347 * @param __x A %shuffle_order_engine random number generator engine.
1349 * @returns The input stream with the state of @p __x extracted or in
1352 template<typename _RandomNumberEngine1
, size_t __k1
,
1353 typename _CharT
, typename _Traits
>
1354 friend std::basic_istream
<_CharT
, _Traits
>&
1355 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1356 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1359 void _M_initialize()
1361 for (size_t __i
= 0; __i
< __k
; ++__i
)
1366 _RandomNumberEngine _M_b
;
1367 result_type _M_v
[__k
];
1372 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1374 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1378 * An alternative LCR (Lehmer Generator function) .
1380 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1384 * The classic Mersenne Twister.
1387 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1388 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1389 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1391 typedef mersenne_twister_engine
<
1397 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1400 * An alternative Mersenne Twister.
1402 typedef mersenne_twister_engine
<
1405 0xb5026f5aa96619e9ULL
, 29,
1406 0x5555555555555555ULL
, 17,
1407 0x71d67fffeda60000ULL
, 37,
1408 0xfff7eee000000000ULL
, 43,
1409 6364136223846793005ULL> mt19937_64
;
1414 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1417 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1419 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1422 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1427 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1432 typedef minstd_rand0 default_random_engine
;
1435 * A standard interface to a platform-specific non-deterministic
1436 * random number generator (if any are available).
1441 /** The type of the generated random value. */
1442 typedef unsigned int result_type
;
1444 // constructors, destructors and member functions
1446 #ifdef _GLIBCXX_USE_RANDOM_TR1
1449 random_device(const std::string
& __token
= "/dev/urandom")
1451 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1452 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1453 std::__throw_runtime_error(__N("random_device::"
1454 "random_device(const std::string&)"));
1458 { std::fclose(_M_file
); }
1463 random_device(const std::string
& __token
= "mt19937")
1464 : _M_mt(_M_strtoul(__token
)) { }
1467 static unsigned long
1468 _M_strtoul(const std::string
& __str
)
1470 unsigned long __ret
= 5489UL;
1471 if (__str
!= "mt19937")
1473 const char* __nptr
= __str
.c_str();
1475 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1476 if (*__nptr
== '\0' || *__endptr
!= '\0')
1477 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1478 "(const std::string&)"));
1489 { return std::numeric_limits
<result_type
>::min(); }
1493 { return std::numeric_limits
<result_type
>::max(); }
1502 #ifdef _GLIBCXX_USE_RANDOM_TR1
1504 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1512 // No copy functions.
1513 random_device(const random_device
&) = delete;
1514 void operator=(const random_device
&) = delete;
1518 #ifdef _GLIBCXX_USE_RANDOM_TR1
1525 /* @} */ // group std_random_generators
1528 * @addtogroup std_random_distributions Random Number Distributions
1529 * @ingroup std_random
1534 * @addtogroup std_random_distributions_uniform Uniform Distributions
1535 * @ingroup std_random_distributions
1540 * @brief Uniform discrete distribution for random numbers.
1541 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1542 * probability throughout the range.
1544 template<typename _IntType
= int>
1545 class uniform_int_distribution
1547 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
1550 /** The type of the range of the distribution. */
1551 typedef _IntType result_type
;
1552 /** Parameter type. */
1555 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1558 param_type(_IntType __a
= 0, _IntType __b
= 9)
1559 : _M_a(__a
), _M_b(__b
)
1561 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1573 operator==(const param_type
& __p1
, const param_type
& __p2
)
1574 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
1583 * @brief Constructs a uniform distribution object.
1586 uniform_int_distribution(_IntType __a
= 0, _IntType __b
= 9)
1587 : _M_param(__a
, __b
)
1591 uniform_int_distribution(const param_type
& __p
)
1596 * @brief Resets the distribution state.
1598 * Does nothing for the uniform integer distribution.
1605 { return _M_param
.a(); }
1609 { return _M_param
.b(); }
1612 * @brief Returns the inclusive lower bound of the distribution range.
1616 { return this->a(); }
1619 * @brief Returns the inclusive upper bound of the distribution range.
1623 { return this->b(); }
1626 * @brief Returns the parameter set of the distribution.
1630 { return _M_param
; }
1633 * @brief Sets the parameter set of the distribution.
1634 * @param __param The new parameter set of the distribution.
1637 param(const param_type
& __param
)
1638 { _M_param
= __param
; }
1641 * Gets a uniformly distributed random number in the range
1644 template<typename _UniformRandomNumberGenerator
>
1646 operator()(_UniformRandomNumberGenerator
& __urng
)
1648 typedef typename
_UniformRandomNumberGenerator::result_type
1650 return _M_call(__urng
, this->a(), this->b(),
1651 typename is_integral
<_UResult_type
>::type());
1655 * Gets a uniform random number in the range @f$[0, n)@f$.
1657 * This function is aimed at use with std::random_shuffle.
1659 template<typename _UniformRandomNumberGenerator
>
1661 operator()(_UniformRandomNumberGenerator
& __urng
,
1662 const param_type
& __p
)
1664 typedef typename
_UniformRandomNumberGenerator::result_type
1666 return _M_call(__urng
, __p
.a(), __p
.b(),
1667 typename is_integral
<_UResult_type
>::type());
1671 template<typename _UniformRandomNumberGenerator
>
1673 _M_call(_UniformRandomNumberGenerator
& __urng
,
1674 result_type __min
, result_type __max
, true_type
);
1676 template<typename _UniformRandomNumberGenerator
>
1678 _M_call(_UniformRandomNumberGenerator
& __urng
,
1679 result_type __min
, result_type __max
, false_type
)
1681 return result_type((__urng() - __urng
.min())
1682 / (__urng
.max() - __urng
.min())
1683 * (__max
- __min
+ 1)) + __min
;
1686 param_type _M_param
;
1690 * @brief Return true if two uniform integer distributions have
1691 * the same parameters.
1693 template<typename _IntType
>
1695 operator==(const std::uniform_int_distribution
<_IntType
>& __d1
,
1696 const std::uniform_int_distribution
<_IntType
>& __d2
)
1697 { return __d1
.param() == __d2
.param(); }
1700 * @brief Inserts a %uniform_int_distribution random number
1701 * distribution @p __x into the output stream @p os.
1703 * @param __os An output stream.
1704 * @param __x A %uniform_int_distribution random number distribution.
1706 * @returns The output stream with the state of @p __x inserted or in
1709 template<typename _IntType
, typename _CharT
, typename _Traits
>
1710 std::basic_ostream
<_CharT
, _Traits
>&
1711 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1712 const std::uniform_int_distribution
<_IntType
>&);
1715 * @brief Extracts a %uniform_int_distribution random number distribution
1716 * @p __x from the input stream @p __is.
1718 * @param __is An input stream.
1719 * @param __x A %uniform_int_distribution random number generator engine.
1721 * @returns The input stream with @p __x extracted or in an error state.
1723 template<typename _IntType
, typename _CharT
, typename _Traits
>
1724 std::basic_istream
<_CharT
, _Traits
>&
1725 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1726 std::uniform_int_distribution
<_IntType
>&);
1730 * @brief Uniform continuous distribution for random numbers.
1732 * A continuous random distribution on the range [min, max) with equal
1733 * probability throughout the range. The URNG should be real-valued and
1734 * deliver number in the range [0, 1).
1736 template<typename _RealType
= double>
1737 class uniform_real_distribution
1740 /** The type of the range of the distribution. */
1741 typedef _RealType result_type
;
1742 /** Parameter type. */
1745 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1748 param_type(_RealType __a
= _RealType(0),
1749 _RealType __b
= _RealType(1))
1750 : _M_a(__a
), _M_b(__b
)
1752 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1764 operator==(const param_type
& __p1
, const param_type
& __p2
)
1765 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
1774 * @brief Constructs a uniform_real_distribution object.
1776 * @param __min [IN] The lower bound of the distribution.
1777 * @param __max [IN] The upper bound of the distribution.
1780 uniform_real_distribution(_RealType __a
= _RealType(0),
1781 _RealType __b
= _RealType(1))
1782 : _M_param(__a
, __b
)
1786 uniform_real_distribution(const param_type
& __p
)
1791 * @brief Resets the distribution state.
1793 * Does nothing for the uniform real distribution.
1800 { return _M_param
.a(); }
1804 { return _M_param
.b(); }
1807 * @brief Returns the inclusive lower bound of the distribution range.
1811 { return this->a(); }
1814 * @brief Returns the inclusive upper bound of the distribution range.
1818 { return this->b(); }
1821 * @brief Returns the parameter set of the distribution.
1825 { return _M_param
; }
1828 * @brief Sets the parameter set of the distribution.
1829 * @param __param The new parameter set of the distribution.
1832 param(const param_type
& __param
)
1833 { _M_param
= __param
; }
1835 template<typename _UniformRandomNumberGenerator
>
1837 operator()(_UniformRandomNumberGenerator
& __urng
)
1839 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1841 return (__aurng() * (this->b() - this->a())) + this->a();
1844 template<typename _UniformRandomNumberGenerator
>
1846 operator()(_UniformRandomNumberGenerator
& __urng
,
1847 const param_type
& __p
)
1849 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1851 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1855 param_type _M_param
;
1859 * @brief Return true if two uniform real distributions have
1860 * the same parameters.
1862 template<typename _IntType
>
1864 operator==(const std::uniform_real_distribution
<_IntType
>& __d1
,
1865 const std::uniform_real_distribution
<_IntType
>& __d2
)
1866 { return __d1
.param() == __d2
.param(); }
1869 * @brief Inserts a %uniform_real_distribution random number
1870 * distribution @p __x into the output stream @p __os.
1872 * @param __os An output stream.
1873 * @param __x A %uniform_real_distribution random number distribution.
1875 * @returns The output stream with the state of @p __x inserted or in
1878 template<typename _RealType
, typename _CharT
, typename _Traits
>
1879 std::basic_ostream
<_CharT
, _Traits
>&
1880 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1881 const std::uniform_real_distribution
<_RealType
>&);
1884 * @brief Extracts a %uniform_real_distribution random number distribution
1885 * @p __x from the input stream @p __is.
1887 * @param __is An input stream.
1888 * @param __x A %uniform_real_distribution random number generator engine.
1890 * @returns The input stream with @p __x extracted or in an error state.
1892 template<typename _RealType
, typename _CharT
, typename _Traits
>
1893 std::basic_istream
<_CharT
, _Traits
>&
1894 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1895 std::uniform_real_distribution
<_RealType
>&);
1897 /* @} */ // group std_random_distributions_uniform
1900 * @addtogroup std_random_distributions_normal Normal Distributions
1901 * @ingroup std_random_distributions
1906 * @brief A normal continuous distribution for random numbers.
1908 * The formula for the normal probability density function is
1909 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1910 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1912 template<typename _RealType
= double>
1913 class normal_distribution
1916 /** The type of the range of the distribution. */
1917 typedef _RealType result_type
;
1918 /** Parameter type. */
1921 typedef normal_distribution
<_RealType
> distribution_type
;
1924 param_type(_RealType __mean
= _RealType(0),
1925 _RealType __stddev
= _RealType(1))
1926 : _M_mean(__mean
), _M_stddev(__stddev
)
1928 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1937 { return _M_stddev
; }
1940 operator==(const param_type
& __p1
, const param_type
& __p2
)
1941 { return (__p1
._M_mean
== __p2
._M_mean
)
1942 && (__p1
._M_stddev
== __p2
._M_stddev
); }
1946 _RealType _M_stddev
;
1951 * Constructs a normal distribution with parameters @f$ mean @f$ and
1952 * standard deviation.
1955 normal_distribution(result_type __mean
= result_type(0),
1956 result_type __stddev
= result_type(1))
1957 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1961 normal_distribution(const param_type
& __p
)
1962 : _M_param(__p
), _M_saved_available(false)
1966 * @brief Resets the distribution state.
1970 { _M_saved_available
= false; }
1973 * @brief Returns the mean of the distribution.
1977 { return _M_param
.mean(); }
1980 * @brief Returns the standard deviation of the distribution.
1984 { return _M_param
.stddev(); }
1987 * @brief Returns the parameter set of the distribution.
1991 { return _M_param
; }
1994 * @brief Sets the parameter set of the distribution.
1995 * @param __param The new parameter set of the distribution.
1998 param(const param_type
& __param
)
1999 { _M_param
= __param
; }
2002 * @brief Returns the greatest lower bound value of the distribution.
2006 { return std::numeric_limits
<result_type
>::min(); }
2009 * @brief Returns the least upper bound value of the distribution.
2013 { return std::numeric_limits
<result_type
>::max(); }
2015 template<typename _UniformRandomNumberGenerator
>
2017 operator()(_UniformRandomNumberGenerator
& __urng
)
2018 { return this->operator()(__urng
, this->param()); }
2020 template<typename _UniformRandomNumberGenerator
>
2022 operator()(_UniformRandomNumberGenerator
& __urng
,
2023 const param_type
& __p
);
2026 * @brief Return true if two normal distributions have
2027 * the same parameters.
2029 template<typename _RealType1
>
2031 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2032 const std::normal_distribution
<_RealType1
>& __d2
);
2035 * @brief Inserts a %normal_distribution random number distribution
2036 * @p __x into the output stream @p __os.
2038 * @param __os An output stream.
2039 * @param __x A %normal_distribution random number distribution.
2041 * @returns The output stream with the state of @p __x inserted or in
2044 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2045 friend std::basic_ostream
<_CharT
, _Traits
>&
2046 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2047 const std::normal_distribution
<_RealType1
>&);
2050 * @brief Extracts a %normal_distribution random number distribution
2051 * @p __x from the input stream @p __is.
2053 * @param __is An input stream.
2054 * @param __x A %normal_distribution random number generator engine.
2056 * @returns The input stream with @p __x extracted or in an error
2059 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2060 friend std::basic_istream
<_CharT
, _Traits
>&
2061 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2062 std::normal_distribution
<_RealType1
>&);
2065 param_type _M_param
;
2066 result_type _M_saved
;
2067 bool _M_saved_available
;
2072 * @brief A lognormal_distribution random number distribution.
2074 * The formula for the normal probability mass function is
2075 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2076 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
2078 template<typename _RealType
= double>
2079 class lognormal_distribution
2082 /** The type of the range of the distribution. */
2083 typedef _RealType result_type
;
2084 /** Parameter type. */
2087 typedef lognormal_distribution
<_RealType
> distribution_type
;
2090 param_type(_RealType __m
= _RealType(0),
2091 _RealType __s
= _RealType(1))
2092 : _M_m(__m
), _M_s(__s
)
2104 operator==(const param_type
& __p1
, const param_type
& __p2
)
2105 { return (__p1
._M_m
== __p2
._M_m
) && (__p1
._M_s
== __p2
._M_s
); }
2113 lognormal_distribution(_RealType __m
= _RealType(0),
2114 _RealType __s
= _RealType(1))
2115 : _M_param(__m
, __s
)
2119 lognormal_distribution(const param_type
& __p
)
2124 * Resets the distribution state.
2135 { return _M_param
.m(); }
2139 { return _M_param
.s(); }
2142 * @brief Returns the parameter set of the distribution.
2146 { return _M_param
; }
2149 * @brief Sets the parameter set of the distribution.
2150 * @param __param The new parameter set of the distribution.
2153 param(const param_type
& __param
)
2154 { _M_param
= __param
; }
2157 * @brief Returns the greatest lower bound value of the distribution.
2161 { return result_type(0); }
2164 * @brief Returns the least upper bound value of the distribution.
2168 { return std::numeric_limits
<result_type
>::max(); }
2170 template<typename _UniformRandomNumberGenerator
>
2172 operator()(_UniformRandomNumberGenerator
& __urng
)
2173 { return this->operator()(__urng
, this->param()); }
2175 template<typename _UniformRandomNumberGenerator
>
2177 operator()(_UniformRandomNumberGenerator
& __urng
,
2178 const param_type
& __p
);
2181 param_type _M_param
;
2185 * @brief Return true if two lognormal distributions have
2186 * the same parameters.
2188 template<typename _RealType
>
2190 operator==(const std::lognormal_distribution
<_RealType
>& __d1
,
2191 const std::lognormal_distribution
<_RealType
>& __d2
)
2192 { return __d1
.param() == __d2
.param(); }
2195 * @brief Inserts a %lognormal_distribution random number distribution
2196 * @p __x into the output stream @p __os.
2198 * @param __os An output stream.
2199 * @param __x A %lognormal_distribution random number distribution.
2201 * @returns The output stream with the state of @p __x inserted or in
2204 template<typename _RealType
, typename _CharT
, typename _Traits
>
2205 std::basic_ostream
<_CharT
, _Traits
>&
2206 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2207 const std::lognormal_distribution
<_RealType
>&);
2210 * @brief Extracts a %lognormal_distribution random number distribution
2211 * @p __x from the input stream @p __is.
2213 * @param __is An input stream.
2214 * @param __x A %lognormal_distribution random number
2217 * @returns The input stream with @p __x extracted or in an error state.
2219 template<typename _RealType
, typename _CharT
, typename _Traits
>
2220 std::basic_istream
<_CharT
, _Traits
>&
2221 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2222 std::lognormal_distribution
<_RealType
>&);
2226 * @brief A chi_squared_distribution random number distribution.
2228 * The formula for the normal probability mass function is
2229 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2231 template<typename _RealType
= double>
2232 class chi_squared_distribution
2235 /** The type of the range of the distribution. */
2236 typedef _RealType result_type
;
2237 /** Parameter type. */
2240 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2243 param_type(_RealType __n
= _RealType(1))
2252 operator==(const param_type
& __p1
, const param_type
& __p2
)
2253 { return __p1
._M_n
== __p2
._M_n
; }
2260 chi_squared_distribution(_RealType __n
= _RealType(1))
2265 chi_squared_distribution(const param_type
& __p
)
2270 * @brief Resets the distribution state.
2281 { return _M_param
.n(); }
2284 * @brief Returns the parameter set of the distribution.
2288 { return _M_param
; }
2291 * @brief Sets the parameter set of the distribution.
2292 * @param __param The new parameter set of the distribution.
2295 param(const param_type
& __param
)
2296 { _M_param
= __param
; }
2299 * @brief Returns the greatest lower bound value of the distribution.
2303 { return result_type(0); }
2306 * @brief Returns the least upper bound value of the distribution.
2310 { return std::numeric_limits
<result_type
>::max(); }
2312 template<typename _UniformRandomNumberGenerator
>
2314 operator()(_UniformRandomNumberGenerator
& __urng
)
2315 { return this->operator()(__urng
, this->param()); }
2317 template<typename _UniformRandomNumberGenerator
>
2319 operator()(_UniformRandomNumberGenerator
& __urng
,
2320 const param_type
& __p
);
2323 param_type _M_param
;
2327 * @brief Return true if two Chi-squared distributions have
2328 * the same parameters.
2330 template<typename _RealType
>
2332 operator==(const std::chi_squared_distribution
<_RealType
>& __d1
,
2333 const std::chi_squared_distribution
<_RealType
>& __d2
)
2334 { return __d1
.param() == __d2
.param(); }
2337 * @brief Inserts a %chi_squared_distribution random number distribution
2338 * @p __x into the output stream @p __os.
2340 * @param __os An output stream.
2341 * @param __x A %chi_squared_distribution random number distribution.
2343 * @returns The output stream with the state of @p __x inserted or in
2346 template<typename _RealType
, typename _CharT
, typename _Traits
>
2347 std::basic_ostream
<_CharT
, _Traits
>&
2348 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2349 const std::chi_squared_distribution
<_RealType
>&);
2352 * @brief Extracts a %chi_squared_distribution random number distribution
2353 * @p __x from the input stream @p __is.
2355 * @param __is An input stream.
2356 * @param __x A %chi_squared_distribution random number
2359 * @returns The input stream with @p __x extracted or in an error state.
2361 template<typename _RealType
, typename _CharT
, typename _Traits
>
2362 std::basic_istream
<_CharT
, _Traits
>&
2363 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2364 std::chi_squared_distribution
<_RealType
>&);
2368 * @brief A cauchy_distribution random number distribution.
2370 * The formula for the normal probability mass function is
2371 * @f$ p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1} @f$
2373 template<typename _RealType
= double>
2374 class cauchy_distribution
2377 /** The type of the range of the distribution. */
2378 typedef _RealType result_type
;
2379 /** Parameter type. */
2382 typedef cauchy_distribution
<_RealType
> distribution_type
;
2385 param_type(_RealType __a
= _RealType(0),
2386 _RealType __b
= _RealType(1))
2387 : _M_a(__a
), _M_b(__b
)
2399 operator==(const param_type
& __p1
, const param_type
& __p2
)
2400 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
2408 cauchy_distribution(_RealType __a
= _RealType(0),
2409 _RealType __b
= _RealType(1))
2410 : _M_param(__a
, __b
)
2414 cauchy_distribution(const param_type
& __p
)
2419 * @brief Resets the distribution state.
2430 { return _M_param
.a(); }
2434 { return _M_param
.b(); }
2437 * @brief Returns the parameter set of the distribution.
2441 { return _M_param
; }
2444 * @brief Sets the parameter set of the distribution.
2445 * @param __param The new parameter set of the distribution.
2448 param(const param_type
& __param
)
2449 { _M_param
= __param
; }
2452 * @brief Returns the greatest lower bound value of the distribution.
2456 { return std::numeric_limits
<result_type
>::min(); }
2459 * @brief Returns the least upper bound value of the distribution.
2463 { return std::numeric_limits
<result_type
>::max(); }
2465 template<typename _UniformRandomNumberGenerator
>
2467 operator()(_UniformRandomNumberGenerator
& __urng
)
2468 { return this->operator()(__urng
, this->param()); }
2470 template<typename _UniformRandomNumberGenerator
>
2472 operator()(_UniformRandomNumberGenerator
& __urng
,
2473 const param_type
& __p
);
2476 param_type _M_param
;
2480 * @brief Return true if two Cauchy distributions have
2481 * the same parameters.
2483 template<typename _RealType
>
2485 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2486 const std::cauchy_distribution
<_RealType
>& __d2
)
2487 { return __d1
.param() == __d2
.param(); }
2490 * @brief Inserts a %cauchy_distribution random number distribution
2491 * @p __x into the output stream @p __os.
2493 * @param __os An output stream.
2494 * @param __x A %cauchy_distribution random number distribution.
2496 * @returns The output stream with the state of @p __x inserted or in
2499 template<typename _RealType
, typename _CharT
, typename _Traits
>
2500 std::basic_ostream
<_CharT
, _Traits
>&
2501 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2502 const std::cauchy_distribution
<_RealType
>&);
2505 * @brief Extracts a %cauchy_distribution random number distribution
2506 * @p __x from the input stream @p __is.
2508 * @param __is An input stream.
2509 * @param __x A %cauchy_distribution random number
2512 * @returns The input stream with @p __x extracted or in an error state.
2514 template<typename _RealType
, typename _CharT
, typename _Traits
>
2515 std::basic_istream
<_CharT
, _Traits
>&
2516 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2517 std::cauchy_distribution
<_RealType
>&);
2521 * @brief A fisher_f_distribution random number distribution.
2523 * The formula for the normal probability mass function is
2524 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2525 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2526 * (1 + \frac{mx}{n})^{-(m+n)/2} @f$
2528 template<typename _RealType
= double>
2529 class fisher_f_distribution
2532 /** The type of the range of the distribution. */
2533 typedef _RealType result_type
;
2534 /** Parameter type. */
2537 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2540 param_type(_RealType __m
= _RealType(1),
2541 _RealType __n
= _RealType(1))
2542 : _M_m(__m
), _M_n(__n
)
2554 operator==(const param_type
& __p1
, const param_type
& __p2
)
2555 { return (__p1
._M_m
== __p2
._M_m
) && (__p1
._M_n
== __p2
._M_n
); }
2563 fisher_f_distribution(_RealType __m
= _RealType(1),
2564 _RealType __n
= _RealType(1))
2565 : _M_param(__m
, __n
)
2569 fisher_f_distribution(const param_type
& __p
)
2574 * @brief Resets the distribution state.
2585 { return _M_param
.m(); }
2589 { return _M_param
.n(); }
2592 * @brief Returns the parameter set of the distribution.
2596 { return _M_param
; }
2599 * @brief Sets the parameter set of the distribution.
2600 * @param __param The new parameter set of the distribution.
2603 param(const param_type
& __param
)
2604 { _M_param
= __param
; }
2607 * @brief Returns the greatest lower bound value of the distribution.
2611 { return result_type(0); }
2614 * @brief Returns the least upper bound value of the distribution.
2618 { return std::numeric_limits
<result_type
>::max(); }
2620 template<typename _UniformRandomNumberGenerator
>
2622 operator()(_UniformRandomNumberGenerator
& __urng
)
2623 { return this->operator()(__urng
, this->param()); }
2625 template<typename _UniformRandomNumberGenerator
>
2627 operator()(_UniformRandomNumberGenerator
& __urng
,
2628 const param_type
& __p
);
2631 param_type _M_param
;
2635 * @brief Return true if two Fisher f distributions have
2636 * the same parameters.
2638 template<typename _RealType
>
2640 operator==(const std::fisher_f_distribution
<_RealType
>& __d1
,
2641 const std::fisher_f_distribution
<_RealType
>& __d2
)
2642 { return __d1
.param() == __d2
.param(); }
2645 * @brief Inserts a %fisher_f_distribution random number distribution
2646 * @p __x into the output stream @p __os.
2648 * @param __os An output stream.
2649 * @param __x A %fisher_f_distribution random number distribution.
2651 * @returns The output stream with the state of @p __x inserted or in
2654 template<typename _RealType
, typename _CharT
, typename _Traits
>
2655 std::basic_ostream
<_CharT
, _Traits
>&
2656 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2657 const std::fisher_f_distribution
<_RealType
>&);
2660 * @brief Extracts a %fisher_f_distribution random number distribution
2661 * @p __x from the input stream @p __is.
2663 * @param __is An input stream.
2664 * @param __x A %fisher_f_distribution random number
2667 * @returns The input stream with @p __x extracted or in an error state.
2669 template<typename _RealType
, typename _CharT
, typename _Traits
>
2670 std::basic_istream
<_CharT
, _Traits
>&
2671 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2672 std::fisher_f_distribution
<_RealType
>&);
2676 * @brief A student_t_distribution random number distribution.
2678 * The formula for the normal probability mass function is
2679 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2680 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} @f$
2682 template<typename _RealType
= double>
2683 class student_t_distribution
2686 /** The type of the range of the distribution. */
2687 typedef _RealType result_type
;
2688 /** Parameter type. */
2691 typedef student_t_distribution
<_RealType
> distribution_type
;
2694 param_type(_RealType __n
= _RealType(1))
2703 operator==(const param_type
& __p1
, const param_type
& __p2
)
2704 { return __p1
._M_n
== __p2
._M_n
; }
2711 student_t_distribution(_RealType __n
= _RealType(1))
2716 student_t_distribution(const param_type
& __p
)
2721 * @brief Resets the distribution state.
2732 { return _M_param
.n(); }
2735 * @brief Returns the parameter set of the distribution.
2739 { return _M_param
; }
2742 * @brief Sets the parameter set of the distribution.
2743 * @param __param The new parameter set of the distribution.
2746 param(const param_type
& __param
)
2747 { _M_param
= __param
; }
2750 * @brief Returns the greatest lower bound value of the distribution.
2754 { return std::numeric_limits
<result_type
>::min(); }
2757 * @brief Returns the least upper bound value of the distribution.
2761 { return std::numeric_limits
<result_type
>::max(); }
2763 template<typename _UniformRandomNumberGenerator
>
2765 operator()(_UniformRandomNumberGenerator
& __urng
)
2766 { return this->operator()(__urng
, this->param()); }
2768 template<typename _UniformRandomNumberGenerator
>
2770 operator()(_UniformRandomNumberGenerator
& __urng
,
2771 const param_type
& __p
);
2774 template<typename _UniformRandomNumberGenerator
>
2776 _M_gaussian(_UniformRandomNumberGenerator
& __urng
,
2777 const result_type __sigma
);
2779 param_type _M_param
;
2783 * @brief Return true if two Student t distributions have
2784 * the same parameters.
2786 template<typename _RealType
>
2788 operator==(const std::student_t_distribution
<_RealType
>& __d1
,
2789 const std::student_t_distribution
<_RealType
>& __d2
)
2790 { return __d1
.param() == __d2
.param(); }
2793 * @brief Inserts a %student_t_distribution random number distribution
2794 * @p __x into the output stream @p __os.
2796 * @param __os An output stream.
2797 * @param __x A %student_t_distribution random number distribution.
2799 * @returns The output stream with the state of @p __x inserted or in
2802 template<typename _RealType
, typename _CharT
, typename _Traits
>
2803 std::basic_ostream
<_CharT
, _Traits
>&
2804 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2805 const std::student_t_distribution
<_RealType
>&);
2808 * @brief Extracts a %student_t_distribution random number distribution
2809 * @p __x from the input stream @p __is.
2811 * @param __is An input stream.
2812 * @param __x A %student_t_distribution random number
2815 * @returns The input stream with @p __x extracted or in an error state.
2817 template<typename _RealType
, typename _CharT
, typename _Traits
>
2818 std::basic_istream
<_CharT
, _Traits
>&
2819 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2820 std::student_t_distribution
<_RealType
>&);
2822 /* @} */ // group std_random_distributions_normal
2825 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2826 * @ingroup std_random_distributions
2831 * @brief A Bernoulli random number distribution.
2833 * Generates a sequence of true and false values with likelihood @f$ p @f$
2834 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2836 class bernoulli_distribution
2839 /** The type of the range of the distribution. */
2840 typedef bool result_type
;
2841 /** Parameter type. */
2844 typedef bernoulli_distribution distribution_type
;
2847 param_type(double __p
= 0.5)
2850 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
2858 operator==(const param_type
& __p1
, const param_type
& __p2
)
2859 { return __p1
._M_p
== __p2
._M_p
; }
2867 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2869 * @param __p [IN] The likelihood of a true result being returned.
2870 * Must be in the interval @f$ [0, 1] @f$.
2873 bernoulli_distribution(double __p
= 0.5)
2878 bernoulli_distribution(const param_type
& __p
)
2883 * @brief Resets the distribution state.
2885 * Does nothing for a Bernoulli distribution.
2891 * @brief Returns the @p p parameter of the distribution.
2895 { return _M_param
.p(); }
2898 * @brief Returns the parameter set of the distribution.
2902 { return _M_param
; }
2905 * @brief Sets the parameter set of the distribution.
2906 * @param __param The new parameter set of the distribution.
2909 param(const param_type
& __param
)
2910 { _M_param
= __param
; }
2913 * @brief Returns the greatest lower bound value of the distribution.
2917 { return std::numeric_limits
<result_type
>::min(); }
2920 * @brief Returns the least upper bound value of the distribution.
2924 { return std::numeric_limits
<result_type
>::max(); }
2927 * @brief Returns the next value in the Bernoullian sequence.
2929 template<typename _UniformRandomNumberGenerator
>
2931 operator()(_UniformRandomNumberGenerator
& __urng
)
2933 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2935 if ((__aurng() - __aurng
.min())
2936 < this->p() * (__aurng
.max() - __aurng
.min()))
2941 template<typename _UniformRandomNumberGenerator
>
2943 operator()(_UniformRandomNumberGenerator
& __urng
,
2944 const param_type
& __p
)
2946 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2948 if ((__aurng() - __aurng
.min())
2949 < __p
.p() * (__aurng
.max() - __aurng
.min()))
2955 param_type _M_param
;
2959 * @brief Return true if two Bernoulli distributions have
2960 * the same parameters.
2963 operator==(const std::bernoulli_distribution
& __d1
,
2964 const std::bernoulli_distribution
& __d2
)
2965 { return __d1
.param() == __d2
.param(); }
2968 * @brief Inserts a %bernoulli_distribution random number distribution
2969 * @p __x into the output stream @p __os.
2971 * @param __os An output stream.
2972 * @param __x A %bernoulli_distribution random number distribution.
2974 * @returns The output stream with the state of @p __x inserted or in
2977 template<typename _CharT
, typename _Traits
>
2978 std::basic_ostream
<_CharT
, _Traits
>&
2979 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2980 const std::bernoulli_distribution
&);
2983 * @brief Extracts a %bernoulli_distribution random number distribution
2984 * @p __x from the input stream @p __is.
2986 * @param __is An input stream.
2987 * @param __x A %bernoulli_distribution random number generator engine.
2989 * @returns The input stream with @p __x extracted or in an error state.
2991 template<typename _CharT
, typename _Traits
>
2992 std::basic_istream
<_CharT
, _Traits
>&
2993 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2994 std::bernoulli_distribution
& __x
)
2998 __x
.param(bernoulli_distribution::param_type(__p
));
3004 * @brief A discrete binomial random number distribution.
3006 * The formula for the binomial probability density function is
3007 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3008 * and @f$ p @f$ are the parameters of the distribution.
3010 template<typename _IntType
= int>
3011 class binomial_distribution
3013 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3016 /** The type of the range of the distribution. */
3017 typedef _IntType result_type
;
3018 /** Parameter type. */
3021 typedef binomial_distribution
<_IntType
> distribution_type
;
3022 friend class binomial_distribution
<_IntType
>;
3025 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3026 : _M_t(__t
), _M_p(__p
)
3028 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3043 operator==(const param_type
& __p1
, const param_type
& __p2
)
3044 { return (__p1
._M_t
== __p2
._M_t
) && (__p1
._M_p
== __p2
._M_p
); }
3054 #if _GLIBCXX_USE_C99_MATH_TR1
3055 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3056 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3061 // constructors and member function
3063 binomial_distribution(_IntType __t
= _IntType(1),
3065 : _M_param(__t
, __p
), _M_nd()
3069 binomial_distribution(const param_type
& __p
)
3070 : _M_param(__p
), _M_nd()
3074 * @brief Resets the distribution state.
3081 * @brief Returns the distribution @p t parameter.
3085 { return _M_param
.t(); }
3088 * @brief Returns the distribution @p p parameter.
3092 { return _M_param
.p(); }
3095 * @brief Returns the parameter set of the distribution.
3099 { return _M_param
; }
3102 * @brief Sets the parameter set of the distribution.
3103 * @param __param The new parameter set of the distribution.
3106 param(const param_type
& __param
)
3107 { _M_param
= __param
; }
3110 * @brief Returns the greatest lower bound value of the distribution.
3117 * @brief Returns the least upper bound value of the distribution.
3121 { return _M_param
.t(); }
3124 * @brief Return true if two binomial distributions have
3125 * the same parameters.
3127 template<typename _IntType1
>
3129 operator==(const std::binomial_distribution
<_IntType1
>& __d1
,
3130 const std::binomial_distribution
<_IntType1
>& __d2
)
3131 { return ((__d1
.param() == __d2
.param())
3132 && (__d1
._M_nd
== __d2
._M_nd
)); }
3134 template<typename _UniformRandomNumberGenerator
>
3136 operator()(_UniformRandomNumberGenerator
& __urng
)
3137 { return this->operator()(__urng
, this->param()); }
3139 template<typename _UniformRandomNumberGenerator
>
3141 operator()(_UniformRandomNumberGenerator
& __urng
,
3142 const param_type
& __p
);
3145 * @brief Inserts a %binomial_distribution random number distribution
3146 * @p __x into the output stream @p __os.
3148 * @param __os An output stream.
3149 * @param __x A %binomial_distribution random number distribution.
3151 * @returns The output stream with the state of @p __x inserted or in
3154 template<typename _IntType1
,
3155 typename _CharT
, typename _Traits
>
3156 friend std::basic_ostream
<_CharT
, _Traits
>&
3157 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3158 const std::binomial_distribution
<_IntType1
>&);
3161 * @brief Extracts a %binomial_distribution random number distribution
3162 * @p __x from the input stream @p __is.
3164 * @param __is An input stream.
3165 * @param __x A %binomial_distribution random number generator engine.
3167 * @returns The input stream with @p __x extracted or in an error
3170 template<typename _IntType1
,
3171 typename _CharT
, typename _Traits
>
3172 friend std::basic_istream
<_CharT
, _Traits
>&
3173 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3174 std::binomial_distribution
<_IntType1
>&);
3177 template<typename _UniformRandomNumberGenerator
>
3179 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3181 param_type _M_param
;
3183 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3184 normal_distribution
<double> _M_nd
;
3189 * @brief A discrete geometric random number distribution.
3191 * The formula for the geometric probability density function is
3192 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3195 template<typename _IntType
= int>
3196 class geometric_distribution
3198 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3201 /** The type of the range of the distribution. */
3202 typedef _IntType result_type
;
3203 /** Parameter type. */
3206 typedef geometric_distribution
<_IntType
> distribution_type
;
3207 friend class geometric_distribution
<_IntType
>;
3210 param_type(double __p
= 0.5)
3213 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3223 operator==(const param_type
& __p1
, const param_type
& __p2
)
3224 { return __p1
._M_p
== __p2
._M_p
; }
3229 { _M_log_p
= std::log(_M_p
); }
3236 // constructors and member function
3238 geometric_distribution(double __p
= 0.5)
3243 geometric_distribution(const param_type
& __p
)
3248 * @brief Resets the distribution state.
3250 * Does nothing for the geometric distribution.
3256 * @brief Returns the distribution parameter @p p.
3260 { return _M_param
.p(); }
3263 * @brief Returns the parameter set of the distribution.
3267 { return _M_param
; }
3270 * @brief Sets the parameter set of the distribution.
3271 * @param __param The new parameter set of the distribution.
3274 param(const param_type
& __param
)
3275 { _M_param
= __param
; }
3278 * @brief Returns the greatest lower bound value of the distribution.
3285 * @brief Returns the least upper bound value of the distribution.
3289 { return std::numeric_limits
<result_type
>::max(); }
3291 template<typename _UniformRandomNumberGenerator
>
3293 operator()(_UniformRandomNumberGenerator
& __urng
)
3294 { return this->operator()(__urng
, this->param()); }
3296 template<typename _UniformRandomNumberGenerator
>
3298 operator()(_UniformRandomNumberGenerator
& __urng
,
3299 const param_type
& __p
);
3302 param_type _M_param
;
3306 * @brief Return true if two geometric distributions have
3307 * the same parameters.
3309 template<typename _IntType
>
3311 operator==(const geometric_distribution
<_IntType
>& __d1
,
3312 const geometric_distribution
<_IntType
>& __d2
)
3313 { return __d1
.param() == __d2
.param(); }
3316 * @brief Inserts a %geometric_distribution random number distribution
3317 * @p __x into the output stream @p __os.
3319 * @param __os An output stream.
3320 * @param __x A %geometric_distribution random number distribution.
3322 * @returns The output stream with the state of @p __x inserted or in
3325 template<typename _IntType
,
3326 typename _CharT
, typename _Traits
>
3327 std::basic_ostream
<_CharT
, _Traits
>&
3328 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3329 const std::geometric_distribution
<_IntType
>&);
3332 * @brief Extracts a %geometric_distribution random number distribution
3333 * @p __x from the input stream @p __is.
3335 * @param __is An input stream.
3336 * @param __x A %geometric_distribution random number generator engine.
3338 * @returns The input stream with @p __x extracted or in an error state.
3340 template<typename _IntType
,
3341 typename _CharT
, typename _Traits
>
3342 std::basic_istream
<_CharT
, _Traits
>&
3343 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3344 std::geometric_distribution
<_IntType
>&);
3348 * @brief A negative_binomial_distribution random number distribution.
3350 * The formula for the negative binomial probability mass function is
3351 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3352 * and @f$ p @f$ are the parameters of the distribution.
3354 template<typename _IntType
= int>
3355 class negative_binomial_distribution
3357 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3360 /** The type of the range of the distribution. */
3361 typedef _IntType result_type
;
3362 /** Parameter type. */
3365 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3368 param_type(_IntType __k
= 1, double __p
= 0.5)
3369 : _M_k(__k
), _M_p(__p
)
3381 operator==(const param_type
& __p1
, const param_type
& __p2
)
3382 { return (__p1
._M_k
== __p2
._M_k
) && (__p1
._M_p
== __p2
._M_p
); }
3390 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3391 : _M_param(__k
, __p
)
3395 negative_binomial_distribution(const param_type
& __p
)
3400 * @brief Resets the distribution state.
3407 * @brief Return the @f$ k @f$ parameter of the distribution.
3411 { return _M_param
.k(); }
3414 * @brief Return the @f$ p @f$ parameter of the distribution.
3418 { return _M_param
.p(); }
3421 * @brief Returns the parameter set of the distribution.
3425 { return _M_param
; }
3428 * @brief Sets the parameter set of the distribution.
3429 * @param __param The new parameter set of the distribution.
3432 param(const param_type
& __param
)
3433 { _M_param
= __param
; }
3436 * @brief Returns the greatest lower bound value of the distribution.
3440 { return result_type(0); }
3443 * @brief Returns the least upper bound value of the distribution.
3447 { return std::numeric_limits
<result_type
>::max(); }
3449 template<typename _UniformRandomNumberGenerator
>
3451 operator()(_UniformRandomNumberGenerator
& __urng
)
3452 { return this->operator()(__urng
, this->param()); }
3454 template<typename _UniformRandomNumberGenerator
>
3456 operator()(_UniformRandomNumberGenerator
& __urng
,
3457 const param_type
& __p
);
3460 param_type _M_param
;
3464 * @brief Return true if two negative binomial distributions have
3465 * the same parameters.
3467 template<typename _IntType
>
3469 operator==(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3470 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3471 { return __d1
.param() == __d2
.param(); }
3474 * @brief Inserts a %negative_binomial_distribution random
3475 * number distribution @p __x into the output stream @p __os.
3477 * @param __os An output stream.
3478 * @param __x A %negative_binomial_distribution random number
3481 * @returns The output stream with the state of @p __x inserted or in
3484 template<typename _IntType
, typename _CharT
, typename _Traits
>
3485 std::basic_ostream
<_CharT
, _Traits
>&
3486 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3487 const std::negative_binomial_distribution
<_IntType
>&);
3490 * @brief Extracts a %negative_binomial_distribution random number
3491 * distribution @p __x from the input stream @p __is.
3493 * @param __is An input stream.
3494 * @param __x A %negative_binomial_distribution random number
3497 * @returns The input stream with @p __x extracted or in an error state.
3499 template<typename _IntType
, typename _CharT
, typename _Traits
>
3500 std::basic_istream
<_CharT
, _Traits
>&
3501 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3502 std::negative_binomial_distribution
<_IntType
>&);
3504 /* @} */ // group std_random_distributions_bernoulli
3507 * @addtogroup std_random_distributions_poisson Poisson Distributions
3508 * @ingroup std_random_distributions
3513 * @brief A discrete Poisson random number distribution.
3515 * The formula for the Poisson probability density function is
3516 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3517 * parameter of the distribution.
3519 template<typename _IntType
= int>
3520 class poisson_distribution
3522 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3525 /** The type of the range of the distribution. */
3526 typedef _IntType result_type
;
3527 /** Parameter type. */
3530 typedef poisson_distribution
<_IntType
> distribution_type
;
3531 friend class poisson_distribution
<_IntType
>;
3534 param_type(double __mean
= 1.0)
3537 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3546 operator==(const param_type
& __p1
, const param_type
& __p2
)
3547 { return __p1
._M_mean
== __p2
._M_mean
; }
3550 // Hosts either log(mean) or the threshold of the simple method.
3557 #if _GLIBCXX_USE_C99_MATH_TR1
3558 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3562 // constructors and member function
3564 poisson_distribution(double __mean
= 1.0)
3565 : _M_param(__mean
), _M_nd()
3569 poisson_distribution(const param_type
& __p
)
3570 : _M_param(__p
), _M_nd()
3574 * @brief Resets the distribution state.
3581 * @brief Returns the distribution parameter @p mean.
3585 { return _M_param
.mean(); }
3588 * @brief Returns the parameter set of the distribution.
3592 { return _M_param
; }
3595 * @brief Sets the parameter set of the distribution.
3596 * @param __param The new parameter set of the distribution.
3599 param(const param_type
& __param
)
3600 { _M_param
= __param
; }
3603 * @brief Returns the greatest lower bound value of the distribution.
3610 * @brief Returns the least upper bound value of the distribution.
3614 { return std::numeric_limits
<result_type
>::max(); }
3616 template<typename _UniformRandomNumberGenerator
>
3618 operator()(_UniformRandomNumberGenerator
& __urng
)
3619 { return this->operator()(__urng
, this->param()); }
3621 template<typename _UniformRandomNumberGenerator
>
3623 operator()(_UniformRandomNumberGenerator
& __urng
,
3624 const param_type
& __p
);
3627 * @brief Return true if two Poisson distributions have the same
3630 template<typename _IntType1
>
3632 operator==(const std::poisson_distribution
<_IntType1
>& __d1
,
3633 const std::poisson_distribution
<_IntType1
>& __d2
)
3634 { return ((__d1
.param() == __d2
.param())
3635 && (__d1
._M_nd
== __d2
._M_nd
)); }
3638 * @brief Inserts a %poisson_distribution random number distribution
3639 * @p __x into the output stream @p __os.
3641 * @param __os An output stream.
3642 * @param __x A %poisson_distribution random number distribution.
3644 * @returns The output stream with the state of @p __x inserted or in
3647 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3648 friend std::basic_ostream
<_CharT
, _Traits
>&
3649 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3650 const std::poisson_distribution
<_IntType1
>&);
3653 * @brief Extracts a %poisson_distribution random number distribution
3654 * @p __x from the input stream @p __is.
3656 * @param __is An input stream.
3657 * @param __x A %poisson_distribution random number generator engine.
3659 * @returns The input stream with @p __x extracted or in an error
3662 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3663 friend std::basic_istream
<_CharT
, _Traits
>&
3664 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3665 std::poisson_distribution
<_IntType1
>&);
3668 param_type _M_param
;
3670 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3671 normal_distribution
<double> _M_nd
;
3675 * @brief An exponential continuous distribution for random numbers.
3677 * The formula for the exponential probability density function is
3678 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3680 * <table border=1 cellpadding=10 cellspacing=0>
3681 * <caption align=top>Distribution Statistics</caption>
3682 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3683 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3684 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3685 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3686 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3689 template<typename _RealType
= double>
3690 class exponential_distribution
3693 /** The type of the range of the distribution. */
3694 typedef _RealType result_type
;
3695 /** Parameter type. */
3698 typedef exponential_distribution
<_RealType
> distribution_type
;
3701 param_type(_RealType __lambda
= _RealType(1))
3702 : _M_lambda(__lambda
)
3704 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
3709 { return _M_lambda
; }
3712 operator==(const param_type
& __p1
, const param_type
& __p2
)
3713 { return __p1
._M_lambda
== __p2
._M_lambda
; }
3716 _RealType _M_lambda
;
3721 * @brief Constructs an exponential distribution with inverse scale
3722 * parameter @f$ \lambda @f$.
3725 exponential_distribution(const result_type
& __lambda
= result_type(1))
3726 : _M_param(__lambda
)
3730 exponential_distribution(const param_type
& __p
)
3735 * @brief Resets the distribution state.
3737 * Has no effect on exponential distributions.
3743 * @brief Returns the inverse scale parameter of the distribution.
3747 { return _M_param
.lambda(); }
3750 * @brief Returns the parameter set of the distribution.
3754 { return _M_param
; }
3757 * @brief Sets the parameter set of the distribution.
3758 * @param __param The new parameter set of the distribution.
3761 param(const param_type
& __param
)
3762 { _M_param
= __param
; }
3765 * @brief Returns the greatest lower bound value of the distribution.
3769 { return result_type(0); }
3772 * @brief Returns the least upper bound value of the distribution.
3776 { return std::numeric_limits
<result_type
>::max(); }
3778 template<typename _UniformRandomNumberGenerator
>
3780 operator()(_UniformRandomNumberGenerator
& __urng
)
3782 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3784 return -std::log(__aurng()) / this->lambda();
3787 template<typename _UniformRandomNumberGenerator
>
3789 operator()(_UniformRandomNumberGenerator
& __urng
,
3790 const param_type
& __p
)
3792 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3794 return -std::log(__aurng()) / __p
.lambda();
3798 param_type _M_param
;
3802 * @brief Return true if two exponential distributions have the same
3805 template<typename _RealType
>
3807 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
3808 const std::exponential_distribution
<_RealType
>& __d2
)
3809 { return __d1
.param() == __d2
.param(); }
3812 * @brief Inserts a %exponential_distribution random number distribution
3813 * @p __x into the output stream @p __os.
3815 * @param __os An output stream.
3816 * @param __x A %exponential_distribution random number distribution.
3818 * @returns The output stream with the state of @p __x inserted or in
3821 template<typename _RealType
, typename _CharT
, typename _Traits
>
3822 std::basic_ostream
<_CharT
, _Traits
>&
3823 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3824 const std::exponential_distribution
<_RealType
>&);
3827 * @brief Extracts a %exponential_distribution random number distribution
3828 * @p __x from the input stream @p __is.
3830 * @param __is An input stream.
3831 * @param __x A %exponential_distribution random number
3834 * @returns The input stream with @p __x extracted or in an error state.
3836 template<typename _RealType
, typename _CharT
, typename _Traits
>
3837 std::basic_istream
<_CharT
, _Traits
>&
3838 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3839 std::exponential_distribution
<_RealType
>&);
3843 * @brief A gamma continuous distribution for random numbers.
3845 * The formula for the gamma probability density function is
3846 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
3847 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
3849 template<typename _RealType
= double>
3850 class gamma_distribution
3853 /** The type of the range of the distribution. */
3854 typedef _RealType result_type
;
3855 /** Parameter type. */
3858 typedef gamma_distribution
<_RealType
> distribution_type
;
3859 friend class gamma_distribution
<_RealType
>;
3862 param_type(_RealType __alpha
= _RealType(1),
3863 _RealType __beta
= _RealType(1))
3864 : _M_alpha(__alpha
), _M_beta(__beta
)
3866 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
3872 { return _M_alpha
; }
3879 operator==(const param_type
& __p1
, const param_type
& __p2
)
3880 { return ((__p1
._M_alpha
== __p2
._M_alpha
)
3881 && (__p1
._M_beta
== __p2
._M_beta
)); }
3890 // Hosts either lambda of GB or d of modified Vaduva's.
3896 * @brief Constructs a gamma distribution with parameters
3897 * @f$ \alpha @f$ and @f$ \beta @f$.
3900 gamma_distribution(_RealType __alpha
= _RealType(1),
3901 _RealType __beta
= _RealType(1))
3902 : _M_param(__alpha
, __beta
)
3906 gamma_distribution(const param_type
& __p
)
3911 * @brief Resets the distribution state.
3913 * Does nothing for the gamma distribution.
3919 * @brief Returns the @f$ \alpha @f$ of the distribution.
3923 { return _M_param
.alpha(); }
3926 * @brief Returns the @f$ \beta @f$ of the distribution.
3930 { return _M_param
.beta(); }
3933 * @brief Returns the parameter set of the distribution.
3937 { return _M_param
; }
3940 * @brief Sets the parameter set of the distribution.
3941 * @param __param The new parameter set of the distribution.
3944 param(const param_type
& __param
)
3945 { _M_param
= __param
; }
3948 * @brief Returns the greatest lower bound value of the distribution.
3952 { return result_type(0); }
3955 * @brief Returns the least upper bound value of the distribution.
3959 { return std::numeric_limits
<result_type
>::max(); }
3961 template<typename _UniformRandomNumberGenerator
>
3963 operator()(_UniformRandomNumberGenerator
& __urng
)
3964 { return this->operator()(__urng
, this->param()); }
3966 template<typename _UniformRandomNumberGenerator
>
3968 operator()(_UniformRandomNumberGenerator
& __urng
,
3969 const param_type
& __p
);
3972 param_type _M_param
;
3976 * @brief Return true if two gamma distributions have the same
3979 template<typename _RealType
>
3981 operator==(const std::gamma_distribution
<_RealType
>& __d1
,
3982 const std::gamma_distribution
<_RealType
>& __d2
)
3983 { return __d1
.param() == __d2
.param(); }
3986 * @brief Inserts a %gamma_distribution random number distribution
3987 * @p __x into the output stream @p __os.
3989 * @param __os An output stream.
3990 * @param __x A %gamma_distribution random number distribution.
3992 * @returns The output stream with the state of @p __x inserted or in
3995 template<typename _RealType
, typename _CharT
, typename _Traits
>
3996 std::basic_ostream
<_CharT
, _Traits
>&
3997 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3998 const std::gamma_distribution
<_RealType
>&);
4001 * @brief Extracts a %gamma_distribution random number distribution
4002 * @p __x from the input stream @p __is.
4004 * @param __is An input stream.
4005 * @param __x A %gamma_distribution random number generator engine.
4007 * @returns The input stream with @p __x extracted or in an error state.
4009 template<typename _RealType
, typename _CharT
, typename _Traits
>
4010 std::basic_istream
<_CharT
, _Traits
>&
4011 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4012 std::gamma_distribution
<_RealType
>&);
4016 * @brief A weibull_distribution random number distribution.
4018 * The formula for the normal probability density function is
4019 * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
4020 * \exp{(-(frac{x}{b})^a)} @f$.
4022 template<typename _RealType
= double>
4023 class weibull_distribution
4026 /** The type of the range of the distribution. */
4027 typedef _RealType result_type
;
4028 /** Parameter type. */
4031 typedef weibull_distribution
<_RealType
> distribution_type
;
4034 param_type(_RealType __a
= _RealType(1),
4035 _RealType __b
= _RealType(1))
4036 : _M_a(__a
), _M_b(__b
)
4048 operator==(const param_type
& __p1
, const param_type
& __p2
)
4049 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
4057 weibull_distribution(_RealType __a
= _RealType(1),
4058 _RealType __b
= _RealType(1))
4059 : _M_param(__a
, __b
)
4063 weibull_distribution(const param_type
& __p
)
4068 * @brief Resets the distribution state.
4075 * @brief Return the @f$ a @f$ parameter of the distribution.
4079 { return _M_param
.a(); }
4082 * @brief Return the @f$ b @f$ parameter of the distribution.
4086 { return _M_param
.b(); }
4089 * @brief Returns the parameter set of the distribution.
4093 { return _M_param
; }
4096 * @brief Sets the parameter set of the distribution.
4097 * @param __param The new parameter set of the distribution.
4100 param(const param_type
& __param
)
4101 { _M_param
= __param
; }
4104 * @brief Returns the greatest lower bound value of the distribution.
4108 { return result_type(0); }
4111 * @brief Returns the least upper bound value of the distribution.
4115 { return std::numeric_limits
<result_type
>::max(); }
4117 template<typename _UniformRandomNumberGenerator
>
4119 operator()(_UniformRandomNumberGenerator
& __urng
)
4120 { return this->operator()(__urng
, this->param()); }
4122 template<typename _UniformRandomNumberGenerator
>
4124 operator()(_UniformRandomNumberGenerator
& __urng
,
4125 const param_type
& __p
)
4127 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4129 return __p
.b() * std::pow(-std::log(__aurng()),
4130 result_type(1) / __p
.a());
4134 param_type _M_param
;
4138 * @brief Return true if two Weibull distributions have the same
4141 template<typename _RealType
>
4143 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4144 const std::weibull_distribution
<_RealType
>& __d2
)
4145 { return __d1
.param() == __d2
.param(); }
4148 * @brief Inserts a %weibull_distribution random number distribution
4149 * @p __x into the output stream @p __os.
4151 * @param __os An output stream.
4152 * @param __x A %weibull_distribution random number distribution.
4154 * @returns The output stream with the state of @p __x inserted or in
4157 template<typename _RealType
, typename _CharT
, typename _Traits
>
4158 std::basic_ostream
<_CharT
, _Traits
>&
4159 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4160 const std::weibull_distribution
<_RealType
>&);
4163 * @brief Extracts a %weibull_distribution random number distribution
4164 * @p __x from the input stream @p __is.
4166 * @param __is An input stream.
4167 * @param __x A %weibull_distribution random number
4170 * @returns The input stream with @p __x extracted or in an error state.
4172 template<typename _RealType
, typename _CharT
, typename _Traits
>
4173 std::basic_istream
<_CharT
, _Traits
>&
4174 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4175 std::weibull_distribution
<_RealType
>&);
4179 * @brief A extreme_value_distribution random number distribution.
4181 * The formula for the normal probability mass function is
4182 * @f$ p(x|a,b) = \frac{1}{b}
4183 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
4185 template<typename _RealType
= double>
4186 class extreme_value_distribution
4189 /** The type of the range of the distribution. */
4190 typedef _RealType result_type
;
4191 /** Parameter type. */
4194 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4197 param_type(_RealType __a
= _RealType(0),
4198 _RealType __b
= _RealType(1))
4199 : _M_a(__a
), _M_b(__b
)
4211 operator==(const param_type
& __p1
, const param_type
& __p2
)
4212 { return (__p1
._M_a
== __p2
._M_a
) && (__p1
._M_b
== __p2
._M_b
); }
4220 extreme_value_distribution(_RealType __a
= _RealType(0),
4221 _RealType __b
= _RealType(1))
4222 : _M_param(__a
, __b
)
4226 extreme_value_distribution(const param_type
& __p
)
4231 * @brief Resets the distribution state.
4238 * @brief Return the @f$ a @f$ parameter of the distribution.
4242 { return _M_param
.a(); }
4245 * @brief Return the @f$ b @f$ parameter of the distribution.
4249 { return _M_param
.b(); }
4252 * @brief Returns the parameter set of the distribution.
4256 { return _M_param
; }
4259 * @brief Sets the parameter set of the distribution.
4260 * @param __param The new parameter set of the distribution.
4263 param(const param_type
& __param
)
4264 { _M_param
= __param
; }
4267 * @brief Returns the greatest lower bound value of the distribution.
4271 { return std::numeric_limits
<result_type
>::min(); }
4274 * @brief Returns the least upper bound value of the distribution.
4278 { return std::numeric_limits
<result_type
>::max(); }
4280 template<typename _UniformRandomNumberGenerator
>
4282 operator()(_UniformRandomNumberGenerator
& __urng
)
4283 { return this->operator()(__urng
, this->param()); }
4285 template<typename _UniformRandomNumberGenerator
>
4287 operator()(_UniformRandomNumberGenerator
& __urng
,
4288 const param_type
& __p
);
4291 param_type _M_param
;
4297 template<typename _RealType
>
4299 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4300 const std::extreme_value_distribution
<_RealType
>& __d2
)
4301 { return __d1
.param() == __d2
.param(); }
4304 * @brief Inserts a %extreme_value_distribution random number distribution
4305 * @p __x into the output stream @p __os.
4307 * @param __os An output stream.
4308 * @param __x A %extreme_value_distribution random number distribution.
4310 * @returns The output stream with the state of @p __x inserted or in
4313 template<typename _RealType
, typename _CharT
, typename _Traits
>
4314 std::basic_ostream
<_CharT
, _Traits
>&
4315 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4316 const std::extreme_value_distribution
<_RealType
>&);
4319 * @brief Extracts a %extreme_value_distribution random number
4320 * distribution @p __x from the input stream @p __is.
4322 * @param __is An input stream.
4323 * @param __x A %extreme_value_distribution random number
4326 * @returns The input stream with @p __x extracted or in an error state.
4328 template<typename _RealType
, typename _CharT
, typename _Traits
>
4329 std::basic_istream
<_CharT
, _Traits
>&
4330 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4331 std::extreme_value_distribution
<_RealType
>&);
4335 * @brief A discrete_distribution random number distribution.
4337 * The formula for the discrete probability mass function is
4340 template<typename _IntType
= int>
4341 class discrete_distribution
4343 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
4346 /** The type of the range of the distribution. */
4347 typedef _IntType result_type
;
4348 /** Parameter type. */
4351 typedef discrete_distribution
<_IntType
> distribution_type
;
4352 friend class discrete_distribution
<_IntType
>;
4355 : _M_prob(), _M_cp()
4356 { _M_initialize(); }
4358 template<typename _InputIterator
>
4359 param_type(_InputIterator __wbegin
,
4360 _InputIterator __wend
)
4361 : _M_prob(__wbegin
, __wend
), _M_cp()
4362 { _M_initialize(); }
4364 param_type(initializer_list
<double> __wil
)
4365 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4366 { _M_initialize(); }
4368 template<typename _Func
>
4369 param_type(size_t __nw
, double __xmin
, double __xmax
,
4373 probabilities() const
4377 operator==(const param_type
& __p1
, const param_type
& __p2
)
4378 { return __p1
._M_prob
== __p2
._M_prob
; }
4384 std::vector
<double> _M_prob
;
4385 std::vector
<double> _M_cp
;
4388 discrete_distribution()
4392 template<typename _InputIterator
>
4393 discrete_distribution(_InputIterator __wbegin
,
4394 _InputIterator __wend
)
4395 : _M_param(__wbegin
, __wend
)
4398 discrete_distribution(initializer_list
<double> __wil
)
4402 template<typename _Func
>
4403 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4405 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4409 discrete_distribution(const param_type
& __p
)
4414 * @brief Resets the distribution state.
4421 * @brief Returns the probabilities of the distribution.
4424 probabilities() const
4425 { return _M_param
.probabilities(); }
4428 * @brief Returns the parameter set of the distribution.
4432 { return _M_param
; }
4435 * @brief Sets the parameter set of the distribution.
4436 * @param __param The new parameter set of the distribution.
4439 param(const param_type
& __param
)
4440 { _M_param
= __param
; }
4443 * @brief Returns the greatest lower bound value of the distribution.
4447 { return result_type(0); }
4450 * @brief Returns the least upper bound value of the distribution.
4454 { return this->_M_param
._M_prob
.size() - 1; }
4456 template<typename _UniformRandomNumberGenerator
>
4458 operator()(_UniformRandomNumberGenerator
& __urng
)
4459 { return this->operator()(__urng
, this->param()); }
4461 template<typename _UniformRandomNumberGenerator
>
4463 operator()(_UniformRandomNumberGenerator
& __urng
,
4464 const param_type
& __p
);
4467 * @brief Inserts a %discrete_distribution random number distribution
4468 * @p __x into the output stream @p __os.
4470 * @param __os An output stream.
4471 * @param __x A %discrete_distribution random number distribution.
4473 * @returns The output stream with the state of @p __x inserted or in
4476 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4477 friend std::basic_ostream
<_CharT
, _Traits
>&
4478 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4479 const std::discrete_distribution
<_IntType1
>&);
4482 * @brief Extracts a %discrete_distribution random number distribution
4483 * @p __x from the input stream @p __is.
4485 * @param __is An input stream.
4486 * @param __x A %discrete_distribution random number
4489 * @returns The input stream with @p __x extracted or in an error
4492 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4493 friend std::basic_istream
<_CharT
, _Traits
>&
4494 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4495 std::discrete_distribution
<_IntType1
>&);
4498 param_type _M_param
;
4504 template<typename _IntType
>
4506 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4507 const std::discrete_distribution
<_IntType
>& __d2
)
4508 { return __d1
.param() == __d2
.param(); }
4512 * @brief A piecewise_constant_distribution random number distribution.
4514 * The formula for the piecewise constant probability mass function is
4517 template<typename _RealType
= double>
4518 class piecewise_constant_distribution
4521 /** The type of the range of the distribution. */
4522 typedef _RealType result_type
;
4523 /** Parameter type. */
4526 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4527 friend class piecewise_constant_distribution
<_RealType
>;
4531 template<typename _InputIteratorB
, typename _InputIteratorW
>
4532 param_type(_InputIteratorB __bfirst
,
4533 _InputIteratorB __bend
,
4534 _InputIteratorW __wbegin
);
4536 template<typename _Func
>
4537 param_type(initializer_list
<_RealType
> __bil
, _Func __fw
);
4539 template<typename _Func
>
4540 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4543 std::vector
<_RealType
>
4552 operator==(const param_type
& __p1
, const param_type
& __p2
)
4553 { return ((__p1
._M_int
== __p2
._M_int
)
4554 && (__p1
._M_den
== __p2
._M_den
)); }
4560 std::vector
<_RealType
> _M_int
;
4561 std::vector
<double> _M_den
;
4562 std::vector
<double> _M_cp
;
4566 piecewise_constant_distribution()
4570 template<typename _InputIteratorB
, typename _InputIteratorW
>
4571 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4572 _InputIteratorB __bend
,
4573 _InputIteratorW __wbegin
)
4574 : _M_param(__bfirst
, __bend
, __wbegin
)
4577 template<typename _Func
>
4578 piecewise_constant_distribution(initializer_list
<_RealType
> __bil
,
4580 : _M_param(__bil
, __fw
)
4583 template<typename _Func
>
4584 piecewise_constant_distribution(size_t __nw
,
4585 _RealType __xmin
, _RealType __xmax
,
4587 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4591 piecewise_constant_distribution(const param_type
& __p
)
4596 * @brief Resets the distribution state.
4603 * @brief Returns a vector of the intervals.
4605 std::vector
<_RealType
>
4607 { return _M_param
.intervals(); }
4610 * @brief Returns a vector of the probability densities.
4614 { return _M_param
.densities(); }
4617 * @brief Returns the parameter set of the distribution.
4621 { return _M_param
; }
4624 * @brief Sets the parameter set of the distribution.
4625 * @param __param The new parameter set of the distribution.
4628 param(const param_type
& __param
)
4629 { _M_param
= __param
; }
4632 * @brief Returns the greatest lower bound value of the distribution.
4636 { return this->_M_param
._M_int
.front(); }
4639 * @brief Returns the least upper bound value of the distribution.
4643 { return this->_M_param
._M_int
.back(); }
4645 template<typename _UniformRandomNumberGenerator
>
4647 operator()(_UniformRandomNumberGenerator
& __urng
)
4648 { return this->operator()(__urng
, this->param()); }
4650 template<typename _UniformRandomNumberGenerator
>
4652 operator()(_UniformRandomNumberGenerator
& __urng
,
4653 const param_type
& __p
);
4656 * @brief Inserts a %piecewise_constan_distribution random
4657 * number distribution @p __x into the output stream @p __os.
4659 * @param __os An output stream.
4660 * @param __x A %piecewise_constan_distribution random number
4663 * @returns The output stream with the state of @p __x inserted or in
4666 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4667 friend std::basic_ostream
<_CharT
, _Traits
>&
4668 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4669 const std::piecewise_constant_distribution
<_RealType1
>&);
4672 * @brief Extracts a %piecewise_constan_distribution random
4673 * number distribution @p __x from the input stream @p __is.
4675 * @param __is An input stream.
4676 * @param __x A %piecewise_constan_distribution random number
4679 * @returns The input stream with @p __x extracted or in an error
4682 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4683 friend std::basic_istream
<_CharT
, _Traits
>&
4684 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4685 std::piecewise_constant_distribution
<_RealType1
>&);
4688 param_type _M_param
;
4694 template<typename _RealType
>
4696 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
4697 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
4698 { return __d1
.param() == __d2
.param(); }
4702 * @brief A piecewise_linear_distribution random number distribution.
4704 * The formula for the piecewise linear probability mass function is
4707 template<typename _RealType
= double>
4708 class piecewise_linear_distribution
4711 /** The type of the range of the distribution. */
4712 typedef _RealType result_type
;
4713 /** Parameter type. */
4716 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
4717 friend class piecewise_linear_distribution
<_RealType
>;
4721 template<typename _InputIteratorB
, typename _InputIteratorW
>
4722 param_type(_InputIteratorB __bfirst
,
4723 _InputIteratorB __bend
,
4724 _InputIteratorW __wbegin
);
4726 template<typename _Func
>
4727 param_type(initializer_list
<_RealType
> __bil
, _Func __fw
);
4729 template<typename _Func
>
4730 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4733 std::vector
<_RealType
>
4742 operator==(const param_type
& __p1
, const param_type
& __p2
)
4743 { return ((__p1
._M_int
== __p2
._M_int
)
4744 && (__p1
._M_den
== __p2
._M_den
)); }
4750 std::vector
<_RealType
> _M_int
;
4751 std::vector
<double> _M_den
;
4752 std::vector
<double> _M_cp
;
4753 std::vector
<double> _M_m
;
4757 piecewise_linear_distribution()
4761 template<typename _InputIteratorB
, typename _InputIteratorW
>
4762 piecewise_linear_distribution(_InputIteratorB __bfirst
,
4763 _InputIteratorB __bend
,
4764 _InputIteratorW __wbegin
)
4765 : _M_param(__bfirst
, __bend
, __wbegin
)
4768 template<typename _Func
>
4769 piecewise_linear_distribution(initializer_list
<_RealType
> __bil
,
4771 : _M_param(__bil
, __fw
)
4774 template<typename _Func
>
4775 piecewise_linear_distribution(size_t __nw
,
4776 _RealType __xmin
, _RealType __xmax
,
4778 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4782 piecewise_linear_distribution(const param_type
& __p
)
4787 * Resets the distribution state.
4794 * @brief Return the intervals of the distribution.
4796 std::vector
<_RealType
>
4798 { return _M_param
.intervals(); }
4801 * @brief Return a vector of the probability densities of the
4806 { return _M_param
.densities(); }
4809 * @brief Returns the parameter set of the distribution.
4813 { return _M_param
; }
4816 * @brief Sets the parameter set of the distribution.
4817 * @param __param The new parameter set of the distribution.
4820 param(const param_type
& __param
)
4821 { _M_param
= __param
; }
4824 * @brief Returns the greatest lower bound value of the distribution.
4828 { return this->_M_param
._M_int
.front(); }
4831 * @brief Returns the least upper bound value of the distribution.
4835 { return this->_M_param
._M_int
.back(); }
4837 template<typename _UniformRandomNumberGenerator
>
4839 operator()(_UniformRandomNumberGenerator
& __urng
)
4840 { return this->operator()(__urng
, this->param()); }
4842 template<typename _UniformRandomNumberGenerator
>
4844 operator()(_UniformRandomNumberGenerator
& __urng
,
4845 const param_type
& __p
);
4848 * @brief Inserts a %piecewise_linear_distribution random number
4849 * distribution @p __x into the output stream @p __os.
4851 * @param __os An output stream.
4852 * @param __x A %piecewise_linear_distribution random number
4855 * @returns The output stream with the state of @p __x inserted or in
4858 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4859 friend std::basic_ostream
<_CharT
, _Traits
>&
4860 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4861 const std::piecewise_linear_distribution
<_RealType1
>&);
4864 * @brief Extracts a %piecewise_linear_distribution random number
4865 * distribution @p __x from the input stream @p __is.
4867 * @param __is An input stream.
4868 * @param __x A %piecewise_linear_distribution random number
4871 * @returns The input stream with @p __x extracted or in an error
4874 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4875 friend std::basic_istream
<_CharT
, _Traits
>&
4876 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4877 std::piecewise_linear_distribution
<_RealType1
>&);
4880 param_type _M_param
;
4886 template<typename _RealType
>
4888 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
4889 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
4890 { return __d1
.param() == __d2
.param(); }
4892 /* @} */ // group std_random_distributions_poisson
4894 /* @} */ // group std_random_distributions
4897 * @addtogroup std_random_utilities Random Number Utilities
4898 * @ingroup std_random
4903 * @brief The seed_seq class generates sequences of seeds for random
4904 * number generators.
4910 /** The type of the seed vales. */
4911 typedef uint_least32_t result_type
;
4913 /** Default constructor. */
4918 template<typename _IntType
>
4919 seed_seq(std::initializer_list
<_IntType
> il
);
4921 template<typename _InputIterator
>
4922 seed_seq(_InputIterator __begin
, _InputIterator __end
);
4924 // generating functions
4925 template<typename _RandomAccessIterator
>
4927 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
4929 // property functions
4931 { return _M_v
.size(); }
4933 template<typename OutputIterator
>
4935 param(OutputIterator __dest
) const
4936 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
4940 std::vector
<result_type
> _M_v
;
4943 /* @} */ // group std_random_utilities
4945 /* @} */ // group std_random