1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 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 * Do not attempt to use it directly. @headername{random}
36 namespace std
_GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 // [26.4] Random number generation
43 * @defgroup random Random Number Generation
46 * A facility for generating random numbers on selected distributions.
51 * @brief A function template for converting the output of a (integral)
52 * uniform random number generator to a floatng point result in the range
55 template<typename _RealType
, size_t __bits
,
56 typename _UniformRandomNumberGenerator
>
58 generate_canonical(_UniformRandomNumberGenerator
& __g
);
60 _GLIBCXX_END_NAMESPACE_VERSION
63 * Implementation-space details.
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 template<typename _UIntType
, size_t __w
,
70 bool = __w
< static_cast<size_t>
71 (std::numeric_limits
<_UIntType
>::digits
)>
73 { static const _UIntType __value
= 0; };
75 template<typename _UIntType
, size_t __w
>
76 struct _Shift
<_UIntType
, __w
, true>
77 { static const _UIntType __value
= _UIntType(1) << __w
; };
79 template<typename _Tp
, _Tp __m
, _Tp __a
, _Tp __c
, bool>
82 // Dispatch based on modulus value to prevent divide-by-zero compile-time
83 // errors when m == 0.
84 template<typename _Tp
, _Tp __m
, _Tp __a
= 1, _Tp __c
= 0>
87 { return _Mod
<_Tp
, __m
, __a
, __c
, __m
== 0>::__calc(__x
); }
90 * An adaptor class for converting the output of any Generator into
91 * the input for a specific Distribution.
93 template<typename _Engine
, typename _DInputType
>
98 _Adaptor(_Engine
& __g
)
103 { return _DInputType(0); }
107 { return _DInputType(1); }
110 * Converts a value generated by the adapted random number generator
111 * into a value in the input domain for the dependent random number
117 return std::generate_canonical
<_DInputType
,
118 std::numeric_limits
<_DInputType
>::digits
,
126 _GLIBCXX_END_NAMESPACE_VERSION
127 } // namespace __detail
129 _GLIBCXX_BEGIN_NAMESPACE_VERSION
132 * @addtogroup random_generators Random Number Generators
135 * These classes define objects which provide random or pseudorandom
136 * numbers, either from a discrete or a continuous interval. The
137 * random number generator supplied as a part of this library are
138 * all uniform random number generators which provide a sequence of
139 * random number uniformly distributed over their range.
141 * A number generator is a function object with an operator() that
142 * takes zero arguments and returns a number.
144 * A compliant random number generator must satisfy the following
145 * requirements. <table border=1 cellpadding=10 cellspacing=0>
146 * <caption align=top>Random Number Generator Requirements</caption>
147 * <tr><td>To be documented.</td></tr> </table>
153 * @brief A model of a linear congruential random number generator.
155 * A random number generator that produces pseudorandom numbers via
158 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
161 * The template parameter @p _UIntType must be an unsigned integral type
162 * large enough to store values up to (__m-1). If the template parameter
163 * @p __m is 0, the modulus @p __m used is
164 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
165 * parameters @p __a and @p __c must be less than @p __m.
167 * The size of the state is @f$1@f$.
169 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
170 class linear_congruential_engine
172 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
173 "substituting _UIntType not an unsigned integral type");
174 static_assert(__m
== 0u || (__a
< __m
&& __c
< __m
),
175 "template argument substituting __m out of bounds");
178 /** The type of the generated random value. */
179 typedef _UIntType result_type
;
181 /** The multiplier. */
182 static constexpr result_type multiplier
= __a
;
184 static constexpr result_type increment
= __c
;
186 static constexpr result_type modulus
= __m
;
187 static constexpr result_type default_seed
= 1u;
190 * @brief Constructs a %linear_congruential_engine random number
191 * generator engine with seed @p __s. The default seed value
194 * @param __s The initial seed value.
197 linear_congruential_engine(result_type __s
= default_seed
)
201 * @brief Constructs a %linear_congruential_engine random number
202 * generator engine seeded from the seed sequence @p __q.
204 * @param __q the seed sequence.
206 template<typename _Sseq
, typename
= typename
207 std::enable_if
<!std::is_same
<_Sseq
, linear_congruential_engine
>::value
>
210 linear_congruential_engine(_Sseq
& __q
)
214 * @brief Reseeds the %linear_congruential_engine random number generator
215 * engine sequence to the seed @p __s.
217 * @param __s The new seed.
220 seed(result_type __s
= default_seed
);
223 * @brief Reseeds the %linear_congruential_engine random number generator
225 * sequence using values from the seed sequence @p __q.
227 * @param __q the seed sequence.
229 template<typename _Sseq
>
230 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
234 * @brief Gets the smallest possible value in the output range.
236 * The minimum depends on the @p __c parameter: if it is zero, the
237 * minimum generated must be > 0, otherwise 0 is allowed.
239 static constexpr result_type
241 { return __c
== 0u ? 1u : 0u; }
244 * @brief Gets the largest possible value in the output range.
246 static constexpr result_type
251 * @brief Discard a sequence of random numbers.
254 discard(unsigned long long __z
)
256 for (; __z
!= 0ULL; --__z
)
261 * @brief Gets the next random number in the sequence.
266 _M_x
= __detail::__mod
<_UIntType
, __m
, __a
, __c
>(_M_x
);
271 * @brief Compares two linear congruential random number generator
272 * objects of the same type for equality.
274 * @param __lhs A linear congruential random number generator object.
275 * @param __rhs Another linear congruential random number generator
278 * @returns true if the infinite sequences of generated values
279 * would be equal, false otherwise.
282 operator==(const linear_congruential_engine
& __lhs
,
283 const linear_congruential_engine
& __rhs
)
284 { return __lhs
._M_x
== __rhs
._M_x
; }
287 * @brief Writes the textual representation of the state x(i) of x to
290 * @param __os The output stream.
291 * @param __lcr A % linear_congruential_engine random number generator.
294 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
295 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
296 friend std::basic_ostream
<_CharT
, _Traits
>&
297 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
298 const std::linear_congruential_engine
<_UIntType1
,
302 * @brief Sets the state of the engine by reading its textual
303 * representation from @p __is.
305 * The textual representation must have been previously written using
306 * an output stream whose imbued locale and whose type's template
307 * specialization arguments _CharT and _Traits were the same as those
310 * @param __is The input stream.
311 * @param __lcr A % linear_congruential_engine random number generator.
314 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
315 _UIntType1 __m1
, typename _CharT
, typename _Traits
>
316 friend std::basic_istream
<_CharT
, _Traits
>&
317 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
318 std::linear_congruential_engine
<_UIntType1
, __a1
,
326 * @brief Compares two linear congruential random number generator
327 * objects of the same type for inequality.
329 * @param __lhs A linear congruential random number generator object.
330 * @param __rhs Another linear congruential random number generator
333 * @returns true if the infinite sequences of generated values
334 * would be different, false otherwise.
336 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
338 operator!=(const std::linear_congruential_engine
<_UIntType
, __a
,
340 const std::linear_congruential_engine
<_UIntType
, __a
,
342 { return !(__lhs
== __rhs
); }
346 * A generalized feedback shift register discrete random number generator.
348 * This algorithm avoids multiplication and division and is designed to be
349 * friendly to a pipelined architecture. If the parameters are chosen
350 * correctly, this generator will produce numbers with a very long period and
351 * fairly good apparent entropy, although still not cryptographically strong.
353 * The best way to use this generator is with the predefined mt19937 class.
355 * This algorithm was originally invented by Makoto Matsumoto and
358 * @var word_size The number of bits in each element of the state vector.
359 * @var state_size The degree of recursion.
360 * @var shift_size The period parameter.
361 * @var mask_bits The separation point bit index.
362 * @var parameter_a The last row of the twist matrix.
363 * @var output_u The first right-shift tempering matrix parameter.
364 * @var output_s The first left-shift tempering matrix parameter.
365 * @var output_b The first left-shift tempering matrix mask.
366 * @var output_t The second left-shift tempering matrix parameter.
367 * @var output_c The second left-shift tempering matrix mask.
368 * @var output_l The second right-shift tempering matrix parameter.
370 template<typename _UIntType
, size_t __w
,
371 size_t __n
, size_t __m
, size_t __r
,
372 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
373 _UIntType __b
, size_t __t
,
374 _UIntType __c
, size_t __l
, _UIntType __f
>
375 class mersenne_twister_engine
377 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
378 "substituting _UIntType not an unsigned integral type");
379 static_assert(1u <= __m
&& __m
<= __n
,
380 "template argument substituting __m out of bounds");
381 static_assert(__r
<= __w
, "template argument substituting "
383 static_assert(__u
<= __w
, "template argument substituting "
385 static_assert(__s
<= __w
, "template argument substituting "
387 static_assert(__t
<= __w
, "template argument substituting "
389 static_assert(__l
<= __w
, "template argument substituting "
391 static_assert(__w
<= std::numeric_limits
<_UIntType
>::digits
,
392 "template argument substituting __w out of bound");
393 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
394 "template argument substituting __a out of bound");
395 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
396 "template argument substituting __b out of bound");
397 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
398 "template argument substituting __c out of bound");
399 static_assert(__d
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
400 "template argument substituting __d out of bound");
401 static_assert(__f
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
402 "template argument substituting __f out of bound");
405 /** The type of the generated random value. */
406 typedef _UIntType result_type
;
409 static constexpr size_t word_size
= __w
;
410 static constexpr size_t state_size
= __n
;
411 static constexpr size_t shift_size
= __m
;
412 static constexpr size_t mask_bits
= __r
;
413 static constexpr result_type xor_mask
= __a
;
414 static constexpr size_t tempering_u
= __u
;
415 static constexpr result_type tempering_d
= __d
;
416 static constexpr size_t tempering_s
= __s
;
417 static constexpr result_type tempering_b
= __b
;
418 static constexpr size_t tempering_t
= __t
;
419 static constexpr result_type tempering_c
= __c
;
420 static constexpr size_t tempering_l
= __l
;
421 static constexpr result_type initialization_multiplier
= __f
;
422 static constexpr result_type default_seed
= 5489u;
424 // constructors and member function
426 mersenne_twister_engine(result_type __sd
= default_seed
)
430 * @brief Constructs a %mersenne_twister_engine random number generator
431 * engine seeded from the seed sequence @p __q.
433 * @param __q the seed sequence.
435 template<typename _Sseq
, typename
= typename
436 std::enable_if
<!std::is_same
<_Sseq
, mersenne_twister_engine
>::value
>
439 mersenne_twister_engine(_Sseq
& __q
)
443 seed(result_type __sd
= default_seed
);
445 template<typename _Sseq
>
446 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
450 * @brief Gets the smallest possible value in the output range.
452 static constexpr result_type
457 * @brief Gets the largest possible value in the output range.
459 static constexpr result_type
461 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
464 * @brief Discard a sequence of random numbers.
467 discard(unsigned long long __z
)
469 for (; __z
!= 0ULL; --__z
)
477 * @brief Compares two % mersenne_twister_engine random number generator
478 * objects of the same type for equality.
480 * @param __lhs A % mersenne_twister_engine random number generator
482 * @param __rhs Another % mersenne_twister_engine random number
485 * @returns true if the infinite sequences of generated values
486 * would be equal, false otherwise.
489 operator==(const mersenne_twister_engine
& __lhs
,
490 const mersenne_twister_engine
& __rhs
)
491 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
494 * @brief Inserts the current state of a % mersenne_twister_engine
495 * random number generator engine @p __x into the output stream
498 * @param __os An output stream.
499 * @param __x A % mersenne_twister_engine random number generator
502 * @returns The output stream with the state of @p __x inserted or in
505 template<typename _UIntType1
,
506 size_t __w1
, size_t __n1
,
507 size_t __m1
, size_t __r1
,
508 _UIntType1 __a1
, size_t __u1
,
509 _UIntType1 __d1
, size_t __s1
,
510 _UIntType1 __b1
, size_t __t1
,
511 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
512 typename _CharT
, typename _Traits
>
513 friend std::basic_ostream
<_CharT
, _Traits
>&
514 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
515 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
516 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
520 * @brief Extracts the current state of a % mersenne_twister_engine
521 * random number generator engine @p __x from the input stream
524 * @param __is An input stream.
525 * @param __x A % mersenne_twister_engine random number generator
528 * @returns The input stream with the state of @p __x extracted or in
531 template<typename _UIntType1
,
532 size_t __w1
, size_t __n1
,
533 size_t __m1
, size_t __r1
,
534 _UIntType1 __a1
, size_t __u1
,
535 _UIntType1 __d1
, size_t __s1
,
536 _UIntType1 __b1
, size_t __t1
,
537 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
538 typename _CharT
, typename _Traits
>
539 friend std::basic_istream
<_CharT
, _Traits
>&
540 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
541 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
542 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
546 _UIntType _M_x
[state_size
];
551 * @brief Compares two % mersenne_twister_engine random number generator
552 * objects of the same type for inequality.
554 * @param __lhs A % mersenne_twister_engine random number generator
556 * @param __rhs Another % mersenne_twister_engine random number
559 * @returns true if the infinite sequences of generated values
560 * would be different, false otherwise.
562 template<typename _UIntType
, size_t __w
,
563 size_t __n
, size_t __m
, size_t __r
,
564 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
565 _UIntType __b
, size_t __t
,
566 _UIntType __c
, size_t __l
, _UIntType __f
>
568 operator!=(const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
569 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __lhs
,
570 const std::mersenne_twister_engine
<_UIntType
, __w
, __n
, __m
,
571 __r
, __a
, __u
, __d
, __s
, __b
, __t
, __c
, __l
, __f
>& __rhs
)
572 { return !(__lhs
== __rhs
); }
576 * @brief The Marsaglia-Zaman generator.
578 * This is a model of a Generalized Fibonacci discrete random number
579 * generator, sometimes referred to as the SWC generator.
581 * A discrete random number generator that produces pseudorandom
584 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
587 * The size of the state is @f$r@f$
588 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
590 * @var _M_x The state of the generator. This is a ring buffer.
591 * @var _M_carry The carry.
592 * @var _M_p Current index of x(i - r).
594 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
595 class subtract_with_carry_engine
597 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
598 "substituting _UIntType not an unsigned integral type");
599 static_assert(0u < __s
&& __s
< __r
,
600 "template argument substituting __s out of bounds");
601 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
602 "template argument substituting __w out of bounds");
605 /** The type of the generated random value. */
606 typedef _UIntType result_type
;
609 static constexpr size_t word_size
= __w
;
610 static constexpr size_t short_lag
= __s
;
611 static constexpr size_t long_lag
= __r
;
612 static constexpr result_type default_seed
= 19780503u;
615 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
616 * random number generator.
619 subtract_with_carry_engine(result_type __sd
= default_seed
)
623 * @brief Constructs a %subtract_with_carry_engine random number engine
624 * seeded from the seed sequence @p __q.
626 * @param __q the seed sequence.
628 template<typename _Sseq
, typename
= typename
629 std::enable_if
<!std::is_same
<_Sseq
, subtract_with_carry_engine
>::value
>
632 subtract_with_carry_engine(_Sseq
& __q
)
636 * @brief Seeds the initial state @f$x_0@f$ of the random number
639 * N1688[4.19] modifies this as follows. If @p __value == 0,
640 * sets value to 19780503. In any case, with a linear
641 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
642 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
643 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
644 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
645 * set carry to 1, otherwise sets carry to 0.
648 seed(result_type __sd
= default_seed
);
651 * @brief Seeds the initial state @f$x_0@f$ of the
652 * % subtract_with_carry_engine random number generator.
654 template<typename _Sseq
>
655 typename
std::enable_if
<std::is_class
<_Sseq
>::value
>::type
659 * @brief Gets the inclusive minimum value of the range of random
660 * integers returned by this generator.
662 static constexpr result_type
667 * @brief Gets the inclusive maximum value of the range of random
668 * integers returned by this generator.
670 static constexpr result_type
672 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
675 * @brief Discard a sequence of random numbers.
678 discard(unsigned long long __z
)
680 for (; __z
!= 0ULL; --__z
)
685 * @brief Gets the next random number in the sequence.
691 * @brief Compares two % subtract_with_carry_engine random number
692 * generator objects of the same type for equality.
694 * @param __lhs A % subtract_with_carry_engine random number generator
696 * @param __rhs Another % subtract_with_carry_engine random number
699 * @returns true if the infinite sequences of generated values
700 * would be equal, false otherwise.
703 operator==(const subtract_with_carry_engine
& __lhs
,
704 const subtract_with_carry_engine
& __rhs
)
705 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
708 * @brief Inserts the current state of a % subtract_with_carry_engine
709 * random number generator engine @p __x into the output stream
712 * @param __os An output stream.
713 * @param __x A % subtract_with_carry_engine random number generator
716 * @returns The output stream with the state of @p __x inserted or in
719 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
720 typename _CharT
, typename _Traits
>
721 friend std::basic_ostream
<_CharT
, _Traits
>&
722 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
723 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
727 * @brief Extracts the current state of a % subtract_with_carry_engine
728 * random number generator engine @p __x from the input stream
731 * @param __is An input stream.
732 * @param __x A % subtract_with_carry_engine random number generator
735 * @returns The input stream with the state of @p __x extracted or in
738 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
739 typename _CharT
, typename _Traits
>
740 friend std::basic_istream
<_CharT
, _Traits
>&
741 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
742 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
746 _UIntType _M_x
[long_lag
];
752 * @brief Compares two % subtract_with_carry_engine random number
753 * generator objects of the same type for inequality.
755 * @param __lhs A % subtract_with_carry_engine random number generator
757 * @param __rhs Another % subtract_with_carry_engine random number
760 * @returns true if the infinite sequences of generated values
761 * would be different, false otherwise.
763 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
765 operator!=(const std::subtract_with_carry_engine
<_UIntType
, __w
,
767 const std::subtract_with_carry_engine
<_UIntType
, __w
,
769 { return !(__lhs
== __rhs
); }
773 * Produces random numbers from some base engine by discarding blocks of
776 * 0 <= @p __r <= @p __p
778 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
779 class discard_block_engine
781 static_assert(1 <= __r
&& __r
<= __p
,
782 "template argument substituting __r out of bounds");
785 /** The type of the generated random value. */
786 typedef typename
_RandomNumberEngine::result_type result_type
;
789 static constexpr size_t block_size
= __p
;
790 static constexpr size_t used_block
= __r
;
793 * @brief Constructs a default %discard_block_engine engine.
795 * The underlying engine is default constructed as well.
797 discard_block_engine()
798 : _M_b(), _M_n(0) { }
801 * @brief Copy 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(const _RandomNumberEngine
& __rne
)
808 : _M_b(__rne
), _M_n(0) { }
811 * @brief Move constructs a %discard_block_engine engine.
813 * Copies an existing base class random number generator.
814 * @param rng An existing (base class) engine object.
817 discard_block_engine(_RandomNumberEngine
&& __rne
)
818 : _M_b(std::move(__rne
)), _M_n(0) { }
821 * @brief Seed constructs a %discard_block_engine engine.
823 * Constructs the underlying generator engine seeded with @p __s.
824 * @param __s A seed value for the base class engine.
827 discard_block_engine(result_type __s
)
828 : _M_b(__s
), _M_n(0) { }
831 * @brief Generator construct a %discard_block_engine engine.
833 * @param __q A seed sequence.
835 template<typename _Sseq
, typename
= typename
836 std::enable_if
<!std::is_same
<_Sseq
, discard_block_engine
>::value
837 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
840 discard_block_engine(_Sseq
& __q
)
845 * @brief Reseeds the %discard_block_engine object with the default
846 * seed for the underlying base class generator engine.
856 * @brief Reseeds the %discard_block_engine object with the default
857 * seed for the underlying base class generator engine.
860 seed(result_type __s
)
867 * @brief Reseeds the %discard_block_engine object with the given seed
869 * @param __q A seed generator function.
871 template<typename _Sseq
>
880 * @brief Gets a const reference to the underlying generator engine
883 const _RandomNumberEngine
&
884 base() const noexcept
888 * @brief Gets the minimum value in the generated random number range.
890 static constexpr result_type
892 { return _RandomNumberEngine::min(); }
895 * @brief Gets the maximum value in the generated random number range.
897 static constexpr result_type
899 { return _RandomNumberEngine::max(); }
902 * @brief Discard a sequence of random numbers.
905 discard(unsigned long long __z
)
907 for (; __z
!= 0ULL; --__z
)
912 * @brief Gets the next value in the generated random number sequence.
918 * @brief Compares two %discard_block_engine random number generator
919 * objects of the same type for equality.
921 * @param __lhs A %discard_block_engine random number generator object.
922 * @param __rhs Another %discard_block_engine random number generator
925 * @returns true if the infinite sequences of generated values
926 * would be equal, false otherwise.
929 operator==(const discard_block_engine
& __lhs
,
930 const discard_block_engine
& __rhs
)
931 { return __lhs
._M_b
== __rhs
._M_b
&& __lhs
._M_n
== __rhs
._M_n
; }
934 * @brief Inserts the current state of a %discard_block_engine random
935 * number generator engine @p __x into the output stream
938 * @param __os An output stream.
939 * @param __x A %discard_block_engine random number generator engine.
941 * @returns The output stream with the state of @p __x inserted or in
944 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
945 typename _CharT
, typename _Traits
>
946 friend std::basic_ostream
<_CharT
, _Traits
>&
947 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
948 const std::discard_block_engine
<_RandomNumberEngine1
,
952 * @brief Extracts the current state of a % subtract_with_carry_engine
953 * random number generator engine @p __x from the input stream
956 * @param __is An input stream.
957 * @param __x A %discard_block_engine random number generator engine.
959 * @returns The input stream with the state of @p __x extracted or in
962 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
963 typename _CharT
, typename _Traits
>
964 friend std::basic_istream
<_CharT
, _Traits
>&
965 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
966 std::discard_block_engine
<_RandomNumberEngine1
,
970 _RandomNumberEngine _M_b
;
975 * @brief Compares two %discard_block_engine random number generator
976 * objects of the same type for inequality.
978 * @param __lhs A %discard_block_engine random number generator object.
979 * @param __rhs Another %discard_block_engine random number generator
982 * @returns true if the infinite sequences of generated values
983 * would be different, false otherwise.
985 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
987 operator!=(const std::discard_block_engine
<_RandomNumberEngine
, __p
,
989 const std::discard_block_engine
<_RandomNumberEngine
, __p
,
991 { return !(__lhs
== __rhs
); }
995 * Produces random numbers by combining random numbers from some base
996 * engine to produce random numbers with a specifies number of bits @p __w.
998 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
999 class independent_bits_engine
1001 static_assert(std::is_unsigned
<_UIntType
>::value
, "template argument "
1002 "substituting _UIntType not an unsigned integral type");
1003 static_assert(0u < __w
&& __w
<= std::numeric_limits
<_UIntType
>::digits
,
1004 "template argument substituting __w out of bounds");
1007 /** The type of the generated random value. */
1008 typedef _UIntType result_type
;
1011 * @brief Constructs a default %independent_bits_engine engine.
1013 * The underlying engine is default constructed as well.
1015 independent_bits_engine()
1019 * @brief Copy 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(const _RandomNumberEngine
& __rne
)
1029 * @brief Move constructs a %independent_bits_engine engine.
1031 * Copies an existing base class random number generator.
1032 * @param rng An existing (base class) engine object.
1035 independent_bits_engine(_RandomNumberEngine
&& __rne
)
1036 : _M_b(std::move(__rne
)) { }
1039 * @brief Seed constructs a %independent_bits_engine engine.
1041 * Constructs the underlying generator engine seeded with @p __s.
1042 * @param __s A seed value for the base class engine.
1045 independent_bits_engine(result_type __s
)
1049 * @brief Generator construct a %independent_bits_engine engine.
1051 * @param __q A seed sequence.
1053 template<typename _Sseq
, typename
= typename
1054 std::enable_if
<!std::is_same
<_Sseq
, independent_bits_engine
>::value
1055 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1058 independent_bits_engine(_Sseq
& __q
)
1063 * @brief Reseeds the %independent_bits_engine object with the default
1064 * seed for the underlying base class generator engine.
1071 * @brief Reseeds the %independent_bits_engine object with the default
1072 * seed for the underlying base class generator engine.
1075 seed(result_type __s
)
1079 * @brief Reseeds the %independent_bits_engine object with the given
1081 * @param __q A seed generator function.
1083 template<typename _Sseq
>
1089 * @brief Gets a const reference to the underlying generator engine
1092 const _RandomNumberEngine
&
1093 base() const noexcept
1097 * @brief Gets the minimum value in the generated random number range.
1099 static constexpr result_type
1104 * @brief Gets the maximum value in the generated random number range.
1106 static constexpr result_type
1108 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1111 * @brief Discard a sequence of random numbers.
1114 discard(unsigned long long __z
)
1116 for (; __z
!= 0ULL; --__z
)
1121 * @brief Gets the next value in the generated random number sequence.
1127 * @brief Compares two %independent_bits_engine random number generator
1128 * objects of the same type for equality.
1130 * @param __lhs A %independent_bits_engine random number generator
1132 * @param __rhs Another %independent_bits_engine random number generator
1135 * @returns true if the infinite sequences of generated values
1136 * would be equal, false otherwise.
1139 operator==(const independent_bits_engine
& __lhs
,
1140 const independent_bits_engine
& __rhs
)
1141 { return __lhs
._M_b
== __rhs
._M_b
; }
1144 * @brief Extracts the current state of a % subtract_with_carry_engine
1145 * random number generator engine @p __x from the input stream
1148 * @param __is An input stream.
1149 * @param __x A %independent_bits_engine random number generator
1152 * @returns The input stream with the state of @p __x extracted or in
1155 template<typename _CharT
, typename _Traits
>
1156 friend std::basic_istream
<_CharT
, _Traits
>&
1157 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1158 std::independent_bits_engine
<_RandomNumberEngine
,
1159 __w
, _UIntType
>& __x
)
1166 _RandomNumberEngine _M_b
;
1170 * @brief Compares two %independent_bits_engine random number generator
1171 * objects of the same type for inequality.
1173 * @param __lhs A %independent_bits_engine random number generator
1175 * @param __rhs Another %independent_bits_engine random number generator
1178 * @returns true if the infinite sequences of generated values
1179 * would be different, false otherwise.
1181 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
1183 operator!=(const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1185 const std::independent_bits_engine
<_RandomNumberEngine
, __w
,
1187 { return !(__lhs
== __rhs
); }
1190 * @brief Inserts the current state of a %independent_bits_engine random
1191 * number generator engine @p __x into the output stream @p __os.
1193 * @param __os An output stream.
1194 * @param __x A %independent_bits_engine random number generator engine.
1196 * @returns The output stream with the state of @p __x inserted or in
1199 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1200 typename _CharT
, typename _Traits
>
1201 std::basic_ostream
<_CharT
, _Traits
>&
1202 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1203 const std::independent_bits_engine
<_RandomNumberEngine
,
1204 __w
, _UIntType
>& __x
)
1212 * @brief Produces random numbers by combining random numbers from some
1213 * base engine to produce random numbers with a specifies number of bits
1216 template<typename _RandomNumberEngine
, size_t __k
>
1217 class shuffle_order_engine
1219 static_assert(1u <= __k
, "template argument substituting "
1220 "__k out of bound");
1223 /** The type of the generated random value. */
1224 typedef typename
_RandomNumberEngine::result_type result_type
;
1226 static constexpr size_t table_size
= __k
;
1229 * @brief Constructs a default %shuffle_order_engine engine.
1231 * The underlying engine is default constructed as well.
1233 shuffle_order_engine()
1235 { _M_initialize(); }
1238 * @brief Copy constructs a %shuffle_order_engine engine.
1240 * Copies an existing base class random number generator.
1241 * @param rng An existing (base class) engine object.
1244 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1246 { _M_initialize(); }
1249 * @brief Move constructs a %shuffle_order_engine engine.
1251 * Copies an existing base class random number generator.
1252 * @param rng An existing (base class) engine object.
1255 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1256 : _M_b(std::move(__rne
))
1257 { _M_initialize(); }
1260 * @brief Seed constructs a %shuffle_order_engine engine.
1262 * Constructs the underlying generator engine seeded with @p __s.
1263 * @param __s A seed value for the base class engine.
1266 shuffle_order_engine(result_type __s
)
1268 { _M_initialize(); }
1271 * @brief Generator construct a %shuffle_order_engine engine.
1273 * @param __q A seed sequence.
1275 template<typename _Sseq
, typename
= typename
1276 std::enable_if
<!std::is_same
<_Sseq
, shuffle_order_engine
>::value
1277 && !std::is_same
<_Sseq
, _RandomNumberEngine
>::value
>
1280 shuffle_order_engine(_Sseq
& __q
)
1282 { _M_initialize(); }
1285 * @brief Reseeds the %shuffle_order_engine object with the default seed
1286 for the underlying base class generator engine.
1296 * @brief Reseeds the %shuffle_order_engine object with the default seed
1297 * for the underlying base class generator engine.
1300 seed(result_type __s
)
1307 * @brief Reseeds the %shuffle_order_engine object with the given seed
1309 * @param __q A seed generator function.
1311 template<typename _Sseq
>
1320 * Gets a const reference to the underlying generator engine object.
1322 const _RandomNumberEngine
&
1323 base() const noexcept
1327 * Gets the minimum value in the generated random number range.
1329 static constexpr result_type
1331 { return _RandomNumberEngine::min(); }
1334 * Gets the maximum value in the generated random number range.
1336 static constexpr result_type
1338 { return _RandomNumberEngine::max(); }
1341 * Discard a sequence of random numbers.
1344 discard(unsigned long long __z
)
1346 for (; __z
!= 0ULL; --__z
)
1351 * Gets the next value in the generated random number sequence.
1357 * Compares two %shuffle_order_engine random number generator objects
1358 * of the same type for equality.
1360 * @param __lhs A %shuffle_order_engine random number generator object.
1361 * @param __rhs Another %shuffle_order_engine random number generator
1364 * @returns true if the infinite sequences of generated values
1365 * would be equal, false otherwise.
1368 operator==(const shuffle_order_engine
& __lhs
,
1369 const shuffle_order_engine
& __rhs
)
1370 { return __lhs
._M_b
== __rhs
._M_b
; }
1373 * @brief Inserts the current state of a %shuffle_order_engine random
1374 * number generator engine @p __x into the output stream
1377 * @param __os An output stream.
1378 * @param __x A %shuffle_order_engine random number generator engine.
1380 * @returns The output stream with the state of @p __x inserted or in
1383 template<typename _RandomNumberEngine1
, size_t __k1
,
1384 typename _CharT
, typename _Traits
>
1385 friend std::basic_ostream
<_CharT
, _Traits
>&
1386 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1387 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1391 * @brief Extracts the current state of a % subtract_with_carry_engine
1392 * random number generator engine @p __x from the input stream
1395 * @param __is An input stream.
1396 * @param __x A %shuffle_order_engine random number generator engine.
1398 * @returns The input stream with the state of @p __x extracted or in
1401 template<typename _RandomNumberEngine1
, size_t __k1
,
1402 typename _CharT
, typename _Traits
>
1403 friend std::basic_istream
<_CharT
, _Traits
>&
1404 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1405 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1408 void _M_initialize()
1410 for (size_t __i
= 0; __i
< __k
; ++__i
)
1415 _RandomNumberEngine _M_b
;
1416 result_type _M_v
[__k
];
1421 * Compares two %shuffle_order_engine random number generator objects
1422 * of the same type for inequality.
1424 * @param __lhs A %shuffle_order_engine random number generator object.
1425 * @param __rhs Another %shuffle_order_engine random number generator
1428 * @returns true if the infinite sequences of generated values
1429 * would be different, false otherwise.
1431 template<typename _RandomNumberEngine
, size_t __k
>
1433 operator!=(const std::shuffle_order_engine
<_RandomNumberEngine
,
1435 const std::shuffle_order_engine
<_RandomNumberEngine
,
1437 { return !(__lhs
== __rhs
); }
1441 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1443 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1447 * An alternative LCR (Lehmer Generator function).
1449 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1453 * The classic Mersenne Twister.
1456 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1457 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1458 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1460 typedef mersenne_twister_engine
<
1466 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1469 * An alternative Mersenne Twister.
1471 typedef mersenne_twister_engine
<
1474 0xb5026f5aa96619e9ULL
, 29,
1475 0x5555555555555555ULL
, 17,
1476 0x71d67fffeda60000ULL
, 37,
1477 0xfff7eee000000000ULL
, 43,
1478 6364136223846793005ULL> mt19937_64
;
1480 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1483 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1486 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1488 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1490 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1492 typedef minstd_rand0 default_random_engine
;
1495 * A standard interface to a platform-specific non-deterministic
1496 * random number generator (if any are available).
1501 /** The type of the generated random value. */
1502 typedef unsigned int result_type
;
1504 // constructors, destructors and member functions
1506 #ifdef _GLIBCXX_USE_RANDOM_TR1
1509 random_device(const std::string
& __token
= "/dev/urandom")
1511 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1512 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1513 std::__throw_runtime_error(__N("random_device::"
1514 "random_device(const std::string&)"));
1518 { std::fclose(_M_file
); }
1523 random_device(const std::string
& __token
= "mt19937")
1524 : _M_mt(_M_strtoul(__token
)) { }
1527 static unsigned long
1528 _M_strtoul(const std::string
& __str
)
1530 unsigned long __ret
= 5489UL;
1531 if (__str
!= "mt19937")
1533 const char* __nptr
= __str
.c_str();
1535 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1536 if (*__nptr
== '\0' || *__endptr
!= '\0')
1537 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1538 "(const std::string&)"));
1549 { return std::numeric_limits
<result_type
>::min(); }
1553 { return std::numeric_limits
<result_type
>::max(); }
1556 entropy() const noexcept
1562 #ifdef _GLIBCXX_USE_RANDOM_TR1
1564 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1572 // No copy functions.
1573 random_device(const random_device
&) = delete;
1574 void operator=(const random_device
&) = delete;
1578 #ifdef _GLIBCXX_USE_RANDOM_TR1
1585 /* @} */ // group random_generators
1588 * @addtogroup random_distributions Random Number Distributions
1594 * @addtogroup random_distributions_uniform Uniform Distributions
1595 * @ingroup random_distributions
1600 * @brief Uniform discrete distribution for random numbers.
1601 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1602 * probability throughout the range.
1604 template<typename _IntType
= int>
1605 class uniform_int_distribution
1607 static_assert(std::is_integral
<_IntType
>::value
,
1608 "template argument not an integral type");
1611 /** The type of the range of the distribution. */
1612 typedef _IntType result_type
;
1613 /** Parameter type. */
1616 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1619 param_type(_IntType __a
= 0,
1620 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1621 : _M_a(__a
), _M_b(__b
)
1623 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1635 operator==(const param_type
& __p1
, const param_type
& __p2
)
1636 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1645 * @brief Constructs a uniform distribution object.
1648 uniform_int_distribution(_IntType __a
= 0,
1649 _IntType __b
= std::numeric_limits
<_IntType
>::max())
1650 : _M_param(__a
, __b
)
1654 uniform_int_distribution(const param_type
& __p
)
1659 * @brief Resets the distribution state.
1661 * Does nothing for the uniform integer distribution.
1668 { return _M_param
.a(); }
1672 { return _M_param
.b(); }
1675 * @brief Returns the parameter set of the distribution.
1679 { return _M_param
; }
1682 * @brief Sets the parameter set of the distribution.
1683 * @param __param The new parameter set of the distribution.
1686 param(const param_type
& __param
)
1687 { _M_param
= __param
; }
1690 * @brief Returns the inclusive lower bound of the distribution range.
1694 { return this->a(); }
1697 * @brief Returns the inclusive upper bound of the distribution range.
1701 { return this->b(); }
1704 * @brief Generating functions.
1706 template<typename _UniformRandomNumberGenerator
>
1708 operator()(_UniformRandomNumberGenerator
& __urng
)
1709 { return this->operator()(__urng
, this->param()); }
1711 template<typename _UniformRandomNumberGenerator
>
1713 operator()(_UniformRandomNumberGenerator
& __urng
,
1714 const param_type
& __p
);
1716 param_type _M_param
;
1720 * @brief Return true if two uniform integer distributions have
1721 * the same 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
.param() == __d2
.param(); }
1730 * @brief Return true if two uniform integer distributions have
1731 * different parameters.
1733 template<typename _IntType
>
1735 operator!=(const std::uniform_int_distribution
<_IntType
>& __d1
,
1736 const std::uniform_int_distribution
<_IntType
>& __d2
)
1737 { return !(__d1
== __d2
); }
1740 * @brief Inserts a %uniform_int_distribution random number
1741 * distribution @p __x into the output stream @p os.
1743 * @param __os An output stream.
1744 * @param __x A %uniform_int_distribution random number distribution.
1746 * @returns The output stream with the state of @p __x inserted or in
1749 template<typename _IntType
, typename _CharT
, typename _Traits
>
1750 std::basic_ostream
<_CharT
, _Traits
>&
1751 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1752 const std::uniform_int_distribution
<_IntType
>&);
1755 * @brief Extracts a %uniform_int_distribution random number distribution
1756 * @p __x from the input stream @p __is.
1758 * @param __is An input stream.
1759 * @param __x A %uniform_int_distribution random number generator engine.
1761 * @returns The input stream with @p __x extracted or in an error state.
1763 template<typename _IntType
, typename _CharT
, typename _Traits
>
1764 std::basic_istream
<_CharT
, _Traits
>&
1765 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1766 std::uniform_int_distribution
<_IntType
>&);
1770 * @brief Uniform continuous distribution for random numbers.
1772 * A continuous random distribution on the range [min, max) with equal
1773 * probability throughout the range. The URNG should be real-valued and
1774 * deliver number in the range [0, 1).
1776 template<typename _RealType
= double>
1777 class uniform_real_distribution
1779 static_assert(std::is_floating_point
<_RealType
>::value
,
1780 "template argument not a floating point type");
1783 /** The type of the range of the distribution. */
1784 typedef _RealType result_type
;
1785 /** Parameter type. */
1788 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1791 param_type(_RealType __a
= _RealType(0),
1792 _RealType __b
= _RealType(1))
1793 : _M_a(__a
), _M_b(__b
)
1795 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1807 operator==(const param_type
& __p1
, const param_type
& __p2
)
1808 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
1817 * @brief Constructs a uniform_real_distribution object.
1819 * @param __min [IN] The lower bound of the distribution.
1820 * @param __max [IN] The upper bound of the distribution.
1823 uniform_real_distribution(_RealType __a
= _RealType(0),
1824 _RealType __b
= _RealType(1))
1825 : _M_param(__a
, __b
)
1829 uniform_real_distribution(const param_type
& __p
)
1834 * @brief Resets the distribution state.
1836 * Does nothing for the uniform real distribution.
1843 { return _M_param
.a(); }
1847 { return _M_param
.b(); }
1850 * @brief Returns the parameter set of the distribution.
1854 { return _M_param
; }
1857 * @brief Sets the parameter set of the distribution.
1858 * @param __param The new parameter set of the distribution.
1861 param(const param_type
& __param
)
1862 { _M_param
= __param
; }
1865 * @brief Returns the inclusive lower bound of the distribution range.
1869 { return this->a(); }
1872 * @brief Returns the inclusive upper bound of the distribution range.
1876 { return this->b(); }
1879 * @brief Generating functions.
1881 template<typename _UniformRandomNumberGenerator
>
1883 operator()(_UniformRandomNumberGenerator
& __urng
)
1884 { return this->operator()(__urng
, this->param()); }
1886 template<typename _UniformRandomNumberGenerator
>
1888 operator()(_UniformRandomNumberGenerator
& __urng
,
1889 const param_type
& __p
)
1891 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1893 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1897 param_type _M_param
;
1901 * @brief Return true if two uniform real distributions have
1902 * the same 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
.param() == __d2
.param(); }
1911 * @brief Return true if two uniform real distributions have
1912 * different parameters.
1914 template<typename _IntType
>
1916 operator!=(const std::uniform_real_distribution
<_IntType
>& __d1
,
1917 const std::uniform_real_distribution
<_IntType
>& __d2
)
1918 { return !(__d1
== __d2
); }
1921 * @brief Inserts a %uniform_real_distribution random number
1922 * distribution @p __x into the output stream @p __os.
1924 * @param __os An output stream.
1925 * @param __x A %uniform_real_distribution random number distribution.
1927 * @returns The output stream with the state of @p __x inserted or in
1930 template<typename _RealType
, typename _CharT
, typename _Traits
>
1931 std::basic_ostream
<_CharT
, _Traits
>&
1932 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1933 const std::uniform_real_distribution
<_RealType
>&);
1936 * @brief Extracts a %uniform_real_distribution random number distribution
1937 * @p __x from the input stream @p __is.
1939 * @param __is An input stream.
1940 * @param __x A %uniform_real_distribution random number generator engine.
1942 * @returns The input stream with @p __x extracted or in an error state.
1944 template<typename _RealType
, typename _CharT
, typename _Traits
>
1945 std::basic_istream
<_CharT
, _Traits
>&
1946 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1947 std::uniform_real_distribution
<_RealType
>&);
1949 /* @} */ // group random_distributions_uniform
1952 * @addtogroup random_distributions_normal Normal Distributions
1953 * @ingroup random_distributions
1958 * @brief A normal continuous distribution for random numbers.
1960 * The formula for the normal probability density function is
1962 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1963 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1966 template<typename _RealType
= double>
1967 class normal_distribution
1969 static_assert(std::is_floating_point
<_RealType
>::value
,
1970 "template argument not a floating point type");
1973 /** The type of the range of the distribution. */
1974 typedef _RealType result_type
;
1975 /** Parameter type. */
1978 typedef normal_distribution
<_RealType
> distribution_type
;
1981 param_type(_RealType __mean
= _RealType(0),
1982 _RealType __stddev
= _RealType(1))
1983 : _M_mean(__mean
), _M_stddev(__stddev
)
1985 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1994 { return _M_stddev
; }
1997 operator==(const param_type
& __p1
, const param_type
& __p2
)
1998 { return (__p1
._M_mean
== __p2
._M_mean
1999 && __p1
._M_stddev
== __p2
._M_stddev
); }
2003 _RealType _M_stddev
;
2008 * Constructs a normal distribution with parameters @f$mean@f$ and
2009 * standard deviation.
2012 normal_distribution(result_type __mean
= result_type(0),
2013 result_type __stddev
= result_type(1))
2014 : _M_param(__mean
, __stddev
), _M_saved_available(false)
2018 normal_distribution(const param_type
& __p
)
2019 : _M_param(__p
), _M_saved_available(false)
2023 * @brief Resets the distribution state.
2027 { _M_saved_available
= false; }
2030 * @brief Returns the mean of the distribution.
2034 { return _M_param
.mean(); }
2037 * @brief Returns the standard deviation of the distribution.
2041 { return _M_param
.stddev(); }
2044 * @brief Returns the parameter set of the distribution.
2048 { return _M_param
; }
2051 * @brief Sets the parameter set of the distribution.
2052 * @param __param The new parameter set of the distribution.
2055 param(const param_type
& __param
)
2056 { _M_param
= __param
; }
2059 * @brief Returns the greatest lower bound value of the distribution.
2063 { return std::numeric_limits
<result_type
>::min(); }
2066 * @brief Returns the least upper bound value of the distribution.
2070 { return std::numeric_limits
<result_type
>::max(); }
2073 * @brief Generating functions.
2075 template<typename _UniformRandomNumberGenerator
>
2077 operator()(_UniformRandomNumberGenerator
& __urng
)
2078 { return this->operator()(__urng
, this->param()); }
2080 template<typename _UniformRandomNumberGenerator
>
2082 operator()(_UniformRandomNumberGenerator
& __urng
,
2083 const param_type
& __p
);
2086 * @brief Return true if two normal distributions have
2087 * the same parameters and the sequences that would
2088 * be generated are equal.
2090 template<typename _RealType1
>
2092 operator==(const std::normal_distribution
<_RealType1
>& __d1
,
2093 const std::normal_distribution
<_RealType1
>& __d2
);
2096 * @brief Inserts a %normal_distribution random number distribution
2097 * @p __x into the output stream @p __os.
2099 * @param __os An output stream.
2100 * @param __x A %normal_distribution random number distribution.
2102 * @returns The output stream with the state of @p __x inserted or in
2105 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2106 friend std::basic_ostream
<_CharT
, _Traits
>&
2107 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2108 const std::normal_distribution
<_RealType1
>&);
2111 * @brief Extracts a %normal_distribution random number distribution
2112 * @p __x from the input stream @p __is.
2114 * @param __is An input stream.
2115 * @param __x A %normal_distribution random number generator engine.
2117 * @returns The input stream with @p __x extracted or in an error
2120 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2121 friend std::basic_istream
<_CharT
, _Traits
>&
2122 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2123 std::normal_distribution
<_RealType1
>&);
2126 param_type _M_param
;
2127 result_type _M_saved
;
2128 bool _M_saved_available
;
2132 * @brief Return true if two normal distributions are different.
2134 template<typename _RealType
>
2136 operator!=(const std::normal_distribution
<_RealType
>& __d1
,
2137 const std::normal_distribution
<_RealType
>& __d2
)
2138 { return !(__d1
== __d2
); }
2142 * @brief A lognormal_distribution random number distribution.
2144 * The formula for the normal probability mass function is
2146 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2147 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2150 template<typename _RealType
= double>
2151 class lognormal_distribution
2153 static_assert(std::is_floating_point
<_RealType
>::value
,
2154 "template argument not a floating point type");
2157 /** The type of the range of the distribution. */
2158 typedef _RealType result_type
;
2159 /** Parameter type. */
2162 typedef lognormal_distribution
<_RealType
> distribution_type
;
2165 param_type(_RealType __m
= _RealType(0),
2166 _RealType __s
= _RealType(1))
2167 : _M_m(__m
), _M_s(__s
)
2179 operator==(const param_type
& __p1
, const param_type
& __p2
)
2180 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_s
== __p2
._M_s
; }
2188 lognormal_distribution(_RealType __m
= _RealType(0),
2189 _RealType __s
= _RealType(1))
2190 : _M_param(__m
, __s
), _M_nd()
2194 lognormal_distribution(const param_type
& __p
)
2195 : _M_param(__p
), _M_nd()
2199 * Resets the distribution state.
2210 { return _M_param
.m(); }
2214 { return _M_param
.s(); }
2217 * @brief Returns the parameter set of the distribution.
2221 { return _M_param
; }
2224 * @brief Sets the parameter set of the distribution.
2225 * @param __param The new parameter set of the distribution.
2228 param(const param_type
& __param
)
2229 { _M_param
= __param
; }
2232 * @brief Returns the greatest lower bound value of the distribution.
2236 { return result_type(0); }
2239 * @brief Returns the least upper bound value of the distribution.
2243 { return std::numeric_limits
<result_type
>::max(); }
2246 * @brief Generating functions.
2248 template<typename _UniformRandomNumberGenerator
>
2250 operator()(_UniformRandomNumberGenerator
& __urng
)
2251 { return this->operator()(__urng
, this->param()); }
2253 template<typename _UniformRandomNumberGenerator
>
2255 operator()(_UniformRandomNumberGenerator
& __urng
,
2256 const param_type
& __p
)
2257 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2260 * @brief Return true if two lognormal distributions have
2261 * the same parameters and the sequences that would
2262 * be generated are equal.
2264 template<typename _RealType1
>
2266 operator==(const std::lognormal_distribution
<_RealType1
>& __d1
,
2267 const std::lognormal_distribution
<_RealType1
>& __d2
)
2268 { return (__d1
.param() == __d2
.param()
2269 && __d1
._M_nd
== __d2
._M_nd
); }
2272 * @brief Inserts a %lognormal_distribution random number distribution
2273 * @p __x into the output stream @p __os.
2275 * @param __os An output stream.
2276 * @param __x A %lognormal_distribution random number distribution.
2278 * @returns The output stream with the state of @p __x inserted or in
2281 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2282 friend std::basic_ostream
<_CharT
, _Traits
>&
2283 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2284 const std::lognormal_distribution
<_RealType1
>&);
2287 * @brief Extracts a %lognormal_distribution random number distribution
2288 * @p __x from the input stream @p __is.
2290 * @param __is An input stream.
2291 * @param __x A %lognormal_distribution random number
2294 * @returns The input stream with @p __x extracted or in an error state.
2296 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2297 friend std::basic_istream
<_CharT
, _Traits
>&
2298 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2299 std::lognormal_distribution
<_RealType1
>&);
2302 param_type _M_param
;
2304 std::normal_distribution
<result_type
> _M_nd
;
2308 * @brief Return true if two lognormal distributions are different.
2310 template<typename _RealType
>
2312 operator!=(const std::lognormal_distribution
<_RealType
>& __d1
,
2313 const std::lognormal_distribution
<_RealType
>& __d2
)
2314 { return !(__d1
== __d2
); }
2318 * @brief A gamma continuous distribution for random numbers.
2320 * The formula for the gamma probability density function is:
2322 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2323 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2326 template<typename _RealType
= double>
2327 class gamma_distribution
2329 static_assert(std::is_floating_point
<_RealType
>::value
,
2330 "template argument not a floating point type");
2333 /** The type of the range of the distribution. */
2334 typedef _RealType result_type
;
2335 /** Parameter type. */
2338 typedef gamma_distribution
<_RealType
> distribution_type
;
2339 friend class gamma_distribution
<_RealType
>;
2342 param_type(_RealType __alpha_val
= _RealType(1),
2343 _RealType __beta_val
= _RealType(1))
2344 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2346 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2352 { return _M_alpha
; }
2359 operator==(const param_type
& __p1
, const param_type
& __p2
)
2360 { return (__p1
._M_alpha
== __p2
._M_alpha
2361 && __p1
._M_beta
== __p2
._M_beta
); }
2370 _RealType _M_malpha
, _M_a2
;
2375 * @brief Constructs a gamma distribution with parameters
2376 * @f$\alpha@f$ and @f$\beta@f$.
2379 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2380 _RealType __beta_val
= _RealType(1))
2381 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2385 gamma_distribution(const param_type
& __p
)
2386 : _M_param(__p
), _M_nd()
2390 * @brief Resets the distribution state.
2397 * @brief Returns the @f$\alpha@f$ of the distribution.
2401 { return _M_param
.alpha(); }
2404 * @brief Returns the @f$\beta@f$ of the distribution.
2408 { return _M_param
.beta(); }
2411 * @brief Returns the parameter set of the distribution.
2415 { return _M_param
; }
2418 * @brief Sets the parameter set of the distribution.
2419 * @param __param The new parameter set of the distribution.
2422 param(const param_type
& __param
)
2423 { _M_param
= __param
; }
2426 * @brief Returns the greatest lower bound value of the distribution.
2430 { return result_type(0); }
2433 * @brief Returns the least upper bound value of the distribution.
2437 { return std::numeric_limits
<result_type
>::max(); }
2440 * @brief Generating functions.
2442 template<typename _UniformRandomNumberGenerator
>
2444 operator()(_UniformRandomNumberGenerator
& __urng
)
2445 { return this->operator()(__urng
, this->param()); }
2447 template<typename _UniformRandomNumberGenerator
>
2449 operator()(_UniformRandomNumberGenerator
& __urng
,
2450 const param_type
& __p
);
2453 * @brief Return true if two gamma distributions have the same
2454 * parameters and the sequences that would be generated
2457 template<typename _RealType1
>
2459 operator==(const std::gamma_distribution
<_RealType1
>& __d1
,
2460 const std::gamma_distribution
<_RealType1
>& __d2
)
2461 { return (__d1
.param() == __d2
.param()
2462 && __d1
._M_nd
== __d2
._M_nd
); }
2465 * @brief Inserts a %gamma_distribution random number distribution
2466 * @p __x into the output stream @p __os.
2468 * @param __os An output stream.
2469 * @param __x A %gamma_distribution random number distribution.
2471 * @returns The output stream with the state of @p __x inserted or in
2474 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2475 friend std::basic_ostream
<_CharT
, _Traits
>&
2476 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2477 const std::gamma_distribution
<_RealType1
>&);
2480 * @brief Extracts a %gamma_distribution random number distribution
2481 * @p __x from the input stream @p __is.
2483 * @param __is An input stream.
2484 * @param __x A %gamma_distribution random number generator engine.
2486 * @returns The input stream with @p __x extracted or in an error state.
2488 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2489 friend std::basic_istream
<_CharT
, _Traits
>&
2490 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2491 std::gamma_distribution
<_RealType1
>&);
2494 param_type _M_param
;
2496 std::normal_distribution
<result_type
> _M_nd
;
2500 * @brief Return true if two gamma distributions are different.
2502 template<typename _RealType
>
2504 operator!=(const std::gamma_distribution
<_RealType
>& __d1
,
2505 const std::gamma_distribution
<_RealType
>& __d2
)
2506 { return !(__d1
== __d2
); }
2510 * @brief A chi_squared_distribution random number distribution.
2512 * The formula for the normal probability mass function is
2513 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2515 template<typename _RealType
= double>
2516 class chi_squared_distribution
2518 static_assert(std::is_floating_point
<_RealType
>::value
,
2519 "template argument not a floating point type");
2522 /** The type of the range of the distribution. */
2523 typedef _RealType result_type
;
2524 /** Parameter type. */
2527 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2530 param_type(_RealType __n
= _RealType(1))
2539 operator==(const param_type
& __p1
, const param_type
& __p2
)
2540 { return __p1
._M_n
== __p2
._M_n
; }
2547 chi_squared_distribution(_RealType __n
= _RealType(1))
2548 : _M_param(__n
), _M_gd(__n
/ 2)
2552 chi_squared_distribution(const param_type
& __p
)
2553 : _M_param(__p
), _M_gd(__p
.n() / 2)
2557 * @brief Resets the distribution state.
2568 { return _M_param
.n(); }
2571 * @brief Returns the parameter set of the distribution.
2575 { return _M_param
; }
2578 * @brief Sets the parameter set of the distribution.
2579 * @param __param The new parameter set of the distribution.
2582 param(const param_type
& __param
)
2583 { _M_param
= __param
; }
2586 * @brief Returns the greatest lower bound value of the distribution.
2590 { return result_type(0); }
2593 * @brief Returns the least upper bound value of the distribution.
2597 { return std::numeric_limits
<result_type
>::max(); }
2600 * @brief Generating functions.
2602 template<typename _UniformRandomNumberGenerator
>
2604 operator()(_UniformRandomNumberGenerator
& __urng
)
2605 { return 2 * _M_gd(__urng
); }
2607 template<typename _UniformRandomNumberGenerator
>
2609 operator()(_UniformRandomNumberGenerator
& __urng
,
2610 const param_type
& __p
)
2612 typedef typename
std::gamma_distribution
<result_type
>::param_type
2614 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2618 * @brief Return true if two Chi-squared distributions have
2619 * the same parameters and the sequences that would be
2620 * generated are equal.
2622 template<typename _RealType1
>
2624 operator==(const std::chi_squared_distribution
<_RealType1
>& __d1
,
2625 const std::chi_squared_distribution
<_RealType1
>& __d2
)
2626 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
2629 * @brief Inserts a %chi_squared_distribution random number distribution
2630 * @p __x into the output stream @p __os.
2632 * @param __os An output stream.
2633 * @param __x A %chi_squared_distribution random number distribution.
2635 * @returns The output stream with the state of @p __x inserted or in
2638 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2639 friend std::basic_ostream
<_CharT
, _Traits
>&
2640 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2641 const std::chi_squared_distribution
<_RealType1
>&);
2644 * @brief Extracts a %chi_squared_distribution random number distribution
2645 * @p __x from the input stream @p __is.
2647 * @param __is An input stream.
2648 * @param __x A %chi_squared_distribution random number
2651 * @returns The input stream with @p __x extracted or in an error state.
2653 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2654 friend std::basic_istream
<_CharT
, _Traits
>&
2655 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2656 std::chi_squared_distribution
<_RealType1
>&);
2659 param_type _M_param
;
2661 std::gamma_distribution
<result_type
> _M_gd
;
2665 * @brief Return true if two Chi-squared distributions are different.
2667 template<typename _RealType
>
2669 operator!=(const std::chi_squared_distribution
<_RealType
>& __d1
,
2670 const std::chi_squared_distribution
<_RealType
>& __d2
)
2671 { return !(__d1
== __d2
); }
2675 * @brief A cauchy_distribution random number distribution.
2677 * The formula for the normal probability mass function is
2678 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2680 template<typename _RealType
= double>
2681 class cauchy_distribution
2683 static_assert(std::is_floating_point
<_RealType
>::value
,
2684 "template argument not a floating point type");
2687 /** The type of the range of the distribution. */
2688 typedef _RealType result_type
;
2689 /** Parameter type. */
2692 typedef cauchy_distribution
<_RealType
> distribution_type
;
2695 param_type(_RealType __a
= _RealType(0),
2696 _RealType __b
= _RealType(1))
2697 : _M_a(__a
), _M_b(__b
)
2709 operator==(const param_type
& __p1
, const param_type
& __p2
)
2710 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
2718 cauchy_distribution(_RealType __a
= _RealType(0),
2719 _RealType __b
= _RealType(1))
2720 : _M_param(__a
, __b
)
2724 cauchy_distribution(const param_type
& __p
)
2729 * @brief Resets the distribution state.
2740 { return _M_param
.a(); }
2744 { return _M_param
.b(); }
2747 * @brief Returns the parameter set of the distribution.
2751 { return _M_param
; }
2754 * @brief Sets the parameter set of the distribution.
2755 * @param __param The new parameter set of the distribution.
2758 param(const param_type
& __param
)
2759 { _M_param
= __param
; }
2762 * @brief Returns the greatest lower bound value of the distribution.
2766 { return std::numeric_limits
<result_type
>::min(); }
2769 * @brief Returns the least upper bound value of the distribution.
2773 { return std::numeric_limits
<result_type
>::max(); }
2776 * @brief Generating functions.
2778 template<typename _UniformRandomNumberGenerator
>
2780 operator()(_UniformRandomNumberGenerator
& __urng
)
2781 { return this->operator()(__urng
, this->param()); }
2783 template<typename _UniformRandomNumberGenerator
>
2785 operator()(_UniformRandomNumberGenerator
& __urng
,
2786 const param_type
& __p
);
2789 param_type _M_param
;
2793 * @brief Return true if two Cauchy distributions have
2794 * the same parameters.
2796 template<typename _RealType
>
2798 operator==(const std::cauchy_distribution
<_RealType
>& __d1
,
2799 const std::cauchy_distribution
<_RealType
>& __d2
)
2800 { return __d1
.param() == __d2
.param(); }
2803 * @brief Return true if two Cauchy distributions have
2804 * different parameters.
2806 template<typename _RealType
>
2808 operator!=(const std::cauchy_distribution
<_RealType
>& __d1
,
2809 const std::cauchy_distribution
<_RealType
>& __d2
)
2810 { return !(__d1
== __d2
); }
2813 * @brief Inserts a %cauchy_distribution random number distribution
2814 * @p __x into the output stream @p __os.
2816 * @param __os An output stream.
2817 * @param __x A %cauchy_distribution random number distribution.
2819 * @returns The output stream with the state of @p __x inserted or in
2822 template<typename _RealType
, typename _CharT
, typename _Traits
>
2823 std::basic_ostream
<_CharT
, _Traits
>&
2824 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2825 const std::cauchy_distribution
<_RealType
>&);
2828 * @brief Extracts a %cauchy_distribution random number distribution
2829 * @p __x from the input stream @p __is.
2831 * @param __is An input stream.
2832 * @param __x A %cauchy_distribution random number
2835 * @returns The input stream with @p __x extracted or in an error state.
2837 template<typename _RealType
, typename _CharT
, typename _Traits
>
2838 std::basic_istream
<_CharT
, _Traits
>&
2839 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2840 std::cauchy_distribution
<_RealType
>&);
2844 * @brief A fisher_f_distribution random number distribution.
2846 * The formula for the normal probability mass function is
2848 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2849 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2850 * (1 + \frac{mx}{n})^{-(m+n)/2}
2853 template<typename _RealType
= double>
2854 class fisher_f_distribution
2856 static_assert(std::is_floating_point
<_RealType
>::value
,
2857 "template argument not a floating point type");
2860 /** The type of the range of the distribution. */
2861 typedef _RealType result_type
;
2862 /** Parameter type. */
2865 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2868 param_type(_RealType __m
= _RealType(1),
2869 _RealType __n
= _RealType(1))
2870 : _M_m(__m
), _M_n(__n
)
2882 operator==(const param_type
& __p1
, const param_type
& __p2
)
2883 { return __p1
._M_m
== __p2
._M_m
&& __p1
._M_n
== __p2
._M_n
; }
2891 fisher_f_distribution(_RealType __m
= _RealType(1),
2892 _RealType __n
= _RealType(1))
2893 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2897 fisher_f_distribution(const param_type
& __p
)
2898 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2902 * @brief Resets the distribution state.
2916 { return _M_param
.m(); }
2920 { return _M_param
.n(); }
2923 * @brief Returns the parameter set of the distribution.
2927 { return _M_param
; }
2930 * @brief Sets the parameter set of the distribution.
2931 * @param __param The new parameter set of the distribution.
2934 param(const param_type
& __param
)
2935 { _M_param
= __param
; }
2938 * @brief Returns the greatest lower bound value of the distribution.
2942 { return result_type(0); }
2945 * @brief Returns the least upper bound value of the distribution.
2949 { return std::numeric_limits
<result_type
>::max(); }
2952 * @brief Generating functions.
2954 template<typename _UniformRandomNumberGenerator
>
2956 operator()(_UniformRandomNumberGenerator
& __urng
)
2957 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2959 template<typename _UniformRandomNumberGenerator
>
2961 operator()(_UniformRandomNumberGenerator
& __urng
,
2962 const param_type
& __p
)
2964 typedef typename
std::gamma_distribution
<result_type
>::param_type
2966 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2967 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2971 * @brief Return true if two Fisher f distributions have
2972 * the same parameters and the sequences that would
2973 * be generated are equal.
2975 template<typename _RealType1
>
2977 operator==(const std::fisher_f_distribution
<_RealType1
>& __d1
,
2978 const std::fisher_f_distribution
<_RealType1
>& __d2
)
2979 { return (__d1
.param() == __d2
.param()
2980 && __d1
._M_gd_x
== __d2
._M_gd_x
2981 && __d1
._M_gd_y
== __d2
._M_gd_y
); }
2984 * @brief Inserts a %fisher_f_distribution random number distribution
2985 * @p __x into the output stream @p __os.
2987 * @param __os An output stream.
2988 * @param __x A %fisher_f_distribution random number distribution.
2990 * @returns The output stream with the state of @p __x inserted or in
2993 template<typename _RealType1
, typename _CharT
, typename _Traits
>
2994 friend std::basic_ostream
<_CharT
, _Traits
>&
2995 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2996 const std::fisher_f_distribution
<_RealType1
>&);
2999 * @brief Extracts a %fisher_f_distribution random number distribution
3000 * @p __x from the input stream @p __is.
3002 * @param __is An input stream.
3003 * @param __x A %fisher_f_distribution random number
3006 * @returns The input stream with @p __x extracted or in an error state.
3008 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3009 friend std::basic_istream
<_CharT
, _Traits
>&
3010 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3011 std::fisher_f_distribution
<_RealType1
>&);
3014 param_type _M_param
;
3016 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
3020 * @brief Return true if two Fisher f distributions are diferent.
3022 template<typename _RealType
>
3024 operator!=(const std::fisher_f_distribution
<_RealType
>& __d1
,
3025 const std::fisher_f_distribution
<_RealType
>& __d2
)
3026 { return !(__d1
== __d2
); }
3029 * @brief A student_t_distribution random number distribution.
3031 * The formula for the normal probability mass function is:
3033 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3034 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3037 template<typename _RealType
= double>
3038 class student_t_distribution
3040 static_assert(std::is_floating_point
<_RealType
>::value
,
3041 "template argument not a floating point type");
3044 /** The type of the range of the distribution. */
3045 typedef _RealType result_type
;
3046 /** Parameter type. */
3049 typedef student_t_distribution
<_RealType
> distribution_type
;
3052 param_type(_RealType __n
= _RealType(1))
3061 operator==(const param_type
& __p1
, const param_type
& __p2
)
3062 { return __p1
._M_n
== __p2
._M_n
; }
3069 student_t_distribution(_RealType __n
= _RealType(1))
3070 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
3074 student_t_distribution(const param_type
& __p
)
3075 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
3079 * @brief Resets the distribution state.
3093 { return _M_param
.n(); }
3096 * @brief Returns the parameter set of the distribution.
3100 { return _M_param
; }
3103 * @brief Sets the parameter set of the distribution.
3104 * @param __param The new parameter set of the distribution.
3107 param(const param_type
& __param
)
3108 { _M_param
= __param
; }
3111 * @brief Returns the greatest lower bound value of the distribution.
3115 { return std::numeric_limits
<result_type
>::min(); }
3118 * @brief Returns the least upper bound value of the distribution.
3122 { return std::numeric_limits
<result_type
>::max(); }
3125 * @brief Generating functions.
3127 template<typename _UniformRandomNumberGenerator
>
3129 operator()(_UniformRandomNumberGenerator
& __urng
)
3130 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
3132 template<typename _UniformRandomNumberGenerator
>
3134 operator()(_UniformRandomNumberGenerator
& __urng
,
3135 const param_type
& __p
)
3137 typedef typename
std::gamma_distribution
<result_type
>::param_type
3140 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
3141 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
3145 * @brief Return true if two Student t distributions have
3146 * the same parameters and the sequences that would
3147 * be generated are equal.
3149 template<typename _RealType1
>
3151 operator==(const std::student_t_distribution
<_RealType1
>& __d1
,
3152 const std::student_t_distribution
<_RealType1
>& __d2
)
3153 { return (__d1
.param() == __d2
.param()
3154 && __d1
._M_nd
== __d2
._M_nd
&& __d1
._M_gd
== __d2
._M_gd
); }
3157 * @brief Inserts a %student_t_distribution random number distribution
3158 * @p __x into the output stream @p __os.
3160 * @param __os An output stream.
3161 * @param __x A %student_t_distribution random number distribution.
3163 * @returns The output stream with the state of @p __x inserted or in
3166 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3167 friend std::basic_ostream
<_CharT
, _Traits
>&
3168 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3169 const std::student_t_distribution
<_RealType1
>&);
3172 * @brief Extracts a %student_t_distribution random number distribution
3173 * @p __x from the input stream @p __is.
3175 * @param __is An input stream.
3176 * @param __x A %student_t_distribution random number
3179 * @returns The input stream with @p __x extracted or in an error state.
3181 template<typename _RealType1
, typename _CharT
, typename _Traits
>
3182 friend std::basic_istream
<_CharT
, _Traits
>&
3183 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3184 std::student_t_distribution
<_RealType1
>&);
3187 param_type _M_param
;
3189 std::normal_distribution
<result_type
> _M_nd
;
3190 std::gamma_distribution
<result_type
> _M_gd
;
3194 * @brief Return true if two Student t distributions are different.
3196 template<typename _RealType
>
3198 operator!=(const std::student_t_distribution
<_RealType
>& __d1
,
3199 const std::student_t_distribution
<_RealType
>& __d2
)
3200 { return !(__d1
== __d2
); }
3203 /* @} */ // group random_distributions_normal
3206 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3207 * @ingroup random_distributions
3212 * @brief A Bernoulli random number distribution.
3214 * Generates a sequence of true and false values with likelihood @f$p@f$
3215 * that true will come up and @f$(1 - p)@f$ that false will appear.
3217 class bernoulli_distribution
3220 /** The type of the range of the distribution. */
3221 typedef bool result_type
;
3222 /** Parameter type. */
3225 typedef bernoulli_distribution distribution_type
;
3228 param_type(double __p
= 0.5)
3231 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
3239 operator==(const param_type
& __p1
, const param_type
& __p2
)
3240 { return __p1
._M_p
== __p2
._M_p
; }
3248 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3250 * @param __p [IN] The likelihood of a true result being returned.
3251 * Must be in the interval @f$[0, 1]@f$.
3254 bernoulli_distribution(double __p
= 0.5)
3259 bernoulli_distribution(const param_type
& __p
)
3264 * @brief Resets the distribution state.
3266 * Does nothing for a Bernoulli distribution.
3272 * @brief Returns the @p p parameter of the distribution.
3276 { return _M_param
.p(); }
3279 * @brief Returns the parameter set of the distribution.
3283 { return _M_param
; }
3286 * @brief Sets the parameter set of the distribution.
3287 * @param __param The new parameter set of the distribution.
3290 param(const param_type
& __param
)
3291 { _M_param
= __param
; }
3294 * @brief Returns the greatest lower bound value of the distribution.
3298 { return std::numeric_limits
<result_type
>::min(); }
3301 * @brief Returns the least upper bound value of the distribution.
3305 { return std::numeric_limits
<result_type
>::max(); }
3308 * @brief Generating functions.
3310 template<typename _UniformRandomNumberGenerator
>
3312 operator()(_UniformRandomNumberGenerator
& __urng
)
3313 { return this->operator()(__urng
, this->param()); }
3315 template<typename _UniformRandomNumberGenerator
>
3317 operator()(_UniformRandomNumberGenerator
& __urng
,
3318 const param_type
& __p
)
3320 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
3322 if ((__aurng() - __aurng
.min())
3323 < __p
.p() * (__aurng
.max() - __aurng
.min()))
3329 param_type _M_param
;
3333 * @brief Return true if two Bernoulli distributions have
3334 * the same parameters.
3337 operator==(const std::bernoulli_distribution
& __d1
,
3338 const std::bernoulli_distribution
& __d2
)
3339 { return __d1
.param() == __d2
.param(); }
3342 * @brief Return true if two Bernoulli distributions have
3343 * different parameters.
3346 operator!=(const std::bernoulli_distribution
& __d1
,
3347 const std::bernoulli_distribution
& __d2
)
3348 { return !(__d1
== __d2
); }
3351 * @brief Inserts a %bernoulli_distribution random number distribution
3352 * @p __x into the output stream @p __os.
3354 * @param __os An output stream.
3355 * @param __x A %bernoulli_distribution random number distribution.
3357 * @returns The output stream with the state of @p __x inserted or in
3360 template<typename _CharT
, typename _Traits
>
3361 std::basic_ostream
<_CharT
, _Traits
>&
3362 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3363 const std::bernoulli_distribution
&);
3366 * @brief Extracts a %bernoulli_distribution random number distribution
3367 * @p __x from the input stream @p __is.
3369 * @param __is An input stream.
3370 * @param __x A %bernoulli_distribution random number generator engine.
3372 * @returns The input stream with @p __x extracted or in an error state.
3374 template<typename _CharT
, typename _Traits
>
3375 std::basic_istream
<_CharT
, _Traits
>&
3376 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
3377 std::bernoulli_distribution
& __x
)
3381 __x
.param(bernoulli_distribution::param_type(__p
));
3387 * @brief A discrete binomial random number distribution.
3389 * The formula for the binomial probability density function is
3390 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3391 * and @f$p@f$ are the parameters of the distribution.
3393 template<typename _IntType
= int>
3394 class binomial_distribution
3396 static_assert(std::is_integral
<_IntType
>::value
,
3397 "template argument not an integral type");
3400 /** The type of the range of the distribution. */
3401 typedef _IntType result_type
;
3402 /** Parameter type. */
3405 typedef binomial_distribution
<_IntType
> distribution_type
;
3406 friend class binomial_distribution
<_IntType
>;
3409 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3410 : _M_t(__t
), _M_p(__p
)
3412 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3427 operator==(const param_type
& __p1
, const param_type
& __p2
)
3428 { return __p1
._M_t
== __p2
._M_t
&& __p1
._M_p
== __p2
._M_p
; }
3438 #if _GLIBCXX_USE_C99_MATH_TR1
3439 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3440 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3445 // constructors and member function
3447 binomial_distribution(_IntType __t
= _IntType(1),
3449 : _M_param(__t
, __p
), _M_nd()
3453 binomial_distribution(const param_type
& __p
)
3454 : _M_param(__p
), _M_nd()
3458 * @brief Resets the distribution state.
3465 * @brief Returns the distribution @p t parameter.
3469 { return _M_param
.t(); }
3472 * @brief Returns the distribution @p p parameter.
3476 { return _M_param
.p(); }
3479 * @brief Returns the parameter set of the distribution.
3483 { return _M_param
; }
3486 * @brief Sets the parameter set of the distribution.
3487 * @param __param The new parameter set of the distribution.
3490 param(const param_type
& __param
)
3491 { _M_param
= __param
; }
3494 * @brief Returns the greatest lower bound value of the distribution.
3501 * @brief Returns the least upper bound value of the distribution.
3505 { return _M_param
.t(); }
3508 * @brief Generating functions.
3510 template<typename _UniformRandomNumberGenerator
>
3512 operator()(_UniformRandomNumberGenerator
& __urng
)
3513 { return this->operator()(__urng
, this->param()); }
3515 template<typename _UniformRandomNumberGenerator
>
3517 operator()(_UniformRandomNumberGenerator
& __urng
,
3518 const param_type
& __p
);
3521 * @brief Return true if two binomial distributions have
3522 * the same parameters and the sequences that would
3523 * be generated are equal.
3525 template<typename _IntType1
>
3527 operator==(const std::binomial_distribution
<_IntType1
>& __d1
,
3528 const std::binomial_distribution
<_IntType1
>& __d2
)
3529 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3530 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
3532 { return __d1
.param() == __d2
.param(); }
3536 * @brief Inserts a %binomial_distribution random number distribution
3537 * @p __x into the output stream @p __os.
3539 * @param __os An output stream.
3540 * @param __x A %binomial_distribution random number distribution.
3542 * @returns The output stream with the state of @p __x inserted or in
3545 template<typename _IntType1
,
3546 typename _CharT
, typename _Traits
>
3547 friend std::basic_ostream
<_CharT
, _Traits
>&
3548 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3549 const std::binomial_distribution
<_IntType1
>&);
3552 * @brief Extracts a %binomial_distribution random number distribution
3553 * @p __x from the input stream @p __is.
3555 * @param __is An input stream.
3556 * @param __x A %binomial_distribution random number generator engine.
3558 * @returns The input stream with @p __x extracted or in an error
3561 template<typename _IntType1
,
3562 typename _CharT
, typename _Traits
>
3563 friend std::basic_istream
<_CharT
, _Traits
>&
3564 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3565 std::binomial_distribution
<_IntType1
>&);
3568 template<typename _UniformRandomNumberGenerator
>
3570 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3572 param_type _M_param
;
3574 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3575 std::normal_distribution
<double> _M_nd
;
3579 * @brief Return true if two binomial distributions are different.
3581 template<typename _IntType
>
3583 operator!=(const std::binomial_distribution
<_IntType
>& __d1
,
3584 const std::binomial_distribution
<_IntType
>& __d2
)
3585 { return !(__d1
== __d2
); }
3589 * @brief A discrete geometric random number distribution.
3591 * The formula for the geometric probability density function is
3592 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3595 template<typename _IntType
= int>
3596 class geometric_distribution
3598 static_assert(std::is_integral
<_IntType
>::value
,
3599 "template argument not an integral type");
3602 /** The type of the range of the distribution. */
3603 typedef _IntType result_type
;
3604 /** Parameter type. */
3607 typedef geometric_distribution
<_IntType
> distribution_type
;
3608 friend class geometric_distribution
<_IntType
>;
3611 param_type(double __p
= 0.5)
3614 _GLIBCXX_DEBUG_ASSERT((_M_p
> 0.0) && (_M_p
< 1.0));
3623 operator==(const param_type
& __p1
, const param_type
& __p2
)
3624 { return __p1
._M_p
== __p2
._M_p
; }
3629 { _M_log_1_p
= std::log(1.0 - _M_p
); }
3636 // constructors and member function
3638 geometric_distribution(double __p
= 0.5)
3643 geometric_distribution(const param_type
& __p
)
3648 * @brief Resets the distribution state.
3650 * Does nothing for the geometric distribution.
3656 * @brief Returns the distribution parameter @p p.
3660 { return _M_param
.p(); }
3663 * @brief Returns the parameter set of the distribution.
3667 { return _M_param
; }
3670 * @brief Sets the parameter set of the distribution.
3671 * @param __param The new parameter set of the distribution.
3674 param(const param_type
& __param
)
3675 { _M_param
= __param
; }
3678 * @brief Returns the greatest lower bound value of the distribution.
3685 * @brief Returns the least upper bound value of the distribution.
3689 { return std::numeric_limits
<result_type
>::max(); }
3692 * @brief Generating functions.
3694 template<typename _UniformRandomNumberGenerator
>
3696 operator()(_UniformRandomNumberGenerator
& __urng
)
3697 { return this->operator()(__urng
, this->param()); }
3699 template<typename _UniformRandomNumberGenerator
>
3701 operator()(_UniformRandomNumberGenerator
& __urng
,
3702 const param_type
& __p
);
3705 param_type _M_param
;
3709 * @brief Return true if two geometric distributions have
3710 * the same parameters.
3712 template<typename _IntType
>
3714 operator==(const std::geometric_distribution
<_IntType
>& __d1
,
3715 const std::geometric_distribution
<_IntType
>& __d2
)
3716 { return __d1
.param() == __d2
.param(); }
3719 * @brief Return true if two geometric distributions have
3720 * different parameters.
3722 template<typename _IntType
>
3724 operator!=(const std::geometric_distribution
<_IntType
>& __d1
,
3725 const std::geometric_distribution
<_IntType
>& __d2
)
3726 { return !(__d1
== __d2
); }
3729 * @brief Inserts a %geometric_distribution random number distribution
3730 * @p __x into the output stream @p __os.
3732 * @param __os An output stream.
3733 * @param __x A %geometric_distribution random number distribution.
3735 * @returns The output stream with the state of @p __x inserted or in
3738 template<typename _IntType
,
3739 typename _CharT
, typename _Traits
>
3740 std::basic_ostream
<_CharT
, _Traits
>&
3741 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3742 const std::geometric_distribution
<_IntType
>&);
3745 * @brief Extracts a %geometric_distribution random number distribution
3746 * @p __x from the input stream @p __is.
3748 * @param __is An input stream.
3749 * @param __x A %geometric_distribution random number generator engine.
3751 * @returns The input stream with @p __x extracted or in an error state.
3753 template<typename _IntType
,
3754 typename _CharT
, typename _Traits
>
3755 std::basic_istream
<_CharT
, _Traits
>&
3756 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3757 std::geometric_distribution
<_IntType
>&);
3761 * @brief A negative_binomial_distribution random number distribution.
3763 * The formula for the negative binomial probability mass function is
3764 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3765 * and @f$p@f$ are the parameters of the distribution.
3767 template<typename _IntType
= int>
3768 class negative_binomial_distribution
3770 static_assert(std::is_integral
<_IntType
>::value
,
3771 "template argument not an integral type");
3774 /** The type of the range of the distribution. */
3775 typedef _IntType result_type
;
3776 /** Parameter type. */
3779 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3782 param_type(_IntType __k
= 1, double __p
= 0.5)
3783 : _M_k(__k
), _M_p(__p
)
3785 _GLIBCXX_DEBUG_ASSERT((_M_k
> 0) && (_M_p
> 0.0) && (_M_p
<= 1.0));
3797 operator==(const param_type
& __p1
, const param_type
& __p2
)
3798 { return __p1
._M_k
== __p2
._M_k
&& __p1
._M_p
== __p2
._M_p
; }
3806 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3807 : _M_param(__k
, __p
), _M_gd(__k
, (1.0 - __p
) / __p
)
3811 negative_binomial_distribution(const param_type
& __p
)
3812 : _M_param(__p
), _M_gd(__p
.k(), (1.0 - __p
.p()) / __p
.p())
3816 * @brief Resets the distribution state.
3823 * @brief Return the @f$k@f$ parameter of the distribution.
3827 { return _M_param
.k(); }
3830 * @brief Return the @f$p@f$ parameter of the distribution.
3834 { return _M_param
.p(); }
3837 * @brief Returns the parameter set of the distribution.
3841 { return _M_param
; }
3844 * @brief Sets the parameter set of the distribution.
3845 * @param __param The new parameter set of the distribution.
3848 param(const param_type
& __param
)
3849 { _M_param
= __param
; }
3852 * @brief Returns the greatest lower bound value of the distribution.
3856 { return result_type(0); }
3859 * @brief Returns the least upper bound value of the distribution.
3863 { return std::numeric_limits
<result_type
>::max(); }
3866 * @brief Generating functions.
3868 template<typename _UniformRandomNumberGenerator
>
3870 operator()(_UniformRandomNumberGenerator
& __urng
);
3872 template<typename _UniformRandomNumberGenerator
>
3874 operator()(_UniformRandomNumberGenerator
& __urng
,
3875 const param_type
& __p
);
3878 * @brief Return true if two negative binomial distributions have
3879 * the same parameters and the sequences that would be
3880 * generated are equal.
3882 template<typename _IntType1
>
3884 operator==(const std::negative_binomial_distribution
<_IntType1
>& __d1
,
3885 const std::negative_binomial_distribution
<_IntType1
>& __d2
)
3886 { return __d1
.param() == __d2
.param() && __d1
._M_gd
== __d2
._M_gd
; }
3889 * @brief Inserts a %negative_binomial_distribution random
3890 * number distribution @p __x into the output stream @p __os.
3892 * @param __os An output stream.
3893 * @param __x A %negative_binomial_distribution random number
3896 * @returns The output stream with the state of @p __x inserted or in
3899 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3900 friend std::basic_ostream
<_CharT
, _Traits
>&
3901 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3902 const std::negative_binomial_distribution
<_IntType1
>&);
3905 * @brief Extracts a %negative_binomial_distribution random number
3906 * distribution @p __x from the input stream @p __is.
3908 * @param __is An input stream.
3909 * @param __x A %negative_binomial_distribution random number
3912 * @returns The input stream with @p __x extracted or in an error state.
3914 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3915 friend std::basic_istream
<_CharT
, _Traits
>&
3916 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3917 std::negative_binomial_distribution
<_IntType1
>&);
3920 param_type _M_param
;
3922 std::gamma_distribution
<double> _M_gd
;
3926 * @brief Return true if two negative binomial distributions are different.
3928 template<typename _IntType
>
3930 operator!=(const std::negative_binomial_distribution
<_IntType
>& __d1
,
3931 const std::negative_binomial_distribution
<_IntType
>& __d2
)
3932 { return !(__d1
== __d2
); }
3935 /* @} */ // group random_distributions_bernoulli
3938 * @addtogroup random_distributions_poisson Poisson Distributions
3939 * @ingroup random_distributions
3944 * @brief A discrete Poisson random number distribution.
3946 * The formula for the Poisson probability density function is
3947 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3948 * parameter of the distribution.
3950 template<typename _IntType
= int>
3951 class poisson_distribution
3953 static_assert(std::is_integral
<_IntType
>::value
,
3954 "template argument not an integral type");
3957 /** The type of the range of the distribution. */
3958 typedef _IntType result_type
;
3959 /** Parameter type. */
3962 typedef poisson_distribution
<_IntType
> distribution_type
;
3963 friend class poisson_distribution
<_IntType
>;
3966 param_type(double __mean
= 1.0)
3969 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3978 operator==(const param_type
& __p1
, const param_type
& __p2
)
3979 { return __p1
._M_mean
== __p2
._M_mean
; }
3982 // Hosts either log(mean) or the threshold of the simple method.
3989 #if _GLIBCXX_USE_C99_MATH_TR1
3990 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3994 // constructors and member function
3996 poisson_distribution(double __mean
= 1.0)
3997 : _M_param(__mean
), _M_nd()
4001 poisson_distribution(const param_type
& __p
)
4002 : _M_param(__p
), _M_nd()
4006 * @brief Resets the distribution state.
4013 * @brief Returns the distribution parameter @p mean.
4017 { return _M_param
.mean(); }
4020 * @brief Returns the parameter set of the distribution.
4024 { return _M_param
; }
4027 * @brief Sets the parameter set of the distribution.
4028 * @param __param The new parameter set of the distribution.
4031 param(const param_type
& __param
)
4032 { _M_param
= __param
; }
4035 * @brief Returns the greatest lower bound value of the distribution.
4042 * @brief Returns the least upper bound value of the distribution.
4046 { return std::numeric_limits
<result_type
>::max(); }
4049 * @brief Generating functions.
4051 template<typename _UniformRandomNumberGenerator
>
4053 operator()(_UniformRandomNumberGenerator
& __urng
)
4054 { return this->operator()(__urng
, this->param()); }
4056 template<typename _UniformRandomNumberGenerator
>
4058 operator()(_UniformRandomNumberGenerator
& __urng
,
4059 const param_type
& __p
);
4062 * @brief Return true if two Poisson distributions have the same
4063 * parameters and the sequences that would be generated
4066 template<typename _IntType1
>
4068 operator==(const std::poisson_distribution
<_IntType1
>& __d1
,
4069 const std::poisson_distribution
<_IntType1
>& __d2
)
4070 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4071 { return __d1
.param() == __d2
.param() && __d1
._M_nd
== __d2
._M_nd
; }
4073 { return __d1
.param() == __d2
.param(); }
4077 * @brief Inserts a %poisson_distribution random number distribution
4078 * @p __x into the output stream @p __os.
4080 * @param __os An output stream.
4081 * @param __x A %poisson_distribution random number distribution.
4083 * @returns The output stream with the state of @p __x inserted or in
4086 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4087 friend std::basic_ostream
<_CharT
, _Traits
>&
4088 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4089 const std::poisson_distribution
<_IntType1
>&);
4092 * @brief Extracts a %poisson_distribution random number distribution
4093 * @p __x from the input stream @p __is.
4095 * @param __is An input stream.
4096 * @param __x A %poisson_distribution random number generator engine.
4098 * @returns The input stream with @p __x extracted or in an error
4101 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4102 friend std::basic_istream
<_CharT
, _Traits
>&
4103 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4104 std::poisson_distribution
<_IntType1
>&);
4107 param_type _M_param
;
4109 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4110 std::normal_distribution
<double> _M_nd
;
4114 * @brief Return true if two Poisson distributions are different.
4116 template<typename _IntType
>
4118 operator!=(const std::poisson_distribution
<_IntType
>& __d1
,
4119 const std::poisson_distribution
<_IntType
>& __d2
)
4120 { return !(__d1
== __d2
); }
4124 * @brief An exponential continuous distribution for random numbers.
4126 * The formula for the exponential probability density function is
4127 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4129 * <table border=1 cellpadding=10 cellspacing=0>
4130 * <caption align=top>Distribution Statistics</caption>
4131 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4132 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4133 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4134 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4135 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4138 template<typename _RealType
= double>
4139 class exponential_distribution
4141 static_assert(std::is_floating_point
<_RealType
>::value
,
4142 "template argument not a floating point type");
4145 /** The type of the range of the distribution. */
4146 typedef _RealType result_type
;
4147 /** Parameter type. */
4150 typedef exponential_distribution
<_RealType
> distribution_type
;
4153 param_type(_RealType __lambda
= _RealType(1))
4154 : _M_lambda(__lambda
)
4156 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
4161 { return _M_lambda
; }
4164 operator==(const param_type
& __p1
, const param_type
& __p2
)
4165 { return __p1
._M_lambda
== __p2
._M_lambda
; }
4168 _RealType _M_lambda
;
4173 * @brief Constructs an exponential distribution with inverse scale
4174 * parameter @f$\lambda@f$.
4177 exponential_distribution(const result_type
& __lambda
= result_type(1))
4178 : _M_param(__lambda
)
4182 exponential_distribution(const param_type
& __p
)
4187 * @brief Resets the distribution state.
4189 * Has no effect on exponential distributions.
4195 * @brief Returns the inverse scale parameter of the distribution.
4199 { return _M_param
.lambda(); }
4202 * @brief Returns the parameter set of the distribution.
4206 { return _M_param
; }
4209 * @brief Sets the parameter set of the distribution.
4210 * @param __param The new parameter set of the distribution.
4213 param(const param_type
& __param
)
4214 { _M_param
= __param
; }
4217 * @brief Returns the greatest lower bound value of the distribution.
4221 { return result_type(0); }
4224 * @brief Returns the least upper bound value of the distribution.
4228 { return std::numeric_limits
<result_type
>::max(); }
4231 * @brief Generating functions.
4233 template<typename _UniformRandomNumberGenerator
>
4235 operator()(_UniformRandomNumberGenerator
& __urng
)
4236 { return this->operator()(__urng
, this->param()); }
4238 template<typename _UniformRandomNumberGenerator
>
4240 operator()(_UniformRandomNumberGenerator
& __urng
,
4241 const param_type
& __p
)
4243 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
4245 return -std::log(__aurng()) / __p
.lambda();
4249 param_type _M_param
;
4253 * @brief Return true if two exponential distributions have the same
4256 template<typename _RealType
>
4258 operator==(const std::exponential_distribution
<_RealType
>& __d1
,
4259 const std::exponential_distribution
<_RealType
>& __d2
)
4260 { return __d1
.param() == __d2
.param(); }
4263 * @brief Return true if two exponential distributions have different
4266 template<typename _RealType
>
4268 operator!=(const std::exponential_distribution
<_RealType
>& __d1
,
4269 const std::exponential_distribution
<_RealType
>& __d2
)
4270 { return !(__d1
== __d2
); }
4273 * @brief Inserts a %exponential_distribution random number distribution
4274 * @p __x into the output stream @p __os.
4276 * @param __os An output stream.
4277 * @param __x A %exponential_distribution random number distribution.
4279 * @returns The output stream with the state of @p __x inserted or in
4282 template<typename _RealType
, typename _CharT
, typename _Traits
>
4283 std::basic_ostream
<_CharT
, _Traits
>&
4284 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4285 const std::exponential_distribution
<_RealType
>&);
4288 * @brief Extracts a %exponential_distribution random number distribution
4289 * @p __x from the input stream @p __is.
4291 * @param __is An input stream.
4292 * @param __x A %exponential_distribution random number
4295 * @returns The input stream with @p __x extracted or in an error state.
4297 template<typename _RealType
, typename _CharT
, typename _Traits
>
4298 std::basic_istream
<_CharT
, _Traits
>&
4299 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4300 std::exponential_distribution
<_RealType
>&);
4304 * @brief A weibull_distribution random number distribution.
4306 * The formula for the normal probability density function is:
4308 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4309 * \exp{(-(\frac{x}{\beta})^\alpha)}
4312 template<typename _RealType
= double>
4313 class weibull_distribution
4315 static_assert(std::is_floating_point
<_RealType
>::value
,
4316 "template argument not a floating point type");
4319 /** The type of the range of the distribution. */
4320 typedef _RealType result_type
;
4321 /** Parameter type. */
4324 typedef weibull_distribution
<_RealType
> distribution_type
;
4327 param_type(_RealType __a
= _RealType(1),
4328 _RealType __b
= _RealType(1))
4329 : _M_a(__a
), _M_b(__b
)
4341 operator==(const param_type
& __p1
, const param_type
& __p2
)
4342 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4350 weibull_distribution(_RealType __a
= _RealType(1),
4351 _RealType __b
= _RealType(1))
4352 : _M_param(__a
, __b
)
4356 weibull_distribution(const param_type
& __p
)
4361 * @brief Resets the distribution state.
4368 * @brief Return the @f$a@f$ parameter of the distribution.
4372 { return _M_param
.a(); }
4375 * @brief Return the @f$b@f$ parameter of the distribution.
4379 { return _M_param
.b(); }
4382 * @brief Returns the parameter set of the distribution.
4386 { return _M_param
; }
4389 * @brief Sets the parameter set of the distribution.
4390 * @param __param The new parameter set of the distribution.
4393 param(const param_type
& __param
)
4394 { _M_param
= __param
; }
4397 * @brief Returns the greatest lower bound value of the distribution.
4401 { return result_type(0); }
4404 * @brief Returns the least upper bound value of the distribution.
4408 { return std::numeric_limits
<result_type
>::max(); }
4411 * @brief Generating functions.
4413 template<typename _UniformRandomNumberGenerator
>
4415 operator()(_UniformRandomNumberGenerator
& __urng
)
4416 { return this->operator()(__urng
, this->param()); }
4418 template<typename _UniformRandomNumberGenerator
>
4420 operator()(_UniformRandomNumberGenerator
& __urng
,
4421 const param_type
& __p
);
4424 param_type _M_param
;
4428 * @brief Return true if two Weibull distributions have the same
4431 template<typename _RealType
>
4433 operator==(const std::weibull_distribution
<_RealType
>& __d1
,
4434 const std::weibull_distribution
<_RealType
>& __d2
)
4435 { return __d1
.param() == __d2
.param(); }
4438 * @brief Return true if two Weibull distributions have different
4441 template<typename _RealType
>
4443 operator!=(const std::weibull_distribution
<_RealType
>& __d1
,
4444 const std::weibull_distribution
<_RealType
>& __d2
)
4445 { return !(__d1
== __d2
); }
4448 * @brief Inserts a %weibull_distribution random number distribution
4449 * @p __x into the output stream @p __os.
4451 * @param __os An output stream.
4452 * @param __x A %weibull_distribution random number distribution.
4454 * @returns The output stream with the state of @p __x inserted or in
4457 template<typename _RealType
, typename _CharT
, typename _Traits
>
4458 std::basic_ostream
<_CharT
, _Traits
>&
4459 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4460 const std::weibull_distribution
<_RealType
>&);
4463 * @brief Extracts a %weibull_distribution random number distribution
4464 * @p __x from the input stream @p __is.
4466 * @param __is An input stream.
4467 * @param __x A %weibull_distribution random number
4470 * @returns The input stream with @p __x extracted or in an error state.
4472 template<typename _RealType
, typename _CharT
, typename _Traits
>
4473 std::basic_istream
<_CharT
, _Traits
>&
4474 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4475 std::weibull_distribution
<_RealType
>&);
4479 * @brief A extreme_value_distribution random number distribution.
4481 * The formula for the normal probability mass function is
4483 * p(x|a,b) = \frac{1}{b}
4484 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4487 template<typename _RealType
= double>
4488 class extreme_value_distribution
4490 static_assert(std::is_floating_point
<_RealType
>::value
,
4491 "template argument not a floating point type");
4494 /** The type of the range of the distribution. */
4495 typedef _RealType result_type
;
4496 /** Parameter type. */
4499 typedef extreme_value_distribution
<_RealType
> distribution_type
;
4502 param_type(_RealType __a
= _RealType(0),
4503 _RealType __b
= _RealType(1))
4504 : _M_a(__a
), _M_b(__b
)
4516 operator==(const param_type
& __p1
, const param_type
& __p2
)
4517 { return __p1
._M_a
== __p2
._M_a
&& __p1
._M_b
== __p2
._M_b
; }
4525 extreme_value_distribution(_RealType __a
= _RealType(0),
4526 _RealType __b
= _RealType(1))
4527 : _M_param(__a
, __b
)
4531 extreme_value_distribution(const param_type
& __p
)
4536 * @brief Resets the distribution state.
4543 * @brief Return the @f$a@f$ parameter of the distribution.
4547 { return _M_param
.a(); }
4550 * @brief Return the @f$b@f$ parameter of the distribution.
4554 { return _M_param
.b(); }
4557 * @brief Returns the parameter set of the distribution.
4561 { return _M_param
; }
4564 * @brief Sets the parameter set of the distribution.
4565 * @param __param The new parameter set of the distribution.
4568 param(const param_type
& __param
)
4569 { _M_param
= __param
; }
4572 * @brief Returns the greatest lower bound value of the distribution.
4576 { return std::numeric_limits
<result_type
>::min(); }
4579 * @brief Returns the least upper bound value of the distribution.
4583 { return std::numeric_limits
<result_type
>::max(); }
4586 * @brief Generating functions.
4588 template<typename _UniformRandomNumberGenerator
>
4590 operator()(_UniformRandomNumberGenerator
& __urng
)
4591 { return this->operator()(__urng
, this->param()); }
4593 template<typename _UniformRandomNumberGenerator
>
4595 operator()(_UniformRandomNumberGenerator
& __urng
,
4596 const param_type
& __p
);
4599 param_type _M_param
;
4603 * @brief Return true if two extreme value distributions have the same
4606 template<typename _RealType
>
4608 operator==(const std::extreme_value_distribution
<_RealType
>& __d1
,
4609 const std::extreme_value_distribution
<_RealType
>& __d2
)
4610 { return __d1
.param() == __d2
.param(); }
4613 * @brief Return true if two extreme value distributions have different
4616 template<typename _RealType
>
4618 operator!=(const std::extreme_value_distribution
<_RealType
>& __d1
,
4619 const std::extreme_value_distribution
<_RealType
>& __d2
)
4620 { return !(__d1
== __d2
); }
4623 * @brief Inserts a %extreme_value_distribution random number distribution
4624 * @p __x into the output stream @p __os.
4626 * @param __os An output stream.
4627 * @param __x A %extreme_value_distribution random number distribution.
4629 * @returns The output stream with the state of @p __x inserted or in
4632 template<typename _RealType
, typename _CharT
, typename _Traits
>
4633 std::basic_ostream
<_CharT
, _Traits
>&
4634 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4635 const std::extreme_value_distribution
<_RealType
>&);
4638 * @brief Extracts a %extreme_value_distribution random number
4639 * distribution @p __x from the input stream @p __is.
4641 * @param __is An input stream.
4642 * @param __x A %extreme_value_distribution random number
4645 * @returns The input stream with @p __x extracted or in an error state.
4647 template<typename _RealType
, typename _CharT
, typename _Traits
>
4648 std::basic_istream
<_CharT
, _Traits
>&
4649 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4650 std::extreme_value_distribution
<_RealType
>&);
4654 * @brief A discrete_distribution random number distribution.
4656 * The formula for the discrete probability mass function is
4659 template<typename _IntType
= int>
4660 class discrete_distribution
4662 static_assert(std::is_integral
<_IntType
>::value
,
4663 "template argument not an integral type");
4666 /** The type of the range of the distribution. */
4667 typedef _IntType result_type
;
4668 /** Parameter type. */
4671 typedef discrete_distribution
<_IntType
> distribution_type
;
4672 friend class discrete_distribution
<_IntType
>;
4675 : _M_prob(), _M_cp()
4678 template<typename _InputIterator
>
4679 param_type(_InputIterator __wbegin
,
4680 _InputIterator __wend
)
4681 : _M_prob(__wbegin
, __wend
), _M_cp()
4682 { _M_initialize(); }
4684 param_type(initializer_list
<double> __wil
)
4685 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4686 { _M_initialize(); }
4688 template<typename _Func
>
4689 param_type(size_t __nw
, double __xmin
, double __xmax
,
4692 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4693 param_type(const param_type
&) = default;
4694 param_type
& operator=(const param_type
&) = default;
4697 probabilities() const
4698 { return _M_prob
.empty() ? std::vector
<double>(1, 1.0) : _M_prob
; }
4701 operator==(const param_type
& __p1
, const param_type
& __p2
)
4702 { return __p1
._M_prob
== __p2
._M_prob
; }
4708 std::vector
<double> _M_prob
;
4709 std::vector
<double> _M_cp
;
4712 discrete_distribution()
4716 template<typename _InputIterator
>
4717 discrete_distribution(_InputIterator __wbegin
,
4718 _InputIterator __wend
)
4719 : _M_param(__wbegin
, __wend
)
4722 discrete_distribution(initializer_list
<double> __wl
)
4726 template<typename _Func
>
4727 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4729 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4733 discrete_distribution(const param_type
& __p
)
4738 * @brief Resets the distribution state.
4745 * @brief Returns the probabilities of the distribution.
4748 probabilities() const
4750 return _M_param
._M_prob
.empty()
4751 ? std::vector
<double>(1, 1.0) : _M_param
._M_prob
;
4755 * @brief Returns the parameter set of the distribution.
4759 { return _M_param
; }
4762 * @brief Sets the parameter set of the distribution.
4763 * @param __param The new parameter set of the distribution.
4766 param(const param_type
& __param
)
4767 { _M_param
= __param
; }
4770 * @brief Returns the greatest lower bound value of the distribution.
4774 { return result_type(0); }
4777 * @brief Returns the least upper bound value of the distribution.
4782 return _M_param
._M_prob
.empty()
4783 ? result_type(0) : result_type(_M_param
._M_prob
.size() - 1);
4787 * @brief Generating functions.
4789 template<typename _UniformRandomNumberGenerator
>
4791 operator()(_UniformRandomNumberGenerator
& __urng
)
4792 { return this->operator()(__urng
, this->param()); }
4794 template<typename _UniformRandomNumberGenerator
>
4796 operator()(_UniformRandomNumberGenerator
& __urng
,
4797 const param_type
& __p
);
4800 * @brief Inserts a %discrete_distribution random number distribution
4801 * @p __x into the output stream @p __os.
4803 * @param __os An output stream.
4804 * @param __x A %discrete_distribution random number distribution.
4806 * @returns The output stream with the state of @p __x inserted or in
4809 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4810 friend std::basic_ostream
<_CharT
, _Traits
>&
4811 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4812 const std::discrete_distribution
<_IntType1
>&);
4815 * @brief Extracts a %discrete_distribution random number distribution
4816 * @p __x from the input stream @p __is.
4818 * @param __is An input stream.
4819 * @param __x A %discrete_distribution random number
4822 * @returns The input stream with @p __x extracted or in an error
4825 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4826 friend std::basic_istream
<_CharT
, _Traits
>&
4827 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4828 std::discrete_distribution
<_IntType1
>&);
4831 param_type _M_param
;
4835 * @brief Return true if two discrete distributions have the same
4838 template<typename _IntType
>
4840 operator==(const std::discrete_distribution
<_IntType
>& __d1
,
4841 const std::discrete_distribution
<_IntType
>& __d2
)
4842 { return __d1
.param() == __d2
.param(); }
4845 * @brief Return true if two discrete distributions have different
4848 template<typename _IntType
>
4850 operator!=(const std::discrete_distribution
<_IntType
>& __d1
,
4851 const std::discrete_distribution
<_IntType
>& __d2
)
4852 { return !(__d1
== __d2
); }
4856 * @brief A piecewise_constant_distribution random number distribution.
4858 * The formula for the piecewise constant probability mass function is
4861 template<typename _RealType
= double>
4862 class piecewise_constant_distribution
4864 static_assert(std::is_floating_point
<_RealType
>::value
,
4865 "template argument not a floating point type");
4868 /** The type of the range of the distribution. */
4869 typedef _RealType result_type
;
4870 /** Parameter type. */
4873 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4874 friend class piecewise_constant_distribution
<_RealType
>;
4877 : _M_int(), _M_den(), _M_cp()
4880 template<typename _InputIteratorB
, typename _InputIteratorW
>
4881 param_type(_InputIteratorB __bfirst
,
4882 _InputIteratorB __bend
,
4883 _InputIteratorW __wbegin
);
4885 template<typename _Func
>
4886 param_type(initializer_list
<_RealType
> __bi
, _Func __fw
);
4888 template<typename _Func
>
4889 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4892 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
4893 param_type(const param_type
&) = default;
4894 param_type
& operator=(const param_type
&) = default;
4896 std::vector
<_RealType
>
4901 std::vector
<_RealType
> __tmp(2);
4902 __tmp
[1] = _RealType(1);
4911 { return _M_den
.empty() ? std::vector
<double>(1, 1.0) : _M_den
; }
4914 operator==(const param_type
& __p1
, const param_type
& __p2
)
4915 { return __p1
._M_int
== __p2
._M_int
&& __p1
._M_den
== __p2
._M_den
; }
4921 std::vector
<_RealType
> _M_int
;
4922 std::vector
<double> _M_den
;
4923 std::vector
<double> _M_cp
;
4927 piecewise_constant_distribution()
4931 template<typename _InputIteratorB
, typename _InputIteratorW
>
4932 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4933 _InputIteratorB __bend
,
4934 _InputIteratorW __wbegin
)
4935 : _M_param(__bfirst
, __bend
, __wbegin
)
4938 template<typename _Func
>
4939 piecewise_constant_distribution(initializer_list
<_RealType
> __bl
,
4941 : _M_param(__bl
, __fw
)
4944 template<typename _Func
>
4945 piecewise_constant_distribution(size_t __nw
,
4946 _RealType __xmin
, _RealType __xmax
,
4948 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4952 piecewise_constant_distribution(const param_type
& __p
)
4957 * @brief Resets the distribution state.
4964 * @brief Returns a vector of the intervals.
4966 std::vector
<_RealType
>
4969 if (_M_param
._M_int
.empty())
4971 std::vector
<_RealType
> __tmp(2);
4972 __tmp
[1] = _RealType(1);
4976 return _M_param
._M_int
;
4980 * @brief Returns a vector of the probability densities.
4985 return _M_param
._M_den
.empty()
4986 ? std::vector
<double>(1, 1.0) : _M_param
._M_den
;
4990 * @brief Returns the parameter set of the distribution.
4994 { return _M_param
; }
4997 * @brief Sets the parameter set of the distribution.
4998 * @param __param The new parameter set of the distribution.
5001 param(const param_type
& __param
)
5002 { _M_param
= __param
; }
5005 * @brief Returns the greatest lower bound value of the distribution.
5010 return _M_param
._M_int
.empty()
5011 ? result_type(0) : _M_param
._M_int
.front();
5015 * @brief Returns the least upper bound value of the distribution.
5020 return _M_param
._M_int
.empty()
5021 ? result_type(1) : _M_param
._M_int
.back();
5025 * @brief Generating functions.
5027 template<typename _UniformRandomNumberGenerator
>
5029 operator()(_UniformRandomNumberGenerator
& __urng
)
5030 { return this->operator()(__urng
, this->param()); }
5032 template<typename _UniformRandomNumberGenerator
>
5034 operator()(_UniformRandomNumberGenerator
& __urng
,
5035 const param_type
& __p
);
5038 * @brief Inserts a %piecewise_constan_distribution random
5039 * number distribution @p __x into the output stream @p __os.
5041 * @param __os An output stream.
5042 * @param __x A %piecewise_constan_distribution random number
5045 * @returns The output stream with the state of @p __x inserted or in
5048 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5049 friend std::basic_ostream
<_CharT
, _Traits
>&
5050 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
5051 const std::piecewise_constant_distribution
<_RealType1
>&);
5054 * @brief Extracts a %piecewise_constan_distribution random
5055 * number distribution @p __x from the input stream @p __is.
5057 * @param __is An input stream.
5058 * @param __x A %piecewise_constan_distribution random number
5061 * @returns The input stream with @p __x extracted or in an error
5064 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5065 friend std::basic_istream
<_CharT
, _Traits
>&
5066 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5067 std::piecewise_constant_distribution
<_RealType1
>&);
5070 param_type _M_param
;
5074 * @brief Return true if two piecewise constant distributions have the
5077 template<typename _RealType
>
5079 operator==(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5080 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5081 { return __d1
.param() == __d2
.param(); }
5084 * @brief Return true if two piecewise constant distributions have
5085 * different parameters.
5087 template<typename _RealType
>
5089 operator!=(const std::piecewise_constant_distribution
<_RealType
>& __d1
,
5090 const std::piecewise_constant_distribution
<_RealType
>& __d2
)
5091 { return !(__d1
== __d2
); }
5095 * @brief A piecewise_linear_distribution random number distribution.
5097 * The formula for the piecewise linear probability mass function is
5100 template<typename _RealType
= double>
5101 class piecewise_linear_distribution
5103 static_assert(std::is_floating_point
<_RealType
>::value
,
5104 "template argument not a floating point type");
5107 /** The type of the range of the distribution. */
5108 typedef _RealType result_type
;
5109 /** Parameter type. */
5112 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
5113 friend class piecewise_linear_distribution
<_RealType
>;
5116 : _M_int(), _M_den(), _M_cp(), _M_m()
5119 template<typename _InputIteratorB
, typename _InputIteratorW
>
5120 param_type(_InputIteratorB __bfirst
,
5121 _InputIteratorB __bend
,
5122 _InputIteratorW __wbegin
);
5124 template<typename _Func
>
5125 param_type(initializer_list
<_RealType
> __bl
, _Func __fw
);
5127 template<typename _Func
>
5128 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
5131 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5132 param_type(const param_type
&) = default;
5133 param_type
& operator=(const param_type
&) = default;
5135 std::vector
<_RealType
>
5140 std::vector
<_RealType
> __tmp(2);
5141 __tmp
[1] = _RealType(1);
5150 { return _M_den
.empty() ? std::vector
<double>(2, 1.0) : _M_den
; }
5153 operator==(const param_type
& __p1
, const param_type
& __p2
)
5154 { return (__p1
._M_int
== __p2
._M_int
5155 && __p1
._M_den
== __p2
._M_den
); }
5161 std::vector
<_RealType
> _M_int
;
5162 std::vector
<double> _M_den
;
5163 std::vector
<double> _M_cp
;
5164 std::vector
<double> _M_m
;
5168 piecewise_linear_distribution()
5172 template<typename _InputIteratorB
, typename _InputIteratorW
>
5173 piecewise_linear_distribution(_InputIteratorB __bfirst
,
5174 _InputIteratorB __bend
,
5175 _InputIteratorW __wbegin
)
5176 : _M_param(__bfirst
, __bend
, __wbegin
)
5179 template<typename _Func
>
5180 piecewise_linear_distribution(initializer_list
<_RealType
> __bl
,
5182 : _M_param(__bl
, __fw
)
5185 template<typename _Func
>
5186 piecewise_linear_distribution(size_t __nw
,
5187 _RealType __xmin
, _RealType __xmax
,
5189 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
5193 piecewise_linear_distribution(const param_type
& __p
)
5198 * Resets the distribution state.
5205 * @brief Return the intervals of the distribution.
5207 std::vector
<_RealType
>
5210 if (_M_param
._M_int
.empty())
5212 std::vector
<_RealType
> __tmp(2);
5213 __tmp
[1] = _RealType(1);
5217 return _M_param
._M_int
;
5221 * @brief Return a vector of the probability densities of the
5227 return _M_param
._M_den
.empty()
5228 ? std::vector
<double>(2, 1.0) : _M_param
._M_den
;
5232 * @brief Returns the parameter set of the distribution.
5236 { return _M_param
; }
5239 * @brief Sets the parameter set of the distribution.
5240 * @param __param The new parameter set of the distribution.
5243 param(const param_type
& __param
)
5244 { _M_param
= __param
; }
5247 * @brief Returns the greatest lower bound value of the distribution.
5252 return _M_param
._M_int
.empty()
5253 ? result_type(0) : _M_param
._M_int
.front();
5257 * @brief Returns the least upper bound value of the distribution.
5262 return _M_param
._M_int
.empty()
5263 ? result_type(1) : _M_param
._M_int
.back();
5267 * @brief Generating functions.
5269 template<typename _UniformRandomNumberGenerator
>
5271 operator()(_UniformRandomNumberGenerator
& __urng
)
5272 { return this->operator()(__urng
, this->param()); }
5274 template<typename _UniformRandomNumberGenerator
>
5276 operator()(_UniformRandomNumberGenerator
& __urng
,
5277 const param_type
& __p
);
5280 * @brief Inserts a %piecewise_linear_distribution random number
5281 * distribution @p __x into the output stream @p __os.
5283 * @param __os An output stream.
5284 * @param __x A %piecewise_linear_distribution random number
5287 * @returns The output stream with the state of @p __x inserted or in
5290 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5291 friend std::basic_ostream
<_CharT
, _Traits
>&
5292 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
5293 const std::piecewise_linear_distribution
<_RealType1
>&);
5296 * @brief Extracts a %piecewise_linear_distribution random number
5297 * distribution @p __x from the input stream @p __is.
5299 * @param __is An input stream.
5300 * @param __x A %piecewise_linear_distribution random number
5303 * @returns The input stream with @p __x extracted or in an error
5306 template<typename _RealType1
, typename _CharT
, typename _Traits
>
5307 friend std::basic_istream
<_CharT
, _Traits
>&
5308 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
5309 std::piecewise_linear_distribution
<_RealType1
>&);
5312 param_type _M_param
;
5316 * @brief Return true if two piecewise linear distributions have the
5319 template<typename _RealType
>
5321 operator==(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5322 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5323 { return __d1
.param() == __d2
.param(); }
5326 * @brief Return true if two piecewise linear distributions have
5327 * different parameters.
5329 template<typename _RealType
>
5331 operator!=(const std::piecewise_linear_distribution
<_RealType
>& __d1
,
5332 const std::piecewise_linear_distribution
<_RealType
>& __d2
)
5333 { return !(__d1
== __d2
); }
5336 /* @} */ // group random_distributions_poisson
5338 /* @} */ // group random_distributions
5341 * @addtogroup random_utilities Random Number Utilities
5347 * @brief The seed_seq class generates sequences of seeds for random
5348 * number generators.
5354 /** The type of the seed vales. */
5355 typedef uint_least32_t result_type
;
5357 /** Default constructor. */
5362 template<typename _IntType
>
5363 seed_seq(std::initializer_list
<_IntType
> il
);
5365 template<typename _InputIterator
>
5366 seed_seq(_InputIterator __begin
, _InputIterator __end
);
5368 // generating functions
5369 template<typename _RandomAccessIterator
>
5371 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
5373 // property functions
5375 { return _M_v
.size(); }
5377 template<typename OutputIterator
>
5379 param(OutputIterator __dest
) const
5380 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
5384 std::vector
<result_type
> _M_v
;
5387 /* @} */ // group random_utilities
5389 /* @} */ // group random
5391 _GLIBCXX_END_NAMESPACE_VERSION