Merge from mainline (157009:157519).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / random.h
blob3f1a61535af49611649ac125e0ab149e97655baf
1 // random number generation -*- C++ -*-
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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/>.
25 /**
26 * @file bits/random.h
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
31 #include <vector>
33 namespace std
35 // [26.4] Random number generation
37 /**
38 * @defgroup random Random Number Generation
39 * @ingroup numerics
41 * A facility for generating random numbers on selected distributions.
42 * @{
45 /**
46 * @brief A function template for converting the output of a (integral)
47 * uniform random number generator to a floatng point result in the range
48 * [0-1).
50 template<typename _RealType, size_t __bits,
51 typename _UniformRandomNumberGenerator>
52 _RealType
53 generate_canonical(_UniformRandomNumberGenerator& __g);
56 * Implementation-space details.
58 namespace __detail
60 template<typename _UIntType, size_t __w,
61 bool = __w < static_cast<size_t>
62 (std::numeric_limits<_UIntType>::digits)>
63 struct _Shift
64 { static const _UIntType __value = 0; };
66 template<typename _UIntType, size_t __w>
67 struct _Shift<_UIntType, __w, true>
68 { static const _UIntType __value = _UIntType(1) << __w; };
70 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool>
71 struct _Mod;
73 // Dispatch based on modulus value to prevent divide-by-zero compile-time
74 // errors when m == 0.
75 template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
76 inline _Tp
77 __mod(_Tp __x)
78 { return _Mod<_Tp, __m, __a, __c, __m == 0>::__calc(__x); }
81 * An adaptor class for converting the output of any Generator into
82 * the input for a specific Distribution.
84 template<typename _Engine, typename _DInputType>
85 struct _Adaptor
88 public:
89 _Adaptor(_Engine& __g)
90 : _M_g(__g) { }
92 _DInputType
93 min() const
94 { return _DInputType(0); }
96 _DInputType
97 max() const
98 { return _DInputType(1); }
101 * Converts a value generated by the adapted random number generator
102 * into a value in the input domain for the dependent random number
103 * distribution.
105 _DInputType
106 operator()()
108 return std::generate_canonical<_DInputType,
109 std::numeric_limits<_DInputType>::digits,
110 _Engine>(_M_g);
113 private:
114 _Engine& _M_g;
116 } // namespace __detail
119 * @addtogroup random_generators Random Number Generators
120 * @ingroup random
122 * These classes define objects which provide random or pseudorandom
123 * numbers, either from a discrete or a continuous interval. The
124 * random number generator supplied as a part of this library are
125 * all uniform random number generators which provide a sequence of
126 * random number uniformly distributed over their range.
128 * A number generator is a function object with an operator() that
129 * takes zero arguments and returns a number.
131 * A compliant random number generator must satisfy the following
132 * requirements. <table border=1 cellpadding=10 cellspacing=0>
133 * <caption align=top>Random Number Generator Requirements</caption>
134 * <tr><td>To be documented.</td></tr> </table>
136 * @{
140 * @brief A model of a linear congruential random number generator.
142 * A random number generator that produces pseudorandom numbers via
143 * linear function:
144 * @f[
145 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
146 * @f]
148 * The template parameter @p _UIntType must be an unsigned integral type
149 * large enough to store values up to (__m-1). If the template parameter
150 * @p __m is 0, the modulus @p __m used is
151 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
152 * parameters @p __a and @p __c must be less than @p __m.
154 * The size of the state is @f$1@f$.
156 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
157 class linear_congruential_engine
159 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
160 "substituting _UIntType not an unsigned integral type");
161 static_assert(__m == 0u || (__a < __m && __c < __m),
162 "template argument substituting __m out of bounds");
164 public:
165 /** The type of the generated random value. */
166 typedef _UIntType result_type;
168 /** The multiplier. */
169 static const result_type multiplier = __a;
170 /** An increment. */
171 static const result_type increment = __c;
172 /** The modulus. */
173 static const result_type modulus = __m;
174 static const result_type default_seed = 1u;
177 * @brief Constructs a %linear_congruential_engine random number
178 * generator engine with seed @p __s. The default seed value
179 * is 1.
181 * @param __s The initial seed value.
183 explicit
184 linear_congruential_engine(result_type __s = default_seed)
185 { seed(__s); }
188 * @brief Constructs a %linear_congruential_engine random number
189 * generator engine seeded from the seed sequence @p __q.
191 * @param __q the seed sequence.
193 template<typename _Sseq, typename = typename
194 std::enable_if<!std::is_same<_Sseq, linear_congruential_engine>::value>
195 ::type>
196 explicit
197 linear_congruential_engine(_Sseq& __q)
198 { seed(__q); }
201 * @brief Reseeds the %linear_congruential_engine random number generator
202 * engine sequence to the seed @p __s.
204 * @param __s The new seed.
206 void
207 seed(result_type __s = default_seed);
210 * @brief Reseeds the %linear_congruential_engine random number generator
211 * engine
212 * sequence using values from the seed sequence @p __q.
214 * @param __q the seed sequence.
216 template<typename _Sseq>
217 typename std::enable_if<std::is_class<_Sseq>::value>::type
218 seed(_Sseq& __q);
221 * @brief Gets the smallest possible value in the output range.
223 * The minimum depends on the @p __c parameter: if it is zero, the
224 * minimum generated must be > 0, otherwise 0 is allowed.
226 * @todo This should be constexpr.
228 result_type
229 min() const
230 { return __c == 0u ? 1u : 0u; }
233 * @brief Gets the largest possible value in the output range.
235 * @todo This should be constexpr.
237 result_type
238 max() const
239 { return __m - 1u; }
242 * @brief Discard a sequence of random numbers.
244 * @todo Look for a faster way to do discard.
246 void
247 discard(unsigned long long __z)
249 for (; __z != 0ULL; --__z)
250 (*this)();
254 * @brief Gets the next random number in the sequence.
256 result_type
257 operator()()
259 _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
260 return _M_x;
264 * @brief Compares two linear congruential random number generator
265 * objects of the same type for equality.
267 * @param __lhs A linear congruential random number generator object.
268 * @param __rhs Another linear congruential random number generator
269 * object.
271 * @returns true if the infinite sequences of generated values
272 * would be equal, false otherwise.
274 friend bool
275 operator==(const linear_congruential_engine& __lhs,
276 const linear_congruential_engine& __rhs)
277 { return __lhs._M_x == __rhs._M_x; }
280 * @brief Writes the textual representation of the state x(i) of x to
281 * @p __os.
283 * @param __os The output stream.
284 * @param __lcr A % linear_congruential_engine random number generator.
285 * @returns __os.
287 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
288 _UIntType1 __m1, typename _CharT, typename _Traits>
289 friend std::basic_ostream<_CharT, _Traits>&
290 operator<<(std::basic_ostream<_CharT, _Traits>&,
291 const std::linear_congruential_engine<_UIntType1,
292 __a1, __c1, __m1>&);
295 * @brief Sets the state of the engine by reading its textual
296 * representation from @p __is.
298 * The textual representation must have been previously written using
299 * an output stream whose imbued locale and whose type's template
300 * specialization arguments _CharT and _Traits were the same as those
301 * of @p __is.
303 * @param __is The input stream.
304 * @param __lcr A % linear_congruential_engine random number generator.
305 * @returns __is.
307 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
308 _UIntType1 __m1, typename _CharT, typename _Traits>
309 friend std::basic_istream<_CharT, _Traits>&
310 operator>>(std::basic_istream<_CharT, _Traits>&,
311 std::linear_congruential_engine<_UIntType1, __a1,
312 __c1, __m1>&);
314 private:
315 _UIntType _M_x;
319 * @brief Compares two linear congruential random number generator
320 * objects of the same type for inequality.
322 * @param __lhs A linear congruential random number generator object.
323 * @param __rhs Another linear congruential random number generator
324 * object.
326 * @returns true if the infinite sequences of generated values
327 * would be different, false otherwise.
329 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
330 inline bool
331 operator!=(const std::linear_congruential_engine<_UIntType, __a,
332 __c, __m>& __lhs,
333 const std::linear_congruential_engine<_UIntType, __a,
334 __c, __m>& __rhs)
335 { return !(__lhs == __rhs); }
339 * A generalized feedback shift register discrete random number generator.
341 * This algorithm avoids multiplication and division and is designed to be
342 * friendly to a pipelined architecture. If the parameters are chosen
343 * correctly, this generator will produce numbers with a very long period and
344 * fairly good apparent entropy, although still not cryptographically strong.
346 * The best way to use this generator is with the predefined mt19937 class.
348 * This algorithm was originally invented by Makoto Matsumoto and
349 * Takuji Nishimura.
351 * @var word_size The number of bits in each element of the state vector.
352 * @var state_size The degree of recursion.
353 * @var shift_size The period parameter.
354 * @var mask_bits The separation point bit index.
355 * @var parameter_a The last row of the twist matrix.
356 * @var output_u The first right-shift tempering matrix parameter.
357 * @var output_s The first left-shift tempering matrix parameter.
358 * @var output_b The first left-shift tempering matrix mask.
359 * @var output_t The second left-shift tempering matrix parameter.
360 * @var output_c The second left-shift tempering matrix mask.
361 * @var output_l The second right-shift tempering matrix parameter.
363 template<typename _UIntType, size_t __w,
364 size_t __n, size_t __m, size_t __r,
365 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
366 _UIntType __b, size_t __t,
367 _UIntType __c, size_t __l, _UIntType __f>
368 class mersenne_twister_engine
370 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
371 "substituting _UIntType not an unsigned integral type");
372 static_assert(1u <= __m && __m <= __n,
373 "template argument substituting __m out of bounds");
374 static_assert(__r <= __w, "template argument substituting "
375 "__r out of bound");
376 static_assert(__u <= __w, "template argument substituting "
377 "__u out of bound");
378 static_assert(__s <= __w, "template argument substituting "
379 "__s out of bound");
380 static_assert(__t <= __w, "template argument substituting "
381 "__t out of bound");
382 static_assert(__l <= __w, "template argument substituting "
383 "__l out of bound");
384 static_assert(__w <= std::numeric_limits<_UIntType>::digits,
385 "template argument substituting __w out of bound");
386 static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
387 "template argument substituting __a out of bound");
388 static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
389 "template argument substituting __b out of bound");
390 static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
391 "template argument substituting __c out of bound");
392 static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
393 "template argument substituting __d out of bound");
394 static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
395 "template argument substituting __f out of bound");
397 public:
398 /** The type of the generated random value. */
399 typedef _UIntType result_type;
401 // parameter values
402 static const size_t word_size = __w;
403 static const size_t state_size = __n;
404 static const size_t shift_size = __m;
405 static const size_t mask_bits = __r;
406 static const result_type xor_mask = __a;
407 static const size_t tempering_u = __u;
408 static const result_type tempering_d = __d;
409 static const size_t tempering_s = __s;
410 static const result_type tempering_b = __b;
411 static const size_t tempering_t = __t;
412 static const result_type tempering_c = __c;
413 static const size_t tempering_l = __l;
414 static const result_type initialization_multiplier = __f;
415 static const result_type default_seed = 5489u;
417 // constructors and member function
418 explicit
419 mersenne_twister_engine(result_type __sd = default_seed)
420 { seed(__sd); }
423 * @brief Constructs a %mersenne_twister_engine random number generator
424 * engine seeded from the seed sequence @p __q.
426 * @param __q the seed sequence.
428 template<typename _Sseq, typename = typename
429 std::enable_if<!std::is_same<_Sseq, mersenne_twister_engine>::value>
430 ::type>
431 explicit
432 mersenne_twister_engine(_Sseq& __q)
433 { seed(__q); }
435 void
436 seed(result_type __sd = default_seed);
438 template<typename _Sseq>
439 typename std::enable_if<std::is_class<_Sseq>::value>::type
440 seed(_Sseq& __q);
443 * @brief Gets the smallest possible value in the output range.
445 * @todo This should be constexpr.
447 result_type
448 min() const
449 { return 0; };
452 * @brief Gets the largest possible value in the output range.
454 * @todo This should be constexpr.
456 result_type
457 max() const
458 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
461 * @brief Discard a sequence of random numbers.
463 * @todo Look for a faster way to do discard.
465 void
466 discard(unsigned long long __z)
468 for (; __z != 0ULL; --__z)
469 (*this)();
472 result_type
473 operator()();
476 * @brief Compares two % mersenne_twister_engine random number generator
477 * objects of the same type for equality.
479 * @param __lhs A % mersenne_twister_engine random number generator
480 * object.
481 * @param __rhs Another % mersenne_twister_engine random number
482 * generator object.
484 * @returns true if the infinite sequences of generated values
485 * would be equal, false otherwise.
487 friend bool
488 operator==(const mersenne_twister_engine& __lhs,
489 const mersenne_twister_engine& __rhs)
490 { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); }
493 * @brief Inserts the current state of a % mersenne_twister_engine
494 * random number generator engine @p __x into the output stream
495 * @p __os.
497 * @param __os An output stream.
498 * @param __x A % mersenne_twister_engine random number generator
499 * engine.
501 * @returns The output stream with the state of @p __x inserted or in
502 * an error state.
504 template<typename _UIntType1,
505 size_t __w1, size_t __n1,
506 size_t __m1, size_t __r1,
507 _UIntType1 __a1, size_t __u1,
508 _UIntType1 __d1, size_t __s1,
509 _UIntType1 __b1, size_t __t1,
510 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
511 typename _CharT, typename _Traits>
512 friend std::basic_ostream<_CharT, _Traits>&
513 operator<<(std::basic_ostream<_CharT, _Traits>&,
514 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
515 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
516 __l1, __f1>&);
519 * @brief Extracts the current state of a % mersenne_twister_engine
520 * random number generator engine @p __x from the input stream
521 * @p __is.
523 * @param __is An input stream.
524 * @param __x A % mersenne_twister_engine random number generator
525 * engine.
527 * @returns The input stream with the state of @p __x extracted or in
528 * an error state.
530 template<typename _UIntType1,
531 size_t __w1, size_t __n1,
532 size_t __m1, size_t __r1,
533 _UIntType1 __a1, size_t __u1,
534 _UIntType1 __d1, size_t __s1,
535 _UIntType1 __b1, size_t __t1,
536 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
537 typename _CharT, typename _Traits>
538 friend std::basic_istream<_CharT, _Traits>&
539 operator>>(std::basic_istream<_CharT, _Traits>&,
540 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
541 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
542 __l1, __f1>&);
544 private:
545 _UIntType _M_x[state_size];
546 size_t _M_p;
550 * @brief Compares two % mersenne_twister_engine random number generator
551 * objects of the same type for inequality.
553 * @param __lhs A % mersenne_twister_engine random number generator
554 * object.
555 * @param __rhs Another % mersenne_twister_engine random number
556 * generator object.
558 * @returns true if the infinite sequences of generated values
559 * would be different, false otherwise.
561 template<typename _UIntType, size_t __w,
562 size_t __n, size_t __m, size_t __r,
563 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
564 _UIntType __b, size_t __t,
565 _UIntType __c, size_t __l, _UIntType __f>
566 inline bool
567 operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
568 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs,
569 const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
570 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs)
571 { return !(__lhs == __rhs); }
575 * @brief The Marsaglia-Zaman generator.
577 * This is a model of a Generalized Fibonacci discrete random number
578 * generator, sometimes referred to as the SWC generator.
580 * A discrete random number generator that produces pseudorandom
581 * numbers using:
582 * @f[
583 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
584 * @f]
586 * The size of the state is @f$r@f$
587 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
589 * @var _M_x The state of the generator. This is a ring buffer.
590 * @var _M_carry The carry.
591 * @var _M_p Current index of x(i - r).
593 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
594 class subtract_with_carry_engine
596 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
597 "substituting _UIntType not an unsigned integral type");
598 static_assert(0u < __s && __s < __r,
599 "template argument substituting __s out of bounds");
600 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
601 "template argument substituting __w out of bounds");
603 public:
604 /** The type of the generated random value. */
605 typedef _UIntType result_type;
607 // parameter values
608 static const size_t word_size = __w;
609 static const size_t short_lag = __s;
610 static const size_t long_lag = __r;
611 static const result_type default_seed = 19780503u;
614 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
615 * random number generator.
617 explicit
618 subtract_with_carry_engine(result_type __sd = default_seed)
619 { seed(__sd); }
622 * @brief Constructs a %subtract_with_carry_engine random number engine
623 * seeded from the seed sequence @p __q.
625 * @param __q the seed sequence.
627 template<typename _Sseq, typename = typename
628 std::enable_if<!std::is_same<_Sseq, subtract_with_carry_engine>::value>
629 ::type>
630 explicit
631 subtract_with_carry_engine(_Sseq& __q)
632 { seed(__q); }
635 * @brief Seeds the initial state @f$x_0@f$ of the random number
636 * generator.
638 * N1688[4.19] modifies this as follows. If @p __value == 0,
639 * sets value to 19780503. In any case, with a linear
640 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
641 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
642 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
643 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
644 * set carry to 1, otherwise sets carry to 0.
646 void
647 seed(result_type __sd = default_seed);
650 * @brief Seeds the initial state @f$x_0@f$ of the
651 * % subtract_with_carry_engine random number generator.
653 template<typename _Sseq>
654 typename std::enable_if<std::is_class<_Sseq>::value>::type
655 seed(_Sseq& __q);
658 * @brief Gets the inclusive minimum value of the range of random
659 * integers returned by this generator.
661 * @todo This should be constexpr.
663 result_type
664 min() const
665 { return 0; }
668 * @brief Gets the inclusive maximum value of the range of random
669 * integers returned by this generator.
671 * @todo This should be constexpr.
673 result_type
674 max() const
675 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
678 * @brief Discard a sequence of random numbers.
680 * @todo Look for a faster way to do discard.
682 void
683 discard(unsigned long long __z)
685 for (; __z != 0ULL; --__z)
686 (*this)();
690 * @brief Gets the next random number in the sequence.
692 result_type
693 operator()();
696 * @brief Compares two % subtract_with_carry_engine random number
697 * generator objects of the same type for equality.
699 * @param __lhs A % subtract_with_carry_engine random number generator
700 * object.
701 * @param __rhs Another % subtract_with_carry_engine random number
702 * generator object.
704 * @returns true if the infinite sequences of generated values
705 * would be equal, false otherwise.
707 friend bool
708 operator==(const subtract_with_carry_engine& __lhs,
709 const subtract_with_carry_engine& __rhs)
710 { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); }
713 * @brief Inserts the current state of a % subtract_with_carry_engine
714 * random number generator engine @p __x into the output stream
715 * @p __os.
717 * @param __os An output stream.
718 * @param __x A % subtract_with_carry_engine random number generator
719 * engine.
721 * @returns The output stream with the state of @p __x inserted or in
722 * an error state.
724 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
725 typename _CharT, typename _Traits>
726 friend std::basic_ostream<_CharT, _Traits>&
727 operator<<(std::basic_ostream<_CharT, _Traits>&,
728 const std::subtract_with_carry_engine<_UIntType1, __w1,
729 __s1, __r1>&);
732 * @brief Extracts the current state of a % subtract_with_carry_engine
733 * random number generator engine @p __x from the input stream
734 * @p __is.
736 * @param __is An input stream.
737 * @param __x A % subtract_with_carry_engine random number generator
738 * engine.
740 * @returns The input stream with the state of @p __x extracted or in
741 * an error state.
743 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
744 typename _CharT, typename _Traits>
745 friend std::basic_istream<_CharT, _Traits>&
746 operator>>(std::basic_istream<_CharT, _Traits>&,
747 std::subtract_with_carry_engine<_UIntType1, __w1,
748 __s1, __r1>&);
750 private:
751 _UIntType _M_x[long_lag];
752 _UIntType _M_carry;
753 size_t _M_p;
757 * @brief Compares two % subtract_with_carry_engine random number
758 * generator objects of the same type for inequality.
760 * @param __lhs A % subtract_with_carry_engine random number generator
761 * object.
762 * @param __rhs Another % subtract_with_carry_engine random number
763 * generator object.
765 * @returns true if the infinite sequences of generated values
766 * would be different, false otherwise.
768 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
769 inline bool
770 operator!=(const std::subtract_with_carry_engine<_UIntType, __w,
771 __s, __r>& __lhs,
772 const std::subtract_with_carry_engine<_UIntType, __w,
773 __s, __r>& __rhs)
774 { return !(__lhs == __rhs); }
778 * Produces random numbers from some base engine by discarding blocks of
779 * data.
781 * 0 <= @p __r <= @p __p
783 template<typename _RandomNumberEngine, size_t __p, size_t __r>
784 class discard_block_engine
786 static_assert(1 <= __r && __r <= __p,
787 "template argument substituting __r out of bounds");
789 public:
790 /** The type of the generated random value. */
791 typedef typename _RandomNumberEngine::result_type result_type;
793 // parameter values
794 static const size_t block_size = __p;
795 static const size_t used_block = __r;
798 * @brief Constructs a default %discard_block_engine engine.
800 * The underlying engine is default constructed as well.
802 discard_block_engine()
803 : _M_b(), _M_n(0) { }
806 * @brief Copy constructs a %discard_block_engine engine.
808 * Copies an existing base class random number generator.
809 * @param rng An existing (base class) engine object.
811 explicit
812 discard_block_engine(const _RandomNumberEngine& __rne)
813 : _M_b(__rne), _M_n(0) { }
816 * @brief Move constructs a %discard_block_engine engine.
818 * Copies an existing base class random number generator.
819 * @param rng An existing (base class) engine object.
821 explicit
822 discard_block_engine(_RandomNumberEngine&& __rne)
823 : _M_b(std::move(__rne)), _M_n(0) { }
826 * @brief Seed constructs a %discard_block_engine engine.
828 * Constructs the underlying generator engine seeded with @p __s.
829 * @param __s A seed value for the base class engine.
831 explicit
832 discard_block_engine(result_type __s)
833 : _M_b(__s), _M_n(0) { }
836 * @brief Generator construct a %discard_block_engine engine.
838 * @param __q A seed sequence.
840 template<typename _Sseq, typename = typename
841 std::enable_if<!std::is_same<_Sseq, discard_block_engine>::value
842 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
843 ::type>
844 explicit
845 discard_block_engine(_Sseq& __q)
846 : _M_b(__q), _M_n(0)
850 * @brief Reseeds the %discard_block_engine object with the default
851 * seed for the underlying base class generator engine.
853 void
854 seed()
856 _M_b.seed();
857 _M_n = 0;
861 * @brief Reseeds the %discard_block_engine object with the default
862 * seed for the underlying base class generator engine.
864 void
865 seed(result_type __s)
867 _M_b.seed(__s);
868 _M_n = 0;
872 * @brief Reseeds the %discard_block_engine object with the given seed
873 * sequence.
874 * @param __q A seed generator function.
876 template<typename _Sseq>
877 void
878 seed(_Sseq& __q)
880 _M_b.seed(__q);
881 _M_n = 0;
885 * @brief Gets a const reference to the underlying generator engine
886 * object.
888 const _RandomNumberEngine&
889 base() const
890 { return _M_b; }
893 * @brief Gets the minimum value in the generated random number range.
895 * @todo This should be constexpr.
897 result_type
898 min() const
899 { return _M_b.min(); }
902 * @brief Gets the maximum value in the generated random number range.
904 * @todo This should be constexpr.
906 result_type
907 max() const
908 { return _M_b.max(); }
911 * @brief Discard a sequence of random numbers.
913 * @todo Look for a faster way to do discard.
915 void
916 discard(unsigned long long __z)
918 for (; __z != 0ULL; --__z)
919 (*this)();
923 * @brief Gets the next value in the generated random number sequence.
925 result_type
926 operator()();
929 * @brief Compares two %discard_block_engine random number generator
930 * objects of the same type for equality.
932 * @param __lhs A %discard_block_engine random number generator object.
933 * @param __rhs Another %discard_block_engine random number generator
934 * object.
936 * @returns true if the infinite sequences of generated values
937 * would be equal, false otherwise.
939 friend bool
940 operator==(const discard_block_engine& __lhs,
941 const discard_block_engine& __rhs)
942 { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; }
945 * @brief Inserts the current state of a %discard_block_engine random
946 * number generator engine @p __x into the output stream
947 * @p __os.
949 * @param __os An output stream.
950 * @param __x A %discard_block_engine random number generator engine.
952 * @returns The output stream with the state of @p __x inserted or in
953 * an error state.
955 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
956 typename _CharT, typename _Traits>
957 friend std::basic_ostream<_CharT, _Traits>&
958 operator<<(std::basic_ostream<_CharT, _Traits>&,
959 const std::discard_block_engine<_RandomNumberEngine1,
960 __p1, __r1>&);
963 * @brief Extracts the current state of a % subtract_with_carry_engine
964 * random number generator engine @p __x from the input stream
965 * @p __is.
967 * @param __is An input stream.
968 * @param __x A %discard_block_engine random number generator engine.
970 * @returns The input stream with the state of @p __x extracted or in
971 * an error state.
973 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
974 typename _CharT, typename _Traits>
975 friend std::basic_istream<_CharT, _Traits>&
976 operator>>(std::basic_istream<_CharT, _Traits>&,
977 std::discard_block_engine<_RandomNumberEngine1,
978 __p1, __r1>&);
980 private:
981 _RandomNumberEngine _M_b;
982 size_t _M_n;
986 * @brief Compares two %discard_block_engine random number generator
987 * objects of the same type for inequality.
989 * @param __lhs A %discard_block_engine random number generator object.
990 * @param __rhs Another %discard_block_engine random number generator
991 * object.
993 * @returns true if the infinite sequences of generated values
994 * would be different, false otherwise.
996 template<typename _RandomNumberEngine, size_t __p, size_t __r>
997 inline bool
998 operator!=(const std::discard_block_engine<_RandomNumberEngine, __p,
999 __r>& __lhs,
1000 const std::discard_block_engine<_RandomNumberEngine, __p,
1001 __r>& __rhs)
1002 { return !(__lhs == __rhs); }
1006 * Produces random numbers by combining random numbers from some base
1007 * engine to produce random numbers with a specifies number of bits @p __w.
1009 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1010 class independent_bits_engine
1012 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
1013 "substituting _UIntType not an unsigned integral type");
1014 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
1015 "template argument substituting __w out of bounds");
1017 public:
1018 /** The type of the generated random value. */
1019 typedef _UIntType result_type;
1022 * @brief Constructs a default %independent_bits_engine engine.
1024 * The underlying engine is default constructed as well.
1026 independent_bits_engine()
1027 : _M_b() { }
1030 * @brief Copy constructs a %independent_bits_engine engine.
1032 * Copies an existing base class random number generator.
1033 * @param rng An existing (base class) engine object.
1035 explicit
1036 independent_bits_engine(const _RandomNumberEngine& __rne)
1037 : _M_b(__rne) { }
1040 * @brief Move constructs a %independent_bits_engine engine.
1042 * Copies an existing base class random number generator.
1043 * @param rng An existing (base class) engine object.
1045 explicit
1046 independent_bits_engine(_RandomNumberEngine&& __rne)
1047 : _M_b(std::move(__rne)) { }
1050 * @brief Seed constructs a %independent_bits_engine engine.
1052 * Constructs the underlying generator engine seeded with @p __s.
1053 * @param __s A seed value for the base class engine.
1055 explicit
1056 independent_bits_engine(result_type __s)
1057 : _M_b(__s) { }
1060 * @brief Generator construct a %independent_bits_engine engine.
1062 * @param __q A seed sequence.
1064 template<typename _Sseq, typename = typename
1065 std::enable_if<!std::is_same<_Sseq, independent_bits_engine>::value
1066 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
1067 ::type>
1068 explicit
1069 independent_bits_engine(_Sseq& __q)
1070 : _M_b(__q)
1074 * @brief Reseeds the %independent_bits_engine object with the default
1075 * seed for the underlying base class generator engine.
1077 void
1078 seed()
1079 { _M_b.seed(); }
1082 * @brief Reseeds the %independent_bits_engine object with the default
1083 * seed for the underlying base class generator engine.
1085 void
1086 seed(result_type __s)
1087 { _M_b.seed(__s); }
1090 * @brief Reseeds the %independent_bits_engine object with the given
1091 * seed sequence.
1092 * @param __q A seed generator function.
1094 template<typename _Sseq>
1095 void
1096 seed(_Sseq& __q)
1097 { _M_b.seed(__q); }
1100 * @brief Gets a const reference to the underlying generator engine
1101 * object.
1103 const _RandomNumberEngine&
1104 base() const
1105 { return _M_b; }
1108 * @brief Gets the minimum value in the generated random number range.
1110 * @todo This should be constexpr.
1112 result_type
1113 min() const
1114 { return 0U; }
1117 * @brief Gets the maximum value in the generated random number range.
1119 * @todo This should be constexpr.
1121 result_type
1122 max() const
1123 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
1126 * @brief Discard a sequence of random numbers.
1128 * @todo Look for a faster way to do discard.
1130 void
1131 discard(unsigned long long __z)
1133 for (; __z != 0ULL; --__z)
1134 (*this)();
1138 * @brief Gets the next value in the generated random number sequence.
1140 result_type
1141 operator()();
1144 * @brief Compares two %independent_bits_engine random number generator
1145 * objects of the same type for equality.
1147 * @param __lhs A %independent_bits_engine random number generator
1148 * object.
1149 * @param __rhs Another %independent_bits_engine random number generator
1150 * object.
1152 * @returns true if the infinite sequences of generated values
1153 * would be equal, false otherwise.
1155 friend bool
1156 operator==(const independent_bits_engine& __lhs,
1157 const independent_bits_engine& __rhs)
1158 { return __lhs._M_b == __rhs._M_b; }
1161 * @brief Extracts the current state of a % subtract_with_carry_engine
1162 * random number generator engine @p __x from the input stream
1163 * @p __is.
1165 * @param __is An input stream.
1166 * @param __x A %independent_bits_engine random number generator
1167 * engine.
1169 * @returns The input stream with the state of @p __x extracted or in
1170 * an error state.
1172 template<typename _CharT, typename _Traits>
1173 friend std::basic_istream<_CharT, _Traits>&
1174 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1175 std::independent_bits_engine<_RandomNumberEngine,
1176 __w, _UIntType>& __x)
1178 __is >> __x._M_b;
1179 return __is;
1182 private:
1183 _RandomNumberEngine _M_b;
1187 * @brief Compares two %independent_bits_engine random number generator
1188 * objects of the same type for inequality.
1190 * @param __lhs A %independent_bits_engine random number generator
1191 * object.
1192 * @param __rhs Another %independent_bits_engine random number generator
1193 * object.
1195 * @returns true if the infinite sequences of generated values
1196 * would be different, false otherwise.
1198 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1199 inline bool
1200 operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w,
1201 _UIntType>& __lhs,
1202 const std::independent_bits_engine<_RandomNumberEngine, __w,
1203 _UIntType>& __rhs)
1204 { return !(__lhs == __rhs); }
1207 * @brief Inserts the current state of a %independent_bits_engine random
1208 * number generator engine @p __x into the output stream @p __os.
1210 * @param __os An output stream.
1211 * @param __x A %independent_bits_engine random number generator engine.
1213 * @returns The output stream with the state of @p __x inserted or in
1214 * an error state.
1216 template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1217 typename _CharT, typename _Traits>
1218 std::basic_ostream<_CharT, _Traits>&
1219 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1220 const std::independent_bits_engine<_RandomNumberEngine,
1221 __w, _UIntType>& __x)
1223 __os << __x.base();
1224 return __os;
1229 * @brief Produces random numbers by combining random numbers from some
1230 * base engine to produce random numbers with a specifies number of bits
1231 * @p __w.
1233 template<typename _RandomNumberEngine, size_t __k>
1234 class shuffle_order_engine
1236 static_assert(1u <= __k, "template argument substituting "
1237 "__k out of bound");
1239 public:
1240 /** The type of the generated random value. */
1241 typedef typename _RandomNumberEngine::result_type result_type;
1243 static const size_t table_size = __k;
1246 * @brief Constructs a default %shuffle_order_engine engine.
1248 * The underlying engine is default constructed as well.
1250 shuffle_order_engine()
1251 : _M_b()
1252 { _M_initialize(); }
1255 * @brief Copy constructs a %shuffle_order_engine engine.
1257 * Copies an existing base class random number generator.
1258 * @param rng An existing (base class) engine object.
1260 explicit
1261 shuffle_order_engine(const _RandomNumberEngine& __rne)
1262 : _M_b(__rne)
1263 { _M_initialize(); }
1266 * @brief Move constructs a %shuffle_order_engine engine.
1268 * Copies an existing base class random number generator.
1269 * @param rng An existing (base class) engine object.
1271 explicit
1272 shuffle_order_engine(_RandomNumberEngine&& __rne)
1273 : _M_b(std::move(__rne))
1274 { _M_initialize(); }
1277 * @brief Seed constructs a %shuffle_order_engine engine.
1279 * Constructs the underlying generator engine seeded with @p __s.
1280 * @param __s A seed value for the base class engine.
1282 explicit
1283 shuffle_order_engine(result_type __s)
1284 : _M_b(__s)
1285 { _M_initialize(); }
1288 * @brief Generator construct a %shuffle_order_engine engine.
1290 * @param __q A seed sequence.
1292 template<typename _Sseq, typename = typename
1293 std::enable_if<!std::is_same<_Sseq, shuffle_order_engine>::value
1294 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
1295 ::type>
1296 explicit
1297 shuffle_order_engine(_Sseq& __q)
1298 : _M_b(__q)
1299 { _M_initialize(); }
1302 * @brief Reseeds the %shuffle_order_engine object with the default seed
1303 for the underlying base class generator engine.
1305 void
1306 seed()
1308 _M_b.seed();
1309 _M_initialize();
1313 * @brief Reseeds the %shuffle_order_engine object with the default seed
1314 * for the underlying base class generator engine.
1316 void
1317 seed(result_type __s)
1319 _M_b.seed(__s);
1320 _M_initialize();
1324 * @brief Reseeds the %shuffle_order_engine object with the given seed
1325 * sequence.
1326 * @param __q A seed generator function.
1328 template<typename _Sseq>
1329 void
1330 seed(_Sseq& __q)
1332 _M_b.seed(__q);
1333 _M_initialize();
1337 * Gets a const reference to the underlying generator engine object.
1339 const _RandomNumberEngine&
1340 base() const
1341 { return _M_b; }
1344 * Gets the minimum value in the generated random number range.
1346 * @todo This should be constexpr.
1348 result_type
1349 min() const
1350 { return _M_b.min(); }
1353 * Gets the maximum value in the generated random number range.
1355 * @todo This should be constexpr.
1357 result_type
1358 max() const
1359 { return _M_b.max(); }
1362 * Discard a sequence of random numbers.
1364 * @todo Look for a faster way to do discard.
1366 void
1367 discard(unsigned long long __z)
1369 for (; __z != 0ULL; --__z)
1370 (*this)();
1374 * Gets the next value in the generated random number sequence.
1376 result_type
1377 operator()();
1380 * Compares two %shuffle_order_engine random number generator objects
1381 * of the same type for equality.
1383 * @param __lhs A %shuffle_order_engine random number generator object.
1384 * @param __rhs Another %shuffle_order_engine random number generator
1385 * object.
1387 * @returns true if the infinite sequences of generated values
1388 * would be equal, false otherwise.
1390 friend bool
1391 operator==(const shuffle_order_engine& __lhs,
1392 const shuffle_order_engine& __rhs)
1393 { return __lhs._M_b == __rhs._M_b; }
1396 * @brief Inserts the current state of a %shuffle_order_engine random
1397 * number generator engine @p __x into the output stream
1398 @p __os.
1400 * @param __os An output stream.
1401 * @param __x A %shuffle_order_engine random number generator engine.
1403 * @returns The output stream with the state of @p __x inserted or in
1404 * an error state.
1406 template<typename _RandomNumberEngine1, size_t __k1,
1407 typename _CharT, typename _Traits>
1408 friend std::basic_ostream<_CharT, _Traits>&
1409 operator<<(std::basic_ostream<_CharT, _Traits>&,
1410 const std::shuffle_order_engine<_RandomNumberEngine1,
1411 __k1>&);
1414 * @brief Extracts the current state of a % subtract_with_carry_engine
1415 * random number generator engine @p __x from the input stream
1416 * @p __is.
1418 * @param __is An input stream.
1419 * @param __x A %shuffle_order_engine random number generator engine.
1421 * @returns The input stream with the state of @p __x extracted or in
1422 * an error state.
1424 template<typename _RandomNumberEngine1, size_t __k1,
1425 typename _CharT, typename _Traits>
1426 friend std::basic_istream<_CharT, _Traits>&
1427 operator>>(std::basic_istream<_CharT, _Traits>&,
1428 std::shuffle_order_engine<_RandomNumberEngine1, __k1>&);
1430 private:
1431 void _M_initialize()
1433 for (size_t __i = 0; __i < __k; ++__i)
1434 _M_v[__i] = _M_b();
1435 _M_y = _M_b();
1438 _RandomNumberEngine _M_b;
1439 result_type _M_v[__k];
1440 result_type _M_y;
1444 * Compares two %shuffle_order_engine random number generator objects
1445 * of the same type for inequality.
1447 * @param __lhs A %shuffle_order_engine random number generator object.
1448 * @param __rhs Another %shuffle_order_engine random number generator
1449 * object.
1451 * @returns true if the infinite sequences of generated values
1452 * would be different, false otherwise.
1454 template<typename _RandomNumberEngine, size_t __k>
1455 inline bool
1456 operator!=(const std::shuffle_order_engine<_RandomNumberEngine,
1457 __k>& __lhs,
1458 const std::shuffle_order_engine<_RandomNumberEngine,
1459 __k>& __rhs)
1460 { return !(__lhs == __rhs); }
1464 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1466 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1467 minstd_rand0;
1470 * An alternative LCR (Lehmer Generator function).
1472 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1473 minstd_rand;
1476 * The classic Mersenne Twister.
1478 * Reference:
1479 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1480 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1481 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1483 typedef mersenne_twister_engine<
1484 uint_fast32_t,
1485 32, 624, 397, 31,
1486 0x9908b0dfUL, 11,
1487 0xffffffffUL, 7,
1488 0x9d2c5680UL, 15,
1489 0xefc60000UL, 18, 1812433253UL> mt19937;
1492 * An alternative Mersenne Twister.
1494 typedef mersenne_twister_engine<
1495 uint_fast64_t,
1496 64, 312, 156, 31,
1497 0xb5026f5aa96619e9ULL, 29,
1498 0x5555555555555555ULL, 17,
1499 0x71d67fffeda60000ULL, 37,
1500 0xfff7eee000000000ULL, 43,
1501 6364136223846793005ULL> mt19937_64;
1503 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1504 ranlux24_base;
1506 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1507 ranlux48_base;
1509 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1511 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1513 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1515 typedef minstd_rand0 default_random_engine;
1518 * A standard interface to a platform-specific non-deterministic
1519 * random number generator (if any are available).
1521 class random_device
1523 public:
1524 /** The type of the generated random value. */
1525 typedef unsigned int result_type;
1527 // constructors, destructors and member functions
1529 #ifdef _GLIBCXX_USE_RANDOM_TR1
1531 explicit
1532 random_device(const std::string& __token = "/dev/urandom")
1534 if ((__token != "/dev/urandom" && __token != "/dev/random")
1535 || !(_M_file = std::fopen(__token.c_str(), "rb")))
1536 std::__throw_runtime_error(__N("random_device::"
1537 "random_device(const std::string&)"));
1540 ~random_device()
1541 { std::fclose(_M_file); }
1543 #else
1545 explicit
1546 random_device(const std::string& __token = "mt19937")
1547 : _M_mt(_M_strtoul(__token)) { }
1549 private:
1550 static unsigned long
1551 _M_strtoul(const std::string& __str)
1553 unsigned long __ret = 5489UL;
1554 if (__str != "mt19937")
1556 const char* __nptr = __str.c_str();
1557 char* __endptr;
1558 __ret = std::strtoul(__nptr, &__endptr, 0);
1559 if (*__nptr == '\0' || *__endptr != '\0')
1560 std::__throw_runtime_error(__N("random_device::_M_strtoul"
1561 "(const std::string&)"));
1563 return __ret;
1566 public:
1568 #endif
1570 result_type
1571 min() const
1572 { return std::numeric_limits<result_type>::min(); }
1574 result_type
1575 max() const
1576 { return std::numeric_limits<result_type>::max(); }
1578 double
1579 entropy() const
1580 { return 0.0; }
1582 result_type
1583 operator()()
1585 #ifdef _GLIBCXX_USE_RANDOM_TR1
1586 result_type __ret;
1587 std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
1588 1, _M_file);
1589 return __ret;
1590 #else
1591 return _M_mt();
1592 #endif
1595 // No copy functions.
1596 random_device(const random_device&) = delete;
1597 void operator=(const random_device&) = delete;
1599 private:
1601 #ifdef _GLIBCXX_USE_RANDOM_TR1
1602 FILE* _M_file;
1603 #else
1604 mt19937 _M_mt;
1605 #endif
1608 /* @} */ // group random_generators
1611 * @addtogroup random_distributions Random Number Distributions
1612 * @ingroup random
1613 * @{
1617 * @addtogroup random_distributions_uniform Uniform Distributions
1618 * @ingroup random_distributions
1619 * @{
1623 * @brief Uniform discrete distribution for random numbers.
1624 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1625 * probability throughout the range.
1627 template<typename _IntType = int>
1628 class uniform_int_distribution
1630 static_assert(std::is_integral<_IntType>::value,
1631 "template argument not an integral type");
1633 public:
1634 /** The type of the range of the distribution. */
1635 typedef _IntType result_type;
1636 /** Parameter type. */
1637 struct param_type
1639 typedef uniform_int_distribution<_IntType> distribution_type;
1641 explicit
1642 param_type(_IntType __a = 0,
1643 _IntType __b = std::numeric_limits<_IntType>::max())
1644 : _M_a(__a), _M_b(__b)
1646 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1649 result_type
1650 a() const
1651 { return _M_a; }
1653 result_type
1654 b() const
1655 { return _M_b; }
1657 friend bool
1658 operator==(const param_type& __p1, const param_type& __p2)
1659 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
1661 private:
1662 _IntType _M_a;
1663 _IntType _M_b;
1666 public:
1668 * @brief Constructs a uniform distribution object.
1670 explicit
1671 uniform_int_distribution(_IntType __a = 0,
1672 _IntType __b = std::numeric_limits<_IntType>::max())
1673 : _M_param(__a, __b)
1676 explicit
1677 uniform_int_distribution(const param_type& __p)
1678 : _M_param(__p)
1682 * @brief Resets the distribution state.
1684 * Does nothing for the uniform integer distribution.
1686 void
1687 reset() { }
1689 result_type
1690 a() const
1691 { return _M_param.a(); }
1693 result_type
1694 b() const
1695 { return _M_param.b(); }
1698 * @brief Returns the inclusive lower bound of the distribution range.
1700 result_type
1701 min() const
1702 { return this->a(); }
1705 * @brief Returns the inclusive upper bound of the distribution range.
1707 result_type
1708 max() const
1709 { return this->b(); }
1712 * @brief Returns the parameter set of the distribution.
1714 param_type
1715 param() const
1716 { return _M_param; }
1719 * @brief Sets the parameter set of the distribution.
1720 * @param __param The new parameter set of the distribution.
1722 void
1723 param(const param_type& __param)
1724 { _M_param = __param; }
1727 * Gets a uniformly distributed random number in the range
1728 * @f$(min, max)@f$.
1730 template<typename _UniformRandomNumberGenerator>
1731 result_type
1732 operator()(_UniformRandomNumberGenerator& __urng)
1733 { return this->operator()(__urng, this->param()); }
1736 * Gets a uniform random number in the range @f$[0, n)@f$.
1738 * This function is aimed at use with std::random_shuffle.
1740 template<typename _UniformRandomNumberGenerator>
1741 result_type
1742 operator()(_UniformRandomNumberGenerator& __urng,
1743 const param_type& __p);
1745 param_type _M_param;
1749 * @brief Return true if two uniform integer distributions have
1750 * the same parameters.
1752 template<typename _IntType>
1753 inline bool
1754 operator==(const std::uniform_int_distribution<_IntType>& __d1,
1755 const std::uniform_int_distribution<_IntType>& __d2)
1756 { return __d1.param() == __d2.param(); }
1759 * @brief Return true if two uniform integer distributions have
1760 * different parameters.
1762 template<typename _IntType>
1763 inline bool
1764 operator!=(const std::uniform_int_distribution<_IntType>& __d1,
1765 const std::uniform_int_distribution<_IntType>& __d2)
1766 { return !(__d1 == __d2); }
1769 * @brief Inserts a %uniform_int_distribution random number
1770 * distribution @p __x into the output stream @p os.
1772 * @param __os An output stream.
1773 * @param __x A %uniform_int_distribution random number distribution.
1775 * @returns The output stream with the state of @p __x inserted or in
1776 * an error state.
1778 template<typename _IntType, typename _CharT, typename _Traits>
1779 std::basic_ostream<_CharT, _Traits>&
1780 operator<<(std::basic_ostream<_CharT, _Traits>&,
1781 const std::uniform_int_distribution<_IntType>&);
1784 * @brief Extracts a %uniform_int_distribution random number distribution
1785 * @p __x from the input stream @p __is.
1787 * @param __is An input stream.
1788 * @param __x A %uniform_int_distribution random number generator engine.
1790 * @returns The input stream with @p __x extracted or in an error state.
1792 template<typename _IntType, typename _CharT, typename _Traits>
1793 std::basic_istream<_CharT, _Traits>&
1794 operator>>(std::basic_istream<_CharT, _Traits>&,
1795 std::uniform_int_distribution<_IntType>&);
1799 * @brief Uniform continuous distribution for random numbers.
1801 * A continuous random distribution on the range [min, max) with equal
1802 * probability throughout the range. The URNG should be real-valued and
1803 * deliver number in the range [0, 1).
1805 template<typename _RealType = double>
1806 class uniform_real_distribution
1808 static_assert(std::is_floating_point<_RealType>::value,
1809 "template argument not a floating point type");
1811 public:
1812 /** The type of the range of the distribution. */
1813 typedef _RealType result_type;
1814 /** Parameter type. */
1815 struct param_type
1817 typedef uniform_real_distribution<_RealType> distribution_type;
1819 explicit
1820 param_type(_RealType __a = _RealType(0),
1821 _RealType __b = _RealType(1))
1822 : _M_a(__a), _M_b(__b)
1824 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1827 result_type
1828 a() const
1829 { return _M_a; }
1831 result_type
1832 b() const
1833 { return _M_b; }
1835 friend bool
1836 operator==(const param_type& __p1, const param_type& __p2)
1837 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
1839 private:
1840 _RealType _M_a;
1841 _RealType _M_b;
1844 public:
1846 * @brief Constructs a uniform_real_distribution object.
1848 * @param __min [IN] The lower bound of the distribution.
1849 * @param __max [IN] The upper bound of the distribution.
1851 explicit
1852 uniform_real_distribution(_RealType __a = _RealType(0),
1853 _RealType __b = _RealType(1))
1854 : _M_param(__a, __b)
1857 explicit
1858 uniform_real_distribution(const param_type& __p)
1859 : _M_param(__p)
1863 * @brief Resets the distribution state.
1865 * Does nothing for the uniform real distribution.
1867 void
1868 reset() { }
1870 result_type
1871 a() const
1872 { return _M_param.a(); }
1874 result_type
1875 b() const
1876 { return _M_param.b(); }
1879 * @brief Returns the inclusive lower bound of the distribution range.
1881 result_type
1882 min() const
1883 { return this->a(); }
1886 * @brief Returns the inclusive upper bound of the distribution range.
1888 result_type
1889 max() const
1890 { return this->b(); }
1893 * @brief Returns the parameter set of the distribution.
1895 param_type
1896 param() const
1897 { return _M_param; }
1900 * @brief Sets the parameter set of the distribution.
1901 * @param __param The new parameter set of the distribution.
1903 void
1904 param(const param_type& __param)
1905 { _M_param = __param; }
1907 template<typename _UniformRandomNumberGenerator>
1908 result_type
1909 operator()(_UniformRandomNumberGenerator& __urng)
1910 { return this->operator()(__urng, this->param()); }
1912 template<typename _UniformRandomNumberGenerator>
1913 result_type
1914 operator()(_UniformRandomNumberGenerator& __urng,
1915 const param_type& __p)
1917 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1918 __aurng(__urng);
1919 return (__aurng() * (__p.b() - __p.a())) + __p.a();
1922 private:
1923 param_type _M_param;
1927 * @brief Return true if two uniform real distributions have
1928 * the same parameters.
1930 template<typename _IntType>
1931 inline bool
1932 operator==(const std::uniform_real_distribution<_IntType>& __d1,
1933 const std::uniform_real_distribution<_IntType>& __d2)
1934 { return __d1.param() == __d2.param(); }
1937 * @brief Return true if two uniform real distributions have
1938 * different parameters.
1940 template<typename _IntType>
1941 inline bool
1942 operator!=(const std::uniform_real_distribution<_IntType>& __d1,
1943 const std::uniform_real_distribution<_IntType>& __d2)
1944 { return !(__d1 == __d2); }
1947 * @brief Inserts a %uniform_real_distribution random number
1948 * distribution @p __x into the output stream @p __os.
1950 * @param __os An output stream.
1951 * @param __x A %uniform_real_distribution random number distribution.
1953 * @returns The output stream with the state of @p __x inserted or in
1954 * an error state.
1956 template<typename _RealType, typename _CharT, typename _Traits>
1957 std::basic_ostream<_CharT, _Traits>&
1958 operator<<(std::basic_ostream<_CharT, _Traits>&,
1959 const std::uniform_real_distribution<_RealType>&);
1962 * @brief Extracts a %uniform_real_distribution random number distribution
1963 * @p __x from the input stream @p __is.
1965 * @param __is An input stream.
1966 * @param __x A %uniform_real_distribution random number generator engine.
1968 * @returns The input stream with @p __x extracted or in an error state.
1970 template<typename _RealType, typename _CharT, typename _Traits>
1971 std::basic_istream<_CharT, _Traits>&
1972 operator>>(std::basic_istream<_CharT, _Traits>&,
1973 std::uniform_real_distribution<_RealType>&);
1975 /* @} */ // group random_distributions_uniform
1978 * @addtogroup random_distributions_normal Normal Distributions
1979 * @ingroup random_distributions
1980 * @{
1984 * @brief A normal continuous distribution for random numbers.
1986 * The formula for the normal probability density function is
1987 * @f[
1988 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
1989 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
1990 * @f]
1992 template<typename _RealType = double>
1993 class normal_distribution
1995 static_assert(std::is_floating_point<_RealType>::value,
1996 "template argument not a floating point type");
1998 public:
1999 /** The type of the range of the distribution. */
2000 typedef _RealType result_type;
2001 /** Parameter type. */
2002 struct param_type
2004 typedef normal_distribution<_RealType> distribution_type;
2006 explicit
2007 param_type(_RealType __mean = _RealType(0),
2008 _RealType __stddev = _RealType(1))
2009 : _M_mean(__mean), _M_stddev(__stddev)
2011 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
2014 _RealType
2015 mean() const
2016 { return _M_mean; }
2018 _RealType
2019 stddev() const
2020 { return _M_stddev; }
2022 friend bool
2023 operator==(const param_type& __p1, const param_type& __p2)
2024 { return (__p1._M_mean == __p2._M_mean
2025 && __p1._M_stddev == __p2._M_stddev); }
2027 private:
2028 _RealType _M_mean;
2029 _RealType _M_stddev;
2032 public:
2034 * Constructs a normal distribution with parameters @f$mean@f$ and
2035 * standard deviation.
2037 explicit
2038 normal_distribution(result_type __mean = result_type(0),
2039 result_type __stddev = result_type(1))
2040 : _M_param(__mean, __stddev), _M_saved_available(false)
2043 explicit
2044 normal_distribution(const param_type& __p)
2045 : _M_param(__p), _M_saved_available(false)
2049 * @brief Resets the distribution state.
2051 void
2052 reset()
2053 { _M_saved_available = false; }
2056 * @brief Returns the mean of the distribution.
2058 _RealType
2059 mean() const
2060 { return _M_param.mean(); }
2063 * @brief Returns the standard deviation of the distribution.
2065 _RealType
2066 stddev() const
2067 { return _M_param.stddev(); }
2070 * @brief Returns the parameter set of the distribution.
2072 param_type
2073 param() const
2074 { return _M_param; }
2077 * @brief Sets the parameter set of the distribution.
2078 * @param __param The new parameter set of the distribution.
2080 void
2081 param(const param_type& __param)
2082 { _M_param = __param; }
2085 * @brief Returns the greatest lower bound value of the distribution.
2087 result_type
2088 min() const
2089 { return std::numeric_limits<result_type>::min(); }
2092 * @brief Returns the least upper bound value of the distribution.
2094 result_type
2095 max() const
2096 { return std::numeric_limits<result_type>::max(); }
2098 template<typename _UniformRandomNumberGenerator>
2099 result_type
2100 operator()(_UniformRandomNumberGenerator& __urng)
2101 { return this->operator()(__urng, this->param()); }
2103 template<typename _UniformRandomNumberGenerator>
2104 result_type
2105 operator()(_UniformRandomNumberGenerator& __urng,
2106 const param_type& __p);
2109 * @brief Return true if two normal distributions have
2110 * the same parameters and the sequences that would
2111 * be generated are equal.
2113 template<typename _RealType1>
2114 friend bool
2115 operator==(const std::normal_distribution<_RealType1>& __d1,
2116 const std::normal_distribution<_RealType1>& __d2);
2119 * @brief Inserts a %normal_distribution random number distribution
2120 * @p __x into the output stream @p __os.
2122 * @param __os An output stream.
2123 * @param __x A %normal_distribution random number distribution.
2125 * @returns The output stream with the state of @p __x inserted or in
2126 * an error state.
2128 template<typename _RealType1, typename _CharT, typename _Traits>
2129 friend std::basic_ostream<_CharT, _Traits>&
2130 operator<<(std::basic_ostream<_CharT, _Traits>&,
2131 const std::normal_distribution<_RealType1>&);
2134 * @brief Extracts a %normal_distribution random number distribution
2135 * @p __x from the input stream @p __is.
2137 * @param __is An input stream.
2138 * @param __x A %normal_distribution random number generator engine.
2140 * @returns The input stream with @p __x extracted or in an error
2141 * state.
2143 template<typename _RealType1, typename _CharT, typename _Traits>
2144 friend std::basic_istream<_CharT, _Traits>&
2145 operator>>(std::basic_istream<_CharT, _Traits>&,
2146 std::normal_distribution<_RealType1>&);
2148 private:
2149 param_type _M_param;
2150 result_type _M_saved;
2151 bool _M_saved_available;
2155 * @brief Return true if two normal distributions are different.
2157 template<typename _RealType>
2158 inline bool
2159 operator!=(const std::normal_distribution<_RealType>& __d1,
2160 const std::normal_distribution<_RealType>& __d2)
2161 { return !(__d1 == __d2); }
2165 * @brief A lognormal_distribution random number distribution.
2167 * The formula for the normal probability mass function is
2168 * @f[
2169 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2170 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2171 * @f]
2173 template<typename _RealType = double>
2174 class lognormal_distribution
2176 static_assert(std::is_floating_point<_RealType>::value,
2177 "template argument not a floating point type");
2179 public:
2180 /** The type of the range of the distribution. */
2181 typedef _RealType result_type;
2182 /** Parameter type. */
2183 struct param_type
2185 typedef lognormal_distribution<_RealType> distribution_type;
2187 explicit
2188 param_type(_RealType __m = _RealType(0),
2189 _RealType __s = _RealType(1))
2190 : _M_m(__m), _M_s(__s)
2193 _RealType
2194 m() const
2195 { return _M_m; }
2197 _RealType
2198 s() const
2199 { return _M_s; }
2201 friend bool
2202 operator==(const param_type& __p1, const param_type& __p2)
2203 { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; }
2205 private:
2206 _RealType _M_m;
2207 _RealType _M_s;
2210 explicit
2211 lognormal_distribution(_RealType __m = _RealType(0),
2212 _RealType __s = _RealType(1))
2213 : _M_param(__m, __s), _M_nd()
2216 explicit
2217 lognormal_distribution(const param_type& __p)
2218 : _M_param(__p), _M_nd()
2222 * Resets the distribution state.
2224 void
2225 reset()
2226 { _M_nd.reset(); }
2231 _RealType
2232 m() const
2233 { return _M_param.m(); }
2235 _RealType
2236 s() const
2237 { return _M_param.s(); }
2240 * @brief Returns the parameter set of the distribution.
2242 param_type
2243 param() const
2244 { return _M_param; }
2247 * @brief Sets the parameter set of the distribution.
2248 * @param __param The new parameter set of the distribution.
2250 void
2251 param(const param_type& __param)
2252 { _M_param = __param; }
2255 * @brief Returns the greatest lower bound value of the distribution.
2257 result_type
2258 min() const
2259 { return result_type(0); }
2262 * @brief Returns the least upper bound value of the distribution.
2264 result_type
2265 max() const
2266 { return std::numeric_limits<result_type>::max(); }
2268 template<typename _UniformRandomNumberGenerator>
2269 result_type
2270 operator()(_UniformRandomNumberGenerator& __urng)
2271 { return this->operator()(__urng, this->param()); }
2273 template<typename _UniformRandomNumberGenerator>
2274 result_type
2275 operator()(_UniformRandomNumberGenerator& __urng,
2276 const param_type& __p)
2277 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
2280 * @brief Return true if two lognormal distributions have
2281 * the same parameters and the sequences that would
2282 * be generated are equal.
2284 template<typename _RealType1>
2285 friend bool
2286 operator==(const std::lognormal_distribution<_RealType1>& __d1,
2287 const std::lognormal_distribution<_RealType1>& __d2)
2288 { return (__d1.param() == __d2.param()
2289 && __d1._M_nd == __d2._M_nd); }
2292 * @brief Inserts a %lognormal_distribution random number distribution
2293 * @p __x into the output stream @p __os.
2295 * @param __os An output stream.
2296 * @param __x A %lognormal_distribution random number distribution.
2298 * @returns The output stream with the state of @p __x inserted or in
2299 * an error state.
2301 template<typename _RealType1, typename _CharT, typename _Traits>
2302 friend std::basic_ostream<_CharT, _Traits>&
2303 operator<<(std::basic_ostream<_CharT, _Traits>&,
2304 const std::lognormal_distribution<_RealType1>&);
2307 * @brief Extracts a %lognormal_distribution random number distribution
2308 * @p __x from the input stream @p __is.
2310 * @param __is An input stream.
2311 * @param __x A %lognormal_distribution random number
2312 * generator engine.
2314 * @returns The input stream with @p __x extracted or in an error state.
2316 template<typename _RealType1, typename _CharT, typename _Traits>
2317 friend std::basic_istream<_CharT, _Traits>&
2318 operator>>(std::basic_istream<_CharT, _Traits>&,
2319 std::lognormal_distribution<_RealType1>&);
2321 private:
2322 param_type _M_param;
2324 std::normal_distribution<result_type> _M_nd;
2328 * @brief Return true if two lognormal distributions are different.
2330 template<typename _RealType>
2331 inline bool
2332 operator!=(const std::lognormal_distribution<_RealType>& __d1,
2333 const std::lognormal_distribution<_RealType>& __d2)
2334 { return !(__d1 == __d2); }
2338 * @brief A gamma continuous distribution for random numbers.
2340 * The formula for the gamma probability density function is:
2341 * @f[
2342 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2343 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2344 * @f]
2346 template<typename _RealType = double>
2347 class gamma_distribution
2349 static_assert(std::is_floating_point<_RealType>::value,
2350 "template argument not a floating point type");
2352 public:
2353 /** The type of the range of the distribution. */
2354 typedef _RealType result_type;
2355 /** Parameter type. */
2356 struct param_type
2358 typedef gamma_distribution<_RealType> distribution_type;
2359 friend class gamma_distribution<_RealType>;
2361 explicit
2362 param_type(_RealType __alpha_val = _RealType(1),
2363 _RealType __beta_val = _RealType(1))
2364 : _M_alpha(__alpha_val), _M_beta(__beta_val)
2366 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
2367 _M_initialize();
2370 _RealType
2371 alpha() const
2372 { return _M_alpha; }
2374 _RealType
2375 beta() const
2376 { return _M_beta; }
2378 friend bool
2379 operator==(const param_type& __p1, const param_type& __p2)
2380 { return (__p1._M_alpha == __p2._M_alpha
2381 && __p1._M_beta == __p2._M_beta); }
2383 private:
2384 void
2385 _M_initialize();
2387 _RealType _M_alpha;
2388 _RealType _M_beta;
2390 _RealType _M_malpha, _M_a2;
2393 public:
2395 * @brief Constructs a gamma distribution with parameters
2396 * @f$\alpha@f$ and @f$\beta@f$.
2398 explicit
2399 gamma_distribution(_RealType __alpha_val = _RealType(1),
2400 _RealType __beta_val = _RealType(1))
2401 : _M_param(__alpha_val, __beta_val), _M_nd()
2404 explicit
2405 gamma_distribution(const param_type& __p)
2406 : _M_param(__p), _M_nd()
2410 * @brief Resets the distribution state.
2412 void
2413 reset()
2414 { _M_nd.reset(); }
2417 * @brief Returns the @f$\alpha@f$ of the distribution.
2419 _RealType
2420 alpha() const
2421 { return _M_param.alpha(); }
2424 * @brief Returns the @f$\beta@f$ of the distribution.
2426 _RealType
2427 beta() const
2428 { return _M_param.beta(); }
2431 * @brief Returns the parameter set of the distribution.
2433 param_type
2434 param() const
2435 { return _M_param; }
2438 * @brief Sets the parameter set of the distribution.
2439 * @param __param The new parameter set of the distribution.
2441 void
2442 param(const param_type& __param)
2443 { _M_param = __param; }
2446 * @brief Returns the greatest lower bound value of the distribution.
2448 result_type
2449 min() const
2450 { return result_type(0); }
2453 * @brief Returns the least upper bound value of the distribution.
2455 result_type
2456 max() const
2457 { return std::numeric_limits<result_type>::max(); }
2459 template<typename _UniformRandomNumberGenerator>
2460 result_type
2461 operator()(_UniformRandomNumberGenerator& __urng)
2462 { return this->operator()(__urng, this->param()); }
2464 template<typename _UniformRandomNumberGenerator>
2465 result_type
2466 operator()(_UniformRandomNumberGenerator& __urng,
2467 const param_type& __p);
2470 * @brief Return true if two gamma distributions have the same
2471 * parameters and the sequences that would be generated
2472 * are equal.
2474 template<typename _RealType1>
2475 friend bool
2476 operator==(const std::gamma_distribution<_RealType1>& __d1,
2477 const std::gamma_distribution<_RealType1>& __d2)
2478 { return (__d1.param() == __d2.param()
2479 && __d1._M_nd == __d2._M_nd); }
2482 * @brief Inserts a %gamma_distribution random number distribution
2483 * @p __x into the output stream @p __os.
2485 * @param __os An output stream.
2486 * @param __x A %gamma_distribution random number distribution.
2488 * @returns The output stream with the state of @p __x inserted or in
2489 * an error state.
2491 template<typename _RealType1, typename _CharT, typename _Traits>
2492 friend std::basic_ostream<_CharT, _Traits>&
2493 operator<<(std::basic_ostream<_CharT, _Traits>&,
2494 const std::gamma_distribution<_RealType1>&);
2497 * @brief Extracts a %gamma_distribution random number distribution
2498 * @p __x from the input stream @p __is.
2500 * @param __is An input stream.
2501 * @param __x A %gamma_distribution random number generator engine.
2503 * @returns The input stream with @p __x extracted or in an error state.
2505 template<typename _RealType1, typename _CharT, typename _Traits>
2506 friend std::basic_istream<_CharT, _Traits>&
2507 operator>>(std::basic_istream<_CharT, _Traits>&,
2508 std::gamma_distribution<_RealType1>&);
2510 private:
2511 param_type _M_param;
2513 std::normal_distribution<result_type> _M_nd;
2517 * @brief Return true if two gamma distributions are different.
2519 template<typename _RealType>
2520 inline bool
2521 operator!=(const std::gamma_distribution<_RealType>& __d1,
2522 const std::gamma_distribution<_RealType>& __d2)
2523 { return !(__d1 == __d2); }
2527 * @brief A chi_squared_distribution random number distribution.
2529 * The formula for the normal probability mass function is
2530 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2532 template<typename _RealType = double>
2533 class chi_squared_distribution
2535 static_assert(std::is_floating_point<_RealType>::value,
2536 "template argument not a floating point type");
2538 public:
2539 /** The type of the range of the distribution. */
2540 typedef _RealType result_type;
2541 /** Parameter type. */
2542 struct param_type
2544 typedef chi_squared_distribution<_RealType> distribution_type;
2546 explicit
2547 param_type(_RealType __n = _RealType(1))
2548 : _M_n(__n)
2551 _RealType
2552 n() const
2553 { return _M_n; }
2555 friend bool
2556 operator==(const param_type& __p1, const param_type& __p2)
2557 { return __p1._M_n == __p2._M_n; }
2559 private:
2560 _RealType _M_n;
2563 explicit
2564 chi_squared_distribution(_RealType __n = _RealType(1))
2565 : _M_param(__n), _M_gd(__n / 2)
2568 explicit
2569 chi_squared_distribution(const param_type& __p)
2570 : _M_param(__p), _M_gd(__p.n() / 2)
2574 * @brief Resets the distribution state.
2576 void
2577 reset()
2578 { _M_gd.reset(); }
2583 _RealType
2584 n() const
2585 { return _M_param.n(); }
2588 * @brief Returns the parameter set of the distribution.
2590 param_type
2591 param() const
2592 { return _M_param; }
2595 * @brief Sets the parameter set of the distribution.
2596 * @param __param The new parameter set of the distribution.
2598 void
2599 param(const param_type& __param)
2600 { _M_param = __param; }
2603 * @brief Returns the greatest lower bound value of the distribution.
2605 result_type
2606 min() const
2607 { return result_type(0); }
2610 * @brief Returns the least upper bound value of the distribution.
2612 result_type
2613 max() const
2614 { return std::numeric_limits<result_type>::max(); }
2616 template<typename _UniformRandomNumberGenerator>
2617 result_type
2618 operator()(_UniformRandomNumberGenerator& __urng)
2619 { return 2 * _M_gd(__urng); }
2621 template<typename _UniformRandomNumberGenerator>
2622 result_type
2623 operator()(_UniformRandomNumberGenerator& __urng,
2624 const param_type& __p)
2626 typedef typename std::gamma_distribution<result_type>::param_type
2627 param_type;
2628 return 2 * _M_gd(__urng, param_type(__p.n() / 2));
2632 * @brief Return true if two Chi-squared distributions have
2633 * the same parameters and the sequences that would be
2634 * generated are equal.
2636 template<typename _RealType1>
2637 friend bool
2638 operator==(const std::chi_squared_distribution<_RealType1>& __d1,
2639 const std::chi_squared_distribution<_RealType1>& __d2)
2640 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; }
2643 * @brief Inserts a %chi_squared_distribution random number distribution
2644 * @p __x into the output stream @p __os.
2646 * @param __os An output stream.
2647 * @param __x A %chi_squared_distribution random number distribution.
2649 * @returns The output stream with the state of @p __x inserted or in
2650 * an error state.
2652 template<typename _RealType1, typename _CharT, typename _Traits>
2653 friend std::basic_ostream<_CharT, _Traits>&
2654 operator<<(std::basic_ostream<_CharT, _Traits>&,
2655 const std::chi_squared_distribution<_RealType1>&);
2658 * @brief Extracts a %chi_squared_distribution random number distribution
2659 * @p __x from the input stream @p __is.
2661 * @param __is An input stream.
2662 * @param __x A %chi_squared_distribution random number
2663 * generator engine.
2665 * @returns The input stream with @p __x extracted or in an error state.
2667 template<typename _RealType1, typename _CharT, typename _Traits>
2668 friend std::basic_istream<_CharT, _Traits>&
2669 operator>>(std::basic_istream<_CharT, _Traits>&,
2670 std::chi_squared_distribution<_RealType1>&);
2672 private:
2673 param_type _M_param;
2675 std::gamma_distribution<result_type> _M_gd;
2679 * @brief Return true if two Chi-squared distributions are different.
2681 template<typename _RealType>
2682 inline bool
2683 operator!=(const std::chi_squared_distribution<_RealType>& __d1,
2684 const std::chi_squared_distribution<_RealType>& __d2)
2685 { return !(__d1 == __d2); }
2689 * @brief A cauchy_distribution random number distribution.
2691 * The formula for the normal probability mass function is
2692 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2694 template<typename _RealType = double>
2695 class cauchy_distribution
2697 static_assert(std::is_floating_point<_RealType>::value,
2698 "template argument not a floating point type");
2700 public:
2701 /** The type of the range of the distribution. */
2702 typedef _RealType result_type;
2703 /** Parameter type. */
2704 struct param_type
2706 typedef cauchy_distribution<_RealType> distribution_type;
2708 explicit
2709 param_type(_RealType __a = _RealType(0),
2710 _RealType __b = _RealType(1))
2711 : _M_a(__a), _M_b(__b)
2714 _RealType
2715 a() const
2716 { return _M_a; }
2718 _RealType
2719 b() const
2720 { return _M_b; }
2722 friend bool
2723 operator==(const param_type& __p1, const param_type& __p2)
2724 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
2726 private:
2727 _RealType _M_a;
2728 _RealType _M_b;
2731 explicit
2732 cauchy_distribution(_RealType __a = _RealType(0),
2733 _RealType __b = _RealType(1))
2734 : _M_param(__a, __b)
2737 explicit
2738 cauchy_distribution(const param_type& __p)
2739 : _M_param(__p)
2743 * @brief Resets the distribution state.
2745 void
2746 reset()
2752 _RealType
2753 a() const
2754 { return _M_param.a(); }
2756 _RealType
2757 b() const
2758 { return _M_param.b(); }
2761 * @brief Returns the parameter set of the distribution.
2763 param_type
2764 param() const
2765 { return _M_param; }
2768 * @brief Sets the parameter set of the distribution.
2769 * @param __param The new parameter set of the distribution.
2771 void
2772 param(const param_type& __param)
2773 { _M_param = __param; }
2776 * @brief Returns the greatest lower bound value of the distribution.
2778 result_type
2779 min() const
2780 { return std::numeric_limits<result_type>::min(); }
2783 * @brief Returns the least upper bound value of the distribution.
2785 result_type
2786 max() const
2787 { return std::numeric_limits<result_type>::max(); }
2789 template<typename _UniformRandomNumberGenerator>
2790 result_type
2791 operator()(_UniformRandomNumberGenerator& __urng)
2792 { return this->operator()(__urng, this->param()); }
2794 template<typename _UniformRandomNumberGenerator>
2795 result_type
2796 operator()(_UniformRandomNumberGenerator& __urng,
2797 const param_type& __p);
2799 private:
2800 param_type _M_param;
2804 * @brief Return true if two Cauchy distributions have
2805 * the same parameters.
2807 template<typename _RealType>
2808 inline bool
2809 operator==(const std::cauchy_distribution<_RealType>& __d1,
2810 const std::cauchy_distribution<_RealType>& __d2)
2811 { return __d1.param() == __d2.param(); }
2814 * @brief Return true if two Cauchy distributions have
2815 * different parameters.
2817 template<typename _RealType>
2818 inline bool
2819 operator!=(const std::cauchy_distribution<_RealType>& __d1,
2820 const std::cauchy_distribution<_RealType>& __d2)
2821 { return !(__d1 == __d2); }
2824 * @brief Inserts a %cauchy_distribution random number distribution
2825 * @p __x into the output stream @p __os.
2827 * @param __os An output stream.
2828 * @param __x A %cauchy_distribution random number distribution.
2830 * @returns The output stream with the state of @p __x inserted or in
2831 * an error state.
2833 template<typename _RealType, typename _CharT, typename _Traits>
2834 std::basic_ostream<_CharT, _Traits>&
2835 operator<<(std::basic_ostream<_CharT, _Traits>&,
2836 const std::cauchy_distribution<_RealType>&);
2839 * @brief Extracts a %cauchy_distribution random number distribution
2840 * @p __x from the input stream @p __is.
2842 * @param __is An input stream.
2843 * @param __x A %cauchy_distribution random number
2844 * generator engine.
2846 * @returns The input stream with @p __x extracted or in an error state.
2848 template<typename _RealType, typename _CharT, typename _Traits>
2849 std::basic_istream<_CharT, _Traits>&
2850 operator>>(std::basic_istream<_CharT, _Traits>&,
2851 std::cauchy_distribution<_RealType>&);
2855 * @brief A fisher_f_distribution random number distribution.
2857 * The formula for the normal probability mass function is
2858 * @f[
2859 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
2860 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
2861 * (1 + \frac{mx}{n})^{-(m+n)/2}
2862 * @f]
2864 template<typename _RealType = double>
2865 class fisher_f_distribution
2867 static_assert(std::is_floating_point<_RealType>::value,
2868 "template argument not a floating point type");
2870 public:
2871 /** The type of the range of the distribution. */
2872 typedef _RealType result_type;
2873 /** Parameter type. */
2874 struct param_type
2876 typedef fisher_f_distribution<_RealType> distribution_type;
2878 explicit
2879 param_type(_RealType __m = _RealType(1),
2880 _RealType __n = _RealType(1))
2881 : _M_m(__m), _M_n(__n)
2884 _RealType
2885 m() const
2886 { return _M_m; }
2888 _RealType
2889 n() const
2890 { return _M_n; }
2892 friend bool
2893 operator==(const param_type& __p1, const param_type& __p2)
2894 { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; }
2896 private:
2897 _RealType _M_m;
2898 _RealType _M_n;
2901 explicit
2902 fisher_f_distribution(_RealType __m = _RealType(1),
2903 _RealType __n = _RealType(1))
2904 : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2)
2907 explicit
2908 fisher_f_distribution(const param_type& __p)
2909 : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2)
2913 * @brief Resets the distribution state.
2915 void
2916 reset()
2918 _M_gd_x.reset();
2919 _M_gd_y.reset();
2925 _RealType
2926 m() const
2927 { return _M_param.m(); }
2929 _RealType
2930 n() const
2931 { return _M_param.n(); }
2934 * @brief Returns the parameter set of the distribution.
2936 param_type
2937 param() const
2938 { return _M_param; }
2941 * @brief Sets the parameter set of the distribution.
2942 * @param __param The new parameter set of the distribution.
2944 void
2945 param(const param_type& __param)
2946 { _M_param = __param; }
2949 * @brief Returns the greatest lower bound value of the distribution.
2951 result_type
2952 min() const
2953 { return result_type(0); }
2956 * @brief Returns the least upper bound value of the distribution.
2958 result_type
2959 max() const
2960 { return std::numeric_limits<result_type>::max(); }
2962 template<typename _UniformRandomNumberGenerator>
2963 result_type
2964 operator()(_UniformRandomNumberGenerator& __urng)
2965 { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); }
2967 template<typename _UniformRandomNumberGenerator>
2968 result_type
2969 operator()(_UniformRandomNumberGenerator& __urng,
2970 const param_type& __p)
2972 typedef typename std::gamma_distribution<result_type>::param_type
2973 param_type;
2974 return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n())
2975 / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m()));
2979 * @brief Return true if two Fisher f distributions have
2980 * the same parameters and the sequences that would
2981 * be generated are equal.
2983 template<typename _RealType1>
2984 friend bool
2985 operator==(const std::fisher_f_distribution<_RealType1>& __d1,
2986 const std::fisher_f_distribution<_RealType1>& __d2)
2987 { return (__d1.param() == __d2.param()
2988 && __d1._M_gd_x == __d2._M_gd_x
2989 && __d1._M_gd_y == __d2._M_gd_y); }
2992 * @brief Inserts a %fisher_f_distribution random number distribution
2993 * @p __x into the output stream @p __os.
2995 * @param __os An output stream.
2996 * @param __x A %fisher_f_distribution random number distribution.
2998 * @returns The output stream with the state of @p __x inserted or in
2999 * an error state.
3001 template<typename _RealType1, typename _CharT, typename _Traits>
3002 friend std::basic_ostream<_CharT, _Traits>&
3003 operator<<(std::basic_ostream<_CharT, _Traits>&,
3004 const std::fisher_f_distribution<_RealType1>&);
3007 * @brief Extracts a %fisher_f_distribution random number distribution
3008 * @p __x from the input stream @p __is.
3010 * @param __is An input stream.
3011 * @param __x A %fisher_f_distribution random number
3012 * generator engine.
3014 * @returns The input stream with @p __x extracted or in an error state.
3016 template<typename _RealType1, typename _CharT, typename _Traits>
3017 friend std::basic_istream<_CharT, _Traits>&
3018 operator>>(std::basic_istream<_CharT, _Traits>&,
3019 std::fisher_f_distribution<_RealType1>&);
3021 private:
3022 param_type _M_param;
3024 std::gamma_distribution<result_type> _M_gd_x, _M_gd_y;
3028 * @brief Return true if two Fisher f distributions are diferent.
3030 template<typename _RealType>
3031 inline bool
3032 operator!=(const std::fisher_f_distribution<_RealType>& __d1,
3033 const std::fisher_f_distribution<_RealType>& __d2)
3034 { return !(__d1 == __d2); }
3037 * @brief A student_t_distribution random number distribution.
3039 * The formula for the normal probability mass function is:
3040 * @f[
3041 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3042 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3043 * @f]
3045 template<typename _RealType = double>
3046 class student_t_distribution
3048 static_assert(std::is_floating_point<_RealType>::value,
3049 "template argument not a floating point type");
3051 public:
3052 /** The type of the range of the distribution. */
3053 typedef _RealType result_type;
3054 /** Parameter type. */
3055 struct param_type
3057 typedef student_t_distribution<_RealType> distribution_type;
3059 explicit
3060 param_type(_RealType __n = _RealType(1))
3061 : _M_n(__n)
3064 _RealType
3065 n() const
3066 { return _M_n; }
3068 friend bool
3069 operator==(const param_type& __p1, const param_type& __p2)
3070 { return __p1._M_n == __p2._M_n; }
3072 private:
3073 _RealType _M_n;
3076 explicit
3077 student_t_distribution(_RealType __n = _RealType(1))
3078 : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2)
3081 explicit
3082 student_t_distribution(const param_type& __p)
3083 : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2)
3087 * @brief Resets the distribution state.
3089 void
3090 reset()
3092 _M_nd.reset();
3093 _M_gd.reset();
3099 _RealType
3100 n() const
3101 { return _M_param.n(); }
3104 * @brief Returns the parameter set of the distribution.
3106 param_type
3107 param() const
3108 { return _M_param; }
3111 * @brief Sets the parameter set of the distribution.
3112 * @param __param The new parameter set of the distribution.
3114 void
3115 param(const param_type& __param)
3116 { _M_param = __param; }
3119 * @brief Returns the greatest lower bound value of the distribution.
3121 result_type
3122 min() const
3123 { return std::numeric_limits<result_type>::min(); }
3126 * @brief Returns the least upper bound value of the distribution.
3128 result_type
3129 max() const
3130 { return std::numeric_limits<result_type>::max(); }
3132 template<typename _UniformRandomNumberGenerator>
3133 result_type
3134 operator()(_UniformRandomNumberGenerator& __urng)
3135 { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); }
3137 template<typename _UniformRandomNumberGenerator>
3138 result_type
3139 operator()(_UniformRandomNumberGenerator& __urng,
3140 const param_type& __p)
3142 typedef typename std::gamma_distribution<result_type>::param_type
3143 param_type;
3145 const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2));
3146 return _M_nd(__urng) * std::sqrt(__p.n() / __g);
3150 * @brief Return true if two Student t distributions have
3151 * the same parameters and the sequences that would
3152 * be generated are equal.
3154 template<typename _RealType1>
3155 friend bool
3156 operator==(const std::student_t_distribution<_RealType1>& __d1,
3157 const std::student_t_distribution<_RealType1>& __d2)
3158 { return (__d1.param() == __d2.param()
3159 && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); }
3162 * @brief Inserts a %student_t_distribution random number distribution
3163 * @p __x into the output stream @p __os.
3165 * @param __os An output stream.
3166 * @param __x A %student_t_distribution random number distribution.
3168 * @returns The output stream with the state of @p __x inserted or in
3169 * an error state.
3171 template<typename _RealType1, typename _CharT, typename _Traits>
3172 friend std::basic_ostream<_CharT, _Traits>&
3173 operator<<(std::basic_ostream<_CharT, _Traits>&,
3174 const std::student_t_distribution<_RealType1>&);
3177 * @brief Extracts a %student_t_distribution random number distribution
3178 * @p __x from the input stream @p __is.
3180 * @param __is An input stream.
3181 * @param __x A %student_t_distribution random number
3182 * generator engine.
3184 * @returns The input stream with @p __x extracted or in an error state.
3186 template<typename _RealType1, typename _CharT, typename _Traits>
3187 friend std::basic_istream<_CharT, _Traits>&
3188 operator>>(std::basic_istream<_CharT, _Traits>&,
3189 std::student_t_distribution<_RealType1>&);
3191 private:
3192 param_type _M_param;
3194 std::normal_distribution<result_type> _M_nd;
3195 std::gamma_distribution<result_type> _M_gd;
3199 * @brief Return true if two Student t distributions are different.
3201 template<typename _RealType>
3202 inline bool
3203 operator!=(const std::student_t_distribution<_RealType>& __d1,
3204 const std::student_t_distribution<_RealType>& __d2)
3205 { return !(__d1 == __d2); }
3208 /* @} */ // group random_distributions_normal
3211 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3212 * @ingroup random_distributions
3213 * @{
3217 * @brief A Bernoulli random number distribution.
3219 * Generates a sequence of true and false values with likelihood @f$p@f$
3220 * that true will come up and @f$(1 - p)@f$ that false will appear.
3222 class bernoulli_distribution
3224 public:
3225 /** The type of the range of the distribution. */
3226 typedef bool result_type;
3227 /** Parameter type. */
3228 struct param_type
3230 typedef bernoulli_distribution distribution_type;
3232 explicit
3233 param_type(double __p = 0.5)
3234 : _M_p(__p)
3236 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
3239 double
3240 p() const
3241 { return _M_p; }
3243 friend bool
3244 operator==(const param_type& __p1, const param_type& __p2)
3245 { return __p1._M_p == __p2._M_p; }
3247 private:
3248 double _M_p;
3251 public:
3253 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3255 * @param __p [IN] The likelihood of a true result being returned.
3256 * Must be in the interval @f$[0, 1]@f$.
3258 explicit
3259 bernoulli_distribution(double __p = 0.5)
3260 : _M_param(__p)
3263 explicit
3264 bernoulli_distribution(const param_type& __p)
3265 : _M_param(__p)
3269 * @brief Resets the distribution state.
3271 * Does nothing for a Bernoulli distribution.
3273 void
3274 reset() { }
3277 * @brief Returns the @p p parameter of the distribution.
3279 double
3280 p() const
3281 { return _M_param.p(); }
3284 * @brief Returns the parameter set of the distribution.
3286 param_type
3287 param() const
3288 { return _M_param; }
3291 * @brief Sets the parameter set of the distribution.
3292 * @param __param The new parameter set of the distribution.
3294 void
3295 param(const param_type& __param)
3296 { _M_param = __param; }
3299 * @brief Returns the greatest lower bound value of the distribution.
3301 result_type
3302 min() const
3303 { return std::numeric_limits<result_type>::min(); }
3306 * @brief Returns the least upper bound value of the distribution.
3308 result_type
3309 max() const
3310 { return std::numeric_limits<result_type>::max(); }
3313 * @brief Returns the next value in the Bernoullian sequence.
3315 template<typename _UniformRandomNumberGenerator>
3316 result_type
3317 operator()(_UniformRandomNumberGenerator& __urng)
3318 { return this->operator()(__urng, this->param()); }
3320 template<typename _UniformRandomNumberGenerator>
3321 result_type
3322 operator()(_UniformRandomNumberGenerator& __urng,
3323 const param_type& __p)
3325 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3326 __aurng(__urng);
3327 if ((__aurng() - __aurng.min())
3328 < __p.p() * (__aurng.max() - __aurng.min()))
3329 return true;
3330 return false;
3333 private:
3334 param_type _M_param;
3338 * @brief Return true if two Bernoulli distributions have
3339 * the same parameters.
3341 inline bool
3342 operator==(const std::bernoulli_distribution& __d1,
3343 const std::bernoulli_distribution& __d2)
3344 { return __d1.param() == __d2.param(); }
3347 * @brief Return true if two Bernoulli distributions have
3348 * different parameters.
3350 inline bool
3351 operator!=(const std::bernoulli_distribution& __d1,
3352 const std::bernoulli_distribution& __d2)
3353 { return !(__d1 == __d2); }
3356 * @brief Inserts a %bernoulli_distribution random number distribution
3357 * @p __x into the output stream @p __os.
3359 * @param __os An output stream.
3360 * @param __x A %bernoulli_distribution random number distribution.
3362 * @returns The output stream with the state of @p __x inserted or in
3363 * an error state.
3365 template<typename _CharT, typename _Traits>
3366 std::basic_ostream<_CharT, _Traits>&
3367 operator<<(std::basic_ostream<_CharT, _Traits>&,
3368 const std::bernoulli_distribution&);
3371 * @brief Extracts a %bernoulli_distribution random number distribution
3372 * @p __x from the input stream @p __is.
3374 * @param __is An input stream.
3375 * @param __x A %bernoulli_distribution random number generator engine.
3377 * @returns The input stream with @p __x extracted or in an error state.
3379 template<typename _CharT, typename _Traits>
3380 std::basic_istream<_CharT, _Traits>&
3381 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3382 std::bernoulli_distribution& __x)
3384 double __p;
3385 __is >> __p;
3386 __x.param(bernoulli_distribution::param_type(__p));
3387 return __is;
3392 * @brief A discrete binomial random number distribution.
3394 * The formula for the binomial probability density function is
3395 * @f$p(i|t,p) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3396 * and @f$p@f$ are the parameters of the distribution.
3398 template<typename _IntType = int>
3399 class binomial_distribution
3401 static_assert(std::is_integral<_IntType>::value,
3402 "template argument not an integral type");
3404 public:
3405 /** The type of the range of the distribution. */
3406 typedef _IntType result_type;
3407 /** Parameter type. */
3408 struct param_type
3410 typedef binomial_distribution<_IntType> distribution_type;
3411 friend class binomial_distribution<_IntType>;
3413 explicit
3414 param_type(_IntType __t = _IntType(1), double __p = 0.5)
3415 : _M_t(__t), _M_p(__p)
3417 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3418 && (_M_p >= 0.0)
3419 && (_M_p <= 1.0));
3420 _M_initialize();
3423 _IntType
3424 t() const
3425 { return _M_t; }
3427 double
3428 p() const
3429 { return _M_p; }
3431 friend bool
3432 operator==(const param_type& __p1, const param_type& __p2)
3433 { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; }
3435 private:
3436 void
3437 _M_initialize();
3439 _IntType _M_t;
3440 double _M_p;
3442 double _M_q;
3443 #if _GLIBCXX_USE_C99_MATH_TR1
3444 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3445 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3446 #endif
3447 bool _M_easy;
3450 // constructors and member function
3451 explicit
3452 binomial_distribution(_IntType __t = _IntType(1),
3453 double __p = 0.5)
3454 : _M_param(__t, __p), _M_nd()
3457 explicit
3458 binomial_distribution(const param_type& __p)
3459 : _M_param(__p), _M_nd()
3463 * @brief Resets the distribution state.
3465 void
3466 reset()
3467 { _M_nd.reset(); }
3470 * @brief Returns the distribution @p t parameter.
3472 _IntType
3473 t() const
3474 { return _M_param.t(); }
3477 * @brief Returns the distribution @p p parameter.
3479 double
3480 p() const
3481 { return _M_param.p(); }
3484 * @brief Returns the parameter set of the distribution.
3486 param_type
3487 param() const
3488 { return _M_param; }
3491 * @brief Sets the parameter set of the distribution.
3492 * @param __param The new parameter set of the distribution.
3494 void
3495 param(const param_type& __param)
3496 { _M_param = __param; }
3499 * @brief Returns the greatest lower bound value of the distribution.
3501 result_type
3502 min() const
3503 { return 0; }
3506 * @brief Returns the least upper bound value of the distribution.
3508 result_type
3509 max() const
3510 { return _M_param.t(); }
3513 * @brief Return true if two binomial distributions have
3514 * the same parameters and the sequences that would
3515 * be generated are equal.
3517 template<typename _IntType1>
3518 friend bool
3519 operator==(const std::binomial_distribution<_IntType1>& __d1,
3520 const std::binomial_distribution<_IntType1>& __d2)
3521 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3522 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; }
3523 #else
3524 { return __d1.param() == __d2.param(); }
3525 #endif
3527 template<typename _UniformRandomNumberGenerator>
3528 result_type
3529 operator()(_UniformRandomNumberGenerator& __urng)
3530 { return this->operator()(__urng, this->param()); }
3532 template<typename _UniformRandomNumberGenerator>
3533 result_type
3534 operator()(_UniformRandomNumberGenerator& __urng,
3535 const param_type& __p);
3538 * @brief Inserts a %binomial_distribution random number distribution
3539 * @p __x into the output stream @p __os.
3541 * @param __os An output stream.
3542 * @param __x A %binomial_distribution random number distribution.
3544 * @returns The output stream with the state of @p __x inserted or in
3545 * an error state.
3547 template<typename _IntType1,
3548 typename _CharT, typename _Traits>
3549 friend std::basic_ostream<_CharT, _Traits>&
3550 operator<<(std::basic_ostream<_CharT, _Traits>&,
3551 const std::binomial_distribution<_IntType1>&);
3554 * @brief Extracts a %binomial_distribution random number distribution
3555 * @p __x from the input stream @p __is.
3557 * @param __is An input stream.
3558 * @param __x A %binomial_distribution random number generator engine.
3560 * @returns The input stream with @p __x extracted or in an error
3561 * state.
3563 template<typename _IntType1,
3564 typename _CharT, typename _Traits>
3565 friend std::basic_istream<_CharT, _Traits>&
3566 operator>>(std::basic_istream<_CharT, _Traits>&,
3567 std::binomial_distribution<_IntType1>&);
3569 private:
3570 template<typename _UniformRandomNumberGenerator>
3571 result_type
3572 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3574 param_type _M_param;
3576 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3577 std::normal_distribution<double> _M_nd;
3581 * @brief Return true if two binomial distributions are different.
3583 template<typename _IntType>
3584 inline bool
3585 operator!=(const std::binomial_distribution<_IntType>& __d1,
3586 const std::binomial_distribution<_IntType>& __d2)
3587 { return !(__d1 == __d2); }
3591 * @brief A discrete geometric random number distribution.
3593 * The formula for the geometric probability density function is
3594 * @f$p(i|p) = (1 - p)p^{i-1}@f$ where @f$p@f$ is the parameter of the
3595 * distribution.
3597 template<typename _IntType = int>
3598 class geometric_distribution
3600 static_assert(std::is_integral<_IntType>::value,
3601 "template argument not an integral type");
3603 public:
3604 /** The type of the range of the distribution. */
3605 typedef _IntType result_type;
3606 /** Parameter type. */
3607 struct param_type
3609 typedef geometric_distribution<_IntType> distribution_type;
3610 friend class geometric_distribution<_IntType>;
3612 explicit
3613 param_type(double __p = 0.5)
3614 : _M_p(__p)
3616 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0)
3617 && (_M_p <= 1.0));
3618 _M_initialize();
3621 double
3622 p() const
3623 { return _M_p; }
3625 friend bool
3626 operator==(const param_type& __p1, const param_type& __p2)
3627 { return __p1._M_p == __p2._M_p; }
3629 private:
3630 void
3631 _M_initialize()
3632 { _M_log_p = std::log(_M_p); }
3634 double _M_p;
3636 double _M_log_p;
3639 // constructors and member function
3640 explicit
3641 geometric_distribution(double __p = 0.5)
3642 : _M_param(__p)
3645 explicit
3646 geometric_distribution(const param_type& __p)
3647 : _M_param(__p)
3651 * @brief Resets the distribution state.
3653 * Does nothing for the geometric distribution.
3655 void
3656 reset() { }
3659 * @brief Returns the distribution parameter @p p.
3661 double
3662 p() const
3663 { return _M_param.p(); }
3666 * @brief Returns the parameter set of the distribution.
3668 param_type
3669 param() const
3670 { return _M_param; }
3673 * @brief Sets the parameter set of the distribution.
3674 * @param __param The new parameter set of the distribution.
3676 void
3677 param(const param_type& __param)
3678 { _M_param = __param; }
3681 * @brief Returns the greatest lower bound value of the distribution.
3683 result_type
3684 min() const
3685 { return 0; }
3688 * @brief Returns the least upper bound value of the distribution.
3690 result_type
3691 max() const
3692 { return std::numeric_limits<result_type>::max(); }
3694 template<typename _UniformRandomNumberGenerator>
3695 result_type
3696 operator()(_UniformRandomNumberGenerator& __urng)
3697 { return this->operator()(__urng, this->param()); }
3699 template<typename _UniformRandomNumberGenerator>
3700 result_type
3701 operator()(_UniformRandomNumberGenerator& __urng,
3702 const param_type& __p);
3704 private:
3705 param_type _M_param;
3709 * @brief Return true if two geometric distributions have
3710 * the same parameters.
3712 template<typename _IntType>
3713 inline bool
3714 operator==(const std::geometric_distribution<_IntType>& __d1,
3715 const std::geometric_distribution<_IntType>& __d2)
3716 { return __d1.param() == __d2.param(); }
3719 * @brief Return true if two geometric distributions have
3720 * different parameters.
3722 template<typename _IntType>
3723 inline bool
3724 operator!=(const std::geometric_distribution<_IntType>& __d1,
3725 const std::geometric_distribution<_IntType>& __d2)
3726 { return !(__d1 == __d2); }
3729 * @brief Inserts a %geometric_distribution random number distribution
3730 * @p __x into the output stream @p __os.
3732 * @param __os An output stream.
3733 * @param __x A %geometric_distribution random number distribution.
3735 * @returns The output stream with the state of @p __x inserted or in
3736 * an error state.
3738 template<typename _IntType,
3739 typename _CharT, typename _Traits>
3740 std::basic_ostream<_CharT, _Traits>&
3741 operator<<(std::basic_ostream<_CharT, _Traits>&,
3742 const std::geometric_distribution<_IntType>&);
3745 * @brief Extracts a %geometric_distribution random number distribution
3746 * @p __x from the input stream @p __is.
3748 * @param __is An input stream.
3749 * @param __x A %geometric_distribution random number generator engine.
3751 * @returns The input stream with @p __x extracted or in an error state.
3753 template<typename _IntType,
3754 typename _CharT, typename _Traits>
3755 std::basic_istream<_CharT, _Traits>&
3756 operator>>(std::basic_istream<_CharT, _Traits>&,
3757 std::geometric_distribution<_IntType>&);
3761 * @brief A negative_binomial_distribution random number distribution.
3763 * The formula for the negative binomial probability mass function is
3764 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
3765 * and @f$p@f$ are the parameters of the distribution.
3767 template<typename _IntType = int>
3768 class negative_binomial_distribution
3770 static_assert(std::is_integral<_IntType>::value,
3771 "template argument not an integral type");
3773 public:
3774 /** The type of the range of the distribution. */
3775 typedef _IntType result_type;
3776 /** Parameter type. */
3777 struct param_type
3779 typedef negative_binomial_distribution<_IntType> distribution_type;
3781 explicit
3782 param_type(_IntType __k = 1, double __p = 0.5)
3783 : _M_k(__k), _M_p(__p)
3786 _IntType
3787 k() const
3788 { return _M_k; }
3790 double
3791 p() const
3792 { return _M_p; }
3794 friend bool
3795 operator==(const param_type& __p1, const param_type& __p2)
3796 { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; }
3798 private:
3799 _IntType _M_k;
3800 double _M_p;
3803 explicit
3804 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
3805 : _M_param(__k, __p), _M_gd(__k, __p / (1.0 - __p))
3808 explicit
3809 negative_binomial_distribution(const param_type& __p)
3810 : _M_param(__p), _M_gd(__p.k(), __p.p() / (1.0 - __p.p()))
3814 * @brief Resets the distribution state.
3816 void
3817 reset()
3818 { _M_gd.reset(); }
3821 * @brief Return the @f$k@f$ parameter of the distribution.
3823 _IntType
3824 k() const
3825 { return _M_param.k(); }
3828 * @brief Return the @f$p@f$ parameter of the distribution.
3830 double
3831 p() const
3832 { return _M_param.p(); }
3835 * @brief Returns the parameter set of the distribution.
3837 param_type
3838 param() const
3839 { return _M_param; }
3842 * @brief Sets the parameter set of the distribution.
3843 * @param __param The new parameter set of the distribution.
3845 void
3846 param(const param_type& __param)
3847 { _M_param = __param; }
3850 * @brief Returns the greatest lower bound value of the distribution.
3852 result_type
3853 min() const
3854 { return result_type(0); }
3857 * @brief Returns the least upper bound value of the distribution.
3859 result_type
3860 max() const
3861 { return std::numeric_limits<result_type>::max(); }
3863 template<typename _UniformRandomNumberGenerator>
3864 result_type
3865 operator()(_UniformRandomNumberGenerator& __urng);
3867 template<typename _UniformRandomNumberGenerator>
3868 result_type
3869 operator()(_UniformRandomNumberGenerator& __urng,
3870 const param_type& __p);
3873 * @brief Return true if two negative binomial distributions have
3874 * the same parameters and the sequences that would be
3875 * generated are equal.
3877 template<typename _IntType1>
3878 friend bool
3879 operator==(const std::negative_binomial_distribution<_IntType1>& __d1,
3880 const std::negative_binomial_distribution<_IntType1>& __d2)
3881 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; }
3884 * @brief Inserts a %negative_binomial_distribution random
3885 * number distribution @p __x into the output stream @p __os.
3887 * @param __os An output stream.
3888 * @param __x A %negative_binomial_distribution random number
3889 * distribution.
3891 * @returns The output stream with the state of @p __x inserted or in
3892 * an error state.
3894 template<typename _IntType1, typename _CharT, typename _Traits>
3895 friend std::basic_ostream<_CharT, _Traits>&
3896 operator<<(std::basic_ostream<_CharT, _Traits>&,
3897 const std::negative_binomial_distribution<_IntType1>&);
3900 * @brief Extracts a %negative_binomial_distribution random number
3901 * distribution @p __x from the input stream @p __is.
3903 * @param __is An input stream.
3904 * @param __x A %negative_binomial_distribution random number
3905 * generator engine.
3907 * @returns The input stream with @p __x extracted or in an error state.
3909 template<typename _IntType1, typename _CharT, typename _Traits>
3910 friend std::basic_istream<_CharT, _Traits>&
3911 operator>>(std::basic_istream<_CharT, _Traits>&,
3912 std::negative_binomial_distribution<_IntType1>&);
3914 private:
3915 param_type _M_param;
3917 std::gamma_distribution<double> _M_gd;
3921 * @brief Return true if two negative binomial distributions are different.
3923 template<typename _IntType>
3924 inline bool
3925 operator!=(const std::negative_binomial_distribution<_IntType>& __d1,
3926 const std::negative_binomial_distribution<_IntType>& __d2)
3927 { return !(__d1 == __d2); }
3930 /* @} */ // group random_distributions_bernoulli
3933 * @addtogroup random_distributions_poisson Poisson Distributions
3934 * @ingroup random_distributions
3935 * @{
3939 * @brief A discrete Poisson random number distribution.
3941 * The formula for the Poisson probability density function is
3942 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
3943 * parameter of the distribution.
3945 template<typename _IntType = int>
3946 class poisson_distribution
3948 static_assert(std::is_integral<_IntType>::value,
3949 "template argument not an integral type");
3951 public:
3952 /** The type of the range of the distribution. */
3953 typedef _IntType result_type;
3954 /** Parameter type. */
3955 struct param_type
3957 typedef poisson_distribution<_IntType> distribution_type;
3958 friend class poisson_distribution<_IntType>;
3960 explicit
3961 param_type(double __mean = 1.0)
3962 : _M_mean(__mean)
3964 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
3965 _M_initialize();
3968 double
3969 mean() const
3970 { return _M_mean; }
3972 friend bool
3973 operator==(const param_type& __p1, const param_type& __p2)
3974 { return __p1._M_mean == __p2._M_mean; }
3976 private:
3977 // Hosts either log(mean) or the threshold of the simple method.
3978 void
3979 _M_initialize();
3981 double _M_mean;
3983 double _M_lm_thr;
3984 #if _GLIBCXX_USE_C99_MATH_TR1
3985 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
3986 #endif
3989 // constructors and member function
3990 explicit
3991 poisson_distribution(double __mean = 1.0)
3992 : _M_param(__mean), _M_nd()
3995 explicit
3996 poisson_distribution(const param_type& __p)
3997 : _M_param(__p), _M_nd()
4001 * @brief Resets the distribution state.
4003 void
4004 reset()
4005 { _M_nd.reset(); }
4008 * @brief Returns the distribution parameter @p mean.
4010 double
4011 mean() const
4012 { return _M_param.mean(); }
4015 * @brief Returns the parameter set of the distribution.
4017 param_type
4018 param() const
4019 { return _M_param; }
4022 * @brief Sets the parameter set of the distribution.
4023 * @param __param The new parameter set of the distribution.
4025 void
4026 param(const param_type& __param)
4027 { _M_param = __param; }
4030 * @brief Returns the greatest lower bound value of the distribution.
4032 result_type
4033 min() const
4034 { return 0; }
4037 * @brief Returns the least upper bound value of the distribution.
4039 result_type
4040 max() const
4041 { return std::numeric_limits<result_type>::max(); }
4043 template<typename _UniformRandomNumberGenerator>
4044 result_type
4045 operator()(_UniformRandomNumberGenerator& __urng)
4046 { return this->operator()(__urng, this->param()); }
4048 template<typename _UniformRandomNumberGenerator>
4049 result_type
4050 operator()(_UniformRandomNumberGenerator& __urng,
4051 const param_type& __p);
4054 * @brief Return true if two Poisson distributions have the same
4055 * parameters and the sequences that would be generated
4056 * are equal.
4058 template<typename _IntType1>
4059 friend bool
4060 operator==(const std::poisson_distribution<_IntType1>& __d1,
4061 const std::poisson_distribution<_IntType1>& __d2)
4062 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4063 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; }
4064 #else
4065 { return __d1.param() == __d2.param(); }
4066 #endif
4069 * @brief Inserts a %poisson_distribution random number distribution
4070 * @p __x into the output stream @p __os.
4072 * @param __os An output stream.
4073 * @param __x A %poisson_distribution random number distribution.
4075 * @returns The output stream with the state of @p __x inserted or in
4076 * an error state.
4078 template<typename _IntType1, typename _CharT, typename _Traits>
4079 friend std::basic_ostream<_CharT, _Traits>&
4080 operator<<(std::basic_ostream<_CharT, _Traits>&,
4081 const std::poisson_distribution<_IntType1>&);
4084 * @brief Extracts a %poisson_distribution random number distribution
4085 * @p __x from the input stream @p __is.
4087 * @param __is An input stream.
4088 * @param __x A %poisson_distribution random number generator engine.
4090 * @returns The input stream with @p __x extracted or in an error
4091 * state.
4093 template<typename _IntType1, typename _CharT, typename _Traits>
4094 friend std::basic_istream<_CharT, _Traits>&
4095 operator>>(std::basic_istream<_CharT, _Traits>&,
4096 std::poisson_distribution<_IntType1>&);
4098 private:
4099 param_type _M_param;
4101 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4102 std::normal_distribution<double> _M_nd;
4106 * @brief Return true if two Poisson distributions are different.
4108 template<typename _IntType>
4109 inline bool
4110 operator!=(const std::poisson_distribution<_IntType>& __d1,
4111 const std::poisson_distribution<_IntType>& __d2)
4112 { return !(__d1 == __d2); }
4116 * @brief An exponential continuous distribution for random numbers.
4118 * The formula for the exponential probability density function is
4119 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4121 * <table border=1 cellpadding=10 cellspacing=0>
4122 * <caption align=top>Distribution Statistics</caption>
4123 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4124 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4125 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4126 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4127 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4128 * </table>
4130 template<typename _RealType = double>
4131 class exponential_distribution
4133 static_assert(std::is_floating_point<_RealType>::value,
4134 "template argument not a floating point type");
4136 public:
4137 /** The type of the range of the distribution. */
4138 typedef _RealType result_type;
4139 /** Parameter type. */
4140 struct param_type
4142 typedef exponential_distribution<_RealType> distribution_type;
4144 explicit
4145 param_type(_RealType __lambda = _RealType(1))
4146 : _M_lambda(__lambda)
4148 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
4151 _RealType
4152 lambda() const
4153 { return _M_lambda; }
4155 friend bool
4156 operator==(const param_type& __p1, const param_type& __p2)
4157 { return __p1._M_lambda == __p2._M_lambda; }
4159 private:
4160 _RealType _M_lambda;
4163 public:
4165 * @brief Constructs an exponential distribution with inverse scale
4166 * parameter @f$\lambda@f$.
4168 explicit
4169 exponential_distribution(const result_type& __lambda = result_type(1))
4170 : _M_param(__lambda)
4173 explicit
4174 exponential_distribution(const param_type& __p)
4175 : _M_param(__p)
4179 * @brief Resets the distribution state.
4181 * Has no effect on exponential distributions.
4183 void
4184 reset() { }
4187 * @brief Returns the inverse scale parameter of the distribution.
4189 _RealType
4190 lambda() const
4191 { return _M_param.lambda(); }
4194 * @brief Returns the parameter set of the distribution.
4196 param_type
4197 param() const
4198 { return _M_param; }
4201 * @brief Sets the parameter set of the distribution.
4202 * @param __param The new parameter set of the distribution.
4204 void
4205 param(const param_type& __param)
4206 { _M_param = __param; }
4209 * @brief Returns the greatest lower bound value of the distribution.
4211 result_type
4212 min() const
4213 { return result_type(0); }
4216 * @brief Returns the least upper bound value of the distribution.
4218 result_type
4219 max() const
4220 { return std::numeric_limits<result_type>::max(); }
4222 template<typename _UniformRandomNumberGenerator>
4223 result_type
4224 operator()(_UniformRandomNumberGenerator& __urng)
4225 { return this->operator()(__urng, this->param()); }
4227 template<typename _UniformRandomNumberGenerator>
4228 result_type
4229 operator()(_UniformRandomNumberGenerator& __urng,
4230 const param_type& __p)
4232 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
4233 __aurng(__urng);
4234 return -std::log(__aurng()) / __p.lambda();
4237 private:
4238 param_type _M_param;
4242 * @brief Return true if two exponential distributions have the same
4243 * parameters.
4245 template<typename _RealType>
4246 inline bool
4247 operator==(const std::exponential_distribution<_RealType>& __d1,
4248 const std::exponential_distribution<_RealType>& __d2)
4249 { return __d1.param() == __d2.param(); }
4252 * @brief Return true if two exponential distributions have different
4253 * parameters.
4255 template<typename _RealType>
4256 inline bool
4257 operator!=(const std::exponential_distribution<_RealType>& __d1,
4258 const std::exponential_distribution<_RealType>& __d2)
4259 { return !(__d1 == __d2); }
4262 * @brief Inserts a %exponential_distribution random number distribution
4263 * @p __x into the output stream @p __os.
4265 * @param __os An output stream.
4266 * @param __x A %exponential_distribution random number distribution.
4268 * @returns The output stream with the state of @p __x inserted or in
4269 * an error state.
4271 template<typename _RealType, typename _CharT, typename _Traits>
4272 std::basic_ostream<_CharT, _Traits>&
4273 operator<<(std::basic_ostream<_CharT, _Traits>&,
4274 const std::exponential_distribution<_RealType>&);
4277 * @brief Extracts a %exponential_distribution random number distribution
4278 * @p __x from the input stream @p __is.
4280 * @param __is An input stream.
4281 * @param __x A %exponential_distribution random number
4282 * generator engine.
4284 * @returns The input stream with @p __x extracted or in an error state.
4286 template<typename _RealType, typename _CharT, typename _Traits>
4287 std::basic_istream<_CharT, _Traits>&
4288 operator>>(std::basic_istream<_CharT, _Traits>&,
4289 std::exponential_distribution<_RealType>&);
4293 * @brief A weibull_distribution random number distribution.
4295 * The formula for the normal probability density function is:
4296 * @f[
4297 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4298 * \exp{(-(\frac{x}{\beta})^\alpha)}
4299 * @f]
4301 template<typename _RealType = double>
4302 class weibull_distribution
4304 static_assert(std::is_floating_point<_RealType>::value,
4305 "template argument not a floating point type");
4307 public:
4308 /** The type of the range of the distribution. */
4309 typedef _RealType result_type;
4310 /** Parameter type. */
4311 struct param_type
4313 typedef weibull_distribution<_RealType> distribution_type;
4315 explicit
4316 param_type(_RealType __a = _RealType(1),
4317 _RealType __b = _RealType(1))
4318 : _M_a(__a), _M_b(__b)
4321 _RealType
4322 a() const
4323 { return _M_a; }
4325 _RealType
4326 b() const
4327 { return _M_b; }
4329 friend bool
4330 operator==(const param_type& __p1, const param_type& __p2)
4331 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
4333 private:
4334 _RealType _M_a;
4335 _RealType _M_b;
4338 explicit
4339 weibull_distribution(_RealType __a = _RealType(1),
4340 _RealType __b = _RealType(1))
4341 : _M_param(__a, __b)
4344 explicit
4345 weibull_distribution(const param_type& __p)
4346 : _M_param(__p)
4350 * @brief Resets the distribution state.
4352 void
4353 reset()
4357 * @brief Return the @f$a@f$ parameter of the distribution.
4359 _RealType
4360 a() const
4361 { return _M_param.a(); }
4364 * @brief Return the @f$b@f$ parameter of the distribution.
4366 _RealType
4367 b() const
4368 { return _M_param.b(); }
4371 * @brief Returns the parameter set of the distribution.
4373 param_type
4374 param() const
4375 { return _M_param; }
4378 * @brief Sets the parameter set of the distribution.
4379 * @param __param The new parameter set of the distribution.
4381 void
4382 param(const param_type& __param)
4383 { _M_param = __param; }
4386 * @brief Returns the greatest lower bound value of the distribution.
4388 result_type
4389 min() const
4390 { return result_type(0); }
4393 * @brief Returns the least upper bound value of the distribution.
4395 result_type
4396 max() const
4397 { return std::numeric_limits<result_type>::max(); }
4399 template<typename _UniformRandomNumberGenerator>
4400 result_type
4401 operator()(_UniformRandomNumberGenerator& __urng)
4402 { return this->operator()(__urng, this->param()); }
4404 template<typename _UniformRandomNumberGenerator>
4405 result_type
4406 operator()(_UniformRandomNumberGenerator& __urng,
4407 const param_type& __p);
4409 private:
4410 param_type _M_param;
4414 * @brief Return true if two Weibull distributions have the same
4415 * parameters.
4417 template<typename _RealType>
4418 inline bool
4419 operator==(const std::weibull_distribution<_RealType>& __d1,
4420 const std::weibull_distribution<_RealType>& __d2)
4421 { return __d1.param() == __d2.param(); }
4424 * @brief Return true if two Weibull distributions have different
4425 * parameters.
4427 template<typename _RealType>
4428 inline bool
4429 operator!=(const std::weibull_distribution<_RealType>& __d1,
4430 const std::weibull_distribution<_RealType>& __d2)
4431 { return !(__d1 == __d2); }
4434 * @brief Inserts a %weibull_distribution random number distribution
4435 * @p __x into the output stream @p __os.
4437 * @param __os An output stream.
4438 * @param __x A %weibull_distribution random number distribution.
4440 * @returns The output stream with the state of @p __x inserted or in
4441 * an error state.
4443 template<typename _RealType, typename _CharT, typename _Traits>
4444 std::basic_ostream<_CharT, _Traits>&
4445 operator<<(std::basic_ostream<_CharT, _Traits>&,
4446 const std::weibull_distribution<_RealType>&);
4449 * @brief Extracts a %weibull_distribution random number distribution
4450 * @p __x from the input stream @p __is.
4452 * @param __is An input stream.
4453 * @param __x A %weibull_distribution random number
4454 * generator engine.
4456 * @returns The input stream with @p __x extracted or in an error state.
4458 template<typename _RealType, typename _CharT, typename _Traits>
4459 std::basic_istream<_CharT, _Traits>&
4460 operator>>(std::basic_istream<_CharT, _Traits>&,
4461 std::weibull_distribution<_RealType>&);
4465 * @brief A extreme_value_distribution random number distribution.
4467 * The formula for the normal probability mass function is
4468 * @f[
4469 * p(x|a,b) = \frac{1}{b}
4470 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
4471 * @f]
4473 template<typename _RealType = double>
4474 class extreme_value_distribution
4476 static_assert(std::is_floating_point<_RealType>::value,
4477 "template argument not a floating point type");
4479 public:
4480 /** The type of the range of the distribution. */
4481 typedef _RealType result_type;
4482 /** Parameter type. */
4483 struct param_type
4485 typedef extreme_value_distribution<_RealType> distribution_type;
4487 explicit
4488 param_type(_RealType __a = _RealType(0),
4489 _RealType __b = _RealType(1))
4490 : _M_a(__a), _M_b(__b)
4493 _RealType
4494 a() const
4495 { return _M_a; }
4497 _RealType
4498 b() const
4499 { return _M_b; }
4501 friend bool
4502 operator==(const param_type& __p1, const param_type& __p2)
4503 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
4505 private:
4506 _RealType _M_a;
4507 _RealType _M_b;
4510 explicit
4511 extreme_value_distribution(_RealType __a = _RealType(0),
4512 _RealType __b = _RealType(1))
4513 : _M_param(__a, __b)
4516 explicit
4517 extreme_value_distribution(const param_type& __p)
4518 : _M_param(__p)
4522 * @brief Resets the distribution state.
4524 void
4525 reset()
4529 * @brief Return the @f$a@f$ parameter of the distribution.
4531 _RealType
4532 a() const
4533 { return _M_param.a(); }
4536 * @brief Return the @f$b@f$ parameter of the distribution.
4538 _RealType
4539 b() const
4540 { return _M_param.b(); }
4543 * @brief Returns the parameter set of the distribution.
4545 param_type
4546 param() const
4547 { return _M_param; }
4550 * @brief Sets the parameter set of the distribution.
4551 * @param __param The new parameter set of the distribution.
4553 void
4554 param(const param_type& __param)
4555 { _M_param = __param; }
4558 * @brief Returns the greatest lower bound value of the distribution.
4560 result_type
4561 min() const
4562 { return std::numeric_limits<result_type>::min(); }
4565 * @brief Returns the least upper bound value of the distribution.
4567 result_type
4568 max() const
4569 { return std::numeric_limits<result_type>::max(); }
4571 template<typename _UniformRandomNumberGenerator>
4572 result_type
4573 operator()(_UniformRandomNumberGenerator& __urng)
4574 { return this->operator()(__urng, this->param()); }
4576 template<typename _UniformRandomNumberGenerator>
4577 result_type
4578 operator()(_UniformRandomNumberGenerator& __urng,
4579 const param_type& __p);
4581 private:
4582 param_type _M_param;
4586 * @brief Return true if two extreme value distributions have the same
4587 * parameters.
4589 template<typename _RealType>
4590 inline bool
4591 operator==(const std::extreme_value_distribution<_RealType>& __d1,
4592 const std::extreme_value_distribution<_RealType>& __d2)
4593 { return __d1.param() == __d2.param(); }
4596 * @brief Return true if two extreme value distributions have different
4597 * parameters.
4599 template<typename _RealType>
4600 inline bool
4601 operator!=(const std::extreme_value_distribution<_RealType>& __d1,
4602 const std::extreme_value_distribution<_RealType>& __d2)
4603 { return !(__d1 == __d2); }
4606 * @brief Inserts a %extreme_value_distribution random number distribution
4607 * @p __x into the output stream @p __os.
4609 * @param __os An output stream.
4610 * @param __x A %extreme_value_distribution random number distribution.
4612 * @returns The output stream with the state of @p __x inserted or in
4613 * an error state.
4615 template<typename _RealType, typename _CharT, typename _Traits>
4616 std::basic_ostream<_CharT, _Traits>&
4617 operator<<(std::basic_ostream<_CharT, _Traits>&,
4618 const std::extreme_value_distribution<_RealType>&);
4621 * @brief Extracts a %extreme_value_distribution random number
4622 * distribution @p __x from the input stream @p __is.
4624 * @param __is An input stream.
4625 * @param __x A %extreme_value_distribution random number
4626 * generator engine.
4628 * @returns The input stream with @p __x extracted or in an error state.
4630 template<typename _RealType, typename _CharT, typename _Traits>
4631 std::basic_istream<_CharT, _Traits>&
4632 operator>>(std::basic_istream<_CharT, _Traits>&,
4633 std::extreme_value_distribution<_RealType>&);
4637 * @brief A discrete_distribution random number distribution.
4639 * The formula for the discrete probability mass function is
4642 template<typename _IntType = int>
4643 class discrete_distribution
4645 static_assert(std::is_integral<_IntType>::value,
4646 "template argument not an integral type");
4648 public:
4649 /** The type of the range of the distribution. */
4650 typedef _IntType result_type;
4651 /** Parameter type. */
4652 struct param_type
4654 typedef discrete_distribution<_IntType> distribution_type;
4655 friend class discrete_distribution<_IntType>;
4657 param_type()
4658 : _M_prob(), _M_cp()
4659 { _M_initialize(); }
4661 template<typename _InputIterator>
4662 param_type(_InputIterator __wbegin,
4663 _InputIterator __wend)
4664 : _M_prob(__wbegin, __wend), _M_cp()
4665 { _M_initialize(); }
4667 param_type(initializer_list<double> __wil)
4668 : _M_prob(__wil.begin(), __wil.end()), _M_cp()
4669 { _M_initialize(); }
4671 template<typename _Func>
4672 param_type(size_t __nw, double __xmin, double __xmax,
4673 _Func __fw);
4675 std::vector<double>
4676 probabilities() const
4677 { return _M_prob; }
4679 friend bool
4680 operator==(const param_type& __p1, const param_type& __p2)
4681 { return __p1._M_prob == __p2._M_prob; }
4683 private:
4684 void
4685 _M_initialize();
4687 std::vector<double> _M_prob;
4688 std::vector<double> _M_cp;
4691 discrete_distribution()
4692 : _M_param()
4695 template<typename _InputIterator>
4696 discrete_distribution(_InputIterator __wbegin,
4697 _InputIterator __wend)
4698 : _M_param(__wbegin, __wend)
4701 discrete_distribution(initializer_list<double> __wl)
4702 : _M_param(__wl)
4705 template<typename _Func>
4706 discrete_distribution(size_t __nw, double __xmin, double __xmax,
4707 _Func __fw)
4708 : _M_param(__nw, __xmin, __xmax, __fw)
4711 explicit
4712 discrete_distribution(const param_type& __p)
4713 : _M_param(__p)
4717 * @brief Resets the distribution state.
4719 void
4720 reset()
4724 * @brief Returns the probabilities of the distribution.
4726 std::vector<double>
4727 probabilities() const
4728 { return _M_param.probabilities(); }
4731 * @brief Returns the parameter set of the distribution.
4733 param_type
4734 param() const
4735 { return _M_param; }
4738 * @brief Sets the parameter set of the distribution.
4739 * @param __param The new parameter set of the distribution.
4741 void
4742 param(const param_type& __param)
4743 { _M_param = __param; }
4746 * @brief Returns the greatest lower bound value of the distribution.
4748 result_type
4749 min() const
4750 { return result_type(0); }
4753 * @brief Returns the least upper bound value of the distribution.
4755 result_type
4756 max() const
4757 { return this->_M_param._M_prob.size() - 1; }
4759 template<typename _UniformRandomNumberGenerator>
4760 result_type
4761 operator()(_UniformRandomNumberGenerator& __urng)
4762 { return this->operator()(__urng, this->param()); }
4764 template<typename _UniformRandomNumberGenerator>
4765 result_type
4766 operator()(_UniformRandomNumberGenerator& __urng,
4767 const param_type& __p);
4770 * @brief Inserts a %discrete_distribution random number distribution
4771 * @p __x into the output stream @p __os.
4773 * @param __os An output stream.
4774 * @param __x A %discrete_distribution random number distribution.
4776 * @returns The output stream with the state of @p __x inserted or in
4777 * an error state.
4779 template<typename _IntType1, typename _CharT, typename _Traits>
4780 friend std::basic_ostream<_CharT, _Traits>&
4781 operator<<(std::basic_ostream<_CharT, _Traits>&,
4782 const std::discrete_distribution<_IntType1>&);
4785 * @brief Extracts a %discrete_distribution random number distribution
4786 * @p __x from the input stream @p __is.
4788 * @param __is An input stream.
4789 * @param __x A %discrete_distribution random number
4790 * generator engine.
4792 * @returns The input stream with @p __x extracted or in an error
4793 * state.
4795 template<typename _IntType1, typename _CharT, typename _Traits>
4796 friend std::basic_istream<_CharT, _Traits>&
4797 operator>>(std::basic_istream<_CharT, _Traits>&,
4798 std::discrete_distribution<_IntType1>&);
4800 private:
4801 param_type _M_param;
4805 * @brief Return true if two discrete distributions have the same
4806 * parameters.
4808 template<typename _IntType>
4809 inline bool
4810 operator==(const std::discrete_distribution<_IntType>& __d1,
4811 const std::discrete_distribution<_IntType>& __d2)
4812 { return __d1.param() == __d2.param(); }
4815 * @brief Return true if two discrete distributions have different
4816 * parameters.
4818 template<typename _IntType>
4819 inline bool
4820 operator!=(const std::discrete_distribution<_IntType>& __d1,
4821 const std::discrete_distribution<_IntType>& __d2)
4822 { return !(__d1 == __d2); }
4826 * @brief A piecewise_constant_distribution random number distribution.
4828 * The formula for the piecewise constant probability mass function is
4831 template<typename _RealType = double>
4832 class piecewise_constant_distribution
4834 static_assert(std::is_floating_point<_RealType>::value,
4835 "template argument not a floating point type");
4837 public:
4838 /** The type of the range of the distribution. */
4839 typedef _RealType result_type;
4840 /** Parameter type. */
4841 struct param_type
4843 typedef piecewise_constant_distribution<_RealType> distribution_type;
4844 friend class piecewise_constant_distribution<_RealType>;
4846 param_type()
4847 : _M_int(), _M_den(), _M_cp()
4848 { _M_initialize(); }
4850 template<typename _InputIteratorB, typename _InputIteratorW>
4851 param_type(_InputIteratorB __bfirst,
4852 _InputIteratorB __bend,
4853 _InputIteratorW __wbegin);
4855 template<typename _Func>
4856 param_type(initializer_list<_RealType> __bi, _Func __fw);
4858 template<typename _Func>
4859 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
4860 _Func __fw);
4862 std::vector<_RealType>
4863 intervals() const
4864 { return _M_int; }
4866 std::vector<double>
4867 densities() const
4868 { return _M_den; }
4870 friend bool
4871 operator==(const param_type& __p1, const param_type& __p2)
4872 { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; }
4874 private:
4875 void
4876 _M_initialize();
4878 std::vector<_RealType> _M_int;
4879 std::vector<double> _M_den;
4880 std::vector<double> _M_cp;
4883 explicit
4884 piecewise_constant_distribution()
4885 : _M_param()
4888 template<typename _InputIteratorB, typename _InputIteratorW>
4889 piecewise_constant_distribution(_InputIteratorB __bfirst,
4890 _InputIteratorB __bend,
4891 _InputIteratorW __wbegin)
4892 : _M_param(__bfirst, __bend, __wbegin)
4895 template<typename _Func>
4896 piecewise_constant_distribution(initializer_list<_RealType> __bl,
4897 _Func __fw)
4898 : _M_param(__bl, __fw)
4901 template<typename _Func>
4902 piecewise_constant_distribution(size_t __nw,
4903 _RealType __xmin, _RealType __xmax,
4904 _Func __fw)
4905 : _M_param(__nw, __xmin, __xmax, __fw)
4908 explicit
4909 piecewise_constant_distribution(const param_type& __p)
4910 : _M_param(__p)
4914 * @brief Resets the distribution state.
4916 void
4917 reset()
4921 * @brief Returns a vector of the intervals.
4923 std::vector<_RealType>
4924 intervals() const
4925 { return _M_param.intervals(); }
4928 * @brief Returns a vector of the probability densities.
4930 std::vector<double>
4931 densities() const
4932 { return _M_param.densities(); }
4935 * @brief Returns the parameter set of the distribution.
4937 param_type
4938 param() const
4939 { return _M_param; }
4942 * @brief Sets the parameter set of the distribution.
4943 * @param __param The new parameter set of the distribution.
4945 void
4946 param(const param_type& __param)
4947 { _M_param = __param; }
4950 * @brief Returns the greatest lower bound value of the distribution.
4952 result_type
4953 min() const
4954 { return this->_M_param._M_int.front(); }
4957 * @brief Returns the least upper bound value of the distribution.
4959 result_type
4960 max() const
4961 { return this->_M_param._M_int.back(); }
4963 template<typename _UniformRandomNumberGenerator>
4964 result_type
4965 operator()(_UniformRandomNumberGenerator& __urng)
4966 { return this->operator()(__urng, this->param()); }
4968 template<typename _UniformRandomNumberGenerator>
4969 result_type
4970 operator()(_UniformRandomNumberGenerator& __urng,
4971 const param_type& __p);
4974 * @brief Inserts a %piecewise_constan_distribution random
4975 * number distribution @p __x into the output stream @p __os.
4977 * @param __os An output stream.
4978 * @param __x A %piecewise_constan_distribution random number
4979 * distribution.
4981 * @returns The output stream with the state of @p __x inserted or in
4982 * an error state.
4984 template<typename _RealType1, typename _CharT, typename _Traits>
4985 friend std::basic_ostream<_CharT, _Traits>&
4986 operator<<(std::basic_ostream<_CharT, _Traits>&,
4987 const std::piecewise_constant_distribution<_RealType1>&);
4990 * @brief Extracts a %piecewise_constan_distribution random
4991 * number distribution @p __x from the input stream @p __is.
4993 * @param __is An input stream.
4994 * @param __x A %piecewise_constan_distribution random number
4995 * generator engine.
4997 * @returns The input stream with @p __x extracted or in an error
4998 * state.
5000 template<typename _RealType1, typename _CharT, typename _Traits>
5001 friend std::basic_istream<_CharT, _Traits>&
5002 operator>>(std::basic_istream<_CharT, _Traits>&,
5003 std::piecewise_constant_distribution<_RealType1>&);
5005 private:
5006 param_type _M_param;
5010 * @brief Return true if two piecewise constant distributions have the
5011 * same parameters.
5013 template<typename _RealType>
5014 inline bool
5015 operator==(const std::piecewise_constant_distribution<_RealType>& __d1,
5016 const std::piecewise_constant_distribution<_RealType>& __d2)
5017 { return __d1.param() == __d2.param(); }
5020 * @brief Return true if two piecewise constant distributions have
5021 * different parameters.
5023 template<typename _RealType>
5024 inline bool
5025 operator!=(const std::piecewise_constant_distribution<_RealType>& __d1,
5026 const std::piecewise_constant_distribution<_RealType>& __d2)
5027 { return !(__d1 == __d2); }
5031 * @brief A piecewise_linear_distribution random number distribution.
5033 * The formula for the piecewise linear probability mass function is
5036 template<typename _RealType = double>
5037 class piecewise_linear_distribution
5039 static_assert(std::is_floating_point<_RealType>::value,
5040 "template argument not a floating point type");
5042 public:
5043 /** The type of the range of the distribution. */
5044 typedef _RealType result_type;
5045 /** Parameter type. */
5046 struct param_type
5048 typedef piecewise_linear_distribution<_RealType> distribution_type;
5049 friend class piecewise_linear_distribution<_RealType>;
5051 param_type()
5052 : _M_int(), _M_den(), _M_cp(), _M_m()
5053 { _M_initialize(); }
5055 template<typename _InputIteratorB, typename _InputIteratorW>
5056 param_type(_InputIteratorB __bfirst,
5057 _InputIteratorB __bend,
5058 _InputIteratorW __wbegin);
5060 template<typename _Func>
5061 param_type(initializer_list<_RealType> __bl, _Func __fw);
5063 template<typename _Func>
5064 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
5065 _Func __fw);
5067 std::vector<_RealType>
5068 intervals() const
5069 { return _M_int; }
5071 std::vector<double>
5072 densities() const
5073 { return _M_den; }
5075 friend bool
5076 operator==(const param_type& __p1, const param_type& __p2)
5077 { return (__p1._M_int == __p2._M_int
5078 && __p1._M_den == __p2._M_den); }
5080 private:
5081 void
5082 _M_initialize();
5084 std::vector<_RealType> _M_int;
5085 std::vector<double> _M_den;
5086 std::vector<double> _M_cp;
5087 std::vector<double> _M_m;
5090 explicit
5091 piecewise_linear_distribution()
5092 : _M_param()
5095 template<typename _InputIteratorB, typename _InputIteratorW>
5096 piecewise_linear_distribution(_InputIteratorB __bfirst,
5097 _InputIteratorB __bend,
5098 _InputIteratorW __wbegin)
5099 : _M_param(__bfirst, __bend, __wbegin)
5102 template<typename _Func>
5103 piecewise_linear_distribution(initializer_list<_RealType> __bl,
5104 _Func __fw)
5105 : _M_param(__bl, __fw)
5108 template<typename _Func>
5109 piecewise_linear_distribution(size_t __nw,
5110 _RealType __xmin, _RealType __xmax,
5111 _Func __fw)
5112 : _M_param(__nw, __xmin, __xmax, __fw)
5115 explicit
5116 piecewise_linear_distribution(const param_type& __p)
5117 : _M_param(__p)
5121 * Resets the distribution state.
5123 void
5124 reset()
5128 * @brief Return the intervals of the distribution.
5130 std::vector<_RealType>
5131 intervals() const
5132 { return _M_param.intervals(); }
5135 * @brief Return a vector of the probability densities of the
5136 * distribution.
5138 std::vector<double>
5139 densities() const
5140 { return _M_param.densities(); }
5143 * @brief Returns the parameter set of the distribution.
5145 param_type
5146 param() const
5147 { return _M_param; }
5150 * @brief Sets the parameter set of the distribution.
5151 * @param __param The new parameter set of the distribution.
5153 void
5154 param(const param_type& __param)
5155 { _M_param = __param; }
5158 * @brief Returns the greatest lower bound value of the distribution.
5160 result_type
5161 min() const
5162 { return this->_M_param._M_int.front(); }
5165 * @brief Returns the least upper bound value of the distribution.
5167 result_type
5168 max() const
5169 { return this->_M_param._M_int.back(); }
5171 template<typename _UniformRandomNumberGenerator>
5172 result_type
5173 operator()(_UniformRandomNumberGenerator& __urng)
5174 { return this->operator()(__urng, this->param()); }
5176 template<typename _UniformRandomNumberGenerator>
5177 result_type
5178 operator()(_UniformRandomNumberGenerator& __urng,
5179 const param_type& __p);
5182 * @brief Inserts a %piecewise_linear_distribution random number
5183 * distribution @p __x into the output stream @p __os.
5185 * @param __os An output stream.
5186 * @param __x A %piecewise_linear_distribution random number
5187 * distribution.
5189 * @returns The output stream with the state of @p __x inserted or in
5190 * an error state.
5192 template<typename _RealType1, typename _CharT, typename _Traits>
5193 friend std::basic_ostream<_CharT, _Traits>&
5194 operator<<(std::basic_ostream<_CharT, _Traits>&,
5195 const std::piecewise_linear_distribution<_RealType1>&);
5198 * @brief Extracts a %piecewise_linear_distribution random number
5199 * distribution @p __x from the input stream @p __is.
5201 * @param __is An input stream.
5202 * @param __x A %piecewise_linear_distribution random number
5203 * generator engine.
5205 * @returns The input stream with @p __x extracted or in an error
5206 * state.
5208 template<typename _RealType1, typename _CharT, typename _Traits>
5209 friend std::basic_istream<_CharT, _Traits>&
5210 operator>>(std::basic_istream<_CharT, _Traits>&,
5211 std::piecewise_linear_distribution<_RealType1>&);
5213 private:
5214 param_type _M_param;
5218 * @brief Return true if two piecewise linear distributions have the
5219 * same parameters.
5221 template<typename _RealType>
5222 inline bool
5223 operator==(const std::piecewise_linear_distribution<_RealType>& __d1,
5224 const std::piecewise_linear_distribution<_RealType>& __d2)
5225 { return __d1.param() == __d2.param(); }
5228 * @brief Return true if two piecewise linear distributions have
5229 * different parameters.
5231 template<typename _RealType>
5232 inline bool
5233 operator!=(const std::piecewise_linear_distribution<_RealType>& __d1,
5234 const std::piecewise_linear_distribution<_RealType>& __d2)
5235 { return !(__d1 == __d2); }
5238 /* @} */ // group random_distributions_poisson
5240 /* @} */ // group random_distributions
5243 * @addtogroup random_utilities Random Number Utilities
5244 * @ingroup random
5245 * @{
5249 * @brief The seed_seq class generates sequences of seeds for random
5250 * number generators.
5252 class seed_seq
5255 public:
5256 /** The type of the seed vales. */
5257 typedef uint_least32_t result_type;
5259 /** Default constructor. */
5260 seed_seq()
5261 : _M_v()
5264 template<typename _IntType>
5265 seed_seq(std::initializer_list<_IntType> il);
5267 template<typename _InputIterator>
5268 seed_seq(_InputIterator __begin, _InputIterator __end);
5270 // generating functions
5271 template<typename _RandomAccessIterator>
5272 void
5273 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
5275 // property functions
5276 size_t size() const
5277 { return _M_v.size(); }
5279 template<typename OutputIterator>
5280 void
5281 param(OutputIterator __dest) const
5282 { std::copy(_M_v.begin(), _M_v.end(), __dest); }
5284 private:
5286 std::vector<result_type> _M_v;
5289 /* @} */ // group random_utilities
5291 /* @} */ // group random