1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
36 // [26.4] Random number generation
39 * @addtogroup std_random Random Number Generation
40 * A facility for generating random numbers on selected distributions.
45 * @brief A function template for converting the output of a (integral)
46 * uniform random number generator to a floatng point result in the range
49 template<typename _RealType
, size_t __bits
,
50 typename _UniformRandomNumberGenerator
>
52 generate_canonical(_UniformRandomNumberGenerator
& __g
);
57 * Implementation-space details.
61 template<typename _UIntType
, size_t __w
,
62 bool = __w
< static_cast<size_t>
63 (std::numeric_limits
<_UIntType
>::digits
)>
65 { static const _UIntType __value
= 0; };
67 template<typename _UIntType
, size_t __w
>
68 struct _Shift
<_UIntType
, __w
, true>
69 { static const _UIntType __value
= _UIntType(1) << __w
; };
71 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
, bool>
74 // Dispatch based on modulus value to prevent divide-by-zero compile-time
75 // errors when m == 0.
76 template<typename _Tp
, _Tp __a
, _Tp __c
, _Tp __m
>
79 { return _Mod
<_Tp
, __a
, __c
, __m
, __m
== 0>::__calc(__x
); }
81 typedef __gnu_cxx::__conditional_type
<(sizeof(unsigned) == 4),
82 unsigned, unsigned long>::__type _UInt32Type
;
85 * An adaptor class for converting the output of any Generator into
86 * the input for a specific Distribution.
88 template<typename _Engine
, typename _DInputType
>
93 _Adaptor(_Engine
& __g
)
99 if (is_integral
<_DInputType
>::value
)
102 return _DInputType(0);
108 if (is_integral
<_DInputType
>::value
)
111 return _DInputType(1);
115 * Converts a value generated by the adapted random number generator
116 * into a value in the input domain for the dependent random number
119 * Because the type traits are compile time constants only the
120 * appropriate clause of the if statements will actually be emitted
126 if (is_integral
<_DInputType
>::value
)
129 return generate_canonical
<_DInputType
,
130 numeric_limits
<_DInputType
>::digits
,
137 } // namespace __detail
140 * @addtogroup std_random_generators Random Number Generators
141 * @ingroup std_random
143 * These classes define objects which provide random or pseudorandom
144 * numbers, either from a discrete or a continuous interval. The
145 * random number generator supplied as a part of this library are
146 * all uniform random number generators which provide a sequence of
147 * random number uniformly distributed over their range.
149 * A number generator is a function object with an operator() that
150 * takes zero arguments and returns a number.
152 * A compliant random number generator must satisfy the following
153 * requirements. <table border=1 cellpadding=10 cellspacing=0>
154 * <caption align=top>Random Number Generator Requirements</caption>
155 * <tr><td>To be documented.</td></tr> </table>
161 * @brief A model of a linear congruential random number generator.
163 * A random number generator that produces pseudorandom numbers using the
164 * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$.
166 * The template parameter @p _UIntType must be an unsigned integral type
167 * large enough to store values up to (__m-1). If the template parameter
168 * @p __m is 0, the modulus @p __m used is
169 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
170 * parameters @p __a and @p __c must be less than @p __m.
172 * The size of the state is @f$ 1 @f$.
174 template<typename _UIntType
, _UIntType __a
, _UIntType __c
, _UIntType __m
>
175 class linear_congruential_engine
177 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
178 static_assert(__m
== 0 || (__a
< __m
&& __c
< __m
),
179 "template arguments out of bounds"
180 " in linear_congruential_engine");
183 /** The type of the generated random value. */
184 typedef _UIntType result_type
;
186 /** The multiplier. */
187 static const result_type multiplier
= __a
;
189 static const result_type increment
= __c
;
191 static const result_type modulus
= __m
;
192 static const result_type default_seed
= 1u;
195 * @brief Constructs a %linear_congruential_engine random number
196 * generator engine with seed @p __s. The default seed value
199 * @param __s The initial seed value.
202 linear_congruential_engine(result_type __s
= default_seed
)
206 * @brief Constructs a %linear_congruential_engine random number
207 * generator engine seeded from the seed sequence @p __q.
209 * @param __q the seed sequence.
212 linear_congruential_engine(seed_seq
& __q
)
216 * @brief Reseeds the %linear_congruential_engine random number generator
217 * engine sequence to the seed @p __s.
219 * @param __s The new seed.
222 seed(result_type __s
= default_seed
);
225 * @brief Reseeds the %linear_congruential_engine random number generator
227 * sequence using values from the seed sequence @p __q.
229 * @param __q the seed sequence.
235 * @brief Gets the smallest possible value in the output range.
237 * The minimum depends on the @p __c parameter: if it is zero, the
238 * minimum generated must be > 0, otherwise 0 is allowed.
240 * @todo This should be constexpr.
244 { return __c
== 0u ? 1u : 0u; }
247 * @brief Gets the largest possible value in the output range.
249 * @todo This should be constexpr.
256 * @brief Discard a sequence of random numbers.
258 * @todo Look for a faster way to do discard.
261 discard(unsigned long long __z
)
263 for (; __z
!= 0ULL; --__z
)
268 * @brief Gets the next random number in the sequence.
273 _M_x
= __detail::__mod
<_UIntType
, __a
, __c
, __m
>(_M_x
);
278 * @brief Compares two linear congruential random number generator
279 * objects of the same type for equality.
281 * @param __lhs A linear congruential random number generator object.
282 * @param __rhs Another linear congruential random number generator
285 * @returns true if the two objects are equal, false otherwise.
288 operator==(const linear_congruential_engine
& __lhs
,
289 const linear_congruential_engine
& __rhs
)
290 { return __lhs
._M_x
== __rhs
._M_x
; }
293 * @brief Writes the textual representation of the state x(i) of x to
296 * @param __os The output stream.
297 * @param __lcr A % linear_congruential_engine random number generator.
300 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
302 typename _CharT
, typename _Traits
>
303 friend std::basic_ostream
<_CharT
, _Traits
>&
304 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
305 const std::linear_congruential_engine
<_UIntType1
,
309 * @brief Sets the state of the engine by reading its textual
310 * representation from @p __is.
312 * The textual representation must have been previously written using
313 * an output stream whose imbued locale and whose type's template
314 * specialization arguments _CharT and _Traits were the same as those
317 * @param __is The input stream.
318 * @param __lcr A % linear_congruential_engine random number generator.
321 template<typename _UIntType1
, _UIntType1 __a1
, _UIntType1 __c1
,
323 typename _CharT
, typename _Traits
>
324 friend std::basic_istream
<_CharT
, _Traits
>&
325 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
326 std::linear_congruential_engine
<_UIntType1
, __a1
,
335 * A generalized feedback shift register discrete random number generator.
337 * This algorithm avoids multiplication and division and is designed to be
338 * friendly to a pipelined architecture. If the parameters are chosen
339 * correctly, this generator will produce numbers with a very long period and
340 * fairly good apparent entropy, although still not cryptographically strong.
342 * The best way to use this generator is with the predefined mt19937 class.
344 * This algorithm was originally invented by Makoto Matsumoto and
347 * @var word_size The number of bits in each element of the state vector.
348 * @var state_size The degree of recursion.
349 * @var shift_size The period parameter.
350 * @var mask_bits The separation point bit index.
351 * @var parameter_a The last row of the twist matrix.
352 * @var output_u The first right-shift tempering matrix parameter.
353 * @var output_s The first left-shift tempering matrix parameter.
354 * @var output_b The first left-shift tempering matrix mask.
355 * @var output_t The second left-shift tempering matrix parameter.
356 * @var output_c The second left-shift tempering matrix mask.
357 * @var output_l The second right-shift tempering matrix parameter.
359 template<typename _UIntType
, size_t __w
,
360 size_t __n
, size_t __m
, size_t __r
,
361 _UIntType __a
, size_t __u
, _UIntType __d
, size_t __s
,
362 _UIntType __b
, size_t __t
,
363 _UIntType __c
, size_t __l
, _UIntType __f
>
364 class mersenne_twister_engine
366 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
368 static_assert(__m
>= 1U,
369 "mersenne_twister_engine template arguments out of bounds");
370 static_assert(__n
>= __m
,
371 "mersenne_twister_engine template arguments out of bounds");
372 static_assert(__w
>= __r
,
373 "mersenne_twister_engine template arguments out of bounds");
374 static_assert(__w
>= __u
,
375 "mersenne_twister_engine template arguments out of bounds");
376 static_assert(__w
>= __s
,
377 "mersenne_twister_engine template arguments out of bounds");
378 static_assert(__w
>= __t
,
379 "mersenne_twister_engine template arguments out of bounds");
380 static_assert(__w
>= __l
,
381 "mersenne_twister_engine template arguments out of bounds");
383 static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
384 "mersenne_twister_engine template arguments out of bounds");
385 static_assert(__a
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
386 "mersenne_twister_engine template arguments out of bounds");
387 static_assert(__b
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
388 "mersenne_twister_engine template arguments out of bounds");
389 static_assert(__c
<= (__detail::_Shift
<_UIntType
, __w
>::__value
- 1),
390 "mersenne_twister_engine template arguments out of bounds");
393 /** The type of the generated random value. */
394 typedef _UIntType result_type
;
397 static const size_t word_size
= __w
;
398 static const size_t state_size
= __n
;
399 static const size_t shift_size
= __m
;
400 static const size_t mask_bits
= __r
;
401 static const result_type xor_mask
= __a
;
402 static const size_t tempering_u
= __u
;
403 static const result_type tempering_d
= __d
;
404 static const size_t tempering_s
= __s
;
405 static const result_type tempering_b
= __b
;
406 static const size_t tempering_t
= __t
;
407 static const result_type tempering_c
= __c
;
408 static const size_t tempering_l
= __l
;
409 static const size_t initialization_multiplier
= __f
;
410 static const result_type default_seed
= 5489u;
412 // constructors and member function
414 mersenne_twister_engine(result_type __sd
= default_seed
)
418 * @brief Constructs a %mersenne_twister_engine random number generator
419 * engine seeded from the seed sequence @p __q.
421 * @param __q the seed sequence.
424 mersenne_twister_engine(seed_seq
& __q
)
428 seed(result_type __sd
= default_seed
);
434 * @brief Gets the smallest possible value in the output range.
436 * @todo This should be constexpr.
443 * @brief Gets the largest possible value in the output range.
445 * @todo This should be constexpr.
449 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
452 * @brief Discard a sequence of random numbers.
454 * @todo Look for a faster way to do discard.
457 discard(unsigned long long __z
)
459 for (; __z
!= 0ULL; --__z
)
467 * @brief Compares two % mersenne_twister_engine random number generator
468 * objects of the same type for equality.
470 * @param __lhs A % mersenne_twister_engine random number generator
472 * @param __rhs Another % mersenne_twister_engine random number
475 * @returns true if the two objects are equal, false otherwise.
478 operator==(const mersenne_twister_engine
& __lhs
,
479 const mersenne_twister_engine
& __rhs
)
480 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ state_size
, __rhs
._M_x
); }
483 * @brief Inserts the current state of a % mersenne_twister_engine
484 * random number generator engine @p __x into the output stream
487 * @param __os An output stream.
488 * @param __x A % mersenne_twister_engine random number generator
491 * @returns The output stream with the state of @p __x inserted or in
494 template<typename _UIntType1
,
495 size_t __w1
, size_t __n1
,
496 size_t __m1
, size_t __r1
,
497 _UIntType1 __a1
, size_t __u1
,
498 _UIntType1 __d1
, size_t __s1
,
499 _UIntType1 __b1
, size_t __t1
,
500 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
501 typename _CharT
, typename _Traits
>
502 friend std::basic_ostream
<_CharT
, _Traits
>&
503 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
504 const std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
,
505 __m1
, __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
509 * @brief Extracts the current state of a % mersenne_twister_engine
510 * random number generator engine @p __x from the input stream
513 * @param __is An input stream.
514 * @param __x A % mersenne_twister_engine random number generator
517 * @returns The input stream with the state of @p __x extracted or in
520 template<typename _UIntType1
,
521 size_t __w1
, size_t __n1
,
522 size_t __m1
, size_t __r1
,
523 _UIntType1 __a1
, size_t __u1
,
524 _UIntType1 __d1
, size_t __s1
,
525 _UIntType1 __b1
, size_t __t1
,
526 _UIntType1 __c1
, size_t __l1
, _UIntType1 __f1
,
527 typename _CharT
, typename _Traits
>
528 friend std::basic_istream
<_CharT
, _Traits
>&
529 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
530 std::mersenne_twister_engine
<_UIntType1
, __w1
, __n1
, __m1
,
531 __r1
, __a1
, __u1
, __d1
, __s1
, __b1
, __t1
, __c1
,
535 _UIntType _M_x
[state_size
];
540 * @brief The Marsaglia-Zaman generator.
542 * This is a model of a Generalized Fibonacci discrete random number
543 * generator, sometimes referred to as the SWC generator.
545 * A discrete random number generator that produces pseudorandom
546 * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} -
547 * carry_{i-1}) \bmod m @f$.
549 * The size of the state is @f$ r @f$
550 * and the maximum period of the generator is @f$ m^r - m^s - 1 @f$.
552 * @var _M_x The state of the generator. This is a ring buffer.
553 * @var _M_carry The carry.
554 * @var _M_p Current index of x(i - r).
556 template<typename _UIntType
, size_t __w
, size_t __s
, size_t __r
>
557 class subtract_with_carry_engine
559 __glibcxx_class_requires(_UIntType
, _UnsignedIntegerConcept
)
560 static_assert(__s
> 0U && __r
> __s
562 && __w
<= static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
563 "template arguments out of bounds"
564 " in subtract_with_carry_engine");
567 /** The type of the generated random value. */
568 typedef _UIntType result_type
;
571 static const size_t word_size
= __w
;
572 static const size_t short_lag
= __s
;
573 static const size_t long_lag
= __r
;
574 static const result_type default_seed
= 19780503u;
577 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
578 * random number generator.
581 subtract_with_carry_engine(result_type __sd
= default_seed
)
582 { this->seed(__sd
); }
585 * @brief Constructs a %subtract_with_carry_engine random number engine
586 * seeded from the seed sequence @p __q.
588 * @param __q the seed sequence.
591 subtract_with_carry_engine(seed_seq
& __q
)
595 * @brief Seeds the initial state @f$ x_0 @f$ of the random number
598 * N1688[4.19] modifies this as follows. If @p __value == 0,
599 * sets value to 19780503. In any case, with a linear
600 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
601 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
602 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
603 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
604 * set carry to 1, otherwise sets carry to 0.
607 seed(result_type __sd
= default_seed
);
610 * @brief Seeds the initial state @f$ x_0 @f$ of the
611 * % subtract_with_carry_engine random number generator.
617 * @brief Gets the inclusive minimum value of the range of random
618 * integers returned by this generator.
620 * @todo This should be constexpr.
627 * @brief Gets the inclusive maximum value of the range of random
628 * integers returned by this generator.
630 * @todo This should be constexpr.
634 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
637 * @brief Discard a sequence of random numbers.
639 * @todo Look for a faster way to do discard.
642 discard(unsigned long long __z
)
644 for (; __z
!= 0ULL; --__z
)
649 * @brief Gets the next random number in the sequence.
655 * @brief Compares two % subtract_with_carry_engine random number
656 * generator objects of the same type for equality.
658 * @param __lhs A % subtract_with_carry_engine random number generator
660 * @param __rhs Another % subtract_with_carry_engine random number
663 * @returns true if the two objects are equal, false otherwise.
666 operator==(const subtract_with_carry_engine
& __lhs
,
667 const subtract_with_carry_engine
& __rhs
)
668 { return std::equal(__lhs
._M_x
, __lhs
._M_x
+ long_lag
, __rhs
._M_x
); }
671 * @brief Inserts the current state of a % subtract_with_carry_engine
672 * random number generator engine @p __x into the output stream
675 * @param __os An output stream.
676 * @param __x A % subtract_with_carry_engine random number generator
679 * @returns The output stream with the state of @p __x inserted or in
682 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
683 typename _CharT
, typename _Traits
>
684 friend std::basic_ostream
<_CharT
, _Traits
>&
685 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
686 const std::subtract_with_carry_engine
<_UIntType1
, __w1
,
690 * @brief Extracts the current state of a % subtract_with_carry_engine
691 * random number generator engine @p __x from the input stream
694 * @param __is An input stream.
695 * @param __x A % subtract_with_carry_engine random number generator engine.
697 * @returns The input stream with the state of @p __x extracted or in
700 template<typename _UIntType1
, size_t __w1
, size_t __s1
, size_t __r1
,
701 typename _CharT
, typename _Traits
>
702 friend std::basic_istream
<_CharT
, _Traits
>&
703 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
704 std::subtract_with_carry_engine
<_UIntType1
, __w1
,
708 _UIntType _M_x
[long_lag
];
714 * Produces random numbers from some base engine by discarding blocks of
717 * 0 <= @p __r <= @p __p
719 template<typename _RandomNumberEngine
, size_t __p
, size_t __r
>
720 class discard_block_engine
722 static_assert(__r
>= 1U && __p
>= __r
,
723 "template arguments out of bounds"
724 " in discard_block_engine");
727 /** The type of the generated random value. */
728 typedef typename
_RandomNumberEngine::result_type result_type
;
731 static const size_t block_size
= __p
;
732 static const size_t used_block
= __r
;
735 * @brief Constructs a default %discard_block_engine engine.
737 * The underlying engine is default constructed as well.
739 discard_block_engine()
740 : _M_b(), _M_n(0) { }
743 * @brief Copy constructs a %discard_block_engine engine.
745 * Copies an existing base class random number generator.
746 * @param rng An existing (base class) engine object.
749 discard_block_engine(const _RandomNumberEngine
& __rne
)
750 : _M_b(__rne
), _M_n(0) { }
753 * @brief Move constructs a %discard_block_engine engine.
755 * Copies an existing base class random number generator.
756 * @param rng An existing (base class) engine object.
759 discard_block_engine(_RandomNumberEngine
&& __rne
)
760 : _M_b(std::move(__rne
)), _M_n(0) { }
763 * @brief Seed constructs a %discard_block_engine engine.
765 * Constructs the underlying generator engine seeded with @p __s.
766 * @param __s A seed value for the base class engine.
769 discard_block_engine(result_type __s
)
770 : _M_b(__s
), _M_n(0) { }
773 * @brief Generator construct a %discard_block_engine engine.
775 * @param __q A seed sequence.
778 discard_block_engine(seed_seq
& __q
)
783 * @brief Reseeds the %discard_block_engine object with the default
784 * seed for the underlying base class generator engine.
794 * @brief Reseeds the %discard_block_engine object with the default
795 * seed for the underlying base class generator engine.
798 seed(result_type __s
)
805 * @brief Reseeds the %discard_block_engine object with the given seed
807 * @param __q A seed generator function.
817 * @brief Gets a const reference to the underlying generator engine
820 const _RandomNumberEngine
&
825 * @brief Gets the minimum value in the generated random number range.
827 * @todo This should be constexpr.
831 { return _M_b
.min(); }
834 * @brief Gets the maximum value in the generated random number range.
836 * @todo This should be constexpr.
840 { return _M_b
.max(); }
843 * @brief Discard a sequence of random numbers.
845 * @todo Look for a faster way to do discard.
848 discard(unsigned long long __z
)
850 for (; __z
!= 0ULL; --__z
)
855 * @brief Gets the next value in the generated random number sequence.
861 * @brief Compares two %discard_block_engine random number generator
862 * objects of the same type for equality.
864 * @param __lhs A %discard_block_engine random number generator object.
865 * @param __rhs Another %discard_block_engine random number generator
868 * @returns true if the two objects are equal, false otherwise.
871 operator==(const discard_block_engine
& __lhs
,
872 const discard_block_engine
& __rhs
)
873 { return (__lhs
._M_b
== __rhs
._M_b
) && (__lhs
._M_n
== __rhs
._M_n
); }
876 * @brief Inserts the current state of a %discard_block_engine random
877 * number generator engine @p __x into the output stream
880 * @param __os An output stream.
881 * @param __x A %discard_block_engine random number generator engine.
883 * @returns The output stream with the state of @p __x inserted or in
886 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
887 typename _CharT
, typename _Traits
>
888 friend std::basic_ostream
<_CharT
, _Traits
>&
889 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
890 const std::discard_block_engine
<_RandomNumberEngine1
,
894 * @brief Extracts the current state of a % subtract_with_carry_engine
895 * random number generator engine @p __x from the input stream
898 * @param __is An input stream.
899 * @param __x A %discard_block_engine random number generator engine.
901 * @returns The input stream with the state of @p __x extracted or in
904 template<typename _RandomNumberEngine1
, size_t __p1
, size_t __r1
,
905 typename _CharT
, typename _Traits
>
906 friend std::basic_istream
<_CharT
, _Traits
>&
907 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
908 std::discard_block_engine
<_RandomNumberEngine1
,
912 _RandomNumberEngine _M_b
;
917 * Produces random numbers by combining random numbers from some base
918 * engine to produce random numbers with a specifies number of bits @p __w.
920 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
>
921 class independent_bits_engine
923 static_assert(__w
> 0U
925 static_cast<size_t>(numeric_limits
<_UIntType
>::digits
),
926 "template arguments out of bounds "
927 "in independent_bits_engine");
930 /** The type of the generated random value. */
931 typedef _UIntType result_type
;
934 * @brief Constructs a default %independent_bits_engine engine.
936 * The underlying engine is default constructed as well.
938 independent_bits_engine()
942 * @brief Copy constructs a %independent_bits_engine engine.
944 * Copies an existing base class random number generator.
945 * @param rng An existing (base class) engine object.
948 independent_bits_engine(const _RandomNumberEngine
& __rne
)
952 * @brief Move constructs a %independent_bits_engine engine.
954 * Copies an existing base class random number generator.
955 * @param rng An existing (base class) engine object.
958 independent_bits_engine(_RandomNumberEngine
&& __rne
)
959 : _M_b(std::move(__rne
)) { }
962 * @brief Seed constructs a %independent_bits_engine engine.
964 * Constructs the underlying generator engine seeded with @p __s.
965 * @param __s A seed value for the base class engine.
968 independent_bits_engine(result_type __s
)
972 * @brief Generator construct a %independent_bits_engine engine.
974 * @param __q A seed sequence.
977 independent_bits_engine(seed_seq
& __q
)
982 * @brief Reseeds the %independent_bits_engine object with the default
983 * seed for the underlying base class generator engine.
990 * @brief Reseeds the %independent_bits_engine object with the default
991 * seed for the underlying base class generator engine.
994 seed(result_type __s
)
998 * @brief Reseeds the %independent_bits_engine object with the given
1000 * @param __q A seed generator function.
1007 * @brief Gets a const reference to the underlying generator engine
1010 const _RandomNumberEngine
&
1015 * @brief Gets the minimum value in the generated random number range.
1017 * @todo This should be constexpr.
1024 * @brief Gets the maximum value in the generated random number range.
1026 * @todo This should be constexpr.
1030 { return __detail::_Shift
<_UIntType
, __w
>::__value
- 1; }
1033 * @brief Discard a sequence of random numbers.
1035 * @todo Look for a faster way to do discard.
1038 discard(unsigned long long __z
)
1040 for (; __z
!= 0ULL; --__z
)
1045 * @brief Gets the next value in the generated random number sequence.
1051 * @brief Compares two %independent_bits_engine random number generator
1052 * objects of the same type for equality.
1054 * @param __lhs A %independent_bits_engine random number generator
1056 * @param __rhs Another %independent_bits_engine random number generator
1059 * @returns true if the two objects are equal, false otherwise.
1062 operator==(const independent_bits_engine
& __lhs
,
1063 const independent_bits_engine
& __rhs
)
1064 { return __lhs
._M_b
== __rhs
._M_b
; }
1067 * @brief Extracts the current state of a % subtract_with_carry_engine
1068 * random number generator engine @p __x from the input stream
1071 * @param __is An input stream.
1072 * @param __x A %independent_bits_engine random number generator
1075 * @returns The input stream with the state of @p __x extracted or in
1078 template<typename _CharT
, typename _Traits
>
1079 friend std::basic_istream
<_CharT
, _Traits
>&
1080 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
1081 std::independent_bits_engine
<_RandomNumberEngine
,
1082 __w
, _UIntType
>& __x
)
1089 _RandomNumberEngine _M_b
;
1093 * @brief Inserts the current state of a %independent_bits_engine random
1094 * number generator engine @p __x into the output stream @p __os.
1096 * @param __os An output stream.
1097 * @param __x A %independent_bits_engine random number generator engine.
1099 * @returns The output stream with the state of @p __x inserted or in
1102 template<typename _RandomNumberEngine
, size_t __w
, typename _UIntType
,
1103 typename _CharT
, typename _Traits
>
1104 std::basic_ostream
<_CharT
, _Traits
>&
1105 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1106 const std::independent_bits_engine
<_RandomNumberEngine
,
1107 __w
, _UIntType
>& __x
)
1114 * @brief Produces random numbers by combining random numbers from some
1115 * base engine to produce random numbers with a specifies number of bits
1118 template<typename _RandomNumberEngine
, size_t __k
>
1119 class shuffle_order_engine
1121 static_assert(__k
>= 1U,
1122 "template arguments out of bounds"
1123 " in shuffle_order_engine");
1126 /** The type of the generated random value. */
1127 typedef typename
_RandomNumberEngine::result_type result_type
;
1129 static const size_t table_size
= __k
;
1132 * @brief Constructs a default %shuffle_order_engine engine.
1134 * The underlying engine is default constructed as well.
1136 shuffle_order_engine()
1138 { _M_initialize(); }
1141 * @brief Copy constructs a %shuffle_order_engine engine.
1143 * Copies an existing base class random number generator.
1144 * @param rng An existing (base class) engine object.
1147 shuffle_order_engine(const _RandomNumberEngine
& __rne
)
1149 { _M_initialize(); }
1152 * @brief Move constructs a %shuffle_order_engine engine.
1154 * Copies an existing base class random number generator.
1155 * @param rng An existing (base class) engine object.
1158 shuffle_order_engine(_RandomNumberEngine
&& __rne
)
1159 : _M_b(std::move(__rne
))
1160 { _M_initialize(); }
1163 * @brief Seed constructs a %shuffle_order_engine engine.
1165 * Constructs the underlying generator engine seeded with @p __s.
1166 * @param __s A seed value for the base class engine.
1169 shuffle_order_engine(result_type __s
)
1171 { _M_initialize(); }
1174 * @brief Generator construct a %shuffle_order_engine engine.
1176 * @param __q A seed sequence.
1179 shuffle_order_engine(seed_seq
& __q
)
1181 { _M_initialize(); }
1184 * @brief Reseeds the %shuffle_order_engine object with the default seed
1185 for the underlying base class generator engine.
1195 * @brief Reseeds the %shuffle_order_engine object with the default seed
1196 * for the underlying base class generator engine.
1199 seed(result_type __s
)
1206 * @brief Reseeds the %shuffle_order_engine object with the given seed
1208 * @param __q A seed generator function.
1218 * Gets a const reference to the underlying generator engine object.
1220 const _RandomNumberEngine
&
1225 * Gets the minimum value in the generated random number range.
1227 * @todo This should be constexpr.
1231 { return _M_b
.min(); }
1234 * Gets the maximum value in the generated random number range.
1236 * @todo This should be constexpr.
1240 { return _M_b
.max(); }
1243 * Discard a sequence of random numbers.
1245 * @todo Look for a faster way to do discard.
1248 discard(unsigned long long __z
)
1250 for (; __z
!= 0ULL; --__z
)
1255 * Gets the next value in the generated random number sequence.
1261 * Compares two %shuffle_order_engine random number generator objects
1262 * of the same type for equality.
1264 * @param __lhs A %shuffle_order_engine random number generator object.
1265 * @param __rhs Another %shuffle_order_engine random number generator
1268 * @returns true if the two objects are equal, false otherwise.
1271 operator==(const shuffle_order_engine
& __lhs
,
1272 const shuffle_order_engine
& __rhs
)
1273 { return __lhs
._M_b
== __rhs
._M_b
; }
1276 * @brief Inserts the current state of a %shuffle_order_engine random
1277 * number generator engine @p __x into the output stream
1280 * @param __os An output stream.
1281 * @param __x A %shuffle_order_engine random number generator engine.
1283 * @returns The output stream with the state of @p __x inserted or in
1286 template<typename _RandomNumberEngine1
, size_t __k1
,
1287 typename _CharT
, typename _Traits
>
1288 friend std::basic_ostream
<_CharT
, _Traits
>&
1289 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1290 const std::shuffle_order_engine
<_RandomNumberEngine1
,
1294 * @brief Extracts the current state of a % subtract_with_carry_engine
1295 * random number generator engine @p __x from the input stream
1298 * @param __is An input stream.
1299 * @param __x A %shuffle_order_engine random number generator engine.
1301 * @returns The input stream with the state of @p __x extracted or in
1304 template<typename _RandomNumberEngine1
, size_t __k1
,
1305 typename _CharT
, typename _Traits
>
1306 friend std::basic_istream
<_CharT
, _Traits
>&
1307 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1308 std::shuffle_order_engine
<_RandomNumberEngine1
, __k1
>&);
1311 void _M_initialize()
1313 for (size_t __i
= 0; __i
< __k
; ++__i
)
1318 _RandomNumberEngine _M_b
;
1319 result_type _M_v
[__k
];
1324 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1326 typedef linear_congruential_engine
<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1330 * An alternative LCR (Lehmer Generator function) .
1332 typedef linear_congruential_engine
<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1336 * The classic Mersenne Twister.
1339 * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
1340 * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions
1341 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1343 typedef mersenne_twister_engine
<
1349 0xefc60000UL
, 18, 1812433253UL> mt19937
;
1352 * An alternative Mersenne Twister.
1354 typedef mersenne_twister_engine
<
1357 0xb5026f5aa96619e9ULL
, 29,
1358 0x5555555555555555ULL
, 17,
1359 0x71d67fffeda60000ULL
, 37,
1360 0xfff7eee000000000ULL
, 43,
1361 6364136223846793005ULL> mt19937_64
;
1366 typedef subtract_with_carry_engine
<uint_fast32_t, 24, 10, 24>
1369 typedef discard_block_engine
<ranlux24_base
, 223, 23> ranlux24
;
1371 typedef subtract_with_carry_engine
<uint_fast64_t, 48, 5, 12>
1374 typedef discard_block_engine
<ranlux48_base
, 389, 11> ranlux48
;
1379 typedef shuffle_order_engine
<minstd_rand0
, 256> knuth_b
;
1384 typedef minstd_rand0 default_random_engine
;
1387 * A standard interface to a platform-specific non-deterministic
1388 * random number generator (if any are available).
1393 /** The type of the generated random value. */
1394 typedef unsigned int result_type
;
1396 // constructors, destructors and member functions
1398 #ifdef _GLIBCXX_USE_RANDOM_TR1
1401 random_device(const std::string
& __token
= "/dev/urandom")
1403 if ((__token
!= "/dev/urandom" && __token
!= "/dev/random")
1404 || !(_M_file
= std::fopen(__token
.c_str(), "rb")))
1405 std::__throw_runtime_error(__N("random_device::"
1406 "random_device(const std::string&)"));
1410 { std::fclose(_M_file
); }
1415 random_device(const std::string
& __token
= "mt19937")
1416 : _M_mt(_M_strtoul(__token
)) { }
1419 static unsigned long
1420 _M_strtoul(const std::string
& __str
)
1422 unsigned long __ret
= 5489UL;
1423 if (__str
!= "mt19937")
1425 const char* __nptr
= __str
.c_str();
1427 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
1428 if (*__nptr
== '\0' || *__endptr
!= '\0')
1429 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1430 "(const std::string&)"));
1441 { return std::numeric_limits
<result_type
>::min(); }
1445 { return std::numeric_limits
<result_type
>::max(); }
1454 #ifdef _GLIBCXX_USE_RANDOM_TR1
1456 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
1464 // No copy functions.
1465 random_device(const random_device
&) = delete;
1466 void operator=(const random_device
&) = delete;
1470 #ifdef _GLIBCXX_USE_RANDOM_TR1
1477 /* @} */ // group std_random_generators
1480 * @addtogroup std_random_distributions Random Number Distributions
1481 * @ingroup std_random
1486 * @addtogroup std_random_distributions_uniform Uniform Distributions
1487 * @ingroup std_random_distributions
1492 * @brief Uniform discrete distribution for random numbers.
1493 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1494 * probability throughout the range.
1496 template<typename _IntType
= int>
1497 class uniform_int_distribution
1499 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
1502 /** The type of the range of the distribution. */
1503 typedef _IntType result_type
;
1504 /** Parameter type. */
1507 typedef uniform_int_distribution
<_IntType
> distribution_type
;
1510 param_type(_IntType __a
= 0, _IntType __b
= 9)
1511 : _M_a(__a
), _M_b(__b
)
1513 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1531 * @brief Constructs a uniform distribution object.
1534 uniform_int_distribution(_IntType __a
= 0, _IntType __b
= 9)
1535 : _M_param(__a
, __b
)
1539 uniform_int_distribution(const param_type
& __p
)
1544 * @brief Resets the distribution state.
1546 * Does nothing for the uniform integer distribution.
1553 { return _M_param
.a(); }
1557 { return _M_param
.b(); }
1560 * @brief Returns the inclusive lower bound of the distribution range.
1564 { return this->a(); }
1567 * @brief Returns the inclusive upper bound of the distribution range.
1571 { return this->b(); }
1574 * @brief Returns the parameter set of the distribution.
1578 { return _M_param
; }
1581 * @brief Sets the parameter set of the distribution.
1582 * @param __param The new parameter set of the distribution.
1585 param(const param_type
& __param
)
1586 { _M_param
= __param
; }
1589 * Gets a uniformly distributed random number in the range
1592 template<typename _UniformRandomNumberGenerator
>
1594 operator()(_UniformRandomNumberGenerator
& __urng
)
1595 { return this->operator()(__urng
, this->param()); }
1598 * Gets a uniform random number in the range @f$[0, n)@f$.
1600 * This function is aimed at use with std::random_shuffle.
1602 template<typename _UniformRandomNumberGenerator
>
1604 operator()(_UniformRandomNumberGenerator
& __urng
,
1605 const param_type
& __p
)
1607 typedef typename
_UniformRandomNumberGenerator::result_type
1609 return _M_call(__urng
, __p
.a(), __p
.b(),
1610 typename is_integral
<_UResult_type
>::type());
1614 template<typename _UniformRandomNumberGenerator
>
1616 _M_call(_UniformRandomNumberGenerator
& __urng
,
1617 result_type __min
, result_type __max
, true_type
);
1619 template<typename _UniformRandomNumberGenerator
>
1621 _M_call(_UniformRandomNumberGenerator
& __urng
,
1622 result_type __min
, result_type __max
, false_type
)
1624 return result_type((__urng() - __urng
.min())
1625 / (__urng
.max() - __urng
.min())
1626 * (__max
- __min
+ 1)) + __min
;
1629 param_type _M_param
;
1633 * @brief Inserts a %uniform_int_distribution random number
1634 * distribution @p __x into the output stream @p os.
1636 * @param __os An output stream.
1637 * @param __x A %uniform_int_distribution random number distribution.
1639 * @returns The output stream with the state of @p __x inserted or in
1642 template<typename _IntType
, typename _CharT
, typename _Traits
>
1643 std::basic_ostream
<_CharT
, _Traits
>&
1644 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1645 const std::uniform_int_distribution
<_IntType
>&);
1648 * @brief Extracts a %uniform_int_distribution random number distribution
1649 * @p __x from the input stream @p __is.
1651 * @param __is An input stream.
1652 * @param __x A %uniform_int_distribution random number generator engine.
1654 * @returns The input stream with @p __x extracted or in an error state.
1656 template<typename _IntType
, typename _CharT
, typename _Traits
>
1657 std::basic_istream
<_CharT
, _Traits
>&
1658 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1659 std::uniform_int_distribution
<_IntType
>&);
1663 * @brief Uniform continuous distribution for random numbers.
1665 * A continuous random distribution on the range [min, max) with equal
1666 * probability throughout the range. The URNG should be real-valued and
1667 * deliver number in the range [0, 1).
1669 template<typename _RealType
= double>
1670 class uniform_real_distribution
1673 /** The type of the range of the distribution. */
1674 typedef _RealType result_type
;
1675 /** Parameter type. */
1678 typedef uniform_real_distribution
<_RealType
> distribution_type
;
1681 param_type(_RealType __a
= _RealType(0),
1682 _RealType __b
= _RealType(1))
1683 : _M_a(__a
), _M_b(__b
)
1685 _GLIBCXX_DEBUG_ASSERT(_M_a
<= _M_b
);
1703 * @brief Constructs a uniform_real_distribution object.
1705 * @param __min [IN] The lower bound of the distribution.
1706 * @param __max [IN] The upper bound of the distribution.
1709 uniform_real_distribution(_RealType __a
= _RealType(0),
1710 _RealType __b
= _RealType(1))
1711 : _M_param(__a
, __b
)
1715 uniform_real_distribution(const param_type
& __p
)
1720 * @brief Resets the distribution state.
1722 * Does nothing for the uniform real distribution.
1729 { return _M_param
.a(); }
1733 { return _M_param
.b(); }
1736 * @brief Returns the inclusive lower bound of the distribution range.
1740 { return this->a(); }
1743 * @brief Returns the inclusive upper bound of the distribution range.
1747 { return this->b(); }
1750 * @brief Returns the parameter set of the distribution.
1754 { return _M_param
; }
1757 * @brief Sets the parameter set of the distribution.
1758 * @param __param The new parameter set of the distribution.
1761 param(const param_type
& __param
)
1762 { _M_param
= __param
; }
1764 template<typename _UniformRandomNumberGenerator
>
1766 operator()(_UniformRandomNumberGenerator
& __urng
)
1767 { return this->operator()(__urng
, this->param()); }
1769 template<typename _UniformRandomNumberGenerator
>
1771 operator()(_UniformRandomNumberGenerator
& __urng
,
1772 const param_type
& __p
)
1774 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
1776 return (__aurng() * (__p
.b() - __p
.a())) + __p
.a();
1780 param_type _M_param
;
1784 * @brief Inserts a %uniform_real_distribution random number
1785 * distribution @p __x into the output stream @p __os.
1787 * @param __os An output stream.
1788 * @param __x A %uniform_real_distribution random number distribution.
1790 * @returns The output stream with the state of @p __x inserted or in
1793 template<typename _RealType
, typename _CharT
, typename _Traits
>
1794 std::basic_ostream
<_CharT
, _Traits
>&
1795 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1796 const std::uniform_real_distribution
<_RealType
>&);
1799 * @brief Extracts a %uniform_real_distribution random number distribution
1800 * @p __x from the input stream @p __is.
1802 * @param __is An input stream.
1803 * @param __x A %uniform_real_distribution random number generator engine.
1805 * @returns The input stream with @p __x extracted or in an error state.
1807 template<typename _RealType
, typename _CharT
, typename _Traits
>
1808 std::basic_istream
<_CharT
, _Traits
>&
1809 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1810 std::uniform_real_distribution
<_RealType
>&);
1812 /* @} */ // group std_random_distributions_uniform
1815 * @addtogroup std_random_distributions_normal Normal Distributions
1816 * @ingroup std_random_distributions
1821 * @brief A normal continuous distribution for random numbers.
1823 * The formula for the normal probability density function is
1824 * @f$ p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1825 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} } @f$.
1827 template<typename _RealType
= double>
1828 class normal_distribution
1831 /** The type of the range of the distribution. */
1832 typedef _RealType result_type
;
1833 /** Parameter type. */
1836 typedef normal_distribution
<_RealType
> distribution_type
;
1839 param_type(_RealType __mean
= _RealType(0),
1840 _RealType __stddev
= _RealType(1))
1841 : _M_mean(__mean
), _M_stddev(__stddev
)
1843 _GLIBCXX_DEBUG_ASSERT(_M_stddev
> _RealType(0));
1852 { return _M_stddev
; }
1856 _RealType _M_stddev
;
1861 * Constructs a normal distribution with parameters @f$ mean @f$ and
1862 * standard deviation.
1865 normal_distribution(result_type __mean
= result_type(0),
1866 result_type __stddev
= result_type(1))
1867 : _M_param(__mean
, __stddev
), _M_saved_available(false)
1871 normal_distribution(const param_type
& __p
)
1872 : _M_param(__p
), _M_saved_available(false)
1876 * @brief Resets the distribution state.
1880 { _M_saved_available
= false; }
1883 * @brief Returns the mean of the distribution.
1887 { return _M_param
.mean(); }
1890 * @brief Returns the standard deviation of the distribution.
1894 { return _M_param
.stddev(); }
1897 * @brief Returns the parameter set of the distribution.
1901 { return _M_param
; }
1904 * @brief Sets the parameter set of the distribution.
1905 * @param __param The new parameter set of the distribution.
1908 param(const param_type
& __param
)
1909 { _M_param
= __param
; }
1912 * @brief Returns the greatest lower bound value of the distribution.
1916 { return std::numeric_limits
<result_type
>::min(); }
1919 * @brief Returns the least upper bound value of the distribution.
1923 { return std::numeric_limits
<result_type
>::max(); }
1925 template<typename _UniformRandomNumberGenerator
>
1927 operator()(_UniformRandomNumberGenerator
& __urng
)
1928 { return this->operator()(__urng
, this->param()); }
1930 template<typename _UniformRandomNumberGenerator
>
1932 operator()(_UniformRandomNumberGenerator
& __urng
,
1933 const param_type
& __p
);
1936 * @brief Inserts a %normal_distribution random number distribution
1937 * @p __x into the output stream @p __os.
1939 * @param __os An output stream.
1940 * @param __x A %normal_distribution random number distribution.
1942 * @returns The output stream with the state of @p __x inserted or in
1945 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1946 friend std::basic_ostream
<_CharT
, _Traits
>&
1947 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
1948 const std::normal_distribution
<_RealType1
>&);
1951 * @brief Extracts a %normal_distribution random number distribution
1952 * @p __x from the input stream @p __is.
1954 * @param __is An input stream.
1955 * @param __x A %normal_distribution random number generator engine.
1957 * @returns The input stream with @p __x extracted or in an error
1960 template<typename _RealType1
, typename _CharT
, typename _Traits
>
1961 friend std::basic_istream
<_CharT
, _Traits
>&
1962 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
1963 std::normal_distribution
<_RealType1
>&);
1966 param_type _M_param
;
1967 result_type _M_saved
;
1968 bool _M_saved_available
;
1973 * @brief A lognormal_distribution random number distribution.
1975 * The formula for the normal probability mass function is
1976 * @f$ p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
1977 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}} @f$
1979 template<typename _RealType
= double>
1980 class lognormal_distribution
1983 /** The type of the range of the distribution. */
1984 typedef _RealType result_type
;
1985 /** Parameter type. */
1988 typedef lognormal_distribution
<_RealType
> distribution_type
;
1991 param_type(_RealType __m
= _RealType(0),
1992 _RealType __s
= _RealType(1))
1993 : _M_m(__m
), _M_s(__s
)
2010 lognormal_distribution(_RealType __m
= _RealType(0),
2011 _RealType __s
= _RealType(1))
2012 : _M_param(__m
, __s
), _M_nd()
2016 lognormal_distribution(const param_type
& __p
)
2017 : _M_param(__p
), _M_nd()
2021 * Resets the distribution state.
2032 { return _M_param
.m(); }
2036 { return _M_param
.s(); }
2039 * @brief Returns the parameter set of the distribution.
2043 { return _M_param
; }
2046 * @brief Sets the parameter set of the distribution.
2047 * @param __param The new parameter set of the distribution.
2050 param(const param_type
& __param
)
2051 { _M_param
= __param
; }
2054 * @brief Returns the greatest lower bound value of the distribution.
2058 { return result_type(0); }
2061 * @brief Returns the least upper bound value of the distribution.
2065 { return std::numeric_limits
<result_type
>::max(); }
2067 template<typename _UniformRandomNumberGenerator
>
2069 operator()(_UniformRandomNumberGenerator
& __urng
)
2070 { return this->operator()(__urng
, this->param()); }
2072 template<typename _UniformRandomNumberGenerator
>
2074 operator()(_UniformRandomNumberGenerator
& __urng
,
2075 const param_type
& __p
)
2076 { return std::exp(__p
.s() * _M_nd(__urng
) + __p
.m()); }
2079 param_type _M_param
;
2081 std::normal_distribution
<result_type
> _M_nd
;
2085 * @brief Inserts a %lognormal_distribution random number distribution
2086 * @p __x into the output stream @p __os.
2088 * @param __os An output stream.
2089 * @param __x A %lognormal_distribution random number distribution.
2091 * @returns The output stream with the state of @p __x inserted or in
2094 template<typename _RealType
, typename _CharT
, typename _Traits
>
2095 std::basic_ostream
<_CharT
, _Traits
>&
2096 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2097 const std::lognormal_distribution
<_RealType
>&);
2100 * @brief Extracts a %lognormal_distribution random number distribution
2101 * @p __x from the input stream @p __is.
2103 * @param __is An input stream.
2104 * @param __x A %lognormal_distribution random number
2107 * @returns The input stream with @p __x extracted or in an error state.
2109 template<typename _RealType
, typename _CharT
, typename _Traits
>
2110 std::basic_istream
<_CharT
, _Traits
>&
2111 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2112 std::lognormal_distribution
<_RealType
>&);
2116 * @brief A gamma continuous distribution for random numbers.
2118 * The formula for the gamma probability density function is
2119 * @f$ p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2120 * (x/\beta)^{\alpha - 1} e^{-x/\beta} @f$.
2122 template<typename _RealType
= double>
2123 class gamma_distribution
2126 /** The type of the range of the distribution. */
2127 typedef _RealType result_type
;
2128 /** Parameter type. */
2131 typedef gamma_distribution
<_RealType
> distribution_type
;
2132 friend class gamma_distribution
<_RealType
>;
2135 param_type(_RealType __alpha_val
= _RealType(1),
2136 _RealType __beta_val
= _RealType(1))
2137 : _M_alpha(__alpha_val
), _M_beta(__beta_val
)
2139 _GLIBCXX_DEBUG_ASSERT(_M_alpha
> _RealType(0));
2145 { return _M_alpha
; }
2158 _RealType _M_malpha
, _M_a2
;
2163 * @brief Constructs a gamma distribution with parameters
2164 * @f$ \alpha @f$ and @f$ \beta @f$.
2167 gamma_distribution(_RealType __alpha_val
= _RealType(1),
2168 _RealType __beta_val
= _RealType(1))
2169 : _M_param(__alpha_val
, __beta_val
), _M_nd()
2173 gamma_distribution(const param_type
& __p
)
2174 : _M_param(__p
), _M_nd()
2178 * @brief Resets the distribution state.
2185 * @brief Returns the @f$ \alpha @f$ of the distribution.
2189 { return _M_param
.alpha(); }
2192 * @brief Returns the @f$ \beta @f$ of the distribution.
2196 { return _M_param
.beta(); }
2199 * @brief Returns the parameter set of the distribution.
2203 { return _M_param
; }
2206 * @brief Sets the parameter set of the distribution.
2207 * @param __param The new parameter set of the distribution.
2210 param(const param_type
& __param
)
2211 { _M_param
= __param
; }
2214 * @brief Returns the greatest lower bound value of the distribution.
2218 { return result_type(0); }
2221 * @brief Returns the least upper bound value of the distribution.
2225 { return std::numeric_limits
<result_type
>::max(); }
2227 template<typename _UniformRandomNumberGenerator
>
2229 operator()(_UniformRandomNumberGenerator
& __urng
)
2230 { return this->operator()(__urng
, this->param()); }
2232 template<typename _UniformRandomNumberGenerator
>
2234 operator()(_UniformRandomNumberGenerator
& __urng
,
2235 const param_type
& __p
);
2238 param_type _M_param
;
2240 std::normal_distribution
<result_type
> _M_nd
;
2244 * @brief Inserts a %gamma_distribution random number distribution
2245 * @p __x into the output stream @p __os.
2247 * @param __os An output stream.
2248 * @param __x A %gamma_distribution random number distribution.
2250 * @returns The output stream with the state of @p __x inserted or in
2253 template<typename _RealType
, typename _CharT
, typename _Traits
>
2254 std::basic_ostream
<_CharT
, _Traits
>&
2255 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2256 const std::gamma_distribution
<_RealType
>&);
2259 * @brief Extracts a %gamma_distribution random number distribution
2260 * @p __x from the input stream @p __is.
2262 * @param __is An input stream.
2263 * @param __x A %gamma_distribution random number generator engine.
2265 * @returns The input stream with @p __x extracted or in an error state.
2267 template<typename _RealType
, typename _CharT
, typename _Traits
>
2268 std::basic_istream
<_CharT
, _Traits
>&
2269 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2270 std::gamma_distribution
<_RealType
>&);
2274 * @brief A chi_squared_distribution random number distribution.
2276 * The formula for the normal probability mass function is
2277 * @f$ p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}} @f$
2279 template<typename _RealType
= double>
2280 class chi_squared_distribution
2283 /** The type of the range of the distribution. */
2284 typedef _RealType result_type
;
2285 /** Parameter type. */
2288 typedef chi_squared_distribution
<_RealType
> distribution_type
;
2291 param_type(_RealType __n
= _RealType(1))
2304 chi_squared_distribution(_RealType __n
= _RealType(1))
2305 : _M_param(__n
), _M_gd(__n
/ 2)
2309 chi_squared_distribution(const param_type
& __p
)
2310 : _M_param(__p
), _M_gd(__p
.n() / 2)
2314 * @brief Resets the distribution state.
2325 { return _M_param
.n(); }
2328 * @brief Returns the parameter set of the distribution.
2332 { return _M_param
; }
2335 * @brief Sets the parameter set of the distribution.
2336 * @param __param The new parameter set of the distribution.
2339 param(const param_type
& __param
)
2340 { _M_param
= __param
; }
2343 * @brief Returns the greatest lower bound value of the distribution.
2347 { return result_type(0); }
2350 * @brief Returns the least upper bound value of the distribution.
2354 { return std::numeric_limits
<result_type
>::max(); }
2356 template<typename _UniformRandomNumberGenerator
>
2358 operator()(_UniformRandomNumberGenerator
& __urng
)
2359 { return 2 * _M_gd(__urng
); }
2361 template<typename _UniformRandomNumberGenerator
>
2363 operator()(_UniformRandomNumberGenerator
& __urng
,
2364 const param_type
& __p
)
2366 typedef typename
std::gamma_distribution
<result_type
>::param_type
2368 return 2 * _M_gd(__urng
, param_type(__p
.n() / 2));
2372 param_type _M_param
;
2374 std::gamma_distribution
<result_type
> _M_gd
;
2378 * @brief Inserts a %chi_squared_distribution random number distribution
2379 * @p __x into the output stream @p __os.
2381 * @param __os An output stream.
2382 * @param __x A %chi_squared_distribution random number distribution.
2384 * @returns The output stream with the state of @p __x inserted or in
2387 template<typename _RealType
, typename _CharT
, typename _Traits
>
2388 std::basic_ostream
<_CharT
, _Traits
>&
2389 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2390 const std::chi_squared_distribution
<_RealType
>&);
2393 * @brief Extracts a %chi_squared_distribution random number distribution
2394 * @p __x from the input stream @p __is.
2396 * @param __is An input stream.
2397 * @param __x A %chi_squared_distribution random number
2400 * @returns The input stream with @p __x extracted or in an error state.
2402 template<typename _RealType
, typename _CharT
, typename _Traits
>
2403 std::basic_istream
<_CharT
, _Traits
>&
2404 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2405 std::chi_squared_distribution
<_RealType
>&);
2409 * @brief A cauchy_distribution random number distribution.
2411 * The formula for the normal probability mass function is
2412 * @f$ p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1} @f$
2414 template<typename _RealType
= double>
2415 class cauchy_distribution
2418 /** The type of the range of the distribution. */
2419 typedef _RealType result_type
;
2420 /** Parameter type. */
2423 typedef cauchy_distribution
<_RealType
> distribution_type
;
2426 param_type(_RealType __a
= _RealType(0),
2427 _RealType __b
= _RealType(1))
2428 : _M_a(__a
), _M_b(__b
)
2445 cauchy_distribution(_RealType __a
= _RealType(0),
2446 _RealType __b
= _RealType(1))
2447 : _M_param(__a
, __b
)
2451 cauchy_distribution(const param_type
& __p
)
2456 * @brief Resets the distribution state.
2467 { return _M_param
.a(); }
2471 { return _M_param
.b(); }
2474 * @brief Returns the parameter set of the distribution.
2478 { return _M_param
; }
2481 * @brief Sets the parameter set of the distribution.
2482 * @param __param The new parameter set of the distribution.
2485 param(const param_type
& __param
)
2486 { _M_param
= __param
; }
2489 * @brief Returns the greatest lower bound value of the distribution.
2493 { return std::numeric_limits
<result_type
>::min(); }
2496 * @brief Returns the least upper bound value of the distribution.
2500 { return std::numeric_limits
<result_type
>::max(); }
2502 template<typename _UniformRandomNumberGenerator
>
2504 operator()(_UniformRandomNumberGenerator
& __urng
)
2505 { return this->operator()(__urng
, this->param()); }
2507 template<typename _UniformRandomNumberGenerator
>
2509 operator()(_UniformRandomNumberGenerator
& __urng
,
2510 const param_type
& __p
);
2513 param_type _M_param
;
2517 * @brief Inserts a %cauchy_distribution random number distribution
2518 * @p __x into the output stream @p __os.
2520 * @param __os An output stream.
2521 * @param __x A %cauchy_distribution random number distribution.
2523 * @returns The output stream with the state of @p __x inserted or in
2526 template<typename _RealType
, typename _CharT
, typename _Traits
>
2527 std::basic_ostream
<_CharT
, _Traits
>&
2528 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2529 const std::cauchy_distribution
<_RealType
>&);
2532 * @brief Extracts a %cauchy_distribution random number distribution
2533 * @p __x from the input stream @p __is.
2535 * @param __is An input stream.
2536 * @param __x A %cauchy_distribution random number
2539 * @returns The input stream with @p __x extracted or in an error state.
2541 template<typename _RealType
, typename _CharT
, typename _Traits
>
2542 std::basic_istream
<_CharT
, _Traits
>&
2543 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2544 std::cauchy_distribution
<_RealType
>&);
2548 * @brief A fisher_f_distribution random number distribution.
2550 * The formula for the normal probability mass function is
2551 * @f$ p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2552 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2553 * (1 + \frac{mx}{n})^{-(m+n)/2} @f$
2555 template<typename _RealType
= double>
2556 class fisher_f_distribution
2559 /** The type of the range of the distribution. */
2560 typedef _RealType result_type
;
2561 /** Parameter type. */
2564 typedef fisher_f_distribution
<_RealType
> distribution_type
;
2567 param_type(_RealType __m
= _RealType(1),
2568 _RealType __n
= _RealType(1))
2569 : _M_m(__m
), _M_n(__n
)
2586 fisher_f_distribution(_RealType __m
= _RealType(1),
2587 _RealType __n
= _RealType(1))
2588 : _M_param(__m
, __n
), _M_gd_x(__m
/ 2), _M_gd_y(__n
/ 2)
2592 fisher_f_distribution(const param_type
& __p
)
2593 : _M_param(__p
), _M_gd_x(__p
.m() / 2), _M_gd_y(__p
.n() / 2)
2597 * @brief Resets the distribution state.
2611 { return _M_param
.m(); }
2615 { return _M_param
.n(); }
2618 * @brief Returns the parameter set of the distribution.
2622 { return _M_param
; }
2625 * @brief Sets the parameter set of the distribution.
2626 * @param __param The new parameter set of the distribution.
2629 param(const param_type
& __param
)
2630 { _M_param
= __param
; }
2633 * @brief Returns the greatest lower bound value of the distribution.
2637 { return result_type(0); }
2640 * @brief Returns the least upper bound value of the distribution.
2644 { return std::numeric_limits
<result_type
>::max(); }
2646 template<typename _UniformRandomNumberGenerator
>
2648 operator()(_UniformRandomNumberGenerator
& __urng
)
2649 { return (_M_gd_x(__urng
) * n()) / (_M_gd_y(__urng
) * m()); }
2651 template<typename _UniformRandomNumberGenerator
>
2653 operator()(_UniformRandomNumberGenerator
& __urng
,
2654 const param_type
& __p
)
2656 typedef typename
std::gamma_distribution
<result_type
>::param_type
2658 return ((_M_gd_x(__urng
, param_type(__p
.m() / 2)) * n())
2659 / (_M_gd_y(__urng
, param_type(__p
.n() / 2)) * m()));
2663 param_type _M_param
;
2665 std::gamma_distribution
<result_type
> _M_gd_x
, _M_gd_y
;
2669 * @brief Inserts a %fisher_f_distribution random number distribution
2670 * @p __x into the output stream @p __os.
2672 * @param __os An output stream.
2673 * @param __x A %fisher_f_distribution random number distribution.
2675 * @returns The output stream with the state of @p __x inserted or in
2678 template<typename _RealType
, typename _CharT
, typename _Traits
>
2679 std::basic_ostream
<_CharT
, _Traits
>&
2680 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2681 const std::fisher_f_distribution
<_RealType
>&);
2684 * @brief Extracts a %fisher_f_distribution random number distribution
2685 * @p __x from the input stream @p __is.
2687 * @param __is An input stream.
2688 * @param __x A %fisher_f_distribution random number
2691 * @returns The input stream with @p __x extracted or in an error state.
2693 template<typename _RealType
, typename _CharT
, typename _Traits
>
2694 std::basic_istream
<_CharT
, _Traits
>&
2695 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2696 std::fisher_f_distribution
<_RealType
>&);
2700 * @brief A student_t_distribution random number distribution.
2702 * The formula for the normal probability mass function is
2703 * @f$ p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2704 * (1 + \frac{x^2}{n}) ^{-(n+1)/2} @f$
2706 template<typename _RealType
= double>
2707 class student_t_distribution
2710 /** The type of the range of the distribution. */
2711 typedef _RealType result_type
;
2712 /** Parameter type. */
2715 typedef student_t_distribution
<_RealType
> distribution_type
;
2718 param_type(_RealType __n
= _RealType(1))
2731 student_t_distribution(_RealType __n
= _RealType(1))
2732 : _M_param(__n
), _M_nd(), _M_gd(__n
/ 2, 2)
2736 student_t_distribution(const param_type
& __p
)
2737 : _M_param(__p
), _M_nd(), _M_gd(__p
.n() / 2, 2)
2741 * @brief Resets the distribution state.
2755 { return _M_param
.n(); }
2758 * @brief Returns the parameter set of the distribution.
2762 { return _M_param
; }
2765 * @brief Sets the parameter set of the distribution.
2766 * @param __param The new parameter set of the distribution.
2769 param(const param_type
& __param
)
2770 { _M_param
= __param
; }
2773 * @brief Returns the greatest lower bound value of the distribution.
2777 { return std::numeric_limits
<result_type
>::min(); }
2780 * @brief Returns the least upper bound value of the distribution.
2784 { return std::numeric_limits
<result_type
>::max(); }
2786 template<typename _UniformRandomNumberGenerator
>
2788 operator()(_UniformRandomNumberGenerator
& __urng
)
2789 { return _M_nd(__urng
) * std::sqrt(n() / _M_gd(__urng
)); }
2791 template<typename _UniformRandomNumberGenerator
>
2793 operator()(_UniformRandomNumberGenerator
& __urng
,
2794 const param_type
& __p
)
2796 typedef typename
std::gamma_distribution
<result_type
>::param_type
2799 const result_type __g
= _M_gd(__urng
, param_type(__p
.n() / 2, 2));
2800 return _M_nd(__urng
) * std::sqrt(__p
.n() / __g
);
2804 param_type _M_param
;
2806 std::normal_distribution
<result_type
> _M_nd
;
2807 std::gamma_distribution
<result_type
> _M_gd
;
2811 * @brief Inserts a %student_t_distribution random number distribution
2812 * @p __x into the output stream @p __os.
2814 * @param __os An output stream.
2815 * @param __x A %student_t_distribution random number distribution.
2817 * @returns The output stream with the state of @p __x inserted or in
2820 template<typename _RealType
, typename _CharT
, typename _Traits
>
2821 std::basic_ostream
<_CharT
, _Traits
>&
2822 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2823 const std::student_t_distribution
<_RealType
>&);
2826 * @brief Extracts a %student_t_distribution random number distribution
2827 * @p __x from the input stream @p __is.
2829 * @param __is An input stream.
2830 * @param __x A %student_t_distribution random number
2833 * @returns The input stream with @p __x extracted or in an error state.
2835 template<typename _RealType
, typename _CharT
, typename _Traits
>
2836 std::basic_istream
<_CharT
, _Traits
>&
2837 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
2838 std::student_t_distribution
<_RealType
>&);
2840 /* @} */ // group std_random_distributions_normal
2843 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2844 * @ingroup std_random_distributions
2849 * @brief A Bernoulli random number distribution.
2851 * Generates a sequence of true and false values with likelihood @f$ p @f$
2852 * that true will come up and @f$ (1 - p) @f$ that false will appear.
2854 class bernoulli_distribution
2857 /** The type of the range of the distribution. */
2858 typedef bool result_type
;
2859 /** Parameter type. */
2862 typedef bernoulli_distribution distribution_type
;
2865 param_type(double __p
= 0.5)
2868 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0) && (_M_p
<= 1.0));
2881 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2883 * @param __p [IN] The likelihood of a true result being returned.
2884 * Must be in the interval @f$ [0, 1] @f$.
2887 bernoulli_distribution(double __p
= 0.5)
2892 bernoulli_distribution(const param_type
& __p
)
2897 * @brief Resets the distribution state.
2899 * Does nothing for a Bernoulli distribution.
2905 * @brief Returns the @p p parameter of the distribution.
2909 { return _M_param
.p(); }
2912 * @brief Returns the parameter set of the distribution.
2916 { return _M_param
; }
2919 * @brief Sets the parameter set of the distribution.
2920 * @param __param The new parameter set of the distribution.
2923 param(const param_type
& __param
)
2924 { _M_param
= __param
; }
2927 * @brief Returns the greatest lower bound value of the distribution.
2931 { return std::numeric_limits
<result_type
>::min(); }
2934 * @brief Returns the least upper bound value of the distribution.
2938 { return std::numeric_limits
<result_type
>::max(); }
2941 * @brief Returns the next value in the Bernoullian sequence.
2943 template<typename _UniformRandomNumberGenerator
>
2945 operator()(_UniformRandomNumberGenerator
& __urng
)
2946 { return this->operator()(__urng
, this->param()); }
2948 template<typename _UniformRandomNumberGenerator
>
2950 operator()(_UniformRandomNumberGenerator
& __urng
,
2951 const param_type
& __p
)
2953 __detail::_Adaptor
<_UniformRandomNumberGenerator
, double>
2955 if ((__aurng() - __aurng
.min())
2956 < __p
.p() * (__aurng
.max() - __aurng
.min()))
2962 param_type _M_param
;
2966 * @brief Inserts a %bernoulli_distribution random number distribution
2967 * @p __x into the output stream @p __os.
2969 * @param __os An output stream.
2970 * @param __x A %bernoulli_distribution random number distribution.
2972 * @returns The output stream with the state of @p __x inserted or in
2975 template<typename _CharT
, typename _Traits
>
2976 std::basic_ostream
<_CharT
, _Traits
>&
2977 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
2978 const std::bernoulli_distribution
&);
2981 * @brief Extracts a %bernoulli_distribution random number distribution
2982 * @p __x from the input stream @p __is.
2984 * @param __is An input stream.
2985 * @param __x A %bernoulli_distribution random number generator engine.
2987 * @returns The input stream with @p __x extracted or in an error state.
2989 template<typename _CharT
, typename _Traits
>
2990 std::basic_istream
<_CharT
, _Traits
>&
2991 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
,
2992 std::bernoulli_distribution
& __x
)
2996 __x
.param(bernoulli_distribution::param_type(__p
));
3002 * @brief A discrete binomial random number distribution.
3004 * The formula for the binomial probability density function is
3005 * @f$ p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3006 * and @f$ p @f$ are the parameters of the distribution.
3008 template<typename _IntType
= int>
3009 class binomial_distribution
3011 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3014 /** The type of the range of the distribution. */
3015 typedef _IntType result_type
;
3016 /** Parameter type. */
3019 typedef binomial_distribution
<_IntType
> distribution_type
;
3020 friend class binomial_distribution
<_IntType
>;
3023 param_type(_IntType __t
= _IntType(1), double __p
= 0.5)
3024 : _M_t(__t
), _M_p(__p
)
3026 _GLIBCXX_DEBUG_ASSERT((_M_t
>= _IntType(0))
3048 #if _GLIBCXX_USE_C99_MATH_TR1
3049 double _M_d1
, _M_d2
, _M_s1
, _M_s2
, _M_c
,
3050 _M_a1
, _M_a123
, _M_s
, _M_lf
, _M_lp1p
;
3055 // constructors and member function
3057 binomial_distribution(_IntType __t
= _IntType(1),
3059 : _M_param(__t
, __p
), _M_nd()
3063 binomial_distribution(const param_type
& __p
)
3064 : _M_param(__p
), _M_nd()
3068 * @brief Resets the distribution state.
3075 * @brief Returns the distribution @p t parameter.
3079 { return _M_param
.t(); }
3082 * @brief Returns the distribution @p p parameter.
3086 { return _M_param
.p(); }
3089 * @brief Returns the parameter set of the distribution.
3093 { return _M_param
; }
3096 * @brief Sets the parameter set of the distribution.
3097 * @param __param The new parameter set of the distribution.
3100 param(const param_type
& __param
)
3101 { _M_param
= __param
; }
3104 * @brief Returns the greatest lower bound value of the distribution.
3111 * @brief Returns the least upper bound value of the distribution.
3115 { return _M_param
.t(); }
3117 template<typename _UniformRandomNumberGenerator
>
3119 operator()(_UniformRandomNumberGenerator
& __urng
)
3120 { return this->operator()(__urng
, this->param()); }
3122 template<typename _UniformRandomNumberGenerator
>
3124 operator()(_UniformRandomNumberGenerator
& __urng
,
3125 const param_type
& __p
);
3128 * @brief Inserts a %binomial_distribution random number distribution
3129 * @p __x into the output stream @p __os.
3131 * @param __os An output stream.
3132 * @param __x A %binomial_distribution random number distribution.
3134 * @returns The output stream with the state of @p __x inserted or in
3137 template<typename _IntType1
,
3138 typename _CharT
, typename _Traits
>
3139 friend std::basic_ostream
<_CharT
, _Traits
>&
3140 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3141 const std::binomial_distribution
<_IntType1
>&);
3144 * @brief Extracts a %binomial_distribution random number distribution
3145 * @p __x from the input stream @p __is.
3147 * @param __is An input stream.
3148 * @param __x A %binomial_distribution random number generator engine.
3150 * @returns The input stream with @p __x extracted or in an error
3153 template<typename _IntType1
,
3154 typename _CharT
, typename _Traits
>
3155 friend std::basic_istream
<_CharT
, _Traits
>&
3156 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3157 std::binomial_distribution
<_IntType1
>&);
3160 template<typename _UniformRandomNumberGenerator
>
3162 _M_waiting(_UniformRandomNumberGenerator
& __urng
, _IntType __t
);
3164 param_type _M_param
;
3166 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3167 std::normal_distribution
<double> _M_nd
;
3172 * @brief A discrete geometric random number distribution.
3174 * The formula for the geometric probability density function is
3175 * @f$ p(i|p) = (1 - p)p^{i-1} @f$ where @f$ p @f$ is the parameter of the
3178 template<typename _IntType
= int>
3179 class geometric_distribution
3181 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3184 /** The type of the range of the distribution. */
3185 typedef _IntType result_type
;
3186 /** Parameter type. */
3189 typedef geometric_distribution
<_IntType
> distribution_type
;
3190 friend class geometric_distribution
<_IntType
>;
3193 param_type(double __p
= 0.5)
3196 _GLIBCXX_DEBUG_ASSERT((_M_p
>= 0.0)
3208 { _M_log_p
= std::log(_M_p
); }
3215 // constructors and member function
3217 geometric_distribution(double __p
= 0.5)
3222 geometric_distribution(const param_type
& __p
)
3227 * @brief Resets the distribution state.
3229 * Does nothing for the geometric distribution.
3235 * @brief Returns the distribution parameter @p p.
3239 { return _M_param
.p(); }
3242 * @brief Returns the parameter set of the distribution.
3246 { return _M_param
; }
3249 * @brief Sets the parameter set of the distribution.
3250 * @param __param The new parameter set of the distribution.
3253 param(const param_type
& __param
)
3254 { _M_param
= __param
; }
3257 * @brief Returns the greatest lower bound value of the distribution.
3264 * @brief Returns the least upper bound value of the distribution.
3268 { return std::numeric_limits
<result_type
>::max(); }
3270 template<typename _UniformRandomNumberGenerator
>
3272 operator()(_UniformRandomNumberGenerator
& __urng
)
3273 { return this->operator()(__urng
, this->param()); }
3275 template<typename _UniformRandomNumberGenerator
>
3277 operator()(_UniformRandomNumberGenerator
& __urng
,
3278 const param_type
& __p
);
3281 param_type _M_param
;
3285 * @brief Inserts a %geometric_distribution random number distribution
3286 * @p __x into the output stream @p __os.
3288 * @param __os An output stream.
3289 * @param __x A %geometric_distribution random number distribution.
3291 * @returns The output stream with the state of @p __x inserted or in
3294 template<typename _IntType
,
3295 typename _CharT
, typename _Traits
>
3296 std::basic_ostream
<_CharT
, _Traits
>&
3297 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3298 const std::geometric_distribution
<_IntType
>&);
3301 * @brief Extracts a %geometric_distribution random number distribution
3302 * @p __x from the input stream @p __is.
3304 * @param __is An input stream.
3305 * @param __x A %geometric_distribution random number generator engine.
3307 * @returns The input stream with @p __x extracted or in an error state.
3309 template<typename _IntType
,
3310 typename _CharT
, typename _Traits
>
3311 std::basic_istream
<_CharT
, _Traits
>&
3312 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3313 std::geometric_distribution
<_IntType
>&);
3317 * @brief A negative_binomial_distribution random number distribution.
3319 * The formula for the negative binomial probability mass function is
3320 * @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
3321 * and @f$ p @f$ are the parameters of the distribution.
3323 template<typename _IntType
= int>
3324 class negative_binomial_distribution
3326 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3329 /** The type of the range of the distribution. */
3330 typedef _IntType result_type
;
3331 /** Parameter type. */
3334 typedef negative_binomial_distribution
<_IntType
> distribution_type
;
3337 param_type(_IntType __k
= 1, double __p
= 0.5)
3338 : _M_k(__k
), _M_p(__p
)
3355 negative_binomial_distribution(_IntType __k
= 1, double __p
= 0.5)
3356 : _M_param(__k
, __p
), _M_gd(__k
, __p
/ (1.0 - __p
))
3360 negative_binomial_distribution(const param_type
& __p
)
3361 : _M_param(__p
), _M_gd(__p
.k(), __p
.p() / (1.0 - __p
.p()))
3365 * @brief Resets the distribution state.
3372 * @brief Return the @f$ k @f$ parameter of the distribution.
3376 { return _M_param
.k(); }
3379 * @brief Return the @f$ p @f$ parameter of the distribution.
3383 { return _M_param
.p(); }
3386 * @brief Returns the parameter set of the distribution.
3390 { return _M_param
; }
3393 * @brief Sets the parameter set of the distribution.
3394 * @param __param The new parameter set of the distribution.
3397 param(const param_type
& __param
)
3398 { _M_param
= __param
; }
3401 * @brief Returns the greatest lower bound value of the distribution.
3405 { return result_type(0); }
3408 * @brief Returns the least upper bound value of the distribution.
3412 { return std::numeric_limits
<result_type
>::max(); }
3414 template<typename _UniformRandomNumberGenerator
>
3416 operator()(_UniformRandomNumberGenerator
& __urng
);
3418 template<typename _UniformRandomNumberGenerator
>
3420 operator()(_UniformRandomNumberGenerator
& __urng
,
3421 const param_type
& __p
);
3424 param_type _M_param
;
3426 std::gamma_distribution
<double> _M_gd
;
3430 * @brief Inserts a %negative_binomial_distribution random
3431 * number distribution @p __x into the output stream @p __os.
3433 * @param __os An output stream.
3434 * @param __x A %negative_binomial_distribution random number
3437 * @returns The output stream with the state of @p __x inserted or in
3440 template<typename _IntType
, typename _CharT
, typename _Traits
>
3441 std::basic_ostream
<_CharT
, _Traits
>&
3442 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3443 const std::negative_binomial_distribution
<_IntType
>&);
3446 * @brief Extracts a %negative_binomial_distribution random number
3447 * distribution @p __x from the input stream @p __is.
3449 * @param __is An input stream.
3450 * @param __x A %negative_binomial_distribution random number
3453 * @returns The input stream with @p __x extracted or in an error state.
3455 template<typename _IntType
, typename _CharT
, typename _Traits
>
3456 std::basic_istream
<_CharT
, _Traits
>&
3457 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3458 std::negative_binomial_distribution
<_IntType
>&);
3460 /* @} */ // group std_random_distributions_bernoulli
3463 * @addtogroup std_random_distributions_poisson Poisson Distributions
3464 * @ingroup std_random_distributions
3469 * @brief A discrete Poisson random number distribution.
3471 * The formula for the Poisson probability density function is
3472 * @f$ p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu} @f$ where @f$ \mu @f$ is the
3473 * parameter of the distribution.
3475 template<typename _IntType
= int>
3476 class poisson_distribution
3478 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
3481 /** The type of the range of the distribution. */
3482 typedef _IntType result_type
;
3483 /** Parameter type. */
3486 typedef poisson_distribution
<_IntType
> distribution_type
;
3487 friend class poisson_distribution
<_IntType
>;
3490 param_type(double __mean
= 1.0)
3493 _GLIBCXX_DEBUG_ASSERT(_M_mean
> 0.0);
3502 // Hosts either log(mean) or the threshold of the simple method.
3509 #if _GLIBCXX_USE_C99_MATH_TR1
3510 double _M_lfm
, _M_sm
, _M_d
, _M_scx
, _M_1cx
, _M_c2b
, _M_cb
;
3514 // constructors and member function
3516 poisson_distribution(double __mean
= 1.0)
3517 : _M_param(__mean
), _M_nd()
3521 poisson_distribution(const param_type
& __p
)
3522 : _M_param(__p
), _M_nd()
3526 * @brief Resets the distribution state.
3533 * @brief Returns the distribution parameter @p mean.
3537 { return _M_param
.mean(); }
3540 * @brief Returns the parameter set of the distribution.
3544 { return _M_param
; }
3547 * @brief Sets the parameter set of the distribution.
3548 * @param __param The new parameter set of the distribution.
3551 param(const param_type
& __param
)
3552 { _M_param
= __param
; }
3555 * @brief Returns the greatest lower bound value of the distribution.
3562 * @brief Returns the least upper bound value of the distribution.
3566 { return std::numeric_limits
<result_type
>::max(); }
3568 template<typename _UniformRandomNumberGenerator
>
3570 operator()(_UniformRandomNumberGenerator
& __urng
)
3571 { return this->operator()(__urng
, this->param()); }
3573 template<typename _UniformRandomNumberGenerator
>
3575 operator()(_UniformRandomNumberGenerator
& __urng
,
3576 const param_type
& __p
);
3579 * @brief Inserts a %poisson_distribution random number distribution
3580 * @p __x into the output stream @p __os.
3582 * @param __os An output stream.
3583 * @param __x A %poisson_distribution random number distribution.
3585 * @returns The output stream with the state of @p __x inserted or in
3588 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3589 friend std::basic_ostream
<_CharT
, _Traits
>&
3590 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3591 const std::poisson_distribution
<_IntType1
>&);
3594 * @brief Extracts a %poisson_distribution random number distribution
3595 * @p __x from the input stream @p __is.
3597 * @param __is An input stream.
3598 * @param __x A %poisson_distribution random number generator engine.
3600 * @returns The input stream with @p __x extracted or in an error
3603 template<typename _IntType1
, typename _CharT
, typename _Traits
>
3604 friend std::basic_istream
<_CharT
, _Traits
>&
3605 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3606 std::poisson_distribution
<_IntType1
>&);
3609 param_type _M_param
;
3611 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3612 std::normal_distribution
<double> _M_nd
;
3616 * @brief An exponential continuous distribution for random numbers.
3618 * The formula for the exponential probability density function is
3619 * @f$ p(x|\lambda) = \lambda e^{-\lambda x} @f$.
3621 * <table border=1 cellpadding=10 cellspacing=0>
3622 * <caption align=top>Distribution Statistics</caption>
3623 * <tr><td>Mean</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3624 * <tr><td>Median</td><td>@f$ \frac{\ln 2}{\lambda} @f$</td></tr>
3625 * <tr><td>Mode</td><td>@f$ zero @f$</td></tr>
3626 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3627 * <tr><td>Standard Deviation</td><td>@f$ \frac{1}{\lambda} @f$</td></tr>
3630 template<typename _RealType
= double>
3631 class exponential_distribution
3634 /** The type of the range of the distribution. */
3635 typedef _RealType result_type
;
3636 /** Parameter type. */
3639 typedef exponential_distribution
<_RealType
> distribution_type
;
3642 param_type(_RealType __lambda
= _RealType(1))
3643 : _M_lambda(__lambda
)
3645 _GLIBCXX_DEBUG_ASSERT(_M_lambda
> _RealType(0));
3650 { return _M_lambda
; }
3653 _RealType _M_lambda
;
3658 * @brief Constructs an exponential distribution with inverse scale
3659 * parameter @f$ \lambda @f$.
3662 exponential_distribution(const result_type
& __lambda
= result_type(1))
3663 : _M_param(__lambda
)
3667 exponential_distribution(const param_type
& __p
)
3672 * @brief Resets the distribution state.
3674 * Has no effect on exponential distributions.
3680 * @brief Returns the inverse scale parameter of the distribution.
3684 { return _M_param
.lambda(); }
3687 * @brief Returns the parameter set of the distribution.
3691 { return _M_param
; }
3694 * @brief Sets the parameter set of the distribution.
3695 * @param __param The new parameter set of the distribution.
3698 param(const param_type
& __param
)
3699 { _M_param
= __param
; }
3702 * @brief Returns the greatest lower bound value of the distribution.
3706 { return result_type(0); }
3709 * @brief Returns the least upper bound value of the distribution.
3713 { return std::numeric_limits
<result_type
>::max(); }
3715 template<typename _UniformRandomNumberGenerator
>
3717 operator()(_UniformRandomNumberGenerator
& __urng
)
3718 { return this->operator()(__urng
, this->param()); }
3720 template<typename _UniformRandomNumberGenerator
>
3722 operator()(_UniformRandomNumberGenerator
& __urng
,
3723 const param_type
& __p
)
3725 __detail::_Adaptor
<_UniformRandomNumberGenerator
, result_type
>
3727 return -std::log(__aurng()) / __p
.lambda();
3731 param_type _M_param
;
3735 * @brief Inserts a %exponential_distribution random number distribution
3736 * @p __x into the output stream @p __os.
3738 * @param __os An output stream.
3739 * @param __x A %exponential_distribution random number distribution.
3741 * @returns The output stream with the state of @p __x inserted or in
3744 template<typename _RealType
, typename _CharT
, typename _Traits
>
3745 std::basic_ostream
<_CharT
, _Traits
>&
3746 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3747 const std::exponential_distribution
<_RealType
>&);
3750 * @brief Extracts a %exponential_distribution random number distribution
3751 * @p __x from the input stream @p __is.
3753 * @param __is An input stream.
3754 * @param __x A %exponential_distribution random number
3757 * @returns The input stream with @p __x extracted or in an error state.
3759 template<typename _RealType
, typename _CharT
, typename _Traits
>
3760 std::basic_istream
<_CharT
, _Traits
>&
3761 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3762 std::exponential_distribution
<_RealType
>&);
3766 * @brief A weibull_distribution random number distribution.
3768 * The formula for the normal probability density function is
3769 * @f$ p(x|\alpha,\beta) = \frac{a}{b} (frac{x}{b})^{a-1}
3770 * \exp{(-(frac{x}{b})^a)} @f$.
3772 template<typename _RealType
= double>
3773 class weibull_distribution
3776 /** The type of the range of the distribution. */
3777 typedef _RealType result_type
;
3778 /** Parameter type. */
3781 typedef weibull_distribution
<_RealType
> distribution_type
;
3784 param_type(_RealType __a
= _RealType(1),
3785 _RealType __b
= _RealType(1))
3786 : _M_a(__a
), _M_b(__b
)
3803 weibull_distribution(_RealType __a
= _RealType(1),
3804 _RealType __b
= _RealType(1))
3805 : _M_param(__a
, __b
)
3809 weibull_distribution(const param_type
& __p
)
3814 * @brief Resets the distribution state.
3821 * @brief Return the @f$ a @f$ parameter of the distribution.
3825 { return _M_param
.a(); }
3828 * @brief Return the @f$ b @f$ parameter of the distribution.
3832 { return _M_param
.b(); }
3835 * @brief Returns the parameter set of the distribution.
3839 { return _M_param
; }
3842 * @brief Sets the parameter set of the distribution.
3843 * @param __param The new parameter set of the distribution.
3846 param(const param_type
& __param
)
3847 { _M_param
= __param
; }
3850 * @brief Returns the greatest lower bound value of the distribution.
3854 { return result_type(0); }
3857 * @brief Returns the least upper bound value of the distribution.
3861 { return std::numeric_limits
<result_type
>::max(); }
3863 template<typename _UniformRandomNumberGenerator
>
3865 operator()(_UniformRandomNumberGenerator
& __urng
)
3866 { return this->operator()(__urng
, this->param()); }
3868 template<typename _UniformRandomNumberGenerator
>
3870 operator()(_UniformRandomNumberGenerator
& __urng
,
3871 const param_type
& __p
);
3874 param_type _M_param
;
3878 * @brief Inserts a %weibull_distribution random number distribution
3879 * @p __x into the output stream @p __os.
3881 * @param __os An output stream.
3882 * @param __x A %weibull_distribution random number distribution.
3884 * @returns The output stream with the state of @p __x inserted or in
3887 template<typename _RealType
, typename _CharT
, typename _Traits
>
3888 std::basic_ostream
<_CharT
, _Traits
>&
3889 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
3890 const std::weibull_distribution
<_RealType
>&);
3893 * @brief Extracts a %weibull_distribution random number distribution
3894 * @p __x from the input stream @p __is.
3896 * @param __is An input stream.
3897 * @param __x A %weibull_distribution random number
3900 * @returns The input stream with @p __x extracted or in an error state.
3902 template<typename _RealType
, typename _CharT
, typename _Traits
>
3903 std::basic_istream
<_CharT
, _Traits
>&
3904 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
3905 std::weibull_distribution
<_RealType
>&);
3909 * @brief A extreme_value_distribution random number distribution.
3911 * The formula for the normal probability mass function is
3912 * @f$ p(x|a,b) = \frac{1}{b}
3913 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b})) @f$
3915 template<typename _RealType
= double>
3916 class extreme_value_distribution
3919 /** The type of the range of the distribution. */
3920 typedef _RealType result_type
;
3921 /** Parameter type. */
3924 typedef extreme_value_distribution
<_RealType
> distribution_type
;
3927 param_type(_RealType __a
= _RealType(0),
3928 _RealType __b
= _RealType(1))
3929 : _M_a(__a
), _M_b(__b
)
3946 extreme_value_distribution(_RealType __a
= _RealType(0),
3947 _RealType __b
= _RealType(1))
3948 : _M_param(__a
, __b
)
3952 extreme_value_distribution(const param_type
& __p
)
3957 * @brief Resets the distribution state.
3964 * @brief Return the @f$ a @f$ parameter of the distribution.
3968 { return _M_param
.a(); }
3971 * @brief Return the @f$ b @f$ parameter of the distribution.
3975 { return _M_param
.b(); }
3978 * @brief Returns the parameter set of the distribution.
3982 { return _M_param
; }
3985 * @brief Sets the parameter set of the distribution.
3986 * @param __param The new parameter set of the distribution.
3989 param(const param_type
& __param
)
3990 { _M_param
= __param
; }
3993 * @brief Returns the greatest lower bound value of the distribution.
3997 { return std::numeric_limits
<result_type
>::min(); }
4000 * @brief Returns the least upper bound value of the distribution.
4004 { return std::numeric_limits
<result_type
>::max(); }
4006 template<typename _UniformRandomNumberGenerator
>
4008 operator()(_UniformRandomNumberGenerator
& __urng
)
4009 { return this->operator()(__urng
, this->param()); }
4011 template<typename _UniformRandomNumberGenerator
>
4013 operator()(_UniformRandomNumberGenerator
& __urng
,
4014 const param_type
& __p
);
4017 param_type _M_param
;
4021 * @brief Inserts a %extreme_value_distribution random number distribution
4022 * @p __x into the output stream @p __os.
4024 * @param __os An output stream.
4025 * @param __x A %extreme_value_distribution random number distribution.
4027 * @returns The output stream with the state of @p __x inserted or in
4030 template<typename _RealType
, typename _CharT
, typename _Traits
>
4031 std::basic_ostream
<_CharT
, _Traits
>&
4032 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4033 const std::extreme_value_distribution
<_RealType
>&);
4036 * @brief Extracts a %extreme_value_distribution random number
4037 * distribution @p __x from the input stream @p __is.
4039 * @param __is An input stream.
4040 * @param __x A %extreme_value_distribution random number
4043 * @returns The input stream with @p __x extracted or in an error state.
4045 template<typename _RealType
, typename _CharT
, typename _Traits
>
4046 std::basic_istream
<_CharT
, _Traits
>&
4047 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4048 std::extreme_value_distribution
<_RealType
>&);
4052 * @brief A discrete_distribution random number distribution.
4054 * The formula for the discrete probability mass function is
4057 template<typename _IntType
= int>
4058 class discrete_distribution
4060 __glibcxx_class_requires(_IntType
, _IntegerConcept
)
4063 /** The type of the range of the distribution. */
4064 typedef _IntType result_type
;
4065 /** Parameter type. */
4068 typedef discrete_distribution
<_IntType
> distribution_type
;
4069 friend class discrete_distribution
<_IntType
>;
4072 : _M_prob(), _M_cp()
4073 { _M_initialize(); }
4075 template<typename _InputIterator
>
4076 param_type(_InputIterator __wbegin
,
4077 _InputIterator __wend
)
4078 : _M_prob(__wbegin
, __wend
), _M_cp()
4079 { _M_initialize(); }
4081 param_type(initializer_list
<double> __wil
)
4082 : _M_prob(__wil
.begin(), __wil
.end()), _M_cp()
4083 { _M_initialize(); }
4085 template<typename _Func
>
4086 param_type(size_t __nw
, double __xmin
, double __xmax
,
4090 probabilities() const
4097 std::vector
<double> _M_prob
;
4098 std::vector
<double> _M_cp
;
4101 discrete_distribution()
4105 template<typename _InputIterator
>
4106 discrete_distribution(_InputIterator __wbegin
,
4107 _InputIterator __wend
)
4108 : _M_param(__wbegin
, __wend
)
4111 discrete_distribution(initializer_list
<double> __wil
)
4115 template<typename _Func
>
4116 discrete_distribution(size_t __nw
, double __xmin
, double __xmax
,
4118 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4122 discrete_distribution(const param_type
& __p
)
4127 * @brief Resets the distribution state.
4134 * @brief Returns the probabilities of the distribution.
4137 probabilities() const
4138 { return _M_param
.probabilities(); }
4141 * @brief Returns the parameter set of the distribution.
4145 { return _M_param
; }
4148 * @brief Sets the parameter set of the distribution.
4149 * @param __param The new parameter set of the distribution.
4152 param(const param_type
& __param
)
4153 { _M_param
= __param
; }
4156 * @brief Returns the greatest lower bound value of the distribution.
4160 { return result_type(0); }
4163 * @brief Returns the least upper bound value of the distribution.
4167 { return this->_M_param
._M_prob
.size() - 1; }
4169 template<typename _UniformRandomNumberGenerator
>
4171 operator()(_UniformRandomNumberGenerator
& __urng
)
4172 { return this->operator()(__urng
, this->param()); }
4174 template<typename _UniformRandomNumberGenerator
>
4176 operator()(_UniformRandomNumberGenerator
& __urng
,
4177 const param_type
& __p
);
4180 * @brief Inserts a %discrete_distribution random number distribution
4181 * @p __x into the output stream @p __os.
4183 * @param __os An output stream.
4184 * @param __x A %discrete_distribution random number distribution.
4186 * @returns The output stream with the state of @p __x inserted or in
4189 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4190 friend std::basic_ostream
<_CharT
, _Traits
>&
4191 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4192 const std::discrete_distribution
<_IntType1
>&);
4195 * @brief Extracts a %discrete_distribution random number distribution
4196 * @p __x from the input stream @p __is.
4198 * @param __is An input stream.
4199 * @param __x A %discrete_distribution random number
4202 * @returns The input stream with @p __x extracted or in an error
4205 template<typename _IntType1
, typename _CharT
, typename _Traits
>
4206 friend std::basic_istream
<_CharT
, _Traits
>&
4207 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4208 std::discrete_distribution
<_IntType1
>&);
4211 param_type _M_param
;
4216 * @brief A piecewise_constant_distribution random number distribution.
4218 * The formula for the piecewise constant probability mass function is
4221 template<typename _RealType
= double>
4222 class piecewise_constant_distribution
4225 /** The type of the range of the distribution. */
4226 typedef _RealType result_type
;
4227 /** Parameter type. */
4230 typedef piecewise_constant_distribution
<_RealType
> distribution_type
;
4231 friend class piecewise_constant_distribution
<_RealType
>;
4234 : _M_int(), _M_den(), _M_cp()
4235 { _M_initialize(); }
4237 template<typename _InputIteratorB
, typename _InputIteratorW
>
4238 param_type(_InputIteratorB __bfirst
,
4239 _InputIteratorB __bend
,
4240 _InputIteratorW __wbegin
);
4242 template<typename _Func
>
4243 param_type(initializer_list
<_RealType
> __bil
, _Func __fw
);
4245 template<typename _Func
>
4246 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4249 std::vector
<_RealType
>
4261 std::vector
<_RealType
> _M_int
;
4262 std::vector
<double> _M_den
;
4263 std::vector
<double> _M_cp
;
4267 piecewise_constant_distribution()
4271 template<typename _InputIteratorB
, typename _InputIteratorW
>
4272 piecewise_constant_distribution(_InputIteratorB __bfirst
,
4273 _InputIteratorB __bend
,
4274 _InputIteratorW __wbegin
)
4275 : _M_param(__bfirst
, __bend
, __wbegin
)
4278 template<typename _Func
>
4279 piecewise_constant_distribution(initializer_list
<_RealType
> __bil
,
4281 : _M_param(__bil
, __fw
)
4284 template<typename _Func
>
4285 piecewise_constant_distribution(size_t __nw
,
4286 _RealType __xmin
, _RealType __xmax
,
4288 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4292 piecewise_constant_distribution(const param_type
& __p
)
4297 * @brief Resets the distribution state.
4304 * @brief Returns a vector of the intervals.
4306 std::vector
<_RealType
>
4308 { return _M_param
.intervals(); }
4311 * @brief Returns a vector of the probability densities.
4315 { return _M_param
.densities(); }
4318 * @brief Returns the parameter set of the distribution.
4322 { return _M_param
; }
4325 * @brief Sets the parameter set of the distribution.
4326 * @param __param The new parameter set of the distribution.
4329 param(const param_type
& __param
)
4330 { _M_param
= __param
; }
4333 * @brief Returns the greatest lower bound value of the distribution.
4337 { return this->_M_param
._M_int
.front(); }
4340 * @brief Returns the least upper bound value of the distribution.
4344 { return this->_M_param
._M_int
.back(); }
4346 template<typename _UniformRandomNumberGenerator
>
4348 operator()(_UniformRandomNumberGenerator
& __urng
)
4349 { return this->operator()(__urng
, this->param()); }
4351 template<typename _UniformRandomNumberGenerator
>
4353 operator()(_UniformRandomNumberGenerator
& __urng
,
4354 const param_type
& __p
);
4357 * @brief Inserts a %piecewise_constan_distribution random
4358 * number distribution @p __x into the output stream @p __os.
4360 * @param __os An output stream.
4361 * @param __x A %piecewise_constan_distribution random number
4364 * @returns The output stream with the state of @p __x inserted or in
4367 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4368 friend std::basic_ostream
<_CharT
, _Traits
>&
4369 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4370 const std::piecewise_constant_distribution
<_RealType1
>&);
4373 * @brief Extracts a %piecewise_constan_distribution random
4374 * number distribution @p __x from the input stream @p __is.
4376 * @param __is An input stream.
4377 * @param __x A %piecewise_constan_distribution random number
4380 * @returns The input stream with @p __x extracted or in an error
4383 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4384 friend std::basic_istream
<_CharT
, _Traits
>&
4385 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4386 std::piecewise_constant_distribution
<_RealType1
>&);
4389 param_type _M_param
;
4394 * @brief A piecewise_linear_distribution random number distribution.
4396 * The formula for the piecewise linear probability mass function is
4399 template<typename _RealType
= double>
4400 class piecewise_linear_distribution
4403 /** The type of the range of the distribution. */
4404 typedef _RealType result_type
;
4405 /** Parameter type. */
4408 typedef piecewise_linear_distribution
<_RealType
> distribution_type
;
4409 friend class piecewise_linear_distribution
<_RealType
>;
4413 template<typename _InputIteratorB
, typename _InputIteratorW
>
4414 param_type(_InputIteratorB __bfirst
,
4415 _InputIteratorB __bend
,
4416 _InputIteratorW __wbegin
);
4418 template<typename _Func
>
4419 param_type(initializer_list
<_RealType
> __bil
, _Func __fw
);
4421 template<typename _Func
>
4422 param_type(size_t __nw
, _RealType __xmin
, _RealType __xmax
,
4425 std::vector
<_RealType
>
4437 std::vector
<_RealType
> _M_int
;
4438 std::vector
<double> _M_den
;
4439 std::vector
<double> _M_cp
;
4440 std::vector
<double> _M_m
;
4444 piecewise_linear_distribution()
4448 template<typename _InputIteratorB
, typename _InputIteratorW
>
4449 piecewise_linear_distribution(_InputIteratorB __bfirst
,
4450 _InputIteratorB __bend
,
4451 _InputIteratorW __wbegin
)
4452 : _M_param(__bfirst
, __bend
, __wbegin
)
4455 template<typename _Func
>
4456 piecewise_linear_distribution(initializer_list
<_RealType
> __bil
,
4458 : _M_param(__bil
, __fw
)
4461 template<typename _Func
>
4462 piecewise_linear_distribution(size_t __nw
,
4463 _RealType __xmin
, _RealType __xmax
,
4465 : _M_param(__nw
, __xmin
, __xmax
, __fw
)
4469 piecewise_linear_distribution(const param_type
& __p
)
4474 * Resets the distribution state.
4481 * @brief Return the intervals of the distribution.
4483 std::vector
<_RealType
>
4485 { return _M_param
.intervals(); }
4488 * @brief Return a vector of the probability densities of the
4493 { return _M_param
.densities(); }
4496 * @brief Returns the parameter set of the distribution.
4500 { return _M_param
; }
4503 * @brief Sets the parameter set of the distribution.
4504 * @param __param The new parameter set of the distribution.
4507 param(const param_type
& __param
)
4508 { _M_param
= __param
; }
4511 * @brief Returns the greatest lower bound value of the distribution.
4515 { return this->_M_param
._M_int
.front(); }
4518 * @brief Returns the least upper bound value of the distribution.
4522 { return this->_M_param
._M_int
.back(); }
4524 template<typename _UniformRandomNumberGenerator
>
4526 operator()(_UniformRandomNumberGenerator
& __urng
)
4527 { return this->operator()(__urng
, this->param()); }
4529 template<typename _UniformRandomNumberGenerator
>
4531 operator()(_UniformRandomNumberGenerator
& __urng
,
4532 const param_type
& __p
);
4535 * @brief Inserts a %piecewise_linear_distribution random number
4536 * distribution @p __x into the output stream @p __os.
4538 * @param __os An output stream.
4539 * @param __x A %piecewise_linear_distribution random number
4542 * @returns The output stream with the state of @p __x inserted or in
4545 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4546 friend std::basic_ostream
<_CharT
, _Traits
>&
4547 operator<<(std::basic_ostream
<_CharT
, _Traits
>&,
4548 const std::piecewise_linear_distribution
<_RealType1
>&);
4551 * @brief Extracts a %piecewise_linear_distribution random number
4552 * distribution @p __x from the input stream @p __is.
4554 * @param __is An input stream.
4555 * @param __x A %piecewise_linear_distribution random number
4558 * @returns The input stream with @p __x extracted or in an error
4561 template<typename _RealType1
, typename _CharT
, typename _Traits
>
4562 friend std::basic_istream
<_CharT
, _Traits
>&
4563 operator>>(std::basic_istream
<_CharT
, _Traits
>&,
4564 std::piecewise_linear_distribution
<_RealType1
>&);
4567 param_type _M_param
;
4571 /* @} */ // group std_random_distributions_poisson
4573 /* @} */ // group std_random_distributions
4576 * @addtogroup std_random_utilities Random Number Utilities
4577 * @ingroup std_random
4582 * @brief The seed_seq class generates sequences of seeds for random
4583 * number generators.
4589 /** The type of the seed vales. */
4590 typedef uint_least32_t result_type
;
4592 /** Default constructor. */
4597 template<typename _IntType
>
4598 seed_seq(std::initializer_list
<_IntType
> il
);
4600 template<typename _InputIterator
>
4601 seed_seq(_InputIterator __begin
, _InputIterator __end
);
4603 // generating functions
4604 template<typename _RandomAccessIterator
>
4606 generate(_RandomAccessIterator __begin
, _RandomAccessIterator __end
);
4608 // property functions
4610 { return _M_v
.size(); }
4612 template<typename OutputIterator
>
4614 param(OutputIterator __dest
) const
4615 { std::copy(_M_v
.begin(), _M_v
.end(), __dest
); }
4619 std::vector
<result_type
> _M_v
;
4622 /* @} */ // group std_random_utilities
4624 /* @} */ // group std_random