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.
38 // [26.4] Random number generation
41 * @defgroup random Random Number Generation
44 * A facility for generating random numbers on selected distributions.
49 * @brief A function template for converting the output of a (integral)
50 * uniform random number generator to a floatng point result in the range
53 template<typename _RealType
, size_t __bits
,
54 typename _UniformRandomNumberGenerator
>
56 generate_canonical(_UniformRandomNumberGenerator
& __g
);
59 * Implementation-space details.
63 template<typename _UIntType
, size_t __w
,
64 bool = __w
< static_cast<size_t>
65 (std::numeric_limits
<_UIntType
>::digits
)>
67 { static const _UIntType __value
= 0; };
69 template<typename _UIntType
, size_t __w
>
70 struct _Shift
<_UIntType
, __w
, true>
71 { static const _UIntType __value
= _UIntType(1) << __w
; };
73 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool>
76 // Dispatch based on modulus value to prevent divide-by-zero compile-time
77 // errors when m == 0.
78 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
81 { return _Mod
<_Tp
, __m
, __a
, __c
, __m
== 0>::__calc(__x
); }
84 * An adaptor class for converting the output of any Generator into
85 * the input for a specific Distribution.
87 template<typename _Engine
, typename _DInputType
>
92 _Adaptor(_Engine
& __g
)
97 { return _DInputType(0); }
101 { return _DInputType(1); }
104 * Converts a value generated by the adapted random number generator
105 * into a value in the input domain for the dependent random number
111 return std::generate_canonical
<_DInputType
,
112 std::numeric_limits
<_DInputType
>::digits
,
119 } // namespace __detail
122 * @addtogroup random_generators Random Number Generators
125 * These classes define objects which provide random or pseudorandom
126 * numbers, either from a discrete or a continuous interval. The
127 * random number generator supplied as a part of this library are
128 * all uniform random number generators which provide a sequence of
129 * random number uniformly distributed over their range.
131 * A number generator is a function object with an operator() that
132 * takes zero arguments and returns a number.
134 * A compliant random number generator must satisfy the following
135 * requirements. <table border=1 cellpadding=10 cellspacing=0>
136 * <caption align=top>Random Number Generator Requirements</caption>
137 * <tr><td>To be documented.</td></tr> </table>
143 * @brief A model of a linear congruential random number generator.
145 * A random number generator that produces pseudorandom numbers via
148 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
151 * The template parameter @p _UIntType must be an unsigned integral type
152 * large enough to store values up to (__m-1). If the template parameter
153 * @p __m is 0, the modulus @p __m used is
154 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
155 * parameters @p __a and @p __c must be less than @p __m.
157 * The size of the state is @f$1@f$.
159 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
160 class linear_congruential_engine
162 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
163 "substituting _UIntType not an unsigned integral type");
164 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
165 "template argument substituting __m out of bounds");
168 /** The type of the generated random value. */
169 typedef _UIntType result_type
;
171 /** The multiplier. */
172 static constexpr result_type multiplier
= __a
;
174 static constexpr result_type increment
= __c
;
176 static constexpr result_type modulus
= __m
;
177 static constexpr result_type default_seed
= 1u;
180 * @brief Constructs a %linear_congruential_engine random number
181 * generator engine with seed @p __s. The default seed value
184 * @param __s The initial seed value.
187 linear_congruential_engine(result_type __s
= default_seed
)
191 * @brief Constructs a %linear_congruential_engine random number
192 * generator engine seeded from the seed sequence @p __q.
194 * @param __q the seed sequence.
196 template<typename _Sseq
, typename
= typename
197 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
200 linear_congruential_engine(_Sseq
& __q
)
204 * @brief Reseeds the %linear_congruential_engine random number generator
205 * engine sequence to the seed @p __s.
207 * @param __s The new seed.
210 seed(result_type __s
= default_seed
);
213 * @brief Reseeds the %linear_congruential_engine random number generator
215 * sequence using values from the seed sequence @p __q.
217 * @param __q the seed sequence.
219 template<typename _Sseq
>
220 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
224 * @brief Gets the smallest possible value in the output range.
226 * The minimum depends on the @p __c parameter: if it is zero, the
227 * minimum generated must be > 0, otherwise 0 is allowed.
229 static constexpr result_type
231 { return __c
== 0u ? 1u : 0u; }
234 * @brief Gets the largest possible value in the output range.
236 static constexpr result_type
241 * @brief Discard a sequence of random numbers.
244 discard(unsigned long long __z
)
246 for (; __z
!= 0ULL; --__z
)
251 * @brief Gets the next random number in the sequence.
256 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
261 * @brief Compares two linear congruential random number generator
262 * objects of the same type for equality.
264 * @param __lhs A linear congruential random number generator object.
265 * @param __rhs Another linear congruential random number generator
268 * @returns true if the infinite sequences of generated values
269 * would be equal, false otherwise.
272 operator==(const linear_congruential_engine
& __lhs
,
273 const linear_congruential_engine
& __rhs
)
274 { return __lhs
._M_x
== __rhs
._M_x
; }
277 * @brief Writes the textual representation of the state x(i) of x to
280 * @param __os The output stream.
281 * @param __lcr A % linear_congruential_engine random number generator.
284 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
285 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
286 friend std::basic_ostream
<_CharT
, _Traits
>&
287 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
288 const std::linear_congruential_engine
<_UIntType1
,
292 * @brief Sets the state of the engine by reading its textual
293 * representation from @p __is.
295 * The textual representation must have been previously written using
296 * an output stream whose imbued locale and whose type's template
297 * specialization arguments _CharT and _Traits were the same as those
300 * @param __is The input stream.
301 * @param __lcr A % linear_congruential_engine random number generator.
304 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
305 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
306 friend std::basic_istream
<_CharT
, _Traits
>&
307 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
308 std::linear_congruential_engine
<_UIntType1
, __a1
,
316 * @brief Compares two linear congruential random number generator
317 * objects of the same type for inequality.
319 * @param __lhs A linear congruential random number generator object.
320 * @param __rhs Another linear congruential random number generator
323 * @returns true if the infinite sequences of generated values
324 * would be different, false otherwise.
326 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
328 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
330 const std::linear_congruential_engine
<_UIntType
, __a
,
332 { return !(__lhs
== __rhs
); }
336 * A generalized feedback shift register discrete random number generator.
338 * This algorithm avoids multiplication and division and is designed to be
339 * friendly to a pipelined architecture. If the parameters are chosen
340 * correctly, this generator will produce numbers with a very long period and
341 * fairly good apparent entropy, although still not cryptographically strong.
343 * The best way to use this generator is with the predefined mt19937 class.
345 * This algorithm was originally invented by Makoto Matsumoto and
348 * @var word_size The number of bits in each element of the state vector.
349 * @var state_size The degree of recursion.
350 * @var shift_size The period parameter.
351 * @var mask_bits The separation point bit index.
352 * @var parameter_a The last row of the twist matrix.
353 * @var output_u The first right-shift tempering matrix parameter.
354 * @var output_s The first left-shift tempering matrix parameter.
355 * @var output_b The first left-shift tempering matrix mask.
356 * @var output_t The second left-shift tempering matrix parameter.
357 * @var output_c The second left-shift tempering matrix mask.
358 * @var output_l The second right-shift tempering matrix parameter.
360 template<typename _UIntType
, size_t __w
,
361 size_t __n
, size_t __m
, size_t __r
,
362 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
363 _UIntType __b
, size_t __t
,
364 _UIntType __c
, size_t __l
, _UIntType __f
>
365 class mersenne_twister_engine
367 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
368 "substituting _UIntType not an unsigned integral type");
369 static_assert(1u <= __m
&& __m
<= __n
,
370 "template argument substituting __m out of bounds");
371 static_assert(__r
<= __w
, "template argument substituting "
373 static_assert(__u
<= __w
, "template argument substituting "
375 static_assert(__s
<= __w
, "template argument substituting "
377 static_assert(__t
<= __w
, "template argument substituting "
379 static_assert(__l
<= __w
, "template argument substituting "
381 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
382 "template argument substituting __w out of bound");
383 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
384 "template argument substituting __a out of bound");
385 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
386 "template argument substituting __b out of bound");
387 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
388 "template argument substituting __c out of bound");
389 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
390 "template argument substituting __d out of bound");
391 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
392 "template argument substituting __f out of bound");
395 /** The type of the generated random value. */
396 typedef _UIntType result_type
;
399 static constexpr size_t word_size
= __w
;
400 static constexpr size_t state_size
= __n
;
401 static constexpr size_t shift_size
= __m
;
402 static constexpr size_t mask_bits
= __r
;
403 static constexpr result_type xor_mask
= __a
;
404 static constexpr size_t tempering_u
= __u
;
405 static constexpr result_type tempering_d
= __d
;
406 static constexpr size_t tempering_s
= __s
;
407 static constexpr result_type tempering_b
= __b
;
408 static constexpr size_t tempering_t
= __t
;
409 static constexpr result_type tempering_c
= __c
;
410 static constexpr size_t tempering_l
= __l
;
411 static constexpr result_type initialization_multiplier
= __f
;
412 static constexpr result_type default_seed
= 5489u;
414 // constructors and member function
416 mersenne_twister_engine(result_type __sd
= default_seed
)
420 * @brief Constructs a %mersenne_twister_engine random number generator
421 * engine seeded from the seed sequence @p __q.
423 * @param __q the seed sequence.
425 template<typename _Sseq
, typename
= typename
426 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
429 mersenne_twister_engine(_Sseq
& __q
)
433 seed(result_type __sd
= default_seed
);
435 template<typename _Sseq
>
436 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
440 * @brief Gets the smallest possible value in the output range.
442 static constexpr result_type
447 * @brief Gets the largest possible value in the output range.
449 static constexpr result_type
451 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
454 * @brief Discard a sequence of random numbers.
457 discard(unsigned long long __z
)
459 for (; __z
!= 0ULL; --__z
)
467 * @brief Compares two % mersenne_twister_engine random number generator
468 * objects of the same type for equality.
470 * @param __lhs A % mersenne_twister_engine random number generator
472 * @param __rhs Another % mersenne_twister_engine random number
475 * @returns true if the infinite sequences of generated values
476 * would be equal, false otherwise.
479 operator==(const mersenne_twister_engine
& __lhs
,
480 const mersenne_twister_engine
& __rhs
)
481 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
484 * @brief Inserts the current state of a % mersenne_twister_engine
485 * random number generator engine @p __x into the output stream
488 * @param __os An output stream.
489 * @param __x A % mersenne_twister_engine random number generator
492 * @returns The output stream with the state of @p __x inserted or in
495 template<typename _UIntType1
,
496 size_t __w1
, size_t __n1
,
497 size_t __m1
, size_t __r1
,
498 _UIntType1 __a1
, size_t __u1
,
499 _UIntType1 __d1
, size_t __s1
,
500 _UIntType1 __b1
, size_t __t1
,
501 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
502 typename _CharT
, typename _Traits
>
503 friend std::basic_ostream
<_CharT
, _Traits
>&
504 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
505 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
506 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
510 * @brief Extracts the current state of a % mersenne_twister_engine
511 * random number generator engine @p __x from the input stream
514 * @param __is An input stream.
515 * @param __x A % mersenne_twister_engine random number generator
518 * @returns The input stream with the state of @p __x extracted or in
521 template<typename _UIntType1
,
522 size_t __w1
, size_t __n1
,
523 size_t __m1
, size_t __r1
,
524 _UIntType1 __a1
, size_t __u1
,
525 _UIntType1 __d1
, size_t __s1
,
526 _UIntType1 __b1
, size_t __t1
,
527 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
528 typename _CharT
, typename _Traits
>
529 friend std::basic_istream
<_CharT
, _Traits
>&
530 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
531 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
532 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
536 _UIntType _M_x
[state_size
];
541 * @brief Compares two % mersenne_twister_engine random number generator
542 * objects of the same type for inequality.
544 * @param __lhs A % mersenne_twister_engine random number generator
546 * @param __rhs Another % mersenne_twister_engine random number
549 * @returns true if the infinite sequences of generated values
550 * would be different, false otherwise.
552 template<typename _UIntType
, size_t __w
,
553 size_t __n
, size_t __m
, size_t __r
,
554 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
555 _UIntType __b
, size_t __t
,
556 _UIntType __c
, size_t __l
, _UIntType __f
>
558 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
559 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
560 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
561 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
562 { return !(__lhs
== __rhs
); }
566 * @brief The Marsaglia-Zaman generator.
568 * This is a model of a Generalized Fibonacci discrete random number
569 * generator, sometimes referred to as the SWC generator.
571 * A discrete random number generator that produces pseudorandom
574 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
577 * The size of the state is @f$r@f$
578 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
580 * @var _M_x The state of the generator. This is a ring buffer.
581 * @var _M_carry The carry.
582 * @var _M_p Current index of x(i - r).
584 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
585 class subtract_with_carry_engine
587 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
588 "substituting _UIntType not an unsigned integral type");
589 static_assert(0u < __s
&& __s
< __r
,
590 "template argument substituting __s out of bounds");
591 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
592 "template argument substituting __w out of bounds");
595 /** The type of the generated random value. */
596 typedef _UIntType result_type
;
599 static constexpr size_t word_size
= __w
;
600 static constexpr size_t short_lag
= __s
;
601 static constexpr size_t long_lag
= __r
;
602 static constexpr result_type default_seed
= 19780503u;
605 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
606 * random number generator.
609 subtract_with_carry_engine(result_type __sd
= default_seed
)
613 * @brief Constructs a %subtract_with_carry_engine random number engine
614 * seeded from the seed sequence @p __q.
616 * @param __q the seed sequence.
618 template<typename _Sseq
, typename
= typename
619 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
622 subtract_with_carry_engine(_Sseq
& __q
)
626 * @brief Seeds the initial state @f$x_0@f$ of the random number
629 * N1688[4.19] modifies this as follows. If @p __value == 0,
630 * sets value to 19780503. In any case, with a linear
631 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
632 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
633 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
634 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
635 * set carry to 1, otherwise sets carry to 0.
638 seed(result_type __sd
= default_seed
);
641 * @brief Seeds the initial state @f$x_0@f$ of the
642 * % subtract_with_carry_engine random number generator.
644 template<typename _Sseq
>
645 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
649 * @brief Gets the inclusive minimum value of the range of random
650 * integers returned by this generator.
652 static constexpr result_type
657 * @brief Gets the inclusive maximum value of the range of random
658 * integers returned by this generator.
660 static constexpr result_type
662 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
665 * @brief Discard a sequence of random numbers.
668 discard(unsigned long long __z
)
670 for (; __z
!= 0ULL; --__z
)
675 * @brief Gets the next random number in the sequence.
681 * @brief Compares two % subtract_with_carry_engine random number
682 * generator objects of the same type for equality.
684 * @param __lhs A % subtract_with_carry_engine random number generator
686 * @param __rhs Another % subtract_with_carry_engine random number
689 * @returns true if the infinite sequences of generated values
690 * would be equal, false otherwise.
693 operator==(const subtract_with_carry_engine
& __lhs
,
694 const subtract_with_carry_engine
& __rhs
)
695 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
698 * @brief Inserts the current state of a % subtract_with_carry_engine
699 * random number generator engine @p __x into the output stream
702 * @param __os An output stream.
703 * @param __x A % subtract_with_carry_engine random number generator
706 * @returns The output stream with the state of @p __x inserted or in
709 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
710 typename _CharT
, typename _Traits
>
711 friend std::basic_ostream
<_CharT
, _Traits
>&
712 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
713 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
717 * @brief Extracts the current state of a % subtract_with_carry_engine
718 * random number generator engine @p __x from the input stream
721 * @param __is An input stream.
722 * @param __x A % subtract_with_carry_engine random number generator
725 * @returns The input stream with the state of @p __x extracted or in
728 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
729 typename _CharT
, typename _Traits
>
730 friend std::basic_istream
<_CharT
, _Traits
>&
731 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
732 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
736 _UIntType _M_x
[long_lag
];
742 * @brief Compares two % subtract_with_carry_engine random number
743 * generator objects of the same type for inequality.
745 * @param __lhs A % subtract_with_carry_engine random number generator
747 * @param __rhs Another % subtract_with_carry_engine random number
750 * @returns true if the infinite sequences of generated values
751 * would be different, false otherwise.
753 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
755 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
757 const std::subtract_with_carry_engine
<_UIntType
, __w
,
759 { return !(__lhs
== __rhs
); }
763 * Produces random numbers from some base engine by discarding blocks of
766 * 0 <= @p __r <= @p __p
768 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
769 class discard_block_engine
771 static_assert(1 <= __r
&& __r
<= __p
,
772 "template argument substituting __r out of bounds");
775 /** The type of the generated random value. */
776 typedef typename
_RandomNumberEngine::result_type result_type
;
779 static constexpr size_t block_size
= __p
;
780 static constexpr size_t used_block
= __r
;
783 * @brief Constructs a default %discard_block_engine engine.
785 * The underlying engine is default constructed as well.
787 discard_block_engine()
788 : _M_b(), _M_n(0) { }
791 * @brief Copy constructs a %discard_block_engine engine.
793 * Copies an existing base class random number generator.
794 * @param rng An existing (base class) engine object.
797 discard_block_engine(const _RandomNumberEngine
& __rne
)
798 : _M_b(__rne
), _M_n(0) { }
801 * @brief Move constructs a %discard_block_engine engine.
803 * Copies an existing base class random number generator.
804 * @param rng An existing (base class) engine object.
807 discard_block_engine(_RandomNumberEngine
&& __rne
)
808 : _M_b(std::move(__rne
)), _M_n(0) { }
811 * @brief Seed constructs a %discard_block_engine engine.
813 * Constructs the underlying generator engine seeded with @p __s.
814 * @param __s A seed value for the base class engine.
817 discard_block_engine(result_type __s
)
818 : _M_b(__s
), _M_n(0) { }
821 * @brief Generator construct a %discard_block_engine engine.
823 * @param __q A seed sequence.
825 template<typename _Sseq
, typename
= typename
826 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
827 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
830 discard_block_engine(_Sseq
& __q
)
835 * @brief Reseeds the %discard_block_engine object with the default
836 * seed for the underlying base class generator engine.
846 * @brief Reseeds the %discard_block_engine object with the default
847 * seed for the underlying base class generator engine.
850 seed(result_type __s
)
857 * @brief Reseeds the %discard_block_engine object with the given seed
859 * @param __q A seed generator function.
861 template<typename _Sseq
>
870 * @brief Gets a const reference to the underlying generator engine
873 const _RandomNumberEngine
&
878 * @brief Gets the minimum value in the generated random number range.
880 static constexpr result_type
882 { return _RandomNumberEngine::min(); }
885 * @brief Gets the maximum value in the generated random number range.
887 static constexpr result_type
889 { return _RandomNumberEngine::max(); }
892 * @brief Discard a sequence of random numbers.
895 discard(unsigned long long __z
)
897 for (; __z
!= 0ULL; --__z
)
902 * @brief Gets the next value in the generated random number sequence.
908 * @brief Compares two %discard_block_engine random number generator
909 * objects of the same type for equality.
911 * @param __lhs A %discard_block_engine random number generator object.
912 * @param __rhs Another %discard_block_engine random number generator
915 * @returns true if the infinite sequences of generated values
916 * would be equal, false otherwise.
919 operator==(const discard_block_engine
& __lhs
,
920 const discard_block_engine
& __rhs
)
921 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
924 * @brief Inserts the current state of a %discard_block_engine random
925 * number generator engine @p __x into the output stream
928 * @param __os An output stream.
929 * @param __x A %discard_block_engine random number generator engine.
931 * @returns The output stream with the state of @p __x inserted or in
934 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
935 typename _CharT
, typename _Traits
>
936 friend std::basic_ostream
<_CharT
, _Traits
>&
937 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
938 const std::discard_block_engine
<_RandomNumberEngine1
,
942 * @brief Extracts the current state of a % subtract_with_carry_engine
943 * random number generator engine @p __x from the input stream
946 * @param __is An input stream.
947 * @param __x A %discard_block_engine random number generator engine.
949 * @returns The input stream with the state of @p __x extracted or in
952 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
953 typename _CharT
, typename _Traits
>
954 friend std::basic_istream
<_CharT
, _Traits
>&
955 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
956 std::discard_block_engine
<_RandomNumberEngine1
,
960 _RandomNumberEngine _M_b
;
965 * @brief Compares two %discard_block_engine random number generator
966 * objects of the same type for inequality.
968 * @param __lhs A %discard_block_engine random number generator object.
969 * @param __rhs Another %discard_block_engine random number generator
972 * @returns true if the infinite sequences of generated values
973 * would be different, false otherwise.
975 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
977 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
979 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
981 { return !(__lhs
== __rhs
); }
985 * Produces random numbers by combining random numbers from some base
986 * engine to produce random numbers with a specifies number of bits @p __w.
988 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
989 class independent_bits_engine
991 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
992 "substituting _UIntType not an unsigned integral type");
993 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
994 "template argument substituting __w out of bounds");
997 /** The type of the generated random value. */
998 typedef _UIntType result_type
;
1001 * @brief Constructs a default %independent_bits_engine engine.
1003 * The underlying engine is default constructed as well.
1005 independent_bits_engine()
1009 * @brief Copy constructs a %independent_bits_engine engine.
1011 * Copies an existing base class random number generator.
1012 * @param rng An existing (base class) engine object.
1015 independent_bits_engine(const _RandomNumberEngine
& __rne
)
1019 * @brief Move constructs a %independent_bits_engine engine.
1021 * Copies an existing base class random number generator.
1022 * @param rng An existing (base class) engine object.
1025 independent_bits_engine(_RandomNumberEngine
&& __rne
)
1026 : _M_b(std::move(__rne
)) { }
1029 * @brief Seed constructs a %independent_bits_engine engine.
1031 * Constructs the underlying generator engine seeded with @p __s.
1032 * @param __s A seed value for the base class engine.
1035 independent_bits_engine(result_type __s
)
1039 * @brief Generator construct a %independent_bits_engine engine.
1041 * @param __q A seed sequence.
1043 template<typename _Sseq
, typename
= typename
1044 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1045 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1048 independent_bits_engine(_Sseq
& __q
)
1053 * @brief Reseeds the %independent_bits_engine object with the default
1054 * seed for the underlying base class generator engine.
1061 * @brief Reseeds the %independent_bits_engine object with the default
1062 * seed for the underlying base class generator engine.
1065 seed(result_type __s
)
1069 * @brief Reseeds the %independent_bits_engine object with the given
1071 * @param __q A seed generator function.
1073 template<typename _Sseq
>
1079 * @brief Gets a const reference to the underlying generator engine
1082 const _RandomNumberEngine
&
1087 * @brief Gets the minimum value in the generated random number range.
1089 static constexpr result_type
1094 * @brief Gets the maximum value in the generated random number range.
1096 static constexpr result_type
1098 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1101 * @brief Discard a sequence of random numbers.
1104 discard(unsigned long long __z
)
1106 for (; __z
!= 0ULL; --__z
)
1111 * @brief Gets the next value in the generated random number sequence.
1117 * @brief Compares two %independent_bits_engine random number generator
1118 * objects of the same type for equality.
1120 * @param __lhs A %independent_bits_engine random number generator
1122 * @param __rhs Another %independent_bits_engine random number generator
1125 * @returns true if the infinite sequences of generated values
1126 * would be equal, false otherwise.
1129 operator==(const independent_bits_engine
& __lhs
,
1130 const independent_bits_engine
& __rhs
)
1131 { return __lhs
._M_b
== __rhs
._M_b
; }
1134 * @brief Extracts the current state of a % subtract_with_carry_engine
1135 * random number generator engine @p __x from the input stream
1138 * @param __is An input stream.
1139 * @param __x A %independent_bits_engine random number generator
1142 * @returns The input stream with the state of @p __x extracted or in
1145 template<typename _CharT
, typename _Traits
>
1146 friend std::basic_istream
<_CharT
, _Traits
>&
1147 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1148 std::independent_bits_engine
<_RandomNumberEngine
,
1149 __w
, _UIntType
>& __x
)
1156 _RandomNumberEngine _M_b
;
1160 * @brief Compares two %independent_bits_engine random number generator
1161 * objects of the same type for inequality.
1163 * @param __lhs A %independent_bits_engine random number generator
1165 * @param __rhs Another %independent_bits_engine random number generator
1168 * @returns true if the infinite sequences of generated values
1169 * would be different, false otherwise.
1171 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1173 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1175 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1177 { return !(__lhs
== __rhs
); }
1180 * @brief Inserts the current state of a %independent_bits_engine random
1181 * number generator engine @p __x into the output stream @p __os.
1183 * @param __os An output stream.
1184 * @param __x A %independent_bits_engine random number generator engine.
1186 * @returns The output stream with the state of @p __x inserted or in
1189 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1190 typename _CharT
, typename _Traits
>
1191 std::basic_ostream
<_CharT
, _Traits
>&
1192 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1193 const std::independent_bits_engine
<_RandomNumberEngine
,
1194 __w
, _UIntType
>& __x
)
1202 * @brief Produces random numbers by combining random numbers from some
1203 * base engine to produce random numbers with a specifies number of bits
1206 template<typename _RandomNumberEngine
, size_t __k
>
1207 class shuffle_order_engine
1209 static_assert(1u <= __k
, "template argument substituting "
1210 "__k out of bound");
1213 /** The type of the generated random value. */
1214 typedef typename
_RandomNumberEngine::result_type result_type
;
1216 static constexpr size_t table_size
= __k
;
1219 * @brief Constructs a default %shuffle_order_engine engine.
1221 * The underlying engine is default constructed as well.
1223 shuffle_order_engine()
1225 { _M_initialize(); }
1228 * @brief Copy constructs a %shuffle_order_engine engine.
1230 * Copies an existing base class random number generator.
1231 * @param rng An existing (base class) engine object.
1234 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1236 { _M_initialize(); }
1239 * @brief Move constructs a %shuffle_order_engine engine.
1241 * Copies an existing base class random number generator.
1242 * @param rng An existing (base class) engine object.
1245 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1246 : _M_b(std::move(__rne
))
1247 { _M_initialize(); }
1250 * @brief Seed constructs a %shuffle_order_engine engine.
1252 * Constructs the underlying generator engine seeded with @p __s.
1253 * @param __s A seed value for the base class engine.
1256 shuffle_order_engine(result_type __s
)
1258 { _M_initialize(); }
1261 * @brief Generator construct a %shuffle_order_engine engine.
1263 * @param __q A seed sequence.
1265 template<typename _Sseq
, typename
= typename
1266 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1267 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1270 shuffle_order_engine(_Sseq
& __q
)
1272 { _M_initialize(); }
1275 * @brief Reseeds the %shuffle_order_engine object with the default seed
1276 for the underlying base class generator engine.
1286 * @brief Reseeds the %shuffle_order_engine object with the default seed
1287 * for the underlying base class generator engine.
1290 seed(result_type __s
)
1297 * @brief Reseeds the %shuffle_order_engine object with the given seed
1299 * @param __q A seed generator function.
1301 template<typename _Sseq
>
1310 * Gets a const reference to the underlying generator engine object.
1312 const _RandomNumberEngine
&
1317 * Gets the minimum value in the generated random number range.
1319 static constexpr result_type
1321 { return _RandomNumberEngine::min(); }
1324 * Gets the maximum value in the generated random number range.
1326 static constexpr result_type
1328 { return _RandomNumberEngine::max(); }
1331 * Discard a sequence of random numbers.
1334 discard(unsigned long long __z
)
1336 for (; __z
!= 0ULL; --__z
)
1341 * Gets the next value in the generated random number sequence.
1347 * Compares two %shuffle_order_engine random number generator objects
1348 * of the same type for equality.
1350 * @param __lhs A %shuffle_order_engine random number generator object.
1351 * @param __rhs Another %shuffle_order_engine random number generator
1354 * @returns true if the infinite sequences of generated values
1355 * would be equal, false otherwise.
1358 operator==(const shuffle_order_engine
& __lhs
,
1359 const shuffle_order_engine
& __rhs
)
1360 { return __lhs
._M_b
== __rhs
._M_b
; }
1363 * @brief Inserts the current state of a %shuffle_order_engine random
1364 * number generator engine @p __x into the output stream
1367 * @param __os An output stream.
1368 * @param __x A %shuffle_order_engine random number generator engine.
1370 * @returns The output stream with the state of @p __x inserted or in
1373 template<typename _RandomNumberEngine1
, size_t __k1
,
1374 typename _CharT
, typename _Traits
>
1375 friend std::basic_ostream
<_CharT
, _Traits
>&
1376 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1377 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1381 * @brief Extracts the current state of a % subtract_with_carry_engine
1382 * random number generator engine @p __x from the input stream
1385 * @param __is An input stream.
1386 * @param __x A %shuffle_order_engine random number generator engine.
1388 * @returns The input stream with the state of @p __x extracted or in
1391 template<typename _RandomNumberEngine1
, size_t __k1
,
1392 typename _CharT
, typename _Traits
>
1393 friend std::basic_istream
<_CharT
, _Traits
>&
1394 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1395 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1398 void _M_initialize()
1400 for (size_t __i
= 0; __i
< __k
; ++__i
)
1405 _RandomNumberEngine _M_b
;
1406 result_type _M_v
[__k
];
1411 * Compares two %shuffle_order_engine random number generator objects
1412 * of the same type for inequality.
1414 * @param __lhs A %shuffle_order_engine random number generator object.
1415 * @param __rhs Another %shuffle_order_engine random number generator
1418 * @returns true if the infinite sequences of generated values
1419 * would be different, false otherwise.
1421 template<typename _RandomNumberEngine
, size_t __k
>
1423 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1425 const std::shuffle_order_engine
<_RandomNumberEngine
,
1427 { return !(__lhs
== __rhs
); }
1431 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1433 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1437 * An alternative LCR (Lehmer Generator function).
1439 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1443 * The classic Mersenne Twister.
1446 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1447 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1448 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1450 typedef mersenne_twister_engine
<
1456 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1459 * An alternative Mersenne Twister.
1461 typedef mersenne_twister_engine
<
1464 0xb5026f5aa96619e9ULL
, 29,
1465 0x5555555555555555ULL
, 17,
1466 0x71d67fffeda60000ULL
, 37,
1467 0xfff7eee000000000ULL
, 43,
1468 6364136223846793005ULL> mt19937_64
;
1470 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1473 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1476 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1478 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1480 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1482 typedef minstd_rand0 default_random_engine
;
1485 * A standard interface to a platform-specific non-deterministic
1486 * random number generator (if any are available).
1491 /** The type of the generated random value. */
1492 typedef unsigned int result_type
;
1494 // constructors, destructors and member functions
1496 #ifdef _GLIBCXX_USE_RANDOM_TR1
1499 random_device(const std::string
& __token
= "/dev/urandom")
1501 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1502 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1503 std::__throw_runtime_error(__N("random_device::"
1504 "random_device(const std::string&)"));
1508 { std::fclose(_M_file
); }
1513 random_device(const std::string
& __token
= "mt19937")
1514 : _M_mt(_M_strtoul(__token
)) { }
1517 static unsigned long
1518 _M_strtoul(const std::string
& __str
)
1520 unsigned long __ret
= 5489UL;
1521 if (__str
!= "mt19937")
1523 const char* __nptr
= __str
.c_str();
1525 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1526 if (*__nptr
== '\0' || *__endptr
!= '\0')
1527 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1528 "(const std::string&)"));
1539 { return std::numeric_limits
<result_type
>::min(); }
1543 { return std::numeric_limits
<result_type
>::max(); }
1552 #ifdef _GLIBCXX_USE_RANDOM_TR1
1554 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1562 // No copy functions.
1563 random_device(const random_device
&) = delete;
1564 void operator=(const random_device
&) = delete;
1568 #ifdef _GLIBCXX_USE_RANDOM_TR1
1575 /* @} */ // group random_generators
1578 * @addtogroup random_distributions Random Number Distributions
1584 * @addtogroup random_distributions_uniform Uniform Distributions
1585 * @ingroup random_distributions
1590 * @brief Uniform discrete distribution for random numbers.
1591 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1592 * probability throughout the range.
1594 template<typename _IntType
= int>
1595 class uniform_int_distribution
1597 static_assert(std::is_integral
<_IntType
>::value
,
1598 "template argument not an integral type");
1601 /** The type of the range of the distribution. */
1602 typedef _IntType result_type
;
1603 /** Parameter type. */
1606 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1609 param_type(_IntType __a
= 0,
1610 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1611 : _M_a(__a
), _M_b(__b
)
1613 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1625 operator==(const param_type
& __p1
, const param_type
& __p2
)
1626 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1635 * @brief Constructs a uniform distribution object.
1638 uniform_int_distribution(_IntType __a
= 0,
1639 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1640 : _M_param(__a
, __b
)
1644 uniform_int_distribution(const param_type
& __p
)
1649 * @brief Resets the distribution state.
1651 * Does nothing for the uniform integer distribution.
1658 { return _M_param
.a(); }
1662 { return _M_param
.b(); }
1665 * @brief Returns the parameter set of the distribution.
1669 { return _M_param
; }
1672 * @brief Sets the parameter set of the distribution.
1673 * @param __param The new parameter set of the distribution.
1676 param(const param_type
& __param
)
1677 { _M_param
= __param
; }
1680 * @brief Returns the inclusive lower bound of the distribution range.
1684 { return this->a(); }
1687 * @brief Returns the inclusive upper bound of the distribution range.
1691 { return this->b(); }
1694 * @brief Generating functions.
1696 template<typename _UniformRandomNumberGenerator
>
1698 operator()(_UniformRandomNumberGenerator
& __urng
)
1699 { return this->operator()(__urng
, this->param()); }
1701 template<typename _UniformRandomNumberGenerator
>
1703 operator()(_UniformRandomNumberGenerator
& __urng
,
1704 const param_type
& __p
);
1706 param_type _M_param
;
1710 * @brief Return true if two uniform integer distributions have
1711 * the same parameters.
1713 template<typename _IntType
>
1715 operator==(const std::uniform_int_distribution
<_IntType
>& __d1
,
1716 const std::uniform_int_distribution
<_IntType
>& __d2
)
1717 { return __d1
.param() == __d2
.param(); }
1720 * @brief Return true if two uniform integer distributions have
1721 * different parameters.
1723 template<typename _IntType
>
1725 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1726 const std::uniform_int_distribution
<_IntType
>& __d2
)
1727 { return !(__d1
== __d2
); }
1730 * @brief Inserts a %uniform_int_distribution random number
1731 * distribution @p __x into the output stream @p os.
1733 * @param __os An output stream.
1734 * @param __x A %uniform_int_distribution random number distribution.
1736 * @returns The output stream with the state of @p __x inserted or in
1739 template<typename _IntType
, typename _CharT
, typename _Traits
>
1740 std::basic_ostream
<_CharT
, _Traits
>&
1741 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1742 const std::uniform_int_distribution
<_IntType
>&);
1745 * @brief Extracts a %uniform_int_distribution random number distribution
1746 * @p __x from the input stream @p __is.
1748 * @param __is An input stream.
1749 * @param __x A %uniform_int_distribution random number generator engine.
1751 * @returns The input stream with @p __x extracted or in an error state.
1753 template<typename _IntType
, typename _CharT
, typename _Traits
>
1754 std::basic_istream
<_CharT
, _Traits
>&
1755 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1756 std::uniform_int_distribution
<_IntType
>&);
1760 * @brief Uniform continuous distribution for random numbers.
1762 * A continuous random distribution on the range [min, max) with equal
1763 * probability throughout the range. The URNG should be real-valued and
1764 * deliver number in the range [0, 1).
1766 template<typename _RealType
= double>
1767 class uniform_real_distribution
1769 static_assert(std::is_floating_point
<_RealType
>::value
,
1770 "template argument not a floating point type");
1773 /** The type of the range of the distribution. */
1774 typedef _RealType result_type
;
1775 /** Parameter type. */
1778 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1781 param_type(_RealType __a
= _RealType(0),
1782 _RealType __b
= _RealType(1))
1783 : _M_a(__a
), _M_b(__b
)
1785 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1797 operator==(const param_type
& __p1
, const param_type
& __p2
)
1798 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1807 * @brief Constructs a uniform_real_distribution object.
1809 * @param __min [IN] The lower bound of the distribution.
1810 * @param __max [IN] The upper bound of the distribution.
1813 uniform_real_distribution(_RealType __a
= _RealType(0),
1814 _RealType __b
= _RealType(1))
1815 : _M_param(__a
, __b
)
1819 uniform_real_distribution(const param_type
& __p
)
1824 * @brief Resets the distribution state.
1826 * Does nothing for the uniform real distribution.
1833 { return _M_param
.a(); }
1837 { return _M_param
.b(); }
1840 * @brief Returns the parameter set of the distribution.
1844 { return _M_param
; }
1847 * @brief Sets the parameter set of the distribution.
1848 * @param __param The new parameter set of the distribution.
1851 param(const param_type
& __param
)
1852 { _M_param
= __param
; }
1855 * @brief Returns the inclusive lower bound of the distribution range.
1859 { return this->a(); }
1862 * @brief Returns the inclusive upper bound of the distribution range.
1866 { return this->b(); }
1869 * @brief Generating functions.
1871 template<typename _UniformRandomNumberGenerator
>
1873 operator()(_UniformRandomNumberGenerator
& __urng
)
1874 { return this->operator()(__urng
, this->param()); }
1876 template<typename _UniformRandomNumberGenerator
>
1878 operator()(_UniformRandomNumberGenerator
& __urng
,
1879 const param_type
& __p
)
1881 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1883 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1887 param_type _M_param
;
1891 * @brief Return true if two uniform real distributions have
1892 * the same parameters.
1894 template<typename _IntType
>
1896 operator==(const std::uniform_real_distribution
<_IntType
>& __d1
,
1897 const std::uniform_real_distribution
<_IntType
>& __d2
)
1898 { return __d1
.param() == __d2
.param(); }
1901 * @brief Return true if two uniform real distributions have
1902 * different parameters.
1904 template<typename _IntType
>
1906 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1907 const std::uniform_real_distribution
<_IntType
>& __d2
)
1908 { return !(__d1
== __d2
); }
1911 * @brief Inserts a %uniform_real_distribution random number
1912 * distribution @p __x into the output stream @p __os.
1914 * @param __os An output stream.
1915 * @param __x A %uniform_real_distribution random number distribution.
1917 * @returns The output stream with the state of @p __x inserted or in
1920 template<typename _RealType
, typename _CharT
, typename _Traits
>
1921 std::basic_ostream
<_CharT
, _Traits
>&
1922 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1923 const std::uniform_real_distribution
<_RealType
>&);
1926 * @brief Extracts a %uniform_real_distribution random number distribution
1927 * @p __x from the input stream @p __is.
1929 * @param __is An input stream.
1930 * @param __x A %uniform_real_distribution random number generator engine.
1932 * @returns The input stream with @p __x extracted or in an error state.
1934 template<typename _RealType
, typename _CharT
, typename _Traits
>
1935 std::basic_istream
<_CharT
, _Traits
>&
1936 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1937 std::uniform_real_distribution
<_RealType
>&);
1939 /* @} */ // group random_distributions_uniform
1942 * @addtogroup random_distributions_normal Normal Distributions
1943 * @ingroup random_distributions
1948 * @brief A normal continuous distribution for random numbers.
1950 * The formula for the normal probability density function is
1952 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1953 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1956 template<typename _RealType
= double>
1957 class normal_distribution
1959 static_assert(std::is_floating_point
<_RealType
>::value
,
1960 "template argument not a floating point type");
1963 /** The type of the range of the distribution. */
1964 typedef _RealType result_type
;
1965 /** Parameter type. */
1968 typedef normal_distribution
<_RealType
> distribution_type
;
1971 param_type(_RealType __mean
= _RealType(0),
1972 _RealType __stddev
= _RealType(1))
1973 : _M_mean(__mean
), _M_stddev(__stddev
)
1975 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1984 { return _M_stddev
; }
1987 operator==(const param_type
& __p1
, const param_type
& __p2
)
1988 { return (__p1
._M_mean
== __p2
._M_mean
1989 && __p1
._M_stddev
== __p2
._M_stddev
); }
1993 _RealType _M_stddev
;
1998 * Constructs a normal distribution with parameters @f$mean@f$ and
1999 * standard deviation.
2002 normal_distribution(result_type __mean
= result_type(0),
2003 result_type __stddev
= result_type(1))
2004 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2008 normal_distribution(const param_type
& __p
)
2009 : _M_param(__p
), _M_saved_available(false)
2013 * @brief Resets the distribution state.
2017 { _M_saved_available
= false; }
2020 * @brief Returns the mean of the distribution.
2024 { return _M_param
.mean(); }
2027 * @brief Returns the standard deviation of the distribution.
2031 { return _M_param
.stddev(); }
2034 * @brief Returns the parameter set of the distribution.
2038 { return _M_param
; }
2041 * @brief Sets the parameter set of the distribution.
2042 * @param __param The new parameter set of the distribution.
2045 param(const param_type
& __param
)
2046 { _M_param
= __param
; }
2049 * @brief Returns the greatest lower bound value of the distribution.
2053 { return std::numeric_limits
<result_type
>::min(); }
2056 * @brief Returns the least upper bound value of the distribution.
2060 { return std::numeric_limits
<result_type
>::max(); }
2063 * @brief Generating functions.
2065 template<typename _UniformRandomNumberGenerator
>
2067 operator()(_UniformRandomNumberGenerator
& __urng
)
2068 { return this->operator()(__urng
, this->param()); }
2070 template<typename _UniformRandomNumberGenerator
>
2072 operator()(_UniformRandomNumberGenerator
& __urng
,
2073 const param_type
& __p
);
2076 * @brief Return true if two normal distributions have
2077 * the same parameters and the sequences that would
2078 * be generated are equal.
2080 template<typename _RealType1
>
2082 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2083 const std::normal_distribution
<_RealType1
>& __d2
);
2086 * @brief Inserts a %normal_distribution random number distribution
2087 * @p __x into the output stream @p __os.
2089 * @param __os An output stream.
2090 * @param __x A %normal_distribution random number distribution.
2092 * @returns The output stream with the state of @p __x inserted or in
2095 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2096 friend std::basic_ostream
<_CharT
, _Traits
>&
2097 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2098 const std::normal_distribution
<_RealType1
>&);
2101 * @brief Extracts a %normal_distribution random number distribution
2102 * @p __x from the input stream @p __is.
2104 * @param __is An input stream.
2105 * @param __x A %normal_distribution random number generator engine.
2107 * @returns The input stream with @p __x extracted or in an error
2110 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2111 friend std::basic_istream
<_CharT
, _Traits
>&
2112 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2113 std::normal_distribution
<_RealType1
>&);
2116 param_type _M_param
;
2117 result_type _M_saved
;
2118 bool _M_saved_available
;
2122 * @brief Return true if two normal distributions are different.
2124 template<typename _RealType
>
2126 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2127 const std::normal_distribution
<_RealType
>& __d2
)
2128 { return !(__d1
== __d2
); }
2132 * @brief A lognormal_distribution random number distribution.
2134 * The formula for the normal probability mass function is
2136 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2137 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2140 template<typename _RealType
= double>
2141 class lognormal_distribution
2143 static_assert(std::is_floating_point
<_RealType
>::value
,
2144 "template argument not a floating point type");
2147 /** The type of the range of the distribution. */
2148 typedef _RealType result_type
;
2149 /** Parameter type. */
2152 typedef lognormal_distribution
<_RealType
> distribution_type
;
2155 param_type(_RealType __m
= _RealType(0),
2156 _RealType __s
= _RealType(1))
2157 : _M_m(__m
), _M_s(__s
)
2169 operator==(const param_type
& __p1
, const param_type
& __p2
)
2170 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2178 lognormal_distribution(_RealType __m
= _RealType(0),
2179 _RealType __s
= _RealType(1))
2180 : _M_param(__m
, __s
), _M_nd()
2184 lognormal_distribution(const param_type
& __p
)
2185 : _M_param(__p
), _M_nd()
2189 * Resets the distribution state.
2200 { return _M_param
.m(); }
2204 { return _M_param
.s(); }
2207 * @brief Returns the parameter set of the distribution.
2211 { return _M_param
; }
2214 * @brief Sets the parameter set of the distribution.
2215 * @param __param The new parameter set of the distribution.
2218 param(const param_type
& __param
)
2219 { _M_param
= __param
; }
2222 * @brief Returns the greatest lower bound value of the distribution.
2226 { return result_type(0); }
2229 * @brief Returns the least upper bound value of the distribution.
2233 { return std::numeric_limits
<result_type
>::max(); }
2236 * @brief Generating functions.
2238 template<typename _UniformRandomNumberGenerator
>
2240 operator()(_UniformRandomNumberGenerator
& __urng
)
2241 { return this->operator()(__urng
, this->param()); }
2243 template<typename _UniformRandomNumberGenerator
>
2245 operator()(_UniformRandomNumberGenerator
& __urng
,
2246 const param_type
& __p
)
2247 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2250 * @brief Return true if two lognormal distributions have
2251 * the same parameters and the sequences that would
2252 * be generated are equal.
2254 template<typename _RealType1
>
2256 operator==(const std::lognormal_distribution
<_RealType1
>& __d1
,
2257 const std::lognormal_distribution
<_RealType1
>& __d2
)
2258 { return (__d1
.param() == __d2
.param()
2259 && __d1
._M_nd
== __d2
._M_nd
); }
2262 * @brief Inserts a %lognormal_distribution random number distribution
2263 * @p __x into the output stream @p __os.
2265 * @param __os An output stream.
2266 * @param __x A %lognormal_distribution random number distribution.
2268 * @returns The output stream with the state of @p __x inserted or in
2271 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2272 friend std::basic_ostream
<_CharT
, _Traits
>&
2273 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2274 const std::lognormal_distribution
<_RealType1
>&);
2277 * @brief Extracts a %lognormal_distribution random number distribution
2278 * @p __x from the input stream @p __is.
2280 * @param __is An input stream.
2281 * @param __x A %lognormal_distribution random number
2284 * @returns The input stream with @p __x extracted or in an error state.
2286 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2287 friend std::basic_istream
<_CharT
, _Traits
>&
2288 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2289 std::lognormal_distribution
<_RealType1
>&);
2292 param_type _M_param
;
2294 std::normal_distribution
<result_type
> _M_nd
;
2298 * @brief Return true if two lognormal distributions are different.
2300 template<typename _RealType
>
2302 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2303 const std::lognormal_distribution
<_RealType
>& __d2
)
2304 { return !(__d1
== __d2
); }
2308 * @brief A gamma continuous distribution for random numbers.
2310 * The formula for the gamma probability density function is:
2312 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2313 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2316 template<typename _RealType
= double>
2317 class gamma_distribution
2319 static_assert(std::is_floating_point
<_RealType
>::value
,
2320 "template argument not a floating point type");
2323 /** The type of the range of the distribution. */
2324 typedef _RealType result_type
;
2325 /** Parameter type. */
2328 typedef gamma_distribution
<_RealType
> distribution_type
;
2329 friend class gamma_distribution
<_RealType
>;
2332 param_type(_RealType __alpha_val
= _RealType(1),
2333 _RealType __beta_val
= _RealType(1))
2334 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2336 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2342 { return _M_alpha
; }
2349 operator==(const param_type
& __p1
, const param_type
& __p2
)
2350 { return (__p1
._M_alpha
== __p2
._M_alpha
2351 && __p1
._M_beta
== __p2
._M_beta
); }
2360 _RealType _M_malpha
, _M_a2
;
2365 * @brief Constructs a gamma distribution with parameters
2366 * @f$\alpha@f$ and @f$\beta@f$.
2369 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2370 _RealType __beta_val
= _RealType(1))
2371 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2375 gamma_distribution(const param_type
& __p
)
2376 : _M_param(__p
), _M_nd()
2380 * @brief Resets the distribution state.
2387 * @brief Returns the @f$\alpha@f$ of the distribution.
2391 { return _M_param
.alpha(); }
2394 * @brief Returns the @f$\beta@f$ of the distribution.
2398 { return _M_param
.beta(); }
2401 * @brief Returns the parameter set of the distribution.
2405 { return _M_param
; }
2408 * @brief Sets the parameter set of the distribution.
2409 * @param __param The new parameter set of the distribution.
2412 param(const param_type
& __param
)
2413 { _M_param
= __param
; }
2416 * @brief Returns the greatest lower bound value of the distribution.
2420 { return result_type(0); }
2423 * @brief Returns the least upper bound value of the distribution.
2427 { return std::numeric_limits
<result_type
>::max(); }
2430 * @brief Generating functions.
2432 template<typename _UniformRandomNumberGenerator
>
2434 operator()(_UniformRandomNumberGenerator
& __urng
)
2435 { return this->operator()(__urng
, this->param()); }
2437 template<typename _UniformRandomNumberGenerator
>
2439 operator()(_UniformRandomNumberGenerator
& __urng
,
2440 const param_type
& __p
);
2443 * @brief Return true if two gamma distributions have the same
2444 * parameters and the sequences that would be generated
2447 template<typename _RealType1
>
2449 operator==(const std::gamma_distribution
<_RealType1
>& __d1
,
2450 const std::gamma_distribution
<_RealType1
>& __d2
)
2451 { return (__d1
.param() == __d2
.param()
2452 && __d1
._M_nd
== __d2
._M_nd
); }
2455 * @brief Inserts a %gamma_distribution random number distribution
2456 * @p __x into the output stream @p __os.
2458 * @param __os An output stream.
2459 * @param __x A %gamma_distribution random number distribution.
2461 * @returns The output stream with the state of @p __x inserted or in
2464 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2465 friend std::basic_ostream
<_CharT
, _Traits
>&
2466 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2467 const std::gamma_distribution
<_RealType1
>&);
2470 * @brief Extracts a %gamma_distribution random number distribution
2471 * @p __x from the input stream @p __is.
2473 * @param __is An input stream.
2474 * @param __x A %gamma_distribution random number generator engine.
2476 * @returns The input stream with @p __x extracted or in an error state.
2478 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2479 friend std::basic_istream
<_CharT
, _Traits
>&
2480 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2481 std::gamma_distribution
<_RealType1
>&);
2484 param_type _M_param
;
2486 std::normal_distribution
<result_type
> _M_nd
;
2490 * @brief Return true if two gamma distributions are different.
2492 template<typename _RealType
>
2494 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2495 const std::gamma_distribution
<_RealType
>& __d2
)
2496 { return !(__d1
== __d2
); }
2500 * @brief A chi_squared_distribution random number distribution.
2502 * The formula for the normal probability mass function is
2503 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2505 template<typename _RealType
= double>
2506 class chi_squared_distribution
2508 static_assert(std::is_floating_point
<_RealType
>::value
,
2509 "template argument not a floating point type");
2512 /** The type of the range of the distribution. */
2513 typedef _RealType result_type
;
2514 /** Parameter type. */
2517 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2520 param_type(_RealType __n
= _RealType(1))
2529 operator==(const param_type
& __p1
, const param_type
& __p2
)
2530 { return __p1
._M_n
== __p2
._M_n
; }
2537 chi_squared_distribution(_RealType __n
= _RealType(1))
2538 : _M_param(__n
), _M_gd(__n
/ 2)
2542 chi_squared_distribution(const param_type
& __p
)
2543 : _M_param(__p
), _M_gd(__p
.n() / 2)
2547 * @brief Resets the distribution state.
2558 { return _M_param
.n(); }
2561 * @brief Returns the parameter set of the distribution.
2565 { return _M_param
; }
2568 * @brief Sets the parameter set of the distribution.
2569 * @param __param The new parameter set of the distribution.
2572 param(const param_type
& __param
)
2573 { _M_param
= __param
; }
2576 * @brief Returns the greatest lower bound value of the distribution.
2580 { return result_type(0); }
2583 * @brief Returns the least upper bound value of the distribution.
2587 { return std::numeric_limits
<result_type
>::max(); }
2590 * @brief Generating functions.
2592 template<typename _UniformRandomNumberGenerator
>
2594 operator()(_UniformRandomNumberGenerator
& __urng
)
2595 { return 2 * _M_gd(__urng
); }
2597 template<typename _UniformRandomNumberGenerator
>
2599 operator()(_UniformRandomNumberGenerator
& __urng
,
2600 const param_type
& __p
)
2602 typedef typename
std::gamma_distribution
<result_type
>::param_type
2604 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2608 * @brief Return true if two Chi-squared distributions have
2609 * the same parameters and the sequences that would be
2610 * generated are equal.
2612 template<typename _RealType1
>
2614 operator==(const std::chi_squared_distribution
<_RealType1
>& __d1
,
2615 const std::chi_squared_distribution
<_RealType1
>& __d2
)
2616 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
2619 * @brief Inserts a %chi_squared_distribution random number distribution
2620 * @p __x into the output stream @p __os.
2622 * @param __os An output stream.
2623 * @param __x A %chi_squared_distribution random number distribution.
2625 * @returns The output stream with the state of @p __x inserted or in
2628 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2629 friend std::basic_ostream
<_CharT
, _Traits
>&
2630 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2631 const std::chi_squared_distribution
<_RealType1
>&);
2634 * @brief Extracts a %chi_squared_distribution random number distribution
2635 * @p __x from the input stream @p __is.
2637 * @param __is An input stream.
2638 * @param __x A %chi_squared_distribution random number
2641 * @returns The input stream with @p __x extracted or in an error state.
2643 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2644 friend std::basic_istream
<_CharT
, _Traits
>&
2645 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2646 std::chi_squared_distribution
<_RealType1
>&);
2649 param_type _M_param
;
2651 std::gamma_distribution
<result_type
> _M_gd
;
2655 * @brief Return true if two Chi-squared distributions are different.
2657 template<typename _RealType
>
2659 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2660 const std::chi_squared_distribution
<_RealType
>& __d2
)
2661 { return !(__d1
== __d2
); }
2665 * @brief A cauchy_distribution random number distribution.
2667 * The formula for the normal probability mass function is
2668 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2670 template<typename _RealType
= double>
2671 class cauchy_distribution
2673 static_assert(std::is_floating_point
<_RealType
>::value
,
2674 "template argument not a floating point type");
2677 /** The type of the range of the distribution. */
2678 typedef _RealType result_type
;
2679 /** Parameter type. */
2682 typedef cauchy_distribution
<_RealType
> distribution_type
;
2685 param_type(_RealType __a
= _RealType(0),
2686 _RealType __b
= _RealType(1))
2687 : _M_a(__a
), _M_b(__b
)
2699 operator==(const param_type
& __p1
, const param_type
& __p2
)
2700 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2708 cauchy_distribution(_RealType __a
= _RealType(0),
2709 _RealType __b
= _RealType(1))
2710 : _M_param(__a
, __b
)
2714 cauchy_distribution(const param_type
& __p
)
2719 * @brief Resets the distribution state.
2730 { return _M_param
.a(); }
2734 { return _M_param
.b(); }
2737 * @brief Returns the parameter set of the distribution.
2741 { return _M_param
; }
2744 * @brief Sets the parameter set of the distribution.
2745 * @param __param The new parameter set of the distribution.
2748 param(const param_type
& __param
)
2749 { _M_param
= __param
; }
2752 * @brief Returns the greatest lower bound value of the distribution.
2756 { return std::numeric_limits
<result_type
>::min(); }
2759 * @brief Returns the least upper bound value of the distribution.
2763 { return std::numeric_limits
<result_type
>::max(); }
2766 * @brief Generating functions.
2768 template<typename _UniformRandomNumberGenerator
>
2770 operator()(_UniformRandomNumberGenerator
& __urng
)
2771 { return this->operator()(__urng
, this->param()); }
2773 template<typename _UniformRandomNumberGenerator
>
2775 operator()(_UniformRandomNumberGenerator
& __urng
,
2776 const param_type
& __p
);
2779 param_type _M_param
;
2783 * @brief Return true if two Cauchy distributions have
2784 * the same parameters.
2786 template<typename _RealType
>
2788 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2789 const std::cauchy_distribution
<_RealType
>& __d2
)
2790 { return __d1
.param() == __d2
.param(); }
2793 * @brief Return true if two Cauchy distributions have
2794 * different parameters.
2796 template<typename _RealType
>
2798 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2799 const std::cauchy_distribution
<_RealType
>& __d2
)
2800 { return !(__d1
== __d2
); }
2803 * @brief Inserts a %cauchy_distribution random number distribution
2804 * @p __x into the output stream @p __os.
2806 * @param __os An output stream.
2807 * @param __x A %cauchy_distribution random number distribution.
2809 * @returns The output stream with the state of @p __x inserted or in
2812 template<typename _RealType
, typename _CharT
, typename _Traits
>
2813 std::basic_ostream
<_CharT
, _Traits
>&
2814 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2815 const std::cauchy_distribution
<_RealType
>&);
2818 * @brief Extracts a %cauchy_distribution random number distribution
2819 * @p __x from the input stream @p __is.
2821 * @param __is An input stream.
2822 * @param __x A %cauchy_distribution random number
2825 * @returns The input stream with @p __x extracted or in an error state.
2827 template<typename _RealType
, typename _CharT
, typename _Traits
>
2828 std::basic_istream
<_CharT
, _Traits
>&
2829 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2830 std::cauchy_distribution
<_RealType
>&);
2834 * @brief A fisher_f_distribution random number distribution.
2836 * The formula for the normal probability mass function is
2838 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2839 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2840 * (1 + \frac{mx}{n})^{-(m+n)/2}
2843 template<typename _RealType
= double>
2844 class fisher_f_distribution
2846 static_assert(std::is_floating_point
<_RealType
>::value
,
2847 "template argument not a floating point type");
2850 /** The type of the range of the distribution. */
2851 typedef _RealType result_type
;
2852 /** Parameter type. */
2855 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2858 param_type(_RealType __m
= _RealType(1),
2859 _RealType __n
= _RealType(1))
2860 : _M_m(__m
), _M_n(__n
)
2872 operator==(const param_type
& __p1
, const param_type
& __p2
)
2873 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
2881 fisher_f_distribution(_RealType __m
= _RealType(1),
2882 _RealType __n
= _RealType(1))
2883 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2887 fisher_f_distribution(const param_type
& __p
)
2888 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2892 * @brief Resets the distribution state.
2906 { return _M_param
.m(); }
2910 { return _M_param
.n(); }
2913 * @brief Returns the parameter set of the distribution.
2917 { return _M_param
; }
2920 * @brief Sets the parameter set of the distribution.
2921 * @param __param The new parameter set of the distribution.
2924 param(const param_type
& __param
)
2925 { _M_param
= __param
; }
2928 * @brief Returns the greatest lower bound value of the distribution.
2932 { return result_type(0); }
2935 * @brief Returns the least upper bound value of the distribution.
2939 { return std::numeric_limits
<result_type
>::max(); }
2942 * @brief Generating functions.
2944 template<typename _UniformRandomNumberGenerator
>
2946 operator()(_UniformRandomNumberGenerator
& __urng
)
2947 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2949 template<typename _UniformRandomNumberGenerator
>
2951 operator()(_UniformRandomNumberGenerator
& __urng
,
2952 const param_type
& __p
)
2954 typedef typename
std::gamma_distribution
<result_type
>::param_type
2956 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2957 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2961 * @brief Return true if two Fisher f distributions have
2962 * the same parameters and the sequences that would
2963 * be generated are equal.
2965 template<typename _RealType1
>
2967 operator==(const std::fisher_f_distribution
<_RealType1
>& __d1
,
2968 const std::fisher_f_distribution
<_RealType1
>& __d2
)
2969 { return (__d1
.param() == __d2
.param()
2970 && __d1
._M_gd_x
== __d2
._M_gd_x
2971 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
2974 * @brief Inserts a %fisher_f_distribution random number distribution
2975 * @p __x into the output stream @p __os.
2977 * @param __os An output stream.
2978 * @param __x A %fisher_f_distribution random number distribution.
2980 * @returns The output stream with the state of @p __x inserted or in
2983 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2984 friend std::basic_ostream
<_CharT
, _Traits
>&
2985 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2986 const std::fisher_f_distribution
<_RealType1
>&);
2989 * @brief Extracts a %fisher_f_distribution random number distribution
2990 * @p __x from the input stream @p __is.
2992 * @param __is An input stream.
2993 * @param __x A %fisher_f_distribution random number
2996 * @returns The input stream with @p __x extracted or in an error state.
2998 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2999 friend std::basic_istream
<_CharT
, _Traits
>&
3000 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3001 std::fisher_f_distribution
<_RealType1
>&);
3004 param_type _M_param
;
3006 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3010 * @brief Return true if two Fisher f distributions are diferent.
3012 template<typename _RealType
>
3014 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3015 const std::fisher_f_distribution
<_RealType
>& __d2
)
3016 { return !(__d1
== __d2
); }
3019 * @brief A student_t_distribution random number distribution.
3021 * The formula for the normal probability mass function is:
3023 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3024 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3027 template<typename _RealType
= double>
3028 class student_t_distribution
3030 static_assert(std::is_floating_point
<_RealType
>::value
,
3031 "template argument not a floating point type");
3034 /** The type of the range of the distribution. */
3035 typedef _RealType result_type
;
3036 /** Parameter type. */
3039 typedef student_t_distribution
<_RealType
> distribution_type
;
3042 param_type(_RealType __n
= _RealType(1))
3051 operator==(const param_type
& __p1
, const param_type
& __p2
)
3052 { return __p1
._M_n
== __p2
._M_n
; }
3059 student_t_distribution(_RealType __n
= _RealType(1))
3060 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3064 student_t_distribution(const param_type
& __p
)
3065 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3069 * @brief Resets the distribution state.
3083 { return _M_param
.n(); }
3086 * @brief Returns the parameter set of the distribution.
3090 { return _M_param
; }
3093 * @brief Sets the parameter set of the distribution.
3094 * @param __param The new parameter set of the distribution.
3097 param(const param_type
& __param
)
3098 { _M_param
= __param
; }
3101 * @brief Returns the greatest lower bound value of the distribution.
3105 { return std::numeric_limits
<result_type
>::min(); }
3108 * @brief Returns the least upper bound value of the distribution.
3112 { return std::numeric_limits
<result_type
>::max(); }
3115 * @brief Generating functions.
3117 template<typename _UniformRandomNumberGenerator
>
3119 operator()(_UniformRandomNumberGenerator
& __urng
)
3120 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3122 template<typename _UniformRandomNumberGenerator
>
3124 operator()(_UniformRandomNumberGenerator
& __urng
,
3125 const param_type
& __p
)
3127 typedef typename
std::gamma_distribution
<result_type
>::param_type
3130 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3131 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3135 * @brief Return true if two Student t distributions have
3136 * the same parameters and the sequences that would
3137 * be generated are equal.
3139 template<typename _RealType1
>
3141 operator==(const std::student_t_distribution
<_RealType1
>& __d1
,
3142 const std::student_t_distribution
<_RealType1
>& __d2
)
3143 { return (__d1
.param() == __d2
.param()
3144 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3147 * @brief Inserts a %student_t_distribution random number distribution
3148 * @p __x into the output stream @p __os.
3150 * @param __os An output stream.
3151 * @param __x A %student_t_distribution random number distribution.
3153 * @returns The output stream with the state of @p __x inserted or in
3156 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3157 friend std::basic_ostream
<_CharT
, _Traits
>&
3158 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3159 const std::student_t_distribution
<_RealType1
>&);
3162 * @brief Extracts a %student_t_distribution random number distribution
3163 * @p __x from the input stream @p __is.
3165 * @param __is An input stream.
3166 * @param __x A %student_t_distribution random number
3169 * @returns The input stream with @p __x extracted or in an error state.
3171 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3172 friend std::basic_istream
<_CharT
, _Traits
>&
3173 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3174 std::student_t_distribution
<_RealType1
>&);
3177 param_type _M_param
;
3179 std::normal_distribution
<result_type
> _M_nd
;
3180 std::gamma_distribution
<result_type
> _M_gd
;
3184 * @brief Return true if two Student t distributions are different.
3186 template<typename _RealType
>
3188 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3189 const std::student_t_distribution
<_RealType
>& __d2
)
3190 { return !(__d1
== __d2
); }
3193 /* @} */ // group random_distributions_normal
3196 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3197 * @ingroup random_distributions
3202 * @brief A Bernoulli random number distribution.
3204 * Generates a sequence of true and false values with likelihood @f$p@f$
3205 * that true will come up and @f$(1 - p)@f$ that false will appear.
3207 class bernoulli_distribution
3210 /** The type of the range of the distribution. */
3211 typedef bool result_type
;
3212 /** Parameter type. */
3215 typedef bernoulli_distribution distribution_type
;
3218 param_type(double __p
= 0.5)
3221 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3229 operator==(const param_type
& __p1
, const param_type
& __p2
)
3230 { return __p1
._M_p
== __p2
._M_p
; }
3238 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3240 * @param __p [IN] The likelihood of a true result being returned.
3241 * Must be in the interval @f$[0, 1]@f$.
3244 bernoulli_distribution(double __p
= 0.5)
3249 bernoulli_distribution(const param_type
& __p
)
3254 * @brief Resets the distribution state.
3256 * Does nothing for a Bernoulli distribution.
3262 * @brief Returns the @p p parameter of the distribution.
3266 { return _M_param
.p(); }
3269 * @brief Returns the parameter set of the distribution.
3273 { return _M_param
; }
3276 * @brief Sets the parameter set of the distribution.
3277 * @param __param The new parameter set of the distribution.
3280 param(const param_type
& __param
)
3281 { _M_param
= __param
; }
3284 * @brief Returns the greatest lower bound value of the distribution.
3288 { return std::numeric_limits
<result_type
>::min(); }
3291 * @brief Returns the least upper bound value of the distribution.
3295 { return std::numeric_limits
<result_type
>::max(); }
3298 * @brief Generating functions.
3300 template<typename _UniformRandomNumberGenerator
>
3302 operator()(_UniformRandomNumberGenerator
& __urng
)
3303 { return this->operator()(__urng
, this->param()); }
3305 template<typename _UniformRandomNumberGenerator
>
3307 operator()(_UniformRandomNumberGenerator
& __urng
,
3308 const param_type
& __p
)
3310 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3312 if ((__aurng() - __aurng
.min())
3313 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3319 param_type _M_param
;
3323 * @brief Return true if two Bernoulli distributions have
3324 * the same parameters.
3327 operator==(const std::bernoulli_distribution
& __d1
,
3328 const std::bernoulli_distribution
& __d2
)
3329 { return __d1
.param() == __d2
.param(); }
3332 * @brief Return true if two Bernoulli distributions have
3333 * different parameters.
3336 operator!=(const std::bernoulli_distribution
& __d1
,
3337 const std::bernoulli_distribution
& __d2
)
3338 { return !(__d1
== __d2
); }
3341 * @brief Inserts a %bernoulli_distribution random number distribution
3342 * @p __x into the output stream @p __os.
3344 * @param __os An output stream.
3345 * @param __x A %bernoulli_distribution random number distribution.
3347 * @returns The output stream with the state of @p __x inserted or in
3350 template<typename _CharT
, typename _Traits
>
3351 std::basic_ostream
<_CharT
, _Traits
>&
3352 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3353 const std::bernoulli_distribution
&);
3356 * @brief Extracts a %bernoulli_distribution random number distribution
3357 * @p __x from the input stream @p __is.
3359 * @param __is An input stream.
3360 * @param __x A %bernoulli_distribution random number generator engine.
3362 * @returns The input stream with @p __x extracted or in an error state.
3364 template<typename _CharT
, typename _Traits
>
3365 std::basic_istream
<_CharT
, _Traits
>&
3366 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3367 std::bernoulli_distribution
& __x
)
3371 __x
.param(bernoulli_distribution::param_type(__p
));
3377 * @brief A discrete binomial random number distribution.
3379 * The formula for the binomial probability density function is
3380 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3381 * and @f$p@f$ are the parameters of the distribution.
3383 template<typename _IntType
= int>
3384 class binomial_distribution
3386 static_assert(std::is_integral
<_IntType
>::value
,
3387 "template argument not an integral type");
3390 /** The type of the range of the distribution. */
3391 typedef _IntType result_type
;
3392 /** Parameter type. */
3395 typedef binomial_distribution
<_IntType
> distribution_type
;
3396 friend class binomial_distribution
<_IntType
>;
3399 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3400 : _M_t(__t
), _M_p(__p
)
3402 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3417 operator==(const param_type
& __p1
, const param_type
& __p2
)
3418 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3428 #if _GLIBCXX_USE_C99_MATH_TR1
3429 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3430 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3435 // constructors and member function
3437 binomial_distribution(_IntType __t
= _IntType(1),
3439 : _M_param(__t
, __p
), _M_nd()
3443 binomial_distribution(const param_type
& __p
)
3444 : _M_param(__p
), _M_nd()
3448 * @brief Resets the distribution state.
3455 * @brief Returns the distribution @p t parameter.
3459 { return _M_param
.t(); }
3462 * @brief Returns the distribution @p p parameter.
3466 { return _M_param
.p(); }
3469 * @brief Returns the parameter set of the distribution.
3473 { return _M_param
; }
3476 * @brief Sets the parameter set of the distribution.
3477 * @param __param The new parameter set of the distribution.
3480 param(const param_type
& __param
)
3481 { _M_param
= __param
; }
3484 * @brief Returns the greatest lower bound value of the distribution.
3491 * @brief Returns the least upper bound value of the distribution.
3495 { return _M_param
.t(); }
3498 * @brief Generating functions.
3500 template<typename _UniformRandomNumberGenerator
>
3502 operator()(_UniformRandomNumberGenerator
& __urng
)
3503 { return this->operator()(__urng
, this->param()); }
3505 template<typename _UniformRandomNumberGenerator
>
3507 operator()(_UniformRandomNumberGenerator
& __urng
,
3508 const param_type
& __p
);
3511 * @brief Return true if two binomial distributions have
3512 * the same parameters and the sequences that would
3513 * be generated are equal.
3515 template<typename _IntType1
>
3517 operator==(const std::binomial_distribution
<_IntType1
>& __d1
,
3518 const std::binomial_distribution
<_IntType1
>& __d2
)
3519 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3520 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
3522 { return __d1
.param() == __d2
.param(); }
3526 * @brief Inserts a %binomial_distribution random number distribution
3527 * @p __x into the output stream @p __os.
3529 * @param __os An output stream.
3530 * @param __x A %binomial_distribution random number distribution.
3532 * @returns The output stream with the state of @p __x inserted or in
3535 template<typename _IntType1
,
3536 typename _CharT
, typename _Traits
>
3537 friend std::basic_ostream
<_CharT
, _Traits
>&
3538 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3539 const std::binomial_distribution
<_IntType1
>&);
3542 * @brief Extracts a %binomial_distribution random number distribution
3543 * @p __x from the input stream @p __is.
3545 * @param __is An input stream.
3546 * @param __x A %binomial_distribution random number generator engine.
3548 * @returns The input stream with @p __x extracted or in an error
3551 template<typename _IntType1
,
3552 typename _CharT
, typename _Traits
>
3553 friend std::basic_istream
<_CharT
, _Traits
>&
3554 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3555 std::binomial_distribution
<_IntType1
>&);
3558 template<typename _UniformRandomNumberGenerator
>
3560 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3562 param_type _M_param
;
3564 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3565 std::normal_distribution
<double> _M_nd
;
3569 * @brief Return true if two binomial distributions are different.
3571 template<typename _IntType
>
3573 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3574 const std::binomial_distribution
<_IntType
>& __d2
)
3575 { return !(__d1
== __d2
); }
3579 * @brief A discrete geometric random number distribution.
3581 * The formula for the geometric probability density function is
3582 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the
3585 template<typename _IntType
= int>
3586 class geometric_distribution
3588 static_assert(std::is_integral
<_IntType
>::value
,
3589 "template argument not an integral type");
3592 /** The type of the range of the distribution. */
3593 typedef _IntType result_type
;
3594 /** Parameter type. */
3597 typedef geometric_distribution
<_IntType
> distribution_type
;
3598 friend class geometric_distribution
<_IntType
>;
3601 param_type(double __p
= 0.5)
3604 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3614 operator==(const param_type
& __p1
, const param_type
& __p2
)
3615 { return __p1
._M_p
== __p2
._M_p
; }
3620 { _M_log_p
= std::log(_M_p
); }
3627 // constructors and member function
3629 geometric_distribution(double __p
= 0.5)
3634 geometric_distribution(const param_type
& __p
)
3639 * @brief Resets the distribution state.
3641 * Does nothing for the geometric distribution.
3647 * @brief Returns the distribution parameter @p p.
3651 { return _M_param
.p(); }
3654 * @brief Returns the parameter set of the distribution.
3658 { return _M_param
; }
3661 * @brief Sets the parameter set of the distribution.
3662 * @param __param The new parameter set of the distribution.
3665 param(const param_type
& __param
)
3666 { _M_param
= __param
; }
3669 * @brief Returns the greatest lower bound value of the distribution.
3676 * @brief Returns the least upper bound value of the distribution.
3680 { return std::numeric_limits
<result_type
>::max(); }
3683 * @brief Generating functions.
3685 template<typename _UniformRandomNumberGenerator
>
3687 operator()(_UniformRandomNumberGenerator
& __urng
)
3688 { return this->operator()(__urng
, this->param()); }
3690 template<typename _UniformRandomNumberGenerator
>
3692 operator()(_UniformRandomNumberGenerator
& __urng
,
3693 const param_type
& __p
);
3696 param_type _M_param
;
3700 * @brief Return true if two geometric distributions have
3701 * the same parameters.
3703 template<typename _IntType
>
3705 operator==(const std::geometric_distribution
<_IntType
>& __d1
,
3706 const std::geometric_distribution
<_IntType
>& __d2
)
3707 { return __d1
.param() == __d2
.param(); }
3710 * @brief Return true if two geometric distributions have
3711 * different parameters.
3713 template<typename _IntType
>
3715 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
3716 const std::geometric_distribution
<_IntType
>& __d2
)
3717 { return !(__d1
== __d2
); }
3720 * @brief Inserts a %geometric_distribution random number distribution
3721 * @p __x into the output stream @p __os.
3723 * @param __os An output stream.
3724 * @param __x A %geometric_distribution random number distribution.
3726 * @returns The output stream with the state of @p __x inserted or in
3729 template<typename _IntType
,
3730 typename _CharT
, typename _Traits
>
3731 std::basic_ostream
<_CharT
, _Traits
>&
3732 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3733 const std::geometric_distribution
<_IntType
>&);
3736 * @brief Extracts a %geometric_distribution random number distribution
3737 * @p __x from the input stream @p __is.
3739 * @param __is An input stream.
3740 * @param __x A %geometric_distribution random number generator engine.
3742 * @returns The input stream with @p __x extracted or in an error state.
3744 template<typename _IntType
,
3745 typename _CharT
, typename _Traits
>
3746 std::basic_istream
<_CharT
, _Traits
>&
3747 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3748 std::geometric_distribution
<_IntType
>&);
3752 * @brief A negative_binomial_distribution random number distribution.
3754 * The formula for the negative binomial probability mass function is
3755 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3756 * and @f$p@f$ are the parameters of the distribution.
3758 template<typename _IntType
= int>
3759 class negative_binomial_distribution
3761 static_assert(std::is_integral
<_IntType
>::value
,
3762 "template argument not an integral type");
3765 /** The type of the range of the distribution. */
3766 typedef _IntType result_type
;
3767 /** Parameter type. */
3770 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3773 param_type(_IntType __k
= 1, double __p
= 0.5)
3774 : _M_k(__k
), _M_p(__p
)
3786 operator==(const param_type
& __p1
, const param_type
& __p2
)
3787 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
3795 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3796 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3800 negative_binomial_distribution(const param_type
& __p
)
3801 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3805 * @brief Resets the distribution state.
3812 * @brief Return the @f$k@f$ parameter of the distribution.
3816 { return _M_param
.k(); }
3819 * @brief Return the @f$p@f$ parameter of the distribution.
3823 { return _M_param
.p(); }
3826 * @brief Returns the parameter set of the distribution.
3830 { return _M_param
; }
3833 * @brief Sets the parameter set of the distribution.
3834 * @param __param The new parameter set of the distribution.
3837 param(const param_type
& __param
)
3838 { _M_param
= __param
; }
3841 * @brief Returns the greatest lower bound value of the distribution.
3845 { return result_type(0); }
3848 * @brief Returns the least upper bound value of the distribution.
3852 { return std::numeric_limits
<result_type
>::max(); }
3855 * @brief Generating functions.
3857 template<typename _UniformRandomNumberGenerator
>
3859 operator()(_UniformRandomNumberGenerator
& __urng
);
3861 template<typename _UniformRandomNumberGenerator
>
3863 operator()(_UniformRandomNumberGenerator
& __urng
,
3864 const param_type
& __p
);
3867 * @brief Return true if two negative binomial distributions have
3868 * the same parameters and the sequences that would be
3869 * generated are equal.
3871 template<typename _IntType1
>
3873 operator==(const std::negative_binomial_distribution
<_IntType1
>& __d1
,
3874 const std::negative_binomial_distribution
<_IntType1
>& __d2
)
3875 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
3878 * @brief Inserts a %negative_binomial_distribution random
3879 * number distribution @p __x into the output stream @p __os.
3881 * @param __os An output stream.
3882 * @param __x A %negative_binomial_distribution random number
3885 * @returns The output stream with the state of @p __x inserted or in
3888 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3889 friend std::basic_ostream
<_CharT
, _Traits
>&
3890 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3891 const std::negative_binomial_distribution
<_IntType1
>&);
3894 * @brief Extracts a %negative_binomial_distribution random number
3895 * distribution @p __x from the input stream @p __is.
3897 * @param __is An input stream.
3898 * @param __x A %negative_binomial_distribution random number
3901 * @returns The input stream with @p __x extracted or in an error state.
3903 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3904 friend std::basic_istream
<_CharT
, _Traits
>&
3905 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3906 std::negative_binomial_distribution
<_IntType1
>&);
3909 param_type _M_param
;
3911 std::gamma_distribution
<double> _M_gd
;
3915 * @brief Return true if two negative binomial distributions are different.
3917 template<typename _IntType
>
3919 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3920 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3921 { return !(__d1
== __d2
); }
3924 /* @} */ // group random_distributions_bernoulli
3927 * @addtogroup random_distributions_poisson Poisson Distributions
3928 * @ingroup random_distributions
3933 * @brief A discrete Poisson random number distribution.
3935 * The formula for the Poisson probability density function is
3936 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3937 * parameter of the distribution.
3939 template<typename _IntType
= int>
3940 class poisson_distribution
3942 static_assert(std::is_integral
<_IntType
>::value
,
3943 "template argument not an integral type");
3946 /** The type of the range of the distribution. */
3947 typedef _IntType result_type
;
3948 /** Parameter type. */
3951 typedef poisson_distribution
<_IntType
> distribution_type
;
3952 friend class poisson_distribution
<_IntType
>;
3955 param_type(double __mean
= 1.0)
3958 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3967 operator==(const param_type
& __p1
, const param_type
& __p2
)
3968 { return __p1
._M_mean
== __p2
._M_mean
; }
3971 // Hosts either log(mean) or the threshold of the simple method.
3978 #if _GLIBCXX_USE_C99_MATH_TR1
3979 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3983 // constructors and member function
3985 poisson_distribution(double __mean
= 1.0)
3986 : _M_param(__mean
), _M_nd()
3990 poisson_distribution(const param_type
& __p
)
3991 : _M_param(__p
), _M_nd()
3995 * @brief Resets the distribution state.
4002 * @brief Returns the distribution parameter @p mean.
4006 { return _M_param
.mean(); }
4009 * @brief Returns the parameter set of the distribution.
4013 { return _M_param
; }
4016 * @brief Sets the parameter set of the distribution.
4017 * @param __param The new parameter set of the distribution.
4020 param(const param_type
& __param
)
4021 { _M_param
= __param
; }
4024 * @brief Returns the greatest lower bound value of the distribution.
4031 * @brief Returns the least upper bound value of the distribution.
4035 { return std::numeric_limits
<result_type
>::max(); }
4038 * @brief Generating functions.
4040 template<typename _UniformRandomNumberGenerator
>
4042 operator()(_UniformRandomNumberGenerator
& __urng
)
4043 { return this->operator()(__urng
, this->param()); }
4045 template<typename _UniformRandomNumberGenerator
>
4047 operator()(_UniformRandomNumberGenerator
& __urng
,
4048 const param_type
& __p
);
4051 * @brief Return true if two Poisson distributions have the same
4052 * parameters and the sequences that would be generated
4055 template<typename _IntType1
>
4057 operator==(const std::poisson_distribution
<_IntType1
>& __d1
,
4058 const std::poisson_distribution
<_IntType1
>& __d2
)
4059 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4060 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
4062 { return __d1
.param() == __d2
.param(); }
4066 * @brief Inserts a %poisson_distribution random number distribution
4067 * @p __x into the output stream @p __os.
4069 * @param __os An output stream.
4070 * @param __x A %poisson_distribution random number distribution.
4072 * @returns The output stream with the state of @p __x inserted or in
4075 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4076 friend std::basic_ostream
<_CharT
, _Traits
>&
4077 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4078 const std::poisson_distribution
<_IntType1
>&);
4081 * @brief Extracts a %poisson_distribution random number distribution
4082 * @p __x from the input stream @p __is.
4084 * @param __is An input stream.
4085 * @param __x A %poisson_distribution random number generator engine.
4087 * @returns The input stream with @p __x extracted or in an error
4090 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4091 friend std::basic_istream
<_CharT
, _Traits
>&
4092 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4093 std::poisson_distribution
<_IntType1
>&);
4096 param_type _M_param
;
4098 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4099 std::normal_distribution
<double> _M_nd
;
4103 * @brief Return true if two Poisson distributions are different.
4105 template<typename _IntType
>
4107 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4108 const std::poisson_distribution
<_IntType
>& __d2
)
4109 { return !(__d1
== __d2
); }
4113 * @brief An exponential continuous distribution for random numbers.
4115 * The formula for the exponential probability density function is
4116 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4118 * <table border=1 cellpadding=10 cellspacing=0>
4119 * <caption align=top>Distribution Statistics</caption>
4120 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4121 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4122 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4123 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4124 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4127 template<typename _RealType
= double>
4128 class exponential_distribution
4130 static_assert(std::is_floating_point
<_RealType
>::value
,
4131 "template argument not a floating point type");
4134 /** The type of the range of the distribution. */
4135 typedef _RealType result_type
;
4136 /** Parameter type. */
4139 typedef exponential_distribution
<_RealType
> distribution_type
;
4142 param_type(_RealType __lambda
= _RealType(1))
4143 : _M_lambda(__lambda
)
4145 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4150 { return _M_lambda
; }
4153 operator==(const param_type
& __p1
, const param_type
& __p2
)
4154 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4157 _RealType _M_lambda
;
4162 * @brief Constructs an exponential distribution with inverse scale
4163 * parameter @f$\lambda@f$.
4166 exponential_distribution(const result_type
& __lambda
= result_type(1))
4167 : _M_param(__lambda
)
4171 exponential_distribution(const param_type
& __p
)
4176 * @brief Resets the distribution state.
4178 * Has no effect on exponential distributions.
4184 * @brief Returns the inverse scale parameter of the distribution.
4188 { return _M_param
.lambda(); }
4191 * @brief Returns the parameter set of the distribution.
4195 { return _M_param
; }
4198 * @brief Sets the parameter set of the distribution.
4199 * @param __param The new parameter set of the distribution.
4202 param(const param_type
& __param
)
4203 { _M_param
= __param
; }
4206 * @brief Returns the greatest lower bound value of the distribution.
4210 { return result_type(0); }
4213 * @brief Returns the least upper bound value of the distribution.
4217 { return std::numeric_limits
<result_type
>::max(); }
4220 * @brief Generating functions.
4222 template<typename _UniformRandomNumberGenerator
>
4224 operator()(_UniformRandomNumberGenerator
& __urng
)
4225 { return this->operator()(__urng
, this->param()); }
4227 template<typename _UniformRandomNumberGenerator
>
4229 operator()(_UniformRandomNumberGenerator
& __urng
,
4230 const param_type
& __p
)
4232 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4234 return -std::log(__aurng()) / __p
.lambda();
4238 param_type _M_param
;
4242 * @brief Return true if two exponential distributions have the same
4245 template<typename _RealType
>
4247 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
4248 const std::exponential_distribution
<_RealType
>& __d2
)
4249 { return __d1
.param() == __d2
.param(); }
4252 * @brief Return true if two exponential distributions have different
4255 template<typename _RealType
>
4257 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4258 const std::exponential_distribution
<_RealType
>& __d2
)
4259 { return !(__d1
== __d2
); }
4262 * @brief Inserts a %exponential_distribution random number distribution
4263 * @p __x into the output stream @p __os.
4265 * @param __os An output stream.
4266 * @param __x A %exponential_distribution random number distribution.
4268 * @returns The output stream with the state of @p __x inserted or in
4271 template<typename _RealType
, typename _CharT
, typename _Traits
>
4272 std::basic_ostream
<_CharT
, _Traits
>&
4273 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4274 const std::exponential_distribution
<_RealType
>&);
4277 * @brief Extracts a %exponential_distribution random number distribution
4278 * @p __x from the input stream @p __is.
4280 * @param __is An input stream.
4281 * @param __x A %exponential_distribution random number
4284 * @returns The input stream with @p __x extracted or in an error state.
4286 template<typename _RealType
, typename _CharT
, typename _Traits
>
4287 std::basic_istream
<_CharT
, _Traits
>&
4288 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4289 std::exponential_distribution
<_RealType
>&);
4293 * @brief A weibull_distribution random number distribution.
4295 * The formula for the normal probability density function is:
4297 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4298 * \exp{(-(\frac{x}{\beta})^\alpha)}
4301 template<typename _RealType
= double>
4302 class weibull_distribution
4304 static_assert(std::is_floating_point
<_RealType
>::value
,
4305 "template argument not a floating point type");
4308 /** The type of the range of the distribution. */
4309 typedef _RealType result_type
;
4310 /** Parameter type. */
4313 typedef weibull_distribution
<_RealType
> distribution_type
;
4316 param_type(_RealType __a
= _RealType(1),
4317 _RealType __b
= _RealType(1))
4318 : _M_a(__a
), _M_b(__b
)
4330 operator==(const param_type
& __p1
, const param_type
& __p2
)
4331 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4339 weibull_distribution(_RealType __a
= _RealType(1),
4340 _RealType __b
= _RealType(1))
4341 : _M_param(__a
, __b
)
4345 weibull_distribution(const param_type
& __p
)
4350 * @brief Resets the distribution state.
4357 * @brief Return the @f$a@f$ parameter of the distribution.
4361 { return _M_param
.a(); }
4364 * @brief Return the @f$b@f$ parameter of the distribution.
4368 { return _M_param
.b(); }
4371 * @brief Returns the parameter set of the distribution.
4375 { return _M_param
; }
4378 * @brief Sets the parameter set of the distribution.
4379 * @param __param The new parameter set of the distribution.
4382 param(const param_type
& __param
)
4383 { _M_param
= __param
; }
4386 * @brief Returns the greatest lower bound value of the distribution.
4390 { return result_type(0); }
4393 * @brief Returns the least upper bound value of the distribution.
4397 { return std::numeric_limits
<result_type
>::max(); }
4400 * @brief Generating functions.
4402 template<typename _UniformRandomNumberGenerator
>
4404 operator()(_UniformRandomNumberGenerator
& __urng
)
4405 { return this->operator()(__urng
, this->param()); }
4407 template<typename _UniformRandomNumberGenerator
>
4409 operator()(_UniformRandomNumberGenerator
& __urng
,
4410 const param_type
& __p
);
4413 param_type _M_param
;
4417 * @brief Return true if two Weibull distributions have the same
4420 template<typename _RealType
>
4422 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4423 const std::weibull_distribution
<_RealType
>& __d2
)
4424 { return __d1
.param() == __d2
.param(); }
4427 * @brief Return true if two Weibull distributions have different
4430 template<typename _RealType
>
4432 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4433 const std::weibull_distribution
<_RealType
>& __d2
)
4434 { return !(__d1
== __d2
); }
4437 * @brief Inserts a %weibull_distribution random number distribution
4438 * @p __x into the output stream @p __os.
4440 * @param __os An output stream.
4441 * @param __x A %weibull_distribution random number distribution.
4443 * @returns The output stream with the state of @p __x inserted or in
4446 template<typename _RealType
, typename _CharT
, typename _Traits
>
4447 std::basic_ostream
<_CharT
, _Traits
>&
4448 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4449 const std::weibull_distribution
<_RealType
>&);
4452 * @brief Extracts a %weibull_distribution random number distribution
4453 * @p __x from the input stream @p __is.
4455 * @param __is An input stream.
4456 * @param __x A %weibull_distribution random number
4459 * @returns The input stream with @p __x extracted or in an error state.
4461 template<typename _RealType
, typename _CharT
, typename _Traits
>
4462 std::basic_istream
<_CharT
, _Traits
>&
4463 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4464 std::weibull_distribution
<_RealType
>&);
4468 * @brief A extreme_value_distribution random number distribution.
4470 * The formula for the normal probability mass function is
4472 * p(x|a,b) = \frac{1}{b}
4473 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4476 template<typename _RealType
= double>
4477 class extreme_value_distribution
4479 static_assert(std::is_floating_point
<_RealType
>::value
,
4480 "template argument not a floating point type");
4483 /** The type of the range of the distribution. */
4484 typedef _RealType result_type
;
4485 /** Parameter type. */
4488 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4491 param_type(_RealType __a
= _RealType(0),
4492 _RealType __b
= _RealType(1))
4493 : _M_a(__a
), _M_b(__b
)
4505 operator==(const param_type
& __p1
, const param_type
& __p2
)
4506 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4514 extreme_value_distribution(_RealType __a
= _RealType(0),
4515 _RealType __b
= _RealType(1))
4516 : _M_param(__a
, __b
)
4520 extreme_value_distribution(const param_type
& __p
)
4525 * @brief Resets the distribution state.
4532 * @brief Return the @f$a@f$ parameter of the distribution.
4536 { return _M_param
.a(); }
4539 * @brief Return the @f$b@f$ parameter of the distribution.
4543 { return _M_param
.b(); }
4546 * @brief Returns the parameter set of the distribution.
4550 { return _M_param
; }
4553 * @brief Sets the parameter set of the distribution.
4554 * @param __param The new parameter set of the distribution.
4557 param(const param_type
& __param
)
4558 { _M_param
= __param
; }
4561 * @brief Returns the greatest lower bound value of the distribution.
4565 { return std::numeric_limits
<result_type
>::min(); }
4568 * @brief Returns the least upper bound value of the distribution.
4572 { return std::numeric_limits
<result_type
>::max(); }
4575 * @brief Generating functions.
4577 template<typename _UniformRandomNumberGenerator
>
4579 operator()(_UniformRandomNumberGenerator
& __urng
)
4580 { return this->operator()(__urng
, this->param()); }
4582 template<typename _UniformRandomNumberGenerator
>
4584 operator()(_UniformRandomNumberGenerator
& __urng
,
4585 const param_type
& __p
);
4588 param_type _M_param
;
4592 * @brief Return true if two extreme value distributions have the same
4595 template<typename _RealType
>
4597 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4598 const std::extreme_value_distribution
<_RealType
>& __d2
)
4599 { return __d1
.param() == __d2
.param(); }
4602 * @brief Return true if two extreme value distributions have different
4605 template<typename _RealType
>
4607 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
4608 const std::extreme_value_distribution
<_RealType
>& __d2
)
4609 { return !(__d1
== __d2
); }
4612 * @brief Inserts a %extreme_value_distribution random number distribution
4613 * @p __x into the output stream @p __os.
4615 * @param __os An output stream.
4616 * @param __x A %extreme_value_distribution random number distribution.
4618 * @returns The output stream with the state of @p __x inserted or in
4621 template<typename _RealType
, typename _CharT
, typename _Traits
>
4622 std::basic_ostream
<_CharT
, _Traits
>&
4623 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4624 const std::extreme_value_distribution
<_RealType
>&);
4627 * @brief Extracts a %extreme_value_distribution random number
4628 * distribution @p __x from the input stream @p __is.
4630 * @param __is An input stream.
4631 * @param __x A %extreme_value_distribution random number
4634 * @returns The input stream with @p __x extracted or in an error state.
4636 template<typename _RealType
, typename _CharT
, typename _Traits
>
4637 std::basic_istream
<_CharT
, _Traits
>&
4638 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4639 std::extreme_value_distribution
<_RealType
>&);
4643 * @brief A discrete_distribution random number distribution.
4645 * The formula for the discrete probability mass function is
4648 template<typename _IntType
= int>
4649 class discrete_distribution
4651 static_assert(std::is_integral
<_IntType
>::value
,
4652 "template argument not an integral type");
4655 /** The type of the range of the distribution. */
4656 typedef _IntType result_type
;
4657 /** Parameter type. */
4660 typedef discrete_distribution
<_IntType
> distribution_type
;
4661 friend class discrete_distribution
<_IntType
>;
4664 : _M_prob(), _M_cp()
4667 template<typename _InputIterator
>
4668 param_type(_InputIterator __wbegin
,
4669 _InputIterator __wend
)
4670 : _M_prob(__wbegin
, __wend
), _M_cp()
4671 { _M_initialize(); }
4673 param_type(initializer_list
<double> __wil
)
4674 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4675 { _M_initialize(); }
4677 template<typename _Func
>
4678 param_type(size_t __nw
, double __xmin
, double __xmax
,
4681 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4682 param_type(const param_type
&) = default;
4683 param_type
& operator=(const param_type
&) = default;
4686 probabilities() const
4687 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
4690 operator==(const param_type
& __p1
, const param_type
& __p2
)
4691 { return __p1
._M_prob
== __p2
._M_prob
; }
4697 std::vector
<double> _M_prob
;
4698 std::vector
<double> _M_cp
;
4701 discrete_distribution()
4705 template<typename _InputIterator
>
4706 discrete_distribution(_InputIterator __wbegin
,
4707 _InputIterator __wend
)
4708 : _M_param(__wbegin
, __wend
)
4711 discrete_distribution(initializer_list
<double> __wl
)
4715 template<typename _Func
>
4716 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4718 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4722 discrete_distribution(const param_type
& __p
)
4727 * @brief Resets the distribution state.
4734 * @brief Returns the probabilities of the distribution.
4737 probabilities() const
4739 return _M_param
._M_prob
.empty()
4740 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
4744 * @brief Returns the parameter set of the distribution.
4748 { return _M_param
; }
4751 * @brief Sets the parameter set of the distribution.
4752 * @param __param The new parameter set of the distribution.
4755 param(const param_type
& __param
)
4756 { _M_param
= __param
; }
4759 * @brief Returns the greatest lower bound value of the distribution.
4763 { return result_type(0); }
4766 * @brief Returns the least upper bound value of the distribution.
4771 return _M_param
._M_prob
.empty()
4772 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
4776 * @brief Generating functions.
4778 template<typename _UniformRandomNumberGenerator
>
4780 operator()(_UniformRandomNumberGenerator
& __urng
)
4781 { return this->operator()(__urng
, this->param()); }
4783 template<typename _UniformRandomNumberGenerator
>
4785 operator()(_UniformRandomNumberGenerator
& __urng
,
4786 const param_type
& __p
);
4789 * @brief Inserts a %discrete_distribution random number distribution
4790 * @p __x into the output stream @p __os.
4792 * @param __os An output stream.
4793 * @param __x A %discrete_distribution random number distribution.
4795 * @returns The output stream with the state of @p __x inserted or in
4798 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4799 friend std::basic_ostream
<_CharT
, _Traits
>&
4800 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4801 const std::discrete_distribution
<_IntType1
>&);
4804 * @brief Extracts a %discrete_distribution random number distribution
4805 * @p __x from the input stream @p __is.
4807 * @param __is An input stream.
4808 * @param __x A %discrete_distribution random number
4811 * @returns The input stream with @p __x extracted or in an error
4814 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4815 friend std::basic_istream
<_CharT
, _Traits
>&
4816 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4817 std::discrete_distribution
<_IntType1
>&);
4820 param_type _M_param
;
4824 * @brief Return true if two discrete distributions have the same
4827 template<typename _IntType
>
4829 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4830 const std::discrete_distribution
<_IntType
>& __d2
)
4831 { return __d1
.param() == __d2
.param(); }
4834 * @brief Return true if two discrete distributions have different
4837 template<typename _IntType
>
4839 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
4840 const std::discrete_distribution
<_IntType
>& __d2
)
4841 { return !(__d1
== __d2
); }
4845 * @brief A piecewise_constant_distribution random number distribution.
4847 * The formula for the piecewise constant probability mass function is
4850 template<typename _RealType
= double>
4851 class piecewise_constant_distribution
4853 static_assert(std::is_floating_point
<_RealType
>::value
,
4854 "template argument not a floating point type");
4857 /** The type of the range of the distribution. */
4858 typedef _RealType result_type
;
4859 /** Parameter type. */
4862 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4863 friend class piecewise_constant_distribution
<_RealType
>;
4866 : _M_int(), _M_den(), _M_cp()
4869 template<typename _InputIteratorB
, typename _InputIteratorW
>
4870 param_type(_InputIteratorB __bfirst
,
4871 _InputIteratorB __bend
,
4872 _InputIteratorW __wbegin
);
4874 template<typename _Func
>
4875 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4877 template<typename _Func
>
4878 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4881 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4882 param_type(const param_type
&) = default;
4883 param_type
& operator=(const param_type
&) = default;
4885 std::vector
<_RealType
>
4890 std::vector
<_RealType
> __tmp(2);
4891 __tmp
[1] = _RealType(1);
4900 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
4903 operator==(const param_type
& __p1
, const param_type
& __p2
)
4904 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
4910 std::vector
<_RealType
> _M_int
;
4911 std::vector
<double> _M_den
;
4912 std::vector
<double> _M_cp
;
4916 piecewise_constant_distribution()
4920 template<typename _InputIteratorB
, typename _InputIteratorW
>
4921 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4922 _InputIteratorB __bend
,
4923 _InputIteratorW __wbegin
)
4924 : _M_param(__bfirst
, __bend
, __wbegin
)
4927 template<typename _Func
>
4928 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4930 : _M_param(__bl
, __fw
)
4933 template<typename _Func
>
4934 piecewise_constant_distribution(size_t __nw
,
4935 _RealType __xmin
, _RealType __xmax
,
4937 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4941 piecewise_constant_distribution(const param_type
& __p
)
4946 * @brief Resets the distribution state.
4953 * @brief Returns a vector of the intervals.
4955 std::vector
<_RealType
>
4958 if (_M_param
._M_int
.empty())
4960 std::vector
<_RealType
> __tmp(2);
4961 __tmp
[1] = _RealType(1);
4965 return _M_param
._M_int
;
4969 * @brief Returns a vector of the probability densities.
4974 return _M_param
._M_den
.empty()
4975 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
4979 * @brief Returns the parameter set of the distribution.
4983 { return _M_param
; }
4986 * @brief Sets the parameter set of the distribution.
4987 * @param __param The new parameter set of the distribution.
4990 param(const param_type
& __param
)
4991 { _M_param
= __param
; }
4994 * @brief Returns the greatest lower bound value of the distribution.
4999 return _M_param
._M_int
.empty()
5000 ? result_type(0) : _M_param
._M_int
.front();
5004 * @brief Returns the least upper bound value of the distribution.
5009 return _M_param
._M_int
.empty()
5010 ? result_type(1) : _M_param
._M_int
.back();
5014 * @brief Generating functions.
5016 template<typename _UniformRandomNumberGenerator
>
5018 operator()(_UniformRandomNumberGenerator
& __urng
)
5019 { return this->operator()(__urng
, this->param()); }
5021 template<typename _UniformRandomNumberGenerator
>
5023 operator()(_UniformRandomNumberGenerator
& __urng
,
5024 const param_type
& __p
);
5027 * @brief Inserts a %piecewise_constan_distribution random
5028 * number distribution @p __x into the output stream @p __os.
5030 * @param __os An output stream.
5031 * @param __x A %piecewise_constan_distribution random number
5034 * @returns The output stream with the state of @p __x inserted or in
5037 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5038 friend std::basic_ostream
<_CharT
, _Traits
>&
5039 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
5040 const std::piecewise_constant_distribution
<_RealType1
>&);
5043 * @brief Extracts a %piecewise_constan_distribution random
5044 * number distribution @p __x from the input stream @p __is.
5046 * @param __is An input stream.
5047 * @param __x A %piecewise_constan_distribution random number
5050 * @returns The input stream with @p __x extracted or in an error
5053 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5054 friend std::basic_istream
<_CharT
, _Traits
>&
5055 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5056 std::piecewise_constant_distribution
<_RealType1
>&);
5059 param_type _M_param
;
5063 * @brief Return true if two piecewise constant distributions have the
5066 template<typename _RealType
>
5068 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5069 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5070 { return __d1
.param() == __d2
.param(); }
5073 * @brief Return true if two piecewise constant distributions have
5074 * different parameters.
5076 template<typename _RealType
>
5078 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5079 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5080 { return !(__d1
== __d2
); }
5084 * @brief A piecewise_linear_distribution random number distribution.
5086 * The formula for the piecewise linear probability mass function is
5089 template<typename _RealType
= double>
5090 class piecewise_linear_distribution
5092 static_assert(std::is_floating_point
<_RealType
>::value
,
5093 "template argument not a floating point type");
5096 /** The type of the range of the distribution. */
5097 typedef _RealType result_type
;
5098 /** Parameter type. */
5101 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5102 friend class piecewise_linear_distribution
<_RealType
>;
5105 : _M_int(), _M_den(), _M_cp(), _M_m()
5108 template<typename _InputIteratorB
, typename _InputIteratorW
>
5109 param_type(_InputIteratorB __bfirst
,
5110 _InputIteratorB __bend
,
5111 _InputIteratorW __wbegin
);
5113 template<typename _Func
>
5114 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5116 template<typename _Func
>
5117 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5120 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5121 param_type(const param_type
&) = default;
5122 param_type
& operator=(const param_type
&) = default;
5124 std::vector
<_RealType
>
5129 std::vector
<_RealType
> __tmp(2);
5130 __tmp
[1] = _RealType(1);
5139 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5142 operator==(const param_type
& __p1
, const param_type
& __p2
)
5143 { return (__p1
._M_int
== __p2
._M_int
5144 && __p1
._M_den
== __p2
._M_den
); }
5150 std::vector
<_RealType
> _M_int
;
5151 std::vector
<double> _M_den
;
5152 std::vector
<double> _M_cp
;
5153 std::vector
<double> _M_m
;
5157 piecewise_linear_distribution()
5161 template<typename _InputIteratorB
, typename _InputIteratorW
>
5162 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5163 _InputIteratorB __bend
,
5164 _InputIteratorW __wbegin
)
5165 : _M_param(__bfirst
, __bend
, __wbegin
)
5168 template<typename _Func
>
5169 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5171 : _M_param(__bl
, __fw
)
5174 template<typename _Func
>
5175 piecewise_linear_distribution(size_t __nw
,
5176 _RealType __xmin
, _RealType __xmax
,
5178 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5182 piecewise_linear_distribution(const param_type
& __p
)
5187 * Resets the distribution state.
5194 * @brief Return the intervals of the distribution.
5196 std::vector
<_RealType
>
5199 if (_M_param
._M_int
.empty())
5201 std::vector
<_RealType
> __tmp(2);
5202 __tmp
[1] = _RealType(1);
5206 return _M_param
._M_int
;
5210 * @brief Return a vector of the probability densities of the
5216 return _M_param
._M_den
.empty()
5217 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5221 * @brief Returns the parameter set of the distribution.
5225 { return _M_param
; }
5228 * @brief Sets the parameter set of the distribution.
5229 * @param __param The new parameter set of the distribution.
5232 param(const param_type
& __param
)
5233 { _M_param
= __param
; }
5236 * @brief Returns the greatest lower bound value of the distribution.
5241 return _M_param
._M_int
.empty()
5242 ? result_type(0) : _M_param
._M_int
.front();
5246 * @brief Returns the least upper bound value of the distribution.
5251 return _M_param
._M_int
.empty()
5252 ? result_type(1) : _M_param
._M_int
.back();
5256 * @brief Generating functions.
5258 template<typename _UniformRandomNumberGenerator
>
5260 operator()(_UniformRandomNumberGenerator
& __urng
)
5261 { return this->operator()(__urng
, this->param()); }
5263 template<typename _UniformRandomNumberGenerator
>
5265 operator()(_UniformRandomNumberGenerator
& __urng
,
5266 const param_type
& __p
);
5269 * @brief Inserts a %piecewise_linear_distribution random number
5270 * distribution @p __x into the output stream @p __os.
5272 * @param __os An output stream.
5273 * @param __x A %piecewise_linear_distribution random number
5276 * @returns The output stream with the state of @p __x inserted or in
5279 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5280 friend std::basic_ostream
<_CharT
, _Traits
>&
5281 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
5282 const std::piecewise_linear_distribution
<_RealType1
>&);
5285 * @brief Extracts a %piecewise_linear_distribution random number
5286 * distribution @p __x from the input stream @p __is.
5288 * @param __is An input stream.
5289 * @param __x A %piecewise_linear_distribution random number
5292 * @returns The input stream with @p __x extracted or in an error
5295 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5296 friend std::basic_istream
<_CharT
, _Traits
>&
5297 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5298 std::piecewise_linear_distribution
<_RealType1
>&);
5301 param_type _M_param
;
5305 * @brief Return true if two piecewise linear distributions have the
5308 template<typename _RealType
>
5310 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5311 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5312 { return __d1
.param() == __d2
.param(); }
5315 * @brief Return true if two piecewise linear distributions have
5316 * different parameters.
5318 template<typename _RealType
>
5320 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5321 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5322 { return !(__d1
== __d2
); }
5325 /* @} */ // group random_distributions_poisson
5327 /* @} */ // group random_distributions
5330 * @addtogroup random_utilities Random Number Utilities
5336 * @brief The seed_seq class generates sequences of seeds for random
5337 * number generators.
5343 /** The type of the seed vales. */
5344 typedef uint_least32_t result_type
;
5346 /** Default constructor. */
5351 template<typename _IntType
>
5352 seed_seq(std::initializer_list
<_IntType
> il
);
5354 template<typename _InputIterator
>
5355 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5357 // generating functions
5358 template<typename _RandomAccessIterator
>
5360 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5362 // property functions
5364 { return _M_v
.size(); }
5366 template<typename OutputIterator
>
5368 param(OutputIterator __dest
) const
5369 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5373 std::vector
<result_type
> _M_v
;
5376 /* @} */ // group random_utilities
5378 /* @} */ // group random