1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010 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
);
54 * Implementation-space details.
58 template<typename _UIntType
, size_t __w
,
59 bool = __w
< static_cast<size_t>
60 (std::numeric_limits
<_UIntType
>::digits
)>
62 { static const _UIntType __value
= 0; };
64 template<typename _UIntType
, size_t __w
>
65 struct _Shift
<_UIntType
, __w
, true>
66 { static const _UIntType __value
= _UIntType(1) << __w
; };
68 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool>
71 // Dispatch based on modulus value to prevent divide-by-zero compile-time
72 // errors when m == 0.
73 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
76 { return _Mod
<_Tp
, __m
, __a
, __c
, __m
== 0>::__calc(__x
); }
79 * An adaptor class for converting the output of any Generator into
80 * the input for a specific Distribution.
82 template<typename _Engine
, typename _DInputType
>
87 _Adaptor(_Engine
& __g
)
92 { return _DInputType(0); }
96 { return _DInputType(1); }
99 * Converts a value generated by the adapted random number generator
100 * into a value in the input domain for the dependent random number
106 return std::generate_canonical
<_DInputType
,
107 std::numeric_limits
<_DInputType
>::digits
,
114 } // namespace __detail
117 * @addtogroup std_random_generators Random Number Generators
118 * @ingroup std_random
120 * These classes define objects which provide random or pseudorandom
121 * numbers, either from a discrete or a continuous interval. The
122 * random number generator supplied as a part of this library are
123 * all uniform random number generators which provide a sequence of
124 * random number uniformly distributed over their range.
126 * A number generator is a function object with an operator() that
127 * takes zero arguments and returns a number.
129 * A compliant random number generator must satisfy the following
130 * requirements. <table border=1 cellpadding=10 cellspacing=0>
131 * <caption align=top>Random Number Generator Requirements</caption>
132 * <tr><td>To be documented.</td></tr> </table>
138 * @brief A model of a linear congruential random number generator.
140 * A random number generator that produces pseudorandom numbers using the
141 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
143 * The template parameter @p _UIntType must be an unsigned integral type
144 * large enough to store values up to (__m-1). If the template parameter
145 * @p __m is 0, the modulus @p __m used is
146 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
147 * parameters @p __a and @p __c must be less than @p __m.
149 * The size of the state is @f$ 1 @f$.
151 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
152 class linear_congruential_engine
154 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
155 "substituting _UIntType not an unsigned integral type");
156 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
157 "template argument substituting __m out of bounds");
160 /** The type of the generated random value. */
161 typedef _UIntType result_type
;
163 /** The multiplier. */
164 static const result_type multiplier
= __a
;
166 static const result_type increment
= __c
;
168 static const result_type modulus
= __m
;
169 static const result_type default_seed
= 1u;
172 * @brief Constructs a %linear_congruential_engine random number
173 * generator engine with seed @p __s. The default seed value
176 * @param __s The initial seed value.
179 linear_congruential_engine(result_type __s
= default_seed
)
183 * @brief Constructs a %linear_congruential_engine random number
184 * generator engine seeded from the seed sequence @p __q.
186 * @param __q the seed sequence.
188 template<typename _Sseq
, typename
189 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
191 linear_congruential_engine(_Sseq
& __q
)
192 { seed
<_Sseq
>(__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.
210 template<typename _Sseq
, typename
211 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
216 * @brief Gets the smallest possible value in the output range.
218 * The minimum depends on the @p __c parameter: if it is zero, the
219 * minimum generated must be > 0, otherwise 0 is allowed.
221 * @todo This should be constexpr.
225 { return __c
== 0u ? 1u : 0u; }
228 * @brief Gets the largest possible value in the output range.
230 * @todo This should be constexpr.
237 * @brief Discard a sequence of random numbers.
239 * @todo Look for a faster way to do discard.
242 discard(unsigned long long __z
)
244 for (; __z
!= 0ULL; --__z
)
249 * @brief Gets the next random number in the sequence.
254 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
259 * @brief Compares two linear congruential random number generator
260 * objects of the same type for equality.
262 * @param __lhs A linear congruential random number generator object.
263 * @param __rhs Another linear congruential random number generator
266 * @returns true if the two objects are equal, false otherwise.
269 operator==(const linear_congruential_engine
& __lhs
,
270 const linear_congruential_engine
& __rhs
)
271 { return __lhs
._M_x
== __rhs
._M_x
; }
274 * @brief Writes the textual representation of the state x(i) of x to
277 * @param __os The output stream.
278 * @param __lcr A % linear_congruential_engine random number generator.
281 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
282 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
283 friend std::basic_ostream
<_CharT
, _Traits
>&
284 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
285 const std::linear_congruential_engine
<_UIntType1
,
289 * @brief Sets the state of the engine by reading its textual
290 * representation from @p __is.
292 * The textual representation must have been previously written using
293 * an output stream whose imbued locale and whose type's template
294 * specialization arguments _CharT and _Traits were the same as those
297 * @param __is The input stream.
298 * @param __lcr A % linear_congruential_engine random number generator.
301 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
302 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
303 friend std::basic_istream
<_CharT
, _Traits
>&
304 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
305 std::linear_congruential_engine
<_UIntType1
, __a1
,
314 * A generalized feedback shift register discrete random number generator.
316 * This algorithm avoids multiplication and division and is designed to be
317 * friendly to a pipelined architecture. If the parameters are chosen
318 * correctly, this generator will produce numbers with a very long period and
319 * fairly good apparent entropy, although still not cryptographically strong.
321 * The best way to use this generator is with the predefined mt19937 class.
323 * This algorithm was originally invented by Makoto Matsumoto and
326 * @var word_size The number of bits in each element of the state vector.
327 * @var state_size The degree of recursion.
328 * @var shift_size The period parameter.
329 * @var mask_bits The separation point bit index.
330 * @var parameter_a The last row of the twist matrix.
331 * @var output_u The first right-shift tempering matrix parameter.
332 * @var output_s The first left-shift tempering matrix parameter.
333 * @var output_b The first left-shift tempering matrix mask.
334 * @var output_t The second left-shift tempering matrix parameter.
335 * @var output_c The second left-shift tempering matrix mask.
336 * @var output_l The second right-shift tempering matrix parameter.
338 template<typename _UIntType
, size_t __w
,
339 size_t __n
, size_t __m
, size_t __r
,
340 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
341 _UIntType __b
, size_t __t
,
342 _UIntType __c
, size_t __l
, _UIntType __f
>
343 class mersenne_twister_engine
345 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
346 "substituting _UIntType not an unsigned integral type");
347 static_assert(1u <= __m
&& __m
<= __n
,
348 "template argument substituting __m out of bounds");
349 static_assert(__r
<= __w
, "template argument substituting "
351 static_assert(__u
<= __w
, "template argument substituting "
353 static_assert(__s
<= __w
, "template argument substituting "
355 static_assert(__t
<= __w
, "template argument substituting "
357 static_assert(__l
<= __w
, "template argument substituting "
359 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
360 "template argument substituting __w out of bound");
361 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
362 "template argument substituting __a out of bound");
363 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
364 "template argument substituting __b out of bound");
365 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
366 "template argument substituting __c out of bound");
367 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
368 "template argument substituting __d out of bound");
369 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
370 "template argument substituting __f out of bound");
373 /** The type of the generated random value. */
374 typedef _UIntType result_type
;
377 static const size_t word_size
= __w
;
378 static const size_t state_size
= __n
;
379 static const size_t shift_size
= __m
;
380 static const size_t mask_bits
= __r
;
381 static const result_type xor_mask
= __a
;
382 static const size_t tempering_u
= __u
;
383 static const result_type tempering_d
= __d
;
384 static const size_t tempering_s
= __s
;
385 static const result_type tempering_b
= __b
;
386 static const size_t tempering_t
= __t
;
387 static const result_type tempering_c
= __c
;
388 static const size_t tempering_l
= __l
;
389 static const result_type initialization_multiplier
= __f
;
390 static const result_type default_seed
= 5489u;
392 // constructors and member function
394 mersenne_twister_engine(result_type __sd
= default_seed
)
398 * @brief Constructs a %mersenne_twister_engine random number generator
399 * engine seeded from the seed sequence @p __q.
401 * @param __q the seed sequence.
403 template<typename _Sseq
, typename
404 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
406 mersenne_twister_engine(_Sseq
& __q
)
407 { seed
<_Sseq
>(__q
); }
410 seed(result_type __sd
= default_seed
);
412 template<typename _Sseq
, typename
413 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
418 * @brief Gets the smallest possible value in the output range.
420 * @todo This should be constexpr.
427 * @brief Gets the largest possible value in the output range.
429 * @todo This should be constexpr.
433 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
436 * @brief Discard a sequence of random numbers.
438 * @todo Look for a faster way to do discard.
441 discard(unsigned long long __z
)
443 for (; __z
!= 0ULL; --__z
)
451 * @brief Compares two % mersenne_twister_engine random number generator
452 * objects of the same type for equality.
454 * @param __lhs A % mersenne_twister_engine random number generator
456 * @param __rhs Another % mersenne_twister_engine random number
459 * @returns true if the two objects are equal, false otherwise.
462 operator==(const mersenne_twister_engine
& __lhs
,
463 const mersenne_twister_engine
& __rhs
)
464 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
467 * @brief Inserts the current state of a % mersenne_twister_engine
468 * random number generator engine @p __x into the output stream
471 * @param __os An output stream.
472 * @param __x A % mersenne_twister_engine random number generator
475 * @returns The output stream with the state of @p __x inserted or in
478 template<typename _UIntType1
,
479 size_t __w1
, size_t __n1
,
480 size_t __m1
, size_t __r1
,
481 _UIntType1 __a1
, size_t __u1
,
482 _UIntType1 __d1
, size_t __s1
,
483 _UIntType1 __b1
, size_t __t1
,
484 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
485 typename _CharT
, typename _Traits
>
486 friend std::basic_ostream
<_CharT
, _Traits
>&
487 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
488 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
489 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
493 * @brief Extracts the current state of a % mersenne_twister_engine
494 * random number generator engine @p __x from the input stream
497 * @param __is An input stream.
498 * @param __x A % mersenne_twister_engine random number generator
501 * @returns The input stream with the state of @p __x extracted or in
504 template<typename _UIntType1
,
505 size_t __w1
, size_t __n1
,
506 size_t __m1
, size_t __r1
,
507 _UIntType1 __a1
, size_t __u1
,
508 _UIntType1 __d1
, size_t __s1
,
509 _UIntType1 __b1
, size_t __t1
,
510 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
511 typename _CharT
, typename _Traits
>
512 friend std::basic_istream
<_CharT
, _Traits
>&
513 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
514 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
515 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
519 _UIntType _M_x
[state_size
];
524 * @brief The Marsaglia-Zaman generator.
526 * This is a model of a Generalized Fibonacci discrete random number
527 * generator, sometimes referred to as the SWC generator.
529 * A discrete random number generator that produces pseudorandom
530 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
531 * carry_{i-1}) \bmod m @f$.
533 * The size of the state is @f$ r @f$
534 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
536 * @var _M_x The state of the generator. This is a ring buffer.
537 * @var _M_carry The carry.
538 * @var _M_p Current index of x(i - r).
540 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
541 class subtract_with_carry_engine
543 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
544 "substituting _UIntType not an unsigned integral type");
545 static_assert(0u < __s
&& __s
< __r
,
546 "template argument substituting __s out of bounds");
547 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
548 "template argument substituting __w out of bounds");
551 /** The type of the generated random value. */
552 typedef _UIntType result_type
;
555 static const size_t word_size
= __w
;
556 static const size_t short_lag
= __s
;
557 static const size_t long_lag
= __r
;
558 static const result_type default_seed
= 19780503u;
561 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
562 * random number generator.
565 subtract_with_carry_engine(result_type __sd
= default_seed
)
569 * @brief Constructs a %subtract_with_carry_engine random number engine
570 * seeded from the seed sequence @p __q.
572 * @param __q the seed sequence.
574 template<typename _Sseq
, typename
575 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
577 subtract_with_carry_engine(_Sseq
& __q
)
578 { seed
<_Sseq
>(__q
); }
581 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
584 * N1688[4.19] modifies this as follows. If @p __value == 0,
585 * sets value to 19780503. In any case, with a linear
586 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
587 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
588 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
589 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
590 * set carry to 1, otherwise sets carry to 0.
593 seed(result_type __sd
= default_seed
);
596 * @brief Seeds the initial state @f$ x_0 @f$ of the
597 * % subtract_with_carry_engine random number generator.
599 template<typename _Sseq
, typename
600 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
605 * @brief Gets the inclusive minimum value of the range of random
606 * integers returned by this generator.
608 * @todo This should be constexpr.
615 * @brief Gets the inclusive maximum value of the range of random
616 * integers returned by this generator.
618 * @todo This should be constexpr.
622 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
625 * @brief Discard a sequence of random numbers.
627 * @todo Look for a faster way to do discard.
630 discard(unsigned long long __z
)
632 for (; __z
!= 0ULL; --__z
)
637 * @brief Gets the next random number in the sequence.
643 * @brief Compares two % subtract_with_carry_engine random number
644 * generator objects of the same type for equality.
646 * @param __lhs A % subtract_with_carry_engine random number generator
648 * @param __rhs Another % subtract_with_carry_engine random number
651 * @returns true if the two objects are equal, false otherwise.
654 operator==(const subtract_with_carry_engine
& __lhs
,
655 const subtract_with_carry_engine
& __rhs
)
656 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
659 * @brief Inserts the current state of a % subtract_with_carry_engine
660 * random number generator engine @p __x into the output stream
663 * @param __os An output stream.
664 * @param __x A % subtract_with_carry_engine random number generator
667 * @returns The output stream with the state of @p __x inserted or in
670 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
671 typename _CharT
, typename _Traits
>
672 friend std::basic_ostream
<_CharT
, _Traits
>&
673 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
674 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
678 * @brief Extracts the current state of a % subtract_with_carry_engine
679 * random number generator engine @p __x from the input stream
682 * @param __is An input stream.
683 * @param __x A % subtract_with_carry_engine random number generator
686 * @returns The input stream with the state of @p __x extracted or in
689 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
690 typename _CharT
, typename _Traits
>
691 friend std::basic_istream
<_CharT
, _Traits
>&
692 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
693 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
697 _UIntType _M_x
[long_lag
];
703 * Produces random numbers from some base engine by discarding blocks of
706 * 0 <= @p __r <= @p __p
708 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
709 class discard_block_engine
711 static_assert(1 <= __r
&& __r
<= __p
,
712 "template argument substituting __r out of bounds");
715 /** The type of the generated random value. */
716 typedef typename
_RandomNumberEngine::result_type result_type
;
719 static const size_t block_size
= __p
;
720 static const size_t used_block
= __r
;
723 * @brief Constructs a default %discard_block_engine engine.
725 * The underlying engine is default constructed as well.
727 discard_block_engine()
728 : _M_b(), _M_n(0) { }
731 * @brief Copy constructs a %discard_block_engine engine.
733 * Copies an existing base class random number generator.
734 * @param rng An existing (base class) engine object.
737 discard_block_engine(const _RandomNumberEngine
& __rne
)
738 : _M_b(__rne
), _M_n(0) { }
741 * @brief Move constructs a %discard_block_engine engine.
743 * Copies an existing base class random number generator.
744 * @param rng An existing (base class) engine object.
747 discard_block_engine(_RandomNumberEngine
&& __rne
)
748 : _M_b(std::move(__rne
)), _M_n(0) { }
751 * @brief Seed constructs a %discard_block_engine engine.
753 * Constructs the underlying generator engine seeded with @p __s.
754 * @param __s A seed value for the base class engine.
757 discard_block_engine(result_type __s
)
758 : _M_b(__s
), _M_n(0) { }
761 * @brief Generator construct a %discard_block_engine engine.
763 * @param __q A seed sequence.
765 template<typename _Sseq
, typename
766 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
767 && !std::is_same
<_Sseq
, _RandomNumberEngine
>
770 discard_block_engine(_Sseq
& __q
)
775 * @brief Reseeds the %discard_block_engine object with the default
776 * seed for the underlying base class generator engine.
786 * @brief Reseeds the %discard_block_engine object with the default
787 * seed for the underlying base class generator engine.
790 seed(result_type __s
)
797 * @brief Reseeds the %discard_block_engine object with the given seed
799 * @param __q A seed generator function.
801 template<typename _Sseq
, typename
802 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
806 _M_b
.seed
<_Sseq
>(__q
);
811 * @brief Gets a const reference to the underlying generator engine
814 const _RandomNumberEngine
&
819 * @brief Gets the minimum value in the generated random number range.
821 * @todo This should be constexpr.
825 { return _M_b
.min(); }
828 * @brief Gets the maximum value in the generated random number range.
830 * @todo This should be constexpr.
834 { return _M_b
.max(); }
837 * @brief Discard a sequence of random numbers.
839 * @todo Look for a faster way to do discard.
842 discard(unsigned long long __z
)
844 for (; __z
!= 0ULL; --__z
)
849 * @brief Gets the next value in the generated random number sequence.
855 * @brief Compares two %discard_block_engine random number generator
856 * objects of the same type for equality.
858 * @param __lhs A %discard_block_engine random number generator object.
859 * @param __rhs Another %discard_block_engine random number generator
862 * @returns true if the two objects are equal, false otherwise.
865 operator==(const discard_block_engine
& __lhs
,
866 const discard_block_engine
& __rhs
)
867 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
870 * @brief Inserts the current state of a %discard_block_engine random
871 * number generator engine @p __x into the output stream
874 * @param __os An output stream.
875 * @param __x A %discard_block_engine random number generator engine.
877 * @returns The output stream with the state of @p __x inserted or in
880 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
881 typename _CharT
, typename _Traits
>
882 friend std::basic_ostream
<_CharT
, _Traits
>&
883 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
884 const std::discard_block_engine
<_RandomNumberEngine1
,
888 * @brief Extracts the current state of a % subtract_with_carry_engine
889 * random number generator engine @p __x from the input stream
892 * @param __is An input stream.
893 * @param __x A %discard_block_engine random number generator engine.
895 * @returns The input stream with the state of @p __x extracted or in
898 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
899 typename _CharT
, typename _Traits
>
900 friend std::basic_istream
<_CharT
, _Traits
>&
901 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
902 std::discard_block_engine
<_RandomNumberEngine1
,
906 _RandomNumberEngine _M_b
;
911 * Produces random numbers by combining random numbers from some base
912 * engine to produce random numbers with a specifies number of bits @p __w.
914 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
915 class independent_bits_engine
917 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
918 "substituting _UIntType not an unsigned integral type");
919 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
920 "template argument substituting __w out of bounds");
923 /** The type of the generated random value. */
924 typedef _UIntType result_type
;
927 * @brief Constructs a default %independent_bits_engine engine.
929 * The underlying engine is default constructed as well.
931 independent_bits_engine()
935 * @brief Copy constructs a %independent_bits_engine engine.
937 * Copies an existing base class random number generator.
938 * @param rng An existing (base class) engine object.
941 independent_bits_engine(const _RandomNumberEngine
& __rne
)
945 * @brief Move constructs a %independent_bits_engine engine.
947 * Copies an existing base class random number generator.
948 * @param rng An existing (base class) engine object.
951 independent_bits_engine(_RandomNumberEngine
&& __rne
)
952 : _M_b(std::move(__rne
)) { }
955 * @brief Seed constructs a %independent_bits_engine engine.
957 * Constructs the underlying generator engine seeded with @p __s.
958 * @param __s A seed value for the base class engine.
961 independent_bits_engine(result_type __s
)
965 * @brief Generator construct a %independent_bits_engine engine.
967 * @param __q A seed sequence.
969 template<typename _Sseq
, typename
970 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
971 && !std::is_same
<_Sseq
, _RandomNumberEngine
>
974 independent_bits_engine(_Sseq
& __q
)
979 * @brief Reseeds the %independent_bits_engine object with the default
980 * seed for the underlying base class generator engine.
987 * @brief Reseeds the %independent_bits_engine object with the default
988 * seed for the underlying base class generator engine.
991 seed(result_type __s
)
995 * @brief Reseeds the %independent_bits_engine object with the given
997 * @param __q A seed generator function.
999 template<typename _Sseq
, typename
1000 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
1003 { _M_b
.seed
<_Sseq
>(__q
); }
1006 * @brief Gets a const reference to the underlying generator engine
1009 const _RandomNumberEngine
&
1014 * @brief Gets the minimum value in the generated random number range.
1016 * @todo This should be constexpr.
1023 * @brief Gets the maximum value in the generated random number range.
1025 * @todo This should be constexpr.
1029 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1032 * @brief Discard a sequence of random numbers.
1034 * @todo Look for a faster way to do discard.
1037 discard(unsigned long long __z
)
1039 for (; __z
!= 0ULL; --__z
)
1044 * @brief Gets the next value in the generated random number sequence.
1050 * @brief Compares two %independent_bits_engine random number generator
1051 * objects of the same type for equality.
1053 * @param __lhs A %independent_bits_engine random number generator
1055 * @param __rhs Another %independent_bits_engine random number generator
1058 * @returns true if the two objects are equal, false otherwise.
1061 operator==(const independent_bits_engine
& __lhs
,
1062 const independent_bits_engine
& __rhs
)
1063 { return __lhs
._M_b
== __rhs
._M_b
; }
1066 * @brief Extracts the current state of a % subtract_with_carry_engine
1067 * random number generator engine @p __x from the input stream
1070 * @param __is An input stream.
1071 * @param __x A %independent_bits_engine random number generator
1074 * @returns The input stream with the state of @p __x extracted or in
1077 template<typename _CharT
, typename _Traits
>
1078 friend std::basic_istream
<_CharT
, _Traits
>&
1079 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1080 std::independent_bits_engine
<_RandomNumberEngine
,
1081 __w
, _UIntType
>& __x
)
1088 _RandomNumberEngine _M_b
;
1092 * @brief Inserts the current state of a %independent_bits_engine random
1093 * number generator engine @p __x into the output stream @p __os.
1095 * @param __os An output stream.
1096 * @param __x A %independent_bits_engine random number generator engine.
1098 * @returns The output stream with the state of @p __x inserted or in
1101 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1102 typename _CharT
, typename _Traits
>
1103 std::basic_ostream
<_CharT
, _Traits
>&
1104 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1105 const std::independent_bits_engine
<_RandomNumberEngine
,
1106 __w
, _UIntType
>& __x
)
1113 * @brief Produces random numbers by combining random numbers from some
1114 * base engine to produce random numbers with a specifies number of bits
1117 template<typename _RandomNumberEngine
, size_t __k
>
1118 class shuffle_order_engine
1120 static_assert(1u <= __k
, "template argument substituting "
1121 "__k out of bound");
1124 /** The type of the generated random value. */
1125 typedef typename
_RandomNumberEngine::result_type result_type
;
1127 static const size_t table_size
= __k
;
1130 * @brief Constructs a default %shuffle_order_engine engine.
1132 * The underlying engine is default constructed as well.
1134 shuffle_order_engine()
1136 { _M_initialize(); }
1139 * @brief Copy constructs a %shuffle_order_engine engine.
1141 * Copies an existing base class random number generator.
1142 * @param rng An existing (base class) engine object.
1145 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1147 { _M_initialize(); }
1150 * @brief Move constructs a %shuffle_order_engine engine.
1152 * Copies an existing base class random number generator.
1153 * @param rng An existing (base class) engine object.
1156 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1157 : _M_b(std::move(__rne
))
1158 { _M_initialize(); }
1161 * @brief Seed constructs a %shuffle_order_engine engine.
1163 * Constructs the underlying generator engine seeded with @p __s.
1164 * @param __s A seed value for the base class engine.
1167 shuffle_order_engine(result_type __s
)
1169 { _M_initialize(); }
1172 * @brief Generator construct a %shuffle_order_engine engine.
1174 * @param __q A seed sequence.
1176 template<typename _Sseq
, typename
1177 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
1178 && !std::is_same
<_Sseq
, _RandomNumberEngine
>
1181 shuffle_order_engine(_Sseq
& __q
)
1183 { _M_initialize(); }
1186 * @brief Reseeds the %shuffle_order_engine object with the default seed
1187 for the underlying base class generator engine.
1197 * @brief Reseeds the %shuffle_order_engine object with the default seed
1198 * for the underlying base class generator engine.
1201 seed(result_type __s
)
1208 * @brief Reseeds the %shuffle_order_engine object with the given seed
1210 * @param __q A seed generator function.
1212 template<typename _Sseq
, typename
1213 = typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
>
1217 _M_b
.seed
<_Sseq
>(__q
);
1222 * Gets a const reference to the underlying generator engine object.
1224 const _RandomNumberEngine
&
1229 * Gets the minimum value in the generated random number range.
1231 * @todo This should be constexpr.
1235 { return _M_b
.min(); }
1238 * Gets the maximum value in the generated random number range.
1240 * @todo This should be constexpr.
1244 { return _M_b
.max(); }
1247 * Discard a sequence of random numbers.
1249 * @todo Look for a faster way to do discard.
1252 discard(unsigned long long __z
)
1254 for (; __z
!= 0ULL; --__z
)
1259 * Gets the next value in the generated random number sequence.
1265 * Compares two %shuffle_order_engine random number generator objects
1266 * of the same type for equality.
1268 * @param __lhs A %shuffle_order_engine random number generator object.
1269 * @param __rhs Another %shuffle_order_engine random number generator
1272 * @returns true if the two objects are equal, false otherwise.
1275 operator==(const shuffle_order_engine
& __lhs
,
1276 const shuffle_order_engine
& __rhs
)
1277 { return __lhs
._M_b
== __rhs
._M_b
; }
1280 * @brief Inserts the current state of a %shuffle_order_engine random
1281 * number generator engine @p __x into the output stream
1284 * @param __os An output stream.
1285 * @param __x A %shuffle_order_engine random number generator engine.
1287 * @returns The output stream with the state of @p __x inserted or in
1290 template<typename _RandomNumberEngine1
, size_t __k1
,
1291 typename _CharT
, typename _Traits
>
1292 friend std::basic_ostream
<_CharT
, _Traits
>&
1293 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1294 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1298 * @brief Extracts the current state of a % subtract_with_carry_engine
1299 * random number generator engine @p __x from the input stream
1302 * @param __is An input stream.
1303 * @param __x A %shuffle_order_engine random number generator engine.
1305 * @returns The input stream with the state of @p __x extracted or in
1308 template<typename _RandomNumberEngine1
, size_t __k1
,
1309 typename _CharT
, typename _Traits
>
1310 friend std::basic_istream
<_CharT
, _Traits
>&
1311 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1312 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1315 void _M_initialize()
1317 for (size_t __i
= 0; __i
< __k
; ++__i
)
1322 _RandomNumberEngine _M_b
;
1323 result_type _M_v
[__k
];
1328 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1330 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1334 * An alternative LCR (Lehmer Generator function) .
1336 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1340 * The classic Mersenne Twister.
1343 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1344 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1345 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1347 typedef mersenne_twister_engine
<
1353 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1356 * An alternative Mersenne Twister.
1358 typedef mersenne_twister_engine
<
1361 0xb5026f5aa96619e9ULL
, 29,
1362 0x5555555555555555ULL
, 17,
1363 0x71d67fffeda60000ULL
, 37,
1364 0xfff7eee000000000ULL
, 43,
1365 6364136223846793005ULL> mt19937_64
;
1370 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1373 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1376 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1378 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1383 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1388 typedef minstd_rand0 default_random_engine
;
1391 * A standard interface to a platform-specific non-deterministic
1392 * random number generator (if any are available).
1397 /** The type of the generated random value. */
1398 typedef unsigned int result_type
;
1400 // constructors, destructors and member functions
1402 #ifdef _GLIBCXX_USE_RANDOM_TR1
1405 random_device(const std::string
& __token
= "/dev/urandom")
1407 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1408 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1409 std::__throw_runtime_error(__N("random_device::"
1410 "random_device(const std::string&)"));
1414 { std::fclose(_M_file
); }
1419 random_device(const std::string
& __token
= "mt19937")
1420 : _M_mt(_M_strtoul(__token
)) { }
1423 static unsigned long
1424 _M_strtoul(const std::string
& __str
)
1426 unsigned long __ret
= 5489UL;
1427 if (__str
!= "mt19937")
1429 const char* __nptr
= __str
.c_str();
1431 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1432 if (*__nptr
== '\0' || *__endptr
!= '\0')
1433 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1434 "(const std::string&)"));
1445 { return std::numeric_limits
<result_type
>::min(); }
1449 { return std::numeric_limits
<result_type
>::max(); }
1458 #ifdef _GLIBCXX_USE_RANDOM_TR1
1460 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1468 // No copy functions.
1469 random_device(const random_device
&) = delete;
1470 void operator=(const random_device
&) = delete;
1474 #ifdef _GLIBCXX_USE_RANDOM_TR1
1481 /* @} */ // group std_random_generators
1484 * @addtogroup std_random_distributions Random Number Distributions
1485 * @ingroup std_random
1490 * @addtogroup std_random_distributions_uniform Uniform Distributions
1491 * @ingroup std_random_distributions
1496 * @brief Uniform discrete distribution for random numbers.
1497 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1498 * probability throughout the range.
1500 template<typename _IntType
= int>
1501 class uniform_int_distribution
1503 static_assert(std::is_integral
<_IntType
>::value
,
1504 "template argument not an integral type");
1507 /** The type of the range of the distribution. */
1508 typedef _IntType result_type
;
1509 /** Parameter type. */
1512 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1515 param_type(_IntType __a
= 0,
1516 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1517 : _M_a(__a
), _M_b(__b
)
1519 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1537 * @brief Constructs a uniform distribution object.
1540 uniform_int_distribution(_IntType __a
= 0,
1541 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1542 : _M_param(__a
, __b
)
1546 uniform_int_distribution(const param_type
& __p
)
1551 * @brief Resets the distribution state.
1553 * Does nothing for the uniform integer distribution.
1560 { return _M_param
.a(); }
1564 { return _M_param
.b(); }
1567 * @brief Returns the inclusive lower bound of the distribution range.
1571 { return this->a(); }
1574 * @brief Returns the inclusive upper bound of the distribution range.
1578 { return this->b(); }
1581 * @brief Returns the parameter set of the distribution.
1585 { return _M_param
; }
1588 * @brief Sets the parameter set of the distribution.
1589 * @param __param The new parameter set of the distribution.
1592 param(const param_type
& __param
)
1593 { _M_param
= __param
; }
1596 * Gets a uniformly distributed random number in the range
1599 template<typename _UniformRandomNumberGenerator
>
1601 operator()(_UniformRandomNumberGenerator
& __urng
)
1602 { return this->operator()(__urng
, this->param()); }
1605 * Gets a uniform random number in the range @f$[0, n)@f$.
1607 * This function is aimed at use with std::random_shuffle.
1609 template<typename _UniformRandomNumberGenerator
>
1611 operator()(_UniformRandomNumberGenerator
& __urng
,
1612 const param_type
& __p
);
1614 param_type _M_param
;
1618 * @brief Inserts a %uniform_int_distribution random number
1619 * distribution @p __x into the output stream @p os.
1621 * @param __os An output stream.
1622 * @param __x A %uniform_int_distribution random number distribution.
1624 * @returns The output stream with the state of @p __x inserted or in
1627 template<typename _IntType
, typename _CharT
, typename _Traits
>
1628 std::basic_ostream
<_CharT
, _Traits
>&
1629 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1630 const std::uniform_int_distribution
<_IntType
>&);
1633 * @brief Extracts a %uniform_int_distribution random number distribution
1634 * @p __x from the input stream @p __is.
1636 * @param __is An input stream.
1637 * @param __x A %uniform_int_distribution random number generator engine.
1639 * @returns The input stream with @p __x extracted or in an error state.
1641 template<typename _IntType
, typename _CharT
, typename _Traits
>
1642 std::basic_istream
<_CharT
, _Traits
>&
1643 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1644 std::uniform_int_distribution
<_IntType
>&);
1648 * @brief Uniform continuous distribution for random numbers.
1650 * A continuous random distribution on the range [min, max) with equal
1651 * probability throughout the range. The URNG should be real-valued and
1652 * deliver number in the range [0, 1).
1654 template<typename _RealType
= double>
1655 class uniform_real_distribution
1657 static_assert(std::is_floating_point
<_RealType
>::value
,
1658 "template argument not a floating point type");
1661 /** The type of the range of the distribution. */
1662 typedef _RealType result_type
;
1663 /** Parameter type. */
1666 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1669 param_type(_RealType __a
= _RealType(0),
1670 _RealType __b
= _RealType(1))
1671 : _M_a(__a
), _M_b(__b
)
1673 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1691 * @brief Constructs a uniform_real_distribution object.
1693 * @param __min [IN] The lower bound of the distribution.
1694 * @param __max [IN] The upper bound of the distribution.
1697 uniform_real_distribution(_RealType __a
= _RealType(0),
1698 _RealType __b
= _RealType(1))
1699 : _M_param(__a
, __b
)
1703 uniform_real_distribution(const param_type
& __p
)
1708 * @brief Resets the distribution state.
1710 * Does nothing for the uniform real distribution.
1717 { return _M_param
.a(); }
1721 { return _M_param
.b(); }
1724 * @brief Returns the inclusive lower bound of the distribution range.
1728 { return this->a(); }
1731 * @brief Returns the inclusive upper bound of the distribution range.
1735 { return this->b(); }
1738 * @brief Returns the parameter set of the distribution.
1742 { return _M_param
; }
1745 * @brief Sets the parameter set of the distribution.
1746 * @param __param The new parameter set of the distribution.
1749 param(const param_type
& __param
)
1750 { _M_param
= __param
; }
1752 template<typename _UniformRandomNumberGenerator
>
1754 operator()(_UniformRandomNumberGenerator
& __urng
)
1755 { return this->operator()(__urng
, this->param()); }
1757 template<typename _UniformRandomNumberGenerator
>
1759 operator()(_UniformRandomNumberGenerator
& __urng
,
1760 const param_type
& __p
)
1762 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1764 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1768 param_type _M_param
;
1772 * @brief Inserts a %uniform_real_distribution random number
1773 * distribution @p __x into the output stream @p __os.
1775 * @param __os An output stream.
1776 * @param __x A %uniform_real_distribution random number distribution.
1778 * @returns The output stream with the state of @p __x inserted or in
1781 template<typename _RealType
, typename _CharT
, typename _Traits
>
1782 std::basic_ostream
<_CharT
, _Traits
>&
1783 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1784 const std::uniform_real_distribution
<_RealType
>&);
1787 * @brief Extracts a %uniform_real_distribution random number distribution
1788 * @p __x from the input stream @p __is.
1790 * @param __is An input stream.
1791 * @param __x A %uniform_real_distribution random number generator engine.
1793 * @returns The input stream with @p __x extracted or in an error state.
1795 template<typename _RealType
, typename _CharT
, typename _Traits
>
1796 std::basic_istream
<_CharT
, _Traits
>&
1797 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1798 std::uniform_real_distribution
<_RealType
>&);
1800 /* @} */ // group std_random_distributions_uniform
1803 * @addtogroup std_random_distributions_normal Normal Distributions
1804 * @ingroup std_random_distributions
1809 * @brief A normal continuous distribution for random numbers.
1811 * The formula for the normal probability density function is
1812 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1813 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1815 template<typename _RealType
= double>
1816 class normal_distribution
1818 static_assert(std::is_floating_point
<_RealType
>::value
,
1819 "template argument not a floating point type");
1822 /** The type of the range of the distribution. */
1823 typedef _RealType result_type
;
1824 /** Parameter type. */
1827 typedef normal_distribution
<_RealType
> distribution_type
;
1830 param_type(_RealType __mean
= _RealType(0),
1831 _RealType __stddev
= _RealType(1))
1832 : _M_mean(__mean
), _M_stddev(__stddev
)
1834 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1843 { return _M_stddev
; }
1847 _RealType _M_stddev
;
1852 * Constructs a normal distribution with parameters @f$ mean @f$ and
1853 * standard deviation.
1856 normal_distribution(result_type __mean
= result_type(0),
1857 result_type __stddev
= result_type(1))
1858 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1862 normal_distribution(const param_type
& __p
)
1863 : _M_param(__p
), _M_saved_available(false)
1867 * @brief Resets the distribution state.
1871 { _M_saved_available
= false; }
1874 * @brief Returns the mean of the distribution.
1878 { return _M_param
.mean(); }
1881 * @brief Returns the standard deviation of the distribution.
1885 { return _M_param
.stddev(); }
1888 * @brief Returns the parameter set of the distribution.
1892 { return _M_param
; }
1895 * @brief Sets the parameter set of the distribution.
1896 * @param __param The new parameter set of the distribution.
1899 param(const param_type
& __param
)
1900 { _M_param
= __param
; }
1903 * @brief Returns the greatest lower bound value of the distribution.
1907 { return std::numeric_limits
<result_type
>::min(); }
1910 * @brief Returns the least upper bound value of the distribution.
1914 { return std::numeric_limits
<result_type
>::max(); }
1916 template<typename _UniformRandomNumberGenerator
>
1918 operator()(_UniformRandomNumberGenerator
& __urng
)
1919 { return this->operator()(__urng
, this->param()); }
1921 template<typename _UniformRandomNumberGenerator
>
1923 operator()(_UniformRandomNumberGenerator
& __urng
,
1924 const param_type
& __p
);
1927 * @brief Inserts a %normal_distribution random number distribution
1928 * @p __x into the output stream @p __os.
1930 * @param __os An output stream.
1931 * @param __x A %normal_distribution random number distribution.
1933 * @returns The output stream with the state of @p __x inserted or in
1936 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1937 friend std::basic_ostream
<_CharT
, _Traits
>&
1938 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1939 const std::normal_distribution
<_RealType1
>&);
1942 * @brief Extracts a %normal_distribution random number distribution
1943 * @p __x from the input stream @p __is.
1945 * @param __is An input stream.
1946 * @param __x A %normal_distribution random number generator engine.
1948 * @returns The input stream with @p __x extracted or in an error
1951 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1952 friend std::basic_istream
<_CharT
, _Traits
>&
1953 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1954 std::normal_distribution
<_RealType1
>&);
1957 param_type _M_param
;
1958 result_type _M_saved
;
1959 bool _M_saved_available
;
1964 * @brief A lognormal_distribution random number distribution.
1966 * The formula for the normal probability mass function is
1967 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
1968 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
1970 template<typename _RealType
= double>
1971 class lognormal_distribution
1973 static_assert(std::is_floating_point
<_RealType
>::value
,
1974 "template argument not a floating point type");
1977 /** The type of the range of the distribution. */
1978 typedef _RealType result_type
;
1979 /** Parameter type. */
1982 typedef lognormal_distribution
<_RealType
> distribution_type
;
1985 param_type(_RealType __m
= _RealType(0),
1986 _RealType __s
= _RealType(1))
1987 : _M_m(__m
), _M_s(__s
)
2004 lognormal_distribution(_RealType __m
= _RealType(0),
2005 _RealType __s
= _RealType(1))
2006 : _M_param(__m
, __s
), _M_nd()
2010 lognormal_distribution(const param_type
& __p
)
2011 : _M_param(__p
), _M_nd()
2015 * Resets the distribution state.
2026 { return _M_param
.m(); }
2030 { return _M_param
.s(); }
2033 * @brief Returns the parameter set of the distribution.
2037 { return _M_param
; }
2040 * @brief Sets the parameter set of the distribution.
2041 * @param __param The new parameter set of the distribution.
2044 param(const param_type
& __param
)
2045 { _M_param
= __param
; }
2048 * @brief Returns the greatest lower bound value of the distribution.
2052 { return result_type(0); }
2055 * @brief Returns the least upper bound value of the distribution.
2059 { return std::numeric_limits
<result_type
>::max(); }
2061 template<typename _UniformRandomNumberGenerator
>
2063 operator()(_UniformRandomNumberGenerator
& __urng
)
2064 { return this->operator()(__urng
, this->param()); }
2066 template<typename _UniformRandomNumberGenerator
>
2068 operator()(_UniformRandomNumberGenerator
& __urng
,
2069 const param_type
& __p
)
2070 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2073 * @brief Inserts a %lognormal_distribution random number distribution
2074 * @p __x into the output stream @p __os.
2076 * @param __os An output stream.
2077 * @param __x A %lognormal_distribution random number distribution.
2079 * @returns The output stream with the state of @p __x inserted or in
2082 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2083 friend std::basic_ostream
<_CharT
, _Traits
>&
2084 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2085 const std::lognormal_distribution
<_RealType1
>&);
2088 * @brief Extracts a %lognormal_distribution random number distribution
2089 * @p __x from the input stream @p __is.
2091 * @param __is An input stream.
2092 * @param __x A %lognormal_distribution random number
2095 * @returns The input stream with @p __x extracted or in an error state.
2097 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2098 friend std::basic_istream
<_CharT
, _Traits
>&
2099 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2100 std::lognormal_distribution
<_RealType1
>&);
2103 param_type _M_param
;
2105 std::normal_distribution
<result_type
> _M_nd
;
2110 * @brief A gamma continuous distribution for random numbers.
2112 * The formula for the gamma probability density function is
2113 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2114 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
2116 template<typename _RealType
= double>
2117 class gamma_distribution
2119 static_assert(std::is_floating_point
<_RealType
>::value
,
2120 "template argument not a floating point type");
2123 /** The type of the range of the distribution. */
2124 typedef _RealType result_type
;
2125 /** Parameter type. */
2128 typedef gamma_distribution
<_RealType
> distribution_type
;
2129 friend class gamma_distribution
<_RealType
>;
2132 param_type(_RealType __alpha_val
= _RealType(1),
2133 _RealType __beta_val
= _RealType(1))
2134 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2136 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2142 { return _M_alpha
; }
2155 _RealType _M_malpha
, _M_a2
;
2160 * @brief Constructs a gamma distribution with parameters
2161 * @f$ \alpha @f$ and @f$ \beta @f$.
2164 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2165 _RealType __beta_val
= _RealType(1))
2166 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2170 gamma_distribution(const param_type
& __p
)
2171 : _M_param(__p
), _M_nd()
2175 * @brief Resets the distribution state.
2182 * @brief Returns the @f$ \alpha @f$ of the distribution.
2186 { return _M_param
.alpha(); }
2189 * @brief Returns the @f$ \beta @f$ of the distribution.
2193 { return _M_param
.beta(); }
2196 * @brief Returns the parameter set of the distribution.
2200 { return _M_param
; }
2203 * @brief Sets the parameter set of the distribution.
2204 * @param __param The new parameter set of the distribution.
2207 param(const param_type
& __param
)
2208 { _M_param
= __param
; }
2211 * @brief Returns the greatest lower bound value of the distribution.
2215 { return result_type(0); }
2218 * @brief Returns the least upper bound value of the distribution.
2222 { return std::numeric_limits
<result_type
>::max(); }
2224 template<typename _UniformRandomNumberGenerator
>
2226 operator()(_UniformRandomNumberGenerator
& __urng
)
2227 { return this->operator()(__urng
, this->param()); }
2229 template<typename _UniformRandomNumberGenerator
>
2231 operator()(_UniformRandomNumberGenerator
& __urng
,
2232 const param_type
& __p
);
2235 * @brief Inserts a %gamma_distribution random number distribution
2236 * @p __x into the output stream @p __os.
2238 * @param __os An output stream.
2239 * @param __x A %gamma_distribution random number distribution.
2241 * @returns The output stream with the state of @p __x inserted or in
2244 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2245 friend std::basic_ostream
<_CharT
, _Traits
>&
2246 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2247 const std::gamma_distribution
<_RealType1
>&);
2250 * @brief Extracts a %gamma_distribution random number distribution
2251 * @p __x from the input stream @p __is.
2253 * @param __is An input stream.
2254 * @param __x A %gamma_distribution random number generator engine.
2256 * @returns The input stream with @p __x extracted or in an error state.
2258 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2259 friend std::basic_istream
<_CharT
, _Traits
>&
2260 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2261 std::gamma_distribution
<_RealType1
>&);
2264 param_type _M_param
;
2266 std::normal_distribution
<result_type
> _M_nd
;
2271 * @brief A chi_squared_distribution random number distribution.
2273 * The formula for the normal probability mass function is
2274 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2276 template<typename _RealType
= double>
2277 class chi_squared_distribution
2279 static_assert(std::is_floating_point
<_RealType
>::value
,
2280 "template argument not a floating point type");
2283 /** The type of the range of the distribution. */
2284 typedef _RealType result_type
;
2285 /** Parameter type. */
2288 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2291 param_type(_RealType __n
= _RealType(1))
2304 chi_squared_distribution(_RealType __n
= _RealType(1))
2305 : _M_param(__n
), _M_gd(__n
/ 2)
2309 chi_squared_distribution(const param_type
& __p
)
2310 : _M_param(__p
), _M_gd(__p
.n() / 2)
2314 * @brief Resets the distribution state.
2325 { return _M_param
.n(); }
2328 * @brief Returns the parameter set of the distribution.
2332 { return _M_param
; }
2335 * @brief Sets the parameter set of the distribution.
2336 * @param __param The new parameter set of the distribution.
2339 param(const param_type
& __param
)
2340 { _M_param
= __param
; }
2343 * @brief Returns the greatest lower bound value of the distribution.
2347 { return result_type(0); }
2350 * @brief Returns the least upper bound value of the distribution.
2354 { return std::numeric_limits
<result_type
>::max(); }
2356 template<typename _UniformRandomNumberGenerator
>
2358 operator()(_UniformRandomNumberGenerator
& __urng
)
2359 { return 2 * _M_gd(__urng
); }
2361 template<typename _UniformRandomNumberGenerator
>
2363 operator()(_UniformRandomNumberGenerator
& __urng
,
2364 const param_type
& __p
)
2366 typedef typename
std::gamma_distribution
<result_type
>::param_type
2368 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2372 * @brief Inserts a %chi_squared_distribution random number distribution
2373 * @p __x into the output stream @p __os.
2375 * @param __os An output stream.
2376 * @param __x A %chi_squared_distribution random number distribution.
2378 * @returns The output stream with the state of @p __x inserted or in
2381 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2382 friend std::basic_ostream
<_CharT
, _Traits
>&
2383 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2384 const std::chi_squared_distribution
<_RealType1
>&);
2387 * @brief Extracts a %chi_squared_distribution random number distribution
2388 * @p __x from the input stream @p __is.
2390 * @param __is An input stream.
2391 * @param __x A %chi_squared_distribution random number
2394 * @returns The input stream with @p __x extracted or in an error state.
2396 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2397 friend std::basic_istream
<_CharT
, _Traits
>&
2398 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2399 std::chi_squared_distribution
<_RealType1
>&);
2402 param_type _M_param
;
2404 std::gamma_distribution
<result_type
> _M_gd
;
2409 * @brief A cauchy_distribution random number distribution.
2411 * The formula for the normal probability mass function is
2412 * @f$ p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1} @f$
2414 template<typename _RealType
= double>
2415 class cauchy_distribution
2417 static_assert(std::is_floating_point
<_RealType
>::value
,
2418 "template argument not a floating point type");
2421 /** The type of the range of the distribution. */
2422 typedef _RealType result_type
;
2423 /** Parameter type. */
2426 typedef cauchy_distribution
<_RealType
> distribution_type
;
2429 param_type(_RealType __a
= _RealType(0),
2430 _RealType __b
= _RealType(1))
2431 : _M_a(__a
), _M_b(__b
)
2448 cauchy_distribution(_RealType __a
= _RealType(0),
2449 _RealType __b
= _RealType(1))
2450 : _M_param(__a
, __b
)
2454 cauchy_distribution(const param_type
& __p
)
2459 * @brief Resets the distribution state.
2470 { return _M_param
.a(); }
2474 { return _M_param
.b(); }
2477 * @brief Returns the parameter set of the distribution.
2481 { return _M_param
; }
2484 * @brief Sets the parameter set of the distribution.
2485 * @param __param The new parameter set of the distribution.
2488 param(const param_type
& __param
)
2489 { _M_param
= __param
; }
2492 * @brief Returns the greatest lower bound value of the distribution.
2496 { return std::numeric_limits
<result_type
>::min(); }
2499 * @brief Returns the least upper bound value of the distribution.
2503 { return std::numeric_limits
<result_type
>::max(); }
2505 template<typename _UniformRandomNumberGenerator
>
2507 operator()(_UniformRandomNumberGenerator
& __urng
)
2508 { return this->operator()(__urng
, this->param()); }
2510 template<typename _UniformRandomNumberGenerator
>
2512 operator()(_UniformRandomNumberGenerator
& __urng
,
2513 const param_type
& __p
);
2516 param_type _M_param
;
2520 * @brief Inserts a %cauchy_distribution random number distribution
2521 * @p __x into the output stream @p __os.
2523 * @param __os An output stream.
2524 * @param __x A %cauchy_distribution random number distribution.
2526 * @returns The output stream with the state of @p __x inserted or in
2529 template<typename _RealType
, typename _CharT
, typename _Traits
>
2530 std::basic_ostream
<_CharT
, _Traits
>&
2531 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2532 const std::cauchy_distribution
<_RealType
>&);
2535 * @brief Extracts a %cauchy_distribution random number distribution
2536 * @p __x from the input stream @p __is.
2538 * @param __is An input stream.
2539 * @param __x A %cauchy_distribution random number
2542 * @returns The input stream with @p __x extracted or in an error state.
2544 template<typename _RealType
, typename _CharT
, typename _Traits
>
2545 std::basic_istream
<_CharT
, _Traits
>&
2546 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2547 std::cauchy_distribution
<_RealType
>&);
2551 * @brief A fisher_f_distribution random number distribution.
2553 * The formula for the normal probability mass function is
2554 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2555 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2556 * (1 + \frac{mx}{n})^{-(m+n)/2} @f$
2558 template<typename _RealType
= double>
2559 class fisher_f_distribution
2561 static_assert(std::is_floating_point
<_RealType
>::value
,
2562 "template argument not a floating point type");
2565 /** The type of the range of the distribution. */
2566 typedef _RealType result_type
;
2567 /** Parameter type. */
2570 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2573 param_type(_RealType __m
= _RealType(1),
2574 _RealType __n
= _RealType(1))
2575 : _M_m(__m
), _M_n(__n
)
2592 fisher_f_distribution(_RealType __m
= _RealType(1),
2593 _RealType __n
= _RealType(1))
2594 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2598 fisher_f_distribution(const param_type
& __p
)
2599 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2603 * @brief Resets the distribution state.
2617 { return _M_param
.m(); }
2621 { return _M_param
.n(); }
2624 * @brief Returns the parameter set of the distribution.
2628 { return _M_param
; }
2631 * @brief Sets the parameter set of the distribution.
2632 * @param __param The new parameter set of the distribution.
2635 param(const param_type
& __param
)
2636 { _M_param
= __param
; }
2639 * @brief Returns the greatest lower bound value of the distribution.
2643 { return result_type(0); }
2646 * @brief Returns the least upper bound value of the distribution.
2650 { return std::numeric_limits
<result_type
>::max(); }
2652 template<typename _UniformRandomNumberGenerator
>
2654 operator()(_UniformRandomNumberGenerator
& __urng
)
2655 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2657 template<typename _UniformRandomNumberGenerator
>
2659 operator()(_UniformRandomNumberGenerator
& __urng
,
2660 const param_type
& __p
)
2662 typedef typename
std::gamma_distribution
<result_type
>::param_type
2664 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2665 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2669 * @brief Inserts a %fisher_f_distribution random number distribution
2670 * @p __x into the output stream @p __os.
2672 * @param __os An output stream.
2673 * @param __x A %fisher_f_distribution random number distribution.
2675 * @returns The output stream with the state of @p __x inserted or in
2678 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2679 friend std::basic_ostream
<_CharT
, _Traits
>&
2680 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2681 const std::fisher_f_distribution
<_RealType1
>&);
2684 * @brief Extracts a %fisher_f_distribution random number distribution
2685 * @p __x from the input stream @p __is.
2687 * @param __is An input stream.
2688 * @param __x A %fisher_f_distribution random number
2691 * @returns The input stream with @p __x extracted or in an error state.
2693 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2694 friend std::basic_istream
<_CharT
, _Traits
>&
2695 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2696 std::fisher_f_distribution
<_RealType1
>&);
2699 param_type _M_param
;
2701 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
2706 * @brief A student_t_distribution random number distribution.
2708 * The formula for the normal probability mass function is
2709 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2710 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} @f$
2712 template<typename _RealType
= double>
2713 class student_t_distribution
2715 static_assert(std::is_floating_point
<_RealType
>::value
,
2716 "template argument not a floating point type");
2719 /** The type of the range of the distribution. */
2720 typedef _RealType result_type
;
2721 /** Parameter type. */
2724 typedef student_t_distribution
<_RealType
> distribution_type
;
2727 param_type(_RealType __n
= _RealType(1))
2740 student_t_distribution(_RealType __n
= _RealType(1))
2741 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
2745 student_t_distribution(const param_type
& __p
)
2746 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
2750 * @brief Resets the distribution state.
2764 { return _M_param
.n(); }
2767 * @brief Returns the parameter set of the distribution.
2771 { return _M_param
; }
2774 * @brief Sets the parameter set of the distribution.
2775 * @param __param The new parameter set of the distribution.
2778 param(const param_type
& __param
)
2779 { _M_param
= __param
; }
2782 * @brief Returns the greatest lower bound value of the distribution.
2786 { return std::numeric_limits
<result_type
>::min(); }
2789 * @brief Returns the least upper bound value of the distribution.
2793 { return std::numeric_limits
<result_type
>::max(); }
2795 template<typename _UniformRandomNumberGenerator
>
2797 operator()(_UniformRandomNumberGenerator
& __urng
)
2798 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
2800 template<typename _UniformRandomNumberGenerator
>
2802 operator()(_UniformRandomNumberGenerator
& __urng
,
2803 const param_type
& __p
)
2805 typedef typename
std::gamma_distribution
<result_type
>::param_type
2808 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
2809 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
2813 * @brief Inserts a %student_t_distribution random number distribution
2814 * @p __x into the output stream @p __os.
2816 * @param __os An output stream.
2817 * @param __x A %student_t_distribution random number distribution.
2819 * @returns The output stream with the state of @p __x inserted or in
2822 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2823 friend std::basic_ostream
<_CharT
, _Traits
>&
2824 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2825 const std::student_t_distribution
<_RealType1
>&);
2828 * @brief Extracts a %student_t_distribution random number distribution
2829 * @p __x from the input stream @p __is.
2831 * @param __is An input stream.
2832 * @param __x A %student_t_distribution random number
2835 * @returns The input stream with @p __x extracted or in an error state.
2837 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2838 friend std::basic_istream
<_CharT
, _Traits
>&
2839 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2840 std::student_t_distribution
<_RealType1
>&);
2843 param_type _M_param
;
2845 std::normal_distribution
<result_type
> _M_nd
;
2846 std::gamma_distribution
<result_type
> _M_gd
;
2849 /* @} */ // group std_random_distributions_normal
2852 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2853 * @ingroup std_random_distributions
2858 * @brief A Bernoulli random number distribution.
2860 * Generates a sequence of true and false values with likelihood @f$ p @f$
2861 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2863 class bernoulli_distribution
2866 /** The type of the range of the distribution. */
2867 typedef bool result_type
;
2868 /** Parameter type. */
2871 typedef bernoulli_distribution distribution_type
;
2874 param_type(double __p
= 0.5)
2877 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
2890 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2892 * @param __p [IN] The likelihood of a true result being returned.
2893 * Must be in the interval @f$ [0, 1] @f$.
2896 bernoulli_distribution(double __p
= 0.5)
2901 bernoulli_distribution(const param_type
& __p
)
2906 * @brief Resets the distribution state.
2908 * Does nothing for a Bernoulli distribution.
2914 * @brief Returns the @p p parameter of the distribution.
2918 { return _M_param
.p(); }
2921 * @brief Returns the parameter set of the distribution.
2925 { return _M_param
; }
2928 * @brief Sets the parameter set of the distribution.
2929 * @param __param The new parameter set of the distribution.
2932 param(const param_type
& __param
)
2933 { _M_param
= __param
; }
2936 * @brief Returns the greatest lower bound value of the distribution.
2940 { return std::numeric_limits
<result_type
>::min(); }
2943 * @brief Returns the least upper bound value of the distribution.
2947 { return std::numeric_limits
<result_type
>::max(); }
2950 * @brief Returns the next value in the Bernoullian sequence.
2952 template<typename _UniformRandomNumberGenerator
>
2954 operator()(_UniformRandomNumberGenerator
& __urng
)
2955 { return this->operator()(__urng
, this->param()); }
2957 template<typename _UniformRandomNumberGenerator
>
2959 operator()(_UniformRandomNumberGenerator
& __urng
,
2960 const param_type
& __p
)
2962 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2964 if ((__aurng() - __aurng
.min())
2965 < __p
.p() * (__aurng
.max() - __aurng
.min()))
2971 param_type _M_param
;
2975 * @brief Inserts a %bernoulli_distribution random number distribution
2976 * @p __x into the output stream @p __os.
2978 * @param __os An output stream.
2979 * @param __x A %bernoulli_distribution random number distribution.
2981 * @returns The output stream with the state of @p __x inserted or in
2984 template<typename _CharT
, typename _Traits
>
2985 std::basic_ostream
<_CharT
, _Traits
>&
2986 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2987 const std::bernoulli_distribution
&);
2990 * @brief Extracts a %bernoulli_distribution random number distribution
2991 * @p __x from the input stream @p __is.
2993 * @param __is An input stream.
2994 * @param __x A %bernoulli_distribution random number generator engine.
2996 * @returns The input stream with @p __x extracted or in an error state.
2998 template<typename _CharT
, typename _Traits
>
2999 std::basic_istream
<_CharT
, _Traits
>&
3000 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3001 std::bernoulli_distribution
& __x
)
3005 __x
.param(bernoulli_distribution::param_type(__p
));
3011 * @brief A discrete binomial random number distribution.
3013 * The formula for the binomial probability density function is
3014 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3015 * and @f$ p @f$ are the parameters of the distribution.
3017 template<typename _IntType
= int>
3018 class binomial_distribution
3020 static_assert(std::is_integral
<_IntType
>::value
,
3021 "template argument not an integral type");
3024 /** The type of the range of the distribution. */
3025 typedef _IntType result_type
;
3026 /** Parameter type. */
3029 typedef binomial_distribution
<_IntType
> distribution_type
;
3030 friend class binomial_distribution
<_IntType
>;
3033 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3034 : _M_t(__t
), _M_p(__p
)
3036 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3058 #if _GLIBCXX_USE_C99_MATH_TR1
3059 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3060 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3065 // constructors and member function
3067 binomial_distribution(_IntType __t
= _IntType(1),
3069 : _M_param(__t
, __p
), _M_nd()
3073 binomial_distribution(const param_type
& __p
)
3074 : _M_param(__p
), _M_nd()
3078 * @brief Resets the distribution state.
3085 * @brief Returns the distribution @p t parameter.
3089 { return _M_param
.t(); }
3092 * @brief Returns the distribution @p p parameter.
3096 { return _M_param
.p(); }
3099 * @brief Returns the parameter set of the distribution.
3103 { return _M_param
; }
3106 * @brief Sets the parameter set of the distribution.
3107 * @param __param The new parameter set of the distribution.
3110 param(const param_type
& __param
)
3111 { _M_param
= __param
; }
3114 * @brief Returns the greatest lower bound value of the distribution.
3121 * @brief Returns the least upper bound value of the distribution.
3125 { return _M_param
.t(); }
3127 template<typename _UniformRandomNumberGenerator
>
3129 operator()(_UniformRandomNumberGenerator
& __urng
)
3130 { return this->operator()(__urng
, this->param()); }
3132 template<typename _UniformRandomNumberGenerator
>
3134 operator()(_UniformRandomNumberGenerator
& __urng
,
3135 const param_type
& __p
);
3138 * @brief Inserts a %binomial_distribution random number distribution
3139 * @p __x into the output stream @p __os.
3141 * @param __os An output stream.
3142 * @param __x A %binomial_distribution random number distribution.
3144 * @returns The output stream with the state of @p __x inserted or in
3147 template<typename _IntType1
,
3148 typename _CharT
, typename _Traits
>
3149 friend std::basic_ostream
<_CharT
, _Traits
>&
3150 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3151 const std::binomial_distribution
<_IntType1
>&);
3154 * @brief Extracts a %binomial_distribution random number distribution
3155 * @p __x from the input stream @p __is.
3157 * @param __is An input stream.
3158 * @param __x A %binomial_distribution random number generator engine.
3160 * @returns The input stream with @p __x extracted or in an error
3163 template<typename _IntType1
,
3164 typename _CharT
, typename _Traits
>
3165 friend std::basic_istream
<_CharT
, _Traits
>&
3166 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3167 std::binomial_distribution
<_IntType1
>&);
3170 template<typename _UniformRandomNumberGenerator
>
3172 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3174 param_type _M_param
;
3176 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3177 std::normal_distribution
<double> _M_nd
;
3182 * @brief A discrete geometric random number distribution.
3184 * The formula for the geometric probability density function is
3185 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3188 template<typename _IntType
= int>
3189 class geometric_distribution
3191 static_assert(std::is_integral
<_IntType
>::value
,
3192 "template argument not an integral type");
3195 /** The type of the range of the distribution. */
3196 typedef _IntType result_type
;
3197 /** Parameter type. */
3200 typedef geometric_distribution
<_IntType
> distribution_type
;
3201 friend class geometric_distribution
<_IntType
>;
3204 param_type(double __p
= 0.5)
3207 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3219 { _M_log_p
= std::log(_M_p
); }
3226 // constructors and member function
3228 geometric_distribution(double __p
= 0.5)
3233 geometric_distribution(const param_type
& __p
)
3238 * @brief Resets the distribution state.
3240 * Does nothing for the geometric distribution.
3246 * @brief Returns the distribution parameter @p p.
3250 { return _M_param
.p(); }
3253 * @brief Returns the parameter set of the distribution.
3257 { return _M_param
; }
3260 * @brief Sets the parameter set of the distribution.
3261 * @param __param The new parameter set of the distribution.
3264 param(const param_type
& __param
)
3265 { _M_param
= __param
; }
3268 * @brief Returns the greatest lower bound value of the distribution.
3275 * @brief Returns the least upper bound value of the distribution.
3279 { return std::numeric_limits
<result_type
>::max(); }
3281 template<typename _UniformRandomNumberGenerator
>
3283 operator()(_UniformRandomNumberGenerator
& __urng
)
3284 { return this->operator()(__urng
, this->param()); }
3286 template<typename _UniformRandomNumberGenerator
>
3288 operator()(_UniformRandomNumberGenerator
& __urng
,
3289 const param_type
& __p
);
3292 param_type _M_param
;
3296 * @brief Inserts a %geometric_distribution random number distribution
3297 * @p __x into the output stream @p __os.
3299 * @param __os An output stream.
3300 * @param __x A %geometric_distribution random number distribution.
3302 * @returns The output stream with the state of @p __x inserted or in
3305 template<typename _IntType
,
3306 typename _CharT
, typename _Traits
>
3307 std::basic_ostream
<_CharT
, _Traits
>&
3308 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3309 const std::geometric_distribution
<_IntType
>&);
3312 * @brief Extracts a %geometric_distribution random number distribution
3313 * @p __x from the input stream @p __is.
3315 * @param __is An input stream.
3316 * @param __x A %geometric_distribution random number generator engine.
3318 * @returns The input stream with @p __x extracted or in an error state.
3320 template<typename _IntType
,
3321 typename _CharT
, typename _Traits
>
3322 std::basic_istream
<_CharT
, _Traits
>&
3323 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3324 std::geometric_distribution
<_IntType
>&);
3328 * @brief A negative_binomial_distribution random number distribution.
3330 * The formula for the negative binomial probability mass function is
3331 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3332 * and @f$ p @f$ are the parameters of the distribution.
3334 template<typename _IntType
= int>
3335 class negative_binomial_distribution
3337 static_assert(std::is_integral
<_IntType
>::value
,
3338 "template argument not an integral type");
3341 /** The type of the range of the distribution. */
3342 typedef _IntType result_type
;
3343 /** Parameter type. */
3346 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3349 param_type(_IntType __k
= 1, double __p
= 0.5)
3350 : _M_k(__k
), _M_p(__p
)
3367 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3368 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3372 negative_binomial_distribution(const param_type
& __p
)
3373 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3377 * @brief Resets the distribution state.
3384 * @brief Return the @f$ k @f$ parameter of the distribution.
3388 { return _M_param
.k(); }
3391 * @brief Return the @f$ p @f$ parameter of the distribution.
3395 { return _M_param
.p(); }
3398 * @brief Returns the parameter set of the distribution.
3402 { return _M_param
; }
3405 * @brief Sets the parameter set of the distribution.
3406 * @param __param The new parameter set of the distribution.
3409 param(const param_type
& __param
)
3410 { _M_param
= __param
; }
3413 * @brief Returns the greatest lower bound value of the distribution.
3417 { return result_type(0); }
3420 * @brief Returns the least upper bound value of the distribution.
3424 { return std::numeric_limits
<result_type
>::max(); }
3426 template<typename _UniformRandomNumberGenerator
>
3428 operator()(_UniformRandomNumberGenerator
& __urng
);
3430 template<typename _UniformRandomNumberGenerator
>
3432 operator()(_UniformRandomNumberGenerator
& __urng
,
3433 const param_type
& __p
);
3436 * @brief Inserts a %negative_binomial_distribution random
3437 * number distribution @p __x into the output stream @p __os.
3439 * @param __os An output stream.
3440 * @param __x A %negative_binomial_distribution random number
3443 * @returns The output stream with the state of @p __x inserted or in
3446 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3447 friend std::basic_ostream
<_CharT
, _Traits
>&
3448 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3449 const std::negative_binomial_distribution
<_IntType1
>&);
3452 * @brief Extracts a %negative_binomial_distribution random number
3453 * distribution @p __x from the input stream @p __is.
3455 * @param __is An input stream.
3456 * @param __x A %negative_binomial_distribution random number
3459 * @returns The input stream with @p __x extracted or in an error state.
3461 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3462 friend std::basic_istream
<_CharT
, _Traits
>&
3463 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3464 std::negative_binomial_distribution
<_IntType1
>&);
3467 param_type _M_param
;
3469 std::gamma_distribution
<double> _M_gd
;
3472 /* @} */ // group std_random_distributions_bernoulli
3475 * @addtogroup std_random_distributions_poisson Poisson Distributions
3476 * @ingroup std_random_distributions
3481 * @brief A discrete Poisson random number distribution.
3483 * The formula for the Poisson probability density function is
3484 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3485 * parameter of the distribution.
3487 template<typename _IntType
= int>
3488 class poisson_distribution
3490 static_assert(std::is_integral
<_IntType
>::value
,
3491 "template argument not an integral type");
3494 /** The type of the range of the distribution. */
3495 typedef _IntType result_type
;
3496 /** Parameter type. */
3499 typedef poisson_distribution
<_IntType
> distribution_type
;
3500 friend class poisson_distribution
<_IntType
>;
3503 param_type(double __mean
= 1.0)
3506 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3515 // Hosts either log(mean) or the threshold of the simple method.
3522 #if _GLIBCXX_USE_C99_MATH_TR1
3523 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3527 // constructors and member function
3529 poisson_distribution(double __mean
= 1.0)
3530 : _M_param(__mean
), _M_nd()
3534 poisson_distribution(const param_type
& __p
)
3535 : _M_param(__p
), _M_nd()
3539 * @brief Resets the distribution state.
3546 * @brief Returns the distribution parameter @p mean.
3550 { return _M_param
.mean(); }
3553 * @brief Returns the parameter set of the distribution.
3557 { return _M_param
; }
3560 * @brief Sets the parameter set of the distribution.
3561 * @param __param The new parameter set of the distribution.
3564 param(const param_type
& __param
)
3565 { _M_param
= __param
; }
3568 * @brief Returns the greatest lower bound value of the distribution.
3575 * @brief Returns the least upper bound value of the distribution.
3579 { return std::numeric_limits
<result_type
>::max(); }
3581 template<typename _UniformRandomNumberGenerator
>
3583 operator()(_UniformRandomNumberGenerator
& __urng
)
3584 { return this->operator()(__urng
, this->param()); }
3586 template<typename _UniformRandomNumberGenerator
>
3588 operator()(_UniformRandomNumberGenerator
& __urng
,
3589 const param_type
& __p
);
3592 * @brief Inserts a %poisson_distribution random number distribution
3593 * @p __x into the output stream @p __os.
3595 * @param __os An output stream.
3596 * @param __x A %poisson_distribution random number distribution.
3598 * @returns The output stream with the state of @p __x inserted or in
3601 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3602 friend std::basic_ostream
<_CharT
, _Traits
>&
3603 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3604 const std::poisson_distribution
<_IntType1
>&);
3607 * @brief Extracts a %poisson_distribution random number distribution
3608 * @p __x from the input stream @p __is.
3610 * @param __is An input stream.
3611 * @param __x A %poisson_distribution random number generator engine.
3613 * @returns The input stream with @p __x extracted or in an error
3616 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3617 friend std::basic_istream
<_CharT
, _Traits
>&
3618 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3619 std::poisson_distribution
<_IntType1
>&);
3622 param_type _M_param
;
3624 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3625 std::normal_distribution
<double> _M_nd
;
3629 * @brief An exponential continuous distribution for random numbers.
3631 * The formula for the exponential probability density function is
3632 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3634 * <table border=1 cellpadding=10 cellspacing=0>
3635 * <caption align=top>Distribution Statistics</caption>
3636 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3637 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3638 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3639 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3640 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3643 template<typename _RealType
= double>
3644 class exponential_distribution
3646 static_assert(std::is_floating_point
<_RealType
>::value
,
3647 "template argument not a floating point type");
3650 /** The type of the range of the distribution. */
3651 typedef _RealType result_type
;
3652 /** Parameter type. */
3655 typedef exponential_distribution
<_RealType
> distribution_type
;
3658 param_type(_RealType __lambda
= _RealType(1))
3659 : _M_lambda(__lambda
)
3661 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
3666 { return _M_lambda
; }
3669 _RealType _M_lambda
;
3674 * @brief Constructs an exponential distribution with inverse scale
3675 * parameter @f$ \lambda @f$.
3678 exponential_distribution(const result_type
& __lambda
= result_type(1))
3679 : _M_param(__lambda
)
3683 exponential_distribution(const param_type
& __p
)
3688 * @brief Resets the distribution state.
3690 * Has no effect on exponential distributions.
3696 * @brief Returns the inverse scale parameter of the distribution.
3700 { return _M_param
.lambda(); }
3703 * @brief Returns the parameter set of the distribution.
3707 { return _M_param
; }
3710 * @brief Sets the parameter set of the distribution.
3711 * @param __param The new parameter set of the distribution.
3714 param(const param_type
& __param
)
3715 { _M_param
= __param
; }
3718 * @brief Returns the greatest lower bound value of the distribution.
3722 { return result_type(0); }
3725 * @brief Returns the least upper bound value of the distribution.
3729 { return std::numeric_limits
<result_type
>::max(); }
3731 template<typename _UniformRandomNumberGenerator
>
3733 operator()(_UniformRandomNumberGenerator
& __urng
)
3734 { return this->operator()(__urng
, this->param()); }
3736 template<typename _UniformRandomNumberGenerator
>
3738 operator()(_UniformRandomNumberGenerator
& __urng
,
3739 const param_type
& __p
)
3741 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3743 return -std::log(__aurng()) / __p
.lambda();
3747 param_type _M_param
;
3751 * @brief Inserts a %exponential_distribution random number distribution
3752 * @p __x into the output stream @p __os.
3754 * @param __os An output stream.
3755 * @param __x A %exponential_distribution random number distribution.
3757 * @returns The output stream with the state of @p __x inserted or in
3760 template<typename _RealType
, typename _CharT
, typename _Traits
>
3761 std::basic_ostream
<_CharT
, _Traits
>&
3762 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3763 const std::exponential_distribution
<_RealType
>&);
3766 * @brief Extracts a %exponential_distribution random number distribution
3767 * @p __x from the input stream @p __is.
3769 * @param __is An input stream.
3770 * @param __x A %exponential_distribution random number
3773 * @returns The input stream with @p __x extracted or in an error state.
3775 template<typename _RealType
, typename _CharT
, typename _Traits
>
3776 std::basic_istream
<_CharT
, _Traits
>&
3777 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3778 std::exponential_distribution
<_RealType
>&);
3782 * @brief A weibull_distribution random number distribution.
3784 * The formula for the normal probability density function is
3785 * @f$ p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
3786 * \exp{(-(\frac{x}{\beta})^\alpha)} @f$.
3788 template<typename _RealType
= double>
3789 class weibull_distribution
3791 static_assert(std::is_floating_point
<_RealType
>::value
,
3792 "template argument not a floating point type");
3795 /** The type of the range of the distribution. */
3796 typedef _RealType result_type
;
3797 /** Parameter type. */
3800 typedef weibull_distribution
<_RealType
> distribution_type
;
3803 param_type(_RealType __a
= _RealType(1),
3804 _RealType __b
= _RealType(1))
3805 : _M_a(__a
), _M_b(__b
)
3822 weibull_distribution(_RealType __a
= _RealType(1),
3823 _RealType __b
= _RealType(1))
3824 : _M_param(__a
, __b
)
3828 weibull_distribution(const param_type
& __p
)
3833 * @brief Resets the distribution state.
3840 * @brief Return the @f$ a @f$ parameter of the distribution.
3844 { return _M_param
.a(); }
3847 * @brief Return the @f$ b @f$ parameter of the distribution.
3851 { return _M_param
.b(); }
3854 * @brief Returns the parameter set of the distribution.
3858 { return _M_param
; }
3861 * @brief Sets the parameter set of the distribution.
3862 * @param __param The new parameter set of the distribution.
3865 param(const param_type
& __param
)
3866 { _M_param
= __param
; }
3869 * @brief Returns the greatest lower bound value of the distribution.
3873 { return result_type(0); }
3876 * @brief Returns the least upper bound value of the distribution.
3880 { return std::numeric_limits
<result_type
>::max(); }
3882 template<typename _UniformRandomNumberGenerator
>
3884 operator()(_UniformRandomNumberGenerator
& __urng
)
3885 { return this->operator()(__urng
, this->param()); }
3887 template<typename _UniformRandomNumberGenerator
>
3889 operator()(_UniformRandomNumberGenerator
& __urng
,
3890 const param_type
& __p
);
3893 param_type _M_param
;
3897 * @brief Inserts a %weibull_distribution random number distribution
3898 * @p __x into the output stream @p __os.
3900 * @param __os An output stream.
3901 * @param __x A %weibull_distribution random number distribution.
3903 * @returns The output stream with the state of @p __x inserted or in
3906 template<typename _RealType
, typename _CharT
, typename _Traits
>
3907 std::basic_ostream
<_CharT
, _Traits
>&
3908 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3909 const std::weibull_distribution
<_RealType
>&);
3912 * @brief Extracts a %weibull_distribution random number distribution
3913 * @p __x from the input stream @p __is.
3915 * @param __is An input stream.
3916 * @param __x A %weibull_distribution random number
3919 * @returns The input stream with @p __x extracted or in an error state.
3921 template<typename _RealType
, typename _CharT
, typename _Traits
>
3922 std::basic_istream
<_CharT
, _Traits
>&
3923 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3924 std::weibull_distribution
<_RealType
>&);
3928 * @brief A extreme_value_distribution random number distribution.
3930 * The formula for the normal probability mass function is
3931 * @f$ p(x|a,b) = \frac{1}{b}
3932 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
3934 template<typename _RealType
= double>
3935 class extreme_value_distribution
3937 static_assert(std::is_floating_point
<_RealType
>::value
,
3938 "template argument not a floating point type");
3941 /** The type of the range of the distribution. */
3942 typedef _RealType result_type
;
3943 /** Parameter type. */
3946 typedef extreme_value_distribution
<_RealType
> distribution_type
;
3949 param_type(_RealType __a
= _RealType(0),
3950 _RealType __b
= _RealType(1))
3951 : _M_a(__a
), _M_b(__b
)
3968 extreme_value_distribution(_RealType __a
= _RealType(0),
3969 _RealType __b
= _RealType(1))
3970 : _M_param(__a
, __b
)
3974 extreme_value_distribution(const param_type
& __p
)
3979 * @brief Resets the distribution state.
3986 * @brief Return the @f$ a @f$ parameter of the distribution.
3990 { return _M_param
.a(); }
3993 * @brief Return the @f$ b @f$ parameter of the distribution.
3997 { return _M_param
.b(); }
4000 * @brief Returns the parameter set of the distribution.
4004 { return _M_param
; }
4007 * @brief Sets the parameter set of the distribution.
4008 * @param __param The new parameter set of the distribution.
4011 param(const param_type
& __param
)
4012 { _M_param
= __param
; }
4015 * @brief Returns the greatest lower bound value of the distribution.
4019 { return std::numeric_limits
<result_type
>::min(); }
4022 * @brief Returns the least upper bound value of the distribution.
4026 { return std::numeric_limits
<result_type
>::max(); }
4028 template<typename _UniformRandomNumberGenerator
>
4030 operator()(_UniformRandomNumberGenerator
& __urng
)
4031 { return this->operator()(__urng
, this->param()); }
4033 template<typename _UniformRandomNumberGenerator
>
4035 operator()(_UniformRandomNumberGenerator
& __urng
,
4036 const param_type
& __p
);
4039 param_type _M_param
;
4043 * @brief Inserts a %extreme_value_distribution random number distribution
4044 * @p __x into the output stream @p __os.
4046 * @param __os An output stream.
4047 * @param __x A %extreme_value_distribution random number distribution.
4049 * @returns The output stream with the state of @p __x inserted or in
4052 template<typename _RealType
, typename _CharT
, typename _Traits
>
4053 std::basic_ostream
<_CharT
, _Traits
>&
4054 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4055 const std::extreme_value_distribution
<_RealType
>&);
4058 * @brief Extracts a %extreme_value_distribution random number
4059 * distribution @p __x from the input stream @p __is.
4061 * @param __is An input stream.
4062 * @param __x A %extreme_value_distribution random number
4065 * @returns The input stream with @p __x extracted or in an error state.
4067 template<typename _RealType
, typename _CharT
, typename _Traits
>
4068 std::basic_istream
<_CharT
, _Traits
>&
4069 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4070 std::extreme_value_distribution
<_RealType
>&);
4074 * @brief A discrete_distribution random number distribution.
4076 * The formula for the discrete probability mass function is
4079 template<typename _IntType
= int>
4080 class discrete_distribution
4082 static_assert(std::is_integral
<_IntType
>::value
,
4083 "template argument not an integral type");
4086 /** The type of the range of the distribution. */
4087 typedef _IntType result_type
;
4088 /** Parameter type. */
4091 typedef discrete_distribution
<_IntType
> distribution_type
;
4092 friend class discrete_distribution
<_IntType
>;
4095 : _M_prob(), _M_cp()
4096 { _M_initialize(); }
4098 template<typename _InputIterator
>
4099 param_type(_InputIterator __wbegin
,
4100 _InputIterator __wend
)
4101 : _M_prob(__wbegin
, __wend
), _M_cp()
4102 { _M_initialize(); }
4104 param_type(initializer_list
<double> __wil
)
4105 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4106 { _M_initialize(); }
4108 template<typename _Func
>
4109 param_type(size_t __nw
, double __xmin
, double __xmax
,
4113 probabilities() const
4120 std::vector
<double> _M_prob
;
4121 std::vector
<double> _M_cp
;
4124 discrete_distribution()
4128 template<typename _InputIterator
>
4129 discrete_distribution(_InputIterator __wbegin
,
4130 _InputIterator __wend
)
4131 : _M_param(__wbegin
, __wend
)
4134 discrete_distribution(initializer_list
<double> __wl
)
4138 template<typename _Func
>
4139 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4141 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4145 discrete_distribution(const param_type
& __p
)
4150 * @brief Resets the distribution state.
4157 * @brief Returns the probabilities of the distribution.
4160 probabilities() const
4161 { return _M_param
.probabilities(); }
4164 * @brief Returns the parameter set of the distribution.
4168 { return _M_param
; }
4171 * @brief Sets the parameter set of the distribution.
4172 * @param __param The new parameter set of the distribution.
4175 param(const param_type
& __param
)
4176 { _M_param
= __param
; }
4179 * @brief Returns the greatest lower bound value of the distribution.
4183 { return result_type(0); }
4186 * @brief Returns the least upper bound value of the distribution.
4190 { return this->_M_param
._M_prob
.size() - 1; }
4192 template<typename _UniformRandomNumberGenerator
>
4194 operator()(_UniformRandomNumberGenerator
& __urng
)
4195 { return this->operator()(__urng
, this->param()); }
4197 template<typename _UniformRandomNumberGenerator
>
4199 operator()(_UniformRandomNumberGenerator
& __urng
,
4200 const param_type
& __p
);
4203 * @brief Inserts a %discrete_distribution random number distribution
4204 * @p __x into the output stream @p __os.
4206 * @param __os An output stream.
4207 * @param __x A %discrete_distribution random number distribution.
4209 * @returns The output stream with the state of @p __x inserted or in
4212 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4213 friend std::basic_ostream
<_CharT
, _Traits
>&
4214 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4215 const std::discrete_distribution
<_IntType1
>&);
4218 * @brief Extracts a %discrete_distribution random number distribution
4219 * @p __x from the input stream @p __is.
4221 * @param __is An input stream.
4222 * @param __x A %discrete_distribution random number
4225 * @returns The input stream with @p __x extracted or in an error
4228 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4229 friend std::basic_istream
<_CharT
, _Traits
>&
4230 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4231 std::discrete_distribution
<_IntType1
>&);
4234 param_type _M_param
;
4239 * @brief A piecewise_constant_distribution random number distribution.
4241 * The formula for the piecewise constant probability mass function is
4244 template<typename _RealType
= double>
4245 class piecewise_constant_distribution
4247 static_assert(std::is_floating_point
<_RealType
>::value
,
4248 "template argument not a floating point type");
4251 /** The type of the range of the distribution. */
4252 typedef _RealType result_type
;
4253 /** Parameter type. */
4256 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4257 friend class piecewise_constant_distribution
<_RealType
>;
4260 : _M_int(), _M_den(), _M_cp()
4261 { _M_initialize(); }
4263 template<typename _InputIteratorB
, typename _InputIteratorW
>
4264 param_type(_InputIteratorB __bfirst
,
4265 _InputIteratorB __bend
,
4266 _InputIteratorW __wbegin
);
4268 template<typename _Func
>
4269 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4271 template<typename _Func
>
4272 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4275 std::vector
<_RealType
>
4287 std::vector
<_RealType
> _M_int
;
4288 std::vector
<double> _M_den
;
4289 std::vector
<double> _M_cp
;
4293 piecewise_constant_distribution()
4297 template<typename _InputIteratorB
, typename _InputIteratorW
>
4298 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4299 _InputIteratorB __bend
,
4300 _InputIteratorW __wbegin
)
4301 : _M_param(__bfirst
, __bend
, __wbegin
)
4304 template<typename _Func
>
4305 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4307 : _M_param(__bl
, __fw
)
4310 template<typename _Func
>
4311 piecewise_constant_distribution(size_t __nw
,
4312 _RealType __xmin
, _RealType __xmax
,
4314 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4318 piecewise_constant_distribution(const param_type
& __p
)
4323 * @brief Resets the distribution state.
4330 * @brief Returns a vector of the intervals.
4332 std::vector
<_RealType
>
4334 { return _M_param
.intervals(); }
4337 * @brief Returns a vector of the probability densities.
4341 { return _M_param
.densities(); }
4344 * @brief Returns the parameter set of the distribution.
4348 { return _M_param
; }
4351 * @brief Sets the parameter set of the distribution.
4352 * @param __param The new parameter set of the distribution.
4355 param(const param_type
& __param
)
4356 { _M_param
= __param
; }
4359 * @brief Returns the greatest lower bound value of the distribution.
4363 { return this->_M_param
._M_int
.front(); }
4366 * @brief Returns the least upper bound value of the distribution.
4370 { return this->_M_param
._M_int
.back(); }
4372 template<typename _UniformRandomNumberGenerator
>
4374 operator()(_UniformRandomNumberGenerator
& __urng
)
4375 { return this->operator()(__urng
, this->param()); }
4377 template<typename _UniformRandomNumberGenerator
>
4379 operator()(_UniformRandomNumberGenerator
& __urng
,
4380 const param_type
& __p
);
4383 * @brief Inserts a %piecewise_constan_distribution random
4384 * number distribution @p __x into the output stream @p __os.
4386 * @param __os An output stream.
4387 * @param __x A %piecewise_constan_distribution random number
4390 * @returns The output stream with the state of @p __x inserted or in
4393 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4394 friend std::basic_ostream
<_CharT
, _Traits
>&
4395 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4396 const std::piecewise_constant_distribution
<_RealType1
>&);
4399 * @brief Extracts a %piecewise_constan_distribution random
4400 * number distribution @p __x from the input stream @p __is.
4402 * @param __is An input stream.
4403 * @param __x A %piecewise_constan_distribution random number
4406 * @returns The input stream with @p __x extracted or in an error
4409 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4410 friend std::basic_istream
<_CharT
, _Traits
>&
4411 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4412 std::piecewise_constant_distribution
<_RealType1
>&);
4415 param_type _M_param
;
4420 * @brief A piecewise_linear_distribution random number distribution.
4422 * The formula for the piecewise linear probability mass function is
4425 template<typename _RealType
= double>
4426 class piecewise_linear_distribution
4428 static_assert(std::is_floating_point
<_RealType
>::value
,
4429 "template argument not a floating point type");
4432 /** The type of the range of the distribution. */
4433 typedef _RealType result_type
;
4434 /** Parameter type. */
4437 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
4438 friend class piecewise_linear_distribution
<_RealType
>;
4441 : _M_int(), _M_den(), _M_cp(), _M_m()
4442 { _M_initialize(); }
4444 template<typename _InputIteratorB
, typename _InputIteratorW
>
4445 param_type(_InputIteratorB __bfirst
,
4446 _InputIteratorB __bend
,
4447 _InputIteratorW __wbegin
);
4449 template<typename _Func
>
4450 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
4452 template<typename _Func
>
4453 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4456 std::vector
<_RealType
>
4468 std::vector
<_RealType
> _M_int
;
4469 std::vector
<double> _M_den
;
4470 std::vector
<double> _M_cp
;
4471 std::vector
<double> _M_m
;
4475 piecewise_linear_distribution()
4479 template<typename _InputIteratorB
, typename _InputIteratorW
>
4480 piecewise_linear_distribution(_InputIteratorB __bfirst
,
4481 _InputIteratorB __bend
,
4482 _InputIteratorW __wbegin
)
4483 : _M_param(__bfirst
, __bend
, __wbegin
)
4486 template<typename _Func
>
4487 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
4489 : _M_param(__bl
, __fw
)
4492 template<typename _Func
>
4493 piecewise_linear_distribution(size_t __nw
,
4494 _RealType __xmin
, _RealType __xmax
,
4496 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4500 piecewise_linear_distribution(const param_type
& __p
)
4505 * Resets the distribution state.
4512 * @brief Return the intervals of the distribution.
4514 std::vector
<_RealType
>
4516 { return _M_param
.intervals(); }
4519 * @brief Return a vector of the probability densities of the
4524 { return _M_param
.densities(); }
4527 * @brief Returns the parameter set of the distribution.
4531 { return _M_param
; }
4534 * @brief Sets the parameter set of the distribution.
4535 * @param __param The new parameter set of the distribution.
4538 param(const param_type
& __param
)
4539 { _M_param
= __param
; }
4542 * @brief Returns the greatest lower bound value of the distribution.
4546 { return this->_M_param
._M_int
.front(); }
4549 * @brief Returns the least upper bound value of the distribution.
4553 { return this->_M_param
._M_int
.back(); }
4555 template<typename _UniformRandomNumberGenerator
>
4557 operator()(_UniformRandomNumberGenerator
& __urng
)
4558 { return this->operator()(__urng
, this->param()); }
4560 template<typename _UniformRandomNumberGenerator
>
4562 operator()(_UniformRandomNumberGenerator
& __urng
,
4563 const param_type
& __p
);
4566 * @brief Inserts a %piecewise_linear_distribution random number
4567 * distribution @p __x into the output stream @p __os.
4569 * @param __os An output stream.
4570 * @param __x A %piecewise_linear_distribution random number
4573 * @returns The output stream with the state of @p __x inserted or in
4576 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4577 friend std::basic_ostream
<_CharT
, _Traits
>&
4578 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4579 const std::piecewise_linear_distribution
<_RealType1
>&);
4582 * @brief Extracts a %piecewise_linear_distribution random number
4583 * distribution @p __x from the input stream @p __is.
4585 * @param __is An input stream.
4586 * @param __x A %piecewise_linear_distribution random number
4589 * @returns The input stream with @p __x extracted or in an error
4592 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4593 friend std::basic_istream
<_CharT
, _Traits
>&
4594 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4595 std::piecewise_linear_distribution
<_RealType1
>&);
4598 param_type _M_param
;
4602 /* @} */ // group std_random_distributions_poisson
4604 /* @} */ // group std_random_distributions
4607 * @addtogroup std_random_utilities Random Number Utilities
4608 * @ingroup std_random
4613 * @brief The seed_seq class generates sequences of seeds for random
4614 * number generators.
4620 /** The type of the seed vales. */
4621 typedef uint_least32_t result_type
;
4623 /** Default constructor. */
4628 template<typename _IntType
>
4629 seed_seq(std::initializer_list
<_IntType
> il
);
4631 template<typename _InputIterator
>
4632 seed_seq(_InputIterator __begin
, _InputIterator __end
);
4634 // generating functions
4635 template<typename _RandomAccessIterator
>
4637 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
4639 // property functions
4641 { return _M_v
.size(); }
4643 template<typename OutputIterator
>
4645 param(OutputIterator __dest
) const
4646 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
4650 std::vector
<result_type
> _M_v
;
4653 /* @} */ // group std_random_utilities
4655 /* @} */ // group std_random