1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
35 // [26.4] Random number generation
38 * @defgroup random Random Number Generation
41 * A facility for generating random numbers on selected distributions.
46 * @brief A function template for converting the output of a (integral)
47 * uniform random number generator to a floatng point result in the range
50 template<typename _RealType, size_t __bits,
51 typename _UniformRandomNumberGenerator>
53 generate_canonical(_UniformRandomNumberGenerator& __g);
56 * Implementation-space details.
60 template<typename _UIntType, size_t __w,
61 bool = __w < static_cast<size_t>
62 (std::numeric_limits<_UIntType>::digits)>
64 { static const _UIntType __value = 0; };
66 template<typename _UIntType, size_t __w>
67 struct _Shift<_UIntType, __w, true>
68 { static const _UIntType __value = _UIntType(1) << __w; };
70 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
73 // Dispatch based on modulus value to prevent divide-by-zero compile-time
74 // errors when m == 0.
75 template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
78 { return _Mod<_Tp, __m, __a, __c, __m == 0>::__calc(__x); }
81 * An adaptor class for converting the output of any Generator into
82 * the input for a specific Distribution.
84 template<typename _Engine, typename _DInputType>
89 _Adaptor(_Engine& __g)
94 { return _DInputType(0); }
98 { return _DInputType(1); }
101 * Converts a value generated by the adapted random number generator
102 * into a value in the input domain for the dependent random number
108 return std::generate_canonical<_DInputType,
109 std::numeric_limits<_DInputType>::digits,
116 } // namespace __detail
119 * @addtogroup random_generators Random Number Generators
122 * These classes define objects which provide random or pseudorandom
123 * numbers, either from a discrete or a continuous interval. The
124 * random number generator supplied as a part of this library are
125 * all uniform random number generators which provide a sequence of
126 * random number uniformly distributed over their range.
128 * A number generator is a function object with an operator() that
129 * takes zero arguments and returns a number.
131 * A compliant random number generator must satisfy the following
132 * requirements. <table border=1 cellpadding=10 cellspacing=0>
133 * <caption align=top>Random Number Generator Requirements</caption>
134 * <tr><td>To be documented.</td></tr> </table>
140 * @brief A model of a linear congruential random number generator.
142 * A random number generator that produces pseudorandom numbers via
145 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
148 * The template parameter @p _UIntType must be an unsigned integral type
149 * large enough to store values up to (__m-1). If the template parameter
150 * @p __m is 0, the modulus @p __m used is
151 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
152 * parameters @p __a and @p __c must be less than @p __m.
154 * The size of the state is @f$1@f$.
156 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
157 class linear_congruential_engine
159 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
160 "substituting _UIntType not an unsigned integral type");
161 static_assert(__m == 0u || (__a < __m && __c < __m),
162 "template argument substituting __m out of bounds");
165 /** The type of the generated random value. */
166 typedef _UIntType result_type;
168 /** The multiplier. */
169 static const result_type multiplier = __a;
171 static const result_type increment = __c;
173 static const result_type modulus = __m;
174 static const result_type default_seed = 1u;
177 * @brief Constructs a %linear_congruential_engine random number
178 * generator engine with seed @p __s. The default seed value
181 * @param __s The initial seed value.
184 linear_congruential_engine(result_type __s = default_seed)
188 * @brief Constructs a %linear_congruential_engine random number
189 * generator engine seeded from the seed sequence @p __q.
191 * @param __q the seed sequence.
193 template<typename _Sseq, typename = typename
194 std::enable_if<!std::is_same<_Sseq, linear_congruential_engine>::value>
197 linear_congruential_engine(_Sseq& __q)
201 * @brief Reseeds the %linear_congruential_engine random number generator
202 * engine sequence to the seed @p __s.
204 * @param __s The new seed.
207 seed(result_type __s = default_seed);
210 * @brief Reseeds the %linear_congruential_engine random number generator
212 * sequence using values from the seed sequence @p __q.
214 * @param __q the seed sequence.
216 template<typename _Sseq>
217 typename std::enable_if<std::is_class<_Sseq>::value>::type
221 * @brief Gets the smallest possible value in the output range.
223 * The minimum depends on the @p __c parameter: if it is zero, the
224 * minimum generated must be > 0, otherwise 0 is allowed.
226 * @todo This should be constexpr.
230 { return __c == 0u ? 1u : 0u; }
233 * @brief Gets the largest possible value in the output range.
235 * @todo This should be constexpr.
242 * @brief Discard a sequence of random numbers.
244 * @todo Look for a faster way to do discard.
247 discard(unsigned long long __z)
249 for (; __z != 0ULL; --__z)
254 * @brief Gets the next random number in the sequence.
259 _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
264 * @brief Compares two linear congruential random number generator
265 * objects of the same type for equality.
267 * @param __lhs A linear congruential random number generator object.
268 * @param __rhs Another linear congruential random number generator
271 * @returns true if the two objects are equal, false otherwise.
274 operator==(const linear_congruential_engine& __lhs,
275 const linear_congruential_engine& __rhs)
276 { return __lhs._M_x == __rhs._M_x; }
279 * @brief Writes the textual representation of the state x(i) of x to
282 * @param __os The output stream.
283 * @param __lcr A % linear_congruential_engine random number generator.
286 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
287 _UIntType1 __m1, typename _CharT, typename _Traits>
288 friend std::basic_ostream<_CharT, _Traits>&
289 operator<<(std::basic_ostream<_CharT, _Traits>&,
290 const std::linear_congruential_engine<_UIntType1,
294 * @brief Sets the state of the engine by reading its textual
295 * representation from @p __is.
297 * The textual representation must have been previously written using
298 * an output stream whose imbued locale and whose type's template
299 * specialization arguments _CharT and _Traits were the same as those
302 * @param __is The input stream.
303 * @param __lcr A % linear_congruential_engine random number generator.
306 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
307 _UIntType1 __m1, typename _CharT, typename _Traits>
308 friend std::basic_istream<_CharT, _Traits>&
309 operator>>(std::basic_istream<_CharT, _Traits>&,
310 std::linear_congruential_engine<_UIntType1, __a1,
319 * A generalized feedback shift register discrete random number generator.
321 * This algorithm avoids multiplication and division and is designed to be
322 * friendly to a pipelined architecture. If the parameters are chosen
323 * correctly, this generator will produce numbers with a very long period and
324 * fairly good apparent entropy, although still not cryptographically strong.
326 * The best way to use this generator is with the predefined mt19937 class.
328 * This algorithm was originally invented by Makoto Matsumoto and
331 * @var word_size The number of bits in each element of the state vector.
332 * @var state_size The degree of recursion.
333 * @var shift_size The period parameter.
334 * @var mask_bits The separation point bit index.
335 * @var parameter_a The last row of the twist matrix.
336 * @var output_u The first right-shift tempering matrix parameter.
337 * @var output_s The first left-shift tempering matrix parameter.
338 * @var output_b The first left-shift tempering matrix mask.
339 * @var output_t The second left-shift tempering matrix parameter.
340 * @var output_c The second left-shift tempering matrix mask.
341 * @var output_l The second right-shift tempering matrix parameter.
343 template<typename _UIntType, size_t __w,
344 size_t __n, size_t __m, size_t __r,
345 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
346 _UIntType __b, size_t __t,
347 _UIntType __c, size_t __l, _UIntType __f>
348 class mersenne_twister_engine
350 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
351 "substituting _UIntType not an unsigned integral type");
352 static_assert(1u <= __m && __m <= __n,
353 "template argument substituting __m out of bounds");
354 static_assert(__r <= __w, "template argument substituting "
356 static_assert(__u <= __w, "template argument substituting "
358 static_assert(__s <= __w, "template argument substituting "
360 static_assert(__t <= __w, "template argument substituting "
362 static_assert(__l <= __w, "template argument substituting "
364 static_assert(__w <= std::numeric_limits<_UIntType>::digits,
365 "template argument substituting __w out of bound");
366 static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
367 "template argument substituting __a out of bound");
368 static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
369 "template argument substituting __b out of bound");
370 static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
371 "template argument substituting __c out of bound");
372 static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
373 "template argument substituting __d out of bound");
374 static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
375 "template argument substituting __f out of bound");
378 /** The type of the generated random value. */
379 typedef _UIntType result_type;
382 static const size_t word_size = __w;
383 static const size_t state_size = __n;
384 static const size_t shift_size = __m;
385 static const size_t mask_bits = __r;
386 static const result_type xor_mask = __a;
387 static const size_t tempering_u = __u;
388 static const result_type tempering_d = __d;
389 static const size_t tempering_s = __s;
390 static const result_type tempering_b = __b;
391 static const size_t tempering_t = __t;
392 static const result_type tempering_c = __c;
393 static const size_t tempering_l = __l;
394 static const result_type initialization_multiplier = __f;
395 static const result_type default_seed = 5489u;
397 // constructors and member function
399 mersenne_twister_engine(result_type __sd = default_seed)
403 * @brief Constructs a %mersenne_twister_engine random number generator
404 * engine seeded from the seed sequence @p __q.
406 * @param __q the seed sequence.
408 template<typename _Sseq, typename = typename
409 std::enable_if<!std::is_same<_Sseq, mersenne_twister_engine>::value>
412 mersenne_twister_engine(_Sseq& __q)
416 seed(result_type __sd = default_seed);
418 template<typename _Sseq>
419 typename std::enable_if<std::is_class<_Sseq>::value>::type
423 * @brief Gets the smallest possible value in the output range.
425 * @todo This should be constexpr.
432 * @brief Gets the largest possible value in the output range.
434 * @todo This should be constexpr.
438 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
441 * @brief Discard a sequence of random numbers.
443 * @todo Look for a faster way to do discard.
446 discard(unsigned long long __z)
448 for (; __z != 0ULL; --__z)
456 * @brief Compares two % mersenne_twister_engine random number generator
457 * objects of the same type for equality.
459 * @param __lhs A % mersenne_twister_engine random number generator
461 * @param __rhs Another % mersenne_twister_engine random number
464 * @returns true if the two objects are equal, false otherwise.
467 operator==(const mersenne_twister_engine& __lhs,
468 const mersenne_twister_engine& __rhs)
469 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
472 * @brief Inserts the current state of a % mersenne_twister_engine
473 * random number generator engine @p __x into the output stream
476 * @param __os An output stream.
477 * @param __x A % mersenne_twister_engine random number generator
480 * @returns The output stream with the state of @p __x inserted or in
483 template<typename _UIntType1,
484 size_t __w1, size_t __n1,
485 size_t __m1, size_t __r1,
486 _UIntType1 __a1, size_t __u1,
487 _UIntType1 __d1, size_t __s1,
488 _UIntType1 __b1, size_t __t1,
489 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
490 typename _CharT, typename _Traits>
491 friend std::basic_ostream<_CharT, _Traits>&
492 operator<<(std::basic_ostream<_CharT, _Traits>&,
493 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
494 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
498 * @brief Extracts the current state of a % mersenne_twister_engine
499 * random number generator engine @p __x from the input stream
502 * @param __is An input stream.
503 * @param __x A % mersenne_twister_engine random number generator
506 * @returns The input stream with the state of @p __x extracted or in
509 template<typename _UIntType1,
510 size_t __w1, size_t __n1,
511 size_t __m1, size_t __r1,
512 _UIntType1 __a1, size_t __u1,
513 _UIntType1 __d1, size_t __s1,
514 _UIntType1 __b1, size_t __t1,
515 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
516 typename _CharT, typename _Traits>
517 friend std::basic_istream<_CharT, _Traits>&
518 operator>>(std::basic_istream<_CharT, _Traits>&,
519 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
520 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
524 _UIntType _M_x[state_size];
529 * @brief The Marsaglia-Zaman generator.
531 * This is a model of a Generalized Fibonacci discrete random number
532 * generator, sometimes referred to as the SWC generator.
534 * A discrete random number generator that produces pseudorandom
537 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
540 * The size of the state is @f$r@f$
541 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
543 * @var _M_x The state of the generator. This is a ring buffer.
544 * @var _M_carry The carry.
545 * @var _M_p Current index of x(i - r).
547 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
548 class subtract_with_carry_engine
550 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
551 "substituting _UIntType not an unsigned integral type");
552 static_assert(0u < __s && __s < __r,
553 "template argument substituting __s out of bounds");
554 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
555 "template argument substituting __w out of bounds");
558 /** The type of the generated random value. */
559 typedef _UIntType result_type;
562 static const size_t word_size = __w;
563 static const size_t short_lag = __s;
564 static const size_t long_lag = __r;
565 static const result_type default_seed = 19780503u;
568 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
569 * random number generator.
572 subtract_with_carry_engine(result_type __sd = default_seed)
576 * @brief Constructs a %subtract_with_carry_engine random number engine
577 * seeded from the seed sequence @p __q.
579 * @param __q the seed sequence.
581 template<typename _Sseq, typename = typename
582 std::enable_if<!std::is_same<_Sseq, subtract_with_carry_engine>::value>
585 subtract_with_carry_engine(_Sseq& __q)
589 * @brief Seeds the initial state @f$x_0@f$ of the random number
592 * N1688[4.19] modifies this as follows. If @p __value == 0,
593 * sets value to 19780503. In any case, with a linear
594 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
595 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
596 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
597 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
598 * set carry to 1, otherwise sets carry to 0.
601 seed(result_type __sd = default_seed);
604 * @brief Seeds the initial state @f$x_0@f$ of the
605 * % subtract_with_carry_engine random number generator.
607 template<typename _Sseq>
608 typename std::enable_if<std::is_class<_Sseq>::value>::type
612 * @brief Gets the inclusive minimum value of the range of random
613 * integers returned by this generator.
615 * @todo This should be constexpr.
622 * @brief Gets the inclusive maximum value of the range of random
623 * integers returned by this generator.
625 * @todo This should be constexpr.
629 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
632 * @brief Discard a sequence of random numbers.
634 * @todo Look for a faster way to do discard.
637 discard(unsigned long long __z)
639 for (; __z != 0ULL; --__z)
644 * @brief Gets the next random number in the sequence.
650 * @brief Compares two % subtract_with_carry_engine random number
651 * generator objects of the same type for equality.
653 * @param __lhs A % subtract_with_carry_engine random number generator
655 * @param __rhs Another % subtract_with_carry_engine random number
658 * @returns true if the two objects are equal, false otherwise.
661 operator==(const subtract_with_carry_engine& __lhs,
662 const subtract_with_carry_engine& __rhs)
663 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
666 * @brief Inserts the current state of a % subtract_with_carry_engine
667 * random number generator engine @p __x into the output stream
670 * @param __os An output stream.
671 * @param __x A % subtract_with_carry_engine random number generator
674 * @returns The output stream with the state of @p __x inserted or in
677 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
678 typename _CharT, typename _Traits>
679 friend std::basic_ostream<_CharT, _Traits>&
680 operator<<(std::basic_ostream<_CharT, _Traits>&,
681 const std::subtract_with_carry_engine<_UIntType1, __w1,
685 * @brief Extracts the current state of a % subtract_with_carry_engine
686 * random number generator engine @p __x from the input stream
689 * @param __is An input stream.
690 * @param __x A % subtract_with_carry_engine random number generator
693 * @returns The input stream with the state of @p __x extracted or in
696 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
697 typename _CharT, typename _Traits>
698 friend std::basic_istream<_CharT, _Traits>&
699 operator>>(std::basic_istream<_CharT, _Traits>&,
700 std::subtract_with_carry_engine<_UIntType1, __w1,
704 _UIntType _M_x[long_lag];
710 * Produces random numbers from some base engine by discarding blocks of
713 * 0 <= @p __r <= @p __p
715 template<typename _RandomNumberEngine, size_t __p, size_t __r>
716 class discard_block_engine
718 static_assert(1 <= __r && __r <= __p,
719 "template argument substituting __r out of bounds");
722 /** The type of the generated random value. */
723 typedef typename _RandomNumberEngine::result_type result_type;
726 static const size_t block_size = __p;
727 static const size_t used_block = __r;
730 * @brief Constructs a default %discard_block_engine engine.
732 * The underlying engine is default constructed as well.
734 discard_block_engine()
735 : _M_b(), _M_n(0) { }
738 * @brief Copy constructs a %discard_block_engine engine.
740 * Copies an existing base class random number generator.
741 * @param rng An existing (base class) engine object.
744 discard_block_engine(const _RandomNumberEngine& __rne)
745 : _M_b(__rne), _M_n(0) { }
748 * @brief Move constructs a %discard_block_engine engine.
750 * Copies an existing base class random number generator.
751 * @param rng An existing (base class) engine object.
754 discard_block_engine(_RandomNumberEngine&& __rne)
755 : _M_b(std::move(__rne)), _M_n(0) { }
758 * @brief Seed constructs a %discard_block_engine engine.
760 * Constructs the underlying generator engine seeded with @p __s.
761 * @param __s A seed value for the base class engine.
764 discard_block_engine(result_type __s)
765 : _M_b(__s), _M_n(0) { }
768 * @brief Generator construct a %discard_block_engine engine.
770 * @param __q A seed sequence.
772 template<typename _Sseq, typename = typename
773 std::enable_if<!std::is_same<_Sseq, discard_block_engine>::value
774 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
777 discard_block_engine(_Sseq& __q)
782 * @brief Reseeds the %discard_block_engine object with the default
783 * seed for the underlying base class generator engine.
793 * @brief Reseeds the %discard_block_engine object with the default
794 * seed for the underlying base class generator engine.
797 seed(result_type __s)
804 * @brief Reseeds the %discard_block_engine object with the given seed
806 * @param __q A seed generator function.
808 template<typename _Sseq>
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(std::is_unsigned<_UIntType>::value, "template argument "
924 "substituting _UIntType not an unsigned integral type");
925 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
926 "template argument substituting __w out of bounds");
929 /** The type of the generated random value. */
930 typedef _UIntType result_type;
933 * @brief Constructs a default %independent_bits_engine engine.
935 * The underlying engine is default constructed as well.
937 independent_bits_engine()
941 * @brief Copy constructs a %independent_bits_engine engine.
943 * Copies an existing base class random number generator.
944 * @param rng An existing (base class) engine object.
947 independent_bits_engine(const _RandomNumberEngine& __rne)
951 * @brief Move constructs a %independent_bits_engine engine.
953 * Copies an existing base class random number generator.
954 * @param rng An existing (base class) engine object.
957 independent_bits_engine(_RandomNumberEngine&& __rne)
958 : _M_b(std::move(__rne)) { }
961 * @brief Seed constructs a %independent_bits_engine engine.
963 * Constructs the underlying generator engine seeded with @p __s.
964 * @param __s A seed value for the base class engine.
967 independent_bits_engine(result_type __s)
971 * @brief Generator construct a %independent_bits_engine engine.
973 * @param __q A seed sequence.
975 template<typename _Sseq, typename = typename
976 std::enable_if<!std::is_same<_Sseq, independent_bits_engine>::value
977 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
980 independent_bits_engine(_Sseq& __q)
985 * @brief Reseeds the %independent_bits_engine object with the default
986 * seed for the underlying base class generator engine.
993 * @brief Reseeds the %independent_bits_engine object with the default
994 * seed for the underlying base class generator engine.
997 seed(result_type __s)
1001 * @brief Reseeds the %independent_bits_engine object with the given
1003 * @param __q A seed generator function.
1005 template<typename _Sseq>
1011 * @brief Gets a const reference to the underlying generator engine
1014 const _RandomNumberEngine&
1019 * @brief Gets the minimum value in the generated random number range.
1021 * @todo This should be constexpr.
1028 * @brief Gets the maximum value in the generated random number range.
1030 * @todo This should be constexpr.
1034 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
1037 * @brief Discard a sequence of random numbers.
1039 * @todo Look for a faster way to do discard.
1042 discard(unsigned long long __z)
1044 for (; __z != 0ULL; --__z)
1049 * @brief Gets the next value in the generated random number sequence.
1055 * @brief Compares two %independent_bits_engine random number generator
1056 * objects of the same type for equality.
1058 * @param __lhs A %independent_bits_engine random number generator
1060 * @param __rhs Another %independent_bits_engine random number generator
1063 * @returns true if the two objects are equal, false otherwise.
1066 operator==(const independent_bits_engine& __lhs,
1067 const independent_bits_engine& __rhs)
1068 { return __lhs._M_b == __rhs._M_b; }
1071 * @brief Extracts the current state of a % subtract_with_carry_engine
1072 * random number generator engine @p __x from the input stream
1075 * @param __is An input stream.
1076 * @param __x A %independent_bits_engine random number generator
1079 * @returns The input stream with the state of @p __x extracted or in
1082 template<typename _CharT, typename _Traits>
1083 friend std::basic_istream<_CharT, _Traits>&
1084 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1085 std::independent_bits_engine<_RandomNumberEngine,
1086 __w, _UIntType>& __x)
1093 _RandomNumberEngine _M_b;
1097 * @brief Inserts the current state of a %independent_bits_engine random
1098 * number generator engine @p __x into the output stream @p __os.
1100 * @param __os An output stream.
1101 * @param __x A %independent_bits_engine random number generator engine.
1103 * @returns The output stream with the state of @p __x inserted or in
1106 template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1107 typename _CharT, typename _Traits>
1108 std::basic_ostream<_CharT, _Traits>&
1109 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1110 const std::independent_bits_engine<_RandomNumberEngine,
1111 __w, _UIntType>& __x)
1118 * @brief Produces random numbers by combining random numbers from some
1119 * base engine to produce random numbers with a specifies number of bits
1122 template<typename _RandomNumberEngine, size_t __k>
1123 class shuffle_order_engine
1125 static_assert(1u <= __k, "template argument substituting "
1126 "__k out of bound");
1129 /** The type of the generated random value. */
1130 typedef typename _RandomNumberEngine::result_type result_type;
1132 static const size_t table_size = __k;
1135 * @brief Constructs a default %shuffle_order_engine engine.
1137 * The underlying engine is default constructed as well.
1139 shuffle_order_engine()
1141 { _M_initialize(); }
1144 * @brief Copy constructs a %shuffle_order_engine engine.
1146 * Copies an existing base class random number generator.
1147 * @param rng An existing (base class) engine object.
1150 shuffle_order_engine(const _RandomNumberEngine& __rne)
1152 { _M_initialize(); }
1155 * @brief Move constructs a %shuffle_order_engine engine.
1157 * Copies an existing base class random number generator.
1158 * @param rng An existing (base class) engine object.
1161 shuffle_order_engine(_RandomNumberEngine&& __rne)
1162 : _M_b(std::move(__rne))
1163 { _M_initialize(); }
1166 * @brief Seed constructs a %shuffle_order_engine engine.
1168 * Constructs the underlying generator engine seeded with @p __s.
1169 * @param __s A seed value for the base class engine.
1172 shuffle_order_engine(result_type __s)
1174 { _M_initialize(); }
1177 * @brief Generator construct a %shuffle_order_engine engine.
1179 * @param __q A seed sequence.
1181 template<typename _Sseq, typename = typename
1182 std::enable_if<!std::is_same<_Sseq, shuffle_order_engine>::value
1183 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
1186 shuffle_order_engine(_Sseq& __q)
1188 { _M_initialize(); }
1191 * @brief Reseeds the %shuffle_order_engine object with the default seed
1192 for the underlying base class generator engine.
1202 * @brief Reseeds the %shuffle_order_engine object with the default seed
1203 * for the underlying base class generator engine.
1206 seed(result_type __s)
1213 * @brief Reseeds the %shuffle_order_engine object with the given seed
1215 * @param __q A seed generator function.
1217 template<typename _Sseq>
1226 * Gets a const reference to the underlying generator engine object.
1228 const _RandomNumberEngine&
1233 * Gets the minimum value in the generated random number range.
1235 * @todo This should be constexpr.
1239 { return _M_b.min(); }
1242 * Gets the maximum value in the generated random number range.
1244 * @todo This should be constexpr.
1248 { return _M_b.max(); }
1251 * Discard a sequence of random numbers.
1253 * @todo Look for a faster way to do discard.
1256 discard(unsigned long long __z)
1258 for (; __z != 0ULL; --__z)
1263 * Gets the next value in the generated random number sequence.
1269 * Compares two %shuffle_order_engine random number generator objects
1270 * of the same type for equality.
1272 * @param __lhs A %shuffle_order_engine random number generator object.
1273 * @param __rhs Another %shuffle_order_engine random number generator
1276 * @returns true if the two objects are equal, false otherwise.
1279 operator==(const shuffle_order_engine& __lhs,
1280 const shuffle_order_engine& __rhs)
1281 { return __lhs._M_b == __rhs._M_b; }
1284 * @brief Inserts the current state of a %shuffle_order_engine random
1285 * number generator engine @p __x into the output stream
1288 * @param __os An output stream.
1289 * @param __x A %shuffle_order_engine random number generator engine.
1291 * @returns The output stream with the state of @p __x inserted or in
1294 template<typename _RandomNumberEngine1, size_t __k1,
1295 typename _CharT, typename _Traits>
1296 friend std::basic_ostream<_CharT, _Traits>&
1297 operator<<(std::basic_ostream<_CharT, _Traits>&,
1298 const std::shuffle_order_engine<_RandomNumberEngine1,
1302 * @brief Extracts the current state of a % subtract_with_carry_engine
1303 * random number generator engine @p __x from the input stream
1306 * @param __is An input stream.
1307 * @param __x A %shuffle_order_engine random number generator engine.
1309 * @returns The input stream with the state of @p __x extracted or in
1312 template<typename _RandomNumberEngine1, size_t __k1,
1313 typename _CharT, typename _Traits>
1314 friend std::basic_istream<_CharT, _Traits>&
1315 operator>>(std::basic_istream<_CharT, _Traits>&,
1316 std::shuffle_order_engine<_RandomNumberEngine1, __k1>&);
1319 void _M_initialize()
1321 for (size_t __i = 0; __i < __k; ++__i)
1326 _RandomNumberEngine _M_b;
1327 result_type _M_v[__k];
1332 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1334 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1338 * An alternative LCR (Lehmer Generator function).
1340 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1344 * The classic Mersenne Twister.
1347 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1348 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1349 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1351 typedef mersenne_twister_engine<
1357 0xefc60000UL, 18, 1812433253UL> mt19937;
1360 * An alternative Mersenne Twister.
1362 typedef mersenne_twister_engine<
1365 0xb5026f5aa96619e9ULL, 29,
1366 0x5555555555555555ULL, 17,
1367 0x71d67fffeda60000ULL, 37,
1368 0xfff7eee000000000ULL, 43,
1369 6364136223846793005ULL> mt19937_64;
1371 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1374 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1377 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1379 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1381 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1383 typedef minstd_rand0 default_random_engine;
1386 * A standard interface to a platform-specific non-deterministic
1387 * random number generator (if any are available).
1392 /** The type of the generated random value. */
1393 typedef unsigned int result_type;
1395 // constructors, destructors and member functions
1397 #ifdef _GLIBCXX_USE_RANDOM_TR1
1400 random_device(const std::string& __token = "/dev/urandom")
1402 if ((__token != "/dev/urandom" && __token != "/dev/random")
1403 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1404 std::__throw_runtime_error(__N("random_device::"
1405 "random_device(const std::string&)"));
1409 { std::fclose(_M_file); }
1414 random_device(const std::string& __token = "mt19937")
1415 : _M_mt(_M_strtoul(__token)) { }
1418 static unsigned long
1419 _M_strtoul(const std::string& __str)
1421 unsigned long __ret = 5489UL;
1422 if (__str != "mt19937")
1424 const char* __nptr = __str.c_str();
1426 __ret = std::strtoul(__nptr, &__endptr, 0);
1427 if (*__nptr == '\0' || *__endptr != '\0')
1428 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1429 "(const std::string&)"));
1440 { return std::numeric_limits<result_type>::min(); }
1444 { return std::numeric_limits<result_type>::max(); }
1453 #ifdef _GLIBCXX_USE_RANDOM_TR1
1455 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1463 // No copy functions.
1464 random_device(const random_device&) = delete;
1465 void operator=(const random_device&) = delete;
1469 #ifdef _GLIBCXX_USE_RANDOM_TR1
1476 /* @} */ // group random_generators
1479 * @addtogroup random_distributions Random Number Distributions
1485 * @addtogroup random_distributions_uniform Uniform Distributions
1486 * @ingroup random_distributions
1491 * @brief Uniform discrete distribution for random numbers.
1492 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1493 * probability throughout the range.
1495 template<typename _IntType = int>
1496 class uniform_int_distribution
1498 static_assert(std::is_integral<_IntType>::value,
1499 "template argument not an integral type");
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,
1511 _IntType __b = std::numeric_limits<_IntType>::max())
1512 : _M_a(__a), _M_b(__b)
1514 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1532 * @brief Constructs a uniform distribution object.
1535 uniform_int_distribution(_IntType __a = 0,
1536 _IntType __b = std::numeric_limits<_IntType>::max())
1537 : _M_param(__a, __b)
1541 uniform_int_distribution(const param_type& __p)
1546 * @brief Resets the distribution state.
1548 * Does nothing for the uniform integer distribution.
1555 { return _M_param.a(); }
1559 { return _M_param.b(); }
1562 * @brief Returns the inclusive lower bound of the distribution range.
1566 { return this->a(); }
1569 * @brief Returns the inclusive upper bound of the distribution range.
1573 { return this->b(); }
1576 * @brief Returns the parameter set of the distribution.
1580 { return _M_param; }
1583 * @brief Sets the parameter set of the distribution.
1584 * @param __param The new parameter set of the distribution.
1587 param(const param_type& __param)
1588 { _M_param = __param; }
1591 * Gets a uniformly distributed random number in the range
1594 template<typename _UniformRandomNumberGenerator>
1596 operator()(_UniformRandomNumberGenerator& __urng)
1597 { return this->operator()(__urng, this->param()); }
1600 * Gets a uniform random number in the range @f$[0, n)@f$.
1602 * This function is aimed at use with std::random_shuffle.
1604 template<typename _UniformRandomNumberGenerator>
1606 operator()(_UniformRandomNumberGenerator& __urng,
1607 const param_type& __p);
1609 param_type _M_param;
1613 * @brief Inserts a %uniform_int_distribution random number
1614 * distribution @p __x into the output stream @p os.
1616 * @param __os An output stream.
1617 * @param __x A %uniform_int_distribution random number distribution.
1619 * @returns The output stream with the state of @p __x inserted or in
1622 template<typename _IntType, typename _CharT, typename _Traits>
1623 std::basic_ostream<_CharT, _Traits>&
1624 operator<<(std::basic_ostream<_CharT, _Traits>&,
1625 const std::uniform_int_distribution<_IntType>&);
1628 * @brief Extracts a %uniform_int_distribution random number distribution
1629 * @p __x from the input stream @p __is.
1631 * @param __is An input stream.
1632 * @param __x A %uniform_int_distribution random number generator engine.
1634 * @returns The input stream with @p __x extracted or in an error state.
1636 template<typename _IntType, typename _CharT, typename _Traits>
1637 std::basic_istream<_CharT, _Traits>&
1638 operator>>(std::basic_istream<_CharT, _Traits>&,
1639 std::uniform_int_distribution<_IntType>&);
1643 * @brief Uniform continuous distribution for random numbers.
1645 * A continuous random distribution on the range [min, max) with equal
1646 * probability throughout the range. The URNG should be real-valued and
1647 * deliver number in the range [0, 1).
1649 template<typename _RealType = double>
1650 class uniform_real_distribution
1652 static_assert(std::is_floating_point<_RealType>::value,
1653 "template argument not a floating point type");
1656 /** The type of the range of the distribution. */
1657 typedef _RealType result_type;
1658 /** Parameter type. */
1661 typedef uniform_real_distribution<_RealType> distribution_type;
1664 param_type(_RealType __a = _RealType(0),
1665 _RealType __b = _RealType(1))
1666 : _M_a(__a), _M_b(__b)
1668 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1686 * @brief Constructs a uniform_real_distribution object.
1688 * @param __min [IN] The lower bound of the distribution.
1689 * @param __max [IN] The upper bound of the distribution.
1692 uniform_real_distribution(_RealType __a = _RealType(0),
1693 _RealType __b = _RealType(1))
1694 : _M_param(__a, __b)
1698 uniform_real_distribution(const param_type& __p)
1703 * @brief Resets the distribution state.
1705 * Does nothing for the uniform real distribution.
1712 { return _M_param.a(); }
1716 { return _M_param.b(); }
1719 * @brief Returns the inclusive lower bound of the distribution range.
1723 { return this->a(); }
1726 * @brief Returns the inclusive upper bound of the distribution range.
1730 { return this->b(); }
1733 * @brief Returns the parameter set of the distribution.
1737 { return _M_param; }
1740 * @brief Sets the parameter set of the distribution.
1741 * @param __param The new parameter set of the distribution.
1744 param(const param_type& __param)
1745 { _M_param = __param; }
1747 template<typename _UniformRandomNumberGenerator>
1749 operator()(_UniformRandomNumberGenerator& __urng)
1750 { return this->operator()(__urng, this->param()); }
1752 template<typename _UniformRandomNumberGenerator>
1754 operator()(_UniformRandomNumberGenerator& __urng,
1755 const param_type& __p)
1757 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1759 return (__aurng() * (__p.b() - __p.a())) + __p.a();
1763 param_type _M_param;
1767 * @brief Inserts a %uniform_real_distribution random number
1768 * distribution @p __x into the output stream @p __os.
1770 * @param __os An output stream.
1771 * @param __x A %uniform_real_distribution random number distribution.
1773 * @returns The output stream with the state of @p __x inserted or in
1776 template<typename _RealType, typename _CharT, typename _Traits>
1777 std::basic_ostream<_CharT, _Traits>&
1778 operator<<(std::basic_ostream<_CharT, _Traits>&,
1779 const std::uniform_real_distribution<_RealType>&);
1782 * @brief Extracts a %uniform_real_distribution random number distribution
1783 * @p __x from the input stream @p __is.
1785 * @param __is An input stream.
1786 * @param __x A %uniform_real_distribution random number generator engine.
1788 * @returns The input stream with @p __x extracted or in an error state.
1790 template<typename _RealType, typename _CharT, typename _Traits>
1791 std::basic_istream<_CharT, _Traits>&
1792 operator>>(std::basic_istream<_CharT, _Traits>&,
1793 std::uniform_real_distribution<_RealType>&);
1795 /* @} */ // group random_distributions_uniform
1798 * @addtogroup random_distributions_normal Normal Distributions
1799 * @ingroup random_distributions
1804 * @brief A normal continuous distribution for random numbers.
1806 * The formula for the normal probability density function is
1808 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1809 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1812 template<typename _RealType = double>
1813 class normal_distribution
1815 static_assert(std::is_floating_point<_RealType>::value,
1816 "template argument not a floating point type");
1819 /** The type of the range of the distribution. */
1820 typedef _RealType result_type;
1821 /** Parameter type. */
1824 typedef normal_distribution<_RealType> distribution_type;
1827 param_type(_RealType __mean = _RealType(0),
1828 _RealType __stddev = _RealType(1))
1829 : _M_mean(__mean), _M_stddev(__stddev)
1831 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
1840 { return _M_stddev; }
1844 _RealType _M_stddev;
1849 * Constructs a normal distribution with parameters @f$mean@f$ and
1850 * standard deviation.
1853 normal_distribution(result_type __mean = result_type(0),
1854 result_type __stddev = result_type(1))
1855 : _M_param(__mean, __stddev), _M_saved_available(false)
1859 normal_distribution(const param_type& __p)
1860 : _M_param(__p), _M_saved_available(false)
1864 * @brief Resets the distribution state.
1868 { _M_saved_available = false; }
1871 * @brief Returns the mean of the distribution.
1875 { return _M_param.mean(); }
1878 * @brief Returns the standard deviation of the distribution.
1882 { return _M_param.stddev(); }
1885 * @brief Returns the parameter set of the distribution.
1889 { return _M_param; }
1892 * @brief Sets the parameter set of the distribution.
1893 * @param __param The new parameter set of the distribution.
1896 param(const param_type& __param)
1897 { _M_param = __param; }
1900 * @brief Returns the greatest lower bound value of the distribution.
1904 { return std::numeric_limits<result_type>::min(); }
1907 * @brief Returns the least upper bound value of the distribution.
1911 { return std::numeric_limits<result_type>::max(); }
1913 template<typename _UniformRandomNumberGenerator>
1915 operator()(_UniformRandomNumberGenerator& __urng)
1916 { return this->operator()(__urng, this->param()); }
1918 template<typename _UniformRandomNumberGenerator>
1920 operator()(_UniformRandomNumberGenerator& __urng,
1921 const param_type& __p);
1924 * @brief Inserts a %normal_distribution random number distribution
1925 * @p __x into the output stream @p __os.
1927 * @param __os An output stream.
1928 * @param __x A %normal_distribution random number distribution.
1930 * @returns The output stream with the state of @p __x inserted or in
1933 template<typename _RealType1, typename _CharT, typename _Traits>
1934 friend std::basic_ostream<_CharT, _Traits>&
1935 operator<<(std::basic_ostream<_CharT, _Traits>&,
1936 const std::normal_distribution<_RealType1>&);
1939 * @brief Extracts a %normal_distribution random number distribution
1940 * @p __x from the input stream @p __is.
1942 * @param __is An input stream.
1943 * @param __x A %normal_distribution random number generator engine.
1945 * @returns The input stream with @p __x extracted or in an error
1948 template<typename _RealType1, typename _CharT, typename _Traits>
1949 friend std::basic_istream<_CharT, _Traits>&
1950 operator>>(std::basic_istream<_CharT, _Traits>&,
1951 std::normal_distribution<_RealType1>&);
1954 param_type _M_param;
1955 result_type _M_saved;
1956 bool _M_saved_available;
1961 * @brief A lognormal_distribution random number distribution.
1963 * The formula for the normal probability mass function is
1965 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
1966 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
1969 template<typename _RealType = double>
1970 class lognormal_distribution
1972 static_assert(std::is_floating_point<_RealType>::value,
1973 "template argument not a floating point type");
1976 /** The type of the range of the distribution. */
1977 typedef _RealType result_type;
1978 /** Parameter type. */
1981 typedef lognormal_distribution<_RealType> distribution_type;
1984 param_type(_RealType __m = _RealType(0),
1985 _RealType __s = _RealType(1))
1986 : _M_m(__m), _M_s(__s)
2003 lognormal_distribution(_RealType __m = _RealType(0),
2004 _RealType __s = _RealType(1))
2005 : _M_param(__m, __s), _M_nd()
2009 lognormal_distribution(const param_type& __p)
2010 : _M_param(__p), _M_nd()
2014 * Resets the distribution state.
2025 { return _M_param.m(); }
2029 { return _M_param.s(); }
2032 * @brief Returns the parameter set of the distribution.
2036 { return _M_param; }
2039 * @brief Sets the parameter set of the distribution.
2040 * @param __param The new parameter set of the distribution.
2043 param(const param_type& __param)
2044 { _M_param = __param; }
2047 * @brief Returns the greatest lower bound value of the distribution.
2051 { return result_type(0); }
2054 * @brief Returns the least upper bound value of the distribution.
2058 { return std::numeric_limits<result_type>::max(); }
2060 template<typename _UniformRandomNumberGenerator>
2062 operator()(_UniformRandomNumberGenerator& __urng)
2063 { return this->operator()(__urng, this->param()); }
2065 template<typename _UniformRandomNumberGenerator>
2067 operator()(_UniformRandomNumberGenerator& __urng,
2068 const param_type& __p)
2069 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
2072 * @brief Inserts a %lognormal_distribution random number distribution
2073 * @p __x into the output stream @p __os.
2075 * @param __os An output stream.
2076 * @param __x A %lognormal_distribution random number distribution.
2078 * @returns The output stream with the state of @p __x inserted or in
2081 template<typename _RealType1, typename _CharT, typename _Traits>
2082 friend std::basic_ostream<_CharT, _Traits>&
2083 operator<<(std::basic_ostream<_CharT, _Traits>&,
2084 const std::lognormal_distribution<_RealType1>&);
2087 * @brief Extracts a %lognormal_distribution random number distribution
2088 * @p __x from the input stream @p __is.
2090 * @param __is An input stream.
2091 * @param __x A %lognormal_distribution random number
2094 * @returns The input stream with @p __x extracted or in an error state.
2096 template<typename _RealType1, typename _CharT, typename _Traits>
2097 friend std::basic_istream<_CharT, _Traits>&
2098 operator>>(std::basic_istream<_CharT, _Traits>&,
2099 std::lognormal_distribution<_RealType1>&);
2102 param_type _M_param;
2104 std::normal_distribution<result_type> _M_nd;
2109 * @brief A gamma continuous distribution for random numbers.
2111 * The formula for the gamma probability density function is:
2113 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2114 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2117 template<typename _RealType = double>
2118 class gamma_distribution
2120 static_assert(std::is_floating_point<_RealType>::value,
2121 "template argument not a floating point type");
2124 /** The type of the range of the distribution. */
2125 typedef _RealType result_type;
2126 /** Parameter type. */
2129 typedef gamma_distribution<_RealType> distribution_type;
2130 friend class gamma_distribution<_RealType>;
2133 param_type(_RealType __alpha_val = _RealType(1),
2134 _RealType __beta_val = _RealType(1))
2135 : _M_alpha(__alpha_val), _M_beta(__beta_val)
2137 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
2143 { return _M_alpha; }
2156 _RealType _M_malpha, _M_a2;
2161 * @brief Constructs a gamma distribution with parameters
2162 * @f$\alpha@f$ and @f$\beta@f$.
2165 gamma_distribution(_RealType __alpha_val = _RealType(1),
2166 _RealType __beta_val = _RealType(1))
2167 : _M_param(__alpha_val, __beta_val), _M_nd()
2171 gamma_distribution(const param_type& __p)
2172 : _M_param(__p), _M_nd()
2176 * @brief Resets the distribution state.
2183 * @brief Returns the @f$\alpha@f$ of the distribution.
2187 { return _M_param.alpha(); }
2190 * @brief Returns the @f$\beta@f$ of the distribution.
2194 { return _M_param.beta(); }
2197 * @brief Returns the parameter set of the distribution.
2201 { return _M_param; }
2204 * @brief Sets the parameter set of the distribution.
2205 * @param __param The new parameter set of the distribution.
2208 param(const param_type& __param)
2209 { _M_param = __param; }
2212 * @brief Returns the greatest lower bound value of the distribution.
2216 { return result_type(0); }
2219 * @brief Returns the least upper bound value of the distribution.
2223 { return std::numeric_limits<result_type>::max(); }
2225 template<typename _UniformRandomNumberGenerator>
2227 operator()(_UniformRandomNumberGenerator& __urng)
2228 { return this->operator()(__urng, this->param()); }
2230 template<typename _UniformRandomNumberGenerator>
2232 operator()(_UniformRandomNumberGenerator& __urng,
2233 const param_type& __p);
2236 * @brief Inserts a %gamma_distribution random number distribution
2237 * @p __x into the output stream @p __os.
2239 * @param __os An output stream.
2240 * @param __x A %gamma_distribution random number distribution.
2242 * @returns The output stream with the state of @p __x inserted or in
2245 template<typename _RealType1, typename _CharT, typename _Traits>
2246 friend std::basic_ostream<_CharT, _Traits>&
2247 operator<<(std::basic_ostream<_CharT, _Traits>&,
2248 const std::gamma_distribution<_RealType1>&);
2251 * @brief Extracts a %gamma_distribution random number distribution
2252 * @p __x from the input stream @p __is.
2254 * @param __is An input stream.
2255 * @param __x A %gamma_distribution random number generator engine.
2257 * @returns The input stream with @p __x extracted or in an error state.
2259 template<typename _RealType1, typename _CharT, typename _Traits>
2260 friend std::basic_istream<_CharT, _Traits>&
2261 operator>>(std::basic_istream<_CharT, _Traits>&,
2262 std::gamma_distribution<_RealType1>&);
2265 param_type _M_param;
2267 std::normal_distribution<result_type> _M_nd;
2272 * @brief A chi_squared_distribution random number distribution.
2274 * The formula for the normal probability mass function is
2275 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2277 template<typename _RealType = double>
2278 class chi_squared_distribution
2280 static_assert(std::is_floating_point<_RealType>::value,
2281 "template argument not a floating point type");
2284 /** The type of the range of the distribution. */
2285 typedef _RealType result_type;
2286 /** Parameter type. */
2289 typedef chi_squared_distribution<_RealType> distribution_type;
2292 param_type(_RealType __n = _RealType(1))
2305 chi_squared_distribution(_RealType __n = _RealType(1))
2306 : _M_param(__n), _M_gd(__n / 2)
2310 chi_squared_distribution(const param_type& __p)
2311 : _M_param(__p), _M_gd(__p.n() / 2)
2315 * @brief Resets the distribution state.
2326 { return _M_param.n(); }
2329 * @brief Returns the parameter set of the distribution.
2333 { return _M_param; }
2336 * @brief Sets the parameter set of the distribution.
2337 * @param __param The new parameter set of the distribution.
2340 param(const param_type& __param)
2341 { _M_param = __param; }
2344 * @brief Returns the greatest lower bound value of the distribution.
2348 { return result_type(0); }
2351 * @brief Returns the least upper bound value of the distribution.
2355 { return std::numeric_limits<result_type>::max(); }
2357 template<typename _UniformRandomNumberGenerator>
2359 operator()(_UniformRandomNumberGenerator& __urng)
2360 { return 2 * _M_gd(__urng); }
2362 template<typename _UniformRandomNumberGenerator>
2364 operator()(_UniformRandomNumberGenerator& __urng,
2365 const param_type& __p)
2367 typedef typename std::gamma_distribution<result_type>::param_type
2369 return 2 * _M_gd(__urng, param_type(__p.n() / 2));
2373 * @brief Inserts a %chi_squared_distribution random number distribution
2374 * @p __x into the output stream @p __os.
2376 * @param __os An output stream.
2377 * @param __x A %chi_squared_distribution random number distribution.
2379 * @returns The output stream with the state of @p __x inserted or in
2382 template<typename _RealType1, typename _CharT, typename _Traits>
2383 friend std::basic_ostream<_CharT, _Traits>&
2384 operator<<(std::basic_ostream<_CharT, _Traits>&,
2385 const std::chi_squared_distribution<_RealType1>&);
2388 * @brief Extracts a %chi_squared_distribution random number distribution
2389 * @p __x from the input stream @p __is.
2391 * @param __is An input stream.
2392 * @param __x A %chi_squared_distribution random number
2395 * @returns The input stream with @p __x extracted or in an error state.
2397 template<typename _RealType1, typename _CharT, typename _Traits>
2398 friend std::basic_istream<_CharT, _Traits>&
2399 operator>>(std::basic_istream<_CharT, _Traits>&,
2400 std::chi_squared_distribution<_RealType1>&);
2403 param_type _M_param;
2405 std::gamma_distribution<result_type> _M_gd;
2410 * @brief A cauchy_distribution random number distribution.
2412 * The formula for the normal probability mass function is
2413 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2415 template<typename _RealType = double>
2416 class cauchy_distribution
2418 static_assert(std::is_floating_point<_RealType>::value,
2419 "template argument not a floating point type");
2422 /** The type of the range of the distribution. */
2423 typedef _RealType result_type;
2424 /** Parameter type. */
2427 typedef cauchy_distribution<_RealType> distribution_type;
2430 param_type(_RealType __a = _RealType(0),
2431 _RealType __b = _RealType(1))
2432 : _M_a(__a), _M_b(__b)
2449 cauchy_distribution(_RealType __a = _RealType(0),
2450 _RealType __b = _RealType(1))
2451 : _M_param(__a, __b)
2455 cauchy_distribution(const param_type& __p)
2460 * @brief Resets the distribution state.
2471 { return _M_param.a(); }
2475 { return _M_param.b(); }
2478 * @brief Returns the parameter set of the distribution.
2482 { return _M_param; }
2485 * @brief Sets the parameter set of the distribution.
2486 * @param __param The new parameter set of the distribution.
2489 param(const param_type& __param)
2490 { _M_param = __param; }
2493 * @brief Returns the greatest lower bound value of the distribution.
2497 { return std::numeric_limits<result_type>::min(); }
2500 * @brief Returns the least upper bound value of the distribution.
2504 { return std::numeric_limits<result_type>::max(); }
2506 template<typename _UniformRandomNumberGenerator>
2508 operator()(_UniformRandomNumberGenerator& __urng)
2509 { return this->operator()(__urng, this->param()); }
2511 template<typename _UniformRandomNumberGenerator>
2513 operator()(_UniformRandomNumberGenerator& __urng,
2514 const param_type& __p);
2517 param_type _M_param;
2521 * @brief Inserts a %cauchy_distribution random number distribution
2522 * @p __x into the output stream @p __os.
2524 * @param __os An output stream.
2525 * @param __x A %cauchy_distribution random number distribution.
2527 * @returns The output stream with the state of @p __x inserted or in
2530 template<typename _RealType, typename _CharT, typename _Traits>
2531 std::basic_ostream<_CharT, _Traits>&
2532 operator<<(std::basic_ostream<_CharT, _Traits>&,
2533 const std::cauchy_distribution<_RealType>&);
2536 * @brief Extracts a %cauchy_distribution random number distribution
2537 * @p __x from the input stream @p __is.
2539 * @param __is An input stream.
2540 * @param __x A %cauchy_distribution random number
2543 * @returns The input stream with @p __x extracted or in an error state.
2545 template<typename _RealType, typename _CharT, typename _Traits>
2546 std::basic_istream<_CharT, _Traits>&
2547 operator>>(std::basic_istream<_CharT, _Traits>&,
2548 std::cauchy_distribution<_RealType>&);
2552 * @brief A fisher_f_distribution random number distribution.
2554 * The formula for the normal probability mass function is
2556 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2557 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2558 * (1 + \frac{mx}{n})^{-(m+n)/2}
2561 template<typename _RealType = double>
2562 class fisher_f_distribution
2564 static_assert(std::is_floating_point<_RealType>::value,
2565 "template argument not a floating point type");
2568 /** The type of the range of the distribution. */
2569 typedef _RealType result_type;
2570 /** Parameter type. */
2573 typedef fisher_f_distribution<_RealType> distribution_type;
2576 param_type(_RealType __m = _RealType(1),
2577 _RealType __n = _RealType(1))
2578 : _M_m(__m), _M_n(__n)
2595 fisher_f_distribution(_RealType __m = _RealType(1),
2596 _RealType __n = _RealType(1))
2597 : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2)
2601 fisher_f_distribution(const param_type& __p)
2602 : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2)
2606 * @brief Resets the distribution state.
2620 { return _M_param.m(); }
2624 { return _M_param.n(); }
2627 * @brief Returns the parameter set of the distribution.
2631 { return _M_param; }
2634 * @brief Sets the parameter set of the distribution.
2635 * @param __param The new parameter set of the distribution.
2638 param(const param_type& __param)
2639 { _M_param = __param; }
2642 * @brief Returns the greatest lower bound value of the distribution.
2646 { return result_type(0); }
2649 * @brief Returns the least upper bound value of the distribution.
2653 { return std::numeric_limits<result_type>::max(); }
2655 template<typename _UniformRandomNumberGenerator>
2657 operator()(_UniformRandomNumberGenerator& __urng)
2658 { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); }
2660 template<typename _UniformRandomNumberGenerator>
2662 operator()(_UniformRandomNumberGenerator& __urng,
2663 const param_type& __p)
2665 typedef typename std::gamma_distribution<result_type>::param_type
2667 return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n())
2668 / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m()));
2672 * @brief Inserts a %fisher_f_distribution random number distribution
2673 * @p __x into the output stream @p __os.
2675 * @param __os An output stream.
2676 * @param __x A %fisher_f_distribution random number distribution.
2678 * @returns The output stream with the state of @p __x inserted or in
2681 template<typename _RealType1, typename _CharT, typename _Traits>
2682 friend std::basic_ostream<_CharT, _Traits>&
2683 operator<<(std::basic_ostream<_CharT, _Traits>&,
2684 const std::fisher_f_distribution<_RealType1>&);
2687 * @brief Extracts a %fisher_f_distribution random number distribution
2688 * @p __x from the input stream @p __is.
2690 * @param __is An input stream.
2691 * @param __x A %fisher_f_distribution random number
2694 * @returns The input stream with @p __x extracted or in an error state.
2696 template<typename _RealType1, typename _CharT, typename _Traits>
2697 friend std::basic_istream<_CharT, _Traits>&
2698 operator>>(std::basic_istream<_CharT, _Traits>&,
2699 std::fisher_f_distribution<_RealType1>&);
2702 param_type _M_param;
2704 std::gamma_distribution<result_type> _M_gd_x, _M_gd_y;
2709 * @brief A student_t_distribution random number distribution.
2711 * The formula for the normal probability mass function is:
2713 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2714 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
2717 template<typename _RealType = double>
2718 class student_t_distribution
2720 static_assert(std::is_floating_point<_RealType>::value,
2721 "template argument not a floating point type");
2724 /** The type of the range of the distribution. */
2725 typedef _RealType result_type;
2726 /** Parameter type. */
2729 typedef student_t_distribution<_RealType> distribution_type;
2732 param_type(_RealType __n = _RealType(1))
2745 student_t_distribution(_RealType __n = _RealType(1))
2746 : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2)
2750 student_t_distribution(const param_type& __p)
2751 : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2)
2755 * @brief Resets the distribution state.
2769 { return _M_param.n(); }
2772 * @brief Returns the parameter set of the distribution.
2776 { return _M_param; }
2779 * @brief Sets the parameter set of the distribution.
2780 * @param __param The new parameter set of the distribution.
2783 param(const param_type& __param)
2784 { _M_param = __param; }
2787 * @brief Returns the greatest lower bound value of the distribution.
2791 { return std::numeric_limits<result_type>::min(); }
2794 * @brief Returns the least upper bound value of the distribution.
2798 { return std::numeric_limits<result_type>::max(); }
2800 template<typename _UniformRandomNumberGenerator>
2802 operator()(_UniformRandomNumberGenerator& __urng)
2803 { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); }
2805 template<typename _UniformRandomNumberGenerator>
2807 operator()(_UniformRandomNumberGenerator& __urng,
2808 const param_type& __p)
2810 typedef typename std::gamma_distribution<result_type>::param_type
2813 const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2));
2814 return _M_nd(__urng) * std::sqrt(__p.n() / __g);
2818 * @brief Inserts a %student_t_distribution random number distribution
2819 * @p __x into the output stream @p __os.
2821 * @param __os An output stream.
2822 * @param __x A %student_t_distribution random number distribution.
2824 * @returns The output stream with the state of @p __x inserted or in
2827 template<typename _RealType1, typename _CharT, typename _Traits>
2828 friend std::basic_ostream<_CharT, _Traits>&
2829 operator<<(std::basic_ostream<_CharT, _Traits>&,
2830 const std::student_t_distribution<_RealType1>&);
2833 * @brief Extracts a %student_t_distribution random number distribution
2834 * @p __x from the input stream @p __is.
2836 * @param __is An input stream.
2837 * @param __x A %student_t_distribution random number
2840 * @returns The input stream with @p __x extracted or in an error state.
2842 template<typename _RealType1, typename _CharT, typename _Traits>
2843 friend std::basic_istream<_CharT, _Traits>&
2844 operator>>(std::basic_istream<_CharT, _Traits>&,
2845 std::student_t_distribution<_RealType1>&);
2848 param_type _M_param;
2850 std::normal_distribution<result_type> _M_nd;
2851 std::gamma_distribution<result_type> _M_gd;
2854 /* @} */ // group random_distributions_normal
2857 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
2858 * @ingroup random_distributions
2863 * @brief A Bernoulli random number distribution.
2865 * Generates a sequence of true and false values with likelihood @f$p@f$
2866 * that true will come up and @f$(1 - p)@f$ that false will appear.
2868 class bernoulli_distribution
2871 /** The type of the range of the distribution. */
2872 typedef bool result_type;
2873 /** Parameter type. */
2876 typedef bernoulli_distribution distribution_type;
2879 param_type(double __p = 0.5)
2882 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
2895 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2897 * @param __p [IN] The likelihood of a true result being returned.
2898 * Must be in the interval @f$[0, 1]@f$.
2901 bernoulli_distribution(double __p = 0.5)
2906 bernoulli_distribution(const param_type& __p)
2911 * @brief Resets the distribution state.
2913 * Does nothing for a Bernoulli distribution.
2919 * @brief Returns the @p p parameter of the distribution.
2923 { return _M_param.p(); }
2926 * @brief Returns the parameter set of the distribution.
2930 { return _M_param; }
2933 * @brief Sets the parameter set of the distribution.
2934 * @param __param The new parameter set of the distribution.
2937 param(const param_type& __param)
2938 { _M_param = __param; }
2941 * @brief Returns the greatest lower bound value of the distribution.
2945 { return std::numeric_limits<result_type>::min(); }
2948 * @brief Returns the least upper bound value of the distribution.
2952 { return std::numeric_limits<result_type>::max(); }
2955 * @brief Returns the next value in the Bernoullian sequence.
2957 template<typename _UniformRandomNumberGenerator>
2959 operator()(_UniformRandomNumberGenerator& __urng)
2960 { return this->operator()(__urng, this->param()); }
2962 template<typename _UniformRandomNumberGenerator>
2964 operator()(_UniformRandomNumberGenerator& __urng,
2965 const param_type& __p)
2967 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2969 if ((__aurng() - __aurng.min())
2970 < __p.p() * (__aurng.max() - __aurng.min()))
2976 param_type _M_param;
2980 * @brief Inserts a %bernoulli_distribution random number distribution
2981 * @p __x into the output stream @p __os.
2983 * @param __os An output stream.
2984 * @param __x A %bernoulli_distribution random number distribution.
2986 * @returns The output stream with the state of @p __x inserted or in
2989 template<typename _CharT, typename _Traits>
2990 std::basic_ostream<_CharT, _Traits>&
2991 operator<<(std::basic_ostream<_CharT, _Traits>&,
2992 const std::bernoulli_distribution&);
2995 * @brief Extracts a %bernoulli_distribution random number distribution
2996 * @p __x from the input stream @p __is.
2998 * @param __is An input stream.
2999 * @param __x A %bernoulli_distribution random number generator engine.
3001 * @returns The input stream with @p __x extracted or in an error state.
3003 template<typename _CharT, typename _Traits>
3004 std::basic_istream<_CharT, _Traits>&
3005 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3006 std::bernoulli_distribution& __x)
3010 __x.param(bernoulli_distribution::param_type(__p));
3016 * @brief A discrete binomial random number distribution.
3018 * The formula for the binomial probability density function is
3019 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3020 * and @f$p@f$ are the parameters of the distribution.
3022 template<typename _IntType = int>
3023 class binomial_distribution
3025 static_assert(std::is_integral<_IntType>::value,
3026 "template argument not an integral type");
3029 /** The type of the range of the distribution. */
3030 typedef _IntType result_type;
3031 /** Parameter type. */
3034 typedef binomial_distribution<_IntType> distribution_type;
3035 friend class binomial_distribution<_IntType>;
3038 param_type(_IntType __t = _IntType(1), double __p = 0.5)
3039 : _M_t(__t), _M_p(__p)
3041 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3063 #if _GLIBCXX_USE_C99_MATH_TR1
3064 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3065 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3070 // constructors and member function
3072 binomial_distribution(_IntType __t = _IntType(1),
3074 : _M_param(__t, __p), _M_nd()
3078 binomial_distribution(const param_type& __p)
3079 : _M_param(__p), _M_nd()
3083 * @brief Resets the distribution state.
3090 * @brief Returns the distribution @p t parameter.
3094 { return _M_param.t(); }
3097 * @brief Returns the distribution @p p parameter.
3101 { return _M_param.p(); }
3104 * @brief Returns the parameter set of the distribution.
3108 { return _M_param; }
3111 * @brief Sets the parameter set of the distribution.
3112 * @param __param The new parameter set of the distribution.
3115 param(const param_type& __param)
3116 { _M_param = __param; }
3119 * @brief Returns the greatest lower bound value of the distribution.
3126 * @brief Returns the least upper bound value of the distribution.
3130 { return _M_param.t(); }
3132 template<typename _UniformRandomNumberGenerator>
3134 operator()(_UniformRandomNumberGenerator& __urng)
3135 { return this->operator()(__urng, this->param()); }
3137 template<typename _UniformRandomNumberGenerator>
3139 operator()(_UniformRandomNumberGenerator& __urng,
3140 const param_type& __p);
3143 * @brief Inserts a %binomial_distribution random number distribution
3144 * @p __x into the output stream @p __os.
3146 * @param __os An output stream.
3147 * @param __x A %binomial_distribution random number distribution.
3149 * @returns The output stream with the state of @p __x inserted or in
3152 template<typename _IntType1,
3153 typename _CharT, typename _Traits>
3154 friend std::basic_ostream<_CharT, _Traits>&
3155 operator<<(std::basic_ostream<_CharT, _Traits>&,
3156 const std::binomial_distribution<_IntType1>&);
3159 * @brief Extracts a %binomial_distribution random number distribution
3160 * @p __x from the input stream @p __is.
3162 * @param __is An input stream.
3163 * @param __x A %binomial_distribution random number generator engine.
3165 * @returns The input stream with @p __x extracted or in an error
3168 template<typename _IntType1,
3169 typename _CharT, typename _Traits>
3170 friend std::basic_istream<_CharT, _Traits>&
3171 operator>>(std::basic_istream<_CharT, _Traits>&,
3172 std::binomial_distribution<_IntType1>&);
3175 template<typename _UniformRandomNumberGenerator>
3177 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3179 param_type _M_param;
3181 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3182 std::normal_distribution<double> _M_nd;
3187 * @brief A discrete geometric random number distribution.
3189 * The formula for the geometric probability density function is
3190 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the
3193 template<typename _IntType = int>
3194 class geometric_distribution
3196 static_assert(std::is_integral<_IntType>::value,
3197 "template argument not an integral type");
3200 /** The type of the range of the distribution. */
3201 typedef _IntType result_type;
3202 /** Parameter type. */
3205 typedef geometric_distribution<_IntType> distribution_type;
3206 friend class geometric_distribution<_IntType>;
3209 param_type(double __p = 0.5)
3212 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0)
3224 { _M_log_p = std::log(_M_p); }
3231 // constructors and member function
3233 geometric_distribution(double __p = 0.5)
3238 geometric_distribution(const param_type& __p)
3243 * @brief Resets the distribution state.
3245 * Does nothing for the geometric distribution.
3251 * @brief Returns the distribution parameter @p p.
3255 { return _M_param.p(); }
3258 * @brief Returns the parameter set of the distribution.
3262 { return _M_param; }
3265 * @brief Sets the parameter set of the distribution.
3266 * @param __param The new parameter set of the distribution.
3269 param(const param_type& __param)
3270 { _M_param = __param; }
3273 * @brief Returns the greatest lower bound value of the distribution.
3280 * @brief Returns the least upper bound value of the distribution.
3284 { return std::numeric_limits<result_type>::max(); }
3286 template<typename _UniformRandomNumberGenerator>
3288 operator()(_UniformRandomNumberGenerator& __urng)
3289 { return this->operator()(__urng, this->param()); }
3291 template<typename _UniformRandomNumberGenerator>
3293 operator()(_UniformRandomNumberGenerator& __urng,
3294 const param_type& __p);
3297 param_type _M_param;
3301 * @brief Inserts a %geometric_distribution random number distribution
3302 * @p __x into the output stream @p __os.
3304 * @param __os An output stream.
3305 * @param __x A %geometric_distribution random number distribution.
3307 * @returns The output stream with the state of @p __x inserted or in
3310 template<typename _IntType,
3311 typename _CharT, typename _Traits>
3312 std::basic_ostream<_CharT, _Traits>&
3313 operator<<(std::basic_ostream<_CharT, _Traits>&,
3314 const std::geometric_distribution<_IntType>&);
3317 * @brief Extracts a %geometric_distribution random number distribution
3318 * @p __x from the input stream @p __is.
3320 * @param __is An input stream.
3321 * @param __x A %geometric_distribution random number generator engine.
3323 * @returns The input stream with @p __x extracted or in an error state.
3325 template<typename _IntType,
3326 typename _CharT, typename _Traits>
3327 std::basic_istream<_CharT, _Traits>&
3328 operator>>(std::basic_istream<_CharT, _Traits>&,
3329 std::geometric_distribution<_IntType>&);
3333 * @brief A negative_binomial_distribution random number distribution.
3335 * The formula for the negative binomial probability mass function is
3336 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3337 * and @f$p@f$ are the parameters of the distribution.
3339 template<typename _IntType = int>
3340 class negative_binomial_distribution
3342 static_assert(std::is_integral<_IntType>::value,
3343 "template argument not an integral type");
3346 /** The type of the range of the distribution. */
3347 typedef _IntType result_type;
3348 /** Parameter type. */
3351 typedef negative_binomial_distribution<_IntType> distribution_type;
3354 param_type(_IntType __k = 1, double __p = 0.5)
3355 : _M_k(__k), _M_p(__p)
3372 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
3373 : _M_param(__k, __p), _M_gd(__k, __p / (1.0 - __p))
3377 negative_binomial_distribution(const param_type& __p)
3378 : _M_param(__p), _M_gd(__p.k(), __p.p() / (1.0 - __p.p()))
3382 * @brief Resets the distribution state.
3389 * @brief Return the @f$k@f$ parameter of the distribution.
3393 { return _M_param.k(); }
3396 * @brief Return the @f$p@f$ parameter of the distribution.
3400 { return _M_param.p(); }
3403 * @brief Returns the parameter set of the distribution.
3407 { return _M_param; }
3410 * @brief Sets the parameter set of the distribution.
3411 * @param __param The new parameter set of the distribution.
3414 param(const param_type& __param)
3415 { _M_param = __param; }
3418 * @brief Returns the greatest lower bound value of the distribution.
3422 { return result_type(0); }
3425 * @brief Returns the least upper bound value of the distribution.
3429 { return std::numeric_limits<result_type>::max(); }
3431 template<typename _UniformRandomNumberGenerator>
3433 operator()(_UniformRandomNumberGenerator& __urng);
3435 template<typename _UniformRandomNumberGenerator>
3437 operator()(_UniformRandomNumberGenerator& __urng,
3438 const param_type& __p);
3441 * @brief Inserts a %negative_binomial_distribution random
3442 * number distribution @p __x into the output stream @p __os.
3444 * @param __os An output stream.
3445 * @param __x A %negative_binomial_distribution random number
3448 * @returns The output stream with the state of @p __x inserted or in
3451 template<typename _IntType1, typename _CharT, typename _Traits>
3452 friend std::basic_ostream<_CharT, _Traits>&
3453 operator<<(std::basic_ostream<_CharT, _Traits>&,
3454 const std::negative_binomial_distribution<_IntType1>&);
3457 * @brief Extracts a %negative_binomial_distribution random number
3458 * distribution @p __x from the input stream @p __is.
3460 * @param __is An input stream.
3461 * @param __x A %negative_binomial_distribution random number
3464 * @returns The input stream with @p __x extracted or in an error state.
3466 template<typename _IntType1, typename _CharT, typename _Traits>
3467 friend std::basic_istream<_CharT, _Traits>&
3468 operator>>(std::basic_istream<_CharT, _Traits>&,
3469 std::negative_binomial_distribution<_IntType1>&);
3472 param_type _M_param;
3474 std::gamma_distribution<double> _M_gd;
3477 /* @} */ // group random_distributions_bernoulli
3480 * @addtogroup random_distributions_poisson Poisson Distributions
3481 * @ingroup random_distributions
3486 * @brief A discrete Poisson random number distribution.
3488 * The formula for the Poisson probability density function is
3489 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3490 * parameter of the distribution.
3492 template<typename _IntType = int>
3493 class poisson_distribution
3495 static_assert(std::is_integral<_IntType>::value,
3496 "template argument not an integral type");
3499 /** The type of the range of the distribution. */
3500 typedef _IntType result_type;
3501 /** Parameter type. */
3504 typedef poisson_distribution<_IntType> distribution_type;
3505 friend class poisson_distribution<_IntType>;
3508 param_type(double __mean = 1.0)
3511 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
3520 // Hosts either log(mean) or the threshold of the simple method.
3527 #if _GLIBCXX_USE_C99_MATH_TR1
3528 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
3532 // constructors and member function
3534 poisson_distribution(double __mean = 1.0)
3535 : _M_param(__mean), _M_nd()
3539 poisson_distribution(const param_type& __p)
3540 : _M_param(__p), _M_nd()
3544 * @brief Resets the distribution state.
3551 * @brief Returns the distribution parameter @p mean.
3555 { return _M_param.mean(); }
3558 * @brief Returns the parameter set of the distribution.
3562 { return _M_param; }
3565 * @brief Sets the parameter set of the distribution.
3566 * @param __param The new parameter set of the distribution.
3569 param(const param_type& __param)
3570 { _M_param = __param; }
3573 * @brief Returns the greatest lower bound value of the distribution.
3580 * @brief Returns the least upper bound value of the distribution.
3584 { return std::numeric_limits<result_type>::max(); }
3586 template<typename _UniformRandomNumberGenerator>
3588 operator()(_UniformRandomNumberGenerator& __urng)
3589 { return this->operator()(__urng, this->param()); }
3591 template<typename _UniformRandomNumberGenerator>
3593 operator()(_UniformRandomNumberGenerator& __urng,
3594 const param_type& __p);
3597 * @brief Inserts a %poisson_distribution random number distribution
3598 * @p __x into the output stream @p __os.
3600 * @param __os An output stream.
3601 * @param __x A %poisson_distribution random number distribution.
3603 * @returns The output stream with the state of @p __x inserted or in
3606 template<typename _IntType1, typename _CharT, typename _Traits>
3607 friend std::basic_ostream<_CharT, _Traits>&
3608 operator<<(std::basic_ostream<_CharT, _Traits>&,
3609 const std::poisson_distribution<_IntType1>&);
3612 * @brief Extracts a %poisson_distribution random number distribution
3613 * @p __x from the input stream @p __is.
3615 * @param __is An input stream.
3616 * @param __x A %poisson_distribution random number generator engine.
3618 * @returns The input stream with @p __x extracted or in an error
3621 template<typename _IntType1, typename _CharT, typename _Traits>
3622 friend std::basic_istream<_CharT, _Traits>&
3623 operator>>(std::basic_istream<_CharT, _Traits>&,
3624 std::poisson_distribution<_IntType1>&);
3627 param_type _M_param;
3629 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3630 std::normal_distribution<double> _M_nd;
3634 * @brief An exponential continuous distribution for random numbers.
3636 * The formula for the exponential probability density function is
3637 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
3639 * <table border=1 cellpadding=10 cellspacing=0>
3640 * <caption align=top>Distribution Statistics</caption>
3641 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
3642 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
3643 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
3644 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3645 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
3648 template<typename _RealType = double>
3649 class exponential_distribution
3651 static_assert(std::is_floating_point<_RealType>::value,
3652 "template argument not a floating point type");
3655 /** The type of the range of the distribution. */
3656 typedef _RealType result_type;
3657 /** Parameter type. */
3660 typedef exponential_distribution<_RealType> distribution_type;
3663 param_type(_RealType __lambda = _RealType(1))
3664 : _M_lambda(__lambda)
3666 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
3671 { return _M_lambda; }
3674 _RealType _M_lambda;
3679 * @brief Constructs an exponential distribution with inverse scale
3680 * parameter @f$\lambda@f$.
3683 exponential_distribution(const result_type& __lambda = result_type(1))
3684 : _M_param(__lambda)
3688 exponential_distribution(const param_type& __p)
3693 * @brief Resets the distribution state.
3695 * Has no effect on exponential distributions.
3701 * @brief Returns the inverse scale parameter of the distribution.
3705 { return _M_param.lambda(); }
3708 * @brief Returns the parameter set of the distribution.
3712 { return _M_param; }
3715 * @brief Sets the parameter set of the distribution.
3716 * @param __param The new parameter set of the distribution.
3719 param(const param_type& __param)
3720 { _M_param = __param; }
3723 * @brief Returns the greatest lower bound value of the distribution.
3727 { return result_type(0); }
3730 * @brief Returns the least upper bound value of the distribution.
3734 { return std::numeric_limits<result_type>::max(); }
3736 template<typename _UniformRandomNumberGenerator>
3738 operator()(_UniformRandomNumberGenerator& __urng)
3739 { return this->operator()(__urng, this->param()); }
3741 template<typename _UniformRandomNumberGenerator>
3743 operator()(_UniformRandomNumberGenerator& __urng,
3744 const param_type& __p)
3746 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3748 return -std::log(__aurng()) / __p.lambda();
3752 param_type _M_param;
3756 * @brief Inserts a %exponential_distribution random number distribution
3757 * @p __x into the output stream @p __os.
3759 * @param __os An output stream.
3760 * @param __x A %exponential_distribution random number distribution.
3762 * @returns The output stream with the state of @p __x inserted or in
3765 template<typename _RealType, typename _CharT, typename _Traits>
3766 std::basic_ostream<_CharT, _Traits>&
3767 operator<<(std::basic_ostream<_CharT, _Traits>&,
3768 const std::exponential_distribution<_RealType>&);
3771 * @brief Extracts a %exponential_distribution random number distribution
3772 * @p __x from the input stream @p __is.
3774 * @param __is An input stream.
3775 * @param __x A %exponential_distribution random number
3778 * @returns The input stream with @p __x extracted or in an error state.
3780 template<typename _RealType, typename _CharT, typename _Traits>
3781 std::basic_istream<_CharT, _Traits>&
3782 operator>>(std::basic_istream<_CharT, _Traits>&,
3783 std::exponential_distribution<_RealType>&);
3787 * @brief A weibull_distribution random number distribution.
3789 * The formula for the normal probability density function is:
3791 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
3792 * \exp{(-(\frac{x}{\beta})^\alpha)}
3795 template<typename _RealType = double>
3796 class weibull_distribution
3798 static_assert(std::is_floating_point<_RealType>::value,
3799 "template argument not a floating point type");
3802 /** The type of the range of the distribution. */
3803 typedef _RealType result_type;
3804 /** Parameter type. */
3807 typedef weibull_distribution<_RealType> distribution_type;
3810 param_type(_RealType __a = _RealType(1),
3811 _RealType __b = _RealType(1))
3812 : _M_a(__a), _M_b(__b)
3829 weibull_distribution(_RealType __a = _RealType(1),
3830 _RealType __b = _RealType(1))
3831 : _M_param(__a, __b)
3835 weibull_distribution(const param_type& __p)
3840 * @brief Resets the distribution state.
3847 * @brief Return the @f$a@f$ parameter of the distribution.
3851 { return _M_param.a(); }
3854 * @brief Return the @f$b@f$ parameter of the distribution.
3858 { return _M_param.b(); }
3861 * @brief Returns the parameter set of the distribution.
3865 { return _M_param; }
3868 * @brief Sets the parameter set of the distribution.
3869 * @param __param The new parameter set of the distribution.
3872 param(const param_type& __param)
3873 { _M_param = __param; }
3876 * @brief Returns the greatest lower bound value of the distribution.
3880 { return result_type(0); }
3883 * @brief Returns the least upper bound value of the distribution.
3887 { return std::numeric_limits<result_type>::max(); }
3889 template<typename _UniformRandomNumberGenerator>
3891 operator()(_UniformRandomNumberGenerator& __urng)
3892 { return this->operator()(__urng, this->param()); }
3894 template<typename _UniformRandomNumberGenerator>
3896 operator()(_UniformRandomNumberGenerator& __urng,
3897 const param_type& __p);
3900 param_type _M_param;
3904 * @brief Inserts a %weibull_distribution random number distribution
3905 * @p __x into the output stream @p __os.
3907 * @param __os An output stream.
3908 * @param __x A %weibull_distribution random number distribution.
3910 * @returns The output stream with the state of @p __x inserted or in
3913 template<typename _RealType, typename _CharT, typename _Traits>
3914 std::basic_ostream<_CharT, _Traits>&
3915 operator<<(std::basic_ostream<_CharT, _Traits>&,
3916 const std::weibull_distribution<_RealType>&);
3919 * @brief Extracts a %weibull_distribution random number distribution
3920 * @p __x from the input stream @p __is.
3922 * @param __is An input stream.
3923 * @param __x A %weibull_distribution random number
3926 * @returns The input stream with @p __x extracted or in an error state.
3928 template<typename _RealType, typename _CharT, typename _Traits>
3929 std::basic_istream<_CharT, _Traits>&
3930 operator>>(std::basic_istream<_CharT, _Traits>&,
3931 std::weibull_distribution<_RealType>&);
3935 * @brief A extreme_value_distribution random number distribution.
3937 * The formula for the normal probability mass function is
3939 * p(x|a,b) = \frac{1}{b}
3940 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
3943 template<typename _RealType = double>
3944 class extreme_value_distribution
3946 static_assert(std::is_floating_point<_RealType>::value,
3947 "template argument not a floating point type");
3950 /** The type of the range of the distribution. */
3951 typedef _RealType result_type;
3952 /** Parameter type. */
3955 typedef extreme_value_distribution<_RealType> distribution_type;
3958 param_type(_RealType __a = _RealType(0),
3959 _RealType __b = _RealType(1))
3960 : _M_a(__a), _M_b(__b)
3977 extreme_value_distribution(_RealType __a = _RealType(0),
3978 _RealType __b = _RealType(1))
3979 : _M_param(__a, __b)
3983 extreme_value_distribution(const param_type& __p)
3988 * @brief Resets the distribution state.
3995 * @brief Return the @f$a@f$ parameter of the distribution.
3999 { return _M_param.a(); }
4002 * @brief Return the @f$b@f$ parameter of the distribution.
4006 { return _M_param.b(); }
4009 * @brief Returns the parameter set of the distribution.
4013 { return _M_param; }
4016 * @brief Sets the parameter set of the distribution.
4017 * @param __param The new parameter set of the distribution.
4020 param(const param_type& __param)
4021 { _M_param = __param; }
4024 * @brief Returns the greatest lower bound value of the distribution.
4028 { return std::numeric_limits<result_type>::min(); }
4031 * @brief Returns the least upper bound value of the distribution.
4035 { return std::numeric_limits<result_type>::max(); }
4037 template<typename _UniformRandomNumberGenerator>
4039 operator()(_UniformRandomNumberGenerator& __urng)
4040 { return this->operator()(__urng, this->param()); }
4042 template<typename _UniformRandomNumberGenerator>
4044 operator()(_UniformRandomNumberGenerator& __urng,
4045 const param_type& __p);
4048 param_type _M_param;
4052 * @brief Inserts a %extreme_value_distribution random number distribution
4053 * @p __x into the output stream @p __os.
4055 * @param __os An output stream.
4056 * @param __x A %extreme_value_distribution random number distribution.
4058 * @returns The output stream with the state of @p __x inserted or in
4061 template<typename _RealType, typename _CharT, typename _Traits>
4062 std::basic_ostream<_CharT, _Traits>&
4063 operator<<(std::basic_ostream<_CharT, _Traits>&,
4064 const std::extreme_value_distribution<_RealType>&);
4067 * @brief Extracts a %extreme_value_distribution random number
4068 * distribution @p __x from the input stream @p __is.
4070 * @param __is An input stream.
4071 * @param __x A %extreme_value_distribution random number
4074 * @returns The input stream with @p __x extracted or in an error state.
4076 template<typename _RealType, typename _CharT, typename _Traits>
4077 std::basic_istream<_CharT, _Traits>&
4078 operator>>(std::basic_istream<_CharT, _Traits>&,
4079 std::extreme_value_distribution<_RealType>&);
4083 * @brief A discrete_distribution random number distribution.
4085 * The formula for the discrete probability mass function is
4088 template<typename _IntType = int>
4089 class discrete_distribution
4091 static_assert(std::is_integral<_IntType>::value,
4092 "template argument not an integral type");
4095 /** The type of the range of the distribution. */
4096 typedef _IntType result_type;
4097 /** Parameter type. */
4100 typedef discrete_distribution<_IntType> distribution_type;
4101 friend class discrete_distribution<_IntType>;
4104 : _M_prob(), _M_cp()
4105 { _M_initialize(); }
4107 template<typename _InputIterator>
4108 param_type(_InputIterator __wbegin,
4109 _InputIterator __wend)
4110 : _M_prob(__wbegin, __wend), _M_cp()
4111 { _M_initialize(); }
4113 param_type(initializer_list<double> __wil)
4114 : _M_prob(__wil.begin(), __wil.end()), _M_cp()
4115 { _M_initialize(); }
4117 template<typename _Func>
4118 param_type(size_t __nw, double __xmin, double __xmax,
4122 probabilities() const
4129 std::vector<double> _M_prob;
4130 std::vector<double> _M_cp;
4133 discrete_distribution()
4137 template<typename _InputIterator>
4138 discrete_distribution(_InputIterator __wbegin,
4139 _InputIterator __wend)
4140 : _M_param(__wbegin, __wend)
4143 discrete_distribution(initializer_list<double> __wl)
4147 template<typename _Func>
4148 discrete_distribution(size_t __nw, double __xmin, double __xmax,
4150 : _M_param(__nw, __xmin, __xmax, __fw)
4154 discrete_distribution(const param_type& __p)
4159 * @brief Resets the distribution state.
4166 * @brief Returns the probabilities of the distribution.
4169 probabilities() const
4170 { return _M_param.probabilities(); }
4173 * @brief Returns the parameter set of the distribution.
4177 { return _M_param; }
4180 * @brief Sets the parameter set of the distribution.
4181 * @param __param The new parameter set of the distribution.
4184 param(const param_type& __param)
4185 { _M_param = __param; }
4188 * @brief Returns the greatest lower bound value of the distribution.
4192 { return result_type(0); }
4195 * @brief Returns the least upper bound value of the distribution.
4199 { return this->_M_param._M_prob.size() - 1; }
4201 template<typename _UniformRandomNumberGenerator>
4203 operator()(_UniformRandomNumberGenerator& __urng)
4204 { return this->operator()(__urng, this->param()); }
4206 template<typename _UniformRandomNumberGenerator>
4208 operator()(_UniformRandomNumberGenerator& __urng,
4209 const param_type& __p);
4212 * @brief Inserts a %discrete_distribution random number distribution
4213 * @p __x into the output stream @p __os.
4215 * @param __os An output stream.
4216 * @param __x A %discrete_distribution random number distribution.
4218 * @returns The output stream with the state of @p __x inserted or in
4221 template<typename _IntType1, typename _CharT, typename _Traits>
4222 friend std::basic_ostream<_CharT, _Traits>&
4223 operator<<(std::basic_ostream<_CharT, _Traits>&,
4224 const std::discrete_distribution<_IntType1>&);
4227 * @brief Extracts a %discrete_distribution random number distribution
4228 * @p __x from the input stream @p __is.
4230 * @param __is An input stream.
4231 * @param __x A %discrete_distribution random number
4234 * @returns The input stream with @p __x extracted or in an error
4237 template<typename _IntType1, typename _CharT, typename _Traits>
4238 friend std::basic_istream<_CharT, _Traits>&
4239 operator>>(std::basic_istream<_CharT, _Traits>&,
4240 std::discrete_distribution<_IntType1>&);
4243 param_type _M_param;
4248 * @brief A piecewise_constant_distribution random number distribution.
4250 * The formula for the piecewise constant probability mass function is
4253 template<typename _RealType = double>
4254 class piecewise_constant_distribution
4256 static_assert(std::is_floating_point<_RealType>::value,
4257 "template argument not a floating point type");
4260 /** The type of the range of the distribution. */
4261 typedef _RealType result_type;
4262 /** Parameter type. */
4265 typedef piecewise_constant_distribution<_RealType> distribution_type;
4266 friend class piecewise_constant_distribution<_RealType>;
4269 : _M_int(), _M_den(), _M_cp()
4270 { _M_initialize(); }
4272 template<typename _InputIteratorB, typename _InputIteratorW>
4273 param_type(_InputIteratorB __bfirst,
4274 _InputIteratorB __bend,
4275 _InputIteratorW __wbegin);
4277 template<typename _Func>
4278 param_type(initializer_list<_RealType> __bi, _Func __fw);
4280 template<typename _Func>
4281 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4284 std::vector<_RealType>
4296 std::vector<_RealType> _M_int;
4297 std::vector<double> _M_den;
4298 std::vector<double> _M_cp;
4302 piecewise_constant_distribution()
4306 template<typename _InputIteratorB, typename _InputIteratorW>
4307 piecewise_constant_distribution(_InputIteratorB __bfirst,
4308 _InputIteratorB __bend,
4309 _InputIteratorW __wbegin)
4310 : _M_param(__bfirst, __bend, __wbegin)
4313 template<typename _Func>
4314 piecewise_constant_distribution(initializer_list<_RealType> __bl,
4316 : _M_param(__bl, __fw)
4319 template<typename _Func>
4320 piecewise_constant_distribution(size_t __nw,
4321 _RealType __xmin, _RealType __xmax,
4323 : _M_param(__nw, __xmin, __xmax, __fw)
4327 piecewise_constant_distribution(const param_type& __p)
4332 * @brief Resets the distribution state.
4339 * @brief Returns a vector of the intervals.
4341 std::vector<_RealType>
4343 { return _M_param.intervals(); }
4346 * @brief Returns a vector of the probability densities.
4350 { return _M_param.densities(); }
4353 * @brief Returns the parameter set of the distribution.
4357 { return _M_param; }
4360 * @brief Sets the parameter set of the distribution.
4361 * @param __param The new parameter set of the distribution.
4364 param(const param_type& __param)
4365 { _M_param = __param; }
4368 * @brief Returns the greatest lower bound value of the distribution.
4372 { return this->_M_param._M_int.front(); }
4375 * @brief Returns the least upper bound value of the distribution.
4379 { return this->_M_param._M_int.back(); }
4381 template<typename _UniformRandomNumberGenerator>
4383 operator()(_UniformRandomNumberGenerator& __urng)
4384 { return this->operator()(__urng, this->param()); }
4386 template<typename _UniformRandomNumberGenerator>
4388 operator()(_UniformRandomNumberGenerator& __urng,
4389 const param_type& __p);
4392 * @brief Inserts a %piecewise_constan_distribution random
4393 * number distribution @p __x into the output stream @p __os.
4395 * @param __os An output stream.
4396 * @param __x A %piecewise_constan_distribution random number
4399 * @returns The output stream with the state of @p __x inserted or in
4402 template<typename _RealType1, typename _CharT, typename _Traits>
4403 friend std::basic_ostream<_CharT, _Traits>&
4404 operator<<(std::basic_ostream<_CharT, _Traits>&,
4405 const std::piecewise_constant_distribution<_RealType1>&);
4408 * @brief Extracts a %piecewise_constan_distribution random
4409 * number distribution @p __x from the input stream @p __is.
4411 * @param __is An input stream.
4412 * @param __x A %piecewise_constan_distribution random number
4415 * @returns The input stream with @p __x extracted or in an error
4418 template<typename _RealType1, typename _CharT, typename _Traits>
4419 friend std::basic_istream<_CharT, _Traits>&
4420 operator>>(std::basic_istream<_CharT, _Traits>&,
4421 std::piecewise_constant_distribution<_RealType1>&);
4424 param_type _M_param;
4429 * @brief A piecewise_linear_distribution random number distribution.
4431 * The formula for the piecewise linear probability mass function is
4434 template<typename _RealType = double>
4435 class piecewise_linear_distribution
4437 static_assert(std::is_floating_point<_RealType>::value,
4438 "template argument not a floating point type");
4441 /** The type of the range of the distribution. */
4442 typedef _RealType result_type;
4443 /** Parameter type. */
4446 typedef piecewise_linear_distribution<_RealType> distribution_type;
4447 friend class piecewise_linear_distribution<_RealType>;
4450 : _M_int(), _M_den(), _M_cp(), _M_m()
4451 { _M_initialize(); }
4453 template<typename _InputIteratorB, typename _InputIteratorW>
4454 param_type(_InputIteratorB __bfirst,
4455 _InputIteratorB __bend,
4456 _InputIteratorW __wbegin);
4458 template<typename _Func>
4459 param_type(initializer_list<_RealType> __bl, _Func __fw);
4461 template<typename _Func>
4462 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4465 std::vector<_RealType>
4477 std::vector<_RealType> _M_int;
4478 std::vector<double> _M_den;
4479 std::vector<double> _M_cp;
4480 std::vector<double> _M_m;
4484 piecewise_linear_distribution()
4488 template<typename _InputIteratorB, typename _InputIteratorW>
4489 piecewise_linear_distribution(_InputIteratorB __bfirst,
4490 _InputIteratorB __bend,
4491 _InputIteratorW __wbegin)
4492 : _M_param(__bfirst, __bend, __wbegin)
4495 template<typename _Func>
4496 piecewise_linear_distribution(initializer_list<_RealType> __bl,
4498 : _M_param(__bl, __fw)
4501 template<typename _Func>
4502 piecewise_linear_distribution(size_t __nw,
4503 _RealType __xmin, _RealType __xmax,
4505 : _M_param(__nw, __xmin, __xmax, __fw)
4509 piecewise_linear_distribution(const param_type& __p)
4514 * Resets the distribution state.
4521 * @brief Return the intervals of the distribution.
4523 std::vector<_RealType>
4525 { return _M_param.intervals(); }
4528 * @brief Return a vector of the probability densities of the
4533 { return _M_param.densities(); }
4536 * @brief Returns the parameter set of the distribution.
4540 { return _M_param; }
4543 * @brief Sets the parameter set of the distribution.
4544 * @param __param The new parameter set of the distribution.
4547 param(const param_type& __param)
4548 { _M_param = __param; }
4551 * @brief Returns the greatest lower bound value of the distribution.
4555 { return this->_M_param._M_int.front(); }
4558 * @brief Returns the least upper bound value of the distribution.
4562 { return this->_M_param._M_int.back(); }
4564 template<typename _UniformRandomNumberGenerator>
4566 operator()(_UniformRandomNumberGenerator& __urng)
4567 { return this->operator()(__urng, this->param()); }
4569 template<typename _UniformRandomNumberGenerator>
4571 operator()(_UniformRandomNumberGenerator& __urng,
4572 const param_type& __p);
4575 * @brief Inserts a %piecewise_linear_distribution random number
4576 * distribution @p __x into the output stream @p __os.
4578 * @param __os An output stream.
4579 * @param __x A %piecewise_linear_distribution random number
4582 * @returns The output stream with the state of @p __x inserted or in
4585 template<typename _RealType1, typename _CharT, typename _Traits>
4586 friend std::basic_ostream<_CharT, _Traits>&
4587 operator<<(std::basic_ostream<_CharT, _Traits>&,
4588 const std::piecewise_linear_distribution<_RealType1>&);
4591 * @brief Extracts a %piecewise_linear_distribution random number
4592 * distribution @p __x from the input stream @p __is.
4594 * @param __is An input stream.
4595 * @param __x A %piecewise_linear_distribution random number
4598 * @returns The input stream with @p __x extracted or in an error
4601 template<typename _RealType1, typename _CharT, typename _Traits>
4602 friend std::basic_istream<_CharT, _Traits>&
4603 operator>>(std::basic_istream<_CharT, _Traits>&,
4604 std::piecewise_linear_distribution<_RealType1>&);
4607 param_type _M_param;
4611 /* @} */ // group random_distributions_poisson
4613 /* @} */ // group random_distributions
4616 * @addtogroup random_utilities Random Number Utilities
4622 * @brief The seed_seq class generates sequences of seeds for random
4623 * number generators.
4629 /** The type of the seed vales. */
4630 typedef uint_least32_t result_type;
4632 /** Default constructor. */
4637 template<typename _IntType>
4638 seed_seq(std::initializer_list<_IntType> il);
4640 template<typename _InputIterator>
4641 seed_seq(_InputIterator __begin, _InputIterator __end);
4643 // generating functions
4644 template<typename _RandomAccessIterator>
4646 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
4648 // property functions
4650 { return _M_v.size(); }
4652 template<typename OutputIterator>
4654 param(OutputIterator __dest) const
4655 { std::copy(_M_v.begin(), _M_v.end(), __dest); }
4659 std::vector<result_type> _M_v;
4662 /* @} */ // group random_utilities
4664 /* @} */ // group random