1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
35 // [26.4] Random number generation
38 * @addtogroup std_random Random Number Generation
39 * A facility for generating random numbers on selected distributions.
44 * @brief A function template for converting the output of a (integral)
45 * uniform random number generator to a floatng point result in the range
48 template<typename _RealType, size_t __bits,
49 typename _UniformRandomNumberGenerator>
51 generate_canonical(_UniformRandomNumberGenerator& __g);
54 * Implementation-space details.
58 template<typename _UIntType, size_t __w,
59 bool = __w < static_cast<size_t>
60 (std::numeric_limits<_UIntType>::digits)>
62 { static const _UIntType __value = 0; };
64 template<typename _UIntType, size_t __w>
65 struct _Shift<_UIntType, __w, true>
66 { static const _UIntType __value = _UIntType(1) << __w; };
68 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
71 // Dispatch based on modulus value to prevent divide-by-zero compile-time
72 // errors when m == 0.
73 template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
76 { return _Mod<_Tp, __m, __a, __c, __m == 0>::__calc(__x); }
79 * An adaptor class for converting the output of any Generator into
80 * the input for a specific Distribution.
82 template<typename _Engine, typename _DInputType>
87 _Adaptor(_Engine& __g)
92 { return _DInputType(0); }
96 { return _DInputType(1); }
99 * Converts a value generated by the adapted random number generator
100 * into a value in the input domain for the dependent random number
106 return std::generate_canonical<_DInputType,
107 std::numeric_limits<_DInputType>::digits,
114 } // namespace __detail
117 * @addtogroup std_random_generators Random Number Generators
118 * @ingroup std_random
120 * These classes define objects which provide random or pseudorandom
121 * numbers, either from a discrete or a continuous interval. The
122 * random number generator supplied as a part of this library are
123 * all uniform random number generators which provide a sequence of
124 * random number uniformly distributed over their range.
126 * A number generator is a function object with an operator() that
127 * takes zero arguments and returns a number.
129 * A compliant random number generator must satisfy the following
130 * requirements. <table border=1 cellpadding=10 cellspacing=0>
131 * <caption align=top>Random Number Generator Requirements</caption>
132 * <tr><td>To be documented.</td></tr> </table>
138 * @brief A model of a linear congruential random number generator.
140 * A random number generator that produces pseudorandom numbers via
143 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
146 * The template parameter @p _UIntType must be an unsigned integral type
147 * large enough to store values up to (__m-1). If the template parameter
148 * @p __m is 0, the modulus @p __m used is
149 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
150 * parameters @p __a and @p __c must be less than @p __m.
152 * The size of the state is @f$1@f$.
154 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
155 class linear_congruential_engine
157 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
158 "substituting _UIntType not an unsigned integral type");
159 static_assert(__m == 0u || (__a < __m && __c < __m),
160 "template argument substituting __m out of bounds");
163 /** The type of the generated random value. */
164 typedef _UIntType result_type;
166 /** The multiplier. */
167 static const result_type multiplier = __a;
169 static const result_type increment = __c;
171 static const result_type modulus = __m;
172 static const result_type default_seed = 1u;
175 * @brief Constructs a %linear_congruential_engine random number
176 * generator engine with seed @p __s. The default seed value
179 * @param __s The initial seed value.
182 linear_congruential_engine(result_type __s = default_seed)
186 * @brief Constructs a %linear_congruential_engine random number
187 * generator engine seeded from the seed sequence @p __q.
189 * @param __q the seed sequence.
191 template<typename _Sseq, typename
192 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
194 linear_congruential_engine(_Sseq& __q)
195 { seed<_Sseq>(__q); }
198 * @brief Reseeds the %linear_congruential_engine random number generator
199 * engine sequence to the seed @p __s.
201 * @param __s The new seed.
204 seed(result_type __s = default_seed);
207 * @brief Reseeds the %linear_congruential_engine random number generator
209 * sequence using values from the seed sequence @p __q.
211 * @param __q the seed sequence.
213 template<typename _Sseq, typename
214 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
219 * @brief Gets the smallest possible value in the output range.
221 * The minimum depends on the @p __c parameter: if it is zero, the
222 * minimum generated must be > 0, otherwise 0 is allowed.
224 * @todo This should be constexpr.
228 { return __c == 0u ? 1u : 0u; }
231 * @brief Gets the largest possible value in the output range.
233 * @todo This should be constexpr.
240 * @brief Discard a sequence of random numbers.
242 * @todo Look for a faster way to do discard.
245 discard(unsigned long long __z)
247 for (; __z != 0ULL; --__z)
252 * @brief Gets the next random number in the sequence.
257 _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
262 * @brief Compares two linear congruential random number generator
263 * objects of the same type for equality.
265 * @param __lhs A linear congruential random number generator object.
266 * @param __rhs Another linear congruential random number generator
269 * @returns true if the two objects are equal, false otherwise.
272 operator==(const linear_congruential_engine& __lhs,
273 const linear_congruential_engine& __rhs)
274 { return __lhs._M_x == __rhs._M_x; }
277 * @brief Writes the textual representation of the state x(i) of x to
280 * @param __os The output stream.
281 * @param __lcr A % linear_congruential_engine random number generator.
284 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
285 _UIntType1 __m1, typename _CharT, typename _Traits>
286 friend std::basic_ostream<_CharT, _Traits>&
287 operator<<(std::basic_ostream<_CharT, _Traits>&,
288 const std::linear_congruential_engine<_UIntType1,
292 * @brief Sets the state of the engine by reading its textual
293 * representation from @p __is.
295 * The textual representation must have been previously written using
296 * an output stream whose imbued locale and whose type's template
297 * specialization arguments _CharT and _Traits were the same as those
300 * @param __is The input stream.
301 * @param __lcr A % linear_congruential_engine random number generator.
304 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
305 _UIntType1 __m1, typename _CharT, typename _Traits>
306 friend std::basic_istream<_CharT, _Traits>&
307 operator>>(std::basic_istream<_CharT, _Traits>&,
308 std::linear_congruential_engine<_UIntType1, __a1,
317 * A generalized feedback shift register discrete random number generator.
319 * This algorithm avoids multiplication and division and is designed to be
320 * friendly to a pipelined architecture. If the parameters are chosen
321 * correctly, this generator will produce numbers with a very long period and
322 * fairly good apparent entropy, although still not cryptographically strong.
324 * The best way to use this generator is with the predefined mt19937 class.
326 * This algorithm was originally invented by Makoto Matsumoto and
329 * @var word_size The number of bits in each element of the state vector.
330 * @var state_size The degree of recursion.
331 * @var shift_size The period parameter.
332 * @var mask_bits The separation point bit index.
333 * @var parameter_a The last row of the twist matrix.
334 * @var output_u The first right-shift tempering matrix parameter.
335 * @var output_s The first left-shift tempering matrix parameter.
336 * @var output_b The first left-shift tempering matrix mask.
337 * @var output_t The second left-shift tempering matrix parameter.
338 * @var output_c The second left-shift tempering matrix mask.
339 * @var output_l The second right-shift tempering matrix parameter.
341 template<typename _UIntType, size_t __w,
342 size_t __n, size_t __m, size_t __r,
343 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
344 _UIntType __b, size_t __t,
345 _UIntType __c, size_t __l, _UIntType __f>
346 class mersenne_twister_engine
348 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
349 "substituting _UIntType not an unsigned integral type");
350 static_assert(1u <= __m && __m <= __n,
351 "template argument substituting __m out of bounds");
352 static_assert(__r <= __w, "template argument substituting "
354 static_assert(__u <= __w, "template argument substituting "
356 static_assert(__s <= __w, "template argument substituting "
358 static_assert(__t <= __w, "template argument substituting "
360 static_assert(__l <= __w, "template argument substituting "
362 static_assert(__w <= std::numeric_limits<_UIntType>::digits,
363 "template argument substituting __w out of bound");
364 static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
365 "template argument substituting __a out of bound");
366 static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
367 "template argument substituting __b out of bound");
368 static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
369 "template argument substituting __c out of bound");
370 static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
371 "template argument substituting __d out of bound");
372 static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
373 "template argument substituting __f out of bound");
376 /** The type of the generated random value. */
377 typedef _UIntType result_type;
380 static const size_t word_size = __w;
381 static const size_t state_size = __n;
382 static const size_t shift_size = __m;
383 static const size_t mask_bits = __r;
384 static const result_type xor_mask = __a;
385 static const size_t tempering_u = __u;
386 static const result_type tempering_d = __d;
387 static const size_t tempering_s = __s;
388 static const result_type tempering_b = __b;
389 static const size_t tempering_t = __t;
390 static const result_type tempering_c = __c;
391 static const size_t tempering_l = __l;
392 static const result_type initialization_multiplier = __f;
393 static const result_type default_seed = 5489u;
395 // constructors and member function
397 mersenne_twister_engine(result_type __sd = default_seed)
401 * @brief Constructs a %mersenne_twister_engine random number generator
402 * engine seeded from the seed sequence @p __q.
404 * @param __q the seed sequence.
406 template<typename _Sseq, typename
407 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
409 mersenne_twister_engine(_Sseq& __q)
410 { seed<_Sseq>(__q); }
413 seed(result_type __sd = default_seed);
415 template<typename _Sseq, typename
416 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
421 * @brief Gets the smallest possible value in the output range.
423 * @todo This should be constexpr.
430 * @brief Gets the largest possible value in the output range.
432 * @todo This should be constexpr.
436 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
439 * @brief Discard a sequence of random numbers.
441 * @todo Look for a faster way to do discard.
444 discard(unsigned long long __z)
446 for (; __z != 0ULL; --__z)
454 * @brief Compares two % mersenne_twister_engine random number generator
455 * objects of the same type for equality.
457 * @param __lhs A % mersenne_twister_engine random number generator
459 * @param __rhs Another % mersenne_twister_engine random number
462 * @returns true if the two objects are equal, false otherwise.
465 operator==(const mersenne_twister_engine& __lhs,
466 const mersenne_twister_engine& __rhs)
467 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
470 * @brief Inserts the current state of a % mersenne_twister_engine
471 * random number generator engine @p __x into the output stream
474 * @param __os An output stream.
475 * @param __x A % mersenne_twister_engine random number generator
478 * @returns The output stream with the state of @p __x inserted or in
481 template<typename _UIntType1,
482 size_t __w1, size_t __n1,
483 size_t __m1, size_t __r1,
484 _UIntType1 __a1, size_t __u1,
485 _UIntType1 __d1, size_t __s1,
486 _UIntType1 __b1, size_t __t1,
487 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
488 typename _CharT, typename _Traits>
489 friend std::basic_ostream<_CharT, _Traits>&
490 operator<<(std::basic_ostream<_CharT, _Traits>&,
491 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
492 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
496 * @brief Extracts the current state of a % mersenne_twister_engine
497 * random number generator engine @p __x from the input stream
500 * @param __is An input stream.
501 * @param __x A % mersenne_twister_engine random number generator
504 * @returns The input stream with the state of @p __x extracted or in
507 template<typename _UIntType1,
508 size_t __w1, size_t __n1,
509 size_t __m1, size_t __r1,
510 _UIntType1 __a1, size_t __u1,
511 _UIntType1 __d1, size_t __s1,
512 _UIntType1 __b1, size_t __t1,
513 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
514 typename _CharT, typename _Traits>
515 friend std::basic_istream<_CharT, _Traits>&
516 operator>>(std::basic_istream<_CharT, _Traits>&,
517 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
518 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
522 _UIntType _M_x[state_size];
527 * @brief The Marsaglia-Zaman generator.
529 * This is a model of a Generalized Fibonacci discrete random number
530 * generator, sometimes referred to as the SWC generator.
532 * A discrete random number generator that produces pseudorandom
535 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
538 * The size of the state is @f$r@f$
539 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
541 * @var _M_x The state of the generator. This is a ring buffer.
542 * @var _M_carry The carry.
543 * @var _M_p Current index of x(i - r).
545 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
546 class subtract_with_carry_engine
548 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
549 "substituting _UIntType not an unsigned integral type");
550 static_assert(0u < __s && __s < __r,
551 "template argument substituting __s out of bounds");
552 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
553 "template argument substituting __w out of bounds");
556 /** The type of the generated random value. */
557 typedef _UIntType result_type;
560 static const size_t word_size = __w;
561 static const size_t short_lag = __s;
562 static const size_t long_lag = __r;
563 static const result_type default_seed = 19780503u;
566 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
567 * random number generator.
570 subtract_with_carry_engine(result_type __sd = default_seed)
574 * @brief Constructs a %subtract_with_carry_engine random number engine
575 * seeded from the seed sequence @p __q.
577 * @param __q the seed sequence.
579 template<typename _Sseq, typename
580 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
582 subtract_with_carry_engine(_Sseq& __q)
583 { seed<_Sseq>(__q); }
586 * @brief Seeds the initial state @f$x_0@f$ of the random number
589 * N1688[4.19] modifies this as follows. If @p __value == 0,
590 * sets value to 19780503. In any case, with a linear
591 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
592 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
593 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
594 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
595 * set carry to 1, otherwise sets carry to 0.
598 seed(result_type __sd = default_seed);
601 * @brief Seeds the initial state @f$x_0@f$ of the
602 * % subtract_with_carry_engine random number generator.
604 template<typename _Sseq, typename
605 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
610 * @brief Gets the inclusive minimum value of the range of random
611 * integers returned by this generator.
613 * @todo This should be constexpr.
620 * @brief Gets the inclusive maximum value of the range of random
621 * integers returned by this generator.
623 * @todo This should be constexpr.
627 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
630 * @brief Discard a sequence of random numbers.
632 * @todo Look for a faster way to do discard.
635 discard(unsigned long long __z)
637 for (; __z != 0ULL; --__z)
642 * @brief Gets the next random number in the sequence.
648 * @brief Compares two % subtract_with_carry_engine random number
649 * generator objects of the same type for equality.
651 * @param __lhs A % subtract_with_carry_engine random number generator
653 * @param __rhs Another % subtract_with_carry_engine random number
656 * @returns true if the two objects are equal, false otherwise.
659 operator==(const subtract_with_carry_engine& __lhs,
660 const subtract_with_carry_engine& __rhs)
661 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
664 * @brief Inserts the current state of a % subtract_with_carry_engine
665 * random number generator engine @p __x into the output stream
668 * @param __os An output stream.
669 * @param __x A % subtract_with_carry_engine random number generator
672 * @returns The output stream with the state of @p __x inserted or in
675 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
676 typename _CharT, typename _Traits>
677 friend std::basic_ostream<_CharT, _Traits>&
678 operator<<(std::basic_ostream<_CharT, _Traits>&,
679 const std::subtract_with_carry_engine<_UIntType1, __w1,
683 * @brief Extracts the current state of a % subtract_with_carry_engine
684 * random number generator engine @p __x from the input stream
687 * @param __is An input stream.
688 * @param __x A % subtract_with_carry_engine random number generator
691 * @returns The input stream with the state of @p __x extracted or in
694 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
695 typename _CharT, typename _Traits>
696 friend std::basic_istream<_CharT, _Traits>&
697 operator>>(std::basic_istream<_CharT, _Traits>&,
698 std::subtract_with_carry_engine<_UIntType1, __w1,
702 _UIntType _M_x[long_lag];
708 * Produces random numbers from some base engine by discarding blocks of
711 * 0 <= @p __r <= @p __p
713 template<typename _RandomNumberEngine, size_t __p, size_t __r>
714 class discard_block_engine
716 static_assert(1 <= __r && __r <= __p,
717 "template argument substituting __r out of bounds");
720 /** The type of the generated random value. */
721 typedef typename _RandomNumberEngine::result_type result_type;
724 static const size_t block_size = __p;
725 static const size_t used_block = __r;
728 * @brief Constructs a default %discard_block_engine engine.
730 * The underlying engine is default constructed as well.
732 discard_block_engine()
733 : _M_b(), _M_n(0) { }
736 * @brief Copy constructs a %discard_block_engine engine.
738 * Copies an existing base class random number generator.
739 * @param rng An existing (base class) engine object.
742 discard_block_engine(const _RandomNumberEngine& __rne)
743 : _M_b(__rne), _M_n(0) { }
746 * @brief Move constructs a %discard_block_engine engine.
748 * Copies an existing base class random number generator.
749 * @param rng An existing (base class) engine object.
752 discard_block_engine(_RandomNumberEngine&& __rne)
753 : _M_b(std::move(__rne)), _M_n(0) { }
756 * @brief Seed constructs a %discard_block_engine engine.
758 * Constructs the underlying generator engine seeded with @p __s.
759 * @param __s A seed value for the base class engine.
762 discard_block_engine(result_type __s)
763 : _M_b(__s), _M_n(0) { }
766 * @brief Generator construct a %discard_block_engine engine.
768 * @param __q A seed sequence.
770 template<typename _Sseq, typename
771 = typename std::enable_if<std::is_class<_Sseq>::value
772 && !std::is_same<_Sseq, _RandomNumberEngine>
775 discard_block_engine(_Sseq& __q)
780 * @brief Reseeds the %discard_block_engine object with the default
781 * seed for the underlying base class generator engine.
791 * @brief Reseeds the %discard_block_engine object with the default
792 * seed for the underlying base class generator engine.
795 seed(result_type __s)
802 * @brief Reseeds the %discard_block_engine object with the given seed
804 * @param __q A seed generator function.
806 template<typename _Sseq, typename
807 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
811 _M_b.seed<_Sseq>(__q);
816 * @brief Gets a const reference to the underlying generator engine
819 const _RandomNumberEngine&
824 * @brief Gets the minimum value in the generated random number range.
826 * @todo This should be constexpr.
830 { return _M_b.min(); }
833 * @brief Gets the maximum value in the generated random number range.
835 * @todo This should be constexpr.
839 { return _M_b.max(); }
842 * @brief Discard a sequence of random numbers.
844 * @todo Look for a faster way to do discard.
847 discard(unsigned long long __z)
849 for (; __z != 0ULL; --__z)
854 * @brief Gets the next value in the generated random number sequence.
860 * @brief Compares two %discard_block_engine random number generator
861 * objects of the same type for equality.
863 * @param __lhs A %discard_block_engine random number generator object.
864 * @param __rhs Another %discard_block_engine random number generator
867 * @returns true if the two objects are equal, false otherwise.
870 operator==(const discard_block_engine& __lhs,
871 const discard_block_engine& __rhs)
872 { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); }
875 * @brief Inserts the current state of a %discard_block_engine random
876 * number generator engine @p __x into the output stream
879 * @param __os An output stream.
880 * @param __x A %discard_block_engine random number generator engine.
882 * @returns The output stream with the state of @p __x inserted or in
885 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
886 typename _CharT, typename _Traits>
887 friend std::basic_ostream<_CharT, _Traits>&
888 operator<<(std::basic_ostream<_CharT, _Traits>&,
889 const std::discard_block_engine<_RandomNumberEngine1,
893 * @brief Extracts the current state of a % subtract_with_carry_engine
894 * random number generator engine @p __x from the input stream
897 * @param __is An input stream.
898 * @param __x A %discard_block_engine random number generator engine.
900 * @returns The input stream with the state of @p __x extracted or in
903 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
904 typename _CharT, typename _Traits>
905 friend std::basic_istream<_CharT, _Traits>&
906 operator>>(std::basic_istream<_CharT, _Traits>&,
907 std::discard_block_engine<_RandomNumberEngine1,
911 _RandomNumberEngine _M_b;
916 * Produces random numbers by combining random numbers from some base
917 * engine to produce random numbers with a specifies number of bits @p __w.
919 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
920 class independent_bits_engine
922 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
923 "substituting _UIntType not an unsigned integral type");
924 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
925 "template argument substituting __w out of bounds");
928 /** The type of the generated random value. */
929 typedef _UIntType result_type;
932 * @brief Constructs a default %independent_bits_engine engine.
934 * The underlying engine is default constructed as well.
936 independent_bits_engine()
940 * @brief Copy constructs a %independent_bits_engine engine.
942 * Copies an existing base class random number generator.
943 * @param rng An existing (base class) engine object.
946 independent_bits_engine(const _RandomNumberEngine& __rne)
950 * @brief Move constructs a %independent_bits_engine engine.
952 * Copies an existing base class random number generator.
953 * @param rng An existing (base class) engine object.
956 independent_bits_engine(_RandomNumberEngine&& __rne)
957 : _M_b(std::move(__rne)) { }
960 * @brief Seed constructs a %independent_bits_engine engine.
962 * Constructs the underlying generator engine seeded with @p __s.
963 * @param __s A seed value for the base class engine.
966 independent_bits_engine(result_type __s)
970 * @brief Generator construct a %independent_bits_engine engine.
972 * @param __q A seed sequence.
974 template<typename _Sseq, typename
975 = typename std::enable_if<std::is_class<_Sseq>::value
976 && !std::is_same<_Sseq, _RandomNumberEngine>
979 independent_bits_engine(_Sseq& __q)
984 * @brief Reseeds the %independent_bits_engine object with the default
985 * seed for the underlying base class generator engine.
992 * @brief Reseeds the %independent_bits_engine object with the default
993 * seed for the underlying base class generator engine.
996 seed(result_type __s)
1000 * @brief Reseeds the %independent_bits_engine object with the given
1002 * @param __q A seed generator function.
1004 template<typename _Sseq, typename
1005 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
1008 { _M_b.seed<_Sseq>(__q); }
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
1182 = typename std::enable_if<std::is_class<_Sseq>::value
1183 && !std::is_same<_Sseq, _RandomNumberEngine>
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, typename
1218 = typename std::enable_if<std::is_class<_Sseq>::value>::type>
1222 _M_b.seed<_Sseq>(__q);
1227 * Gets a const reference to the underlying generator engine object.
1229 const _RandomNumberEngine&
1234 * Gets the minimum value in the generated random number range.
1236 * @todo This should be constexpr.
1240 { return _M_b.min(); }
1243 * Gets the maximum value in the generated random number range.
1245 * @todo This should be constexpr.
1249 { return _M_b.max(); }
1252 * Discard a sequence of random numbers.
1254 * @todo Look for a faster way to do discard.
1257 discard(unsigned long long __z)
1259 for (; __z != 0ULL; --__z)
1264 * Gets the next value in the generated random number sequence.
1270 * Compares two %shuffle_order_engine random number generator objects
1271 * of the same type for equality.
1273 * @param __lhs A %shuffle_order_engine random number generator object.
1274 * @param __rhs Another %shuffle_order_engine random number generator
1277 * @returns true if the two objects are equal, false otherwise.
1280 operator==(const shuffle_order_engine& __lhs,
1281 const shuffle_order_engine& __rhs)
1282 { return __lhs._M_b == __rhs._M_b; }
1285 * @brief Inserts the current state of a %shuffle_order_engine random
1286 * number generator engine @p __x into the output stream
1289 * @param __os An output stream.
1290 * @param __x A %shuffle_order_engine random number generator engine.
1292 * @returns The output stream with the state of @p __x inserted or in
1295 template<typename _RandomNumberEngine1, size_t __k1,
1296 typename _CharT, typename _Traits>
1297 friend std::basic_ostream<_CharT, _Traits>&
1298 operator<<(std::basic_ostream<_CharT, _Traits>&,
1299 const std::shuffle_order_engine<_RandomNumberEngine1,
1303 * @brief Extracts the current state of a % subtract_with_carry_engine
1304 * random number generator engine @p __x from the input stream
1307 * @param __is An input stream.
1308 * @param __x A %shuffle_order_engine random number generator engine.
1310 * @returns The input stream with the state of @p __x extracted or in
1313 template<typename _RandomNumberEngine1, size_t __k1,
1314 typename _CharT, typename _Traits>
1315 friend std::basic_istream<_CharT, _Traits>&
1316 operator>>(std::basic_istream<_CharT, _Traits>&,
1317 std::shuffle_order_engine<_RandomNumberEngine1, __k1>&);
1320 void _M_initialize()
1322 for (size_t __i = 0; __i < __k; ++__i)
1327 _RandomNumberEngine _M_b;
1328 result_type _M_v[__k];
1333 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1335 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1339 * An alternative LCR (Lehmer Generator function).
1341 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1345 * The classic Mersenne Twister.
1348 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1349 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1350 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1352 typedef mersenne_twister_engine<
1358 0xefc60000UL, 18, 1812433253UL> mt19937;
1361 * An alternative Mersenne Twister.
1363 typedef mersenne_twister_engine<
1366 0xb5026f5aa96619e9ULL, 29,
1367 0x5555555555555555ULL, 17,
1368 0x71d67fffeda60000ULL, 37,
1369 0xfff7eee000000000ULL, 43,
1370 6364136223846793005ULL> mt19937_64;
1372 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1375 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1378 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1380 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1382 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1384 typedef minstd_rand0 default_random_engine;
1387 * A standard interface to a platform-specific non-deterministic
1388 * random number generator (if any are available).
1393 /** The type of the generated random value. */
1394 typedef unsigned int result_type;
1396 // constructors, destructors and member functions
1398 #ifdef _GLIBCXX_USE_RANDOM_TR1
1401 random_device(const std::string& __token = "/dev/urandom")
1403 if ((__token != "/dev/urandom" && __token != "/dev/random")
1404 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1405 std::__throw_runtime_error(__N("random_device::"
1406 "random_device(const std::string&)"));
1410 { std::fclose(_M_file); }
1415 random_device(const std::string& __token = "mt19937")
1416 : _M_mt(_M_strtoul(__token)) { }
1419 static unsigned long
1420 _M_strtoul(const std::string& __str)
1422 unsigned long __ret = 5489UL;
1423 if (__str != "mt19937")
1425 const char* __nptr = __str.c_str();
1427 __ret = std::strtoul(__nptr, &__endptr, 0);
1428 if (*__nptr == '\0' || *__endptr != '\0')
1429 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1430 "(const std::string&)"));
1441 { return std::numeric_limits<result_type>::min(); }
1445 { return std::numeric_limits<result_type>::max(); }
1454 #ifdef _GLIBCXX_USE_RANDOM_TR1
1456 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1464 // No copy functions.
1465 random_device(const random_device&) = delete;
1466 void operator=(const random_device&) = delete;
1470 #ifdef _GLIBCXX_USE_RANDOM_TR1
1477 /* @} */ // group std_random_generators
1480 * @addtogroup std_random_distributions Random Number Distributions
1481 * @ingroup std_random
1486 * @addtogroup std_random_distributions_uniform Uniform Distributions
1487 * @ingroup std_random_distributions
1492 * @brief Uniform discrete distribution for random numbers.
1493 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1494 * probability throughout the range.
1496 template<typename _IntType = int>
1497 class uniform_int_distribution
1499 static_assert(std::is_integral<_IntType>::value,
1500 "template argument not an integral type");
1503 /** The type of the range of the distribution. */
1504 typedef _IntType result_type;
1505 /** Parameter type. */
1508 typedef uniform_int_distribution<_IntType> distribution_type;
1511 param_type(_IntType __a = 0,
1512 _IntType __b = std::numeric_limits<_IntType>::max())
1513 : _M_a(__a), _M_b(__b)
1515 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1533 * @brief Constructs a uniform distribution object.
1536 uniform_int_distribution(_IntType __a = 0,
1537 _IntType __b = std::numeric_limits<_IntType>::max())
1538 : _M_param(__a, __b)
1542 uniform_int_distribution(const param_type& __p)
1547 * @brief Resets the distribution state.
1549 * Does nothing for the uniform integer distribution.
1556 { return _M_param.a(); }
1560 { return _M_param.b(); }
1563 * @brief Returns the inclusive lower bound of the distribution range.
1567 { return this->a(); }
1570 * @brief Returns the inclusive upper bound of the distribution range.
1574 { return this->b(); }
1577 * @brief Returns the parameter set of the distribution.
1581 { return _M_param; }
1584 * @brief Sets the parameter set of the distribution.
1585 * @param __param The new parameter set of the distribution.
1588 param(const param_type& __param)
1589 { _M_param = __param; }
1592 * Gets a uniformly distributed random number in the range
1595 template<typename _UniformRandomNumberGenerator>
1597 operator()(_UniformRandomNumberGenerator& __urng)
1598 { return this->operator()(__urng, this->param()); }
1601 * Gets a uniform random number in the range @f$[0, n)@f$.
1603 * This function is aimed at use with std::random_shuffle.
1605 template<typename _UniformRandomNumberGenerator>
1607 operator()(_UniformRandomNumberGenerator& __urng,
1608 const param_type& __p);
1610 param_type _M_param;
1614 * @brief Inserts a %uniform_int_distribution random number
1615 * distribution @p __x into the output stream @p os.
1617 * @param __os An output stream.
1618 * @param __x A %uniform_int_distribution random number distribution.
1620 * @returns The output stream with the state of @p __x inserted or in
1623 template<typename _IntType, typename _CharT, typename _Traits>
1624 std::basic_ostream<_CharT, _Traits>&
1625 operator<<(std::basic_ostream<_CharT, _Traits>&,
1626 const std::uniform_int_distribution<_IntType>&);
1629 * @brief Extracts a %uniform_int_distribution random number distribution
1630 * @p __x from the input stream @p __is.
1632 * @param __is An input stream.
1633 * @param __x A %uniform_int_distribution random number generator engine.
1635 * @returns The input stream with @p __x extracted or in an error state.
1637 template<typename _IntType, typename _CharT, typename _Traits>
1638 std::basic_istream<_CharT, _Traits>&
1639 operator>>(std::basic_istream<_CharT, _Traits>&,
1640 std::uniform_int_distribution<_IntType>&);
1644 * @brief Uniform continuous distribution for random numbers.
1646 * A continuous random distribution on the range [min, max) with equal
1647 * probability throughout the range. The URNG should be real-valued and
1648 * deliver number in the range [0, 1).
1650 template<typename _RealType = double>
1651 class uniform_real_distribution
1653 static_assert(std::is_floating_point<_RealType>::value,
1654 "template argument not a floating point type");
1657 /** The type of the range of the distribution. */
1658 typedef _RealType result_type;
1659 /** Parameter type. */
1662 typedef uniform_real_distribution<_RealType> distribution_type;
1665 param_type(_RealType __a = _RealType(0),
1666 _RealType __b = _RealType(1))
1667 : _M_a(__a), _M_b(__b)
1669 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1687 * @brief Constructs a uniform_real_distribution object.
1689 * @param __min [IN] The lower bound of the distribution.
1690 * @param __max [IN] The upper bound of the distribution.
1693 uniform_real_distribution(_RealType __a = _RealType(0),
1694 _RealType __b = _RealType(1))
1695 : _M_param(__a, __b)
1699 uniform_real_distribution(const param_type& __p)
1704 * @brief Resets the distribution state.
1706 * Does nothing for the uniform real distribution.
1713 { return _M_param.a(); }
1717 { return _M_param.b(); }
1720 * @brief Returns the inclusive lower bound of the distribution range.
1724 { return this->a(); }
1727 * @brief Returns the inclusive upper bound of the distribution range.
1731 { return this->b(); }
1734 * @brief Returns the parameter set of the distribution.
1738 { return _M_param; }
1741 * @brief Sets the parameter set of the distribution.
1742 * @param __param The new parameter set of the distribution.
1745 param(const param_type& __param)
1746 { _M_param = __param; }
1748 template<typename _UniformRandomNumberGenerator>
1750 operator()(_UniformRandomNumberGenerator& __urng)
1751 { return this->operator()(__urng, this->param()); }
1753 template<typename _UniformRandomNumberGenerator>
1755 operator()(_UniformRandomNumberGenerator& __urng,
1756 const param_type& __p)
1758 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1760 return (__aurng() * (__p.b() - __p.a())) + __p.a();
1764 param_type _M_param;
1768 * @brief Inserts a %uniform_real_distribution random number
1769 * distribution @p __x into the output stream @p __os.
1771 * @param __os An output stream.
1772 * @param __x A %uniform_real_distribution random number distribution.
1774 * @returns The output stream with the state of @p __x inserted or in
1777 template<typename _RealType, typename _CharT, typename _Traits>
1778 std::basic_ostream<_CharT, _Traits>&
1779 operator<<(std::basic_ostream<_CharT, _Traits>&,
1780 const std::uniform_real_distribution<_RealType>&);
1783 * @brief Extracts a %uniform_real_distribution random number distribution
1784 * @p __x from the input stream @p __is.
1786 * @param __is An input stream.
1787 * @param __x A %uniform_real_distribution random number generator engine.
1789 * @returns The input stream with @p __x extracted or in an error state.
1791 template<typename _RealType, typename _CharT, typename _Traits>
1792 std::basic_istream<_CharT, _Traits>&
1793 operator>>(std::basic_istream<_CharT, _Traits>&,
1794 std::uniform_real_distribution<_RealType>&);
1796 /* @} */ // group std_random_distributions_uniform
1799 * @addtogroup std_random_distributions_normal Normal Distributions
1800 * @ingroup std_random_distributions
1805 * @brief A normal continuous distribution for random numbers.
1807 * The formula for the normal probability density function is
1809 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1810 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1813 template<typename _RealType = double>
1814 class normal_distribution
1816 static_assert(std::is_floating_point<_RealType>::value,
1817 "template argument not a floating point type");
1820 /** The type of the range of the distribution. */
1821 typedef _RealType result_type;
1822 /** Parameter type. */
1825 typedef normal_distribution<_RealType> distribution_type;
1828 param_type(_RealType __mean = _RealType(0),
1829 _RealType __stddev = _RealType(1))
1830 : _M_mean(__mean), _M_stddev(__stddev)
1832 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
1841 { return _M_stddev; }
1845 _RealType _M_stddev;
1850 * Constructs a normal distribution with parameters @f$mean@f$ and
1851 * standard deviation.
1854 normal_distribution(result_type __mean = result_type(0),
1855 result_type __stddev = result_type(1))
1856 : _M_param(__mean, __stddev), _M_saved_available(false)
1860 normal_distribution(const param_type& __p)
1861 : _M_param(__p), _M_saved_available(false)
1865 * @brief Resets the distribution state.
1869 { _M_saved_available = false; }
1872 * @brief Returns the mean of the distribution.
1876 { return _M_param.mean(); }
1879 * @brief Returns the standard deviation of the distribution.
1883 { return _M_param.stddev(); }
1886 * @brief Returns the parameter set of the distribution.
1890 { return _M_param; }
1893 * @brief Sets the parameter set of the distribution.
1894 * @param __param The new parameter set of the distribution.
1897 param(const param_type& __param)
1898 { _M_param = __param; }
1901 * @brief Returns the greatest lower bound value of the distribution.
1905 { return std::numeric_limits<result_type>::min(); }
1908 * @brief Returns the least upper bound value of the distribution.
1912 { return std::numeric_limits<result_type>::max(); }
1914 template<typename _UniformRandomNumberGenerator>
1916 operator()(_UniformRandomNumberGenerator& __urng)
1917 { return this->operator()(__urng, this->param()); }
1919 template<typename _UniformRandomNumberGenerator>
1921 operator()(_UniformRandomNumberGenerator& __urng,
1922 const param_type& __p);
1925 * @brief Inserts a %normal_distribution random number distribution
1926 * @p __x into the output stream @p __os.
1928 * @param __os An output stream.
1929 * @param __x A %normal_distribution random number distribution.
1931 * @returns The output stream with the state of @p __x inserted or in
1934 template<typename _RealType1, typename _CharT, typename _Traits>
1935 friend std::basic_ostream<_CharT, _Traits>&
1936 operator<<(std::basic_ostream<_CharT, _Traits>&,
1937 const std::normal_distribution<_RealType1>&);
1940 * @brief Extracts a %normal_distribution random number distribution
1941 * @p __x from the input stream @p __is.
1943 * @param __is An input stream.
1944 * @param __x A %normal_distribution random number generator engine.
1946 * @returns The input stream with @p __x extracted or in an error
1949 template<typename _RealType1, typename _CharT, typename _Traits>
1950 friend std::basic_istream<_CharT, _Traits>&
1951 operator>>(std::basic_istream<_CharT, _Traits>&,
1952 std::normal_distribution<_RealType1>&);
1955 param_type _M_param;
1956 result_type _M_saved;
1957 bool _M_saved_available;
1962 * @brief A lognormal_distribution random number distribution.
1964 * The formula for the normal probability mass function is
1966 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
1967 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
1970 template<typename _RealType = double>
1971 class lognormal_distribution
1973 static_assert(std::is_floating_point<_RealType>::value,
1974 "template argument not a floating point type");
1977 /** The type of the range of the distribution. */
1978 typedef _RealType result_type;
1979 /** Parameter type. */
1982 typedef lognormal_distribution<_RealType> distribution_type;
1985 param_type(_RealType __m = _RealType(0),
1986 _RealType __s = _RealType(1))
1987 : _M_m(__m), _M_s(__s)
2004 lognormal_distribution(_RealType __m = _RealType(0),
2005 _RealType __s = _RealType(1))
2006 : _M_param(__m, __s), _M_nd()
2010 lognormal_distribution(const param_type& __p)
2011 : _M_param(__p), _M_nd()
2015 * Resets the distribution state.
2026 { return _M_param.m(); }
2030 { return _M_param.s(); }
2033 * @brief Returns the parameter set of the distribution.
2037 { return _M_param; }
2040 * @brief Sets the parameter set of the distribution.
2041 * @param __param The new parameter set of the distribution.
2044 param(const param_type& __param)
2045 { _M_param = __param; }
2048 * @brief Returns the greatest lower bound value of the distribution.
2052 { return result_type(0); }
2055 * @brief Returns the least upper bound value of the distribution.
2059 { return std::numeric_limits<result_type>::max(); }
2061 template<typename _UniformRandomNumberGenerator>
2063 operator()(_UniformRandomNumberGenerator& __urng)
2064 { return this->operator()(__urng, this->param()); }
2066 template<typename _UniformRandomNumberGenerator>
2068 operator()(_UniformRandomNumberGenerator& __urng,
2069 const param_type& __p)
2070 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
2073 * @brief Inserts a %lognormal_distribution random number distribution
2074 * @p __x into the output stream @p __os.
2076 * @param __os An output stream.
2077 * @param __x A %lognormal_distribution random number distribution.
2079 * @returns The output stream with the state of @p __x inserted or in
2082 template<typename _RealType1, typename _CharT, typename _Traits>
2083 friend std::basic_ostream<_CharT, _Traits>&
2084 operator<<(std::basic_ostream<_CharT, _Traits>&,
2085 const std::lognormal_distribution<_RealType1>&);
2088 * @brief Extracts a %lognormal_distribution random number distribution
2089 * @p __x from the input stream @p __is.
2091 * @param __is An input stream.
2092 * @param __x A %lognormal_distribution random number
2095 * @returns The input stream with @p __x extracted or in an error state.
2097 template<typename _RealType1, typename _CharT, typename _Traits>
2098 friend std::basic_istream<_CharT, _Traits>&
2099 operator>>(std::basic_istream<_CharT, _Traits>&,
2100 std::lognormal_distribution<_RealType1>&);
2103 param_type _M_param;
2105 std::normal_distribution<result_type> _M_nd;
2110 * @brief A gamma continuous distribution for random numbers.
2112 * The formula for the gamma probability density function is:
2114 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2115 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2118 template<typename _RealType = double>
2119 class gamma_distribution
2121 static_assert(std::is_floating_point<_RealType>::value,
2122 "template argument not a floating point type");
2125 /** The type of the range of the distribution. */
2126 typedef _RealType result_type;
2127 /** Parameter type. */
2130 typedef gamma_distribution<_RealType> distribution_type;
2131 friend class gamma_distribution<_RealType>;
2134 param_type(_RealType __alpha_val = _RealType(1),
2135 _RealType __beta_val = _RealType(1))
2136 : _M_alpha(__alpha_val), _M_beta(__beta_val)
2138 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
2144 { return _M_alpha; }
2157 _RealType _M_malpha, _M_a2;
2162 * @brief Constructs a gamma distribution with parameters
2163 * @f$\alpha@f$ and @f$\beta@f$.
2166 gamma_distribution(_RealType __alpha_val = _RealType(1),
2167 _RealType __beta_val = _RealType(1))
2168 : _M_param(__alpha_val, __beta_val), _M_nd()
2172 gamma_distribution(const param_type& __p)
2173 : _M_param(__p), _M_nd()
2177 * @brief Resets the distribution state.
2184 * @brief Returns the @f$\alpha@f$ of the distribution.
2188 { return _M_param.alpha(); }
2191 * @brief Returns the @f$\beta@f$ of the distribution.
2195 { return _M_param.beta(); }
2198 * @brief Returns the parameter set of the distribution.
2202 { return _M_param; }
2205 * @brief Sets the parameter set of the distribution.
2206 * @param __param The new parameter set of the distribution.
2209 param(const param_type& __param)
2210 { _M_param = __param; }
2213 * @brief Returns the greatest lower bound value of the distribution.
2217 { return result_type(0); }
2220 * @brief Returns the least upper bound value of the distribution.
2224 { return std::numeric_limits<result_type>::max(); }
2226 template<typename _UniformRandomNumberGenerator>
2228 operator()(_UniformRandomNumberGenerator& __urng)
2229 { return this->operator()(__urng, this->param()); }
2231 template<typename _UniformRandomNumberGenerator>
2233 operator()(_UniformRandomNumberGenerator& __urng,
2234 const param_type& __p);
2237 * @brief Inserts a %gamma_distribution random number distribution
2238 * @p __x into the output stream @p __os.
2240 * @param __os An output stream.
2241 * @param __x A %gamma_distribution random number distribution.
2243 * @returns The output stream with the state of @p __x inserted or in
2246 template<typename _RealType1, typename _CharT, typename _Traits>
2247 friend std::basic_ostream<_CharT, _Traits>&
2248 operator<<(std::basic_ostream<_CharT, _Traits>&,
2249 const std::gamma_distribution<_RealType1>&);
2252 * @brief Extracts a %gamma_distribution random number distribution
2253 * @p __x from the input stream @p __is.
2255 * @param __is An input stream.
2256 * @param __x A %gamma_distribution random number generator engine.
2258 * @returns The input stream with @p __x extracted or in an error state.
2260 template<typename _RealType1, typename _CharT, typename _Traits>
2261 friend std::basic_istream<_CharT, _Traits>&
2262 operator>>(std::basic_istream<_CharT, _Traits>&,
2263 std::gamma_distribution<_RealType1>&);
2266 param_type _M_param;
2268 std::normal_distribution<result_type> _M_nd;
2273 * @brief A chi_squared_distribution random number distribution.
2275 * The formula for the normal probability mass function is
2276 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2278 template<typename _RealType = double>
2279 class chi_squared_distribution
2281 static_assert(std::is_floating_point<_RealType>::value,
2282 "template argument not a floating point type");
2285 /** The type of the range of the distribution. */
2286 typedef _RealType result_type;
2287 /** Parameter type. */
2290 typedef chi_squared_distribution<_RealType> distribution_type;
2293 param_type(_RealType __n = _RealType(1))
2306 chi_squared_distribution(_RealType __n = _RealType(1))
2307 : _M_param(__n), _M_gd(__n / 2)
2311 chi_squared_distribution(const param_type& __p)
2312 : _M_param(__p), _M_gd(__p.n() / 2)
2316 * @brief Resets the distribution state.
2327 { return _M_param.n(); }
2330 * @brief Returns the parameter set of the distribution.
2334 { return _M_param; }
2337 * @brief Sets the parameter set of the distribution.
2338 * @param __param The new parameter set of the distribution.
2341 param(const param_type& __param)
2342 { _M_param = __param; }
2345 * @brief Returns the greatest lower bound value of the distribution.
2349 { return result_type(0); }
2352 * @brief Returns the least upper bound value of the distribution.
2356 { return std::numeric_limits<result_type>::max(); }
2358 template<typename _UniformRandomNumberGenerator>
2360 operator()(_UniformRandomNumberGenerator& __urng)
2361 { return 2 * _M_gd(__urng); }
2363 template<typename _UniformRandomNumberGenerator>
2365 operator()(_UniformRandomNumberGenerator& __urng,
2366 const param_type& __p)
2368 typedef typename std::gamma_distribution<result_type>::param_type
2370 return 2 * _M_gd(__urng, param_type(__p.n() / 2));
2374 * @brief Inserts a %chi_squared_distribution random number distribution
2375 * @p __x into the output stream @p __os.
2377 * @param __os An output stream.
2378 * @param __x A %chi_squared_distribution random number distribution.
2380 * @returns The output stream with the state of @p __x inserted or in
2383 template<typename _RealType1, typename _CharT, typename _Traits>
2384 friend std::basic_ostream<_CharT, _Traits>&
2385 operator<<(std::basic_ostream<_CharT, _Traits>&,
2386 const std::chi_squared_distribution<_RealType1>&);
2389 * @brief Extracts a %chi_squared_distribution random number distribution
2390 * @p __x from the input stream @p __is.
2392 * @param __is An input stream.
2393 * @param __x A %chi_squared_distribution random number
2396 * @returns The input stream with @p __x extracted or in an error state.
2398 template<typename _RealType1, typename _CharT, typename _Traits>
2399 friend std::basic_istream<_CharT, _Traits>&
2400 operator>>(std::basic_istream<_CharT, _Traits>&,
2401 std::chi_squared_distribution<_RealType1>&);
2404 param_type _M_param;
2406 std::gamma_distribution<result_type> _M_gd;
2411 * @brief A cauchy_distribution random number distribution.
2413 * The formula for the normal probability mass function is
2414 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2416 template<typename _RealType = double>
2417 class cauchy_distribution
2419 static_assert(std::is_floating_point<_RealType>::value,
2420 "template argument not a floating point type");
2423 /** The type of the range of the distribution. */
2424 typedef _RealType result_type;
2425 /** Parameter type. */
2428 typedef cauchy_distribution<_RealType> distribution_type;
2431 param_type(_RealType __a = _RealType(0),
2432 _RealType __b = _RealType(1))
2433 : _M_a(__a), _M_b(__b)
2450 cauchy_distribution(_RealType __a = _RealType(0),
2451 _RealType __b = _RealType(1))
2452 : _M_param(__a, __b)
2456 cauchy_distribution(const param_type& __p)
2461 * @brief Resets the distribution state.
2472 { return _M_param.a(); }
2476 { return _M_param.b(); }
2479 * @brief Returns the parameter set of the distribution.
2483 { return _M_param; }
2486 * @brief Sets the parameter set of the distribution.
2487 * @param __param The new parameter set of the distribution.
2490 param(const param_type& __param)
2491 { _M_param = __param; }
2494 * @brief Returns the greatest lower bound value of the distribution.
2498 { return std::numeric_limits<result_type>::min(); }
2501 * @brief Returns the least upper bound value of the distribution.
2505 { return std::numeric_limits<result_type>::max(); }
2507 template<typename _UniformRandomNumberGenerator>
2509 operator()(_UniformRandomNumberGenerator& __urng)
2510 { return this->operator()(__urng, this->param()); }
2512 template<typename _UniformRandomNumberGenerator>
2514 operator()(_UniformRandomNumberGenerator& __urng,
2515 const param_type& __p);
2518 param_type _M_param;
2522 * @brief Inserts a %cauchy_distribution random number distribution
2523 * @p __x into the output stream @p __os.
2525 * @param __os An output stream.
2526 * @param __x A %cauchy_distribution random number distribution.
2528 * @returns The output stream with the state of @p __x inserted or in
2531 template<typename _RealType, typename _CharT, typename _Traits>
2532 std::basic_ostream<_CharT, _Traits>&
2533 operator<<(std::basic_ostream<_CharT, _Traits>&,
2534 const std::cauchy_distribution<_RealType>&);
2537 * @brief Extracts a %cauchy_distribution random number distribution
2538 * @p __x from the input stream @p __is.
2540 * @param __is An input stream.
2541 * @param __x A %cauchy_distribution random number
2544 * @returns The input stream with @p __x extracted or in an error state.
2546 template<typename _RealType, typename _CharT, typename _Traits>
2547 std::basic_istream<_CharT, _Traits>&
2548 operator>>(std::basic_istream<_CharT, _Traits>&,
2549 std::cauchy_distribution<_RealType>&);
2553 * @brief A fisher_f_distribution random number distribution.
2555 * The formula for the normal probability mass function is
2557 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2558 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2559 * (1 + \frac{mx}{n})^{-(m+n)/2}
2562 template<typename _RealType = double>
2563 class fisher_f_distribution
2565 static_assert(std::is_floating_point<_RealType>::value,
2566 "template argument not a floating point type");
2569 /** The type of the range of the distribution. */
2570 typedef _RealType result_type;
2571 /** Parameter type. */
2574 typedef fisher_f_distribution<_RealType> distribution_type;
2577 param_type(_RealType __m = _RealType(1),
2578 _RealType __n = _RealType(1))
2579 : _M_m(__m), _M_n(__n)
2596 fisher_f_distribution(_RealType __m = _RealType(1),
2597 _RealType __n = _RealType(1))
2598 : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2)
2602 fisher_f_distribution(const param_type& __p)
2603 : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2)
2607 * @brief Resets the distribution state.
2621 { return _M_param.m(); }
2625 { return _M_param.n(); }
2628 * @brief Returns the parameter set of the distribution.
2632 { return _M_param; }
2635 * @brief Sets the parameter set of the distribution.
2636 * @param __param The new parameter set of the distribution.
2639 param(const param_type& __param)
2640 { _M_param = __param; }
2643 * @brief Returns the greatest lower bound value of the distribution.
2647 { return result_type(0); }
2650 * @brief Returns the least upper bound value of the distribution.
2654 { return std::numeric_limits<result_type>::max(); }
2656 template<typename _UniformRandomNumberGenerator>
2658 operator()(_UniformRandomNumberGenerator& __urng)
2659 { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); }
2661 template<typename _UniformRandomNumberGenerator>
2663 operator()(_UniformRandomNumberGenerator& __urng,
2664 const param_type& __p)
2666 typedef typename std::gamma_distribution<result_type>::param_type
2668 return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n())
2669 / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m()));
2673 * @brief Inserts a %fisher_f_distribution random number distribution
2674 * @p __x into the output stream @p __os.
2676 * @param __os An output stream.
2677 * @param __x A %fisher_f_distribution random number distribution.
2679 * @returns The output stream with the state of @p __x inserted or in
2682 template<typename _RealType1, typename _CharT, typename _Traits>
2683 friend std::basic_ostream<_CharT, _Traits>&
2684 operator<<(std::basic_ostream<_CharT, _Traits>&,
2685 const std::fisher_f_distribution<_RealType1>&);
2688 * @brief Extracts a %fisher_f_distribution random number distribution
2689 * @p __x from the input stream @p __is.
2691 * @param __is An input stream.
2692 * @param __x A %fisher_f_distribution random number
2695 * @returns The input stream with @p __x extracted or in an error state.
2697 template<typename _RealType1, typename _CharT, typename _Traits>
2698 friend std::basic_istream<_CharT, _Traits>&
2699 operator>>(std::basic_istream<_CharT, _Traits>&,
2700 std::fisher_f_distribution<_RealType1>&);
2703 param_type _M_param;
2705 std::gamma_distribution<result_type> _M_gd_x, _M_gd_y;
2710 * @brief A student_t_distribution random number distribution.
2712 * The formula for the normal probability mass function is:
2714 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
2715 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
2718 template<typename _RealType = double>
2719 class student_t_distribution
2721 static_assert(std::is_floating_point<_RealType>::value,
2722 "template argument not a floating point type");
2725 /** The type of the range of the distribution. */
2726 typedef _RealType result_type;
2727 /** Parameter type. */
2730 typedef student_t_distribution<_RealType> distribution_type;
2733 param_type(_RealType __n = _RealType(1))
2746 student_t_distribution(_RealType __n = _RealType(1))
2747 : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2)
2751 student_t_distribution(const param_type& __p)
2752 : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2)
2756 * @brief Resets the distribution state.
2770 { return _M_param.n(); }
2773 * @brief Returns the parameter set of the distribution.
2777 { return _M_param; }
2780 * @brief Sets the parameter set of the distribution.
2781 * @param __param The new parameter set of the distribution.
2784 param(const param_type& __param)
2785 { _M_param = __param; }
2788 * @brief Returns the greatest lower bound value of the distribution.
2792 { return std::numeric_limits<result_type>::min(); }
2795 * @brief Returns the least upper bound value of the distribution.
2799 { return std::numeric_limits<result_type>::max(); }
2801 template<typename _UniformRandomNumberGenerator>
2803 operator()(_UniformRandomNumberGenerator& __urng)
2804 { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); }
2806 template<typename _UniformRandomNumberGenerator>
2808 operator()(_UniformRandomNumberGenerator& __urng,
2809 const param_type& __p)
2811 typedef typename std::gamma_distribution<result_type>::param_type
2814 const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2));
2815 return _M_nd(__urng) * std::sqrt(__p.n() / __g);
2819 * @brief Inserts a %student_t_distribution random number distribution
2820 * @p __x into the output stream @p __os.
2822 * @param __os An output stream.
2823 * @param __x A %student_t_distribution random number distribution.
2825 * @returns The output stream with the state of @p __x inserted or in
2828 template<typename _RealType1, typename _CharT, typename _Traits>
2829 friend std::basic_ostream<_CharT, _Traits>&
2830 operator<<(std::basic_ostream<_CharT, _Traits>&,
2831 const std::student_t_distribution<_RealType1>&);
2834 * @brief Extracts a %student_t_distribution random number distribution
2835 * @p __x from the input stream @p __is.
2837 * @param __is An input stream.
2838 * @param __x A %student_t_distribution random number
2841 * @returns The input stream with @p __x extracted or in an error state.
2843 template<typename _RealType1, typename _CharT, typename _Traits>
2844 friend std::basic_istream<_CharT, _Traits>&
2845 operator>>(std::basic_istream<_CharT, _Traits>&,
2846 std::student_t_distribution<_RealType1>&);
2849 param_type _M_param;
2851 std::normal_distribution<result_type> _M_nd;
2852 std::gamma_distribution<result_type> _M_gd;
2855 /* @} */ // group std_random_distributions_normal
2858 * @addtogroup std_random_distributions_bernoulli Bernoulli Distributions
2859 * @ingroup std_random_distributions
2864 * @brief A Bernoulli random number distribution.
2866 * Generates a sequence of true and false values with likelihood @f$p@f$
2867 * that true will come up and @f$(1 - p)@f$ that false will appear.
2869 class bernoulli_distribution
2872 /** The type of the range of the distribution. */
2873 typedef bool result_type;
2874 /** Parameter type. */
2877 typedef bernoulli_distribution distribution_type;
2880 param_type(double __p = 0.5)
2883 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
2896 * @brief Constructs a Bernoulli distribution with likelihood @p p.
2898 * @param __p [IN] The likelihood of a true result being returned.
2899 * Must be in the interval @f$[0, 1]@f$.
2902 bernoulli_distribution(double __p = 0.5)
2907 bernoulli_distribution(const param_type& __p)
2912 * @brief Resets the distribution state.
2914 * Does nothing for a Bernoulli distribution.
2920 * @brief Returns the @p p parameter of the distribution.
2924 { return _M_param.p(); }
2927 * @brief Returns the parameter set of the distribution.
2931 { return _M_param; }
2934 * @brief Sets the parameter set of the distribution.
2935 * @param __param The new parameter set of the distribution.
2938 param(const param_type& __param)
2939 { _M_param = __param; }
2942 * @brief Returns the greatest lower bound value of the distribution.
2946 { return std::numeric_limits<result_type>::min(); }
2949 * @brief Returns the least upper bound value of the distribution.
2953 { return std::numeric_limits<result_type>::max(); }
2956 * @brief Returns the next value in the Bernoullian sequence.
2958 template<typename _UniformRandomNumberGenerator>
2960 operator()(_UniformRandomNumberGenerator& __urng)
2961 { return this->operator()(__urng, this->param()); }
2963 template<typename _UniformRandomNumberGenerator>
2965 operator()(_UniformRandomNumberGenerator& __urng,
2966 const param_type& __p)
2968 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
2970 if ((__aurng() - __aurng.min())
2971 < __p.p() * (__aurng.max() - __aurng.min()))
2977 param_type _M_param;
2981 * @brief Inserts a %bernoulli_distribution random number distribution
2982 * @p __x into the output stream @p __os.
2984 * @param __os An output stream.
2985 * @param __x A %bernoulli_distribution random number distribution.
2987 * @returns The output stream with the state of @p __x inserted or in
2990 template<typename _CharT, typename _Traits>
2991 std::basic_ostream<_CharT, _Traits>&
2992 operator<<(std::basic_ostream<_CharT, _Traits>&,
2993 const std::bernoulli_distribution&);
2996 * @brief Extracts a %bernoulli_distribution random number distribution
2997 * @p __x from the input stream @p __is.
2999 * @param __is An input stream.
3000 * @param __x A %bernoulli_distribution random number generator engine.
3002 * @returns The input stream with @p __x extracted or in an error state.
3004 template<typename _CharT, typename _Traits>
3005 std::basic_istream<_CharT, _Traits>&
3006 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3007 std::bernoulli_distribution& __x)
3011 __x.param(bernoulli_distribution::param_type(__p));
3017 * @brief A discrete binomial random number distribution.
3019 * The formula for the binomial probability density function is
3020 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3021 * and @f$p@f$ are the parameters of the distribution.
3023 template<typename _IntType = int>
3024 class binomial_distribution
3026 static_assert(std::is_integral<_IntType>::value,
3027 "template argument not an integral type");
3030 /** The type of the range of the distribution. */
3031 typedef _IntType result_type;
3032 /** Parameter type. */
3035 typedef binomial_distribution<_IntType> distribution_type;
3036 friend class binomial_distribution<_IntType>;
3039 param_type(_IntType __t = _IntType(1), double __p = 0.5)
3040 : _M_t(__t), _M_p(__p)
3042 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3064 #if _GLIBCXX_USE_C99_MATH_TR1
3065 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3066 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3071 // constructors and member function
3073 binomial_distribution(_IntType __t = _IntType(1),
3075 : _M_param(__t, __p), _M_nd()
3079 binomial_distribution(const param_type& __p)
3080 : _M_param(__p), _M_nd()
3084 * @brief Resets the distribution state.
3091 * @brief Returns the distribution @p t parameter.
3095 { return _M_param.t(); }
3098 * @brief Returns the distribution @p p parameter.
3102 { return _M_param.p(); }
3105 * @brief Returns the parameter set of the distribution.
3109 { return _M_param; }
3112 * @brief Sets the parameter set of the distribution.
3113 * @param __param The new parameter set of the distribution.
3116 param(const param_type& __param)
3117 { _M_param = __param; }
3120 * @brief Returns the greatest lower bound value of the distribution.
3127 * @brief Returns the least upper bound value of the distribution.
3131 { return _M_param.t(); }
3133 template<typename _UniformRandomNumberGenerator>
3135 operator()(_UniformRandomNumberGenerator& __urng)
3136 { return this->operator()(__urng, this->param()); }
3138 template<typename _UniformRandomNumberGenerator>
3140 operator()(_UniformRandomNumberGenerator& __urng,
3141 const param_type& __p);
3144 * @brief Inserts a %binomial_distribution random number distribution
3145 * @p __x into the output stream @p __os.
3147 * @param __os An output stream.
3148 * @param __x A %binomial_distribution random number distribution.
3150 * @returns The output stream with the state of @p __x inserted or in
3153 template<typename _IntType1,
3154 typename _CharT, typename _Traits>
3155 friend std::basic_ostream<_CharT, _Traits>&
3156 operator<<(std::basic_ostream<_CharT, _Traits>&,
3157 const std::binomial_distribution<_IntType1>&);
3160 * @brief Extracts a %binomial_distribution random number distribution
3161 * @p __x from the input stream @p __is.
3163 * @param __is An input stream.
3164 * @param __x A %binomial_distribution random number generator engine.
3166 * @returns The input stream with @p __x extracted or in an error
3169 template<typename _IntType1,
3170 typename _CharT, typename _Traits>
3171 friend std::basic_istream<_CharT, _Traits>&
3172 operator>>(std::basic_istream<_CharT, _Traits>&,
3173 std::binomial_distribution<_IntType1>&);
3176 template<typename _UniformRandomNumberGenerator>
3178 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3180 param_type _M_param;
3182 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3183 std::normal_distribution<double> _M_nd;
3188 * @brief A discrete geometric random number distribution.
3190 * The formula for the geometric probability density function is
3191 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the
3194 template<typename _IntType = int>
3195 class geometric_distribution
3197 static_assert(std::is_integral<_IntType>::value,
3198 "template argument not an integral type");
3201 /** The type of the range of the distribution. */
3202 typedef _IntType result_type;
3203 /** Parameter type. */
3206 typedef geometric_distribution<_IntType> distribution_type;
3207 friend class geometric_distribution<_IntType>;
3210 param_type(double __p = 0.5)
3213 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0)
3225 { _M_log_p = std::log(_M_p); }
3232 // constructors and member function
3234 geometric_distribution(double __p = 0.5)
3239 geometric_distribution(const param_type& __p)
3244 * @brief Resets the distribution state.
3246 * Does nothing for the geometric distribution.
3252 * @brief Returns the distribution parameter @p p.
3256 { return _M_param.p(); }
3259 * @brief Returns the parameter set of the distribution.
3263 { return _M_param; }
3266 * @brief Sets the parameter set of the distribution.
3267 * @param __param The new parameter set of the distribution.
3270 param(const param_type& __param)
3271 { _M_param = __param; }
3274 * @brief Returns the greatest lower bound value of the distribution.
3281 * @brief Returns the least upper bound value of the distribution.
3285 { return std::numeric_limits<result_type>::max(); }
3287 template<typename _UniformRandomNumberGenerator>
3289 operator()(_UniformRandomNumberGenerator& __urng)
3290 { return this->operator()(__urng, this->param()); }
3292 template<typename _UniformRandomNumberGenerator>
3294 operator()(_UniformRandomNumberGenerator& __urng,
3295 const param_type& __p);
3298 param_type _M_param;
3302 * @brief Inserts a %geometric_distribution random number distribution
3303 * @p __x into the output stream @p __os.
3305 * @param __os An output stream.
3306 * @param __x A %geometric_distribution random number distribution.
3308 * @returns The output stream with the state of @p __x inserted or in
3311 template<typename _IntType,
3312 typename _CharT, typename _Traits>
3313 std::basic_ostream<_CharT, _Traits>&
3314 operator<<(std::basic_ostream<_CharT, _Traits>&,
3315 const std::geometric_distribution<_IntType>&);
3318 * @brief Extracts a %geometric_distribution random number distribution
3319 * @p __x from the input stream @p __is.
3321 * @param __is An input stream.
3322 * @param __x A %geometric_distribution random number generator engine.
3324 * @returns The input stream with @p __x extracted or in an error state.
3326 template<typename _IntType,
3327 typename _CharT, typename _Traits>
3328 std::basic_istream<_CharT, _Traits>&
3329 operator>>(std::basic_istream<_CharT, _Traits>&,
3330 std::geometric_distribution<_IntType>&);
3334 * @brief A negative_binomial_distribution random number distribution.
3336 * The formula for the negative binomial probability mass function is
3337 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3338 * and @f$p@f$ are the parameters of the distribution.
3340 template<typename _IntType = int>
3341 class negative_binomial_distribution
3343 static_assert(std::is_integral<_IntType>::value,
3344 "template argument not an integral type");
3347 /** The type of the range of the distribution. */
3348 typedef _IntType result_type;
3349 /** Parameter type. */
3352 typedef negative_binomial_distribution<_IntType> distribution_type;
3355 param_type(_IntType __k = 1, double __p = 0.5)
3356 : _M_k(__k), _M_p(__p)
3373 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
3374 : _M_param(__k, __p), _M_gd(__k, __p / (1.0 - __p))
3378 negative_binomial_distribution(const param_type& __p)
3379 : _M_param(__p), _M_gd(__p.k(), __p.p() / (1.0 - __p.p()))
3383 * @brief Resets the distribution state.
3390 * @brief Return the @f$k@f$ parameter of the distribution.
3394 { return _M_param.k(); }
3397 * @brief Return the @f$p@f$ parameter of the distribution.
3401 { return _M_param.p(); }
3404 * @brief Returns the parameter set of the distribution.
3408 { return _M_param; }
3411 * @brief Sets the parameter set of the distribution.
3412 * @param __param The new parameter set of the distribution.
3415 param(const param_type& __param)
3416 { _M_param = __param; }
3419 * @brief Returns the greatest lower bound value of the distribution.
3423 { return result_type(0); }
3426 * @brief Returns the least upper bound value of the distribution.
3430 { return std::numeric_limits<result_type>::max(); }
3432 template<typename _UniformRandomNumberGenerator>
3434 operator()(_UniformRandomNumberGenerator& __urng);
3436 template<typename _UniformRandomNumberGenerator>
3438 operator()(_UniformRandomNumberGenerator& __urng,
3439 const param_type& __p);
3442 * @brief Inserts a %negative_binomial_distribution random
3443 * number distribution @p __x into the output stream @p __os.
3445 * @param __os An output stream.
3446 * @param __x A %negative_binomial_distribution random number
3449 * @returns The output stream with the state of @p __x inserted or in
3452 template<typename _IntType1, typename _CharT, typename _Traits>
3453 friend std::basic_ostream<_CharT, _Traits>&
3454 operator<<(std::basic_ostream<_CharT, _Traits>&,
3455 const std::negative_binomial_distribution<_IntType1>&);
3458 * @brief Extracts a %negative_binomial_distribution random number
3459 * distribution @p __x from the input stream @p __is.
3461 * @param __is An input stream.
3462 * @param __x A %negative_binomial_distribution random number
3465 * @returns The input stream with @p __x extracted or in an error state.
3467 template<typename _IntType1, typename _CharT, typename _Traits>
3468 friend std::basic_istream<_CharT, _Traits>&
3469 operator>>(std::basic_istream<_CharT, _Traits>&,
3470 std::negative_binomial_distribution<_IntType1>&);
3473 param_type _M_param;
3475 std::gamma_distribution<double> _M_gd;
3478 /* @} */ // group std_random_distributions_bernoulli
3481 * @addtogroup std_random_distributions_poisson Poisson Distributions
3482 * @ingroup std_random_distributions
3487 * @brief A discrete Poisson random number distribution.
3489 * The formula for the Poisson probability density function is
3490 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3491 * parameter of the distribution.
3493 template<typename _IntType = int>
3494 class poisson_distribution
3496 static_assert(std::is_integral<_IntType>::value,
3497 "template argument not an integral type");
3500 /** The type of the range of the distribution. */
3501 typedef _IntType result_type;
3502 /** Parameter type. */
3505 typedef poisson_distribution<_IntType> distribution_type;
3506 friend class poisson_distribution<_IntType>;
3509 param_type(double __mean = 1.0)
3512 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
3521 // Hosts either log(mean) or the threshold of the simple method.
3528 #if _GLIBCXX_USE_C99_MATH_TR1
3529 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
3533 // constructors and member function
3535 poisson_distribution(double __mean = 1.0)
3536 : _M_param(__mean), _M_nd()
3540 poisson_distribution(const param_type& __p)
3541 : _M_param(__p), _M_nd()
3545 * @brief Resets the distribution state.
3552 * @brief Returns the distribution parameter @p mean.
3556 { return _M_param.mean(); }
3559 * @brief Returns the parameter set of the distribution.
3563 { return _M_param; }
3566 * @brief Sets the parameter set of the distribution.
3567 * @param __param The new parameter set of the distribution.
3570 param(const param_type& __param)
3571 { _M_param = __param; }
3574 * @brief Returns the greatest lower bound value of the distribution.
3581 * @brief Returns the least upper bound value of the distribution.
3585 { return std::numeric_limits<result_type>::max(); }
3587 template<typename _UniformRandomNumberGenerator>
3589 operator()(_UniformRandomNumberGenerator& __urng)
3590 { return this->operator()(__urng, this->param()); }
3592 template<typename _UniformRandomNumberGenerator>
3594 operator()(_UniformRandomNumberGenerator& __urng,
3595 const param_type& __p);
3598 * @brief Inserts a %poisson_distribution random number distribution
3599 * @p __x into the output stream @p __os.
3601 * @param __os An output stream.
3602 * @param __x A %poisson_distribution random number distribution.
3604 * @returns The output stream with the state of @p __x inserted or in
3607 template<typename _IntType1, typename _CharT, typename _Traits>
3608 friend std::basic_ostream<_CharT, _Traits>&
3609 operator<<(std::basic_ostream<_CharT, _Traits>&,
3610 const std::poisson_distribution<_IntType1>&);
3613 * @brief Extracts a %poisson_distribution random number distribution
3614 * @p __x from the input stream @p __is.
3616 * @param __is An input stream.
3617 * @param __x A %poisson_distribution random number generator engine.
3619 * @returns The input stream with @p __x extracted or in an error
3622 template<typename _IntType1, typename _CharT, typename _Traits>
3623 friend std::basic_istream<_CharT, _Traits>&
3624 operator>>(std::basic_istream<_CharT, _Traits>&,
3625 std::poisson_distribution<_IntType1>&);
3628 param_type _M_param;
3630 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3631 std::normal_distribution<double> _M_nd;
3635 * @brief An exponential continuous distribution for random numbers.
3637 * The formula for the exponential probability density function is
3638 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
3640 * <table border=1 cellpadding=10 cellspacing=0>
3641 * <caption align=top>Distribution Statistics</caption>
3642 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
3643 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
3644 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
3645 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
3646 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
3649 template<typename _RealType = double>
3650 class exponential_distribution
3652 static_assert(std::is_floating_point<_RealType>::value,
3653 "template argument not a floating point type");
3656 /** The type of the range of the distribution. */
3657 typedef _RealType result_type;
3658 /** Parameter type. */
3661 typedef exponential_distribution<_RealType> distribution_type;
3664 param_type(_RealType __lambda = _RealType(1))
3665 : _M_lambda(__lambda)
3667 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
3672 { return _M_lambda; }
3675 _RealType _M_lambda;
3680 * @brief Constructs an exponential distribution with inverse scale
3681 * parameter @f$\lambda@f$.
3684 exponential_distribution(const result_type& __lambda = result_type(1))
3685 : _M_param(__lambda)
3689 exponential_distribution(const param_type& __p)
3694 * @brief Resets the distribution state.
3696 * Has no effect on exponential distributions.
3702 * @brief Returns the inverse scale parameter of the distribution.
3706 { return _M_param.lambda(); }
3709 * @brief Returns the parameter set of the distribution.
3713 { return _M_param; }
3716 * @brief Sets the parameter set of the distribution.
3717 * @param __param The new parameter set of the distribution.
3720 param(const param_type& __param)
3721 { _M_param = __param; }
3724 * @brief Returns the greatest lower bound value of the distribution.
3728 { return result_type(0); }
3731 * @brief Returns the least upper bound value of the distribution.
3735 { return std::numeric_limits<result_type>::max(); }
3737 template<typename _UniformRandomNumberGenerator>
3739 operator()(_UniformRandomNumberGenerator& __urng)
3740 { return this->operator()(__urng, this->param()); }
3742 template<typename _UniformRandomNumberGenerator>
3744 operator()(_UniformRandomNumberGenerator& __urng,
3745 const param_type& __p)
3747 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
3749 return -std::log(__aurng()) / __p.lambda();
3753 param_type _M_param;
3757 * @brief Inserts a %exponential_distribution random number distribution
3758 * @p __x into the output stream @p __os.
3760 * @param __os An output stream.
3761 * @param __x A %exponential_distribution random number distribution.
3763 * @returns The output stream with the state of @p __x inserted or in
3766 template<typename _RealType, typename _CharT, typename _Traits>
3767 std::basic_ostream<_CharT, _Traits>&
3768 operator<<(std::basic_ostream<_CharT, _Traits>&,
3769 const std::exponential_distribution<_RealType>&);
3772 * @brief Extracts a %exponential_distribution random number distribution
3773 * @p __x from the input stream @p __is.
3775 * @param __is An input stream.
3776 * @param __x A %exponential_distribution random number
3779 * @returns The input stream with @p __x extracted or in an error state.
3781 template<typename _RealType, typename _CharT, typename _Traits>
3782 std::basic_istream<_CharT, _Traits>&
3783 operator>>(std::basic_istream<_CharT, _Traits>&,
3784 std::exponential_distribution<_RealType>&);
3788 * @brief A weibull_distribution random number distribution.
3790 * The formula for the normal probability density function is:
3792 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
3793 * \exp{(-(\frac{x}{\beta})^\alpha)}
3796 template<typename _RealType = double>
3797 class weibull_distribution
3799 static_assert(std::is_floating_point<_RealType>::value,
3800 "template argument not a floating point type");
3803 /** The type of the range of the distribution. */
3804 typedef _RealType result_type;
3805 /** Parameter type. */
3808 typedef weibull_distribution<_RealType> distribution_type;
3811 param_type(_RealType __a = _RealType(1),
3812 _RealType __b = _RealType(1))
3813 : _M_a(__a), _M_b(__b)
3830 weibull_distribution(_RealType __a = _RealType(1),
3831 _RealType __b = _RealType(1))
3832 : _M_param(__a, __b)
3836 weibull_distribution(const param_type& __p)
3841 * @brief Resets the distribution state.
3848 * @brief Return the @f$a@f$ parameter of the distribution.
3852 { return _M_param.a(); }
3855 * @brief Return the @f$b@f$ parameter of the distribution.
3859 { return _M_param.b(); }
3862 * @brief Returns the parameter set of the distribution.
3866 { return _M_param; }
3869 * @brief Sets the parameter set of the distribution.
3870 * @param __param The new parameter set of the distribution.
3873 param(const param_type& __param)
3874 { _M_param = __param; }
3877 * @brief Returns the greatest lower bound value of the distribution.
3881 { return result_type(0); }
3884 * @brief Returns the least upper bound value of the distribution.
3888 { return std::numeric_limits<result_type>::max(); }
3890 template<typename _UniformRandomNumberGenerator>
3892 operator()(_UniformRandomNumberGenerator& __urng)
3893 { return this->operator()(__urng, this->param()); }
3895 template<typename _UniformRandomNumberGenerator>
3897 operator()(_UniformRandomNumberGenerator& __urng,
3898 const param_type& __p);
3901 param_type _M_param;
3905 * @brief Inserts a %weibull_distribution random number distribution
3906 * @p __x into the output stream @p __os.
3908 * @param __os An output stream.
3909 * @param __x A %weibull_distribution random number distribution.
3911 * @returns The output stream with the state of @p __x inserted or in
3914 template<typename _RealType, typename _CharT, typename _Traits>
3915 std::basic_ostream<_CharT, _Traits>&
3916 operator<<(std::basic_ostream<_CharT, _Traits>&,
3917 const std::weibull_distribution<_RealType>&);
3920 * @brief Extracts a %weibull_distribution random number distribution
3921 * @p __x from the input stream @p __is.
3923 * @param __is An input stream.
3924 * @param __x A %weibull_distribution random number
3927 * @returns The input stream with @p __x extracted or in an error state.
3929 template<typename _RealType, typename _CharT, typename _Traits>
3930 std::basic_istream<_CharT, _Traits>&
3931 operator>>(std::basic_istream<_CharT, _Traits>&,
3932 std::weibull_distribution<_RealType>&);
3936 * @brief A extreme_value_distribution random number distribution.
3938 * The formula for the normal probability mass function is
3940 * p(x|a,b) = \frac{1}{b}
3941 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
3944 template<typename _RealType = double>
3945 class extreme_value_distribution
3947 static_assert(std::is_floating_point<_RealType>::value,
3948 "template argument not a floating point type");
3951 /** The type of the range of the distribution. */
3952 typedef _RealType result_type;
3953 /** Parameter type. */
3956 typedef extreme_value_distribution<_RealType> distribution_type;
3959 param_type(_RealType __a = _RealType(0),
3960 _RealType __b = _RealType(1))
3961 : _M_a(__a), _M_b(__b)
3978 extreme_value_distribution(_RealType __a = _RealType(0),
3979 _RealType __b = _RealType(1))
3980 : _M_param(__a, __b)
3984 extreme_value_distribution(const param_type& __p)
3989 * @brief Resets the distribution state.
3996 * @brief Return the @f$a@f$ parameter of the distribution.
4000 { return _M_param.a(); }
4003 * @brief Return the @f$b@f$ parameter of the distribution.
4007 { return _M_param.b(); }
4010 * @brief Returns the parameter set of the distribution.
4014 { return _M_param; }
4017 * @brief Sets the parameter set of the distribution.
4018 * @param __param The new parameter set of the distribution.
4021 param(const param_type& __param)
4022 { _M_param = __param; }
4025 * @brief Returns the greatest lower bound value of the distribution.
4029 { return std::numeric_limits<result_type>::min(); }
4032 * @brief Returns the least upper bound value of the distribution.
4036 { return std::numeric_limits<result_type>::max(); }
4038 template<typename _UniformRandomNumberGenerator>
4040 operator()(_UniformRandomNumberGenerator& __urng)
4041 { return this->operator()(__urng, this->param()); }
4043 template<typename _UniformRandomNumberGenerator>
4045 operator()(_UniformRandomNumberGenerator& __urng,
4046 const param_type& __p);
4049 param_type _M_param;
4053 * @brief Inserts a %extreme_value_distribution random number distribution
4054 * @p __x into the output stream @p __os.
4056 * @param __os An output stream.
4057 * @param __x A %extreme_value_distribution random number distribution.
4059 * @returns The output stream with the state of @p __x inserted or in
4062 template<typename _RealType, typename _CharT, typename _Traits>
4063 std::basic_ostream<_CharT, _Traits>&
4064 operator<<(std::basic_ostream<_CharT, _Traits>&,
4065 const std::extreme_value_distribution<_RealType>&);
4068 * @brief Extracts a %extreme_value_distribution random number
4069 * distribution @p __x from the input stream @p __is.
4071 * @param __is An input stream.
4072 * @param __x A %extreme_value_distribution random number
4075 * @returns The input stream with @p __x extracted or in an error state.
4077 template<typename _RealType, typename _CharT, typename _Traits>
4078 std::basic_istream<_CharT, _Traits>&
4079 operator>>(std::basic_istream<_CharT, _Traits>&,
4080 std::extreme_value_distribution<_RealType>&);
4084 * @brief A discrete_distribution random number distribution.
4086 * The formula for the discrete probability mass function is
4089 template<typename _IntType = int>
4090 class discrete_distribution
4092 static_assert(std::is_integral<_IntType>::value,
4093 "template argument not an integral type");
4096 /** The type of the range of the distribution. */
4097 typedef _IntType result_type;
4098 /** Parameter type. */
4101 typedef discrete_distribution<_IntType> distribution_type;
4102 friend class discrete_distribution<_IntType>;
4105 : _M_prob(), _M_cp()
4106 { _M_initialize(); }
4108 template<typename _InputIterator>
4109 param_type(_InputIterator __wbegin,
4110 _InputIterator __wend)
4111 : _M_prob(__wbegin, __wend), _M_cp()
4112 { _M_initialize(); }
4114 param_type(initializer_list<double> __wil)
4115 : _M_prob(__wil.begin(), __wil.end()), _M_cp()
4116 { _M_initialize(); }
4118 template<typename _Func>
4119 param_type(size_t __nw, double __xmin, double __xmax,
4123 probabilities() const
4130 std::vector<double> _M_prob;
4131 std::vector<double> _M_cp;
4134 discrete_distribution()
4138 template<typename _InputIterator>
4139 discrete_distribution(_InputIterator __wbegin,
4140 _InputIterator __wend)
4141 : _M_param(__wbegin, __wend)
4144 discrete_distribution(initializer_list<double> __wl)
4148 template<typename _Func>
4149 discrete_distribution(size_t __nw, double __xmin, double __xmax,
4151 : _M_param(__nw, __xmin, __xmax, __fw)
4155 discrete_distribution(const param_type& __p)
4160 * @brief Resets the distribution state.
4167 * @brief Returns the probabilities of the distribution.
4170 probabilities() const
4171 { return _M_param.probabilities(); }
4174 * @brief Returns the parameter set of the distribution.
4178 { return _M_param; }
4181 * @brief Sets the parameter set of the distribution.
4182 * @param __param The new parameter set of the distribution.
4185 param(const param_type& __param)
4186 { _M_param = __param; }
4189 * @brief Returns the greatest lower bound value of the distribution.
4193 { return result_type(0); }
4196 * @brief Returns the least upper bound value of the distribution.
4200 { return this->_M_param._M_prob.size() - 1; }
4202 template<typename _UniformRandomNumberGenerator>
4204 operator()(_UniformRandomNumberGenerator& __urng)
4205 { return this->operator()(__urng, this->param()); }
4207 template<typename _UniformRandomNumberGenerator>
4209 operator()(_UniformRandomNumberGenerator& __urng,
4210 const param_type& __p);
4213 * @brief Inserts a %discrete_distribution random number distribution
4214 * @p __x into the output stream @p __os.
4216 * @param __os An output stream.
4217 * @param __x A %discrete_distribution random number distribution.
4219 * @returns The output stream with the state of @p __x inserted or in
4222 template<typename _IntType1, typename _CharT, typename _Traits>
4223 friend std::basic_ostream<_CharT, _Traits>&
4224 operator<<(std::basic_ostream<_CharT, _Traits>&,
4225 const std::discrete_distribution<_IntType1>&);
4228 * @brief Extracts a %discrete_distribution random number distribution
4229 * @p __x from the input stream @p __is.
4231 * @param __is An input stream.
4232 * @param __x A %discrete_distribution random number
4235 * @returns The input stream with @p __x extracted or in an error
4238 template<typename _IntType1, typename _CharT, typename _Traits>
4239 friend std::basic_istream<_CharT, _Traits>&
4240 operator>>(std::basic_istream<_CharT, _Traits>&,
4241 std::discrete_distribution<_IntType1>&);
4244 param_type _M_param;
4249 * @brief A piecewise_constant_distribution random number distribution.
4251 * The formula for the piecewise constant probability mass function is
4254 template<typename _RealType = double>
4255 class piecewise_constant_distribution
4257 static_assert(std::is_floating_point<_RealType>::value,
4258 "template argument not a floating point type");
4261 /** The type of the range of the distribution. */
4262 typedef _RealType result_type;
4263 /** Parameter type. */
4266 typedef piecewise_constant_distribution<_RealType> distribution_type;
4267 friend class piecewise_constant_distribution<_RealType>;
4270 : _M_int(), _M_den(), _M_cp()
4271 { _M_initialize(); }
4273 template<typename _InputIteratorB, typename _InputIteratorW>
4274 param_type(_InputIteratorB __bfirst,
4275 _InputIteratorB __bend,
4276 _InputIteratorW __wbegin);
4278 template<typename _Func>
4279 param_type(initializer_list<_RealType> __bi, _Func __fw);
4281 template<typename _Func>
4282 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4285 std::vector<_RealType>
4297 std::vector<_RealType> _M_int;
4298 std::vector<double> _M_den;
4299 std::vector<double> _M_cp;
4303 piecewise_constant_distribution()
4307 template<typename _InputIteratorB, typename _InputIteratorW>
4308 piecewise_constant_distribution(_InputIteratorB __bfirst,
4309 _InputIteratorB __bend,
4310 _InputIteratorW __wbegin)
4311 : _M_param(__bfirst, __bend, __wbegin)
4314 template<typename _Func>
4315 piecewise_constant_distribution(initializer_list<_RealType> __bl,
4317 : _M_param(__bl, __fw)
4320 template<typename _Func>
4321 piecewise_constant_distribution(size_t __nw,
4322 _RealType __xmin, _RealType __xmax,
4324 : _M_param(__nw, __xmin, __xmax, __fw)
4328 piecewise_constant_distribution(const param_type& __p)
4333 * @brief Resets the distribution state.
4340 * @brief Returns a vector of the intervals.
4342 std::vector<_RealType>
4344 { return _M_param.intervals(); }
4347 * @brief Returns a vector of the probability densities.
4351 { return _M_param.densities(); }
4354 * @brief Returns the parameter set of the distribution.
4358 { return _M_param; }
4361 * @brief Sets the parameter set of the distribution.
4362 * @param __param The new parameter set of the distribution.
4365 param(const param_type& __param)
4366 { _M_param = __param; }
4369 * @brief Returns the greatest lower bound value of the distribution.
4373 { return this->_M_param._M_int.front(); }
4376 * @brief Returns the least upper bound value of the distribution.
4380 { return this->_M_param._M_int.back(); }
4382 template<typename _UniformRandomNumberGenerator>
4384 operator()(_UniformRandomNumberGenerator& __urng)
4385 { return this->operator()(__urng, this->param()); }
4387 template<typename _UniformRandomNumberGenerator>
4389 operator()(_UniformRandomNumberGenerator& __urng,
4390 const param_type& __p);
4393 * @brief Inserts a %piecewise_constan_distribution random
4394 * number distribution @p __x into the output stream @p __os.
4396 * @param __os An output stream.
4397 * @param __x A %piecewise_constan_distribution random number
4400 * @returns The output stream with the state of @p __x inserted or in
4403 template<typename _RealType1, typename _CharT, typename _Traits>
4404 friend std::basic_ostream<_CharT, _Traits>&
4405 operator<<(std::basic_ostream<_CharT, _Traits>&,
4406 const std::piecewise_constant_distribution<_RealType1>&);
4409 * @brief Extracts a %piecewise_constan_distribution random
4410 * number distribution @p __x from the input stream @p __is.
4412 * @param __is An input stream.
4413 * @param __x A %piecewise_constan_distribution random number
4416 * @returns The input stream with @p __x extracted or in an error
4419 template<typename _RealType1, typename _CharT, typename _Traits>
4420 friend std::basic_istream<_CharT, _Traits>&
4421 operator>>(std::basic_istream<_CharT, _Traits>&,
4422 std::piecewise_constant_distribution<_RealType1>&);
4425 param_type _M_param;
4430 * @brief A piecewise_linear_distribution random number distribution.
4432 * The formula for the piecewise linear probability mass function is
4435 template<typename _RealType = double>
4436 class piecewise_linear_distribution
4438 static_assert(std::is_floating_point<_RealType>::value,
4439 "template argument not a floating point type");
4442 /** The type of the range of the distribution. */
4443 typedef _RealType result_type;
4444 /** Parameter type. */
4447 typedef piecewise_linear_distribution<_RealType> distribution_type;
4448 friend class piecewise_linear_distribution<_RealType>;
4451 : _M_int(), _M_den(), _M_cp(), _M_m()
4452 { _M_initialize(); }
4454 template<typename _InputIteratorB, typename _InputIteratorW>
4455 param_type(_InputIteratorB __bfirst,
4456 _InputIteratorB __bend,
4457 _InputIteratorW __wbegin);
4459 template<typename _Func>
4460 param_type(initializer_list<_RealType> __bl, _Func __fw);
4462 template<typename _Func>
4463 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4466 std::vector<_RealType>
4478 std::vector<_RealType> _M_int;
4479 std::vector<double> _M_den;
4480 std::vector<double> _M_cp;
4481 std::vector<double> _M_m;
4485 piecewise_linear_distribution()
4489 template<typename _InputIteratorB, typename _InputIteratorW>
4490 piecewise_linear_distribution(_InputIteratorB __bfirst,
4491 _InputIteratorB __bend,
4492 _InputIteratorW __wbegin)
4493 : _M_param(__bfirst, __bend, __wbegin)
4496 template<typename _Func>
4497 piecewise_linear_distribution(initializer_list<_RealType> __bl,
4499 : _M_param(__bl, __fw)
4502 template<typename _Func>
4503 piecewise_linear_distribution(size_t __nw,
4504 _RealType __xmin, _RealType __xmax,
4506 : _M_param(__nw, __xmin, __xmax, __fw)
4510 piecewise_linear_distribution(const param_type& __p)
4515 * Resets the distribution state.
4522 * @brief Return the intervals of the distribution.
4524 std::vector<_RealType>
4526 { return _M_param.intervals(); }
4529 * @brief Return a vector of the probability densities of the
4534 { return _M_param.densities(); }
4537 * @brief Returns the parameter set of the distribution.
4541 { return _M_param; }
4544 * @brief Sets the parameter set of the distribution.
4545 * @param __param The new parameter set of the distribution.
4548 param(const param_type& __param)
4549 { _M_param = __param; }
4552 * @brief Returns the greatest lower bound value of the distribution.
4556 { return this->_M_param._M_int.front(); }
4559 * @brief Returns the least upper bound value of the distribution.
4563 { return this->_M_param._M_int.back(); }
4565 template<typename _UniformRandomNumberGenerator>
4567 operator()(_UniformRandomNumberGenerator& __urng)
4568 { return this->operator()(__urng, this->param()); }
4570 template<typename _UniformRandomNumberGenerator>
4572 operator()(_UniformRandomNumberGenerator& __urng,
4573 const param_type& __p);
4576 * @brief Inserts a %piecewise_linear_distribution random number
4577 * distribution @p __x into the output stream @p __os.
4579 * @param __os An output stream.
4580 * @param __x A %piecewise_linear_distribution random number
4583 * @returns The output stream with the state of @p __x inserted or in
4586 template<typename _RealType1, typename _CharT, typename _Traits>
4587 friend std::basic_ostream<_CharT, _Traits>&
4588 operator<<(std::basic_ostream<_CharT, _Traits>&,
4589 const std::piecewise_linear_distribution<_RealType1>&);
4592 * @brief Extracts a %piecewise_linear_distribution random number
4593 * distribution @p __x from the input stream @p __is.
4595 * @param __is An input stream.
4596 * @param __x A %piecewise_linear_distribution random number
4599 * @returns The input stream with @p __x extracted or in an error
4602 template<typename _RealType1, typename _CharT, typename _Traits>
4603 friend std::basic_istream<_CharT, _Traits>&
4604 operator>>(std::basic_istream<_CharT, _Traits>&,
4605 std::piecewise_linear_distribution<_RealType1>&);
4608 param_type _M_param;
4612 /* @} */ // group std_random_distributions_poisson
4614 /* @} */ // group std_random_distributions
4617 * @addtogroup std_random_utilities Random Number Utilities
4618 * @ingroup std_random
4623 * @brief The seed_seq class generates sequences of seeds for random
4624 * number generators.
4630 /** The type of the seed vales. */
4631 typedef uint_least32_t result_type;
4633 /** Default constructor. */
4638 template<typename _IntType>
4639 seed_seq(std::initializer_list<_IntType> il);
4641 template<typename _InputIterator>
4642 seed_seq(_InputIterator __begin, _InputIterator __end);
4644 // generating functions
4645 template<typename _RandomAccessIterator>
4647 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
4649 // property functions
4651 { return _M_v.size(); }
4653 template<typename OutputIterator>
4655 param(OutputIterator __dest) const
4656 { std::copy(_M_v.begin(), _M_v.end(), __dest); }
4660 std::vector<result_type> _M_v;
4663 /* @} */ // group std_random_utilities
4665 /* @} */ // group std_random