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 "_UIntType not an unsigned integral type");
158 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
159 "template argument __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 "_UIntType not an unsigned integral type");
345 static_assert(1u <= __m
&& __m
<= __n
,
346 "template argument __m out of bounds");
347 static_assert(__r
<= __w
, "template argument __r out of bound");
348 static_assert(__u
<= __w
, "template argument __u out of bound");
349 static_assert(__s
<= __w
, "template argument __s out of bound");
350 static_assert(__t
<= __w
, "template argument __t out of bound");
351 static_assert(__l
<= __w
, "template argument __l out of bound");
352 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
353 "template argument __w out of bound");
354 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
355 "template argument __a out of bound");
356 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
357 "template argument __b out of bound");
358 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
359 "template argument __c out of bound");
360 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
361 "template argument __d out of bound");
362 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
363 "template argument __f out of bound");
366 /** The type of the generated random value. */
367 typedef _UIntType result_type
;
370 static const size_t word_size
= __w
;
371 static const size_t state_size
= __n
;
372 static const size_t shift_size
= __m
;
373 static const size_t mask_bits
= __r
;
374 static const result_type xor_mask
= __a
;
375 static const size_t tempering_u
= __u
;
376 static const result_type tempering_d
= __d
;
377 static const size_t tempering_s
= __s
;
378 static const result_type tempering_b
= __b
;
379 static const size_t tempering_t
= __t
;
380 static const result_type tempering_c
= __c
;
381 static const size_t tempering_l
= __l
;
382 static const result_type initialization_multiplier
= __f
;
383 static const result_type default_seed
= 5489u;
385 // constructors and member function
387 mersenne_twister_engine(result_type __sd
= default_seed
)
391 * @brief Constructs a %mersenne_twister_engine random number generator
392 * engine seeded from the seed sequence @p __q.
394 * @param __q the seed sequence.
397 mersenne_twister_engine(seed_seq
& __q
)
401 seed(result_type __sd
= default_seed
);
407 * @brief Gets the smallest possible value in the output range.
409 * @todo This should be constexpr.
416 * @brief Gets the largest possible value in the output range.
418 * @todo This should be constexpr.
422 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
425 * @brief Discard a sequence of random numbers.
427 * @todo Look for a faster way to do discard.
430 discard(unsigned long long __z
)
432 for (; __z
!= 0ULL; --__z
)
440 * @brief Compares two % mersenne_twister_engine random number generator
441 * objects of the same type for equality.
443 * @param __lhs A % mersenne_twister_engine random number generator
445 * @param __rhs Another % mersenne_twister_engine random number
448 * @returns true if the two objects are equal, false otherwise.
451 operator==(const mersenne_twister_engine
& __lhs
,
452 const mersenne_twister_engine
& __rhs
)
453 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
456 * @brief Inserts the current state of a % mersenne_twister_engine
457 * random number generator engine @p __x into the output stream
460 * @param __os An output stream.
461 * @param __x A % mersenne_twister_engine random number generator
464 * @returns The output stream with the state of @p __x inserted or in
467 template<typename _UIntType1
,
468 size_t __w1
, size_t __n1
,
469 size_t __m1
, size_t __r1
,
470 _UIntType1 __a1
, size_t __u1
,
471 _UIntType1 __d1
, size_t __s1
,
472 _UIntType1 __b1
, size_t __t1
,
473 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
474 typename _CharT
, typename _Traits
>
475 friend std::basic_ostream
<_CharT
, _Traits
>&
476 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
477 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
478 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
482 * @brief Extracts the current state of a % mersenne_twister_engine
483 * random number generator engine @p __x from the input stream
486 * @param __is An input stream.
487 * @param __x A % mersenne_twister_engine random number generator
490 * @returns The input stream with the state of @p __x extracted or in
493 template<typename _UIntType1
,
494 size_t __w1
, size_t __n1
,
495 size_t __m1
, size_t __r1
,
496 _UIntType1 __a1
, size_t __u1
,
497 _UIntType1 __d1
, size_t __s1
,
498 _UIntType1 __b1
, size_t __t1
,
499 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
500 typename _CharT
, typename _Traits
>
501 friend std::basic_istream
<_CharT
, _Traits
>&
502 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
503 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
504 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
508 _UIntType _M_x
[state_size
];
513 * @brief The Marsaglia-Zaman generator.
515 * This is a model of a Generalized Fibonacci discrete random number
516 * generator, sometimes referred to as the SWC generator.
518 * A discrete random number generator that produces pseudorandom
519 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
520 * carry_{i-1}) \bmod m @f$.
522 * The size of the state is @f$ r @f$
523 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
525 * @var _M_x The state of the generator. This is a ring buffer.
526 * @var _M_carry The carry.
527 * @var _M_p Current index of x(i - r).
529 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
530 class subtract_with_carry_engine
532 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
533 "_UIntType not an unsigned integral type");
534 static_assert(0u < __s
&& __s
< __r
,
535 "template argument __s out of bounds");
536 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
537 "template argument __w out of bounds");
540 /** The type of the generated random value. */
541 typedef _UIntType result_type
;
544 static const size_t word_size
= __w
;
545 static const size_t short_lag
= __s
;
546 static const size_t long_lag
= __r
;
547 static const result_type default_seed
= 19780503u;
550 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
551 * random number generator.
554 subtract_with_carry_engine(result_type __sd
= default_seed
)
555 { this->seed(__sd
); }
558 * @brief Constructs a %subtract_with_carry_engine random number engine
559 * seeded from the seed sequence @p __q.
561 * @param __q the seed sequence.
564 subtract_with_carry_engine(seed_seq
& __q
)
568 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
571 * N1688[4.19] modifies this as follows. If @p __value == 0,
572 * sets value to 19780503. In any case, with a linear
573 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
574 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
575 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
576 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
577 * set carry to 1, otherwise sets carry to 0.
580 seed(result_type __sd
= default_seed
);
583 * @brief Seeds the initial state @f$ x_0 @f$ of the
584 * % subtract_with_carry_engine random number generator.
590 * @brief Gets the inclusive minimum value of the range of random
591 * integers returned by this generator.
593 * @todo This should be constexpr.
600 * @brief Gets the inclusive maximum value of the range of random
601 * integers returned by this generator.
603 * @todo This should be constexpr.
607 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
610 * @brief Discard a sequence of random numbers.
612 * @todo Look for a faster way to do discard.
615 discard(unsigned long long __z
)
617 for (; __z
!= 0ULL; --__z
)
622 * @brief Gets the next random number in the sequence.
628 * @brief Compares two % subtract_with_carry_engine random number
629 * generator objects of the same type for equality.
631 * @param __lhs A % subtract_with_carry_engine random number generator
633 * @param __rhs Another % subtract_with_carry_engine random number
636 * @returns true if the two objects are equal, false otherwise.
639 operator==(const subtract_with_carry_engine
& __lhs
,
640 const subtract_with_carry_engine
& __rhs
)
641 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
644 * @brief Inserts the current state of a % subtract_with_carry_engine
645 * random number generator engine @p __x into the output stream
648 * @param __os An output stream.
649 * @param __x A % subtract_with_carry_engine random number generator
652 * @returns The output stream with the state of @p __x inserted or in
655 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
656 typename _CharT
, typename _Traits
>
657 friend std::basic_ostream
<_CharT
, _Traits
>&
658 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
659 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
663 * @brief Extracts the current state of a % subtract_with_carry_engine
664 * random number generator engine @p __x from the input stream
667 * @param __is An input stream.
668 * @param __x A % subtract_with_carry_engine random number generator engine.
670 * @returns The input stream with the state of @p __x extracted or in
673 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
674 typename _CharT
, typename _Traits
>
675 friend std::basic_istream
<_CharT
, _Traits
>&
676 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
677 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
681 _UIntType _M_x
[long_lag
];
687 * Produces random numbers from some base engine by discarding blocks of
690 * 0 <= @p __r <= @p __p
692 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
693 class discard_block_engine
695 static_assert(1 <= __r
&& __r
<= __p
,
696 "template argument __r out of bounds");
699 /** The type of the generated random value. */
700 typedef typename
_RandomNumberEngine::result_type result_type
;
703 static const size_t block_size
= __p
;
704 static const size_t used_block
= __r
;
707 * @brief Constructs a default %discard_block_engine engine.
709 * The underlying engine is default constructed as well.
711 discard_block_engine()
712 : _M_b(), _M_n(0) { }
715 * @brief Copy constructs a %discard_block_engine engine.
717 * Copies an existing base class random number generator.
718 * @param rng An existing (base class) engine object.
721 discard_block_engine(const _RandomNumberEngine
& __rne
)
722 : _M_b(__rne
), _M_n(0) { }
725 * @brief Move constructs a %discard_block_engine engine.
727 * Copies an existing base class random number generator.
728 * @param rng An existing (base class) engine object.
731 discard_block_engine(_RandomNumberEngine
&& __rne
)
732 : _M_b(std::move(__rne
)), _M_n(0) { }
735 * @brief Seed constructs a %discard_block_engine engine.
737 * Constructs the underlying generator engine seeded with @p __s.
738 * @param __s A seed value for the base class engine.
741 discard_block_engine(result_type __s
)
742 : _M_b(__s
), _M_n(0) { }
745 * @brief Generator construct a %discard_block_engine engine.
747 * @param __q A seed sequence.
750 discard_block_engine(seed_seq
& __q
)
755 * @brief Reseeds the %discard_block_engine object with the default
756 * seed for the underlying base class generator engine.
766 * @brief Reseeds the %discard_block_engine object with the default
767 * seed for the underlying base class generator engine.
770 seed(result_type __s
)
777 * @brief Reseeds the %discard_block_engine object with the given seed
779 * @param __q A seed generator function.
789 * @brief Gets a const reference to the underlying generator engine
792 const _RandomNumberEngine
&
797 * @brief Gets the minimum value in the generated random number range.
799 * @todo This should be constexpr.
803 { return _M_b
.min(); }
806 * @brief Gets the maximum value in the generated random number range.
808 * @todo This should be constexpr.
812 { return _M_b
.max(); }
815 * @brief Discard a sequence of random numbers.
817 * @todo Look for a faster way to do discard.
820 discard(unsigned long long __z
)
822 for (; __z
!= 0ULL; --__z
)
827 * @brief Gets the next value in the generated random number sequence.
833 * @brief Compares two %discard_block_engine random number generator
834 * objects of the same type for equality.
836 * @param __lhs A %discard_block_engine random number generator object.
837 * @param __rhs Another %discard_block_engine random number generator
840 * @returns true if the two objects are equal, false otherwise.
843 operator==(const discard_block_engine
& __lhs
,
844 const discard_block_engine
& __rhs
)
845 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
848 * @brief Inserts the current state of a %discard_block_engine random
849 * number generator engine @p __x into the output stream
852 * @param __os An output stream.
853 * @param __x A %discard_block_engine random number generator engine.
855 * @returns The output stream with the state of @p __x inserted or in
858 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
859 typename _CharT
, typename _Traits
>
860 friend std::basic_ostream
<_CharT
, _Traits
>&
861 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
862 const std::discard_block_engine
<_RandomNumberEngine1
,
866 * @brief Extracts the current state of a % subtract_with_carry_engine
867 * random number generator engine @p __x from the input stream
870 * @param __is An input stream.
871 * @param __x A %discard_block_engine random number generator engine.
873 * @returns The input stream with the state of @p __x extracted or in
876 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
877 typename _CharT
, typename _Traits
>
878 friend std::basic_istream
<_CharT
, _Traits
>&
879 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
880 std::discard_block_engine
<_RandomNumberEngine1
,
884 _RandomNumberEngine _M_b
;
889 * Produces random numbers by combining random numbers from some base
890 * engine to produce random numbers with a specifies number of bits @p __w.
892 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
893 class independent_bits_engine
895 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
896 "_UIntType not an unsigned integral type");
897 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
898 "template argument __w out of bounds");
901 /** The type of the generated random value. */
902 typedef _UIntType result_type
;
905 * @brief Constructs a default %independent_bits_engine engine.
907 * The underlying engine is default constructed as well.
909 independent_bits_engine()
913 * @brief Copy constructs a %independent_bits_engine engine.
915 * Copies an existing base class random number generator.
916 * @param rng An existing (base class) engine object.
919 independent_bits_engine(const _RandomNumberEngine
& __rne
)
923 * @brief Move constructs a %independent_bits_engine engine.
925 * Copies an existing base class random number generator.
926 * @param rng An existing (base class) engine object.
929 independent_bits_engine(_RandomNumberEngine
&& __rne
)
930 : _M_b(std::move(__rne
)) { }
933 * @brief Seed constructs a %independent_bits_engine engine.
935 * Constructs the underlying generator engine seeded with @p __s.
936 * @param __s A seed value for the base class engine.
939 independent_bits_engine(result_type __s
)
943 * @brief Generator construct a %independent_bits_engine engine.
945 * @param __q A seed sequence.
948 independent_bits_engine(seed_seq
& __q
)
953 * @brief Reseeds the %independent_bits_engine object with the default
954 * seed for the underlying base class generator engine.
961 * @brief Reseeds the %independent_bits_engine object with the default
962 * seed for the underlying base class generator engine.
965 seed(result_type __s
)
969 * @brief Reseeds the %independent_bits_engine object with the given
971 * @param __q A seed generator function.
978 * @brief Gets a const reference to the underlying generator engine
981 const _RandomNumberEngine
&
986 * @brief Gets the minimum value in the generated random number range.
988 * @todo This should be constexpr.
995 * @brief Gets the maximum value in the generated random number range.
997 * @todo This should be constexpr.
1001 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1004 * @brief Discard a sequence of random numbers.
1006 * @todo Look for a faster way to do discard.
1009 discard(unsigned long long __z
)
1011 for (; __z
!= 0ULL; --__z
)
1016 * @brief Gets the next value in the generated random number sequence.
1022 * @brief Compares two %independent_bits_engine random number generator
1023 * objects of the same type for equality.
1025 * @param __lhs A %independent_bits_engine random number generator
1027 * @param __rhs Another %independent_bits_engine random number generator
1030 * @returns true if the two objects are equal, false otherwise.
1033 operator==(const independent_bits_engine
& __lhs
,
1034 const independent_bits_engine
& __rhs
)
1035 { return __lhs
._M_b
== __rhs
._M_b
; }
1038 * @brief Extracts the current state of a % subtract_with_carry_engine
1039 * random number generator engine @p __x from the input stream
1042 * @param __is An input stream.
1043 * @param __x A %independent_bits_engine random number generator
1046 * @returns The input stream with the state of @p __x extracted or in
1049 template<typename _CharT
, typename _Traits
>
1050 friend std::basic_istream
<_CharT
, _Traits
>&
1051 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1052 std::independent_bits_engine
<_RandomNumberEngine
,
1053 __w
, _UIntType
>& __x
)
1060 _RandomNumberEngine _M_b
;
1064 * @brief Inserts the current state of a %independent_bits_engine random
1065 * number generator engine @p __x into the output stream @p __os.
1067 * @param __os An output stream.
1068 * @param __x A %independent_bits_engine random number generator engine.
1070 * @returns The output stream with the state of @p __x inserted or in
1073 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1074 typename _CharT
, typename _Traits
>
1075 std::basic_ostream
<_CharT
, _Traits
>&
1076 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1077 const std::independent_bits_engine
<_RandomNumberEngine
,
1078 __w
, _UIntType
>& __x
)
1085 * @brief Produces random numbers by combining random numbers from some
1086 * base engine to produce random numbers with a specifies number of bits
1089 template<typename _RandomNumberEngine
, size_t __k
>
1090 class shuffle_order_engine
1092 static_assert(1u <= __k
, "template argument __k out of bound");
1095 /** The type of the generated random value. */
1096 typedef typename
_RandomNumberEngine::result_type result_type
;
1098 static const size_t table_size
= __k
;
1101 * @brief Constructs a default %shuffle_order_engine engine.
1103 * The underlying engine is default constructed as well.
1105 shuffle_order_engine()
1107 { _M_initialize(); }
1110 * @brief Copy constructs a %shuffle_order_engine engine.
1112 * Copies an existing base class random number generator.
1113 * @param rng An existing (base class) engine object.
1116 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1118 { _M_initialize(); }
1121 * @brief Move constructs a %shuffle_order_engine engine.
1123 * Copies an existing base class random number generator.
1124 * @param rng An existing (base class) engine object.
1127 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1128 : _M_b(std::move(__rne
))
1129 { _M_initialize(); }
1132 * @brief Seed constructs a %shuffle_order_engine engine.
1134 * Constructs the underlying generator engine seeded with @p __s.
1135 * @param __s A seed value for the base class engine.
1138 shuffle_order_engine(result_type __s
)
1140 { _M_initialize(); }
1143 * @brief Generator construct a %shuffle_order_engine engine.
1145 * @param __q A seed sequence.
1148 shuffle_order_engine(seed_seq
& __q
)
1150 { _M_initialize(); }
1153 * @brief Reseeds the %shuffle_order_engine object with the default seed
1154 for the underlying base class generator engine.
1164 * @brief Reseeds the %shuffle_order_engine object with the default seed
1165 * for the underlying base class generator engine.
1168 seed(result_type __s
)
1175 * @brief Reseeds the %shuffle_order_engine object with the given seed
1177 * @param __q A seed generator function.
1187 * Gets a const reference to the underlying generator engine object.
1189 const _RandomNumberEngine
&
1194 * Gets the minimum value in the generated random number range.
1196 * @todo This should be constexpr.
1200 { return _M_b
.min(); }
1203 * Gets the maximum value in the generated random number range.
1205 * @todo This should be constexpr.
1209 { return _M_b
.max(); }
1212 * Discard a sequence of random numbers.
1214 * @todo Look for a faster way to do discard.
1217 discard(unsigned long long __z
)
1219 for (; __z
!= 0ULL; --__z
)
1224 * Gets the next value in the generated random number sequence.
1230 * Compares two %shuffle_order_engine random number generator objects
1231 * of the same type for equality.
1233 * @param __lhs A %shuffle_order_engine random number generator object.
1234 * @param __rhs Another %shuffle_order_engine random number generator
1237 * @returns true if the two objects are equal, false otherwise.
1240 operator==(const shuffle_order_engine
& __lhs
,
1241 const shuffle_order_engine
& __rhs
)
1242 { return __lhs
._M_b
== __rhs
._M_b
; }
1245 * @brief Inserts the current state of a %shuffle_order_engine random
1246 * number generator engine @p __x into the output stream
1249 * @param __os An output stream.
1250 * @param __x A %shuffle_order_engine random number generator engine.
1252 * @returns The output stream with the state of @p __x inserted or in
1255 template<typename _RandomNumberEngine1
, size_t __k1
,
1256 typename _CharT
, typename _Traits
>
1257 friend std::basic_ostream
<_CharT
, _Traits
>&
1258 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1259 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1263 * @brief Extracts the current state of a % subtract_with_carry_engine
1264 * random number generator engine @p __x from the input stream
1267 * @param __is An input stream.
1268 * @param __x A %shuffle_order_engine random number generator engine.
1270 * @returns The input stream with the state of @p __x extracted or in
1273 template<typename _RandomNumberEngine1
, size_t __k1
,
1274 typename _CharT
, typename _Traits
>
1275 friend std::basic_istream
<_CharT
, _Traits
>&
1276 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1277 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1280 void _M_initialize()
1282 for (size_t __i
= 0; __i
< __k
; ++__i
)
1287 _RandomNumberEngine _M_b
;
1288 result_type _M_v
[__k
];
1293 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1295 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1299 * An alternative LCR (Lehmer Generator function) .
1301 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1305 * The classic Mersenne Twister.
1308 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1309 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1310 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1312 typedef mersenne_twister_engine
<
1318 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1321 * An alternative Mersenne Twister.
1323 typedef mersenne_twister_engine
<
1326 0xb5026f5aa96619e9ULL
, 29,
1327 0x5555555555555555ULL
, 17,
1328 0x71d67fffeda60000ULL
, 37,
1329 0xfff7eee000000000ULL
, 43,
1330 6364136223846793005ULL> mt19937_64
;
1335 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1338 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1341 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1343 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1348 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1353 typedef minstd_rand0 default_random_engine
;
1356 * A standard interface to a platform-specific non-deterministic
1357 * random number generator (if any are available).
1362 /** The type of the generated random value. */
1363 typedef unsigned int result_type
;
1365 // constructors, destructors and member functions
1367 #ifdef _GLIBCXX_USE_RANDOM_TR1
1370 random_device(const std::string
& __token
= "/dev/urandom")
1372 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1373 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1374 std::__throw_runtime_error(__N("random_device::"
1375 "random_device(const std::string&)"));
1379 { std::fclose(_M_file
); }
1384 random_device(const std::string
& __token
= "mt19937")
1385 : _M_mt(_M_strtoul(__token
)) { }
1388 static unsigned long
1389 _M_strtoul(const std::string
& __str
)
1391 unsigned long __ret
= 5489UL;
1392 if (__str
!= "mt19937")
1394 const char* __nptr
= __str
.c_str();
1396 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1397 if (*__nptr
== '\0' || *__endptr
!= '\0')
1398 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1399 "(const std::string&)"));
1410 { return std::numeric_limits
<result_type
>::min(); }
1414 { return std::numeric_limits
<result_type
>::max(); }
1423 #ifdef _GLIBCXX_USE_RANDOM_TR1
1425 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1433 // No copy functions.
1434 random_device(const random_device
&) = delete;
1435 void operator=(const random_device
&) = delete;
1439 #ifdef _GLIBCXX_USE_RANDOM_TR1
1446 /* @} */ // group std_random_generators
1449 * @addtogroup std_random_distributions Random Number Distributions
1450 * @ingroup std_random
1455 * @addtogroup std_random_distributions_uniform Uniform Distributions
1456 * @ingroup std_random_distributions
1461 * @brief Uniform discrete distribution for random numbers.
1462 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1463 * probability throughout the range.
1465 template<typename _IntType
= int>
1466 class uniform_int_distribution
1468 static_assert(std::is_integral
<_IntType
>::value
,
1469 "template argument not an integral type");
1472 /** The type of the range of the distribution. */
1473 typedef _IntType result_type
;
1474 /** Parameter type. */
1477 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1480 param_type(_IntType __a
= 0,
1481 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1482 : _M_a(__a
), _M_b(__b
)
1484 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1502 * @brief Constructs a uniform distribution object.
1505 uniform_int_distribution(_IntType __a
= 0,
1506 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1507 : _M_param(__a
, __b
)
1511 uniform_int_distribution(const param_type
& __p
)
1516 * @brief Resets the distribution state.
1518 * Does nothing for the uniform integer distribution.
1525 { return _M_param
.a(); }
1529 { return _M_param
.b(); }
1532 * @brief Returns the inclusive lower bound of the distribution range.
1536 { return this->a(); }
1539 * @brief Returns the inclusive upper bound of the distribution range.
1543 { return this->b(); }
1546 * @brief Returns the parameter set of the distribution.
1550 { return _M_param
; }
1553 * @brief Sets the parameter set of the distribution.
1554 * @param __param The new parameter set of the distribution.
1557 param(const param_type
& __param
)
1558 { _M_param
= __param
; }
1561 * Gets a uniformly distributed random number in the range
1564 template<typename _UniformRandomNumberGenerator
>
1566 operator()(_UniformRandomNumberGenerator
& __urng
)
1567 { return this->operator()(__urng
, this->param()); }
1570 * Gets a uniform random number in the range @f$[0, n)@f$.
1572 * This function is aimed at use with std::random_shuffle.
1574 template<typename _UniformRandomNumberGenerator
>
1576 operator()(_UniformRandomNumberGenerator
& __urng
,
1577 const param_type
& __p
);
1579 param_type _M_param
;
1583 * @brief Inserts a %uniform_int_distribution random number
1584 * distribution @p __x into the output stream @p os.
1586 * @param __os An output stream.
1587 * @param __x A %uniform_int_distribution random number distribution.
1589 * @returns The output stream with the state of @p __x inserted or in
1592 template<typename _IntType
, typename _CharT
, typename _Traits
>
1593 std::basic_ostream
<_CharT
, _Traits
>&
1594 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1595 const std::uniform_int_distribution
<_IntType
>&);
1598 * @brief Extracts a %uniform_int_distribution random number distribution
1599 * @p __x from the input stream @p __is.
1601 * @param __is An input stream.
1602 * @param __x A %uniform_int_distribution random number generator engine.
1604 * @returns The input stream with @p __x extracted or in an error state.
1606 template<typename _IntType
, typename _CharT
, typename _Traits
>
1607 std::basic_istream
<_CharT
, _Traits
>&
1608 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1609 std::uniform_int_distribution
<_IntType
>&);
1613 * @brief Uniform continuous distribution for random numbers.
1615 * A continuous random distribution on the range [min, max) with equal
1616 * probability throughout the range. The URNG should be real-valued and
1617 * deliver number in the range [0, 1).
1619 template<typename _RealType
= double>
1620 class uniform_real_distribution
1622 static_assert(std::is_floating_point
<_RealType
>::value
,
1623 "template argument not a floating point type");
1626 /** The type of the range of the distribution. */
1627 typedef _RealType result_type
;
1628 /** Parameter type. */
1631 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1634 param_type(_RealType __a
= _RealType(0),
1635 _RealType __b
= _RealType(1))
1636 : _M_a(__a
), _M_b(__b
)
1638 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1656 * @brief Constructs a uniform_real_distribution object.
1658 * @param __min [IN] The lower bound of the distribution.
1659 * @param __max [IN] The upper bound of the distribution.
1662 uniform_real_distribution(_RealType __a
= _RealType(0),
1663 _RealType __b
= _RealType(1))
1664 : _M_param(__a
, __b
)
1668 uniform_real_distribution(const param_type
& __p
)
1673 * @brief Resets the distribution state.
1675 * Does nothing for the uniform real distribution.
1682 { return _M_param
.a(); }
1686 { return _M_param
.b(); }
1689 * @brief Returns the inclusive lower bound of the distribution range.
1693 { return this->a(); }
1696 * @brief Returns the inclusive upper bound of the distribution range.
1700 { return this->b(); }
1703 * @brief Returns the parameter set of the distribution.
1707 { return _M_param
; }
1710 * @brief Sets the parameter set of the distribution.
1711 * @param __param The new parameter set of the distribution.
1714 param(const param_type
& __param
)
1715 { _M_param
= __param
; }
1717 template<typename _UniformRandomNumberGenerator
>
1719 operator()(_UniformRandomNumberGenerator
& __urng
)
1720 { return this->operator()(__urng
, this->param()); }
1722 template<typename _UniformRandomNumberGenerator
>
1724 operator()(_UniformRandomNumberGenerator
& __urng
,
1725 const param_type
& __p
)
1727 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1729 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1733 param_type _M_param
;
1737 * @brief Inserts a %uniform_real_distribution random number
1738 * distribution @p __x into the output stream @p __os.
1740 * @param __os An output stream.
1741 * @param __x A %uniform_real_distribution random number distribution.
1743 * @returns The output stream with the state of @p __x inserted or in
1746 template<typename _RealType
, typename _CharT
, typename _Traits
>
1747 std::basic_ostream
<_CharT
, _Traits
>&
1748 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1749 const std::uniform_real_distribution
<_RealType
>&);
1752 * @brief Extracts a %uniform_real_distribution random number distribution
1753 * @p __x from the input stream @p __is.
1755 * @param __is An input stream.
1756 * @param __x A %uniform_real_distribution random number generator engine.
1758 * @returns The input stream with @p __x extracted or in an error state.
1760 template<typename _RealType
, typename _CharT
, typename _Traits
>
1761 std::basic_istream
<_CharT
, _Traits
>&
1762 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1763 std::uniform_real_distribution
<_RealType
>&);
1765 /* @} */ // group std_random_distributions_uniform
1768 * @addtogroup std_random_distributions_normal Normal Distributions
1769 * @ingroup std_random_distributions
1774 * @brief A normal continuous distribution for random numbers.
1776 * The formula for the normal probability density function is
1777 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1778 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1780 template<typename _RealType
= double>
1781 class normal_distribution
1783 static_assert(std::is_floating_point
<_RealType
>::value
,
1784 "template argument not a floating point type");
1787 /** The type of the range of the distribution. */
1788 typedef _RealType result_type
;
1789 /** Parameter type. */
1792 typedef normal_distribution
<_RealType
> distribution_type
;
1795 param_type(_RealType __mean
= _RealType(0),
1796 _RealType __stddev
= _RealType(1))
1797 : _M_mean(__mean
), _M_stddev(__stddev
)
1799 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1808 { return _M_stddev
; }
1812 _RealType _M_stddev
;
1817 * Constructs a normal distribution with parameters @f$ mean @f$ and
1818 * standard deviation.
1821 normal_distribution(result_type __mean
= result_type(0),
1822 result_type __stddev
= result_type(1))
1823 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1827 normal_distribution(const param_type
& __p
)
1828 : _M_param(__p
), _M_saved_available(false)
1832 * @brief Resets the distribution state.
1836 { _M_saved_available
= false; }
1839 * @brief Returns the mean of the distribution.
1843 { return _M_param
.mean(); }
1846 * @brief Returns the standard deviation of the distribution.
1850 { return _M_param
.stddev(); }
1853 * @brief Returns the parameter set of the distribution.
1857 { return _M_param
; }
1860 * @brief Sets the parameter set of the distribution.
1861 * @param __param The new parameter set of the distribution.
1864 param(const param_type
& __param
)
1865 { _M_param
= __param
; }
1868 * @brief Returns the greatest lower bound value of the distribution.
1872 { return std::numeric_limits
<result_type
>::min(); }
1875 * @brief Returns the least upper bound value of the distribution.
1879 { return std::numeric_limits
<result_type
>::max(); }
1881 template<typename _UniformRandomNumberGenerator
>
1883 operator()(_UniformRandomNumberGenerator
& __urng
)
1884 { return this->operator()(__urng
, this->param()); }
1886 template<typename _UniformRandomNumberGenerator
>
1888 operator()(_UniformRandomNumberGenerator
& __urng
,
1889 const param_type
& __p
);
1892 * @brief Inserts a %normal_distribution random number distribution
1893 * @p __x into the output stream @p __os.
1895 * @param __os An output stream.
1896 * @param __x A %normal_distribution random number distribution.
1898 * @returns The output stream with the state of @p __x inserted or in
1901 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1902 friend std::basic_ostream
<_CharT
, _Traits
>&
1903 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1904 const std::normal_distribution
<_RealType1
>&);
1907 * @brief Extracts a %normal_distribution random number distribution
1908 * @p __x from the input stream @p __is.
1910 * @param __is An input stream.
1911 * @param __x A %normal_distribution random number generator engine.
1913 * @returns The input stream with @p __x extracted or in an error
1916 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1917 friend std::basic_istream
<_CharT
, _Traits
>&
1918 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1919 std::normal_distribution
<_RealType1
>&);
1922 param_type _M_param
;
1923 result_type _M_saved
;
1924 bool _M_saved_available
;
1929 * @brief A lognormal_distribution random number distribution.
1931 * The formula for the normal probability mass function is
1932 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
1933 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
1935 template<typename _RealType
= double>
1936 class lognormal_distribution
1938 static_assert(std::is_floating_point
<_RealType
>::value
,
1939 "template argument not a floating point type");
1942 /** The type of the range of the distribution. */
1943 typedef _RealType result_type
;
1944 /** Parameter type. */
1947 typedef lognormal_distribution
<_RealType
> distribution_type
;
1950 param_type(_RealType __m
= _RealType(0),
1951 _RealType __s
= _RealType(1))
1952 : _M_m(__m
), _M_s(__s
)
1969 lognormal_distribution(_RealType __m
= _RealType(0),
1970 _RealType __s
= _RealType(1))
1971 : _M_param(__m
, __s
), _M_nd()
1975 lognormal_distribution(const param_type
& __p
)
1976 : _M_param(__p
), _M_nd()
1980 * Resets the distribution state.
1991 { return _M_param
.m(); }
1995 { return _M_param
.s(); }
1998 * @brief Returns the parameter set of the distribution.
2002 { return _M_param
; }
2005 * @brief Sets the parameter set of the distribution.
2006 * @param __param The new parameter set of the distribution.
2009 param(const param_type
& __param
)
2010 { _M_param
= __param
; }
2013 * @brief Returns the greatest lower bound value of the distribution.
2017 { return result_type(0); }
2020 * @brief Returns the least upper bound value of the distribution.
2024 { return std::numeric_limits
<result_type
>::max(); }
2026 template<typename _UniformRandomNumberGenerator
>
2028 operator()(_UniformRandomNumberGenerator
& __urng
)
2029 { return this->operator()(__urng
, this->param()); }
2031 template<typename _UniformRandomNumberGenerator
>
2033 operator()(_UniformRandomNumberGenerator
& __urng
,
2034 const param_type
& __p
)
2035 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2038 * @brief Inserts a %lognormal_distribution random number distribution
2039 * @p __x into the output stream @p __os.
2041 * @param __os An output stream.
2042 * @param __x A %lognormal_distribution random number distribution.
2044 * @returns The output stream with the state of @p __x inserted or in
2047 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2048 friend std::basic_ostream
<_CharT
, _Traits
>&
2049 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2050 const std::lognormal_distribution
<_RealType1
>&);
2053 * @brief Extracts a %lognormal_distribution random number distribution
2054 * @p __x from the input stream @p __is.
2056 * @param __is An input stream.
2057 * @param __x A %lognormal_distribution random number
2060 * @returns The input stream with @p __x extracted or in an error state.
2062 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2063 friend std::basic_istream
<_CharT
, _Traits
>&
2064 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2065 std::lognormal_distribution
<_RealType1
>&);
2068 param_type _M_param
;
2070 std::normal_distribution
<result_type
> _M_nd
;
2075 * @brief A gamma continuous distribution for random numbers.
2077 * The formula for the gamma probability density function is
2078 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2079 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
2081 template<typename _RealType
= double>
2082 class gamma_distribution
2084 static_assert(std::is_floating_point
<_RealType
>::value
,
2085 "template argument not a floating point type");
2088 /** The type of the range of the distribution. */
2089 typedef _RealType result_type
;
2090 /** Parameter type. */
2093 typedef gamma_distribution
<_RealType
> distribution_type
;
2094 friend class gamma_distribution
<_RealType
>;
2097 param_type(_RealType __alpha_val
= _RealType(1),
2098 _RealType __beta_val
= _RealType(1))
2099 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2101 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2107 { return _M_alpha
; }
2120 _RealType _M_malpha
, _M_a2
;
2125 * @brief Constructs a gamma distribution with parameters
2126 * @f$ \alpha @f$ and @f$ \beta @f$.
2129 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2130 _RealType __beta_val
= _RealType(1))
2131 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2135 gamma_distribution(const param_type
& __p
)
2136 : _M_param(__p
), _M_nd()
2140 * @brief Resets the distribution state.
2147 * @brief Returns the @f$ \alpha @f$ of the distribution.
2151 { return _M_param
.alpha(); }
2154 * @brief Returns the @f$ \beta @f$ of the distribution.
2158 { return _M_param
.beta(); }
2161 * @brief Returns the parameter set of the distribution.
2165 { return _M_param
; }
2168 * @brief Sets the parameter set of the distribution.
2169 * @param __param The new parameter set of the distribution.
2172 param(const param_type
& __param
)
2173 { _M_param
= __param
; }
2176 * @brief Returns the greatest lower bound value of the distribution.
2180 { return result_type(0); }
2183 * @brief Returns the least upper bound value of the distribution.
2187 { return std::numeric_limits
<result_type
>::max(); }
2189 template<typename _UniformRandomNumberGenerator
>
2191 operator()(_UniformRandomNumberGenerator
& __urng
)
2192 { return this->operator()(__urng
, this->param()); }
2194 template<typename _UniformRandomNumberGenerator
>
2196 operator()(_UniformRandomNumberGenerator
& __urng
,
2197 const param_type
& __p
);
2200 * @brief Inserts a %gamma_distribution random number distribution
2201 * @p __x into the output stream @p __os.
2203 * @param __os An output stream.
2204 * @param __x A %gamma_distribution random number distribution.
2206 * @returns The output stream with the state of @p __x inserted or in
2209 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2210 friend std::basic_ostream
<_CharT
, _Traits
>&
2211 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2212 const std::gamma_distribution
<_RealType1
>&);
2215 * @brief Extracts a %gamma_distribution random number distribution
2216 * @p __x from the input stream @p __is.
2218 * @param __is An input stream.
2219 * @param __x A %gamma_distribution random number generator engine.
2221 * @returns The input stream with @p __x extracted or in an error state.
2223 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2224 friend std::basic_istream
<_CharT
, _Traits
>&
2225 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2226 std::gamma_distribution
<_RealType1
>&);
2229 param_type _M_param
;
2231 std::normal_distribution
<result_type
> _M_nd
;
2236 * @brief A chi_squared_distribution random number distribution.
2238 * The formula for the normal probability mass function is
2239 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2241 template<typename _RealType
= double>
2242 class chi_squared_distribution
2244 static_assert(std::is_floating_point
<_RealType
>::value
,
2245 "template argument not a floating point type");
2248 /** The type of the range of the distribution. */
2249 typedef _RealType result_type
;
2250 /** Parameter type. */
2253 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2256 param_type(_RealType __n
= _RealType(1))
2269 chi_squared_distribution(_RealType __n
= _RealType(1))
2270 : _M_param(__n
), _M_gd(__n
/ 2)
2274 chi_squared_distribution(const param_type
& __p
)
2275 : _M_param(__p
), _M_gd(__p
.n() / 2)
2279 * @brief Resets the distribution state.
2290 { return _M_param
.n(); }
2293 * @brief Returns the parameter set of the distribution.
2297 { return _M_param
; }
2300 * @brief Sets the parameter set of the distribution.
2301 * @param __param The new parameter set of the distribution.
2304 param(const param_type
& __param
)
2305 { _M_param
= __param
; }
2308 * @brief Returns the greatest lower bound value of the distribution.
2312 { return result_type(0); }
2315 * @brief Returns the least upper bound value of the distribution.
2319 { return std::numeric_limits
<result_type
>::max(); }
2321 template<typename _UniformRandomNumberGenerator
>
2323 operator()(_UniformRandomNumberGenerator
& __urng
)
2324 { return 2 * _M_gd(__urng
); }
2326 template<typename _UniformRandomNumberGenerator
>
2328 operator()(_UniformRandomNumberGenerator
& __urng
,
2329 const param_type
& __p
)
2331 typedef typename
std::gamma_distribution
<result_type
>::param_type
2333 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2337 * @brief Inserts a %chi_squared_distribution random number distribution
2338 * @p __x into the output stream @p __os.
2340 * @param __os An output stream.
2341 * @param __x A %chi_squared_distribution random number distribution.
2343 * @returns The output stream with the state of @p __x inserted or in
2346 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2347 friend std::basic_ostream
<_CharT
, _Traits
>&
2348 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2349 const std::chi_squared_distribution
<_RealType1
>&);
2352 * @brief Extracts a %chi_squared_distribution random number distribution
2353 * @p __x from the input stream @p __is.
2355 * @param __is An input stream.
2356 * @param __x A %chi_squared_distribution random number
2359 * @returns The input stream with @p __x extracted or in an error state.
2361 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2362 friend std::basic_istream
<_CharT
, _Traits
>&
2363 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2364 std::chi_squared_distribution
<_RealType1
>&);
2367 param_type _M_param
;
2369 std::gamma_distribution
<result_type
> _M_gd
;
2374 * @brief A cauchy_distribution random number distribution.
2376 * The formula for the normal probability mass function is
2377 * @f$ p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1} @f$
2379 template<typename _RealType
= double>
2380 class cauchy_distribution
2382 static_assert(std::is_floating_point
<_RealType
>::value
,
2383 "template argument not a floating point type");
2386 /** The type of the range of the distribution. */
2387 typedef _RealType result_type
;
2388 /** Parameter type. */
2391 typedef cauchy_distribution
<_RealType
> distribution_type
;
2394 param_type(_RealType __a
= _RealType(0),
2395 _RealType __b
= _RealType(1))
2396 : _M_a(__a
), _M_b(__b
)
2413 cauchy_distribution(_RealType __a
= _RealType(0),
2414 _RealType __b
= _RealType(1))
2415 : _M_param(__a
, __b
)
2419 cauchy_distribution(const param_type
& __p
)
2424 * @brief Resets the distribution state.
2435 { return _M_param
.a(); }
2439 { return _M_param
.b(); }
2442 * @brief Returns the parameter set of the distribution.
2446 { return _M_param
; }
2449 * @brief Sets the parameter set of the distribution.
2450 * @param __param The new parameter set of the distribution.
2453 param(const param_type
& __param
)
2454 { _M_param
= __param
; }
2457 * @brief Returns the greatest lower bound value of the distribution.
2461 { return std::numeric_limits
<result_type
>::min(); }
2464 * @brief Returns the least upper bound value of the distribution.
2468 { return std::numeric_limits
<result_type
>::max(); }
2470 template<typename _UniformRandomNumberGenerator
>
2472 operator()(_UniformRandomNumberGenerator
& __urng
)
2473 { return this->operator()(__urng
, this->param()); }
2475 template<typename _UniformRandomNumberGenerator
>
2477 operator()(_UniformRandomNumberGenerator
& __urng
,
2478 const param_type
& __p
);
2481 param_type _M_param
;
2485 * @brief Inserts a %cauchy_distribution random number distribution
2486 * @p __x into the output stream @p __os.
2488 * @param __os An output stream.
2489 * @param __x A %cauchy_distribution random number distribution.
2491 * @returns The output stream with the state of @p __x inserted or in
2494 template<typename _RealType
, typename _CharT
, typename _Traits
>
2495 std::basic_ostream
<_CharT
, _Traits
>&
2496 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2497 const std::cauchy_distribution
<_RealType
>&);
2500 * @brief Extracts a %cauchy_distribution random number distribution
2501 * @p __x from the input stream @p __is.
2503 * @param __is An input stream.
2504 * @param __x A %cauchy_distribution random number
2507 * @returns The input stream with @p __x extracted or in an error state.
2509 template<typename _RealType
, typename _CharT
, typename _Traits
>
2510 std::basic_istream
<_CharT
, _Traits
>&
2511 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2512 std::cauchy_distribution
<_RealType
>&);
2516 * @brief A fisher_f_distribution random number distribution.
2518 * The formula for the normal probability mass function is
2519 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2520 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2521 * (1 + \frac{mx}{n})^{-(m+n)/2} @f$
2523 template<typename _RealType
= double>
2524 class fisher_f_distribution
2526 static_assert(std::is_floating_point
<_RealType
>::value
,
2527 "template argument not a floating point type");
2530 /** The type of the range of the distribution. */
2531 typedef _RealType result_type
;
2532 /** Parameter type. */
2535 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2538 param_type(_RealType __m
= _RealType(1),
2539 _RealType __n
= _RealType(1))
2540 : _M_m(__m
), _M_n(__n
)
2557 fisher_f_distribution(_RealType __m
= _RealType(1),
2558 _RealType __n
= _RealType(1))
2559 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2563 fisher_f_distribution(const param_type
& __p
)
2564 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2568 * @brief Resets the distribution state.
2582 { return _M_param
.m(); }
2586 { return _M_param
.n(); }
2589 * @brief Returns the parameter set of the distribution.
2593 { return _M_param
; }
2596 * @brief Sets the parameter set of the distribution.
2597 * @param __param The new parameter set of the distribution.
2600 param(const param_type
& __param
)
2601 { _M_param
= __param
; }
2604 * @brief Returns the greatest lower bound value of the distribution.
2608 { return result_type(0); }
2611 * @brief Returns the least upper bound value of the distribution.
2615 { return std::numeric_limits
<result_type
>::max(); }
2617 template<typename _UniformRandomNumberGenerator
>
2619 operator()(_UniformRandomNumberGenerator
& __urng
)
2620 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2622 template<typename _UniformRandomNumberGenerator
>
2624 operator()(_UniformRandomNumberGenerator
& __urng
,
2625 const param_type
& __p
)
2627 typedef typename
std::gamma_distribution
<result_type
>::param_type
2629 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2630 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2634 * @brief Inserts a %fisher_f_distribution random number distribution
2635 * @p __x into the output stream @p __os.
2637 * @param __os An output stream.
2638 * @param __x A %fisher_f_distribution random number distribution.
2640 * @returns The output stream with the state of @p __x inserted or in
2643 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2644 friend std::basic_ostream
<_CharT
, _Traits
>&
2645 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2646 const std::fisher_f_distribution
<_RealType1
>&);
2649 * @brief Extracts a %fisher_f_distribution random number distribution
2650 * @p __x from the input stream @p __is.
2652 * @param __is An input stream.
2653 * @param __x A %fisher_f_distribution random number
2656 * @returns The input stream with @p __x extracted or in an error state.
2658 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2659 friend std::basic_istream
<_CharT
, _Traits
>&
2660 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2661 std::fisher_f_distribution
<_RealType1
>&);
2664 param_type _M_param
;
2666 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
2671 * @brief A student_t_distribution random number distribution.
2673 * The formula for the normal probability mass function is
2674 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2675 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} @f$
2677 template<typename _RealType
= double>
2678 class student_t_distribution
2680 static_assert(std::is_floating_point
<_RealType
>::value
,
2681 "template argument not a floating point type");
2684 /** The type of the range of the distribution. */
2685 typedef _RealType result_type
;
2686 /** Parameter type. */
2689 typedef student_t_distribution
<_RealType
> distribution_type
;
2692 param_type(_RealType __n
= _RealType(1))
2705 student_t_distribution(_RealType __n
= _RealType(1))
2706 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
2710 student_t_distribution(const param_type
& __p
)
2711 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
2715 * @brief Resets the distribution state.
2729 { return _M_param
.n(); }
2732 * @brief Returns the parameter set of the distribution.
2736 { return _M_param
; }
2739 * @brief Sets the parameter set of the distribution.
2740 * @param __param The new parameter set of the distribution.
2743 param(const param_type
& __param
)
2744 { _M_param
= __param
; }
2747 * @brief Returns the greatest lower bound value of the distribution.
2751 { return std::numeric_limits
<result_type
>::min(); }
2754 * @brief Returns the least upper bound value of the distribution.
2758 { return std::numeric_limits
<result_type
>::max(); }
2760 template<typename _UniformRandomNumberGenerator
>
2762 operator()(_UniformRandomNumberGenerator
& __urng
)
2763 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
2765 template<typename _UniformRandomNumberGenerator
>
2767 operator()(_UniformRandomNumberGenerator
& __urng
,
2768 const param_type
& __p
)
2770 typedef typename
std::gamma_distribution
<result_type
>::param_type
2773 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
2774 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
2778 * @brief Inserts a %student_t_distribution random number distribution
2779 * @p __x into the output stream @p __os.
2781 * @param __os An output stream.
2782 * @param __x A %student_t_distribution random number distribution.
2784 * @returns The output stream with the state of @p __x inserted or in
2787 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2788 friend std::basic_ostream
<_CharT
, _Traits
>&
2789 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2790 const std::student_t_distribution
<_RealType1
>&);
2793 * @brief Extracts a %student_t_distribution random number distribution
2794 * @p __x from the input stream @p __is.
2796 * @param __is An input stream.
2797 * @param __x A %student_t_distribution random number
2800 * @returns The input stream with @p __x extracted or in an error state.
2802 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2803 friend std::basic_istream
<_CharT
, _Traits
>&
2804 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2805 std::student_t_distribution
<_RealType1
>&);
2808 param_type _M_param
;
2810 std::normal_distribution
<result_type
> _M_nd
;
2811 std::gamma_distribution
<result_type
> _M_gd
;
2814 /* @} */ // group std_random_distributions_normal
2817 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2818 * @ingroup std_random_distributions
2823 * @brief A Bernoulli random number distribution.
2825 * Generates a sequence of true and false values with likelihood @f$ p @f$
2826 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2828 class bernoulli_distribution
2831 /** The type of the range of the distribution. */
2832 typedef bool result_type
;
2833 /** Parameter type. */
2836 typedef bernoulli_distribution distribution_type
;
2839 param_type(double __p
= 0.5)
2842 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
2855 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2857 * @param __p [IN] The likelihood of a true result being returned.
2858 * Must be in the interval @f$ [0, 1] @f$.
2861 bernoulli_distribution(double __p
= 0.5)
2866 bernoulli_distribution(const param_type
& __p
)
2871 * @brief Resets the distribution state.
2873 * Does nothing for a Bernoulli distribution.
2879 * @brief Returns the @p p parameter of the distribution.
2883 { return _M_param
.p(); }
2886 * @brief Returns the parameter set of the distribution.
2890 { return _M_param
; }
2893 * @brief Sets the parameter set of the distribution.
2894 * @param __param The new parameter set of the distribution.
2897 param(const param_type
& __param
)
2898 { _M_param
= __param
; }
2901 * @brief Returns the greatest lower bound value of the distribution.
2905 { return std::numeric_limits
<result_type
>::min(); }
2908 * @brief Returns the least upper bound value of the distribution.
2912 { return std::numeric_limits
<result_type
>::max(); }
2915 * @brief Returns the next value in the Bernoullian sequence.
2917 template<typename _UniformRandomNumberGenerator
>
2919 operator()(_UniformRandomNumberGenerator
& __urng
)
2920 { return this->operator()(__urng
, this->param()); }
2922 template<typename _UniformRandomNumberGenerator
>
2924 operator()(_UniformRandomNumberGenerator
& __urng
,
2925 const param_type
& __p
)
2927 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2929 if ((__aurng() - __aurng
.min())
2930 < __p
.p() * (__aurng
.max() - __aurng
.min()))
2936 param_type _M_param
;
2940 * @brief Inserts a %bernoulli_distribution random number distribution
2941 * @p __x into the output stream @p __os.
2943 * @param __os An output stream.
2944 * @param __x A %bernoulli_distribution random number distribution.
2946 * @returns The output stream with the state of @p __x inserted or in
2949 template<typename _CharT
, typename _Traits
>
2950 std::basic_ostream
<_CharT
, _Traits
>&
2951 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2952 const std::bernoulli_distribution
&);
2955 * @brief Extracts a %bernoulli_distribution random number distribution
2956 * @p __x from the input stream @p __is.
2958 * @param __is An input stream.
2959 * @param __x A %bernoulli_distribution random number generator engine.
2961 * @returns The input stream with @p __x extracted or in an error state.
2963 template<typename _CharT
, typename _Traits
>
2964 std::basic_istream
<_CharT
, _Traits
>&
2965 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2966 std::bernoulli_distribution
& __x
)
2970 __x
.param(bernoulli_distribution::param_type(__p
));
2976 * @brief A discrete binomial random number distribution.
2978 * The formula for the binomial probability density function is
2979 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
2980 * and @f$ p @f$ are the parameters of the distribution.
2982 template<typename _IntType
= int>
2983 class binomial_distribution
2985 static_assert(std::is_integral
<_IntType
>::value
,
2986 "template argument not an integral type");
2989 /** The type of the range of the distribution. */
2990 typedef _IntType result_type
;
2991 /** Parameter type. */
2994 typedef binomial_distribution
<_IntType
> distribution_type
;
2995 friend class binomial_distribution
<_IntType
>;
2998 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
2999 : _M_t(__t
), _M_p(__p
)
3001 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3023 #if _GLIBCXX_USE_C99_MATH_TR1
3024 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3025 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3030 // constructors and member function
3032 binomial_distribution(_IntType __t
= _IntType(1),
3034 : _M_param(__t
, __p
), _M_nd()
3038 binomial_distribution(const param_type
& __p
)
3039 : _M_param(__p
), _M_nd()
3043 * @brief Resets the distribution state.
3050 * @brief Returns the distribution @p t parameter.
3054 { return _M_param
.t(); }
3057 * @brief Returns the distribution @p p parameter.
3061 { return _M_param
.p(); }
3064 * @brief Returns the parameter set of the distribution.
3068 { return _M_param
; }
3071 * @brief Sets the parameter set of the distribution.
3072 * @param __param The new parameter set of the distribution.
3075 param(const param_type
& __param
)
3076 { _M_param
= __param
; }
3079 * @brief Returns the greatest lower bound value of the distribution.
3086 * @brief Returns the least upper bound value of the distribution.
3090 { return _M_param
.t(); }
3092 template<typename _UniformRandomNumberGenerator
>
3094 operator()(_UniformRandomNumberGenerator
& __urng
)
3095 { return this->operator()(__urng
, this->param()); }
3097 template<typename _UniformRandomNumberGenerator
>
3099 operator()(_UniformRandomNumberGenerator
& __urng
,
3100 const param_type
& __p
);
3103 * @brief Inserts a %binomial_distribution random number distribution
3104 * @p __x into the output stream @p __os.
3106 * @param __os An output stream.
3107 * @param __x A %binomial_distribution random number distribution.
3109 * @returns The output stream with the state of @p __x inserted or in
3112 template<typename _IntType1
,
3113 typename _CharT
, typename _Traits
>
3114 friend std::basic_ostream
<_CharT
, _Traits
>&
3115 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3116 const std::binomial_distribution
<_IntType1
>&);
3119 * @brief Extracts a %binomial_distribution random number distribution
3120 * @p __x from the input stream @p __is.
3122 * @param __is An input stream.
3123 * @param __x A %binomial_distribution random number generator engine.
3125 * @returns The input stream with @p __x extracted or in an error
3128 template<typename _IntType1
,
3129 typename _CharT
, typename _Traits
>
3130 friend std::basic_istream
<_CharT
, _Traits
>&
3131 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3132 std::binomial_distribution
<_IntType1
>&);
3135 template<typename _UniformRandomNumberGenerator
>
3137 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3139 param_type _M_param
;
3141 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3142 std::normal_distribution
<double> _M_nd
;
3147 * @brief A discrete geometric random number distribution.
3149 * The formula for the geometric probability density function is
3150 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3153 template<typename _IntType
= int>
3154 class geometric_distribution
3156 static_assert(std::is_integral
<_IntType
>::value
,
3157 "template argument not an integral type");
3160 /** The type of the range of the distribution. */
3161 typedef _IntType result_type
;
3162 /** Parameter type. */
3165 typedef geometric_distribution
<_IntType
> distribution_type
;
3166 friend class geometric_distribution
<_IntType
>;
3169 param_type(double __p
= 0.5)
3172 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3184 { _M_log_p
= std::log(_M_p
); }
3191 // constructors and member function
3193 geometric_distribution(double __p
= 0.5)
3198 geometric_distribution(const param_type
& __p
)
3203 * @brief Resets the distribution state.
3205 * Does nothing for the geometric distribution.
3211 * @brief Returns the distribution parameter @p p.
3215 { return _M_param
.p(); }
3218 * @brief Returns the parameter set of the distribution.
3222 { return _M_param
; }
3225 * @brief Sets the parameter set of the distribution.
3226 * @param __param The new parameter set of the distribution.
3229 param(const param_type
& __param
)
3230 { _M_param
= __param
; }
3233 * @brief Returns the greatest lower bound value of the distribution.
3240 * @brief Returns the least upper bound value of the distribution.
3244 { return std::numeric_limits
<result_type
>::max(); }
3246 template<typename _UniformRandomNumberGenerator
>
3248 operator()(_UniformRandomNumberGenerator
& __urng
)
3249 { return this->operator()(__urng
, this->param()); }
3251 template<typename _UniformRandomNumberGenerator
>
3253 operator()(_UniformRandomNumberGenerator
& __urng
,
3254 const param_type
& __p
);
3257 param_type _M_param
;
3261 * @brief Inserts a %geometric_distribution random number distribution
3262 * @p __x into the output stream @p __os.
3264 * @param __os An output stream.
3265 * @param __x A %geometric_distribution random number distribution.
3267 * @returns The output stream with the state of @p __x inserted or in
3270 template<typename _IntType
,
3271 typename _CharT
, typename _Traits
>
3272 std::basic_ostream
<_CharT
, _Traits
>&
3273 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3274 const std::geometric_distribution
<_IntType
>&);
3277 * @brief Extracts a %geometric_distribution random number distribution
3278 * @p __x from the input stream @p __is.
3280 * @param __is An input stream.
3281 * @param __x A %geometric_distribution random number generator engine.
3283 * @returns The input stream with @p __x extracted or in an error state.
3285 template<typename _IntType
,
3286 typename _CharT
, typename _Traits
>
3287 std::basic_istream
<_CharT
, _Traits
>&
3288 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3289 std::geometric_distribution
<_IntType
>&);
3293 * @brief A negative_binomial_distribution random number distribution.
3295 * The formula for the negative binomial probability mass function is
3296 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3297 * and @f$ p @f$ are the parameters of the distribution.
3299 template<typename _IntType
= int>
3300 class negative_binomial_distribution
3302 static_assert(std::is_integral
<_IntType
>::value
,
3303 "template argument not an integral type");
3306 /** The type of the range of the distribution. */
3307 typedef _IntType result_type
;
3308 /** Parameter type. */
3311 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3314 param_type(_IntType __k
= 1, double __p
= 0.5)
3315 : _M_k(__k
), _M_p(__p
)
3332 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3333 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3337 negative_binomial_distribution(const param_type
& __p
)
3338 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3342 * @brief Resets the distribution state.
3349 * @brief Return the @f$ k @f$ parameter of the distribution.
3353 { return _M_param
.k(); }
3356 * @brief Return the @f$ p @f$ parameter of the distribution.
3360 { return _M_param
.p(); }
3363 * @brief Returns the parameter set of the distribution.
3367 { return _M_param
; }
3370 * @brief Sets the parameter set of the distribution.
3371 * @param __param The new parameter set of the distribution.
3374 param(const param_type
& __param
)
3375 { _M_param
= __param
; }
3378 * @brief Returns the greatest lower bound value of the distribution.
3382 { return result_type(0); }
3385 * @brief Returns the least upper bound value of the distribution.
3389 { return std::numeric_limits
<result_type
>::max(); }
3391 template<typename _UniformRandomNumberGenerator
>
3393 operator()(_UniformRandomNumberGenerator
& __urng
);
3395 template<typename _UniformRandomNumberGenerator
>
3397 operator()(_UniformRandomNumberGenerator
& __urng
,
3398 const param_type
& __p
);
3401 * @brief Inserts a %negative_binomial_distribution random
3402 * number distribution @p __x into the output stream @p __os.
3404 * @param __os An output stream.
3405 * @param __x A %negative_binomial_distribution random number
3408 * @returns The output stream with the state of @p __x inserted or in
3411 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3412 friend std::basic_ostream
<_CharT
, _Traits
>&
3413 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3414 const std::negative_binomial_distribution
<_IntType1
>&);
3417 * @brief Extracts a %negative_binomial_distribution random number
3418 * distribution @p __x from the input stream @p __is.
3420 * @param __is An input stream.
3421 * @param __x A %negative_binomial_distribution random number
3424 * @returns The input stream with @p __x extracted or in an error state.
3426 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3427 friend std::basic_istream
<_CharT
, _Traits
>&
3428 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3429 std::negative_binomial_distribution
<_IntType1
>&);
3432 param_type _M_param
;
3434 std::gamma_distribution
<double> _M_gd
;
3437 /* @} */ // group std_random_distributions_bernoulli
3440 * @addtogroup std_random_distributions_poisson Poisson Distributions
3441 * @ingroup std_random_distributions
3446 * @brief A discrete Poisson random number distribution.
3448 * The formula for the Poisson probability density function is
3449 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3450 * parameter of the distribution.
3452 template<typename _IntType
= int>
3453 class poisson_distribution
3455 static_assert(std::is_integral
<_IntType
>::value
,
3456 "template argument not an integral type");
3459 /** The type of the range of the distribution. */
3460 typedef _IntType result_type
;
3461 /** Parameter type. */
3464 typedef poisson_distribution
<_IntType
> distribution_type
;
3465 friend class poisson_distribution
<_IntType
>;
3468 param_type(double __mean
= 1.0)
3471 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3480 // Hosts either log(mean) or the threshold of the simple method.
3487 #if _GLIBCXX_USE_C99_MATH_TR1
3488 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3492 // constructors and member function
3494 poisson_distribution(double __mean
= 1.0)
3495 : _M_param(__mean
), _M_nd()
3499 poisson_distribution(const param_type
& __p
)
3500 : _M_param(__p
), _M_nd()
3504 * @brief Resets the distribution state.
3511 * @brief Returns the distribution parameter @p mean.
3515 { return _M_param
.mean(); }
3518 * @brief Returns the parameter set of the distribution.
3522 { return _M_param
; }
3525 * @brief Sets the parameter set of the distribution.
3526 * @param __param The new parameter set of the distribution.
3529 param(const param_type
& __param
)
3530 { _M_param
= __param
; }
3533 * @brief Returns the greatest lower bound value of the distribution.
3540 * @brief Returns the least upper bound value of the distribution.
3544 { return std::numeric_limits
<result_type
>::max(); }
3546 template<typename _UniformRandomNumberGenerator
>
3548 operator()(_UniformRandomNumberGenerator
& __urng
)
3549 { return this->operator()(__urng
, this->param()); }
3551 template<typename _UniformRandomNumberGenerator
>
3553 operator()(_UniformRandomNumberGenerator
& __urng
,
3554 const param_type
& __p
);
3557 * @brief Inserts a %poisson_distribution random number distribution
3558 * @p __x into the output stream @p __os.
3560 * @param __os An output stream.
3561 * @param __x A %poisson_distribution random number distribution.
3563 * @returns The output stream with the state of @p __x inserted or in
3566 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3567 friend std::basic_ostream
<_CharT
, _Traits
>&
3568 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3569 const std::poisson_distribution
<_IntType1
>&);
3572 * @brief Extracts a %poisson_distribution random number distribution
3573 * @p __x from the input stream @p __is.
3575 * @param __is An input stream.
3576 * @param __x A %poisson_distribution random number generator engine.
3578 * @returns The input stream with @p __x extracted or in an error
3581 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3582 friend std::basic_istream
<_CharT
, _Traits
>&
3583 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3584 std::poisson_distribution
<_IntType1
>&);
3587 param_type _M_param
;
3589 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3590 std::normal_distribution
<double> _M_nd
;
3594 * @brief An exponential continuous distribution for random numbers.
3596 * The formula for the exponential probability density function is
3597 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3599 * <table border=1 cellpadding=10 cellspacing=0>
3600 * <caption align=top>Distribution Statistics</caption>
3601 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3602 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3603 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3604 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3605 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3608 template<typename _RealType
= double>
3609 class exponential_distribution
3611 static_assert(std::is_floating_point
<_RealType
>::value
,
3612 "template argument not a floating point type");
3615 /** The type of the range of the distribution. */
3616 typedef _RealType result_type
;
3617 /** Parameter type. */
3620 typedef exponential_distribution
<_RealType
> distribution_type
;
3623 param_type(_RealType __lambda
= _RealType(1))
3624 : _M_lambda(__lambda
)
3626 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
3631 { return _M_lambda
; }
3634 _RealType _M_lambda
;
3639 * @brief Constructs an exponential distribution with inverse scale
3640 * parameter @f$ \lambda @f$.
3643 exponential_distribution(const result_type
& __lambda
= result_type(1))
3644 : _M_param(__lambda
)
3648 exponential_distribution(const param_type
& __p
)
3653 * @brief Resets the distribution state.
3655 * Has no effect on exponential distributions.
3661 * @brief Returns the inverse scale parameter of the distribution.
3665 { return _M_param
.lambda(); }
3668 * @brief Returns the parameter set of the distribution.
3672 { return _M_param
; }
3675 * @brief Sets the parameter set of the distribution.
3676 * @param __param The new parameter set of the distribution.
3679 param(const param_type
& __param
)
3680 { _M_param
= __param
; }
3683 * @brief Returns the greatest lower bound value of the distribution.
3687 { return result_type(0); }
3690 * @brief Returns the least upper bound value of the distribution.
3694 { return std::numeric_limits
<result_type
>::max(); }
3696 template<typename _UniformRandomNumberGenerator
>
3698 operator()(_UniformRandomNumberGenerator
& __urng
)
3699 { return this->operator()(__urng
, this->param()); }
3701 template<typename _UniformRandomNumberGenerator
>
3703 operator()(_UniformRandomNumberGenerator
& __urng
,
3704 const param_type
& __p
)
3706 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3708 return -std::log(__aurng()) / __p
.lambda();
3712 param_type _M_param
;
3716 * @brief Inserts a %exponential_distribution random number distribution
3717 * @p __x into the output stream @p __os.
3719 * @param __os An output stream.
3720 * @param __x A %exponential_distribution random number distribution.
3722 * @returns The output stream with the state of @p __x inserted or in
3725 template<typename _RealType
, typename _CharT
, typename _Traits
>
3726 std::basic_ostream
<_CharT
, _Traits
>&
3727 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3728 const std::exponential_distribution
<_RealType
>&);
3731 * @brief Extracts a %exponential_distribution random number distribution
3732 * @p __x from the input stream @p __is.
3734 * @param __is An input stream.
3735 * @param __x A %exponential_distribution random number
3738 * @returns The input stream with @p __x extracted or in an error state.
3740 template<typename _RealType
, typename _CharT
, typename _Traits
>
3741 std::basic_istream
<_CharT
, _Traits
>&
3742 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3743 std::exponential_distribution
<_RealType
>&);
3747 * @brief A weibull_distribution random number distribution.
3749 * The formula for the normal probability density function is
3750 * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
3751 * \exp{(-(frac{x}{b})^a)} @f$.
3753 template<typename _RealType
= double>
3754 class weibull_distribution
3756 static_assert(std::is_floating_point
<_RealType
>::value
,
3757 "template argument not a floating point type");
3760 /** The type of the range of the distribution. */
3761 typedef _RealType result_type
;
3762 /** Parameter type. */
3765 typedef weibull_distribution
<_RealType
> distribution_type
;
3768 param_type(_RealType __a
= _RealType(1),
3769 _RealType __b
= _RealType(1))
3770 : _M_a(__a
), _M_b(__b
)
3787 weibull_distribution(_RealType __a
= _RealType(1),
3788 _RealType __b
= _RealType(1))
3789 : _M_param(__a
, __b
)
3793 weibull_distribution(const param_type
& __p
)
3798 * @brief Resets the distribution state.
3805 * @brief Return the @f$ a @f$ parameter of the distribution.
3809 { return _M_param
.a(); }
3812 * @brief Return the @f$ b @f$ parameter of the distribution.
3816 { return _M_param
.b(); }
3819 * @brief Returns the parameter set of the distribution.
3823 { return _M_param
; }
3826 * @brief Sets the parameter set of the distribution.
3827 * @param __param The new parameter set of the distribution.
3830 param(const param_type
& __param
)
3831 { _M_param
= __param
; }
3834 * @brief Returns the greatest lower bound value of the distribution.
3838 { return result_type(0); }
3841 * @brief Returns the least upper bound value of the distribution.
3845 { return std::numeric_limits
<result_type
>::max(); }
3847 template<typename _UniformRandomNumberGenerator
>
3849 operator()(_UniformRandomNumberGenerator
& __urng
)
3850 { return this->operator()(__urng
, this->param()); }
3852 template<typename _UniformRandomNumberGenerator
>
3854 operator()(_UniformRandomNumberGenerator
& __urng
,
3855 const param_type
& __p
);
3858 param_type _M_param
;
3862 * @brief Inserts a %weibull_distribution random number distribution
3863 * @p __x into the output stream @p __os.
3865 * @param __os An output stream.
3866 * @param __x A %weibull_distribution random number distribution.
3868 * @returns The output stream with the state of @p __x inserted or in
3871 template<typename _RealType
, typename _CharT
, typename _Traits
>
3872 std::basic_ostream
<_CharT
, _Traits
>&
3873 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3874 const std::weibull_distribution
<_RealType
>&);
3877 * @brief Extracts a %weibull_distribution random number distribution
3878 * @p __x from the input stream @p __is.
3880 * @param __is An input stream.
3881 * @param __x A %weibull_distribution random number
3884 * @returns The input stream with @p __x extracted or in an error state.
3886 template<typename _RealType
, typename _CharT
, typename _Traits
>
3887 std::basic_istream
<_CharT
, _Traits
>&
3888 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3889 std::weibull_distribution
<_RealType
>&);
3893 * @brief A extreme_value_distribution random number distribution.
3895 * The formula for the normal probability mass function is
3896 * @f$ p(x|a,b) = \frac{1}{b}
3897 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
3899 template<typename _RealType
= double>
3900 class extreme_value_distribution
3902 static_assert(std::is_floating_point
<_RealType
>::value
,
3903 "template argument not a floating point type");
3906 /** The type of the range of the distribution. */
3907 typedef _RealType result_type
;
3908 /** Parameter type. */
3911 typedef extreme_value_distribution
<_RealType
> distribution_type
;
3914 param_type(_RealType __a
= _RealType(0),
3915 _RealType __b
= _RealType(1))
3916 : _M_a(__a
), _M_b(__b
)
3933 extreme_value_distribution(_RealType __a
= _RealType(0),
3934 _RealType __b
= _RealType(1))
3935 : _M_param(__a
, __b
)
3939 extreme_value_distribution(const param_type
& __p
)
3944 * @brief Resets the distribution state.
3951 * @brief Return the @f$ a @f$ parameter of the distribution.
3955 { return _M_param
.a(); }
3958 * @brief Return the @f$ b @f$ parameter of the distribution.
3962 { return _M_param
.b(); }
3965 * @brief Returns the parameter set of the distribution.
3969 { return _M_param
; }
3972 * @brief Sets the parameter set of the distribution.
3973 * @param __param The new parameter set of the distribution.
3976 param(const param_type
& __param
)
3977 { _M_param
= __param
; }
3980 * @brief Returns the greatest lower bound value of the distribution.
3984 { return std::numeric_limits
<result_type
>::min(); }
3987 * @brief Returns the least upper bound value of the distribution.
3991 { return std::numeric_limits
<result_type
>::max(); }
3993 template<typename _UniformRandomNumberGenerator
>
3995 operator()(_UniformRandomNumberGenerator
& __urng
)
3996 { return this->operator()(__urng
, this->param()); }
3998 template<typename _UniformRandomNumberGenerator
>
4000 operator()(_UniformRandomNumberGenerator
& __urng
,
4001 const param_type
& __p
);
4004 param_type _M_param
;
4008 * @brief Inserts a %extreme_value_distribution random number distribution
4009 * @p __x into the output stream @p __os.
4011 * @param __os An output stream.
4012 * @param __x A %extreme_value_distribution random number distribution.
4014 * @returns The output stream with the state of @p __x inserted or in
4017 template<typename _RealType
, typename _CharT
, typename _Traits
>
4018 std::basic_ostream
<_CharT
, _Traits
>&
4019 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4020 const std::extreme_value_distribution
<_RealType
>&);
4023 * @brief Extracts a %extreme_value_distribution random number
4024 * distribution @p __x from the input stream @p __is.
4026 * @param __is An input stream.
4027 * @param __x A %extreme_value_distribution random number
4030 * @returns The input stream with @p __x extracted or in an error state.
4032 template<typename _RealType
, typename _CharT
, typename _Traits
>
4033 std::basic_istream
<_CharT
, _Traits
>&
4034 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4035 std::extreme_value_distribution
<_RealType
>&);
4039 * @brief A discrete_distribution random number distribution.
4041 * The formula for the discrete probability mass function is
4044 template<typename _IntType
= int>
4045 class discrete_distribution
4047 static_assert(std::is_integral
<_IntType
>::value
,
4048 "template argument not an integral type");
4051 /** The type of the range of the distribution. */
4052 typedef _IntType result_type
;
4053 /** Parameter type. */
4056 typedef discrete_distribution
<_IntType
> distribution_type
;
4057 friend class discrete_distribution
<_IntType
>;
4060 : _M_prob(), _M_cp()
4061 { _M_initialize(); }
4063 template<typename _InputIterator
>
4064 param_type(_InputIterator __wbegin
,
4065 _InputIterator __wend
)
4066 : _M_prob(__wbegin
, __wend
), _M_cp()
4067 { _M_initialize(); }
4069 param_type(initializer_list
<double> __wil
)
4070 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4071 { _M_initialize(); }
4073 template<typename _Func
>
4074 param_type(size_t __nw
, double __xmin
, double __xmax
,
4078 probabilities() const
4085 std::vector
<double> _M_prob
;
4086 std::vector
<double> _M_cp
;
4089 discrete_distribution()
4093 template<typename _InputIterator
>
4094 discrete_distribution(_InputIterator __wbegin
,
4095 _InputIterator __wend
)
4096 : _M_param(__wbegin
, __wend
)
4099 discrete_distribution(initializer_list
<double> __wl
)
4103 template<typename _Func
>
4104 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4106 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4110 discrete_distribution(const param_type
& __p
)
4115 * @brief Resets the distribution state.
4122 * @brief Returns the probabilities of the distribution.
4125 probabilities() const
4126 { return _M_param
.probabilities(); }
4129 * @brief Returns the parameter set of the distribution.
4133 { return _M_param
; }
4136 * @brief Sets the parameter set of the distribution.
4137 * @param __param The new parameter set of the distribution.
4140 param(const param_type
& __param
)
4141 { _M_param
= __param
; }
4144 * @brief Returns the greatest lower bound value of the distribution.
4148 { return result_type(0); }
4151 * @brief Returns the least upper bound value of the distribution.
4155 { return this->_M_param
._M_prob
.size() - 1; }
4157 template<typename _UniformRandomNumberGenerator
>
4159 operator()(_UniformRandomNumberGenerator
& __urng
)
4160 { return this->operator()(__urng
, this->param()); }
4162 template<typename _UniformRandomNumberGenerator
>
4164 operator()(_UniformRandomNumberGenerator
& __urng
,
4165 const param_type
& __p
);
4168 * @brief Inserts a %discrete_distribution random number distribution
4169 * @p __x into the output stream @p __os.
4171 * @param __os An output stream.
4172 * @param __x A %discrete_distribution random number distribution.
4174 * @returns The output stream with the state of @p __x inserted or in
4177 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4178 friend std::basic_ostream
<_CharT
, _Traits
>&
4179 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4180 const std::discrete_distribution
<_IntType1
>&);
4183 * @brief Extracts a %discrete_distribution random number distribution
4184 * @p __x from the input stream @p __is.
4186 * @param __is An input stream.
4187 * @param __x A %discrete_distribution random number
4190 * @returns The input stream with @p __x extracted or in an error
4193 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4194 friend std::basic_istream
<_CharT
, _Traits
>&
4195 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4196 std::discrete_distribution
<_IntType1
>&);
4199 param_type _M_param
;
4204 * @brief A piecewise_constant_distribution random number distribution.
4206 * The formula for the piecewise constant probability mass function is
4209 template<typename _RealType
= double>
4210 class piecewise_constant_distribution
4212 static_assert(std::is_floating_point
<_RealType
>::value
,
4213 "template argument not a floating point type");
4216 /** The type of the range of the distribution. */
4217 typedef _RealType result_type
;
4218 /** Parameter type. */
4221 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4222 friend class piecewise_constant_distribution
<_RealType
>;
4225 : _M_int(), _M_den(), _M_cp()
4226 { _M_initialize(); }
4228 template<typename _InputIteratorB
, typename _InputIteratorW
>
4229 param_type(_InputIteratorB __bfirst
,
4230 _InputIteratorB __bend
,
4231 _InputIteratorW __wbegin
);
4233 template<typename _Func
>
4234 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4236 template<typename _Func
>
4237 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4240 std::vector
<_RealType
>
4252 std::vector
<_RealType
> _M_int
;
4253 std::vector
<double> _M_den
;
4254 std::vector
<double> _M_cp
;
4258 piecewise_constant_distribution()
4262 template<typename _InputIteratorB
, typename _InputIteratorW
>
4263 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4264 _InputIteratorB __bend
,
4265 _InputIteratorW __wbegin
)
4266 : _M_param(__bfirst
, __bend
, __wbegin
)
4269 template<typename _Func
>
4270 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4272 : _M_param(__bl
, __fw
)
4275 template<typename _Func
>
4276 piecewise_constant_distribution(size_t __nw
,
4277 _RealType __xmin
, _RealType __xmax
,
4279 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4283 piecewise_constant_distribution(const param_type
& __p
)
4288 * @brief Resets the distribution state.
4295 * @brief Returns a vector of the intervals.
4297 std::vector
<_RealType
>
4299 { return _M_param
.intervals(); }
4302 * @brief Returns a vector of the probability densities.
4306 { return _M_param
.densities(); }
4309 * @brief Returns the parameter set of the distribution.
4313 { return _M_param
; }
4316 * @brief Sets the parameter set of the distribution.
4317 * @param __param The new parameter set of the distribution.
4320 param(const param_type
& __param
)
4321 { _M_param
= __param
; }
4324 * @brief Returns the greatest lower bound value of the distribution.
4328 { return this->_M_param
._M_int
.front(); }
4331 * @brief Returns the least upper bound value of the distribution.
4335 { return this->_M_param
._M_int
.back(); }
4337 template<typename _UniformRandomNumberGenerator
>
4339 operator()(_UniformRandomNumberGenerator
& __urng
)
4340 { return this->operator()(__urng
, this->param()); }
4342 template<typename _UniformRandomNumberGenerator
>
4344 operator()(_UniformRandomNumberGenerator
& __urng
,
4345 const param_type
& __p
);
4348 * @brief Inserts a %piecewise_constan_distribution random
4349 * number distribution @p __x into the output stream @p __os.
4351 * @param __os An output stream.
4352 * @param __x A %piecewise_constan_distribution random number
4355 * @returns The output stream with the state of @p __x inserted or in
4358 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4359 friend std::basic_ostream
<_CharT
, _Traits
>&
4360 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4361 const std::piecewise_constant_distribution
<_RealType1
>&);
4364 * @brief Extracts a %piecewise_constan_distribution random
4365 * number distribution @p __x from the input stream @p __is.
4367 * @param __is An input stream.
4368 * @param __x A %piecewise_constan_distribution random number
4371 * @returns The input stream with @p __x extracted or in an error
4374 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4375 friend std::basic_istream
<_CharT
, _Traits
>&
4376 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4377 std::piecewise_constant_distribution
<_RealType1
>&);
4380 param_type _M_param
;
4385 * @brief A piecewise_linear_distribution random number distribution.
4387 * The formula for the piecewise linear probability mass function is
4390 template<typename _RealType
= double>
4391 class piecewise_linear_distribution
4393 static_assert(std::is_floating_point
<_RealType
>::value
,
4394 "template argument not a floating point type");
4397 /** The type of the range of the distribution. */
4398 typedef _RealType result_type
;
4399 /** Parameter type. */
4402 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
4403 friend class piecewise_linear_distribution
<_RealType
>;
4406 : _M_int(), _M_den(), _M_cp(), _M_m()
4407 { _M_initialize(); }
4409 template<typename _InputIteratorB
, typename _InputIteratorW
>
4410 param_type(_InputIteratorB __bfirst
,
4411 _InputIteratorB __bend
,
4412 _InputIteratorW __wbegin
);
4414 template<typename _Func
>
4415 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
4417 template<typename _Func
>
4418 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4421 std::vector
<_RealType
>
4433 std::vector
<_RealType
> _M_int
;
4434 std::vector
<double> _M_den
;
4435 std::vector
<double> _M_cp
;
4436 std::vector
<double> _M_m
;
4440 piecewise_linear_distribution()
4444 template<typename _InputIteratorB
, typename _InputIteratorW
>
4445 piecewise_linear_distribution(_InputIteratorB __bfirst
,
4446 _InputIteratorB __bend
,
4447 _InputIteratorW __wbegin
)
4448 : _M_param(__bfirst
, __bend
, __wbegin
)
4451 template<typename _Func
>
4452 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
4454 : _M_param(__bl
, __fw
)
4457 template<typename _Func
>
4458 piecewise_linear_distribution(size_t __nw
,
4459 _RealType __xmin
, _RealType __xmax
,
4461 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4465 piecewise_linear_distribution(const param_type
& __p
)
4470 * Resets the distribution state.
4477 * @brief Return the intervals of the distribution.
4479 std::vector
<_RealType
>
4481 { return _M_param
.intervals(); }
4484 * @brief Return a vector of the probability densities of the
4489 { return _M_param
.densities(); }
4492 * @brief Returns the parameter set of the distribution.
4496 { return _M_param
; }
4499 * @brief Sets the parameter set of the distribution.
4500 * @param __param The new parameter set of the distribution.
4503 param(const param_type
& __param
)
4504 { _M_param
= __param
; }
4507 * @brief Returns the greatest lower bound value of the distribution.
4511 { return this->_M_param
._M_int
.front(); }
4514 * @brief Returns the least upper bound value of the distribution.
4518 { return this->_M_param
._M_int
.back(); }
4520 template<typename _UniformRandomNumberGenerator
>
4522 operator()(_UniformRandomNumberGenerator
& __urng
)
4523 { return this->operator()(__urng
, this->param()); }
4525 template<typename _UniformRandomNumberGenerator
>
4527 operator()(_UniformRandomNumberGenerator
& __urng
,
4528 const param_type
& __p
);
4531 * @brief Inserts a %piecewise_linear_distribution random number
4532 * distribution @p __x into the output stream @p __os.
4534 * @param __os An output stream.
4535 * @param __x A %piecewise_linear_distribution random number
4538 * @returns The output stream with the state of @p __x inserted or in
4541 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4542 friend std::basic_ostream
<_CharT
, _Traits
>&
4543 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4544 const std::piecewise_linear_distribution
<_RealType1
>&);
4547 * @brief Extracts a %piecewise_linear_distribution random number
4548 * distribution @p __x from the input stream @p __is.
4550 * @param __is An input stream.
4551 * @param __x A %piecewise_linear_distribution random number
4554 * @returns The input stream with @p __x extracted or in an error
4557 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4558 friend std::basic_istream
<_CharT
, _Traits
>&
4559 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4560 std::piecewise_linear_distribution
<_RealType1
>&);
4563 param_type _M_param
;
4567 /* @} */ // group std_random_distributions_poisson
4569 /* @} */ // group std_random_distributions
4572 * @addtogroup std_random_utilities Random Number Utilities
4573 * @ingroup std_random
4578 * @brief The seed_seq class generates sequences of seeds for random
4579 * number generators.
4585 /** The type of the seed vales. */
4586 typedef uint_least32_t result_type
;
4588 /** Default constructor. */
4593 template<typename _IntType
>
4594 seed_seq(std::initializer_list
<_IntType
> il
);
4596 template<typename _InputIterator
>
4597 seed_seq(_InputIterator __begin
, _InputIterator __end
);
4599 // generating functions
4600 template<typename _RandomAccessIterator
>
4602 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
4604 // property functions
4606 { return _M_v
.size(); }
4608 template<typename OutputIterator
>
4610 param(OutputIterator __dest
) const
4611 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
4615 std::vector
<result_type
> _M_v
;
4618 /* @} */ // group std_random_utilities
4620 /* @} */ // group std_random