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 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
35 // [26.4] Random number generation
38 * @addtogroup std_random Random Number Generation
39 * A facility for generating random numbers on selected distributions.
44 * @brief A function template for converting the output of a (integral)
45 * uniform random number generator to a floatng point result in the range
48 template<typename _RealType
, size_t __bits
,
49 typename _UniformRandomNumberGenerator
>
51 generate_canonical(_UniformRandomNumberGenerator
& __g
);
56 * Implementation-space details.
60 template<typename _UIntType
, size_t __w
,
61 bool = __w
< static_cast<size_t>
62 (std::numeric_limits
<_UIntType
>::digits
)>
64 { static const _UIntType __value
= 0; };
66 template<typename _UIntType
, size_t __w
>
67 struct _Shift
<_UIntType
, __w
, true>
68 { static const _UIntType __value
= _UIntType(1) << __w
; };
70 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool>
73 // Dispatch based on modulus value to prevent divide-by-zero compile-time
74 // errors when m == 0.
75 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
78 { return _Mod
<_Tp
, __m
, __a
, __c
, __m
== 0>::__calc(__x
); }
81 * An adaptor class for converting the output of any Generator into
82 * the input for a specific Distribution.
84 template<typename _Engine
, typename _DInputType
>
89 _Adaptor(_Engine
& __g
)
94 { return _DInputType(0); }
98 { return _DInputType(1); }
101 * Converts a value generated by the adapted random number generator
102 * into a value in the input domain for the dependent random number
108 return std::generate_canonical
<_DInputType
,
109 std::numeric_limits
<_DInputType
>::digits
,
116 } // namespace __detail
119 * @addtogroup std_random_generators Random Number Generators
120 * @ingroup std_random
122 * These classes define objects which provide random or pseudorandom
123 * numbers, either from a discrete or a continuous interval. The
124 * random number generator supplied as a part of this library are
125 * all uniform random number generators which provide a sequence of
126 * random number uniformly distributed over their range.
128 * A number generator is a function object with an operator() that
129 * takes zero arguments and returns a number.
131 * A compliant random number generator must satisfy the following
132 * requirements. <table border=1 cellpadding=10 cellspacing=0>
133 * <caption align=top>Random Number Generator Requirements</caption>
134 * <tr><td>To be documented.</td></tr> </table>
140 * @brief A model of a linear congruential random number generator.
142 * A random number generator that produces pseudorandom numbers using the
143 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
145 * The template parameter @p _UIntType must be an unsigned integral type
146 * large enough to store values up to (__m-1). If the template parameter
147 * @p __m is 0, the modulus @p __m used is
148 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
149 * parameters @p __a and @p __c must be less than @p __m.
151 * The size of the state is @f$ 1 @f$.
153 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
154 class linear_congruential_engine
156 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
157 "substituting _UIntType not an unsigned integral type");
158 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
159 "template argument substituting __m out of bounds");
162 /** The type of the generated random value. */
163 typedef _UIntType result_type
;
165 /** The multiplier. */
166 static const result_type multiplier
= __a
;
168 static const result_type increment
= __c
;
170 static const result_type modulus
= __m
;
171 static const result_type default_seed
= 1u;
174 * @brief Constructs a %linear_congruential_engine random number
175 * generator engine with seed @p __s. The default seed value
178 * @param __s The initial seed value.
181 linear_congruential_engine(result_type __s
= default_seed
)
185 * @brief Constructs a %linear_congruential_engine random number
186 * generator engine seeded from the seed sequence @p __q.
188 * @param __q the seed sequence.
191 linear_congruential_engine(seed_seq
& __q
)
195 * @brief Reseeds the %linear_congruential_engine random number generator
196 * engine sequence to the seed @p __s.
198 * @param __s The new seed.
201 seed(result_type __s
= default_seed
);
204 * @brief Reseeds the %linear_congruential_engine random number generator
206 * sequence using values from the seed sequence @p __q.
208 * @param __q the seed sequence.
214 * @brief Gets the smallest possible value in the output range.
216 * The minimum depends on the @p __c parameter: if it is zero, the
217 * minimum generated must be > 0, otherwise 0 is allowed.
219 * @todo This should be constexpr.
223 { return __c
== 0u ? 1u : 0u; }
226 * @brief Gets the largest possible value in the output range.
228 * @todo This should be constexpr.
235 * @brief Discard a sequence of random numbers.
237 * @todo Look for a faster way to do discard.
240 discard(unsigned long long __z
)
242 for (; __z
!= 0ULL; --__z
)
247 * @brief Gets the next random number in the sequence.
252 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
257 * @brief Compares two linear congruential random number generator
258 * objects of the same type for equality.
260 * @param __lhs A linear congruential random number generator object.
261 * @param __rhs Another linear congruential random number generator
264 * @returns true if the two objects are equal, false otherwise.
267 operator==(const linear_congruential_engine
& __lhs
,
268 const linear_congruential_engine
& __rhs
)
269 { return __lhs
._M_x
== __rhs
._M_x
; }
272 * @brief Writes the textual representation of the state x(i) of x to
275 * @param __os The output stream.
276 * @param __lcr A % linear_congruential_engine random number generator.
279 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
280 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
281 friend std::basic_ostream
<_CharT
, _Traits
>&
282 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
283 const std::linear_congruential_engine
<_UIntType1
,
287 * @brief Sets the state of the engine by reading its textual
288 * representation from @p __is.
290 * The textual representation must have been previously written using
291 * an output stream whose imbued locale and whose type's template
292 * specialization arguments _CharT and _Traits were the same as those
295 * @param __is The input stream.
296 * @param __lcr A % linear_congruential_engine random number generator.
299 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
300 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
301 friend std::basic_istream
<_CharT
, _Traits
>&
302 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
303 std::linear_congruential_engine
<_UIntType1
, __a1
,
312 * A generalized feedback shift register discrete random number generator.
314 * This algorithm avoids multiplication and division and is designed to be
315 * friendly to a pipelined architecture. If the parameters are chosen
316 * correctly, this generator will produce numbers with a very long period and
317 * fairly good apparent entropy, although still not cryptographically strong.
319 * The best way to use this generator is with the predefined mt19937 class.
321 * This algorithm was originally invented by Makoto Matsumoto and
324 * @var word_size The number of bits in each element of the state vector.
325 * @var state_size The degree of recursion.
326 * @var shift_size The period parameter.
327 * @var mask_bits The separation point bit index.
328 * @var parameter_a The last row of the twist matrix.
329 * @var output_u The first right-shift tempering matrix parameter.
330 * @var output_s The first left-shift tempering matrix parameter.
331 * @var output_b The first left-shift tempering matrix mask.
332 * @var output_t The second left-shift tempering matrix parameter.
333 * @var output_c The second left-shift tempering matrix mask.
334 * @var output_l The second right-shift tempering matrix parameter.
336 template<typename _UIntType
, size_t __w
,
337 size_t __n
, size_t __m
, size_t __r
,
338 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
339 _UIntType __b
, size_t __t
,
340 _UIntType __c
, size_t __l
, _UIntType __f
>
341 class mersenne_twister_engine
343 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
344 "substituting _UIntType not an unsigned integral type");
345 static_assert(1u <= __m
&& __m
<= __n
,
346 "template argument substituting __m out of bounds");
347 static_assert(__r
<= __w
, "template argument substituting "
349 static_assert(__u
<= __w
, "template argument substituting "
351 static_assert(__s
<= __w
, "template argument substituting "
353 static_assert(__t
<= __w
, "template argument substituting "
355 static_assert(__l
<= __w
, "template argument substituting "
357 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
358 "template argument substituting __w out of bound");
359 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
360 "template argument substituting __a out of bound");
361 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
362 "template argument substituting __b out of bound");
363 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
364 "template argument substituting __c out of bound");
365 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
366 "template argument substituting __d out of bound");
367 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
368 "template argument substituting __f out of bound");
371 /** The type of the generated random value. */
372 typedef _UIntType result_type
;
375 static const size_t word_size
= __w
;
376 static const size_t state_size
= __n
;
377 static const size_t shift_size
= __m
;
378 static const size_t mask_bits
= __r
;
379 static const result_type xor_mask
= __a
;
380 static const size_t tempering_u
= __u
;
381 static const result_type tempering_d
= __d
;
382 static const size_t tempering_s
= __s
;
383 static const result_type tempering_b
= __b
;
384 static const size_t tempering_t
= __t
;
385 static const result_type tempering_c
= __c
;
386 static const size_t tempering_l
= __l
;
387 static const result_type initialization_multiplier
= __f
;
388 static const result_type default_seed
= 5489u;
390 // constructors and member function
392 mersenne_twister_engine(result_type __sd
= default_seed
)
396 * @brief Constructs a %mersenne_twister_engine random number generator
397 * engine seeded from the seed sequence @p __q.
399 * @param __q the seed sequence.
402 mersenne_twister_engine(seed_seq
& __q
)
406 seed(result_type __sd
= default_seed
);
412 * @brief Gets the smallest possible value in the output range.
414 * @todo This should be constexpr.
421 * @brief Gets the largest possible value in the output range.
423 * @todo This should be constexpr.
427 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
430 * @brief Discard a sequence of random numbers.
432 * @todo Look for a faster way to do discard.
435 discard(unsigned long long __z
)
437 for (; __z
!= 0ULL; --__z
)
445 * @brief Compares two % mersenne_twister_engine random number generator
446 * objects of the same type for equality.
448 * @param __lhs A % mersenne_twister_engine random number generator
450 * @param __rhs Another % mersenne_twister_engine random number
453 * @returns true if the two objects are equal, false otherwise.
456 operator==(const mersenne_twister_engine
& __lhs
,
457 const mersenne_twister_engine
& __rhs
)
458 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
461 * @brief Inserts the current state of a % mersenne_twister_engine
462 * random number generator engine @p __x into the output stream
465 * @param __os An output stream.
466 * @param __x A % mersenne_twister_engine random number generator
469 * @returns The output stream with the state of @p __x inserted or in
472 template<typename _UIntType1
,
473 size_t __w1
, size_t __n1
,
474 size_t __m1
, size_t __r1
,
475 _UIntType1 __a1
, size_t __u1
,
476 _UIntType1 __d1
, size_t __s1
,
477 _UIntType1 __b1
, size_t __t1
,
478 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
479 typename _CharT
, typename _Traits
>
480 friend std::basic_ostream
<_CharT
, _Traits
>&
481 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
482 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
483 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
487 * @brief Extracts the current state of a % mersenne_twister_engine
488 * random number generator engine @p __x from the input stream
491 * @param __is An input stream.
492 * @param __x A % mersenne_twister_engine random number generator
495 * @returns The input stream with the state of @p __x extracted or in
498 template<typename _UIntType1
,
499 size_t __w1
, size_t __n1
,
500 size_t __m1
, size_t __r1
,
501 _UIntType1 __a1
, size_t __u1
,
502 _UIntType1 __d1
, size_t __s1
,
503 _UIntType1 __b1
, size_t __t1
,
504 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
505 typename _CharT
, typename _Traits
>
506 friend std::basic_istream
<_CharT
, _Traits
>&
507 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
508 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
509 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
513 _UIntType _M_x
[state_size
];
518 * @brief The Marsaglia-Zaman generator.
520 * This is a model of a Generalized Fibonacci discrete random number
521 * generator, sometimes referred to as the SWC generator.
523 * A discrete random number generator that produces pseudorandom
524 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
525 * carry_{i-1}) \bmod m @f$.
527 * The size of the state is @f$ r @f$
528 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
530 * @var _M_x The state of the generator. This is a ring buffer.
531 * @var _M_carry The carry.
532 * @var _M_p Current index of x(i - r).
534 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
535 class subtract_with_carry_engine
537 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
538 "substituting _UIntType not an unsigned integral type");
539 static_assert(0u < __s
&& __s
< __r
,
540 "template argument substituting __s out of bounds");
541 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
542 "template argument substituting __w out of bounds");
545 /** The type of the generated random value. */
546 typedef _UIntType result_type
;
549 static const size_t word_size
= __w
;
550 static const size_t short_lag
= __s
;
551 static const size_t long_lag
= __r
;
552 static const result_type default_seed
= 19780503u;
555 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
556 * random number generator.
559 subtract_with_carry_engine(result_type __sd
= default_seed
)
560 { this->seed(__sd
); }
563 * @brief Constructs a %subtract_with_carry_engine random number engine
564 * seeded from the seed sequence @p __q.
566 * @param __q the seed sequence.
569 subtract_with_carry_engine(seed_seq
& __q
)
573 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
576 * N1688[4.19] modifies this as follows. If @p __value == 0,
577 * sets value to 19780503. In any case, with a linear
578 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
579 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
580 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
581 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
582 * set carry to 1, otherwise sets carry to 0.
585 seed(result_type __sd
= default_seed
);
588 * @brief Seeds the initial state @f$ x_0 @f$ of the
589 * % subtract_with_carry_engine random number generator.
595 * @brief Gets the inclusive minimum value of the range of random
596 * integers returned by this generator.
598 * @todo This should be constexpr.
605 * @brief Gets the inclusive maximum value of the range of random
606 * integers returned by this generator.
608 * @todo This should be constexpr.
612 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
615 * @brief Discard a sequence of random numbers.
617 * @todo Look for a faster way to do discard.
620 discard(unsigned long long __z
)
622 for (; __z
!= 0ULL; --__z
)
627 * @brief Gets the next random number in the sequence.
633 * @brief Compares two % subtract_with_carry_engine random number
634 * generator objects of the same type for equality.
636 * @param __lhs A % subtract_with_carry_engine random number generator
638 * @param __rhs Another % subtract_with_carry_engine random number
641 * @returns true if the two objects are equal, false otherwise.
644 operator==(const subtract_with_carry_engine
& __lhs
,
645 const subtract_with_carry_engine
& __rhs
)
646 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
649 * @brief Inserts the current state of a % subtract_with_carry_engine
650 * random number generator engine @p __x into the output stream
653 * @param __os An output stream.
654 * @param __x A % subtract_with_carry_engine random number generator
657 * @returns The output stream with the state of @p __x inserted or in
660 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
661 typename _CharT
, typename _Traits
>
662 friend std::basic_ostream
<_CharT
, _Traits
>&
663 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
664 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
668 * @brief Extracts the current state of a % subtract_with_carry_engine
669 * random number generator engine @p __x from the input stream
672 * @param __is An input stream.
673 * @param __x A % subtract_with_carry_engine random number generator engine.
675 * @returns The input stream with the state of @p __x extracted or in
678 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
679 typename _CharT
, typename _Traits
>
680 friend std::basic_istream
<_CharT
, _Traits
>&
681 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
682 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
686 _UIntType _M_x
[long_lag
];
692 * Produces random numbers from some base engine by discarding blocks of
695 * 0 <= @p __r <= @p __p
697 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
698 class discard_block_engine
700 static_assert(1 <= __r
&& __r
<= __p
,
701 "template argument substituting __r out of bounds");
704 /** The type of the generated random value. */
705 typedef typename
_RandomNumberEngine::result_type result_type
;
708 static const size_t block_size
= __p
;
709 static const size_t used_block
= __r
;
712 * @brief Constructs a default %discard_block_engine engine.
714 * The underlying engine is default constructed as well.
716 discard_block_engine()
717 : _M_b(), _M_n(0) { }
720 * @brief Copy constructs a %discard_block_engine engine.
722 * Copies an existing base class random number generator.
723 * @param rng An existing (base class) engine object.
726 discard_block_engine(const _RandomNumberEngine
& __rne
)
727 : _M_b(__rne
), _M_n(0) { }
730 * @brief Move constructs a %discard_block_engine engine.
732 * Copies an existing base class random number generator.
733 * @param rng An existing (base class) engine object.
736 discard_block_engine(_RandomNumberEngine
&& __rne
)
737 : _M_b(std::move(__rne
)), _M_n(0) { }
740 * @brief Seed constructs a %discard_block_engine engine.
742 * Constructs the underlying generator engine seeded with @p __s.
743 * @param __s A seed value for the base class engine.
746 discard_block_engine(result_type __s
)
747 : _M_b(__s
), _M_n(0) { }
750 * @brief Generator construct a %discard_block_engine engine.
752 * @param __q A seed sequence.
755 discard_block_engine(seed_seq
& __q
)
760 * @brief Reseeds the %discard_block_engine object with the default
761 * seed for the underlying base class generator engine.
771 * @brief Reseeds the %discard_block_engine object with the default
772 * seed for the underlying base class generator engine.
775 seed(result_type __s
)
782 * @brief Reseeds the %discard_block_engine object with the given seed
784 * @param __q A seed generator function.
794 * @brief Gets a const reference to the underlying generator engine
797 const _RandomNumberEngine
&
802 * @brief Gets the minimum value in the generated random number range.
804 * @todo This should be constexpr.
808 { return _M_b
.min(); }
811 * @brief Gets the maximum value in the generated random number range.
813 * @todo This should be constexpr.
817 { return _M_b
.max(); }
820 * @brief Discard a sequence of random numbers.
822 * @todo Look for a faster way to do discard.
825 discard(unsigned long long __z
)
827 for (; __z
!= 0ULL; --__z
)
832 * @brief Gets the next value in the generated random number sequence.
838 * @brief Compares two %discard_block_engine random number generator
839 * objects of the same type for equality.
841 * @param __lhs A %discard_block_engine random number generator object.
842 * @param __rhs Another %discard_block_engine random number generator
845 * @returns true if the two objects are equal, false otherwise.
848 operator==(const discard_block_engine
& __lhs
,
849 const discard_block_engine
& __rhs
)
850 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
853 * @brief Inserts the current state of a %discard_block_engine random
854 * number generator engine @p __x into the output stream
857 * @param __os An output stream.
858 * @param __x A %discard_block_engine random number generator engine.
860 * @returns The output stream with the state of @p __x inserted or in
863 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
864 typename _CharT
, typename _Traits
>
865 friend std::basic_ostream
<_CharT
, _Traits
>&
866 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
867 const std::discard_block_engine
<_RandomNumberEngine1
,
871 * @brief Extracts the current state of a % subtract_with_carry_engine
872 * random number generator engine @p __x from the input stream
875 * @param __is An input stream.
876 * @param __x A %discard_block_engine random number generator engine.
878 * @returns The input stream with the state of @p __x extracted or in
881 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
882 typename _CharT
, typename _Traits
>
883 friend std::basic_istream
<_CharT
, _Traits
>&
884 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
885 std::discard_block_engine
<_RandomNumberEngine1
,
889 _RandomNumberEngine _M_b
;
894 * Produces random numbers by combining random numbers from some base
895 * engine to produce random numbers with a specifies number of bits @p __w.
897 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
898 class independent_bits_engine
900 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
901 "substituting _UIntType not an unsigned integral type");
902 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
903 "template argument substituting __w out of bounds");
906 /** The type of the generated random value. */
907 typedef _UIntType result_type
;
910 * @brief Constructs a default %independent_bits_engine engine.
912 * The underlying engine is default constructed as well.
914 independent_bits_engine()
918 * @brief Copy constructs a %independent_bits_engine engine.
920 * Copies an existing base class random number generator.
921 * @param rng An existing (base class) engine object.
924 independent_bits_engine(const _RandomNumberEngine
& __rne
)
928 * @brief Move constructs a %independent_bits_engine engine.
930 * Copies an existing base class random number generator.
931 * @param rng An existing (base class) engine object.
934 independent_bits_engine(_RandomNumberEngine
&& __rne
)
935 : _M_b(std::move(__rne
)) { }
938 * @brief Seed constructs a %independent_bits_engine engine.
940 * Constructs the underlying generator engine seeded with @p __s.
941 * @param __s A seed value for the base class engine.
944 independent_bits_engine(result_type __s
)
948 * @brief Generator construct a %independent_bits_engine engine.
950 * @param __q A seed sequence.
953 independent_bits_engine(seed_seq
& __q
)
958 * @brief Reseeds the %independent_bits_engine object with the default
959 * seed for the underlying base class generator engine.
966 * @brief Reseeds the %independent_bits_engine object with the default
967 * seed for the underlying base class generator engine.
970 seed(result_type __s
)
974 * @brief Reseeds the %independent_bits_engine object with the given
976 * @param __q A seed generator function.
983 * @brief Gets a const reference to the underlying generator engine
986 const _RandomNumberEngine
&
991 * @brief Gets the minimum value in the generated random number range.
993 * @todo This should be constexpr.
1000 * @brief Gets the maximum value in the generated random number range.
1002 * @todo This should be constexpr.
1006 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1009 * @brief Discard a sequence of random numbers.
1011 * @todo Look for a faster way to do discard.
1014 discard(unsigned long long __z
)
1016 for (; __z
!= 0ULL; --__z
)
1021 * @brief Gets the next value in the generated random number sequence.
1027 * @brief Compares two %independent_bits_engine random number generator
1028 * objects of the same type for equality.
1030 * @param __lhs A %independent_bits_engine random number generator
1032 * @param __rhs Another %independent_bits_engine random number generator
1035 * @returns true if the two objects are equal, false otherwise.
1038 operator==(const independent_bits_engine
& __lhs
,
1039 const independent_bits_engine
& __rhs
)
1040 { return __lhs
._M_b
== __rhs
._M_b
; }
1043 * @brief Extracts the current state of a % subtract_with_carry_engine
1044 * random number generator engine @p __x from the input stream
1047 * @param __is An input stream.
1048 * @param __x A %independent_bits_engine random number generator
1051 * @returns The input stream with the state of @p __x extracted or in
1054 template<typename _CharT
, typename _Traits
>
1055 friend std::basic_istream
<_CharT
, _Traits
>&
1056 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1057 std::independent_bits_engine
<_RandomNumberEngine
,
1058 __w
, _UIntType
>& __x
)
1065 _RandomNumberEngine _M_b
;
1069 * @brief Inserts the current state of a %independent_bits_engine random
1070 * number generator engine @p __x into the output stream @p __os.
1072 * @param __os An output stream.
1073 * @param __x A %independent_bits_engine random number generator engine.
1075 * @returns The output stream with the state of @p __x inserted or in
1078 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1079 typename _CharT
, typename _Traits
>
1080 std::basic_ostream
<_CharT
, _Traits
>&
1081 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1082 const std::independent_bits_engine
<_RandomNumberEngine
,
1083 __w
, _UIntType
>& __x
)
1090 * @brief Produces random numbers by combining random numbers from some
1091 * base engine to produce random numbers with a specifies number of bits
1094 template<typename _RandomNumberEngine
, size_t __k
>
1095 class shuffle_order_engine
1097 static_assert(1u <= __k
, "template argument substituing "
1098 "__k out of bound");
1101 /** The type of the generated random value. */
1102 typedef typename
_RandomNumberEngine::result_type result_type
;
1104 static const size_t table_size
= __k
;
1107 * @brief Constructs a default %shuffle_order_engine engine.
1109 * The underlying engine is default constructed as well.
1111 shuffle_order_engine()
1113 { _M_initialize(); }
1116 * @brief Copy constructs a %shuffle_order_engine engine.
1118 * Copies an existing base class random number generator.
1119 * @param rng An existing (base class) engine object.
1122 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1124 { _M_initialize(); }
1127 * @brief Move constructs a %shuffle_order_engine engine.
1129 * Copies an existing base class random number generator.
1130 * @param rng An existing (base class) engine object.
1133 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1134 : _M_b(std::move(__rne
))
1135 { _M_initialize(); }
1138 * @brief Seed constructs a %shuffle_order_engine engine.
1140 * Constructs the underlying generator engine seeded with @p __s.
1141 * @param __s A seed value for the base class engine.
1144 shuffle_order_engine(result_type __s
)
1146 { _M_initialize(); }
1149 * @brief Generator construct a %shuffle_order_engine engine.
1151 * @param __q A seed sequence.
1154 shuffle_order_engine(seed_seq
& __q
)
1156 { _M_initialize(); }
1159 * @brief Reseeds the %shuffle_order_engine object with the default seed
1160 for the underlying base class generator engine.
1170 * @brief Reseeds the %shuffle_order_engine object with the default seed
1171 * for the underlying base class generator engine.
1174 seed(result_type __s
)
1181 * @brief Reseeds the %shuffle_order_engine object with the given seed
1183 * @param __q A seed generator function.
1193 * Gets a const reference to the underlying generator engine object.
1195 const _RandomNumberEngine
&
1200 * Gets the minimum value in the generated random number range.
1202 * @todo This should be constexpr.
1206 { return _M_b
.min(); }
1209 * Gets the maximum value in the generated random number range.
1211 * @todo This should be constexpr.
1215 { return _M_b
.max(); }
1218 * Discard a sequence of random numbers.
1220 * @todo Look for a faster way to do discard.
1223 discard(unsigned long long __z
)
1225 for (; __z
!= 0ULL; --__z
)
1230 * Gets the next value in the generated random number sequence.
1236 * Compares two %shuffle_order_engine random number generator objects
1237 * of the same type for equality.
1239 * @param __lhs A %shuffle_order_engine random number generator object.
1240 * @param __rhs Another %shuffle_order_engine random number generator
1243 * @returns true if the two objects are equal, false otherwise.
1246 operator==(const shuffle_order_engine
& __lhs
,
1247 const shuffle_order_engine
& __rhs
)
1248 { return __lhs
._M_b
== __rhs
._M_b
; }
1251 * @brief Inserts the current state of a %shuffle_order_engine random
1252 * number generator engine @p __x into the output stream
1255 * @param __os An output stream.
1256 * @param __x A %shuffle_order_engine random number generator engine.
1258 * @returns The output stream with the state of @p __x inserted or in
1261 template<typename _RandomNumberEngine1
, size_t __k1
,
1262 typename _CharT
, typename _Traits
>
1263 friend std::basic_ostream
<_CharT
, _Traits
>&
1264 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1265 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1269 * @brief Extracts the current state of a % subtract_with_carry_engine
1270 * random number generator engine @p __x from the input stream
1273 * @param __is An input stream.
1274 * @param __x A %shuffle_order_engine random number generator engine.
1276 * @returns The input stream with the state of @p __x extracted or in
1279 template<typename _RandomNumberEngine1
, size_t __k1
,
1280 typename _CharT
, typename _Traits
>
1281 friend std::basic_istream
<_CharT
, _Traits
>&
1282 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1283 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1286 void _M_initialize()
1288 for (size_t __i
= 0; __i
< __k
; ++__i
)
1293 _RandomNumberEngine _M_b
;
1294 result_type _M_v
[__k
];
1299 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1301 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1305 * An alternative LCR (Lehmer Generator function) .
1307 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1311 * The classic Mersenne Twister.
1314 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1315 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1316 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1318 typedef mersenne_twister_engine
<
1324 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1327 * An alternative Mersenne Twister.
1329 typedef mersenne_twister_engine
<
1332 0xb5026f5aa96619e9ULL
, 29,
1333 0x5555555555555555ULL
, 17,
1334 0x71d67fffeda60000ULL
, 37,
1335 0xfff7eee000000000ULL
, 43,
1336 6364136223846793005ULL> mt19937_64
;
1341 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1344 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1347 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1349 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1354 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1359 typedef minstd_rand0 default_random_engine
;
1362 * A standard interface to a platform-specific non-deterministic
1363 * random number generator (if any are available).
1368 /** The type of the generated random value. */
1369 typedef unsigned int result_type
;
1371 // constructors, destructors and member functions
1373 #ifdef _GLIBCXX_USE_RANDOM_TR1
1376 random_device(const std::string
& __token
= "/dev/urandom")
1378 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1379 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1380 std::__throw_runtime_error(__N("random_device::"
1381 "random_device(const std::string&)"));
1385 { std::fclose(_M_file
); }
1390 random_device(const std::string
& __token
= "mt19937")
1391 : _M_mt(_M_strtoul(__token
)) { }
1394 static unsigned long
1395 _M_strtoul(const std::string
& __str
)
1397 unsigned long __ret
= 5489UL;
1398 if (__str
!= "mt19937")
1400 const char* __nptr
= __str
.c_str();
1402 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1403 if (*__nptr
== '\0' || *__endptr
!= '\0')
1404 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1405 "(const std::string&)"));
1416 { return std::numeric_limits
<result_type
>::min(); }
1420 { return std::numeric_limits
<result_type
>::max(); }
1429 #ifdef _GLIBCXX_USE_RANDOM_TR1
1431 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1439 // No copy functions.
1440 random_device(const random_device
&) = delete;
1441 void operator=(const random_device
&) = delete;
1445 #ifdef _GLIBCXX_USE_RANDOM_TR1
1452 /* @} */ // group std_random_generators
1455 * @addtogroup std_random_distributions Random Number Distributions
1456 * @ingroup std_random
1461 * @addtogroup std_random_distributions_uniform Uniform Distributions
1462 * @ingroup std_random_distributions
1467 * @brief Uniform discrete distribution for random numbers.
1468 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1469 * probability throughout the range.
1471 template<typename _IntType
= int>
1472 class uniform_int_distribution
1474 static_assert(std::is_integral
<_IntType
>::value
,
1475 "template argument not an integral type");
1478 /** The type of the range of the distribution. */
1479 typedef _IntType result_type
;
1480 /** Parameter type. */
1483 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1486 param_type(_IntType __a
= 0,
1487 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1488 : _M_a(__a
), _M_b(__b
)
1490 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1508 * @brief Constructs a uniform distribution object.
1511 uniform_int_distribution(_IntType __a
= 0,
1512 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1513 : _M_param(__a
, __b
)
1517 uniform_int_distribution(const param_type
& __p
)
1522 * @brief Resets the distribution state.
1524 * Does nothing for the uniform integer distribution.
1531 { return _M_param
.a(); }
1535 { return _M_param
.b(); }
1538 * @brief Returns the inclusive lower bound of the distribution range.
1542 { return this->a(); }
1545 * @brief Returns the inclusive upper bound of the distribution range.
1549 { return this->b(); }
1552 * @brief Returns the parameter set of the distribution.
1556 { return _M_param
; }
1559 * @brief Sets the parameter set of the distribution.
1560 * @param __param The new parameter set of the distribution.
1563 param(const param_type
& __param
)
1564 { _M_param
= __param
; }
1567 * Gets a uniformly distributed random number in the range
1570 template<typename _UniformRandomNumberGenerator
>
1572 operator()(_UniformRandomNumberGenerator
& __urng
)
1573 { return this->operator()(__urng
, this->param()); }
1576 * Gets a uniform random number in the range @f$[0, n)@f$.
1578 * This function is aimed at use with std::random_shuffle.
1580 template<typename _UniformRandomNumberGenerator
>
1582 operator()(_UniformRandomNumberGenerator
& __urng
,
1583 const param_type
& __p
);
1585 param_type _M_param
;
1589 * @brief Inserts a %uniform_int_distribution random number
1590 * distribution @p __x into the output stream @p os.
1592 * @param __os An output stream.
1593 * @param __x A %uniform_int_distribution random number distribution.
1595 * @returns The output stream with the state of @p __x inserted or in
1598 template<typename _IntType
, typename _CharT
, typename _Traits
>
1599 std::basic_ostream
<_CharT
, _Traits
>&
1600 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1601 const std::uniform_int_distribution
<_IntType
>&);
1604 * @brief Extracts a %uniform_int_distribution random number distribution
1605 * @p __x from the input stream @p __is.
1607 * @param __is An input stream.
1608 * @param __x A %uniform_int_distribution random number generator engine.
1610 * @returns The input stream with @p __x extracted or in an error state.
1612 template<typename _IntType
, typename _CharT
, typename _Traits
>
1613 std::basic_istream
<_CharT
, _Traits
>&
1614 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1615 std::uniform_int_distribution
<_IntType
>&);
1619 * @brief Uniform continuous distribution for random numbers.
1621 * A continuous random distribution on the range [min, max) with equal
1622 * probability throughout the range. The URNG should be real-valued and
1623 * deliver number in the range [0, 1).
1625 template<typename _RealType
= double>
1626 class uniform_real_distribution
1628 static_assert(std::is_floating_point
<_RealType
>::value
,
1629 "template argument not a floating point type");
1632 /** The type of the range of the distribution. */
1633 typedef _RealType result_type
;
1634 /** Parameter type. */
1637 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1640 param_type(_RealType __a
= _RealType(0),
1641 _RealType __b
= _RealType(1))
1642 : _M_a(__a
), _M_b(__b
)
1644 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1662 * @brief Constructs a uniform_real_distribution object.
1664 * @param __min [IN] The lower bound of the distribution.
1665 * @param __max [IN] The upper bound of the distribution.
1668 uniform_real_distribution(_RealType __a
= _RealType(0),
1669 _RealType __b
= _RealType(1))
1670 : _M_param(__a
, __b
)
1674 uniform_real_distribution(const param_type
& __p
)
1679 * @brief Resets the distribution state.
1681 * Does nothing for the uniform real distribution.
1688 { return _M_param
.a(); }
1692 { return _M_param
.b(); }
1695 * @brief Returns the inclusive lower bound of the distribution range.
1699 { return this->a(); }
1702 * @brief Returns the inclusive upper bound of the distribution range.
1706 { return this->b(); }
1709 * @brief Returns the parameter set of the distribution.
1713 { return _M_param
; }
1716 * @brief Sets the parameter set of the distribution.
1717 * @param __param The new parameter set of the distribution.
1720 param(const param_type
& __param
)
1721 { _M_param
= __param
; }
1723 template<typename _UniformRandomNumberGenerator
>
1725 operator()(_UniformRandomNumberGenerator
& __urng
)
1726 { return this->operator()(__urng
, this->param()); }
1728 template<typename _UniformRandomNumberGenerator
>
1730 operator()(_UniformRandomNumberGenerator
& __urng
,
1731 const param_type
& __p
)
1733 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1735 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1739 param_type _M_param
;
1743 * @brief Inserts a %uniform_real_distribution random number
1744 * distribution @p __x into the output stream @p __os.
1746 * @param __os An output stream.
1747 * @param __x A %uniform_real_distribution random number distribution.
1749 * @returns The output stream with the state of @p __x inserted or in
1752 template<typename _RealType
, typename _CharT
, typename _Traits
>
1753 std::basic_ostream
<_CharT
, _Traits
>&
1754 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1755 const std::uniform_real_distribution
<_RealType
>&);
1758 * @brief Extracts a %uniform_real_distribution random number distribution
1759 * @p __x from the input stream @p __is.
1761 * @param __is An input stream.
1762 * @param __x A %uniform_real_distribution random number generator engine.
1764 * @returns The input stream with @p __x extracted or in an error state.
1766 template<typename _RealType
, typename _CharT
, typename _Traits
>
1767 std::basic_istream
<_CharT
, _Traits
>&
1768 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1769 std::uniform_real_distribution
<_RealType
>&);
1771 /* @} */ // group std_random_distributions_uniform
1774 * @addtogroup std_random_distributions_normal Normal Distributions
1775 * @ingroup std_random_distributions
1780 * @brief A normal continuous distribution for random numbers.
1782 * The formula for the normal probability density function is
1783 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1784 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1786 template<typename _RealType
= double>
1787 class normal_distribution
1789 static_assert(std::is_floating_point
<_RealType
>::value
,
1790 "template argument not a floating point type");
1793 /** The type of the range of the distribution. */
1794 typedef _RealType result_type
;
1795 /** Parameter type. */
1798 typedef normal_distribution
<_RealType
> distribution_type
;
1801 param_type(_RealType __mean
= _RealType(0),
1802 _RealType __stddev
= _RealType(1))
1803 : _M_mean(__mean
), _M_stddev(__stddev
)
1805 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1814 { return _M_stddev
; }
1818 _RealType _M_stddev
;
1823 * Constructs a normal distribution with parameters @f$ mean @f$ and
1824 * standard deviation.
1827 normal_distribution(result_type __mean
= result_type(0),
1828 result_type __stddev
= result_type(1))
1829 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1833 normal_distribution(const param_type
& __p
)
1834 : _M_param(__p
), _M_saved_available(false)
1838 * @brief Resets the distribution state.
1842 { _M_saved_available
= false; }
1845 * @brief Returns the mean of the distribution.
1849 { return _M_param
.mean(); }
1852 * @brief Returns the standard deviation of the distribution.
1856 { return _M_param
.stddev(); }
1859 * @brief Returns the parameter set of the distribution.
1863 { return _M_param
; }
1866 * @brief Sets the parameter set of the distribution.
1867 * @param __param The new parameter set of the distribution.
1870 param(const param_type
& __param
)
1871 { _M_param
= __param
; }
1874 * @brief Returns the greatest lower bound value of the distribution.
1878 { return std::numeric_limits
<result_type
>::min(); }
1881 * @brief Returns the least upper bound value of the distribution.
1885 { return std::numeric_limits
<result_type
>::max(); }
1887 template<typename _UniformRandomNumberGenerator
>
1889 operator()(_UniformRandomNumberGenerator
& __urng
)
1890 { return this->operator()(__urng
, this->param()); }
1892 template<typename _UniformRandomNumberGenerator
>
1894 operator()(_UniformRandomNumberGenerator
& __urng
,
1895 const param_type
& __p
);
1898 * @brief Inserts a %normal_distribution random number distribution
1899 * @p __x into the output stream @p __os.
1901 * @param __os An output stream.
1902 * @param __x A %normal_distribution random number distribution.
1904 * @returns The output stream with the state of @p __x inserted or in
1907 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1908 friend std::basic_ostream
<_CharT
, _Traits
>&
1909 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1910 const std::normal_distribution
<_RealType1
>&);
1913 * @brief Extracts a %normal_distribution random number distribution
1914 * @p __x from the input stream @p __is.
1916 * @param __is An input stream.
1917 * @param __x A %normal_distribution random number generator engine.
1919 * @returns The input stream with @p __x extracted or in an error
1922 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1923 friend std::basic_istream
<_CharT
, _Traits
>&
1924 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1925 std::normal_distribution
<_RealType1
>&);
1928 param_type _M_param
;
1929 result_type _M_saved
;
1930 bool _M_saved_available
;
1935 * @brief A lognormal_distribution random number distribution.
1937 * The formula for the normal probability mass function is
1938 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
1939 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
1941 template<typename _RealType
= double>
1942 class lognormal_distribution
1944 static_assert(std::is_floating_point
<_RealType
>::value
,
1945 "template argument not a floating point type");
1948 /** The type of the range of the distribution. */
1949 typedef _RealType result_type
;
1950 /** Parameter type. */
1953 typedef lognormal_distribution
<_RealType
> distribution_type
;
1956 param_type(_RealType __m
= _RealType(0),
1957 _RealType __s
= _RealType(1))
1958 : _M_m(__m
), _M_s(__s
)
1975 lognormal_distribution(_RealType __m
= _RealType(0),
1976 _RealType __s
= _RealType(1))
1977 : _M_param(__m
, __s
), _M_nd()
1981 lognormal_distribution(const param_type
& __p
)
1982 : _M_param(__p
), _M_nd()
1986 * Resets the distribution state.
1997 { return _M_param
.m(); }
2001 { return _M_param
.s(); }
2004 * @brief Returns the parameter set of the distribution.
2008 { return _M_param
; }
2011 * @brief Sets the parameter set of the distribution.
2012 * @param __param The new parameter set of the distribution.
2015 param(const param_type
& __param
)
2016 { _M_param
= __param
; }
2019 * @brief Returns the greatest lower bound value of the distribution.
2023 { return result_type(0); }
2026 * @brief Returns the least upper bound value of the distribution.
2030 { return std::numeric_limits
<result_type
>::max(); }
2032 template<typename _UniformRandomNumberGenerator
>
2034 operator()(_UniformRandomNumberGenerator
& __urng
)
2035 { return this->operator()(__urng
, this->param()); }
2037 template<typename _UniformRandomNumberGenerator
>
2039 operator()(_UniformRandomNumberGenerator
& __urng
,
2040 const param_type
& __p
)
2041 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2044 * @brief Inserts a %lognormal_distribution random number distribution
2045 * @p __x into the output stream @p __os.
2047 * @param __os An output stream.
2048 * @param __x A %lognormal_distribution random number distribution.
2050 * @returns The output stream with the state of @p __x inserted or in
2053 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2054 friend std::basic_ostream
<_CharT
, _Traits
>&
2055 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2056 const std::lognormal_distribution
<_RealType1
>&);
2059 * @brief Extracts a %lognormal_distribution random number distribution
2060 * @p __x from the input stream @p __is.
2062 * @param __is An input stream.
2063 * @param __x A %lognormal_distribution random number
2066 * @returns The input stream with @p __x extracted or in an error state.
2068 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2069 friend std::basic_istream
<_CharT
, _Traits
>&
2070 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2071 std::lognormal_distribution
<_RealType1
>&);
2074 param_type _M_param
;
2076 std::normal_distribution
<result_type
> _M_nd
;
2081 * @brief A gamma continuous distribution for random numbers.
2083 * The formula for the gamma probability density function is
2084 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2085 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
2087 template<typename _RealType
= double>
2088 class gamma_distribution
2090 static_assert(std::is_floating_point
<_RealType
>::value
,
2091 "template argument not a floating point type");
2094 /** The type of the range of the distribution. */
2095 typedef _RealType result_type
;
2096 /** Parameter type. */
2099 typedef gamma_distribution
<_RealType
> distribution_type
;
2100 friend class gamma_distribution
<_RealType
>;
2103 param_type(_RealType __alpha_val
= _RealType(1),
2104 _RealType __beta_val
= _RealType(1))
2105 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2107 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2113 { return _M_alpha
; }
2126 _RealType _M_malpha
, _M_a2
;
2131 * @brief Constructs a gamma distribution with parameters
2132 * @f$ \alpha @f$ and @f$ \beta @f$.
2135 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2136 _RealType __beta_val
= _RealType(1))
2137 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2141 gamma_distribution(const param_type
& __p
)
2142 : _M_param(__p
), _M_nd()
2146 * @brief Resets the distribution state.
2153 * @brief Returns the @f$ \alpha @f$ of the distribution.
2157 { return _M_param
.alpha(); }
2160 * @brief Returns the @f$ \beta @f$ of the distribution.
2164 { return _M_param
.beta(); }
2167 * @brief Returns the parameter set of the distribution.
2171 { return _M_param
; }
2174 * @brief Sets the parameter set of the distribution.
2175 * @param __param The new parameter set of the distribution.
2178 param(const param_type
& __param
)
2179 { _M_param
= __param
; }
2182 * @brief Returns the greatest lower bound value of the distribution.
2186 { return result_type(0); }
2189 * @brief Returns the least upper bound value of the distribution.
2193 { return std::numeric_limits
<result_type
>::max(); }
2195 template<typename _UniformRandomNumberGenerator
>
2197 operator()(_UniformRandomNumberGenerator
& __urng
)
2198 { return this->operator()(__urng
, this->param()); }
2200 template<typename _UniformRandomNumberGenerator
>
2202 operator()(_UniformRandomNumberGenerator
& __urng
,
2203 const param_type
& __p
);
2206 * @brief Inserts a %gamma_distribution random number distribution
2207 * @p __x into the output stream @p __os.
2209 * @param __os An output stream.
2210 * @param __x A %gamma_distribution random number distribution.
2212 * @returns The output stream with the state of @p __x inserted or in
2215 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2216 friend std::basic_ostream
<_CharT
, _Traits
>&
2217 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2218 const std::gamma_distribution
<_RealType1
>&);
2221 * @brief Extracts a %gamma_distribution random number distribution
2222 * @p __x from the input stream @p __is.
2224 * @param __is An input stream.
2225 * @param __x A %gamma_distribution random number generator engine.
2227 * @returns The input stream with @p __x extracted or in an error state.
2229 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2230 friend std::basic_istream
<_CharT
, _Traits
>&
2231 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2232 std::gamma_distribution
<_RealType1
>&);
2235 param_type _M_param
;
2237 std::normal_distribution
<result_type
> _M_nd
;
2242 * @brief A chi_squared_distribution random number distribution.
2244 * The formula for the normal probability mass function is
2245 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2247 template<typename _RealType
= double>
2248 class chi_squared_distribution
2250 static_assert(std::is_floating_point
<_RealType
>::value
,
2251 "template argument not a floating point type");
2254 /** The type of the range of the distribution. */
2255 typedef _RealType result_type
;
2256 /** Parameter type. */
2259 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2262 param_type(_RealType __n
= _RealType(1))
2275 chi_squared_distribution(_RealType __n
= _RealType(1))
2276 : _M_param(__n
), _M_gd(__n
/ 2)
2280 chi_squared_distribution(const param_type
& __p
)
2281 : _M_param(__p
), _M_gd(__p
.n() / 2)
2285 * @brief Resets the distribution state.
2296 { return _M_param
.n(); }
2299 * @brief Returns the parameter set of the distribution.
2303 { return _M_param
; }
2306 * @brief Sets the parameter set of the distribution.
2307 * @param __param The new parameter set of the distribution.
2310 param(const param_type
& __param
)
2311 { _M_param
= __param
; }
2314 * @brief Returns the greatest lower bound value of the distribution.
2318 { return result_type(0); }
2321 * @brief Returns the least upper bound value of the distribution.
2325 { return std::numeric_limits
<result_type
>::max(); }
2327 template<typename _UniformRandomNumberGenerator
>
2329 operator()(_UniformRandomNumberGenerator
& __urng
)
2330 { return 2 * _M_gd(__urng
); }
2332 template<typename _UniformRandomNumberGenerator
>
2334 operator()(_UniformRandomNumberGenerator
& __urng
,
2335 const param_type
& __p
)
2337 typedef typename
std::gamma_distribution
<result_type
>::param_type
2339 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2343 * @brief Inserts a %chi_squared_distribution random number distribution
2344 * @p __x into the output stream @p __os.
2346 * @param __os An output stream.
2347 * @param __x A %chi_squared_distribution random number distribution.
2349 * @returns The output stream with the state of @p __x inserted or in
2352 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2353 friend std::basic_ostream
<_CharT
, _Traits
>&
2354 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2355 const std::chi_squared_distribution
<_RealType1
>&);
2358 * @brief Extracts a %chi_squared_distribution random number distribution
2359 * @p __x from the input stream @p __is.
2361 * @param __is An input stream.
2362 * @param __x A %chi_squared_distribution random number
2365 * @returns The input stream with @p __x extracted or in an error state.
2367 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2368 friend std::basic_istream
<_CharT
, _Traits
>&
2369 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2370 std::chi_squared_distribution
<_RealType1
>&);
2373 param_type _M_param
;
2375 std::gamma_distribution
<result_type
> _M_gd
;
2380 * @brief A cauchy_distribution random number distribution.
2382 * The formula for the normal probability mass function is
2383 * @f$ p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1} @f$
2385 template<typename _RealType
= double>
2386 class cauchy_distribution
2388 static_assert(std::is_floating_point
<_RealType
>::value
,
2389 "template argument not a floating point type");
2392 /** The type of the range of the distribution. */
2393 typedef _RealType result_type
;
2394 /** Parameter type. */
2397 typedef cauchy_distribution
<_RealType
> distribution_type
;
2400 param_type(_RealType __a
= _RealType(0),
2401 _RealType __b
= _RealType(1))
2402 : _M_a(__a
), _M_b(__b
)
2419 cauchy_distribution(_RealType __a
= _RealType(0),
2420 _RealType __b
= _RealType(1))
2421 : _M_param(__a
, __b
)
2425 cauchy_distribution(const param_type
& __p
)
2430 * @brief Resets the distribution state.
2441 { return _M_param
.a(); }
2445 { return _M_param
.b(); }
2448 * @brief Returns the parameter set of the distribution.
2452 { return _M_param
; }
2455 * @brief Sets the parameter set of the distribution.
2456 * @param __param The new parameter set of the distribution.
2459 param(const param_type
& __param
)
2460 { _M_param
= __param
; }
2463 * @brief Returns the greatest lower bound value of the distribution.
2467 { return std::numeric_limits
<result_type
>::min(); }
2470 * @brief Returns the least upper bound value of the distribution.
2474 { return std::numeric_limits
<result_type
>::max(); }
2476 template<typename _UniformRandomNumberGenerator
>
2478 operator()(_UniformRandomNumberGenerator
& __urng
)
2479 { return this->operator()(__urng
, this->param()); }
2481 template<typename _UniformRandomNumberGenerator
>
2483 operator()(_UniformRandomNumberGenerator
& __urng
,
2484 const param_type
& __p
);
2487 param_type _M_param
;
2491 * @brief Inserts a %cauchy_distribution random number distribution
2492 * @p __x into the output stream @p __os.
2494 * @param __os An output stream.
2495 * @param __x A %cauchy_distribution random number distribution.
2497 * @returns The output stream with the state of @p __x inserted or in
2500 template<typename _RealType
, typename _CharT
, typename _Traits
>
2501 std::basic_ostream
<_CharT
, _Traits
>&
2502 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2503 const std::cauchy_distribution
<_RealType
>&);
2506 * @brief Extracts a %cauchy_distribution random number distribution
2507 * @p __x from the input stream @p __is.
2509 * @param __is An input stream.
2510 * @param __x A %cauchy_distribution random number
2513 * @returns The input stream with @p __x extracted or in an error state.
2515 template<typename _RealType
, typename _CharT
, typename _Traits
>
2516 std::basic_istream
<_CharT
, _Traits
>&
2517 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2518 std::cauchy_distribution
<_RealType
>&);
2522 * @brief A fisher_f_distribution random number distribution.
2524 * The formula for the normal probability mass function is
2525 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2526 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2527 * (1 + \frac{mx}{n})^{-(m+n)/2} @f$
2529 template<typename _RealType
= double>
2530 class fisher_f_distribution
2532 static_assert(std::is_floating_point
<_RealType
>::value
,
2533 "template argument not a floating point type");
2536 /** The type of the range of the distribution. */
2537 typedef _RealType result_type
;
2538 /** Parameter type. */
2541 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2544 param_type(_RealType __m
= _RealType(1),
2545 _RealType __n
= _RealType(1))
2546 : _M_m(__m
), _M_n(__n
)
2563 fisher_f_distribution(_RealType __m
= _RealType(1),
2564 _RealType __n
= _RealType(1))
2565 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2569 fisher_f_distribution(const param_type
& __p
)
2570 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2574 * @brief Resets the distribution state.
2588 { return _M_param
.m(); }
2592 { return _M_param
.n(); }
2595 * @brief Returns the parameter set of the distribution.
2599 { return _M_param
; }
2602 * @brief Sets the parameter set of the distribution.
2603 * @param __param The new parameter set of the distribution.
2606 param(const param_type
& __param
)
2607 { _M_param
= __param
; }
2610 * @brief Returns the greatest lower bound value of the distribution.
2614 { return result_type(0); }
2617 * @brief Returns the least upper bound value of the distribution.
2621 { return std::numeric_limits
<result_type
>::max(); }
2623 template<typename _UniformRandomNumberGenerator
>
2625 operator()(_UniformRandomNumberGenerator
& __urng
)
2626 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2628 template<typename _UniformRandomNumberGenerator
>
2630 operator()(_UniformRandomNumberGenerator
& __urng
,
2631 const param_type
& __p
)
2633 typedef typename
std::gamma_distribution
<result_type
>::param_type
2635 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2636 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2640 * @brief Inserts a %fisher_f_distribution random number distribution
2641 * @p __x into the output stream @p __os.
2643 * @param __os An output stream.
2644 * @param __x A %fisher_f_distribution random number distribution.
2646 * @returns The output stream with the state of @p __x inserted or in
2649 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2650 friend std::basic_ostream
<_CharT
, _Traits
>&
2651 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2652 const std::fisher_f_distribution
<_RealType1
>&);
2655 * @brief Extracts a %fisher_f_distribution random number distribution
2656 * @p __x from the input stream @p __is.
2658 * @param __is An input stream.
2659 * @param __x A %fisher_f_distribution random number
2662 * @returns The input stream with @p __x extracted or in an error state.
2664 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2665 friend std::basic_istream
<_CharT
, _Traits
>&
2666 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2667 std::fisher_f_distribution
<_RealType1
>&);
2670 param_type _M_param
;
2672 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
2677 * @brief A student_t_distribution random number distribution.
2679 * The formula for the normal probability mass function is
2680 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2681 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} @f$
2683 template<typename _RealType
= double>
2684 class student_t_distribution
2686 static_assert(std::is_floating_point
<_RealType
>::value
,
2687 "template argument not a floating point type");
2690 /** The type of the range of the distribution. */
2691 typedef _RealType result_type
;
2692 /** Parameter type. */
2695 typedef student_t_distribution
<_RealType
> distribution_type
;
2698 param_type(_RealType __n
= _RealType(1))
2711 student_t_distribution(_RealType __n
= _RealType(1))
2712 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
2716 student_t_distribution(const param_type
& __p
)
2717 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
2721 * @brief Resets the distribution state.
2735 { return _M_param
.n(); }
2738 * @brief Returns the parameter set of the distribution.
2742 { return _M_param
; }
2745 * @brief Sets the parameter set of the distribution.
2746 * @param __param The new parameter set of the distribution.
2749 param(const param_type
& __param
)
2750 { _M_param
= __param
; }
2753 * @brief Returns the greatest lower bound value of the distribution.
2757 { return std::numeric_limits
<result_type
>::min(); }
2760 * @brief Returns the least upper bound value of the distribution.
2764 { return std::numeric_limits
<result_type
>::max(); }
2766 template<typename _UniformRandomNumberGenerator
>
2768 operator()(_UniformRandomNumberGenerator
& __urng
)
2769 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
2771 template<typename _UniformRandomNumberGenerator
>
2773 operator()(_UniformRandomNumberGenerator
& __urng
,
2774 const param_type
& __p
)
2776 typedef typename
std::gamma_distribution
<result_type
>::param_type
2779 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
2780 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
2784 * @brief Inserts a %student_t_distribution random number distribution
2785 * @p __x into the output stream @p __os.
2787 * @param __os An output stream.
2788 * @param __x A %student_t_distribution random number distribution.
2790 * @returns The output stream with the state of @p __x inserted or in
2793 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2794 friend std::basic_ostream
<_CharT
, _Traits
>&
2795 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2796 const std::student_t_distribution
<_RealType1
>&);
2799 * @brief Extracts a %student_t_distribution random number distribution
2800 * @p __x from the input stream @p __is.
2802 * @param __is An input stream.
2803 * @param __x A %student_t_distribution random number
2806 * @returns The input stream with @p __x extracted or in an error state.
2808 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2809 friend std::basic_istream
<_CharT
, _Traits
>&
2810 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2811 std::student_t_distribution
<_RealType1
>&);
2814 param_type _M_param
;
2816 std::normal_distribution
<result_type
> _M_nd
;
2817 std::gamma_distribution
<result_type
> _M_gd
;
2820 /* @} */ // group std_random_distributions_normal
2823 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2824 * @ingroup std_random_distributions
2829 * @brief A Bernoulli random number distribution.
2831 * Generates a sequence of true and false values with likelihood @f$ p @f$
2832 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2834 class bernoulli_distribution
2837 /** The type of the range of the distribution. */
2838 typedef bool result_type
;
2839 /** Parameter type. */
2842 typedef bernoulli_distribution distribution_type
;
2845 param_type(double __p
= 0.5)
2848 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
2861 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2863 * @param __p [IN] The likelihood of a true result being returned.
2864 * Must be in the interval @f$ [0, 1] @f$.
2867 bernoulli_distribution(double __p
= 0.5)
2872 bernoulli_distribution(const param_type
& __p
)
2877 * @brief Resets the distribution state.
2879 * Does nothing for a Bernoulli distribution.
2885 * @brief Returns the @p p parameter of the distribution.
2889 { return _M_param
.p(); }
2892 * @brief Returns the parameter set of the distribution.
2896 { return _M_param
; }
2899 * @brief Sets the parameter set of the distribution.
2900 * @param __param The new parameter set of the distribution.
2903 param(const param_type
& __param
)
2904 { _M_param
= __param
; }
2907 * @brief Returns the greatest lower bound value of the distribution.
2911 { return std::numeric_limits
<result_type
>::min(); }
2914 * @brief Returns the least upper bound value of the distribution.
2918 { return std::numeric_limits
<result_type
>::max(); }
2921 * @brief Returns the next value in the Bernoullian sequence.
2923 template<typename _UniformRandomNumberGenerator
>
2925 operator()(_UniformRandomNumberGenerator
& __urng
)
2926 { return this->operator()(__urng
, this->param()); }
2928 template<typename _UniformRandomNumberGenerator
>
2930 operator()(_UniformRandomNumberGenerator
& __urng
,
2931 const param_type
& __p
)
2933 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2935 if ((__aurng() - __aurng
.min())
2936 < __p
.p() * (__aurng
.max() - __aurng
.min()))
2942 param_type _M_param
;
2946 * @brief Inserts a %bernoulli_distribution random number distribution
2947 * @p __x into the output stream @p __os.
2949 * @param __os An output stream.
2950 * @param __x A %bernoulli_distribution random number distribution.
2952 * @returns The output stream with the state of @p __x inserted or in
2955 template<typename _CharT
, typename _Traits
>
2956 std::basic_ostream
<_CharT
, _Traits
>&
2957 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2958 const std::bernoulli_distribution
&);
2961 * @brief Extracts a %bernoulli_distribution random number distribution
2962 * @p __x from the input stream @p __is.
2964 * @param __is An input stream.
2965 * @param __x A %bernoulli_distribution random number generator engine.
2967 * @returns The input stream with @p __x extracted or in an error state.
2969 template<typename _CharT
, typename _Traits
>
2970 std::basic_istream
<_CharT
, _Traits
>&
2971 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2972 std::bernoulli_distribution
& __x
)
2976 __x
.param(bernoulli_distribution::param_type(__p
));
2982 * @brief A discrete binomial random number distribution.
2984 * The formula for the binomial probability density function is
2985 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
2986 * and @f$ p @f$ are the parameters of the distribution.
2988 template<typename _IntType
= int>
2989 class binomial_distribution
2991 static_assert(std::is_integral
<_IntType
>::value
,
2992 "template argument not an integral type");
2995 /** The type of the range of the distribution. */
2996 typedef _IntType result_type
;
2997 /** Parameter type. */
3000 typedef binomial_distribution
<_IntType
> distribution_type
;
3001 friend class binomial_distribution
<_IntType
>;
3004 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3005 : _M_t(__t
), _M_p(__p
)
3007 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3029 #if _GLIBCXX_USE_C99_MATH_TR1
3030 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3031 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3036 // constructors and member function
3038 binomial_distribution(_IntType __t
= _IntType(1),
3040 : _M_param(__t
, __p
), _M_nd()
3044 binomial_distribution(const param_type
& __p
)
3045 : _M_param(__p
), _M_nd()
3049 * @brief Resets the distribution state.
3056 * @brief Returns the distribution @p t parameter.
3060 { return _M_param
.t(); }
3063 * @brief Returns the distribution @p p parameter.
3067 { return _M_param
.p(); }
3070 * @brief Returns the parameter set of the distribution.
3074 { return _M_param
; }
3077 * @brief Sets the parameter set of the distribution.
3078 * @param __param The new parameter set of the distribution.
3081 param(const param_type
& __param
)
3082 { _M_param
= __param
; }
3085 * @brief Returns the greatest lower bound value of the distribution.
3092 * @brief Returns the least upper bound value of the distribution.
3096 { return _M_param
.t(); }
3098 template<typename _UniformRandomNumberGenerator
>
3100 operator()(_UniformRandomNumberGenerator
& __urng
)
3101 { return this->operator()(__urng
, this->param()); }
3103 template<typename _UniformRandomNumberGenerator
>
3105 operator()(_UniformRandomNumberGenerator
& __urng
,
3106 const param_type
& __p
);
3109 * @brief Inserts a %binomial_distribution random number distribution
3110 * @p __x into the output stream @p __os.
3112 * @param __os An output stream.
3113 * @param __x A %binomial_distribution random number distribution.
3115 * @returns The output stream with the state of @p __x inserted or in
3118 template<typename _IntType1
,
3119 typename _CharT
, typename _Traits
>
3120 friend std::basic_ostream
<_CharT
, _Traits
>&
3121 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3122 const std::binomial_distribution
<_IntType1
>&);
3125 * @brief Extracts a %binomial_distribution random number distribution
3126 * @p __x from the input stream @p __is.
3128 * @param __is An input stream.
3129 * @param __x A %binomial_distribution random number generator engine.
3131 * @returns The input stream with @p __x extracted or in an error
3134 template<typename _IntType1
,
3135 typename _CharT
, typename _Traits
>
3136 friend std::basic_istream
<_CharT
, _Traits
>&
3137 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3138 std::binomial_distribution
<_IntType1
>&);
3141 template<typename _UniformRandomNumberGenerator
>
3143 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3145 param_type _M_param
;
3147 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3148 std::normal_distribution
<double> _M_nd
;
3153 * @brief A discrete geometric random number distribution.
3155 * The formula for the geometric probability density function is
3156 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3159 template<typename _IntType
= int>
3160 class geometric_distribution
3162 static_assert(std::is_integral
<_IntType
>::value
,
3163 "template argument not an integral type");
3166 /** The type of the range of the distribution. */
3167 typedef _IntType result_type
;
3168 /** Parameter type. */
3171 typedef geometric_distribution
<_IntType
> distribution_type
;
3172 friend class geometric_distribution
<_IntType
>;
3175 param_type(double __p
= 0.5)
3178 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3190 { _M_log_p
= std::log(_M_p
); }
3197 // constructors and member function
3199 geometric_distribution(double __p
= 0.5)
3204 geometric_distribution(const param_type
& __p
)
3209 * @brief Resets the distribution state.
3211 * Does nothing for the geometric distribution.
3217 * @brief Returns the distribution parameter @p p.
3221 { return _M_param
.p(); }
3224 * @brief Returns the parameter set of the distribution.
3228 { return _M_param
; }
3231 * @brief Sets the parameter set of the distribution.
3232 * @param __param The new parameter set of the distribution.
3235 param(const param_type
& __param
)
3236 { _M_param
= __param
; }
3239 * @brief Returns the greatest lower bound value of the distribution.
3246 * @brief Returns the least upper bound value of the distribution.
3250 { return std::numeric_limits
<result_type
>::max(); }
3252 template<typename _UniformRandomNumberGenerator
>
3254 operator()(_UniformRandomNumberGenerator
& __urng
)
3255 { return this->operator()(__urng
, this->param()); }
3257 template<typename _UniformRandomNumberGenerator
>
3259 operator()(_UniformRandomNumberGenerator
& __urng
,
3260 const param_type
& __p
);
3263 param_type _M_param
;
3267 * @brief Inserts a %geometric_distribution random number distribution
3268 * @p __x into the output stream @p __os.
3270 * @param __os An output stream.
3271 * @param __x A %geometric_distribution random number distribution.
3273 * @returns The output stream with the state of @p __x inserted or in
3276 template<typename _IntType
,
3277 typename _CharT
, typename _Traits
>
3278 std::basic_ostream
<_CharT
, _Traits
>&
3279 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3280 const std::geometric_distribution
<_IntType
>&);
3283 * @brief Extracts a %geometric_distribution random number distribution
3284 * @p __x from the input stream @p __is.
3286 * @param __is An input stream.
3287 * @param __x A %geometric_distribution random number generator engine.
3289 * @returns The input stream with @p __x extracted or in an error state.
3291 template<typename _IntType
,
3292 typename _CharT
, typename _Traits
>
3293 std::basic_istream
<_CharT
, _Traits
>&
3294 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3295 std::geometric_distribution
<_IntType
>&);
3299 * @brief A negative_binomial_distribution random number distribution.
3301 * The formula for the negative binomial probability mass function is
3302 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3303 * and @f$ p @f$ are the parameters of the distribution.
3305 template<typename _IntType
= int>
3306 class negative_binomial_distribution
3308 static_assert(std::is_integral
<_IntType
>::value
,
3309 "template argument not an integral type");
3312 /** The type of the range of the distribution. */
3313 typedef _IntType result_type
;
3314 /** Parameter type. */
3317 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3320 param_type(_IntType __k
= 1, double __p
= 0.5)
3321 : _M_k(__k
), _M_p(__p
)
3338 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3339 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3343 negative_binomial_distribution(const param_type
& __p
)
3344 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3348 * @brief Resets the distribution state.
3355 * @brief Return the @f$ k @f$ parameter of the distribution.
3359 { return _M_param
.k(); }
3362 * @brief Return the @f$ p @f$ parameter of the distribution.
3366 { return _M_param
.p(); }
3369 * @brief Returns the parameter set of the distribution.
3373 { return _M_param
; }
3376 * @brief Sets the parameter set of the distribution.
3377 * @param __param The new parameter set of the distribution.
3380 param(const param_type
& __param
)
3381 { _M_param
= __param
; }
3384 * @brief Returns the greatest lower bound value of the distribution.
3388 { return result_type(0); }
3391 * @brief Returns the least upper bound value of the distribution.
3395 { return std::numeric_limits
<result_type
>::max(); }
3397 template<typename _UniformRandomNumberGenerator
>
3399 operator()(_UniformRandomNumberGenerator
& __urng
);
3401 template<typename _UniformRandomNumberGenerator
>
3403 operator()(_UniformRandomNumberGenerator
& __urng
,
3404 const param_type
& __p
);
3407 * @brief Inserts a %negative_binomial_distribution random
3408 * number distribution @p __x into the output stream @p __os.
3410 * @param __os An output stream.
3411 * @param __x A %negative_binomial_distribution random number
3414 * @returns The output stream with the state of @p __x inserted or in
3417 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3418 friend std::basic_ostream
<_CharT
, _Traits
>&
3419 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3420 const std::negative_binomial_distribution
<_IntType1
>&);
3423 * @brief Extracts a %negative_binomial_distribution random number
3424 * distribution @p __x from the input stream @p __is.
3426 * @param __is An input stream.
3427 * @param __x A %negative_binomial_distribution random number
3430 * @returns The input stream with @p __x extracted or in an error state.
3432 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3433 friend std::basic_istream
<_CharT
, _Traits
>&
3434 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3435 std::negative_binomial_distribution
<_IntType1
>&);
3438 param_type _M_param
;
3440 std::gamma_distribution
<double> _M_gd
;
3443 /* @} */ // group std_random_distributions_bernoulli
3446 * @addtogroup std_random_distributions_poisson Poisson Distributions
3447 * @ingroup std_random_distributions
3452 * @brief A discrete Poisson random number distribution.
3454 * The formula for the Poisson probability density function is
3455 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3456 * parameter of the distribution.
3458 template<typename _IntType
= int>
3459 class poisson_distribution
3461 static_assert(std::is_integral
<_IntType
>::value
,
3462 "template argument not an integral type");
3465 /** The type of the range of the distribution. */
3466 typedef _IntType result_type
;
3467 /** Parameter type. */
3470 typedef poisson_distribution
<_IntType
> distribution_type
;
3471 friend class poisson_distribution
<_IntType
>;
3474 param_type(double __mean
= 1.0)
3477 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3486 // Hosts either log(mean) or the threshold of the simple method.
3493 #if _GLIBCXX_USE_C99_MATH_TR1
3494 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3498 // constructors and member function
3500 poisson_distribution(double __mean
= 1.0)
3501 : _M_param(__mean
), _M_nd()
3505 poisson_distribution(const param_type
& __p
)
3506 : _M_param(__p
), _M_nd()
3510 * @brief Resets the distribution state.
3517 * @brief Returns the distribution parameter @p mean.
3521 { return _M_param
.mean(); }
3524 * @brief Returns the parameter set of the distribution.
3528 { return _M_param
; }
3531 * @brief Sets the parameter set of the distribution.
3532 * @param __param The new parameter set of the distribution.
3535 param(const param_type
& __param
)
3536 { _M_param
= __param
; }
3539 * @brief Returns the greatest lower bound value of the distribution.
3546 * @brief Returns the least upper bound value of the distribution.
3550 { return std::numeric_limits
<result_type
>::max(); }
3552 template<typename _UniformRandomNumberGenerator
>
3554 operator()(_UniformRandomNumberGenerator
& __urng
)
3555 { return this->operator()(__urng
, this->param()); }
3557 template<typename _UniformRandomNumberGenerator
>
3559 operator()(_UniformRandomNumberGenerator
& __urng
,
3560 const param_type
& __p
);
3563 * @brief Inserts a %poisson_distribution random number distribution
3564 * @p __x into the output stream @p __os.
3566 * @param __os An output stream.
3567 * @param __x A %poisson_distribution random number distribution.
3569 * @returns The output stream with the state of @p __x inserted or in
3572 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3573 friend std::basic_ostream
<_CharT
, _Traits
>&
3574 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3575 const std::poisson_distribution
<_IntType1
>&);
3578 * @brief Extracts a %poisson_distribution random number distribution
3579 * @p __x from the input stream @p __is.
3581 * @param __is An input stream.
3582 * @param __x A %poisson_distribution random number generator engine.
3584 * @returns The input stream with @p __x extracted or in an error
3587 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3588 friend std::basic_istream
<_CharT
, _Traits
>&
3589 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3590 std::poisson_distribution
<_IntType1
>&);
3593 param_type _M_param
;
3595 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3596 std::normal_distribution
<double> _M_nd
;
3600 * @brief An exponential continuous distribution for random numbers.
3602 * The formula for the exponential probability density function is
3603 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3605 * <table border=1 cellpadding=10 cellspacing=0>
3606 * <caption align=top>Distribution Statistics</caption>
3607 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3608 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3609 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3610 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3611 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3614 template<typename _RealType
= double>
3615 class exponential_distribution
3617 static_assert(std::is_floating_point
<_RealType
>::value
,
3618 "template argument not a floating point type");
3621 /** The type of the range of the distribution. */
3622 typedef _RealType result_type
;
3623 /** Parameter type. */
3626 typedef exponential_distribution
<_RealType
> distribution_type
;
3629 param_type(_RealType __lambda
= _RealType(1))
3630 : _M_lambda(__lambda
)
3632 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
3637 { return _M_lambda
; }
3640 _RealType _M_lambda
;
3645 * @brief Constructs an exponential distribution with inverse scale
3646 * parameter @f$ \lambda @f$.
3649 exponential_distribution(const result_type
& __lambda
= result_type(1))
3650 : _M_param(__lambda
)
3654 exponential_distribution(const param_type
& __p
)
3659 * @brief Resets the distribution state.
3661 * Has no effect on exponential distributions.
3667 * @brief Returns the inverse scale parameter of the distribution.
3671 { return _M_param
.lambda(); }
3674 * @brief Returns the parameter set of the distribution.
3678 { return _M_param
; }
3681 * @brief Sets the parameter set of the distribution.
3682 * @param __param The new parameter set of the distribution.
3685 param(const param_type
& __param
)
3686 { _M_param
= __param
; }
3689 * @brief Returns the greatest lower bound value of the distribution.
3693 { return result_type(0); }
3696 * @brief Returns the least upper bound value of the distribution.
3700 { return std::numeric_limits
<result_type
>::max(); }
3702 template<typename _UniformRandomNumberGenerator
>
3704 operator()(_UniformRandomNumberGenerator
& __urng
)
3705 { return this->operator()(__urng
, this->param()); }
3707 template<typename _UniformRandomNumberGenerator
>
3709 operator()(_UniformRandomNumberGenerator
& __urng
,
3710 const param_type
& __p
)
3712 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3714 return -std::log(__aurng()) / __p
.lambda();
3718 param_type _M_param
;
3722 * @brief Inserts a %exponential_distribution random number distribution
3723 * @p __x into the output stream @p __os.
3725 * @param __os An output stream.
3726 * @param __x A %exponential_distribution random number distribution.
3728 * @returns The output stream with the state of @p __x inserted or in
3731 template<typename _RealType
, typename _CharT
, typename _Traits
>
3732 std::basic_ostream
<_CharT
, _Traits
>&
3733 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3734 const std::exponential_distribution
<_RealType
>&);
3737 * @brief Extracts a %exponential_distribution random number distribution
3738 * @p __x from the input stream @p __is.
3740 * @param __is An input stream.
3741 * @param __x A %exponential_distribution random number
3744 * @returns The input stream with @p __x extracted or in an error state.
3746 template<typename _RealType
, typename _CharT
, typename _Traits
>
3747 std::basic_istream
<_CharT
, _Traits
>&
3748 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3749 std::exponential_distribution
<_RealType
>&);
3753 * @brief A weibull_distribution random number distribution.
3755 * The formula for the normal probability density function is
3756 * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
3757 * \exp{(-(frac{x}{b})^a)} @f$.
3759 template<typename _RealType
= double>
3760 class weibull_distribution
3762 static_assert(std::is_floating_point
<_RealType
>::value
,
3763 "template argument not a floating point type");
3766 /** The type of the range of the distribution. */
3767 typedef _RealType result_type
;
3768 /** Parameter type. */
3771 typedef weibull_distribution
<_RealType
> distribution_type
;
3774 param_type(_RealType __a
= _RealType(1),
3775 _RealType __b
= _RealType(1))
3776 : _M_a(__a
), _M_b(__b
)
3793 weibull_distribution(_RealType __a
= _RealType(1),
3794 _RealType __b
= _RealType(1))
3795 : _M_param(__a
, __b
)
3799 weibull_distribution(const param_type
& __p
)
3804 * @brief Resets the distribution state.
3811 * @brief Return the @f$ a @f$ parameter of the distribution.
3815 { return _M_param
.a(); }
3818 * @brief Return the @f$ b @f$ parameter of the distribution.
3822 { return _M_param
.b(); }
3825 * @brief Returns the parameter set of the distribution.
3829 { return _M_param
; }
3832 * @brief Sets the parameter set of the distribution.
3833 * @param __param The new parameter set of the distribution.
3836 param(const param_type
& __param
)
3837 { _M_param
= __param
; }
3840 * @brief Returns the greatest lower bound value of the distribution.
3844 { return result_type(0); }
3847 * @brief Returns the least upper bound value of the distribution.
3851 { return std::numeric_limits
<result_type
>::max(); }
3853 template<typename _UniformRandomNumberGenerator
>
3855 operator()(_UniformRandomNumberGenerator
& __urng
)
3856 { return this->operator()(__urng
, this->param()); }
3858 template<typename _UniformRandomNumberGenerator
>
3860 operator()(_UniformRandomNumberGenerator
& __urng
,
3861 const param_type
& __p
);
3864 param_type _M_param
;
3868 * @brief Inserts a %weibull_distribution random number distribution
3869 * @p __x into the output stream @p __os.
3871 * @param __os An output stream.
3872 * @param __x A %weibull_distribution random number distribution.
3874 * @returns The output stream with the state of @p __x inserted or in
3877 template<typename _RealType
, typename _CharT
, typename _Traits
>
3878 std::basic_ostream
<_CharT
, _Traits
>&
3879 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3880 const std::weibull_distribution
<_RealType
>&);
3883 * @brief Extracts a %weibull_distribution random number distribution
3884 * @p __x from the input stream @p __is.
3886 * @param __is An input stream.
3887 * @param __x A %weibull_distribution random number
3890 * @returns The input stream with @p __x extracted or in an error state.
3892 template<typename _RealType
, typename _CharT
, typename _Traits
>
3893 std::basic_istream
<_CharT
, _Traits
>&
3894 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3895 std::weibull_distribution
<_RealType
>&);
3899 * @brief A extreme_value_distribution random number distribution.
3901 * The formula for the normal probability mass function is
3902 * @f$ p(x|a,b) = \frac{1}{b}
3903 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
3905 template<typename _RealType
= double>
3906 class extreme_value_distribution
3908 static_assert(std::is_floating_point
<_RealType
>::value
,
3909 "template argument not a floating point type");
3912 /** The type of the range of the distribution. */
3913 typedef _RealType result_type
;
3914 /** Parameter type. */
3917 typedef extreme_value_distribution
<_RealType
> distribution_type
;
3920 param_type(_RealType __a
= _RealType(0),
3921 _RealType __b
= _RealType(1))
3922 : _M_a(__a
), _M_b(__b
)
3939 extreme_value_distribution(_RealType __a
= _RealType(0),
3940 _RealType __b
= _RealType(1))
3941 : _M_param(__a
, __b
)
3945 extreme_value_distribution(const param_type
& __p
)
3950 * @brief Resets the distribution state.
3957 * @brief Return the @f$ a @f$ parameter of the distribution.
3961 { return _M_param
.a(); }
3964 * @brief Return the @f$ b @f$ parameter of the distribution.
3968 { return _M_param
.b(); }
3971 * @brief Returns the parameter set of the distribution.
3975 { return _M_param
; }
3978 * @brief Sets the parameter set of the distribution.
3979 * @param __param The new parameter set of the distribution.
3982 param(const param_type
& __param
)
3983 { _M_param
= __param
; }
3986 * @brief Returns the greatest lower bound value of the distribution.
3990 { return std::numeric_limits
<result_type
>::min(); }
3993 * @brief Returns the least upper bound value of the distribution.
3997 { return std::numeric_limits
<result_type
>::max(); }
3999 template<typename _UniformRandomNumberGenerator
>
4001 operator()(_UniformRandomNumberGenerator
& __urng
)
4002 { return this->operator()(__urng
, this->param()); }
4004 template<typename _UniformRandomNumberGenerator
>
4006 operator()(_UniformRandomNumberGenerator
& __urng
,
4007 const param_type
& __p
);
4010 param_type _M_param
;
4014 * @brief Inserts a %extreme_value_distribution random number distribution
4015 * @p __x into the output stream @p __os.
4017 * @param __os An output stream.
4018 * @param __x A %extreme_value_distribution random number distribution.
4020 * @returns The output stream with the state of @p __x inserted or in
4023 template<typename _RealType
, typename _CharT
, typename _Traits
>
4024 std::basic_ostream
<_CharT
, _Traits
>&
4025 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4026 const std::extreme_value_distribution
<_RealType
>&);
4029 * @brief Extracts a %extreme_value_distribution random number
4030 * distribution @p __x from the input stream @p __is.
4032 * @param __is An input stream.
4033 * @param __x A %extreme_value_distribution random number
4036 * @returns The input stream with @p __x extracted or in an error state.
4038 template<typename _RealType
, typename _CharT
, typename _Traits
>
4039 std::basic_istream
<_CharT
, _Traits
>&
4040 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4041 std::extreme_value_distribution
<_RealType
>&);
4045 * @brief A discrete_distribution random number distribution.
4047 * The formula for the discrete probability mass function is
4050 template<typename _IntType
= int>
4051 class discrete_distribution
4053 static_assert(std::is_integral
<_IntType
>::value
,
4054 "template argument not an integral type");
4057 /** The type of the range of the distribution. */
4058 typedef _IntType result_type
;
4059 /** Parameter type. */
4062 typedef discrete_distribution
<_IntType
> distribution_type
;
4063 friend class discrete_distribution
<_IntType
>;
4066 : _M_prob(), _M_cp()
4067 { _M_initialize(); }
4069 template<typename _InputIterator
>
4070 param_type(_InputIterator __wbegin
,
4071 _InputIterator __wend
)
4072 : _M_prob(__wbegin
, __wend
), _M_cp()
4073 { _M_initialize(); }
4075 param_type(initializer_list
<double> __wil
)
4076 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4077 { _M_initialize(); }
4079 template<typename _Func
>
4080 param_type(size_t __nw
, double __xmin
, double __xmax
,
4084 probabilities() const
4091 std::vector
<double> _M_prob
;
4092 std::vector
<double> _M_cp
;
4095 discrete_distribution()
4099 template<typename _InputIterator
>
4100 discrete_distribution(_InputIterator __wbegin
,
4101 _InputIterator __wend
)
4102 : _M_param(__wbegin
, __wend
)
4105 discrete_distribution(initializer_list
<double> __wl
)
4109 template<typename _Func
>
4110 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4112 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4116 discrete_distribution(const param_type
& __p
)
4121 * @brief Resets the distribution state.
4128 * @brief Returns the probabilities of the distribution.
4131 probabilities() const
4132 { return _M_param
.probabilities(); }
4135 * @brief Returns the parameter set of the distribution.
4139 { return _M_param
; }
4142 * @brief Sets the parameter set of the distribution.
4143 * @param __param The new parameter set of the distribution.
4146 param(const param_type
& __param
)
4147 { _M_param
= __param
; }
4150 * @brief Returns the greatest lower bound value of the distribution.
4154 { return result_type(0); }
4157 * @brief Returns the least upper bound value of the distribution.
4161 { return this->_M_param
._M_prob
.size() - 1; }
4163 template<typename _UniformRandomNumberGenerator
>
4165 operator()(_UniformRandomNumberGenerator
& __urng
)
4166 { return this->operator()(__urng
, this->param()); }
4168 template<typename _UniformRandomNumberGenerator
>
4170 operator()(_UniformRandomNumberGenerator
& __urng
,
4171 const param_type
& __p
);
4174 * @brief Inserts a %discrete_distribution random number distribution
4175 * @p __x into the output stream @p __os.
4177 * @param __os An output stream.
4178 * @param __x A %discrete_distribution random number distribution.
4180 * @returns The output stream with the state of @p __x inserted or in
4183 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4184 friend std::basic_ostream
<_CharT
, _Traits
>&
4185 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4186 const std::discrete_distribution
<_IntType1
>&);
4189 * @brief Extracts a %discrete_distribution random number distribution
4190 * @p __x from the input stream @p __is.
4192 * @param __is An input stream.
4193 * @param __x A %discrete_distribution random number
4196 * @returns The input stream with @p __x extracted or in an error
4199 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4200 friend std::basic_istream
<_CharT
, _Traits
>&
4201 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4202 std::discrete_distribution
<_IntType1
>&);
4205 param_type _M_param
;
4210 * @brief A piecewise_constant_distribution random number distribution.
4212 * The formula for the piecewise constant probability mass function is
4215 template<typename _RealType
= double>
4216 class piecewise_constant_distribution
4218 static_assert(std::is_floating_point
<_RealType
>::value
,
4219 "template argument not a floating point type");
4222 /** The type of the range of the distribution. */
4223 typedef _RealType result_type
;
4224 /** Parameter type. */
4227 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4228 friend class piecewise_constant_distribution
<_RealType
>;
4231 : _M_int(), _M_den(), _M_cp()
4232 { _M_initialize(); }
4234 template<typename _InputIteratorB
, typename _InputIteratorW
>
4235 param_type(_InputIteratorB __bfirst
,
4236 _InputIteratorB __bend
,
4237 _InputIteratorW __wbegin
);
4239 template<typename _Func
>
4240 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4242 template<typename _Func
>
4243 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4246 std::vector
<_RealType
>
4258 std::vector
<_RealType
> _M_int
;
4259 std::vector
<double> _M_den
;
4260 std::vector
<double> _M_cp
;
4264 piecewise_constant_distribution()
4268 template<typename _InputIteratorB
, typename _InputIteratorW
>
4269 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4270 _InputIteratorB __bend
,
4271 _InputIteratorW __wbegin
)
4272 : _M_param(__bfirst
, __bend
, __wbegin
)
4275 template<typename _Func
>
4276 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4278 : _M_param(__bl
, __fw
)
4281 template<typename _Func
>
4282 piecewise_constant_distribution(size_t __nw
,
4283 _RealType __xmin
, _RealType __xmax
,
4285 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4289 piecewise_constant_distribution(const param_type
& __p
)
4294 * @brief Resets the distribution state.
4301 * @brief Returns a vector of the intervals.
4303 std::vector
<_RealType
>
4305 { return _M_param
.intervals(); }
4308 * @brief Returns a vector of the probability densities.
4312 { return _M_param
.densities(); }
4315 * @brief Returns the parameter set of the distribution.
4319 { return _M_param
; }
4322 * @brief Sets the parameter set of the distribution.
4323 * @param __param The new parameter set of the distribution.
4326 param(const param_type
& __param
)
4327 { _M_param
= __param
; }
4330 * @brief Returns the greatest lower bound value of the distribution.
4334 { return this->_M_param
._M_int
.front(); }
4337 * @brief Returns the least upper bound value of the distribution.
4341 { return this->_M_param
._M_int
.back(); }
4343 template<typename _UniformRandomNumberGenerator
>
4345 operator()(_UniformRandomNumberGenerator
& __urng
)
4346 { return this->operator()(__urng
, this->param()); }
4348 template<typename _UniformRandomNumberGenerator
>
4350 operator()(_UniformRandomNumberGenerator
& __urng
,
4351 const param_type
& __p
);
4354 * @brief Inserts a %piecewise_constan_distribution random
4355 * number distribution @p __x into the output stream @p __os.
4357 * @param __os An output stream.
4358 * @param __x A %piecewise_constan_distribution random number
4361 * @returns The output stream with the state of @p __x inserted or in
4364 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4365 friend std::basic_ostream
<_CharT
, _Traits
>&
4366 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4367 const std::piecewise_constant_distribution
<_RealType1
>&);
4370 * @brief Extracts a %piecewise_constan_distribution random
4371 * number distribution @p __x from the input stream @p __is.
4373 * @param __is An input stream.
4374 * @param __x A %piecewise_constan_distribution random number
4377 * @returns The input stream with @p __x extracted or in an error
4380 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4381 friend std::basic_istream
<_CharT
, _Traits
>&
4382 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4383 std::piecewise_constant_distribution
<_RealType1
>&);
4386 param_type _M_param
;
4391 * @brief A piecewise_linear_distribution random number distribution.
4393 * The formula for the piecewise linear probability mass function is
4396 template<typename _RealType
= double>
4397 class piecewise_linear_distribution
4399 static_assert(std::is_floating_point
<_RealType
>::value
,
4400 "template argument not a floating point type");
4403 /** The type of the range of the distribution. */
4404 typedef _RealType result_type
;
4405 /** Parameter type. */
4408 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
4409 friend class piecewise_linear_distribution
<_RealType
>;
4412 : _M_int(), _M_den(), _M_cp(), _M_m()
4413 { _M_initialize(); }
4415 template<typename _InputIteratorB
, typename _InputIteratorW
>
4416 param_type(_InputIteratorB __bfirst
,
4417 _InputIteratorB __bend
,
4418 _InputIteratorW __wbegin
);
4420 template<typename _Func
>
4421 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
4423 template<typename _Func
>
4424 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4427 std::vector
<_RealType
>
4439 std::vector
<_RealType
> _M_int
;
4440 std::vector
<double> _M_den
;
4441 std::vector
<double> _M_cp
;
4442 std::vector
<double> _M_m
;
4446 piecewise_linear_distribution()
4450 template<typename _InputIteratorB
, typename _InputIteratorW
>
4451 piecewise_linear_distribution(_InputIteratorB __bfirst
,
4452 _InputIteratorB __bend
,
4453 _InputIteratorW __wbegin
)
4454 : _M_param(__bfirst
, __bend
, __wbegin
)
4457 template<typename _Func
>
4458 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
4460 : _M_param(__bl
, __fw
)
4463 template<typename _Func
>
4464 piecewise_linear_distribution(size_t __nw
,
4465 _RealType __xmin
, _RealType __xmax
,
4467 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4471 piecewise_linear_distribution(const param_type
& __p
)
4476 * Resets the distribution state.
4483 * @brief Return the intervals of the distribution.
4485 std::vector
<_RealType
>
4487 { return _M_param
.intervals(); }
4490 * @brief Return a vector of the probability densities of the
4495 { return _M_param
.densities(); }
4498 * @brief Returns the parameter set of the distribution.
4502 { return _M_param
; }
4505 * @brief Sets the parameter set of the distribution.
4506 * @param __param The new parameter set of the distribution.
4509 param(const param_type
& __param
)
4510 { _M_param
= __param
; }
4513 * @brief Returns the greatest lower bound value of the distribution.
4517 { return this->_M_param
._M_int
.front(); }
4520 * @brief Returns the least upper bound value of the distribution.
4524 { return this->_M_param
._M_int
.back(); }
4526 template<typename _UniformRandomNumberGenerator
>
4528 operator()(_UniformRandomNumberGenerator
& __urng
)
4529 { return this->operator()(__urng
, this->param()); }
4531 template<typename _UniformRandomNumberGenerator
>
4533 operator()(_UniformRandomNumberGenerator
& __urng
,
4534 const param_type
& __p
);
4537 * @brief Inserts a %piecewise_linear_distribution random number
4538 * distribution @p __x into the output stream @p __os.
4540 * @param __os An output stream.
4541 * @param __x A %piecewise_linear_distribution random number
4544 * @returns The output stream with the state of @p __x inserted or in
4547 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4548 friend std::basic_ostream
<_CharT
, _Traits
>&
4549 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4550 const std::piecewise_linear_distribution
<_RealType1
>&);
4553 * @brief Extracts a %piecewise_linear_distribution random number
4554 * distribution @p __x from the input stream @p __is.
4556 * @param __is An input stream.
4557 * @param __x A %piecewise_linear_distribution random number
4560 * @returns The input stream with @p __x extracted or in an error
4563 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4564 friend std::basic_istream
<_CharT
, _Traits
>&
4565 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4566 std::piecewise_linear_distribution
<_RealType1
>&);
4569 param_type _M_param
;
4573 /* @} */ // group std_random_distributions_poisson
4575 /* @} */ // group std_random_distributions
4578 * @addtogroup std_random_utilities Random Number Utilities
4579 * @ingroup std_random
4584 * @brief The seed_seq class generates sequences of seeds for random
4585 * number generators.
4591 /** The type of the seed vales. */
4592 typedef uint_least32_t result_type
;
4594 /** Default constructor. */
4599 template<typename _IntType
>
4600 seed_seq(std::initializer_list
<_IntType
> il
);
4602 template<typename _InputIterator
>
4603 seed_seq(_InputIterator __begin
, _InputIterator __end
);
4605 // generating functions
4606 template<typename _RandomAccessIterator
>
4608 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
4610 // property functions
4612 { return _M_v
.size(); }
4614 template<typename OutputIterator
>
4616 param(OutputIterator __dest
) const
4617 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
4621 std::vector
<result_type
> _M_v
;
4624 /* @} */ // group std_random_utilities
4626 /* @} */ // group std_random