random.h (random_device): Move implementation to...
[gcc.git] / libstdc++-v3 / include / bits / random.h
1 // random number generation -*- C++ -*-
2
3 // Copyright (C) 2009, 2010, 2011, 2012 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.
10
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.
15
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.
19
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/>.
24
25 /**
26 * @file bits/random.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
29 */
30
31 #ifndef _RANDOM_H
32 #define _RANDOM_H 1
33
34 #include <vector>
35
36 namespace std _GLIBCXX_VISIBILITY(default)
37 {
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39
40 // [26.4] Random number generation
41
42 /**
43 * @defgroup random Random Number Generation
44 * @ingroup numerics
45 *
46 * A facility for generating random numbers on selected distributions.
47 * @{
48 */
49
50 /**
51 * @brief A function template for converting the output of a (integral)
52 * uniform random number generator to a floatng point result in the range
53 * [0-1).
54 */
55 template<typename _RealType, size_t __bits,
56 typename _UniformRandomNumberGenerator>
57 _RealType
58 generate_canonical(_UniformRandomNumberGenerator& __g);
59
60 _GLIBCXX_END_NAMESPACE_VERSION
61
62 /*
63 * Implementation-space details.
64 */
65 namespace __detail
66 {
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68
69 template<typename _UIntType, size_t __w,
70 bool = __w < static_cast<size_t>
71 (std::numeric_limits<_UIntType>::digits)>
72 struct _Shift
73 { static const _UIntType __value = 0; };
74
75 template<typename _UIntType, size_t __w>
76 struct _Shift<_UIntType, __w, true>
77 { static const _UIntType __value = _UIntType(1) << __w; };
78
79 template<int __s,
80 int __which = ((__s <= __CHAR_BIT__ * sizeof (int))
81 + (__s <= __CHAR_BIT__ * sizeof (long))
82 + (__s <= __CHAR_BIT__ * sizeof (long long))
83 /* assume long long no bigger than __int128 */
84 + (__s <= 128))>
85 struct _Select_uint_least_t
86 {
87 static_assert(__which < 0, /* needs to be dependent */
88 "sorry, would be too much trouble for a slow result");
89 };
90
91 template<int __s>
92 struct _Select_uint_least_t<__s, 4>
93 { typedef unsigned int type; };
94
95 template<int __s>
96 struct _Select_uint_least_t<__s, 3>
97 { typedef unsigned long type; };
98
99 template<int __s>
100 struct _Select_uint_least_t<__s, 2>
101 { typedef unsigned long long type; };
102
103 #ifdef _GLIBCXX_USE_INT128
104 template<int __s>
105 struct _Select_uint_least_t<__s, 1>
106 { typedef unsigned __int128 type; };
107 #endif
108
109 // Assume a != 0, a < m, c < m, x < m.
110 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c,
111 bool __big_enough = (!(__m & (__m - 1))
112 || (_Tp(-1) - __c) / __a >= __m - 1),
113 bool __schrage_ok = __m % __a < __m / __a>
114 struct _Mod
115 {
116 typedef typename _Select_uint_least_t<std::__lg(__a)
117 + std::__lg(__m) + 2>::type _Tp2;
118 static _Tp
119 __calc(_Tp __x)
120 { return static_cast<_Tp>((_Tp2(__a) * __x + __c) % __m); }
121 };
122
123 // Schrage.
124 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
125 struct _Mod<_Tp, __m, __a, __c, false, true>
126 {
127 static _Tp
128 __calc(_Tp __x);
129 };
130
131 // Special cases:
132 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
133 // - a * (m - 1) + c fits in _Tp, there is no overflow.
134 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool __s>
135 struct _Mod<_Tp, __m, __a, __c, true, __s>
136 {
137 static _Tp
138 __calc(_Tp __x)
139 {
140 _Tp __res = __a * __x + __c;
141 if (__m)
142 __res %= __m;
143 return __res;
144 }
145 };
146
147 template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
148 inline _Tp
149 __mod(_Tp __x)
150 { return _Mod<_Tp, __m, __a, __c>::__calc(__x); }
151
152 /*
153 * An adaptor class for converting the output of any Generator into
154 * the input for a specific Distribution.
155 */
156 template<typename _Engine, typename _DInputType>
157 struct _Adaptor
158 {
159
160 public:
161 _Adaptor(_Engine& __g)
162 : _M_g(__g) { }
163
164 _DInputType
165 min() const
166 { return _DInputType(0); }
167
168 _DInputType
169 max() const
170 { return _DInputType(1); }
171
172 /*
173 * Converts a value generated by the adapted random number generator
174 * into a value in the input domain for the dependent random number
175 * distribution.
176 */
177 _DInputType
178 operator()()
179 {
180 return std::generate_canonical<_DInputType,
181 std::numeric_limits<_DInputType>::digits,
182 _Engine>(_M_g);
183 }
184
185 private:
186 _Engine& _M_g;
187 };
188
189 _GLIBCXX_END_NAMESPACE_VERSION
190 } // namespace __detail
191
192 _GLIBCXX_BEGIN_NAMESPACE_VERSION
193
194 /**
195 * @addtogroup random_generators Random Number Generators
196 * @ingroup random
197 *
198 * These classes define objects which provide random or pseudorandom
199 * numbers, either from a discrete or a continuous interval. The
200 * random number generator supplied as a part of this library are
201 * all uniform random number generators which provide a sequence of
202 * random number uniformly distributed over their range.
203 *
204 * A number generator is a function object with an operator() that
205 * takes zero arguments and returns a number.
206 *
207 * A compliant random number generator must satisfy the following
208 * requirements. <table border=1 cellpadding=10 cellspacing=0>
209 * <caption align=top>Random Number Generator Requirements</caption>
210 * <tr><td>To be documented.</td></tr> </table>
211 *
212 * @{
213 */
214
215 /**
216 * @brief A model of a linear congruential random number generator.
217 *
218 * A random number generator that produces pseudorandom numbers via
219 * linear function:
220 * @f[
221 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
222 * @f]
223 *
224 * The template parameter @p _UIntType must be an unsigned integral type
225 * large enough to store values up to (__m-1). If the template parameter
226 * @p __m is 0, the modulus @p __m used is
227 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
228 * parameters @p __a and @p __c must be less than @p __m.
229 *
230 * The size of the state is @f$1@f$.
231 */
232 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
233 class linear_congruential_engine
234 {
235 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
236 "substituting _UIntType not an unsigned integral type");
237 static_assert(__m == 0u || (__a < __m && __c < __m),
238 "template argument substituting __m out of bounds");
239
240 public:
241 /** The type of the generated random value. */
242 typedef _UIntType result_type;
243
244 /** The multiplier. */
245 static constexpr result_type multiplier = __a;
246 /** An increment. */
247 static constexpr result_type increment = __c;
248 /** The modulus. */
249 static constexpr result_type modulus = __m;
250 static constexpr result_type default_seed = 1u;
251
252 /**
253 * @brief Constructs a %linear_congruential_engine random number
254 * generator engine with seed @p __s. The default seed value
255 * is 1.
256 *
257 * @param __s The initial seed value.
258 */
259 explicit
260 linear_congruential_engine(result_type __s = default_seed)
261 { seed(__s); }
262
263 /**
264 * @brief Constructs a %linear_congruential_engine random number
265 * generator engine seeded from the seed sequence @p __q.
266 *
267 * @param __q the seed sequence.
268 */
269 template<typename _Sseq, typename = typename
270 std::enable_if<!std::is_same<_Sseq, linear_congruential_engine>::value>
271 ::type>
272 explicit
273 linear_congruential_engine(_Sseq& __q)
274 { seed(__q); }
275
276 /**
277 * @brief Reseeds the %linear_congruential_engine random number generator
278 * engine sequence to the seed @p __s.
279 *
280 * @param __s The new seed.
281 */
282 void
283 seed(result_type __s = default_seed);
284
285 /**
286 * @brief Reseeds the %linear_congruential_engine random number generator
287 * engine
288 * sequence using values from the seed sequence @p __q.
289 *
290 * @param __q the seed sequence.
291 */
292 template<typename _Sseq>
293 typename std::enable_if<std::is_class<_Sseq>::value>::type
294 seed(_Sseq& __q);
295
296 /**
297 * @brief Gets the smallest possible value in the output range.
298 *
299 * The minimum depends on the @p __c parameter: if it is zero, the
300 * minimum generated must be > 0, otherwise 0 is allowed.
301 */
302 static constexpr result_type
303 min()
304 { return __c == 0u ? 1u : 0u; }
305
306 /**
307 * @brief Gets the largest possible value in the output range.
308 */
309 static constexpr result_type
310 max()
311 { return __m - 1u; }
312
313 /**
314 * @brief Discard a sequence of random numbers.
315 */
316 void
317 discard(unsigned long long __z)
318 {
319 for (; __z != 0ULL; --__z)
320 (*this)();
321 }
322
323 /**
324 * @brief Gets the next random number in the sequence.
325 */
326 result_type
327 operator()()
328 {
329 _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
330 return _M_x;
331 }
332
333 /**
334 * @brief Compares two linear congruential random number generator
335 * objects of the same type for equality.
336 *
337 * @param __lhs A linear congruential random number generator object.
338 * @param __rhs Another linear congruential random number generator
339 * object.
340 *
341 * @returns true if the infinite sequences of generated values
342 * would be equal, false otherwise.
343 */
344 friend bool
345 operator==(const linear_congruential_engine& __lhs,
346 const linear_congruential_engine& __rhs)
347 { return __lhs._M_x == __rhs._M_x; }
348
349 /**
350 * @brief Writes the textual representation of the state x(i) of x to
351 * @p __os.
352 *
353 * @param __os The output stream.
354 * @param __lcr A % linear_congruential_engine random number generator.
355 * @returns __os.
356 */
357 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
358 _UIntType1 __m1, typename _CharT, typename _Traits>
359 friend std::basic_ostream<_CharT, _Traits>&
360 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
361 const std::linear_congruential_engine<_UIntType1,
362 __a1, __c1, __m1>& __lcr);
363
364 /**
365 * @brief Sets the state of the engine by reading its textual
366 * representation from @p __is.
367 *
368 * The textual representation must have been previously written using
369 * an output stream whose imbued locale and whose type's template
370 * specialization arguments _CharT and _Traits were the same as those
371 * of @p __is.
372 *
373 * @param __is The input stream.
374 * @param __lcr A % linear_congruential_engine random number generator.
375 * @returns __is.
376 */
377 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
378 _UIntType1 __m1, typename _CharT, typename _Traits>
379 friend std::basic_istream<_CharT, _Traits>&
380 operator>>(std::basic_istream<_CharT, _Traits>& __is,
381 std::linear_congruential_engine<_UIntType1, __a1,
382 __c1, __m1>& __lcr);
383
384 private:
385 _UIntType _M_x;
386 };
387
388 /**
389 * @brief Compares two linear congruential random number generator
390 * objects of the same type for inequality.
391 *
392 * @param __lhs A linear congruential random number generator object.
393 * @param __rhs Another linear congruential random number generator
394 * object.
395 *
396 * @returns true if the infinite sequences of generated values
397 * would be different, false otherwise.
398 */
399 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
400 inline bool
401 operator!=(const std::linear_congruential_engine<_UIntType, __a,
402 __c, __m>& __lhs,
403 const std::linear_congruential_engine<_UIntType, __a,
404 __c, __m>& __rhs)
405 { return !(__lhs == __rhs); }
406
407
408 /**
409 * A generalized feedback shift register discrete random number generator.
410 *
411 * This algorithm avoids multiplication and division and is designed to be
412 * friendly to a pipelined architecture. If the parameters are chosen
413 * correctly, this generator will produce numbers with a very long period and
414 * fairly good apparent entropy, although still not cryptographically strong.
415 *
416 * The best way to use this generator is with the predefined mt19937 class.
417 *
418 * This algorithm was originally invented by Makoto Matsumoto and
419 * Takuji Nishimura.
420 *
421 * @tparam __w Word size, the number of bits in each element of
422 * the state vector.
423 * @tparam __n The degree of recursion.
424 * @tparam __m The period parameter.
425 * @tparam __r The separation point bit index.
426 * @tparam __a The last row of the twist matrix.
427 * @tparam __u The first right-shift tempering matrix parameter.
428 * @tparam __d The first right-shift tempering matrix mask.
429 * @tparam __s The first left-shift tempering matrix parameter.
430 * @tparam __b The first left-shift tempering matrix mask.
431 * @tparam __t The second left-shift tempering matrix parameter.
432 * @tparam __c The second left-shift tempering matrix mask.
433 * @tparam __l The second right-shift tempering matrix parameter.
434 * @tparam __f Initialization multiplier.
435 */
436 template<typename _UIntType, size_t __w,
437 size_t __n, size_t __m, size_t __r,
438 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
439 _UIntType __b, size_t __t,
440 _UIntType __c, size_t __l, _UIntType __f>
441 class mersenne_twister_engine
442 {
443 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
444 "substituting _UIntType not an unsigned integral type");
445 static_assert(1u <= __m && __m <= __n,
446 "template argument substituting __m out of bounds");
447 static_assert(__r <= __w, "template argument substituting "
448 "__r out of bound");
449 static_assert(__u <= __w, "template argument substituting "
450 "__u out of bound");
451 static_assert(__s <= __w, "template argument substituting "
452 "__s out of bound");
453 static_assert(__t <= __w, "template argument substituting "
454 "__t out of bound");
455 static_assert(__l <= __w, "template argument substituting "
456 "__l out of bound");
457 static_assert(__w <= std::numeric_limits<_UIntType>::digits,
458 "template argument substituting __w out of bound");
459 static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
460 "template argument substituting __a out of bound");
461 static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
462 "template argument substituting __b out of bound");
463 static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
464 "template argument substituting __c out of bound");
465 static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
466 "template argument substituting __d out of bound");
467 static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
468 "template argument substituting __f out of bound");
469
470 public:
471 /** The type of the generated random value. */
472 typedef _UIntType result_type;
473
474 // parameter values
475 static constexpr size_t word_size = __w;
476 static constexpr size_t state_size = __n;
477 static constexpr size_t shift_size = __m;
478 static constexpr size_t mask_bits = __r;
479 static constexpr result_type xor_mask = __a;
480 static constexpr size_t tempering_u = __u;
481 static constexpr result_type tempering_d = __d;
482 static constexpr size_t tempering_s = __s;
483 static constexpr result_type tempering_b = __b;
484 static constexpr size_t tempering_t = __t;
485 static constexpr result_type tempering_c = __c;
486 static constexpr size_t tempering_l = __l;
487 static constexpr result_type initialization_multiplier = __f;
488 static constexpr result_type default_seed = 5489u;
489
490 // constructors and member function
491 explicit
492 mersenne_twister_engine(result_type __sd = default_seed)
493 { seed(__sd); }
494
495 /**
496 * @brief Constructs a %mersenne_twister_engine random number generator
497 * engine seeded from the seed sequence @p __q.
498 *
499 * @param __q the seed sequence.
500 */
501 template<typename _Sseq, typename = typename
502 std::enable_if<!std::is_same<_Sseq, mersenne_twister_engine>::value>
503 ::type>
504 explicit
505 mersenne_twister_engine(_Sseq& __q)
506 { seed(__q); }
507
508 void
509 seed(result_type __sd = default_seed);
510
511 template<typename _Sseq>
512 typename std::enable_if<std::is_class<_Sseq>::value>::type
513 seed(_Sseq& __q);
514
515 /**
516 * @brief Gets the smallest possible value in the output range.
517 */
518 static constexpr result_type
519 min()
520 { return 0; };
521
522 /**
523 * @brief Gets the largest possible value in the output range.
524 */
525 static constexpr result_type
526 max()
527 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
528
529 /**
530 * @brief Discard a sequence of random numbers.
531 */
532 void
533 discard(unsigned long long __z);
534
535 result_type
536 operator()();
537
538 /**
539 * @brief Compares two % mersenne_twister_engine random number generator
540 * objects of the same type for equality.
541 *
542 * @param __lhs A % mersenne_twister_engine random number generator
543 * object.
544 * @param __rhs Another % mersenne_twister_engine random number
545 * generator object.
546 *
547 * @returns true if the infinite sequences of generated values
548 * would be equal, false otherwise.
549 */
550 friend bool
551 operator==(const mersenne_twister_engine& __lhs,
552 const mersenne_twister_engine& __rhs)
553 { return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x)
554 && __lhs._M_p == __rhs._M_p); }
555
556 /**
557 * @brief Inserts the current state of a % mersenne_twister_engine
558 * random number generator engine @p __x into the output stream
559 * @p __os.
560 *
561 * @param __os An output stream.
562 * @param __x A % mersenne_twister_engine random number generator
563 * engine.
564 *
565 * @returns The output stream with the state of @p __x inserted or in
566 * an error state.
567 */
568 template<typename _UIntType1,
569 size_t __w1, size_t __n1,
570 size_t __m1, size_t __r1,
571 _UIntType1 __a1, size_t __u1,
572 _UIntType1 __d1, size_t __s1,
573 _UIntType1 __b1, size_t __t1,
574 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
575 typename _CharT, typename _Traits>
576 friend std::basic_ostream<_CharT, _Traits>&
577 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
578 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
579 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
580 __l1, __f1>& __x);
581
582 /**
583 * @brief Extracts the current state of a % mersenne_twister_engine
584 * random number generator engine @p __x from the input stream
585 * @p __is.
586 *
587 * @param __is An input stream.
588 * @param __x A % mersenne_twister_engine random number generator
589 * engine.
590 *
591 * @returns The input stream with the state of @p __x extracted or in
592 * an error state.
593 */
594 template<typename _UIntType1,
595 size_t __w1, size_t __n1,
596 size_t __m1, size_t __r1,
597 _UIntType1 __a1, size_t __u1,
598 _UIntType1 __d1, size_t __s1,
599 _UIntType1 __b1, size_t __t1,
600 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
601 typename _CharT, typename _Traits>
602 friend std::basic_istream<_CharT, _Traits>&
603 operator>>(std::basic_istream<_CharT, _Traits>& __is,
604 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
605 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
606 __l1, __f1>& __x);
607
608 private:
609 void _M_gen_rand();
610
611 _UIntType _M_x[state_size];
612 size_t _M_p;
613 };
614
615 /**
616 * @brief Compares two % mersenne_twister_engine random number generator
617 * objects of the same type for inequality.
618 *
619 * @param __lhs A % mersenne_twister_engine random number generator
620 * object.
621 * @param __rhs Another % mersenne_twister_engine random number
622 * generator object.
623 *
624 * @returns true if the infinite sequences of generated values
625 * would be different, false otherwise.
626 */
627 template<typename _UIntType, size_t __w,
628 size_t __n, size_t __m, size_t __r,
629 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
630 _UIntType __b, size_t __t,
631 _UIntType __c, size_t __l, _UIntType __f>
632 inline bool
633 operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
634 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs,
635 const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
636 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs)
637 { return !(__lhs == __rhs); }
638
639
640 /**
641 * @brief The Marsaglia-Zaman generator.
642 *
643 * This is a model of a Generalized Fibonacci discrete random number
644 * generator, sometimes referred to as the SWC generator.
645 *
646 * A discrete random number generator that produces pseudorandom
647 * numbers using:
648 * @f[
649 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
650 * @f]
651 *
652 * The size of the state is @f$r@f$
653 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
654 *
655 * @var _M_x The state of the generator. This is a ring buffer.
656 * @var _M_carry The carry.
657 * @var _M_p Current index of x(i - r).
658 */
659 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
660 class subtract_with_carry_engine
661 {
662 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
663 "substituting _UIntType not an unsigned integral type");
664 static_assert(0u < __s && __s < __r,
665 "template argument substituting __s out of bounds");
666 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
667 "template argument substituting __w out of bounds");
668
669 public:
670 /** The type of the generated random value. */
671 typedef _UIntType result_type;
672
673 // parameter values
674 static constexpr size_t word_size = __w;
675 static constexpr size_t short_lag = __s;
676 static constexpr size_t long_lag = __r;
677 static constexpr result_type default_seed = 19780503u;
678
679 /**
680 * @brief Constructs an explicitly seeded % subtract_with_carry_engine
681 * random number generator.
682 */
683 explicit
684 subtract_with_carry_engine(result_type __sd = default_seed)
685 { seed(__sd); }
686
687 /**
688 * @brief Constructs a %subtract_with_carry_engine random number engine
689 * seeded from the seed sequence @p __q.
690 *
691 * @param __q the seed sequence.
692 */
693 template<typename _Sseq, typename = typename
694 std::enable_if<!std::is_same<_Sseq, subtract_with_carry_engine>::value>
695 ::type>
696 explicit
697 subtract_with_carry_engine(_Sseq& __q)
698 { seed(__q); }
699
700 /**
701 * @brief Seeds the initial state @f$x_0@f$ of the random number
702 * generator.
703 *
704 * N1688[4.19] modifies this as follows. If @p __value == 0,
705 * sets value to 19780503. In any case, with a linear
706 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
707 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
708 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
709 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
710 * set carry to 1, otherwise sets carry to 0.
711 */
712 void
713 seed(result_type __sd = default_seed);
714
715 /**
716 * @brief Seeds the initial state @f$x_0@f$ of the
717 * % subtract_with_carry_engine random number generator.
718 */
719 template<typename _Sseq>
720 typename std::enable_if<std::is_class<_Sseq>::value>::type
721 seed(_Sseq& __q);
722
723 /**
724 * @brief Gets the inclusive minimum value of the range of random
725 * integers returned by this generator.
726 */
727 static constexpr result_type
728 min()
729 { return 0; }
730
731 /**
732 * @brief Gets the inclusive maximum value of the range of random
733 * integers returned by this generator.
734 */
735 static constexpr result_type
736 max()
737 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
738
739 /**
740 * @brief Discard a sequence of random numbers.
741 */
742 void
743 discard(unsigned long long __z)
744 {
745 for (; __z != 0ULL; --__z)
746 (*this)();
747 }
748
749 /**
750 * @brief Gets the next random number in the sequence.
751 */
752 result_type
753 operator()();
754
755 /**
756 * @brief Compares two % subtract_with_carry_engine random number
757 * generator objects of the same type for equality.
758 *
759 * @param __lhs A % subtract_with_carry_engine random number generator
760 * object.
761 * @param __rhs Another % subtract_with_carry_engine random number
762 * generator object.
763 *
764 * @returns true if the infinite sequences of generated values
765 * would be equal, false otherwise.
766 */
767 friend bool
768 operator==(const subtract_with_carry_engine& __lhs,
769 const subtract_with_carry_engine& __rhs)
770 { return (std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x)
771 && __lhs._M_carry == __rhs._M_carry
772 && __lhs._M_p == __rhs._M_p); }
773
774 /**
775 * @brief Inserts the current state of a % subtract_with_carry_engine
776 * random number generator engine @p __x into the output stream
777 * @p __os.
778 *
779 * @param __os An output stream.
780 * @param __x A % subtract_with_carry_engine random number generator
781 * engine.
782 *
783 * @returns The output stream with the state of @p __x inserted or in
784 * an error state.
785 */
786 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
787 typename _CharT, typename _Traits>
788 friend std::basic_ostream<_CharT, _Traits>&
789 operator<<(std::basic_ostream<_CharT, _Traits>&,
790 const std::subtract_with_carry_engine<_UIntType1, __w1,
791 __s1, __r1>&);
792
793 /**
794 * @brief Extracts the current state of a % subtract_with_carry_engine
795 * random number generator engine @p __x from the input stream
796 * @p __is.
797 *
798 * @param __is An input stream.
799 * @param __x A % subtract_with_carry_engine random number generator
800 * engine.
801 *
802 * @returns The input stream with the state of @p __x extracted or in
803 * an error state.
804 */
805 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
806 typename _CharT, typename _Traits>
807 friend std::basic_istream<_CharT, _Traits>&
808 operator>>(std::basic_istream<_CharT, _Traits>&,
809 std::subtract_with_carry_engine<_UIntType1, __w1,
810 __s1, __r1>&);
811
812 private:
813 _UIntType _M_x[long_lag];
814 _UIntType _M_carry;
815 size_t _M_p;
816 };
817
818 /**
819 * @brief Compares two % subtract_with_carry_engine random number
820 * generator objects of the same type for inequality.
821 *
822 * @param __lhs A % subtract_with_carry_engine random number generator
823 * object.
824 * @param __rhs Another % subtract_with_carry_engine random number
825 * generator object.
826 *
827 * @returns true if the infinite sequences of generated values
828 * would be different, false otherwise.
829 */
830 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
831 inline bool
832 operator!=(const std::subtract_with_carry_engine<_UIntType, __w,
833 __s, __r>& __lhs,
834 const std::subtract_with_carry_engine<_UIntType, __w,
835 __s, __r>& __rhs)
836 { return !(__lhs == __rhs); }
837
838
839 /**
840 * Produces random numbers from some base engine by discarding blocks of
841 * data.
842 *
843 * 0 <= @p __r <= @p __p
844 */
845 template<typename _RandomNumberEngine, size_t __p, size_t __r>
846 class discard_block_engine
847 {
848 static_assert(1 <= __r && __r <= __p,
849 "template argument substituting __r out of bounds");
850
851 public:
852 /** The type of the generated random value. */
853 typedef typename _RandomNumberEngine::result_type result_type;
854
855 // parameter values
856 static constexpr size_t block_size = __p;
857 static constexpr size_t used_block = __r;
858
859 /**
860 * @brief Constructs a default %discard_block_engine engine.
861 *
862 * The underlying engine is default constructed as well.
863 */
864 discard_block_engine()
865 : _M_b(), _M_n(0) { }
866
867 /**
868 * @brief Copy constructs a %discard_block_engine engine.
869 *
870 * Copies an existing base class random number generator.
871 * @param __rng An existing (base class) engine object.
872 */
873 explicit
874 discard_block_engine(const _RandomNumberEngine& __rng)
875 : _M_b(__rng), _M_n(0) { }
876
877 /**
878 * @brief Move constructs a %discard_block_engine engine.
879 *
880 * Copies an existing base class random number generator.
881 * @param __rng An existing (base class) engine object.
882 */
883 explicit
884 discard_block_engine(_RandomNumberEngine&& __rng)
885 : _M_b(std::move(__rng)), _M_n(0) { }
886
887 /**
888 * @brief Seed constructs a %discard_block_engine engine.
889 *
890 * Constructs the underlying generator engine seeded with @p __s.
891 * @param __s A seed value for the base class engine.
892 */
893 explicit
894 discard_block_engine(result_type __s)
895 : _M_b(__s), _M_n(0) { }
896
897 /**
898 * @brief Generator construct a %discard_block_engine engine.
899 *
900 * @param __q A seed sequence.
901 */
902 template<typename _Sseq, typename = typename
903 std::enable_if<!std::is_same<_Sseq, discard_block_engine>::value
904 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
905 ::type>
906 explicit
907 discard_block_engine(_Sseq& __q)
908 : _M_b(__q), _M_n(0)
909 { }
910
911 /**
912 * @brief Reseeds the %discard_block_engine object with the default
913 * seed for the underlying base class generator engine.
914 */
915 void
916 seed()
917 {
918 _M_b.seed();
919 _M_n = 0;
920 }
921
922 /**
923 * @brief Reseeds the %discard_block_engine object with the default
924 * seed for the underlying base class generator engine.
925 */
926 void
927 seed(result_type __s)
928 {
929 _M_b.seed(__s);
930 _M_n = 0;
931 }
932
933 /**
934 * @brief Reseeds the %discard_block_engine object with the given seed
935 * sequence.
936 * @param __q A seed generator function.
937 */
938 template<typename _Sseq>
939 void
940 seed(_Sseq& __q)
941 {
942 _M_b.seed(__q);
943 _M_n = 0;
944 }
945
946 /**
947 * @brief Gets a const reference to the underlying generator engine
948 * object.
949 */
950 const _RandomNumberEngine&
951 base() const noexcept
952 { return _M_b; }
953
954 /**
955 * @brief Gets the minimum value in the generated random number range.
956 */
957 static constexpr result_type
958 min()
959 { return _RandomNumberEngine::min(); }
960
961 /**
962 * @brief Gets the maximum value in the generated random number range.
963 */
964 static constexpr result_type
965 max()
966 { return _RandomNumberEngine::max(); }
967
968 /**
969 * @brief Discard a sequence of random numbers.
970 */
971 void
972 discard(unsigned long long __z)
973 {
974 for (; __z != 0ULL; --__z)
975 (*this)();
976 }
977
978 /**
979 * @brief Gets the next value in the generated random number sequence.
980 */
981 result_type
982 operator()();
983
984 /**
985 * @brief Compares two %discard_block_engine random number generator
986 * objects of the same type for equality.
987 *
988 * @param __lhs A %discard_block_engine random number generator object.
989 * @param __rhs Another %discard_block_engine random number generator
990 * object.
991 *
992 * @returns true if the infinite sequences of generated values
993 * would be equal, false otherwise.
994 */
995 friend bool
996 operator==(const discard_block_engine& __lhs,
997 const discard_block_engine& __rhs)
998 { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; }
999
1000 /**
1001 * @brief Inserts the current state of a %discard_block_engine random
1002 * number generator engine @p __x into the output stream
1003 * @p __os.
1004 *
1005 * @param __os An output stream.
1006 * @param __x A %discard_block_engine random number generator engine.
1007 *
1008 * @returns The output stream with the state of @p __x inserted or in
1009 * an error state.
1010 */
1011 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
1012 typename _CharT, typename _Traits>
1013 friend std::basic_ostream<_CharT, _Traits>&
1014 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1015 const std::discard_block_engine<_RandomNumberEngine1,
1016 __p1, __r1>& __x);
1017
1018 /**
1019 * @brief Extracts the current state of a % subtract_with_carry_engine
1020 * random number generator engine @p __x from the input stream
1021 * @p __is.
1022 *
1023 * @param __is An input stream.
1024 * @param __x A %discard_block_engine random number generator engine.
1025 *
1026 * @returns The input stream with the state of @p __x extracted or in
1027 * an error state.
1028 */
1029 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
1030 typename _CharT, typename _Traits>
1031 friend std::basic_istream<_CharT, _Traits>&
1032 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1033 std::discard_block_engine<_RandomNumberEngine1,
1034 __p1, __r1>& __x);
1035
1036 private:
1037 _RandomNumberEngine _M_b;
1038 size_t _M_n;
1039 };
1040
1041 /**
1042 * @brief Compares two %discard_block_engine random number generator
1043 * objects of the same type for inequality.
1044 *
1045 * @param __lhs A %discard_block_engine random number generator object.
1046 * @param __rhs Another %discard_block_engine random number generator
1047 * object.
1048 *
1049 * @returns true if the infinite sequences of generated values
1050 * would be different, false otherwise.
1051 */
1052 template<typename _RandomNumberEngine, size_t __p, size_t __r>
1053 inline bool
1054 operator!=(const std::discard_block_engine<_RandomNumberEngine, __p,
1055 __r>& __lhs,
1056 const std::discard_block_engine<_RandomNumberEngine, __p,
1057 __r>& __rhs)
1058 { return !(__lhs == __rhs); }
1059
1060
1061 /**
1062 * Produces random numbers by combining random numbers from some base
1063 * engine to produce random numbers with a specifies number of bits @p __w.
1064 */
1065 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1066 class independent_bits_engine
1067 {
1068 static_assert(std::is_unsigned<_UIntType>::value, "template argument "
1069 "substituting _UIntType not an unsigned integral type");
1070 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
1071 "template argument substituting __w out of bounds");
1072
1073 public:
1074 /** The type of the generated random value. */
1075 typedef _UIntType result_type;
1076
1077 /**
1078 * @brief Constructs a default %independent_bits_engine engine.
1079 *
1080 * The underlying engine is default constructed as well.
1081 */
1082 independent_bits_engine()
1083 : _M_b() { }
1084
1085 /**
1086 * @brief Copy constructs a %independent_bits_engine engine.
1087 *
1088 * Copies an existing base class random number generator.
1089 * @param __rng An existing (base class) engine object.
1090 */
1091 explicit
1092 independent_bits_engine(const _RandomNumberEngine& __rng)
1093 : _M_b(__rng) { }
1094
1095 /**
1096 * @brief Move constructs a %independent_bits_engine engine.
1097 *
1098 * Copies an existing base class random number generator.
1099 * @param __rng An existing (base class) engine object.
1100 */
1101 explicit
1102 independent_bits_engine(_RandomNumberEngine&& __rng)
1103 : _M_b(std::move(__rng)) { }
1104
1105 /**
1106 * @brief Seed constructs a %independent_bits_engine engine.
1107 *
1108 * Constructs the underlying generator engine seeded with @p __s.
1109 * @param __s A seed value for the base class engine.
1110 */
1111 explicit
1112 independent_bits_engine(result_type __s)
1113 : _M_b(__s) { }
1114
1115 /**
1116 * @brief Generator construct a %independent_bits_engine engine.
1117 *
1118 * @param __q A seed sequence.
1119 */
1120 template<typename _Sseq, typename = typename
1121 std::enable_if<!std::is_same<_Sseq, independent_bits_engine>::value
1122 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
1123 ::type>
1124 explicit
1125 independent_bits_engine(_Sseq& __q)
1126 : _M_b(__q)
1127 { }
1128
1129 /**
1130 * @brief Reseeds the %independent_bits_engine object with the default
1131 * seed for the underlying base class generator engine.
1132 */
1133 void
1134 seed()
1135 { _M_b.seed(); }
1136
1137 /**
1138 * @brief Reseeds the %independent_bits_engine object with the default
1139 * seed for the underlying base class generator engine.
1140 */
1141 void
1142 seed(result_type __s)
1143 { _M_b.seed(__s); }
1144
1145 /**
1146 * @brief Reseeds the %independent_bits_engine object with the given
1147 * seed sequence.
1148 * @param __q A seed generator function.
1149 */
1150 template<typename _Sseq>
1151 void
1152 seed(_Sseq& __q)
1153 { _M_b.seed(__q); }
1154
1155 /**
1156 * @brief Gets a const reference to the underlying generator engine
1157 * object.
1158 */
1159 const _RandomNumberEngine&
1160 base() const noexcept
1161 { return _M_b; }
1162
1163 /**
1164 * @brief Gets the minimum value in the generated random number range.
1165 */
1166 static constexpr result_type
1167 min()
1168 { return 0U; }
1169
1170 /**
1171 * @brief Gets the maximum value in the generated random number range.
1172 */
1173 static constexpr result_type
1174 max()
1175 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
1176
1177 /**
1178 * @brief Discard a sequence of random numbers.
1179 */
1180 void
1181 discard(unsigned long long __z)
1182 {
1183 for (; __z != 0ULL; --__z)
1184 (*this)();
1185 }
1186
1187 /**
1188 * @brief Gets the next value in the generated random number sequence.
1189 */
1190 result_type
1191 operator()();
1192
1193 /**
1194 * @brief Compares two %independent_bits_engine random number generator
1195 * objects of the same type for equality.
1196 *
1197 * @param __lhs A %independent_bits_engine random number generator
1198 * object.
1199 * @param __rhs Another %independent_bits_engine random number generator
1200 * object.
1201 *
1202 * @returns true if the infinite sequences of generated values
1203 * would be equal, false otherwise.
1204 */
1205 friend bool
1206 operator==(const independent_bits_engine& __lhs,
1207 const independent_bits_engine& __rhs)
1208 { return __lhs._M_b == __rhs._M_b; }
1209
1210 /**
1211 * @brief Extracts the current state of a % subtract_with_carry_engine
1212 * random number generator engine @p __x from the input stream
1213 * @p __is.
1214 *
1215 * @param __is An input stream.
1216 * @param __x A %independent_bits_engine random number generator
1217 * engine.
1218 *
1219 * @returns The input stream with the state of @p __x extracted or in
1220 * an error state.
1221 */
1222 template<typename _CharT, typename _Traits>
1223 friend std::basic_istream<_CharT, _Traits>&
1224 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1225 std::independent_bits_engine<_RandomNumberEngine,
1226 __w, _UIntType>& __x)
1227 {
1228 __is >> __x._M_b;
1229 return __is;
1230 }
1231
1232 private:
1233 _RandomNumberEngine _M_b;
1234 };
1235
1236 /**
1237 * @brief Compares two %independent_bits_engine random number generator
1238 * objects of the same type for inequality.
1239 *
1240 * @param __lhs A %independent_bits_engine random number generator
1241 * object.
1242 * @param __rhs Another %independent_bits_engine random number generator
1243 * object.
1244 *
1245 * @returns true if the infinite sequences of generated values
1246 * would be different, false otherwise.
1247 */
1248 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1249 inline bool
1250 operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w,
1251 _UIntType>& __lhs,
1252 const std::independent_bits_engine<_RandomNumberEngine, __w,
1253 _UIntType>& __rhs)
1254 { return !(__lhs == __rhs); }
1255
1256 /**
1257 * @brief Inserts the current state of a %independent_bits_engine random
1258 * number generator engine @p __x into the output stream @p __os.
1259 *
1260 * @param __os An output stream.
1261 * @param __x A %independent_bits_engine random number generator engine.
1262 *
1263 * @returns The output stream with the state of @p __x inserted or in
1264 * an error state.
1265 */
1266 template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1267 typename _CharT, typename _Traits>
1268 std::basic_ostream<_CharT, _Traits>&
1269 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1270 const std::independent_bits_engine<_RandomNumberEngine,
1271 __w, _UIntType>& __x)
1272 {
1273 __os << __x.base();
1274 return __os;
1275 }
1276
1277
1278 /**
1279 * @brief Produces random numbers by combining random numbers from some
1280 * base engine to produce random numbers with a specifies number of bits
1281 * @p __w.
1282 */
1283 template<typename _RandomNumberEngine, size_t __k>
1284 class shuffle_order_engine
1285 {
1286 static_assert(1u <= __k, "template argument substituting "
1287 "__k out of bound");
1288
1289 public:
1290 /** The type of the generated random value. */
1291 typedef typename _RandomNumberEngine::result_type result_type;
1292
1293 static constexpr size_t table_size = __k;
1294
1295 /**
1296 * @brief Constructs a default %shuffle_order_engine engine.
1297 *
1298 * The underlying engine is default constructed as well.
1299 */
1300 shuffle_order_engine()
1301 : _M_b()
1302 { _M_initialize(); }
1303
1304 /**
1305 * @brief Copy constructs a %shuffle_order_engine engine.
1306 *
1307 * Copies an existing base class random number generator.
1308 * @param __rng An existing (base class) engine object.
1309 */
1310 explicit
1311 shuffle_order_engine(const _RandomNumberEngine& __rng)
1312 : _M_b(__rng)
1313 { _M_initialize(); }
1314
1315 /**
1316 * @brief Move constructs a %shuffle_order_engine engine.
1317 *
1318 * Copies an existing base class random number generator.
1319 * @param __rng An existing (base class) engine object.
1320 */
1321 explicit
1322 shuffle_order_engine(_RandomNumberEngine&& __rng)
1323 : _M_b(std::move(__rng))
1324 { _M_initialize(); }
1325
1326 /**
1327 * @brief Seed constructs a %shuffle_order_engine engine.
1328 *
1329 * Constructs the underlying generator engine seeded with @p __s.
1330 * @param __s A seed value for the base class engine.
1331 */
1332 explicit
1333 shuffle_order_engine(result_type __s)
1334 : _M_b(__s)
1335 { _M_initialize(); }
1336
1337 /**
1338 * @brief Generator construct a %shuffle_order_engine engine.
1339 *
1340 * @param __q A seed sequence.
1341 */
1342 template<typename _Sseq, typename = typename
1343 std::enable_if<!std::is_same<_Sseq, shuffle_order_engine>::value
1344 && !std::is_same<_Sseq, _RandomNumberEngine>::value>
1345 ::type>
1346 explicit
1347 shuffle_order_engine(_Sseq& __q)
1348 : _M_b(__q)
1349 { _M_initialize(); }
1350
1351 /**
1352 * @brief Reseeds the %shuffle_order_engine object with the default seed
1353 for the underlying base class generator engine.
1354 */
1355 void
1356 seed()
1357 {
1358 _M_b.seed();
1359 _M_initialize();
1360 }
1361
1362 /**
1363 * @brief Reseeds the %shuffle_order_engine object with the default seed
1364 * for the underlying base class generator engine.
1365 */
1366 void
1367 seed(result_type __s)
1368 {
1369 _M_b.seed(__s);
1370 _M_initialize();
1371 }
1372
1373 /**
1374 * @brief Reseeds the %shuffle_order_engine object with the given seed
1375 * sequence.
1376 * @param __q A seed generator function.
1377 */
1378 template<typename _Sseq>
1379 void
1380 seed(_Sseq& __q)
1381 {
1382 _M_b.seed(__q);
1383 _M_initialize();
1384 }
1385
1386 /**
1387 * Gets a const reference to the underlying generator engine object.
1388 */
1389 const _RandomNumberEngine&
1390 base() const noexcept
1391 { return _M_b; }
1392
1393 /**
1394 * Gets the minimum value in the generated random number range.
1395 */
1396 static constexpr result_type
1397 min()
1398 { return _RandomNumberEngine::min(); }
1399
1400 /**
1401 * Gets the maximum value in the generated random number range.
1402 */
1403 static constexpr result_type
1404 max()
1405 { return _RandomNumberEngine::max(); }
1406
1407 /**
1408 * Discard a sequence of random numbers.
1409 */
1410 void
1411 discard(unsigned long long __z)
1412 {
1413 for (; __z != 0ULL; --__z)
1414 (*this)();
1415 }
1416
1417 /**
1418 * Gets the next value in the generated random number sequence.
1419 */
1420 result_type
1421 operator()();
1422
1423 /**
1424 * Compares two %shuffle_order_engine random number generator objects
1425 * of the same type for equality.
1426 *
1427 * @param __lhs A %shuffle_order_engine random number generator object.
1428 * @param __rhs Another %shuffle_order_engine random number generator
1429 * object.
1430 *
1431 * @returns true if the infinite sequences of generated values
1432 * would be equal, false otherwise.
1433 */
1434 friend bool
1435 operator==(const shuffle_order_engine& __lhs,
1436 const shuffle_order_engine& __rhs)
1437 { return (__lhs._M_b == __rhs._M_b
1438 && std::equal(__lhs._M_v, __lhs._M_v + __k, __rhs._M_v)
1439 && __lhs._M_y == __rhs._M_y); }
1440
1441 /**
1442 * @brief Inserts the current state of a %shuffle_order_engine random
1443 * number generator engine @p __x into the output stream
1444 @p __os.
1445 *
1446 * @param __os An output stream.
1447 * @param __x A %shuffle_order_engine random number generator engine.
1448 *
1449 * @returns The output stream with the state of @p __x inserted or in
1450 * an error state.
1451 */
1452 template<typename _RandomNumberEngine1, size_t __k1,
1453 typename _CharT, typename _Traits>
1454 friend std::basic_ostream<_CharT, _Traits>&
1455 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1456 const std::shuffle_order_engine<_RandomNumberEngine1,
1457 __k1>& __x);
1458
1459 /**
1460 * @brief Extracts the current state of a % subtract_with_carry_engine
1461 * random number generator engine @p __x from the input stream
1462 * @p __is.
1463 *
1464 * @param __is An input stream.
1465 * @param __x A %shuffle_order_engine random number generator engine.
1466 *
1467 * @returns The input stream with the state of @p __x extracted or in
1468 * an error state.
1469 */
1470 template<typename _RandomNumberEngine1, size_t __k1,
1471 typename _CharT, typename _Traits>
1472 friend std::basic_istream<_CharT, _Traits>&
1473 operator>>(std::basic_istream<_CharT, _Traits>& __is,
1474 std::shuffle_order_engine<_RandomNumberEngine1, __k1>& __x);
1475
1476 private:
1477 void _M_initialize()
1478 {
1479 for (size_t __i = 0; __i < __k; ++__i)
1480 _M_v[__i] = _M_b();
1481 _M_y = _M_b();
1482 }
1483
1484 _RandomNumberEngine _M_b;
1485 result_type _M_v[__k];
1486 result_type _M_y;
1487 };
1488
1489 /**
1490 * Compares two %shuffle_order_engine random number generator objects
1491 * of the same type for inequality.
1492 *
1493 * @param __lhs A %shuffle_order_engine random number generator object.
1494 * @param __rhs Another %shuffle_order_engine random number generator
1495 * object.
1496 *
1497 * @returns true if the infinite sequences of generated values
1498 * would be different, false otherwise.
1499 */
1500 template<typename _RandomNumberEngine, size_t __k>
1501 inline bool
1502 operator!=(const std::shuffle_order_engine<_RandomNumberEngine,
1503 __k>& __lhs,
1504 const std::shuffle_order_engine<_RandomNumberEngine,
1505 __k>& __rhs)
1506 { return !(__lhs == __rhs); }
1507
1508
1509 /**
1510 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1511 */
1512 typedef linear_congruential_engine<uint_fast32_t, 16807UL, 0UL, 2147483647UL>
1513 minstd_rand0;
1514
1515 /**
1516 * An alternative LCR (Lehmer Generator function).
1517 */
1518 typedef linear_congruential_engine<uint_fast32_t, 48271UL, 0UL, 2147483647UL>
1519 minstd_rand;
1520
1521 /**
1522 * The classic Mersenne Twister.
1523 *
1524 * Reference:
1525 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1526 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1527 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1528 */
1529 typedef mersenne_twister_engine<
1530 uint_fast32_t,
1531 32, 624, 397, 31,
1532 0x9908b0dfUL, 11,
1533 0xffffffffUL, 7,
1534 0x9d2c5680UL, 15,
1535 0xefc60000UL, 18, 1812433253UL> mt19937;
1536
1537 /**
1538 * An alternative Mersenne Twister.
1539 */
1540 typedef mersenne_twister_engine<
1541 uint_fast64_t,
1542 64, 312, 156, 31,
1543 0xb5026f5aa96619e9ULL, 29,
1544 0x5555555555555555ULL, 17,
1545 0x71d67fffeda60000ULL, 37,
1546 0xfff7eee000000000ULL, 43,
1547 6364136223846793005ULL> mt19937_64;
1548
1549 typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
1550 ranlux24_base;
1551
1552 typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
1553 ranlux48_base;
1554
1555 typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1556
1557 typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1558
1559 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
1560
1561 typedef minstd_rand0 default_random_engine;
1562
1563 /**
1564 * A standard interface to a platform-specific non-deterministic
1565 * random number generator (if any are available).
1566 */
1567 class random_device
1568 {
1569 public:
1570 /** The type of the generated random value. */
1571 typedef unsigned int result_type;
1572
1573 // constructors, destructors and member functions
1574
1575 #ifdef _GLIBCXX_USE_RANDOM_TR1
1576
1577 explicit
1578 random_device(const std::string& __token = "default")
1579 {
1580 _M_init(__token);
1581 }
1582
1583 ~random_device()
1584 { _M_fini(); }
1585
1586 #else
1587
1588 explicit
1589 random_device(const std::string& __token = "mt19937")
1590 { return _M_init_pretr1(__token); }
1591
1592 public:
1593
1594 #endif
1595
1596 static constexpr result_type
1597 min()
1598 { return std::numeric_limits<result_type>::min(); }
1599
1600 static constexpr result_type
1601 max()
1602 { return std::numeric_limits<result_type>::max(); }
1603
1604 double
1605 entropy() const noexcept
1606 { return 0.0; }
1607
1608 result_type
1609 operator()()
1610 {
1611 #ifdef _GLIBCXX_USE_RANDOM_TR1
1612 return this->_M_getval();
1613 #else
1614 return this->_M_getval_pretr1();
1615 #endif
1616 }
1617
1618 // No copy functions.
1619 random_device(const random_device&) = delete;
1620 void operator=(const random_device&) = delete;
1621
1622 private:
1623
1624 void _M_init(const std::string& __token);
1625 void _M_init_pretr1(const std::string& __token);
1626 void _M_fini();
1627
1628 result_type _M_getval();
1629 result_type _M_getval_pretr1();
1630
1631 union
1632 {
1633 FILE* _M_file;
1634 mt19937 _M_mt;
1635 };
1636 };
1637
1638 /* @} */ // group random_generators
1639
1640 /**
1641 * @addtogroup random_distributions Random Number Distributions
1642 * @ingroup random
1643 * @{
1644 */
1645
1646 /**
1647 * @addtogroup random_distributions_uniform Uniform Distributions
1648 * @ingroup random_distributions
1649 * @{
1650 */
1651
1652 /**
1653 * @brief Uniform discrete distribution for random numbers.
1654 * A discrete random distribution on the range @f$[min, max]@f$ with equal
1655 * probability throughout the range.
1656 */
1657 template<typename _IntType = int>
1658 class uniform_int_distribution
1659 {
1660 static_assert(std::is_integral<_IntType>::value,
1661 "template argument not an integral type");
1662
1663 public:
1664 /** The type of the range of the distribution. */
1665 typedef _IntType result_type;
1666 /** Parameter type. */
1667 struct param_type
1668 {
1669 typedef uniform_int_distribution<_IntType> distribution_type;
1670
1671 explicit
1672 param_type(_IntType __a = 0,
1673 _IntType __b = std::numeric_limits<_IntType>::max())
1674 : _M_a(__a), _M_b(__b)
1675 {
1676 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1677 }
1678
1679 result_type
1680 a() const
1681 { return _M_a; }
1682
1683 result_type
1684 b() const
1685 { return _M_b; }
1686
1687 friend bool
1688 operator==(const param_type& __p1, const param_type& __p2)
1689 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
1690
1691 private:
1692 _IntType _M_a;
1693 _IntType _M_b;
1694 };
1695
1696 public:
1697 /**
1698 * @brief Constructs a uniform distribution object.
1699 */
1700 explicit
1701 uniform_int_distribution(_IntType __a = 0,
1702 _IntType __b = std::numeric_limits<_IntType>::max())
1703 : _M_param(__a, __b)
1704 { }
1705
1706 explicit
1707 uniform_int_distribution(const param_type& __p)
1708 : _M_param(__p)
1709 { }
1710
1711 /**
1712 * @brief Resets the distribution state.
1713 *
1714 * Does nothing for the uniform integer distribution.
1715 */
1716 void
1717 reset() { }
1718
1719 result_type
1720 a() const
1721 { return _M_param.a(); }
1722
1723 result_type
1724 b() const
1725 { return _M_param.b(); }
1726
1727 /**
1728 * @brief Returns the parameter set of the distribution.
1729 */
1730 param_type
1731 param() const
1732 { return _M_param; }
1733
1734 /**
1735 * @brief Sets the parameter set of the distribution.
1736 * @param __param The new parameter set of the distribution.
1737 */
1738 void
1739 param(const param_type& __param)
1740 { _M_param = __param; }
1741
1742 /**
1743 * @brief Returns the inclusive lower bound of the distribution range.
1744 */
1745 result_type
1746 min() const
1747 { return this->a(); }
1748
1749 /**
1750 * @brief Returns the inclusive upper bound of the distribution range.
1751 */
1752 result_type
1753 max() const
1754 { return this->b(); }
1755
1756 /**
1757 * @brief Generating functions.
1758 */
1759 template<typename _UniformRandomNumberGenerator>
1760 result_type
1761 operator()(_UniformRandomNumberGenerator& __urng)
1762 { return this->operator()(__urng, this->param()); }
1763
1764 template<typename _UniformRandomNumberGenerator>
1765 result_type
1766 operator()(_UniformRandomNumberGenerator& __urng,
1767 const param_type& __p);
1768
1769 template<typename _ForwardIterator,
1770 typename _UniformRandomNumberGenerator>
1771 void
1772 __generate(_ForwardIterator __f, _ForwardIterator __t,
1773 _UniformRandomNumberGenerator& __urng)
1774 { this->__generate(__f, __t, __urng, this->param()); }
1775
1776 template<typename _ForwardIterator,
1777 typename _UniformRandomNumberGenerator>
1778 void
1779 __generate(_ForwardIterator __f, _ForwardIterator __t,
1780 _UniformRandomNumberGenerator& __urng,
1781 const param_type& __p)
1782 { this->__generate_impl(__f, __t, __urng, __p); }
1783
1784 template<typename _UniformRandomNumberGenerator>
1785 void
1786 __generate(result_type* __f, result_type* __t,
1787 _UniformRandomNumberGenerator& __urng,
1788 const param_type& __p)
1789 { this->__generate_impl(__f, __t, __urng, __p); }
1790
1791 private:
1792 template<typename _ForwardIterator,
1793 typename _UniformRandomNumberGenerator>
1794 void
1795 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
1796 _UniformRandomNumberGenerator& __urng,
1797 const param_type& __p);
1798
1799 param_type _M_param;
1800 };
1801
1802 /**
1803 * @brief Return true if two uniform integer distributions have
1804 * the same parameters.
1805 */
1806 template<typename _IntType>
1807 inline bool
1808 operator==(const std::uniform_int_distribution<_IntType>& __d1,
1809 const std::uniform_int_distribution<_IntType>& __d2)
1810 { return __d1.param() == __d2.param(); }
1811
1812 /**
1813 * @brief Return true if two uniform integer distributions have
1814 * different parameters.
1815 */
1816 template<typename _IntType>
1817 inline bool
1818 operator!=(const std::uniform_int_distribution<_IntType>& __d1,
1819 const std::uniform_int_distribution<_IntType>& __d2)
1820 { return !(__d1 == __d2); }
1821
1822 /**
1823 * @brief Inserts a %uniform_int_distribution random number
1824 * distribution @p __x into the output stream @p os.
1825 *
1826 * @param __os An output stream.
1827 * @param __x A %uniform_int_distribution random number distribution.
1828 *
1829 * @returns The output stream with the state of @p __x inserted or in
1830 * an error state.
1831 */
1832 template<typename _IntType, typename _CharT, typename _Traits>
1833 std::basic_ostream<_CharT, _Traits>&
1834 operator<<(std::basic_ostream<_CharT, _Traits>&,
1835 const std::uniform_int_distribution<_IntType>&);
1836
1837 /**
1838 * @brief Extracts a %uniform_int_distribution random number distribution
1839 * @p __x from the input stream @p __is.
1840 *
1841 * @param __is An input stream.
1842 * @param __x A %uniform_int_distribution random number generator engine.
1843 *
1844 * @returns The input stream with @p __x extracted or in an error state.
1845 */
1846 template<typename _IntType, typename _CharT, typename _Traits>
1847 std::basic_istream<_CharT, _Traits>&
1848 operator>>(std::basic_istream<_CharT, _Traits>&,
1849 std::uniform_int_distribution<_IntType>&);
1850
1851
1852 /**
1853 * @brief Uniform continuous distribution for random numbers.
1854 *
1855 * A continuous random distribution on the range [min, max) with equal
1856 * probability throughout the range. The URNG should be real-valued and
1857 * deliver number in the range [0, 1).
1858 */
1859 template<typename _RealType = double>
1860 class uniform_real_distribution
1861 {
1862 static_assert(std::is_floating_point<_RealType>::value,
1863 "template argument not a floating point type");
1864
1865 public:
1866 /** The type of the range of the distribution. */
1867 typedef _RealType result_type;
1868 /** Parameter type. */
1869 struct param_type
1870 {
1871 typedef uniform_real_distribution<_RealType> distribution_type;
1872
1873 explicit
1874 param_type(_RealType __a = _RealType(0),
1875 _RealType __b = _RealType(1))
1876 : _M_a(__a), _M_b(__b)
1877 {
1878 _GLIBCXX_DEBUG_ASSERT(_M_a <= _M_b);
1879 }
1880
1881 result_type
1882 a() const
1883 { return _M_a; }
1884
1885 result_type
1886 b() const
1887 { return _M_b; }
1888
1889 friend bool
1890 operator==(const param_type& __p1, const param_type& __p2)
1891 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
1892
1893 private:
1894 _RealType _M_a;
1895 _RealType _M_b;
1896 };
1897
1898 public:
1899 /**
1900 * @brief Constructs a uniform_real_distribution object.
1901 *
1902 * @param __a [IN] The lower bound of the distribution.
1903 * @param __b [IN] The upper bound of the distribution.
1904 */
1905 explicit
1906 uniform_real_distribution(_RealType __a = _RealType(0),
1907 _RealType __b = _RealType(1))
1908 : _M_param(__a, __b)
1909 { }
1910
1911 explicit
1912 uniform_real_distribution(const param_type& __p)
1913 : _M_param(__p)
1914 { }
1915
1916 /**
1917 * @brief Resets the distribution state.
1918 *
1919 * Does nothing for the uniform real distribution.
1920 */
1921 void
1922 reset() { }
1923
1924 result_type
1925 a() const
1926 { return _M_param.a(); }
1927
1928 result_type
1929 b() const
1930 { return _M_param.b(); }
1931
1932 /**
1933 * @brief Returns the parameter set of the distribution.
1934 */
1935 param_type
1936 param() const
1937 { return _M_param; }
1938
1939 /**
1940 * @brief Sets the parameter set of the distribution.
1941 * @param __param The new parameter set of the distribution.
1942 */
1943 void
1944 param(const param_type& __param)
1945 { _M_param = __param; }
1946
1947 /**
1948 * @brief Returns the inclusive lower bound of the distribution range.
1949 */
1950 result_type
1951 min() const
1952 { return this->a(); }
1953
1954 /**
1955 * @brief Returns the inclusive upper bound of the distribution range.
1956 */
1957 result_type
1958 max() const
1959 { return this->b(); }
1960
1961 /**
1962 * @brief Generating functions.
1963 */
1964 template<typename _UniformRandomNumberGenerator>
1965 result_type
1966 operator()(_UniformRandomNumberGenerator& __urng)
1967 { return this->operator()(__urng, this->param()); }
1968
1969 template<typename _UniformRandomNumberGenerator>
1970 result_type
1971 operator()(_UniformRandomNumberGenerator& __urng,
1972 const param_type& __p)
1973 {
1974 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
1975 __aurng(__urng);
1976 return (__aurng() * (__p.b() - __p.a())) + __p.a();
1977 }
1978
1979 template<typename _ForwardIterator,
1980 typename _UniformRandomNumberGenerator>
1981 void
1982 __generate(_ForwardIterator __f, _ForwardIterator __t,
1983 _UniformRandomNumberGenerator& __urng)
1984 { this->__generate(__f, __t, __urng, this->param()); }
1985
1986 template<typename _ForwardIterator,
1987 typename _UniformRandomNumberGenerator>
1988 void
1989 __generate(_ForwardIterator __f, _ForwardIterator __t,
1990 _UniformRandomNumberGenerator& __urng,
1991 const param_type& __p)
1992 { this->__generate_impl(__f, __t, __urng, __p); }
1993
1994 template<typename _UniformRandomNumberGenerator>
1995 void
1996 __generate(result_type* __f, result_type* __t,
1997 _UniformRandomNumberGenerator& __urng,
1998 const param_type& __p)
1999 { this->__generate_impl(__f, __t, __urng, __p); }
2000
2001 private:
2002 template<typename _ForwardIterator,
2003 typename _UniformRandomNumberGenerator>
2004 void
2005 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2006 _UniformRandomNumberGenerator& __urng,
2007 const param_type& __p);
2008
2009 param_type _M_param;
2010 };
2011
2012 /**
2013 * @brief Return true if two uniform real distributions have
2014 * the same parameters.
2015 */
2016 template<typename _IntType>
2017 inline bool
2018 operator==(const std::uniform_real_distribution<_IntType>& __d1,
2019 const std::uniform_real_distribution<_IntType>& __d2)
2020 { return __d1.param() == __d2.param(); }
2021
2022 /**
2023 * @brief Return true if two uniform real distributions have
2024 * different parameters.
2025 */
2026 template<typename _IntType>
2027 inline bool
2028 operator!=(const std::uniform_real_distribution<_IntType>& __d1,
2029 const std::uniform_real_distribution<_IntType>& __d2)
2030 { return !(__d1 == __d2); }
2031
2032 /**
2033 * @brief Inserts a %uniform_real_distribution random number
2034 * distribution @p __x into the output stream @p __os.
2035 *
2036 * @param __os An output stream.
2037 * @param __x A %uniform_real_distribution random number distribution.
2038 *
2039 * @returns The output stream with the state of @p __x inserted or in
2040 * an error state.
2041 */
2042 template<typename _RealType, typename _CharT, typename _Traits>
2043 std::basic_ostream<_CharT, _Traits>&
2044 operator<<(std::basic_ostream<_CharT, _Traits>&,
2045 const std::uniform_real_distribution<_RealType>&);
2046
2047 /**
2048 * @brief Extracts a %uniform_real_distribution random number distribution
2049 * @p __x from the input stream @p __is.
2050 *
2051 * @param __is An input stream.
2052 * @param __x A %uniform_real_distribution random number generator engine.
2053 *
2054 * @returns The input stream with @p __x extracted or in an error state.
2055 */
2056 template<typename _RealType, typename _CharT, typename _Traits>
2057 std::basic_istream<_CharT, _Traits>&
2058 operator>>(std::basic_istream<_CharT, _Traits>&,
2059 std::uniform_real_distribution<_RealType>&);
2060
2061 /* @} */ // group random_distributions_uniform
2062
2063 /**
2064 * @addtogroup random_distributions_normal Normal Distributions
2065 * @ingroup random_distributions
2066 * @{
2067 */
2068
2069 /**
2070 * @brief A normal continuous distribution for random numbers.
2071 *
2072 * The formula for the normal probability density function is
2073 * @f[
2074 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
2075 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
2076 * @f]
2077 */
2078 template<typename _RealType = double>
2079 class normal_distribution
2080 {
2081 static_assert(std::is_floating_point<_RealType>::value,
2082 "template argument not a floating point type");
2083
2084 public:
2085 /** The type of the range of the distribution. */
2086 typedef _RealType result_type;
2087 /** Parameter type. */
2088 struct param_type
2089 {
2090 typedef normal_distribution<_RealType> distribution_type;
2091
2092 explicit
2093 param_type(_RealType __mean = _RealType(0),
2094 _RealType __stddev = _RealType(1))
2095 : _M_mean(__mean), _M_stddev(__stddev)
2096 {
2097 _GLIBCXX_DEBUG_ASSERT(_M_stddev > _RealType(0));
2098 }
2099
2100 _RealType
2101 mean() const
2102 { return _M_mean; }
2103
2104 _RealType
2105 stddev() const
2106 { return _M_stddev; }
2107
2108 friend bool
2109 operator==(const param_type& __p1, const param_type& __p2)
2110 { return (__p1._M_mean == __p2._M_mean
2111 && __p1._M_stddev == __p2._M_stddev); }
2112
2113 private:
2114 _RealType _M_mean;
2115 _RealType _M_stddev;
2116 };
2117
2118 public:
2119 /**
2120 * Constructs a normal distribution with parameters @f$mean@f$ and
2121 * standard deviation.
2122 */
2123 explicit
2124 normal_distribution(result_type __mean = result_type(0),
2125 result_type __stddev = result_type(1))
2126 : _M_param(__mean, __stddev), _M_saved_available(false)
2127 { }
2128
2129 explicit
2130 normal_distribution(const param_type& __p)
2131 : _M_param(__p), _M_saved_available(false)
2132 { }
2133
2134 /**
2135 * @brief Resets the distribution state.
2136 */
2137 void
2138 reset()
2139 { _M_saved_available = false; }
2140
2141 /**
2142 * @brief Returns the mean of the distribution.
2143 */
2144 _RealType
2145 mean() const
2146 { return _M_param.mean(); }
2147
2148 /**
2149 * @brief Returns the standard deviation of the distribution.
2150 */
2151 _RealType
2152 stddev() const
2153 { return _M_param.stddev(); }
2154
2155 /**
2156 * @brief Returns the parameter set of the distribution.
2157 */
2158 param_type
2159 param() const
2160 { return _M_param; }
2161
2162 /**
2163 * @brief Sets the parameter set of the distribution.
2164 * @param __param The new parameter set of the distribution.
2165 */
2166 void
2167 param(const param_type& __param)
2168 { _M_param = __param; }
2169
2170 /**
2171 * @brief Returns the greatest lower bound value of the distribution.
2172 */
2173 result_type
2174 min() const
2175 { return std::numeric_limits<result_type>::min(); }
2176
2177 /**
2178 * @brief Returns the least upper bound value of the distribution.
2179 */
2180 result_type
2181 max() const
2182 { return std::numeric_limits<result_type>::max(); }
2183
2184 /**
2185 * @brief Generating functions.
2186 */
2187 template<typename _UniformRandomNumberGenerator>
2188 result_type
2189 operator()(_UniformRandomNumberGenerator& __urng)
2190 { return this->operator()(__urng, this->param()); }
2191
2192 template<typename _UniformRandomNumberGenerator>
2193 result_type
2194 operator()(_UniformRandomNumberGenerator& __urng,
2195 const param_type& __p);
2196
2197 template<typename _ForwardIterator,
2198 typename _UniformRandomNumberGenerator>
2199 void
2200 __generate(_ForwardIterator __f, _ForwardIterator __t,
2201 _UniformRandomNumberGenerator& __urng)
2202 { this->__generate(__f, __t, __urng, this->param()); }
2203
2204 template<typename _ForwardIterator,
2205 typename _UniformRandomNumberGenerator>
2206 void
2207 __generate(_ForwardIterator __f, _ForwardIterator __t,
2208 _UniformRandomNumberGenerator& __urng,
2209 const param_type& __p)
2210 { this->__generate_impl(__f, __t, __urng, __p); }
2211
2212 template<typename _UniformRandomNumberGenerator>
2213 void
2214 __generate(result_type* __f, result_type* __t,
2215 _UniformRandomNumberGenerator& __urng,
2216 const param_type& __p)
2217 { this->__generate_impl(__f, __t, __urng, __p); }
2218
2219 /**
2220 * @brief Return true if two normal distributions have
2221 * the same parameters and the sequences that would
2222 * be generated are equal.
2223 */
2224 template<typename _RealType1>
2225 friend bool
2226 operator==(const std::normal_distribution<_RealType1>& __d1,
2227 const std::normal_distribution<_RealType1>& __d2);
2228
2229 /**
2230 * @brief Inserts a %normal_distribution random number distribution
2231 * @p __x into the output stream @p __os.
2232 *
2233 * @param __os An output stream.
2234 * @param __x A %normal_distribution random number distribution.
2235 *
2236 * @returns The output stream with the state of @p __x inserted or in
2237 * an error state.
2238 */
2239 template<typename _RealType1, typename _CharT, typename _Traits>
2240 friend std::basic_ostream<_CharT, _Traits>&
2241 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2242 const std::normal_distribution<_RealType1>& __x);
2243
2244 /**
2245 * @brief Extracts a %normal_distribution random number distribution
2246 * @p __x from the input stream @p __is.
2247 *
2248 * @param __is An input stream.
2249 * @param __x A %normal_distribution random number generator engine.
2250 *
2251 * @returns The input stream with @p __x extracted or in an error
2252 * state.
2253 */
2254 template<typename _RealType1, typename _CharT, typename _Traits>
2255 friend std::basic_istream<_CharT, _Traits>&
2256 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2257 std::normal_distribution<_RealType1>& __x);
2258
2259 private:
2260 template<typename _ForwardIterator,
2261 typename _UniformRandomNumberGenerator>
2262 void
2263 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2264 _UniformRandomNumberGenerator& __urng,
2265 const param_type& __p);
2266
2267 param_type _M_param;
2268 result_type _M_saved;
2269 bool _M_saved_available;
2270 };
2271
2272 /**
2273 * @brief Return true if two normal distributions are different.
2274 */
2275 template<typename _RealType>
2276 inline bool
2277 operator!=(const std::normal_distribution<_RealType>& __d1,
2278 const std::normal_distribution<_RealType>& __d2)
2279 { return !(__d1 == __d2); }
2280
2281
2282 /**
2283 * @brief A lognormal_distribution random number distribution.
2284 *
2285 * The formula for the normal probability mass function is
2286 * @f[
2287 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2288 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2289 * @f]
2290 */
2291 template<typename _RealType = double>
2292 class lognormal_distribution
2293 {
2294 static_assert(std::is_floating_point<_RealType>::value,
2295 "template argument not a floating point type");
2296
2297 public:
2298 /** The type of the range of the distribution. */
2299 typedef _RealType result_type;
2300 /** Parameter type. */
2301 struct param_type
2302 {
2303 typedef lognormal_distribution<_RealType> distribution_type;
2304
2305 explicit
2306 param_type(_RealType __m = _RealType(0),
2307 _RealType __s = _RealType(1))
2308 : _M_m(__m), _M_s(__s)
2309 { }
2310
2311 _RealType
2312 m() const
2313 { return _M_m; }
2314
2315 _RealType
2316 s() const
2317 { return _M_s; }
2318
2319 friend bool
2320 operator==(const param_type& __p1, const param_type& __p2)
2321 { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; }
2322
2323 private:
2324 _RealType _M_m;
2325 _RealType _M_s;
2326 };
2327
2328 explicit
2329 lognormal_distribution(_RealType __m = _RealType(0),
2330 _RealType __s = _RealType(1))
2331 : _M_param(__m, __s), _M_nd()
2332 { }
2333
2334 explicit
2335 lognormal_distribution(const param_type& __p)
2336 : _M_param(__p), _M_nd()
2337 { }
2338
2339 /**
2340 * Resets the distribution state.
2341 */
2342 void
2343 reset()
2344 { _M_nd.reset(); }
2345
2346 /**
2347 *
2348 */
2349 _RealType
2350 m() const
2351 { return _M_param.m(); }
2352
2353 _RealType
2354 s() const
2355 { return _M_param.s(); }
2356
2357 /**
2358 * @brief Returns the parameter set of the distribution.
2359 */
2360 param_type
2361 param() const
2362 { return _M_param; }
2363
2364 /**
2365 * @brief Sets the parameter set of the distribution.
2366 * @param __param The new parameter set of the distribution.
2367 */
2368 void
2369 param(const param_type& __param)
2370 { _M_param = __param; }
2371
2372 /**
2373 * @brief Returns the greatest lower bound value of the distribution.
2374 */
2375 result_type
2376 min() const
2377 { return result_type(0); }
2378
2379 /**
2380 * @brief Returns the least upper bound value of the distribution.
2381 */
2382 result_type
2383 max() const
2384 { return std::numeric_limits<result_type>::max(); }
2385
2386 /**
2387 * @brief Generating functions.
2388 */
2389 template<typename _UniformRandomNumberGenerator>
2390 result_type
2391 operator()(_UniformRandomNumberGenerator& __urng)
2392 { return this->operator()(__urng, this->param()); }
2393
2394 template<typename _UniformRandomNumberGenerator>
2395 result_type
2396 operator()(_UniformRandomNumberGenerator& __urng,
2397 const param_type& __p)
2398 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
2399
2400 template<typename _ForwardIterator,
2401 typename _UniformRandomNumberGenerator>
2402 void
2403 __generate(_ForwardIterator __f, _ForwardIterator __t,
2404 _UniformRandomNumberGenerator& __urng)
2405 { this->__generate(__f, __t, __urng, this->param()); }
2406
2407 template<typename _ForwardIterator,
2408 typename _UniformRandomNumberGenerator>
2409 void
2410 __generate(_ForwardIterator __f, _ForwardIterator __t,
2411 _UniformRandomNumberGenerator& __urng,
2412 const param_type& __p)
2413 { this->__generate_impl(__f, __t, __urng, __p); }
2414
2415 template<typename _UniformRandomNumberGenerator>
2416 void
2417 __generate(result_type* __f, result_type* __t,
2418 _UniformRandomNumberGenerator& __urng,
2419 const param_type& __p)
2420 { this->__generate_impl(__f, __t, __urng, __p); }
2421
2422 /**
2423 * @brief Return true if two lognormal distributions have
2424 * the same parameters and the sequences that would
2425 * be generated are equal.
2426 */
2427 friend bool
2428 operator==(const lognormal_distribution& __d1,
2429 const lognormal_distribution& __d2)
2430 { return (__d1.param() == __d2.param()
2431 && __d1._M_nd == __d2._M_nd); }
2432
2433 /**
2434 * @brief Inserts a %lognormal_distribution random number distribution
2435 * @p __x into the output stream @p __os.
2436 *
2437 * @param __os An output stream.
2438 * @param __x A %lognormal_distribution random number distribution.
2439 *
2440 * @returns The output stream with the state of @p __x inserted or in
2441 * an error state.
2442 */
2443 template<typename _RealType1, typename _CharT, typename _Traits>
2444 friend std::basic_ostream<_CharT, _Traits>&
2445 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2446 const std::lognormal_distribution<_RealType1>& __x);
2447
2448 /**
2449 * @brief Extracts a %lognormal_distribution random number distribution
2450 * @p __x from the input stream @p __is.
2451 *
2452 * @param __is An input stream.
2453 * @param __x A %lognormal_distribution random number
2454 * generator engine.
2455 *
2456 * @returns The input stream with @p __x extracted or in an error state.
2457 */
2458 template<typename _RealType1, typename _CharT, typename _Traits>
2459 friend std::basic_istream<_CharT, _Traits>&
2460 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2461 std::lognormal_distribution<_RealType1>& __x);
2462
2463 private:
2464 template<typename _ForwardIterator,
2465 typename _UniformRandomNumberGenerator>
2466 void
2467 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2468 _UniformRandomNumberGenerator& __urng,
2469 const param_type& __p);
2470
2471 param_type _M_param;
2472
2473 std::normal_distribution<result_type> _M_nd;
2474 };
2475
2476 /**
2477 * @brief Return true if two lognormal distributions are different.
2478 */
2479 template<typename _RealType>
2480 inline bool
2481 operator!=(const std::lognormal_distribution<_RealType>& __d1,
2482 const std::lognormal_distribution<_RealType>& __d2)
2483 { return !(__d1 == __d2); }
2484
2485
2486 /**
2487 * @brief A gamma continuous distribution for random numbers.
2488 *
2489 * The formula for the gamma probability density function is:
2490 * @f[
2491 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2492 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2493 * @f]
2494 */
2495 template<typename _RealType = double>
2496 class gamma_distribution
2497 {
2498 static_assert(std::is_floating_point<_RealType>::value,
2499 "template argument not a floating point type");
2500
2501 public:
2502 /** The type of the range of the distribution. */
2503 typedef _RealType result_type;
2504 /** Parameter type. */
2505 struct param_type
2506 {
2507 typedef gamma_distribution<_RealType> distribution_type;
2508 friend class gamma_distribution<_RealType>;
2509
2510 explicit
2511 param_type(_RealType __alpha_val = _RealType(1),
2512 _RealType __beta_val = _RealType(1))
2513 : _M_alpha(__alpha_val), _M_beta(__beta_val)
2514 {
2515 _GLIBCXX_DEBUG_ASSERT(_M_alpha > _RealType(0));
2516 _M_initialize();
2517 }
2518
2519 _RealType
2520 alpha() const
2521 { return _M_alpha; }
2522
2523 _RealType
2524 beta() const
2525 { return _M_beta; }
2526
2527 friend bool
2528 operator==(const param_type& __p1, const param_type& __p2)
2529 { return (__p1._M_alpha == __p2._M_alpha
2530 && __p1._M_beta == __p2._M_beta); }
2531
2532 private:
2533 void
2534 _M_initialize();
2535
2536 _RealType _M_alpha;
2537 _RealType _M_beta;
2538
2539 _RealType _M_malpha, _M_a2;
2540 };
2541
2542 public:
2543 /**
2544 * @brief Constructs a gamma distribution with parameters
2545 * @f$\alpha@f$ and @f$\beta@f$.
2546 */
2547 explicit
2548 gamma_distribution(_RealType __alpha_val = _RealType(1),
2549 _RealType __beta_val = _RealType(1))
2550 : _M_param(__alpha_val, __beta_val), _M_nd()
2551 { }
2552
2553 explicit
2554 gamma_distribution(const param_type& __p)
2555 : _M_param(__p), _M_nd()
2556 { }
2557
2558 /**
2559 * @brief Resets the distribution state.
2560 */
2561 void
2562 reset()
2563 { _M_nd.reset(); }
2564
2565 /**
2566 * @brief Returns the @f$\alpha@f$ of the distribution.
2567 */
2568 _RealType
2569 alpha() const
2570 { return _M_param.alpha(); }
2571
2572 /**
2573 * @brief Returns the @f$\beta@f$ of the distribution.
2574 */
2575 _RealType
2576 beta() const
2577 { return _M_param.beta(); }
2578
2579 /**
2580 * @brief Returns the parameter set of the distribution.
2581 */
2582 param_type
2583 param() const
2584 { return _M_param; }
2585
2586 /**
2587 * @brief Sets the parameter set of the distribution.
2588 * @param __param The new parameter set of the distribution.
2589 */
2590 void
2591 param(const param_type& __param)
2592 { _M_param = __param; }
2593
2594 /**
2595 * @brief Returns the greatest lower bound value of the distribution.
2596 */
2597 result_type
2598 min() const
2599 { return result_type(0); }
2600
2601 /**
2602 * @brief Returns the least upper bound value of the distribution.
2603 */
2604 result_type
2605 max() const
2606 { return std::numeric_limits<result_type>::max(); }
2607
2608 /**
2609 * @brief Generating functions.
2610 */
2611 template<typename _UniformRandomNumberGenerator>
2612 result_type
2613 operator()(_UniformRandomNumberGenerator& __urng)
2614 { return this->operator()(__urng, this->param()); }
2615
2616 template<typename _UniformRandomNumberGenerator>
2617 result_type
2618 operator()(_UniformRandomNumberGenerator& __urng,
2619 const param_type& __p);
2620
2621 template<typename _ForwardIterator,
2622 typename _UniformRandomNumberGenerator>
2623 void
2624 __generate(_ForwardIterator __f, _ForwardIterator __t,
2625 _UniformRandomNumberGenerator& __urng)
2626 { this->__generate(__f, __t, __urng, this->param()); }
2627
2628 template<typename _ForwardIterator,
2629 typename _UniformRandomNumberGenerator>
2630 void
2631 __generate(_ForwardIterator __f, _ForwardIterator __t,
2632 _UniformRandomNumberGenerator& __urng,
2633 const param_type& __p)
2634 { this->__generate_impl(__f, __t, __urng, __p); }
2635
2636 template<typename _UniformRandomNumberGenerator>
2637 void
2638 __generate(result_type* __f, result_type* __t,
2639 _UniformRandomNumberGenerator& __urng,
2640 const param_type& __p)
2641 { this->__generate_impl(__f, __t, __urng, __p); }
2642
2643 /**
2644 * @brief Return true if two gamma distributions have the same
2645 * parameters and the sequences that would be generated
2646 * are equal.
2647 */
2648 friend bool
2649 operator==(const gamma_distribution& __d1,
2650 const gamma_distribution& __d2)
2651 { return (__d1.param() == __d2.param()
2652 && __d1._M_nd == __d2._M_nd); }
2653
2654 /**
2655 * @brief Inserts a %gamma_distribution random number distribution
2656 * @p __x into the output stream @p __os.
2657 *
2658 * @param __os An output stream.
2659 * @param __x A %gamma_distribution random number distribution.
2660 *
2661 * @returns The output stream with the state of @p __x inserted or in
2662 * an error state.
2663 */
2664 template<typename _RealType1, typename _CharT, typename _Traits>
2665 friend std::basic_ostream<_CharT, _Traits>&
2666 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2667 const std::gamma_distribution<_RealType1>& __x);
2668
2669 /**
2670 * @brief Extracts a %gamma_distribution random number distribution
2671 * @p __x from the input stream @p __is.
2672 *
2673 * @param __is An input stream.
2674 * @param __x A %gamma_distribution random number generator engine.
2675 *
2676 * @returns The input stream with @p __x extracted or in an error state.
2677 */
2678 template<typename _RealType1, typename _CharT, typename _Traits>
2679 friend std::basic_istream<_CharT, _Traits>&
2680 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2681 std::gamma_distribution<_RealType1>& __x);
2682
2683 private:
2684 template<typename _ForwardIterator,
2685 typename _UniformRandomNumberGenerator>
2686 void
2687 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2688 _UniformRandomNumberGenerator& __urng,
2689 const param_type& __p);
2690
2691 param_type _M_param;
2692
2693 std::normal_distribution<result_type> _M_nd;
2694 };
2695
2696 /**
2697 * @brief Return true if two gamma distributions are different.
2698 */
2699 template<typename _RealType>
2700 inline bool
2701 operator!=(const std::gamma_distribution<_RealType>& __d1,
2702 const std::gamma_distribution<_RealType>& __d2)
2703 { return !(__d1 == __d2); }
2704
2705
2706 /**
2707 * @brief A chi_squared_distribution random number distribution.
2708 *
2709 * The formula for the normal probability mass function is
2710 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2711 */
2712 template<typename _RealType = double>
2713 class chi_squared_distribution
2714 {
2715 static_assert(std::is_floating_point<_RealType>::value,
2716 "template argument not a floating point type");
2717
2718 public:
2719 /** The type of the range of the distribution. */
2720 typedef _RealType result_type;
2721 /** Parameter type. */
2722 struct param_type
2723 {
2724 typedef chi_squared_distribution<_RealType> distribution_type;
2725
2726 explicit
2727 param_type(_RealType __n = _RealType(1))
2728 : _M_n(__n)
2729 { }
2730
2731 _RealType
2732 n() const
2733 { return _M_n; }
2734
2735 friend bool
2736 operator==(const param_type& __p1, const param_type& __p2)
2737 { return __p1._M_n == __p2._M_n; }
2738
2739 private:
2740 _RealType _M_n;
2741 };
2742
2743 explicit
2744 chi_squared_distribution(_RealType __n = _RealType(1))
2745 : _M_param(__n), _M_gd(__n / 2)
2746 { }
2747
2748 explicit
2749 chi_squared_distribution(const param_type& __p)
2750 : _M_param(__p), _M_gd(__p.n() / 2)
2751 { }
2752
2753 /**
2754 * @brief Resets the distribution state.
2755 */
2756 void
2757 reset()
2758 { _M_gd.reset(); }
2759
2760 /**
2761 *
2762 */
2763 _RealType
2764 n() const
2765 { return _M_param.n(); }
2766
2767 /**
2768 * @brief Returns the parameter set of the distribution.
2769 */
2770 param_type
2771 param() const
2772 { return _M_param; }
2773
2774 /**
2775 * @brief Sets the parameter set of the distribution.
2776 * @param __param The new parameter set of the distribution.
2777 */
2778 void
2779 param(const param_type& __param)
2780 { _M_param = __param; }
2781
2782 /**
2783 * @brief Returns the greatest lower bound value of the distribution.
2784 */
2785 result_type
2786 min() const
2787 { return result_type(0); }
2788
2789 /**
2790 * @brief Returns the least upper bound value of the distribution.
2791 */
2792 result_type
2793 max() const
2794 { return std::numeric_limits<result_type>::max(); }
2795
2796 /**
2797 * @brief Generating functions.
2798 */
2799 template<typename _UniformRandomNumberGenerator>
2800 result_type
2801 operator()(_UniformRandomNumberGenerator& __urng)
2802 { return 2 * _M_gd(__urng); }
2803
2804 template<typename _UniformRandomNumberGenerator>
2805 result_type
2806 operator()(_UniformRandomNumberGenerator& __urng,
2807 const param_type& __p)
2808 {
2809 typedef typename std::gamma_distribution<result_type>::param_type
2810 param_type;
2811 return 2 * _M_gd(__urng, param_type(__p.n() / 2));
2812 }
2813
2814 template<typename _ForwardIterator,
2815 typename _UniformRandomNumberGenerator>
2816 void
2817 __generate(_ForwardIterator __f, _ForwardIterator __t,
2818 _UniformRandomNumberGenerator& __urng)
2819 { this->__generate_impl(__f, __t, __urng, _M_gd.param()); }
2820
2821 template<typename _ForwardIterator,
2822 typename _UniformRandomNumberGenerator>
2823 void
2824 __generate(_ForwardIterator __f, _ForwardIterator __t,
2825 _UniformRandomNumberGenerator& __urng,
2826 const param_type& __p)
2827 { typename std::gamma_distribution<result_type>::param_type
2828 __p2(__p.n() / 2);
2829 this->__generate_impl(__f, __t, __urng, __p2); }
2830
2831 template<typename _UniformRandomNumberGenerator>
2832 void
2833 __generate(result_type* __f, result_type* __t,
2834 _UniformRandomNumberGenerator& __urng)
2835 { typename std::gamma_distribution<result_type>::param_type
2836 __p2(_M_gd.param());
2837 this->__generate_impl(__f, __t, __urng, __p2); }
2838
2839 template<typename _UniformRandomNumberGenerator>
2840 void
2841 __generate(result_type* __f, result_type* __t,
2842 _UniformRandomNumberGenerator& __urng,
2843 const param_type& __p)
2844 { typename std::gamma_distribution<result_type>::param_type
2845 __p2(__p.n() / 2);
2846 this->__generate_impl(__f, __t, __urng, __p2); }
2847
2848 /**
2849 * @brief Return true if two Chi-squared distributions have
2850 * the same parameters and the sequences that would be
2851 * generated are equal.
2852 */
2853 friend bool
2854 operator==(const chi_squared_distribution& __d1,
2855 const chi_squared_distribution& __d2)
2856 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; }
2857
2858 /**
2859 * @brief Inserts a %chi_squared_distribution random number distribution
2860 * @p __x into the output stream @p __os.
2861 *
2862 * @param __os An output stream.
2863 * @param __x A %chi_squared_distribution random number distribution.
2864 *
2865 * @returns The output stream with the state of @p __x inserted or in
2866 * an error state.
2867 */
2868 template<typename _RealType1, typename _CharT, typename _Traits>
2869 friend std::basic_ostream<_CharT, _Traits>&
2870 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
2871 const std::chi_squared_distribution<_RealType1>& __x);
2872
2873 /**
2874 * @brief Extracts a %chi_squared_distribution random number distribution
2875 * @p __x from the input stream @p __is.
2876 *
2877 * @param __is An input stream.
2878 * @param __x A %chi_squared_distribution random number
2879 * generator engine.
2880 *
2881 * @returns The input stream with @p __x extracted or in an error state.
2882 */
2883 template<typename _RealType1, typename _CharT, typename _Traits>
2884 friend std::basic_istream<_CharT, _Traits>&
2885 operator>>(std::basic_istream<_CharT, _Traits>& __is,
2886 std::chi_squared_distribution<_RealType1>& __x);
2887
2888 private:
2889 template<typename _ForwardIterator,
2890 typename _UniformRandomNumberGenerator>
2891 void
2892 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2893 _UniformRandomNumberGenerator& __urng,
2894 typename std::gamma_distribution<result_type>::param_type&
2895 __p);
2896
2897 param_type _M_param;
2898
2899 std::gamma_distribution<result_type> _M_gd;
2900 };
2901
2902 /**
2903 * @brief Return true if two Chi-squared distributions are different.
2904 */
2905 template<typename _RealType>
2906 inline bool
2907 operator!=(const std::chi_squared_distribution<_RealType>& __d1,
2908 const std::chi_squared_distribution<_RealType>& __d2)
2909 { return !(__d1 == __d2); }
2910
2911
2912 /**
2913 * @brief A cauchy_distribution random number distribution.
2914 *
2915 * The formula for the normal probability mass function is
2916 * @f$p(x|a,b) = (\pi b (1 + (\frac{x-a}{b})^2))^{-1}@f$
2917 */
2918 template<typename _RealType = double>
2919 class cauchy_distribution
2920 {
2921 static_assert(std::is_floating_point<_RealType>::value,
2922 "template argument not a floating point type");
2923
2924 public:
2925 /** The type of the range of the distribution. */
2926 typedef _RealType result_type;
2927 /** Parameter type. */
2928 struct param_type
2929 {
2930 typedef cauchy_distribution<_RealType> distribution_type;
2931
2932 explicit
2933 param_type(_RealType __a = _RealType(0),
2934 _RealType __b = _RealType(1))
2935 : _M_a(__a), _M_b(__b)
2936 { }
2937
2938 _RealType
2939 a() const
2940 { return _M_a; }
2941
2942 _RealType
2943 b() const
2944 { return _M_b; }
2945
2946 friend bool
2947 operator==(const param_type& __p1, const param_type& __p2)
2948 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
2949
2950 private:
2951 _RealType _M_a;
2952 _RealType _M_b;
2953 };
2954
2955 explicit
2956 cauchy_distribution(_RealType __a = _RealType(0),
2957 _RealType __b = _RealType(1))
2958 : _M_param(__a, __b)
2959 { }
2960
2961 explicit
2962 cauchy_distribution(const param_type& __p)
2963 : _M_param(__p)
2964 { }
2965
2966 /**
2967 * @brief Resets the distribution state.
2968 */
2969 void
2970 reset()
2971 { }
2972
2973 /**
2974 *
2975 */
2976 _RealType
2977 a() const
2978 { return _M_param.a(); }
2979
2980 _RealType
2981 b() const
2982 { return _M_param.b(); }
2983
2984 /**
2985 * @brief Returns the parameter set of the distribution.
2986 */
2987 param_type
2988 param() const
2989 { return _M_param; }
2990
2991 /**
2992 * @brief Sets the parameter set of the distribution.
2993 * @param __param The new parameter set of the distribution.
2994 */
2995 void
2996 param(const param_type& __param)
2997 { _M_param = __param; }
2998
2999 /**
3000 * @brief Returns the greatest lower bound value of the distribution.
3001 */
3002 result_type
3003 min() const
3004 { return std::numeric_limits<result_type>::min(); }
3005
3006 /**
3007 * @brief Returns the least upper bound value of the distribution.
3008 */
3009 result_type
3010 max() const
3011 { return std::numeric_limits<result_type>::max(); }
3012
3013 /**
3014 * @brief Generating functions.
3015 */
3016 template<typename _UniformRandomNumberGenerator>
3017 result_type
3018 operator()(_UniformRandomNumberGenerator& __urng)
3019 { return this->operator()(__urng, this->param()); }
3020
3021 template<typename _UniformRandomNumberGenerator>
3022 result_type
3023 operator()(_UniformRandomNumberGenerator& __urng,
3024 const param_type& __p);
3025
3026 template<typename _ForwardIterator,
3027 typename _UniformRandomNumberGenerator>
3028 void
3029 __generate(_ForwardIterator __f, _ForwardIterator __t,
3030 _UniformRandomNumberGenerator& __urng)
3031 { this->__generate(__f, __t, __urng, this->param()); }
3032
3033 template<typename _ForwardIterator,
3034 typename _UniformRandomNumberGenerator>
3035 void
3036 __generate(_ForwardIterator __f, _ForwardIterator __t,
3037 _UniformRandomNumberGenerator& __urng,
3038 const param_type& __p)
3039 { this->__generate_impl(__f, __t, __urng, __p); }
3040
3041 template<typename _UniformRandomNumberGenerator>
3042 void
3043 __generate(result_type* __f, result_type* __t,
3044 _UniformRandomNumberGenerator& __urng,
3045 const param_type& __p)
3046 { this->__generate_impl(__f, __t, __urng, __p); }
3047
3048 private:
3049 template<typename _ForwardIterator,
3050 typename _UniformRandomNumberGenerator>
3051 void
3052 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3053 _UniformRandomNumberGenerator& __urng,
3054 const param_type& __p);
3055
3056 param_type _M_param;
3057 };
3058
3059 /**
3060 * @brief Return true if two Cauchy distributions have
3061 * the same parameters.
3062 */
3063 template<typename _RealType>
3064 inline bool
3065 operator==(const std::cauchy_distribution<_RealType>& __d1,
3066 const std::cauchy_distribution<_RealType>& __d2)
3067 { return __d1.param() == __d2.param(); }
3068
3069 /**
3070 * @brief Return true if two Cauchy distributions have
3071 * different parameters.
3072 */
3073 template<typename _RealType>
3074 inline bool
3075 operator!=(const std::cauchy_distribution<_RealType>& __d1,
3076 const std::cauchy_distribution<_RealType>& __d2)
3077 { return !(__d1 == __d2); }
3078
3079 /**
3080 * @brief Inserts a %cauchy_distribution random number distribution
3081 * @p __x into the output stream @p __os.
3082 *
3083 * @param __os An output stream.
3084 * @param __x A %cauchy_distribution random number distribution.
3085 *
3086 * @returns The output stream with the state of @p __x inserted or in
3087 * an error state.
3088 */
3089 template<typename _RealType, typename _CharT, typename _Traits>
3090 std::basic_ostream<_CharT, _Traits>&
3091 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3092 const std::cauchy_distribution<_RealType>& __x);
3093
3094 /**
3095 * @brief Extracts a %cauchy_distribution random number distribution
3096 * @p __x from the input stream @p __is.
3097 *
3098 * @param __is An input stream.
3099 * @param __x A %cauchy_distribution random number
3100 * generator engine.
3101 *
3102 * @returns The input stream with @p __x extracted or in an error state.
3103 */
3104 template<typename _RealType, typename _CharT, typename _Traits>
3105 std::basic_istream<_CharT, _Traits>&
3106 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3107 std::cauchy_distribution<_RealType>& __x);
3108
3109
3110 /**
3111 * @brief A fisher_f_distribution random number distribution.
3112 *
3113 * The formula for the normal probability mass function is
3114 * @f[
3115 * p(x|m,n) = \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
3116 * (\frac{m}{n})^{m/2} x^{(m/2)-1}
3117 * (1 + \frac{mx}{n})^{-(m+n)/2}
3118 * @f]
3119 */
3120 template<typename _RealType = double>
3121 class fisher_f_distribution
3122 {
3123 static_assert(std::is_floating_point<_RealType>::value,
3124 "template argument not a floating point type");
3125
3126 public:
3127 /** The type of the range of the distribution. */
3128 typedef _RealType result_type;
3129 /** Parameter type. */
3130 struct param_type
3131 {
3132 typedef fisher_f_distribution<_RealType> distribution_type;
3133
3134 explicit
3135 param_type(_RealType __m = _RealType(1),
3136 _RealType __n = _RealType(1))
3137 : _M_m(__m), _M_n(__n)
3138 { }
3139
3140 _RealType
3141 m() const
3142 { return _M_m; }
3143
3144 _RealType
3145 n() const
3146 { return _M_n; }
3147
3148 friend bool
3149 operator==(const param_type& __p1, const param_type& __p2)
3150 { return __p1._M_m == __p2._M_m && __p1._M_n == __p2._M_n; }
3151
3152 private:
3153 _RealType _M_m;
3154 _RealType _M_n;
3155 };
3156
3157 explicit
3158 fisher_f_distribution(_RealType __m = _RealType(1),
3159 _RealType __n = _RealType(1))
3160 : _M_param(__m, __n), _M_gd_x(__m / 2), _M_gd_y(__n / 2)
3161 { }
3162
3163 explicit
3164 fisher_f_distribution(const param_type& __p)
3165 : _M_param(__p), _M_gd_x(__p.m() / 2), _M_gd_y(__p.n() / 2)
3166 { }
3167
3168 /**
3169 * @brief Resets the distribution state.
3170 */
3171 void
3172 reset()
3173 {
3174 _M_gd_x.reset();
3175 _M_gd_y.reset();
3176 }
3177
3178 /**
3179 *
3180 */
3181 _RealType
3182 m() const
3183 { return _M_param.m(); }
3184
3185 _RealType
3186 n() const
3187 { return _M_param.n(); }
3188
3189 /**
3190 * @brief Returns the parameter set of the distribution.
3191 */
3192 param_type
3193 param() const
3194 { return _M_param; }
3195
3196 /**
3197 * @brief Sets the parameter set of the distribution.
3198 * @param __param The new parameter set of the distribution.
3199 */
3200 void
3201 param(const param_type& __param)
3202 { _M_param = __param; }
3203
3204 /**
3205 * @brief Returns the greatest lower bound value of the distribution.
3206 */
3207 result_type
3208 min() const
3209 { return result_type(0); }
3210
3211 /**
3212 * @brief Returns the least upper bound value of the distribution.
3213 */
3214 result_type
3215 max() const
3216 { return std::numeric_limits<result_type>::max(); }
3217
3218 /**
3219 * @brief Generating functions.
3220 */
3221 template<typename _UniformRandomNumberGenerator>
3222 result_type
3223 operator()(_UniformRandomNumberGenerator& __urng)
3224 { return (_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m()); }
3225
3226 template<typename _UniformRandomNumberGenerator>
3227 result_type
3228 operator()(_UniformRandomNumberGenerator& __urng,
3229 const param_type& __p)
3230 {
3231 typedef typename std::gamma_distribution<result_type>::param_type
3232 param_type;
3233 return ((_M_gd_x(__urng, param_type(__p.m() / 2)) * n())
3234 / (_M_gd_y(__urng, param_type(__p.n() / 2)) * m()));
3235 }
3236
3237 template<typename _ForwardIterator,
3238 typename _UniformRandomNumberGenerator>
3239 void
3240 __generate(_ForwardIterator __f, _ForwardIterator __t,
3241 _UniformRandomNumberGenerator& __urng)
3242 { this->__generate_impl(__f, __t, __urng); }
3243
3244 template<typename _ForwardIterator,
3245 typename _UniformRandomNumberGenerator>
3246 void
3247 __generate(_ForwardIterator __f, _ForwardIterator __t,
3248 _UniformRandomNumberGenerator& __urng,
3249 const param_type& __p)
3250 { this->__generate_impl(__f, __t, __urng, __p); }
3251
3252 template<typename _UniformRandomNumberGenerator>
3253 void
3254 __generate(result_type* __f, result_type* __t,
3255 _UniformRandomNumberGenerator& __urng)
3256 { this->__generate_impl(__f, __t, __urng); }
3257
3258 template<typename _UniformRandomNumberGenerator>
3259 void
3260 __generate(result_type* __f, result_type* __t,
3261 _UniformRandomNumberGenerator& __urng,
3262 const param_type& __p)
3263 { this->__generate_impl(__f, __t, __urng, __p); }
3264
3265 /**
3266 * @brief Return true if two Fisher f distributions have
3267 * the same parameters and the sequences that would
3268 * be generated are equal.
3269 */
3270 friend bool
3271 operator==(const fisher_f_distribution& __d1,
3272 const fisher_f_distribution& __d2)
3273 { return (__d1.param() == __d2.param()
3274 && __d1._M_gd_x == __d2._M_gd_x
3275 && __d1._M_gd_y == __d2._M_gd_y); }
3276
3277 /**
3278 * @brief Inserts a %fisher_f_distribution random number distribution
3279 * @p __x into the output stream @p __os.
3280 *
3281 * @param __os An output stream.
3282 * @param __x A %fisher_f_distribution random number distribution.
3283 *
3284 * @returns The output stream with the state of @p __x inserted or in
3285 * an error state.
3286 */
3287 template<typename _RealType1, typename _CharT, typename _Traits>
3288 friend std::basic_ostream<_CharT, _Traits>&
3289 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3290 const std::fisher_f_distribution<_RealType1>& __x);
3291
3292 /**
3293 * @brief Extracts a %fisher_f_distribution random number distribution
3294 * @p __x from the input stream @p __is.
3295 *
3296 * @param __is An input stream.
3297 * @param __x A %fisher_f_distribution random number
3298 * generator engine.
3299 *
3300 * @returns The input stream with @p __x extracted or in an error state.
3301 */
3302 template<typename _RealType1, typename _CharT, typename _Traits>
3303 friend std::basic_istream<_CharT, _Traits>&
3304 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3305 std::fisher_f_distribution<_RealType1>& __x);
3306
3307 private:
3308 template<typename _ForwardIterator,
3309 typename _UniformRandomNumberGenerator>
3310 void
3311 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3312 _UniformRandomNumberGenerator& __urng);
3313
3314 template<typename _ForwardIterator,
3315 typename _UniformRandomNumberGenerator>
3316 void
3317 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3318 _UniformRandomNumberGenerator& __urng,
3319 const param_type& __p);
3320
3321 param_type _M_param;
3322
3323 std::gamma_distribution<result_type> _M_gd_x, _M_gd_y;
3324 };
3325
3326 /**
3327 * @brief Return true if two Fisher f distributions are diferent.
3328 */
3329 template<typename _RealType>
3330 inline bool
3331 operator!=(const std::fisher_f_distribution<_RealType>& __d1,
3332 const std::fisher_f_distribution<_RealType>& __d2)
3333 { return !(__d1 == __d2); }
3334
3335 /**
3336 * @brief A student_t_distribution random number distribution.
3337 *
3338 * The formula for the normal probability mass function is:
3339 * @f[
3340 * p(x|n) = \frac{1}{\sqrt(n\pi)} \frac{\Gamma((n+1)/2)}{\Gamma(n/2)}
3341 * (1 + \frac{x^2}{n}) ^{-(n+1)/2}
3342 * @f]
3343 */
3344 template<typename _RealType = double>
3345 class student_t_distribution
3346 {
3347 static_assert(std::is_floating_point<_RealType>::value,
3348 "template argument not a floating point type");
3349
3350 public:
3351 /** The type of the range of the distribution. */
3352 typedef _RealType result_type;
3353 /** Parameter type. */
3354 struct param_type
3355 {
3356 typedef student_t_distribution<_RealType> distribution_type;
3357
3358 explicit
3359 param_type(_RealType __n = _RealType(1))
3360 : _M_n(__n)
3361 { }
3362
3363 _RealType
3364 n() const
3365 { return _M_n; }
3366
3367 friend bool
3368 operator==(const param_type& __p1, const param_type& __p2)
3369 { return __p1._M_n == __p2._M_n; }
3370
3371 private:
3372 _RealType _M_n;
3373 };
3374
3375 explicit
3376 student_t_distribution(_RealType __n = _RealType(1))
3377 : _M_param(__n), _M_nd(), _M_gd(__n / 2, 2)
3378 { }
3379
3380 explicit
3381 student_t_distribution(const param_type& __p)
3382 : _M_param(__p), _M_nd(), _M_gd(__p.n() / 2, 2)
3383 { }
3384
3385 /**
3386 * @brief Resets the distribution state.
3387 */
3388 void
3389 reset()
3390 {
3391 _M_nd.reset();
3392 _M_gd.reset();
3393 }
3394
3395 /**
3396 *
3397 */
3398 _RealType
3399 n() const
3400 { return _M_param.n(); }
3401
3402 /**
3403 * @brief Returns the parameter set of the distribution.
3404 */
3405 param_type
3406 param() const
3407 { return _M_param; }
3408
3409 /**
3410 * @brief Sets the parameter set of the distribution.
3411 * @param __param The new parameter set of the distribution.
3412 */
3413 void
3414 param(const param_type& __param)
3415 { _M_param = __param; }
3416
3417 /**
3418 * @brief Returns the greatest lower bound value of the distribution.
3419 */
3420 result_type
3421 min() const
3422 { return std::numeric_limits<result_type>::min(); }
3423
3424 /**
3425 * @brief Returns the least upper bound value of the distribution.
3426 */
3427 result_type
3428 max() const
3429 { return std::numeric_limits<result_type>::max(); }
3430
3431 /**
3432 * @brief Generating functions.
3433 */
3434 template<typename _UniformRandomNumberGenerator>
3435 result_type
3436 operator()(_UniformRandomNumberGenerator& __urng)
3437 { return _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); }
3438
3439 template<typename _UniformRandomNumberGenerator>
3440 result_type
3441 operator()(_UniformRandomNumberGenerator& __urng,
3442 const param_type& __p)
3443 {
3444 typedef typename std::gamma_distribution<result_type>::param_type
3445 param_type;
3446
3447 const result_type __g = _M_gd(__urng, param_type(__p.n() / 2, 2));
3448 return _M_nd(__urng) * std::sqrt(__p.n() / __g);
3449 }
3450
3451 template<typename _ForwardIterator,
3452 typename _UniformRandomNumberGenerator>
3453 void
3454 __generate(_ForwardIterator __f, _ForwardIterator __t,
3455 _UniformRandomNumberGenerator& __urng)
3456 { this->__generate_impl(__f, __t, __urng); }
3457
3458 template<typename _ForwardIterator,
3459 typename _UniformRandomNumberGenerator>
3460 void
3461 __generate(_ForwardIterator __f, _ForwardIterator __t,
3462 _UniformRandomNumberGenerator& __urng,
3463 const param_type& __p)
3464 { this->__generate_impl(__f, __t, __urng, __p); }
3465
3466 template<typename _UniformRandomNumberGenerator>
3467 void
3468 __generate(result_type* __f, result_type* __t,
3469 _UniformRandomNumberGenerator& __urng)
3470 { this->__generate_impl(__f, __t, __urng); }
3471
3472 template<typename _UniformRandomNumberGenerator>
3473 void
3474 __generate(result_type* __f, result_type* __t,
3475 _UniformRandomNumberGenerator& __urng,
3476 const param_type& __p)
3477 { this->__generate_impl(__f, __t, __urng, __p); }
3478
3479 /**
3480 * @brief Return true if two Student t distributions have
3481 * the same parameters and the sequences that would
3482 * be generated are equal.
3483 */
3484 friend bool
3485 operator==(const student_t_distribution& __d1,
3486 const student_t_distribution& __d2)
3487 { return (__d1.param() == __d2.param()
3488 && __d1._M_nd == __d2._M_nd && __d1._M_gd == __d2._M_gd); }
3489
3490 /**
3491 * @brief Inserts a %student_t_distribution random number distribution
3492 * @p __x into the output stream @p __os.
3493 *
3494 * @param __os An output stream.
3495 * @param __x A %student_t_distribution random number distribution.
3496 *
3497 * @returns The output stream with the state of @p __x inserted or in
3498 * an error state.
3499 */
3500 template<typename _RealType1, typename _CharT, typename _Traits>
3501 friend std::basic_ostream<_CharT, _Traits>&
3502 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3503 const std::student_t_distribution<_RealType1>& __x);
3504
3505 /**
3506 * @brief Extracts a %student_t_distribution random number distribution
3507 * @p __x from the input stream @p __is.
3508 *
3509 * @param __is An input stream.
3510 * @param __x A %student_t_distribution random number
3511 * generator engine.
3512 *
3513 * @returns The input stream with @p __x extracted or in an error state.
3514 */
3515 template<typename _RealType1, typename _CharT, typename _Traits>
3516 friend std::basic_istream<_CharT, _Traits>&
3517 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3518 std::student_t_distribution<_RealType1>& __x);
3519
3520 private:
3521 template<typename _ForwardIterator,
3522 typename _UniformRandomNumberGenerator>
3523 void
3524 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3525 _UniformRandomNumberGenerator& __urng);
3526 template<typename _ForwardIterator,
3527 typename _UniformRandomNumberGenerator>
3528 void
3529 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3530 _UniformRandomNumberGenerator& __urng,
3531 const param_type& __p);
3532
3533 param_type _M_param;
3534
3535 std::normal_distribution<result_type> _M_nd;
3536 std::gamma_distribution<result_type> _M_gd;
3537 };
3538
3539 /**
3540 * @brief Return true if two Student t distributions are different.
3541 */
3542 template<typename _RealType>
3543 inline bool
3544 operator!=(const std::student_t_distribution<_RealType>& __d1,
3545 const std::student_t_distribution<_RealType>& __d2)
3546 { return !(__d1 == __d2); }
3547
3548
3549 /* @} */ // group random_distributions_normal
3550
3551 /**
3552 * @addtogroup random_distributions_bernoulli Bernoulli Distributions
3553 * @ingroup random_distributions
3554 * @{
3555 */
3556
3557 /**
3558 * @brief A Bernoulli random number distribution.
3559 *
3560 * Generates a sequence of true and false values with likelihood @f$p@f$
3561 * that true will come up and @f$(1 - p)@f$ that false will appear.
3562 */
3563 class bernoulli_distribution
3564 {
3565 public:
3566 /** The type of the range of the distribution. */
3567 typedef bool result_type;
3568 /** Parameter type. */
3569 struct param_type
3570 {
3571 typedef bernoulli_distribution distribution_type;
3572
3573 explicit
3574 param_type(double __p = 0.5)
3575 : _M_p(__p)
3576 {
3577 _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
3578 }
3579
3580 double
3581 p() const
3582 { return _M_p; }
3583
3584 friend bool
3585 operator==(const param_type& __p1, const param_type& __p2)
3586 { return __p1._M_p == __p2._M_p; }
3587
3588 private:
3589 double _M_p;
3590 };
3591
3592 public:
3593 /**
3594 * @brief Constructs a Bernoulli distribution with likelihood @p p.
3595 *
3596 * @param __p [IN] The likelihood of a true result being returned.
3597 * Must be in the interval @f$[0, 1]@f$.
3598 */
3599 explicit
3600 bernoulli_distribution(double __p = 0.5)
3601 : _M_param(__p)
3602 { }
3603
3604 explicit
3605 bernoulli_distribution(const param_type& __p)
3606 : _M_param(__p)
3607 { }
3608
3609 /**
3610 * @brief Resets the distribution state.
3611 *
3612 * Does nothing for a Bernoulli distribution.
3613 */
3614 void
3615 reset() { }
3616
3617 /**
3618 * @brief Returns the @p p parameter of the distribution.
3619 */
3620 double
3621 p() const
3622 { return _M_param.p(); }
3623
3624 /**
3625 * @brief Returns the parameter set of the distribution.
3626 */
3627 param_type
3628 param() const
3629 { return _M_param; }
3630
3631 /**
3632 * @brief Sets the parameter set of the distribution.
3633 * @param __param The new parameter set of the distribution.
3634 */
3635 void
3636 param(const param_type& __param)
3637 { _M_param = __param; }
3638
3639 /**
3640 * @brief Returns the greatest lower bound value of the distribution.
3641 */
3642 result_type
3643 min() const
3644 { return std::numeric_limits<result_type>::min(); }
3645
3646 /**
3647 * @brief Returns the least upper bound value of the distribution.
3648 */
3649 result_type
3650 max() const
3651 { return std::numeric_limits<result_type>::max(); }
3652
3653 /**
3654 * @brief Generating functions.
3655 */
3656 template<typename _UniformRandomNumberGenerator>
3657 result_type
3658 operator()(_UniformRandomNumberGenerator& __urng)
3659 { return this->operator()(__urng, this->param()); }
3660
3661 template<typename _UniformRandomNumberGenerator>
3662 result_type
3663 operator()(_UniformRandomNumberGenerator& __urng,
3664 const param_type& __p)
3665 {
3666 __detail::_Adaptor<_UniformRandomNumberGenerator, double>
3667 __aurng(__urng);
3668 if ((__aurng() - __aurng.min())
3669 < __p.p() * (__aurng.max() - __aurng.min()))
3670 return true;
3671 return false;
3672 }
3673
3674 template<typename _ForwardIterator,
3675 typename _UniformRandomNumberGenerator>
3676 void
3677 __generate(_ForwardIterator __f, _ForwardIterator __t,
3678 _UniformRandomNumberGenerator& __urng)
3679 { this->__generate(__f, __t, __urng, this->param()); }
3680
3681 template<typename _ForwardIterator,
3682 typename _UniformRandomNumberGenerator>
3683 void
3684 __generate(_ForwardIterator __f, _ForwardIterator __t,
3685 _UniformRandomNumberGenerator& __urng, const param_type& __p)
3686 { this->__generate_impl(__f, __t, __urng, __p); }
3687
3688 template<typename _UniformRandomNumberGenerator>
3689 void
3690 __generate(result_type* __f, result_type* __t,
3691 _UniformRandomNumberGenerator& __urng,
3692 const param_type& __p)
3693 { this->__generate_impl(__f, __t, __urng, __p); }
3694
3695 private:
3696 template<typename _ForwardIterator,
3697 typename _UniformRandomNumberGenerator>
3698 void
3699 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3700 _UniformRandomNumberGenerator& __urng,
3701 const param_type& __p);
3702
3703 param_type _M_param;
3704 };
3705
3706 /**
3707 * @brief Return true if two Bernoulli distributions have
3708 * the same parameters.
3709 */
3710 inline bool
3711 operator==(const std::bernoulli_distribution& __d1,
3712 const std::bernoulli_distribution& __d2)
3713 { return __d1.param() == __d2.param(); }
3714
3715 /**
3716 * @brief Return true if two Bernoulli distributions have
3717 * different parameters.
3718 */
3719 inline bool
3720 operator!=(const std::bernoulli_distribution& __d1,
3721 const std::bernoulli_distribution& __d2)
3722 { return !(__d1 == __d2); }
3723
3724 /**
3725 * @brief Inserts a %bernoulli_distribution random number distribution
3726 * @p __x into the output stream @p __os.
3727 *
3728 * @param __os An output stream.
3729 * @param __x A %bernoulli_distribution random number distribution.
3730 *
3731 * @returns The output stream with the state of @p __x inserted or in
3732 * an error state.
3733 */
3734 template<typename _CharT, typename _Traits>
3735 std::basic_ostream<_CharT, _Traits>&
3736 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3737 const std::bernoulli_distribution& __x);
3738
3739 /**
3740 * @brief Extracts a %bernoulli_distribution random number distribution
3741 * @p __x from the input stream @p __is.
3742 *
3743 * @param __is An input stream.
3744 * @param __x A %bernoulli_distribution random number generator engine.
3745 *
3746 * @returns The input stream with @p __x extracted or in an error state.
3747 */
3748 template<typename _CharT, typename _Traits>
3749 std::basic_istream<_CharT, _Traits>&
3750 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3751 std::bernoulli_distribution& __x)
3752 {
3753 double __p;
3754 __is >> __p;
3755 __x.param(bernoulli_distribution::param_type(__p));
3756 return __is;
3757 }
3758
3759
3760 /**
3761 * @brief A discrete binomial random number distribution.
3762 *
3763 * The formula for the binomial probability density function is
3764 * @f$p(i|t,p) = \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.
3766 */
3767 template<typename _IntType = int>
3768 class binomial_distribution
3769 {
3770 static_assert(std::is_integral<_IntType>::value,
3771 "template argument not an integral type");
3772
3773 public:
3774 /** The type of the range of the distribution. */
3775 typedef _IntType result_type;
3776 /** Parameter type. */
3777 struct param_type
3778 {
3779 typedef binomial_distribution<_IntType> distribution_type;
3780 friend class binomial_distribution<_IntType>;
3781
3782 explicit
3783 param_type(_IntType __t = _IntType(1), double __p = 0.5)
3784 : _M_t(__t), _M_p(__p)
3785 {
3786 _GLIBCXX_DEBUG_ASSERT((_M_t >= _IntType(0))
3787 && (_M_p >= 0.0)
3788 && (_M_p <= 1.0));
3789 _M_initialize();
3790 }
3791
3792 _IntType
3793 t() const
3794 { return _M_t; }
3795
3796 double
3797 p() const
3798 { return _M_p; }
3799
3800 friend bool
3801 operator==(const param_type& __p1, const param_type& __p2)
3802 { return __p1._M_t == __p2._M_t && __p1._M_p == __p2._M_p; }
3803
3804 private:
3805 void
3806 _M_initialize();
3807
3808 _IntType _M_t;
3809 double _M_p;
3810
3811 double _M_q;
3812 #if _GLIBCXX_USE_C99_MATH_TR1
3813 double _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
3814 _M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
3815 #endif
3816 bool _M_easy;
3817 };
3818
3819 // constructors and member function
3820 explicit
3821 binomial_distribution(_IntType __t = _IntType(1),
3822 double __p = 0.5)
3823 : _M_param(__t, __p), _M_nd()
3824 { }
3825
3826 explicit
3827 binomial_distribution(const param_type& __p)
3828 : _M_param(__p), _M_nd()
3829 { }
3830
3831 /**
3832 * @brief Resets the distribution state.
3833 */
3834 void
3835 reset()
3836 { _M_nd.reset(); }
3837
3838 /**
3839 * @brief Returns the distribution @p t parameter.
3840 */
3841 _IntType
3842 t() const
3843 { return _M_param.t(); }
3844
3845 /**
3846 * @brief Returns the distribution @p p parameter.
3847 */
3848 double
3849 p() const
3850 { return _M_param.p(); }
3851
3852 /**
3853 * @brief Returns the parameter set of the distribution.
3854 */
3855 param_type
3856 param() const
3857 { return _M_param; }
3858
3859 /**
3860 * @brief Sets the parameter set of the distribution.
3861 * @param __param The new parameter set of the distribution.
3862 */
3863 void
3864 param(const param_type& __param)
3865 { _M_param = __param; }
3866
3867 /**
3868 * @brief Returns the greatest lower bound value of the distribution.
3869 */
3870 result_type
3871 min() const
3872 { return 0; }
3873
3874 /**
3875 * @brief Returns the least upper bound value of the distribution.
3876 */
3877 result_type
3878 max() const
3879 { return _M_param.t(); }
3880
3881 /**
3882 * @brief Generating functions.
3883 */
3884 template<typename _UniformRandomNumberGenerator>
3885 result_type
3886 operator()(_UniformRandomNumberGenerator& __urng)
3887 { return this->operator()(__urng, this->param()); }
3888
3889 template<typename _UniformRandomNumberGenerator>
3890 result_type
3891 operator()(_UniformRandomNumberGenerator& __urng,
3892 const param_type& __p);
3893
3894 template<typename _ForwardIterator,
3895 typename _UniformRandomNumberGenerator>
3896 void
3897 __generate(_ForwardIterator __f, _ForwardIterator __t,
3898 _UniformRandomNumberGenerator& __urng)
3899 { this->__generate(__f, __t, __urng, this->param()); }
3900
3901 template<typename _ForwardIterator,
3902 typename _UniformRandomNumberGenerator>
3903 void
3904 __generate(_ForwardIterator __f, _ForwardIterator __t,
3905 _UniformRandomNumberGenerator& __urng,
3906 const param_type& __p)
3907 { this->__generate_impl(__f, __t, __urng, __p); }
3908
3909 template<typename _UniformRandomNumberGenerator>
3910 void
3911 __generate(result_type* __f, result_type* __t,
3912 _UniformRandomNumberGenerator& __urng,
3913 const param_type& __p)
3914 { this->__generate_impl(__f, __t, __urng, __p); }
3915
3916 /**
3917 * @brief Return true if two binomial distributions have
3918 * the same parameters and the sequences that would
3919 * be generated are equal.
3920 */
3921 friend bool
3922 operator==(const binomial_distribution& __d1,
3923 const binomial_distribution& __d2)
3924 #ifdef _GLIBCXX_USE_C99_MATH_TR1
3925 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; }
3926 #else
3927 { return __d1.param() == __d2.param(); }
3928 #endif
3929
3930 /**
3931 * @brief Inserts a %binomial_distribution random number distribution
3932 * @p __x into the output stream @p __os.
3933 *
3934 * @param __os An output stream.
3935 * @param __x A %binomial_distribution random number distribution.
3936 *
3937 * @returns The output stream with the state of @p __x inserted or in
3938 * an error state.
3939 */
3940 template<typename _IntType1,
3941 typename _CharT, typename _Traits>
3942 friend std::basic_ostream<_CharT, _Traits>&
3943 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
3944 const std::binomial_distribution<_IntType1>& __x);
3945
3946 /**
3947 * @brief Extracts a %binomial_distribution random number distribution
3948 * @p __x from the input stream @p __is.
3949 *
3950 * @param __is An input stream.
3951 * @param __x A %binomial_distribution random number generator engine.
3952 *
3953 * @returns The input stream with @p __x extracted or in an error
3954 * state.
3955 */
3956 template<typename _IntType1,
3957 typename _CharT, typename _Traits>
3958 friend std::basic_istream<_CharT, _Traits>&
3959 operator>>(std::basic_istream<_CharT, _Traits>& __is,
3960 std::binomial_distribution<_IntType1>& __x);
3961
3962 private:
3963 template<typename _ForwardIterator,
3964 typename _UniformRandomNumberGenerator>
3965 void
3966 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
3967 _UniformRandomNumberGenerator& __urng,
3968 const param_type& __p);
3969
3970 template<typename _UniformRandomNumberGenerator>
3971 result_type
3972 _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
3973
3974 param_type _M_param;
3975
3976 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
3977 std::normal_distribution<double> _M_nd;
3978 };
3979
3980 /**
3981 * @brief Return true if two binomial distributions are different.
3982 */
3983 template<typename _IntType>
3984 inline bool
3985 operator!=(const std::binomial_distribution<_IntType>& __d1,
3986 const std::binomial_distribution<_IntType>& __d2)
3987 { return !(__d1 == __d2); }
3988
3989
3990 /**
3991 * @brief A discrete geometric random number distribution.
3992 *
3993 * The formula for the geometric probability density function is
3994 * @f$p(i|p) = p(1 - p)^{i}@f$ where @f$p@f$ is the parameter of the
3995 * distribution.
3996 */
3997 template<typename _IntType = int>
3998 class geometric_distribution
3999 {
4000 static_assert(std::is_integral<_IntType>::value,
4001 "template argument not an integral type");
4002
4003 public:
4004 /** The type of the range of the distribution. */
4005 typedef _IntType result_type;
4006 /** Parameter type. */
4007 struct param_type
4008 {
4009 typedef geometric_distribution<_IntType> distribution_type;
4010 friend class geometric_distribution<_IntType>;
4011
4012 explicit
4013 param_type(double __p = 0.5)
4014 : _M_p(__p)
4015 {
4016 _GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
4017 _M_initialize();
4018 }
4019
4020 double
4021 p() const
4022 { return _M_p; }
4023
4024 friend bool
4025 operator==(const param_type& __p1, const param_type& __p2)
4026 { return __p1._M_p == __p2._M_p; }
4027
4028 private:
4029 void
4030 _M_initialize()
4031 { _M_log_1_p = std::log(1.0 - _M_p); }
4032
4033 double _M_p;
4034
4035 double _M_log_1_p;
4036 };
4037
4038 // constructors and member function
4039 explicit
4040 geometric_distribution(double __p = 0.5)
4041 : _M_param(__p)
4042 { }
4043
4044 explicit
4045 geometric_distribution(const param_type& __p)
4046 : _M_param(__p)
4047 { }
4048
4049 /**
4050 * @brief Resets the distribution state.
4051 *
4052 * Does nothing for the geometric distribution.
4053 */
4054 void
4055 reset() { }
4056
4057 /**
4058 * @brief Returns the distribution parameter @p p.
4059 */
4060 double
4061 p() const
4062 { return _M_param.p(); }
4063
4064 /**
4065 * @brief Returns the parameter set of the distribution.
4066 */
4067 param_type
4068 param() const
4069 { return _M_param; }
4070
4071 /**
4072 * @brief Sets the parameter set of the distribution.
4073 * @param __param The new parameter set of the distribution.
4074 */
4075 void
4076 param(const param_type& __param)
4077 { _M_param = __param; }
4078
4079 /**
4080 * @brief Returns the greatest lower bound value of the distribution.
4081 */
4082 result_type
4083 min() const
4084 { return 0; }
4085
4086 /**
4087 * @brief Returns the least upper bound value of the distribution.
4088 */
4089 result_type
4090 max() const
4091 { return std::numeric_limits<result_type>::max(); }
4092
4093 /**
4094 * @brief Generating functions.
4095 */
4096 template<typename _UniformRandomNumberGenerator>
4097 result_type
4098 operator()(_UniformRandomNumberGenerator& __urng)
4099 { return this->operator()(__urng, this->param()); }
4100
4101 template<typename _UniformRandomNumberGenerator>
4102 result_type
4103 operator()(_UniformRandomNumberGenerator& __urng,
4104 const param_type& __p);
4105
4106 template<typename _ForwardIterator,
4107 typename _UniformRandomNumberGenerator>
4108 void
4109 __generate(_ForwardIterator __f, _ForwardIterator __t,
4110 _UniformRandomNumberGenerator& __urng)
4111 { this->__generate(__f, __t, __urng, this->param()); }
4112
4113 template<typename _ForwardIterator,
4114 typename _UniformRandomNumberGenerator>
4115 void
4116 __generate(_ForwardIterator __f, _ForwardIterator __t,
4117 _UniformRandomNumberGenerator& __urng,
4118 const param_type& __p)
4119 { this->__generate_impl(__f, __t, __urng, __p); }
4120
4121 template<typename _UniformRandomNumberGenerator>
4122 void
4123 __generate(result_type* __f, result_type* __t,
4124 _UniformRandomNumberGenerator& __urng,
4125 const param_type& __p)
4126 { this->__generate_impl(__f, __t, __urng, __p); }
4127
4128 private:
4129 template<typename _ForwardIterator,
4130 typename _UniformRandomNumberGenerator>
4131 void
4132 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
4133 _UniformRandomNumberGenerator& __urng,
4134 const param_type& __p);
4135
4136 param_type _M_param;
4137 };
4138
4139 /**
4140 * @brief Return true if two geometric distributions have
4141 * the same parameters.
4142 */
4143 template<typename _IntType>
4144 inline bool
4145 operator==(const std::geometric_distribution<_IntType>& __d1,
4146 const std::geometric_distribution<_IntType>& __d2)
4147 { return __d1.param() == __d2.param(); }
4148
4149 /**
4150 * @brief Return true if two geometric distributions have
4151 * different parameters.
4152 */
4153 template<typename _IntType>
4154 inline bool
4155 operator!=(const std::geometric_distribution<_IntType>& __d1,
4156 const std::geometric_distribution<_IntType>& __d2)
4157 { return !(__d1 == __d2); }
4158
4159 /**
4160 * @brief Inserts a %geometric_distribution random number distribution
4161 * @p __x into the output stream @p __os.
4162 *
4163 * @param __os An output stream.
4164 * @param __x A %geometric_distribution random number distribution.
4165 *
4166 * @returns The output stream with the state of @p __x inserted or in
4167 * an error state.
4168 */
4169 template<typename _IntType,
4170 typename _CharT, typename _Traits>
4171 std::basic_ostream<_CharT, _Traits>&
4172 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4173 const std::geometric_distribution<_IntType>& __x);
4174
4175 /**
4176 * @brief Extracts a %geometric_distribution random number distribution
4177 * @p __x from the input stream @p __is.
4178 *
4179 * @param __is An input stream.
4180 * @param __x A %geometric_distribution random number generator engine.
4181 *
4182 * @returns The input stream with @p __x extracted or in an error state.
4183 */
4184 template<typename _IntType,
4185 typename _CharT, typename _Traits>
4186 std::basic_istream<_CharT, _Traits>&
4187 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4188 std::geometric_distribution<_IntType>& __x);
4189
4190
4191 /**
4192 * @brief A negative_binomial_distribution random number distribution.
4193 *
4194 * The formula for the negative binomial probability mass function is
4195 * @f$p(i) = \binom{n}{i} p^i (1 - p)^{t - i}@f$ where @f$t@f$
4196 * and @f$p@f$ are the parameters of the distribution.
4197 */
4198 template<typename _IntType = int>
4199 class negative_binomial_distribution
4200 {
4201 static_assert(std::is_integral<_IntType>::value,
4202 "template argument not an integral type");
4203
4204 public:
4205 /** The type of the range of the distribution. */
4206 typedef _IntType result_type;
4207 /** Parameter type. */
4208 struct param_type
4209 {
4210 typedef negative_binomial_distribution<_IntType> distribution_type;
4211
4212 explicit
4213 param_type(_IntType __k = 1, double __p = 0.5)
4214 : _M_k(__k), _M_p(__p)
4215 {
4216 _GLIBCXX_DEBUG_ASSERT((_M_k > 0) && (_M_p > 0.0) && (_M_p <= 1.0));
4217 }
4218
4219 _IntType
4220 k() const
4221 { return _M_k; }
4222
4223 double
4224 p() const
4225 { return _M_p; }
4226
4227 friend bool
4228 operator==(const param_type& __p1, const param_type& __p2)
4229 { return __p1._M_k == __p2._M_k && __p1._M_p == __p2._M_p; }
4230
4231 private:
4232 _IntType _M_k;
4233 double _M_p;
4234 };
4235
4236 explicit
4237 negative_binomial_distribution(_IntType __k = 1, double __p = 0.5)
4238 : _M_param(__k, __p), _M_gd(__k, (1.0 - __p) / __p)
4239 { }
4240
4241 explicit
4242 negative_binomial_distribution(const param_type& __p)
4243 : _M_param(__p), _M_gd(__p.k(), (1.0 - __p.p()) / __p.p())
4244 { }
4245
4246 /**
4247 * @brief Resets the distribution state.
4248 */
4249 void
4250 reset()
4251 { _M_gd.reset(); }
4252
4253 /**
4254 * @brief Return the @f$k@f$ parameter of the distribution.
4255 */
4256 _IntType
4257 k() const
4258 { return _M_param.k(); }
4259
4260 /**
4261 * @brief Return the @f$p@f$ parameter of the distribution.
4262 */
4263 double
4264 p() const
4265 { return _M_param.p(); }
4266
4267 /**
4268 * @brief Returns the parameter set of the distribution.
4269 */
4270 param_type
4271 param() const
4272 { return _M_param; }
4273
4274 /**
4275 * @brief Sets the parameter set of the distribution.
4276 * @param __param The new parameter set of the distribution.
4277 */
4278 void
4279 param(const param_type& __param)
4280 { _M_param = __param; }
4281
4282 /**
4283 * @brief Returns the greatest lower bound value of the distribution.
4284 */
4285 result_type
4286 min() const
4287 { return result_type(0); }
4288
4289 /**
4290 * @brief Returns the least upper bound value of the distribution.
4291 */
4292 result_type
4293 max() const
4294 { return std::numeric_limits<result_type>::max(); }
4295
4296 /**
4297 * @brief Generating functions.
4298 */
4299 template<typename _UniformRandomNumberGenerator>
4300 result_type
4301 operator()(_UniformRandomNumberGenerator& __urng);
4302
4303 template<typename _UniformRandomNumberGenerator>
4304 result_type
4305 operator()(_UniformRandomNumberGenerator& __urng,
4306 const param_type& __p);
4307
4308 template<typename _ForwardIterator,
4309 typename _UniformRandomNumberGenerator>
4310 void
4311 __generate(_ForwardIterator __f, _ForwardIterator __t,
4312 _UniformRandomNumberGenerator& __urng)
4313 { this->__generate_impl(__f, __t, __urng); }
4314
4315 template<typename _ForwardIterator,
4316 typename _UniformRandomNumberGenerator>
4317 void
4318 __generate(_ForwardIterator __f, _ForwardIterator __t,
4319 _UniformRandomNumberGenerator& __urng,
4320 const param_type& __p)
4321 { this->__generate_impl(__f, __t, __urng, __p); }
4322
4323 template<typename _UniformRandomNumberGenerator>
4324 void
4325 __generate(result_type* __f, result_type* __t,
4326 _UniformRandomNumberGenerator& __urng)
4327 { this->__generate_impl(__f, __t, __urng); }
4328
4329 template<typename _UniformRandomNumberGenerator>
4330 void
4331 __generate(result_type* __f, result_type* __t,
4332 _UniformRandomNumberGenerator& __urng,
4333 const param_type& __p)
4334 { this->__generate_impl(__f, __t, __urng, __p); }
4335
4336 /**
4337 * @brief Return true if two negative binomial distributions have
4338 * the same parameters and the sequences that would be
4339 * generated are equal.
4340 */
4341 friend bool
4342 operator==(const negative_binomial_distribution& __d1,
4343 const negative_binomial_distribution& __d2)
4344 { return __d1.param() == __d2.param() && __d1._M_gd == __d2._M_gd; }
4345
4346 /**
4347 * @brief Inserts a %negative_binomial_distribution random
4348 * number distribution @p __x into the output stream @p __os.
4349 *
4350 * @param __os An output stream.
4351 * @param __x A %negative_binomial_distribution random number
4352 * distribution.
4353 *
4354 * @returns The output stream with the state of @p __x inserted or in
4355 * an error state.
4356 */
4357 template<typename _IntType1, typename _CharT, typename _Traits>
4358 friend std::basic_ostream<_CharT, _Traits>&
4359 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4360 const std::negative_binomial_distribution<_IntType1>& __x);
4361
4362 /**
4363 * @brief Extracts a %negative_binomial_distribution random number
4364 * distribution @p __x from the input stream @p __is.
4365 *
4366 * @param __is An input stream.
4367 * @param __x A %negative_binomial_distribution random number
4368 * generator engine.
4369 *
4370 * @returns The input stream with @p __x extracted or in an error state.
4371 */
4372 template<typename _IntType1, typename _CharT, typename _Traits>
4373 friend std::basic_istream<_CharT, _Traits>&
4374 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4375 std::negative_binomial_distribution<_IntType1>& __x);
4376
4377 private:
4378 template<typename _ForwardIterator,
4379 typename _UniformRandomNumberGenerator>
4380 void
4381 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
4382 _UniformRandomNumberGenerator& __urng);
4383 template<typename _ForwardIterator,
4384 typename _UniformRandomNumberGenerator>
4385 void
4386 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
4387 _UniformRandomNumberGenerator& __urng,
4388 const param_type& __p);
4389
4390 param_type _M_param;
4391
4392 std::gamma_distribution<double> _M_gd;
4393 };
4394
4395 /**
4396 * @brief Return true if two negative binomial distributions are different.
4397 */
4398 template<typename _IntType>
4399 inline bool
4400 operator!=(const std::negative_binomial_distribution<_IntType>& __d1,
4401 const std::negative_binomial_distribution<_IntType>& __d2)
4402 { return !(__d1 == __d2); }
4403
4404
4405 /* @} */ // group random_distributions_bernoulli
4406
4407 /**
4408 * @addtogroup random_distributions_poisson Poisson Distributions
4409 * @ingroup random_distributions
4410 * @{
4411 */
4412
4413 /**
4414 * @brief A discrete Poisson random number distribution.
4415 *
4416 * The formula for the Poisson probability density function is
4417 * @f$p(i|\mu) = \frac{\mu^i}{i!} e^{-\mu}@f$ where @f$\mu@f$ is the
4418 * parameter of the distribution.
4419 */
4420 template<typename _IntType = int>
4421 class poisson_distribution
4422 {
4423 static_assert(std::is_integral<_IntType>::value,
4424 "template argument not an integral type");
4425
4426 public:
4427 /** The type of the range of the distribution. */
4428 typedef _IntType result_type;
4429 /** Parameter type. */
4430 struct param_type
4431 {
4432 typedef poisson_distribution<_IntType> distribution_type;
4433 friend class poisson_distribution<_IntType>;
4434
4435 explicit
4436 param_type(double __mean = 1.0)
4437 : _M_mean(__mean)
4438 {
4439 _GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
4440 _M_initialize();
4441 }
4442
4443 double
4444 mean() const
4445 { return _M_mean; }
4446
4447 friend bool
4448 operator==(const param_type& __p1, const param_type& __p2)
4449 { return __p1._M_mean == __p2._M_mean; }
4450
4451 private:
4452 // Hosts either log(mean) or the threshold of the simple method.
4453 void
4454 _M_initialize();
4455
4456 double _M_mean;
4457
4458 double _M_lm_thr;
4459 #if _GLIBCXX_USE_C99_MATH_TR1
4460 double _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
4461 #endif
4462 };
4463
4464 // constructors and member function
4465 explicit
4466 poisson_distribution(double __mean = 1.0)
4467 : _M_param(__mean), _M_nd()
4468 { }
4469
4470 explicit
4471 poisson_distribution(const param_type& __p)
4472 : _M_param(__p), _M_nd()
4473 { }
4474
4475 /**
4476 * @brief Resets the distribution state.
4477 */
4478 void
4479 reset()
4480 { _M_nd.reset(); }
4481
4482 /**
4483 * @brief Returns the distribution parameter @p mean.
4484 */
4485 double
4486 mean() const
4487 { return _M_param.mean(); }
4488
4489 /**
4490 * @brief Returns the parameter set of the distribution.
4491 */
4492 param_type
4493 param() const
4494 { return _M_param; }
4495
4496 /**
4497 * @brief Sets the parameter set of the distribution.
4498 * @param __param The new parameter set of the distribution.
4499 */
4500 void
4501 param(const param_type& __param)
4502 { _M_param = __param; }
4503
4504 /**
4505 * @brief Returns the greatest lower bound value of the distribution.
4506 */
4507 result_type
4508 min() const
4509 { return 0; }
4510
4511 /**
4512 * @brief Returns the least upper bound value of the distribution.
4513 */
4514 result_type
4515 max() const
4516 { return std::numeric_limits<result_type>::max(); }
4517
4518 /**
4519 * @brief Generating functions.
4520 */
4521 template<typename _UniformRandomNumberGenerator>
4522 result_type
4523 operator()(_UniformRandomNumberGenerator& __urng)
4524 { return this->operator()(__urng, this->param()); }
4525
4526 template<typename _UniformRandomNumberGenerator>
4527 result_type
4528 operator()(_UniformRandomNumberGenerator& __urng,
4529 const param_type& __p);
4530
4531 template<typename _ForwardIterator,
4532 typename _UniformRandomNumberGenerator>
4533 void
4534 __generate(_ForwardIterator __f, _ForwardIterator __t,
4535 _UniformRandomNumberGenerator& __urng)
4536 { this->__generate(__f, __t, __urng, this->param()); }
4537
4538 template<typename _ForwardIterator,
4539 typename _UniformRandomNumberGenerator>
4540 void
4541 __generate(_ForwardIterator __f, _ForwardIterator __t,
4542 _UniformRandomNumberGenerator& __urng,
4543 const param_type& __p)
4544 { this->__generate_impl(__f, __t, __urng, __p); }
4545
4546 template<typename _UniformRandomNumberGenerator>
4547 void
4548 __generate(result_type* __f, result_type* __t,
4549 _UniformRandomNumberGenerator& __urng,
4550 const param_type& __p)
4551 { this->__generate_impl(__f, __t, __urng, __p); }
4552
4553 /**
4554 * @brief Return true if two Poisson distributions have the same
4555 * parameters and the sequences that would be generated
4556 * are equal.
4557 */
4558 friend bool
4559 operator==(const poisson_distribution& __d1,
4560 const poisson_distribution& __d2)
4561 #ifdef _GLIBCXX_USE_C99_MATH_TR1
4562 { return __d1.param() == __d2.param() && __d1._M_nd == __d2._M_nd; }
4563 #else
4564 { return __d1.param() == __d2.param(); }
4565 #endif
4566
4567 /**
4568 * @brief Inserts a %poisson_distribution random number distribution
4569 * @p __x into the output stream @p __os.
4570 *
4571 * @param __os An output stream.
4572 * @param __x A %poisson_distribution random number distribution.
4573 *
4574 * @returns The output stream with the state of @p __x inserted or in
4575 * an error state.
4576 */
4577 template<typename _IntType1, typename _CharT, typename _Traits>
4578 friend std::basic_ostream<_CharT, _Traits>&
4579 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4580 const std::poisson_distribution<_IntType1>& __x);
4581
4582 /**
4583 * @brief Extracts a %poisson_distribution random number distribution
4584 * @p __x from the input stream @p __is.
4585 *
4586 * @param __is An input stream.
4587 * @param __x A %poisson_distribution random number generator engine.
4588 *
4589 * @returns The input stream with @p __x extracted or in an error
4590 * state.
4591 */
4592 template<typename _IntType1, typename _CharT, typename _Traits>
4593 friend std::basic_istream<_CharT, _Traits>&
4594 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4595 std::poisson_distribution<_IntType1>& __x);
4596
4597 private:
4598 template<typename _ForwardIterator,
4599 typename _UniformRandomNumberGenerator>
4600 void
4601 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
4602 _UniformRandomNumberGenerator& __urng,
4603 const param_type& __p);
4604
4605 param_type _M_param;
4606
4607 // NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
4608 std::normal_distribution<double> _M_nd;
4609 };
4610
4611 /**
4612 * @brief Return true if two Poisson distributions are different.
4613 */
4614 template<typename _IntType>
4615 inline bool
4616 operator!=(const std::poisson_distribution<_IntType>& __d1,
4617 const std::poisson_distribution<_IntType>& __d2)
4618 { return !(__d1 == __d2); }
4619
4620
4621 /**
4622 * @brief An exponential continuous distribution for random numbers.
4623 *
4624 * The formula for the exponential probability density function is
4625 * @f$p(x|\lambda) = \lambda e^{-\lambda x}@f$.
4626 *
4627 * <table border=1 cellpadding=10 cellspacing=0>
4628 * <caption align=top>Distribution Statistics</caption>
4629 * <tr><td>Mean</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4630 * <tr><td>Median</td><td>@f$\frac{\ln 2}{\lambda}@f$</td></tr>
4631 * <tr><td>Mode</td><td>@f$zero@f$</td></tr>
4632 * <tr><td>Range</td><td>@f$[0, \infty]@f$</td></tr>
4633 * <tr><td>Standard Deviation</td><td>@f$\frac{1}{\lambda}@f$</td></tr>
4634 * </table>
4635 */
4636 template<typename _RealType = double>
4637 class exponential_distribution
4638 {
4639 static_assert(std::is_floating_point<_RealType>::value,
4640 "template argument not a floating point type");
4641
4642 public:
4643 /** The type of the range of the distribution. */
4644 typedef _RealType result_type;
4645 /** Parameter type. */
4646 struct param_type
4647 {
4648 typedef exponential_distribution<_RealType> distribution_type;
4649
4650 explicit
4651 param_type(_RealType __lambda = _RealType(1))
4652 : _M_lambda(__lambda)
4653 {
4654 _GLIBCXX_DEBUG_ASSERT(_M_lambda > _RealType(0));
4655 }
4656
4657 _RealType
4658 lambda() const
4659 { return _M_lambda; }
4660
4661 friend bool
4662 operator==(const param_type& __p1, const param_type& __p2)
4663 { return __p1._M_lambda == __p2._M_lambda; }
4664
4665 private:
4666 _RealType _M_lambda;
4667 };
4668
4669 public:
4670 /**
4671 * @brief Constructs an exponential distribution with inverse scale
4672 * parameter @f$\lambda@f$.
4673 */
4674 explicit
4675 exponential_distribution(const result_type& __lambda = result_type(1))
4676 : _M_param(__lambda)
4677 { }
4678
4679 explicit
4680 exponential_distribution(const param_type& __p)
4681 : _M_param(__p)
4682 { }
4683
4684 /**
4685 * @brief Resets the distribution state.
4686 *
4687 * Has no effect on exponential distributions.
4688 */
4689 void
4690 reset() { }
4691
4692 /**
4693 * @brief Returns the inverse scale parameter of the distribution.
4694 */
4695 _RealType
4696 lambda() const
4697 { return _M_param.lambda(); }
4698
4699 /**
4700 * @brief Returns the parameter set of the distribution.
4701 */
4702 param_type
4703 param() const
4704 { return _M_param; }
4705
4706 /**
4707 * @brief Sets the parameter set of the distribution.
4708 * @param __param The new parameter set of the distribution.
4709 */
4710 void
4711 param(const param_type& __param)
4712 { _M_param = __param; }
4713
4714 /**
4715 * @brief Returns the greatest lower bound value of the distribution.
4716 */
4717 result_type
4718 min() const
4719 { return result_type(0); }
4720
4721 /**
4722 * @brief Returns the least upper bound value of the distribution.
4723 */
4724 result_type
4725 max() const
4726 { return std::numeric_limits<result_type>::max(); }
4727
4728 /**
4729 * @brief Generating functions.
4730 */
4731 template<typename _UniformRandomNumberGenerator>
4732 result_type
4733 operator()(_UniformRandomNumberGenerator& __urng)
4734 { return this->operator()(__urng, this->param()); }
4735
4736 template<typename _UniformRandomNumberGenerator>
4737 result_type
4738 operator()(_UniformRandomNumberGenerator& __urng,
4739 const param_type& __p)
4740 {
4741 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
4742 __aurng(__urng);
4743 return -std::log(__aurng()) / __p.lambda();
4744 }
4745
4746 template<typename _ForwardIterator,
4747 typename _UniformRandomNumberGenerator>
4748 void
4749 __generate(_ForwardIterator __f, _ForwardIterator __t,
4750 _UniformRandomNumberGenerator& __urng)
4751 { this->__generate(__f, __t, __urng, this->param()); }
4752
4753 template<typename _ForwardIterator,
4754 typename _UniformRandomNumberGenerator>
4755 void
4756 __generate(_ForwardIterator __f, _ForwardIterator __t,
4757 _UniformRandomNumberGenerator& __urng,
4758 const param_type& __p)
4759 { this->__generate_impl(__f, __t, __urng, __p); }
4760
4761 template<typename _UniformRandomNumberGenerator>
4762 void
4763 __generate(result_type* __f, result_type* __t,
4764 _UniformRandomNumberGenerator& __urng,
4765 const param_type& __p)
4766 { this->__generate_impl(__f, __t, __urng, __p); }
4767
4768 private:
4769 template<typename _ForwardIterator,
4770 typename _UniformRandomNumberGenerator>
4771 void
4772 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
4773 _UniformRandomNumberGenerator& __urng,
4774 const param_type& __p);
4775
4776 param_type _M_param;
4777 };
4778
4779 /**
4780 * @brief Return true if two exponential distributions have the same
4781 * parameters.
4782 */
4783 template<typename _RealType>
4784 inline bool
4785 operator==(const std::exponential_distribution<_RealType>& __d1,
4786 const std::exponential_distribution<_RealType>& __d2)
4787 { return __d1.param() == __d2.param(); }
4788
4789 /**
4790 * @brief Return true if two exponential distributions have different
4791 * parameters.
4792 */
4793 template<typename _RealType>
4794 inline bool
4795 operator!=(const std::exponential_distribution<_RealType>& __d1,
4796 const std::exponential_distribution<_RealType>& __d2)
4797 { return !(__d1 == __d2); }
4798
4799 /**
4800 * @brief Inserts a %exponential_distribution random number distribution
4801 * @p __x into the output stream @p __os.
4802 *
4803 * @param __os An output stream.
4804 * @param __x A %exponential_distribution random number distribution.
4805 *
4806 * @returns The output stream with the state of @p __x inserted or in
4807 * an error state.
4808 */
4809 template<typename _RealType, typename _CharT, typename _Traits>
4810 std::basic_ostream<_CharT, _Traits>&
4811 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
4812 const std::exponential_distribution<_RealType>& __x);
4813
4814 /**
4815 * @brief Extracts a %exponential_distribution random number distribution
4816 * @p __x from the input stream @p __is.
4817 *
4818 * @param __is An input stream.
4819 * @param __x A %exponential_distribution random number
4820 * generator engine.
4821 *
4822 * @returns The input stream with @p __x extracted or in an error state.
4823 */
4824 template<typename _RealType, typename _CharT, typename _Traits>
4825 std::basic_istream<_CharT, _Traits>&
4826 operator>>(std::basic_istream<_CharT, _Traits>& __is,
4827 std::exponential_distribution<_RealType>& __x);
4828
4829
4830 /**
4831 * @brief A weibull_distribution random number distribution.
4832 *
4833 * The formula for the normal probability density function is:
4834 * @f[
4835 * p(x|\alpha,\beta) = \frac{\alpha}{\beta} (\frac{x}{\beta})^{\alpha-1}
4836 * \exp{(-(\frac{x}{\beta})^\alpha)}
4837 * @f]
4838 */
4839 template<typename _RealType = double>
4840 class weibull_distribution
4841 {
4842 static_assert(std::is_floating_point<_RealType>::value,
4843 "template argument not a floating point type");
4844
4845 public:
4846 /** The type of the range of the distribution. */
4847 typedef _RealType result_type;
4848 /** Parameter type. */
4849 struct param_type
4850 {
4851 typedef weibull_distribution<_RealType> distribution_type;
4852
4853 explicit
4854 param_type(_RealType __a = _RealType(1),
4855 _RealType __b = _RealType(1))
4856 : _M_a(__a), _M_b(__b)
4857 { }
4858
4859 _RealType
4860 a() const
4861 { return _M_a; }
4862
4863 _RealType
4864 b() const
4865 { return _M_b; }
4866
4867 friend bool
4868 operator==(const param_type& __p1, const param_type& __p2)
4869 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
4870
4871 private:
4872 _RealType _M_a;
4873 _RealType _M_b;
4874 };
4875
4876 explicit
4877 weibull_distribution(_RealType __a = _RealType(1),
4878 _RealType __b = _RealType(1))
4879 : _M_param(__a, __b)
4880 { }
4881
4882 explicit
4883 weibull_distribution(const param_type& __p)
4884 : _M_param(__p)
4885 { }
4886
4887 /**
4888 * @brief Resets the distribution state.
4889 */
4890 void
4891 reset()
4892 { }
4893
4894 /**
4895 * @brief Return the @f$a@f$ parameter of the distribution.
4896 */
4897 _RealType
4898 a() const
4899 { return _M_param.a(); }
4900
4901 /**
4902 * @brief Return the @f$b@f$ parameter of the distribution.
4903 */
4904 _RealType
4905 b() const
4906 { return _M_param.b(); }
4907
4908 /**
4909 * @brief Returns the parameter set of the distribution.
4910 */
4911 param_type
4912 param() const
4913 { return _M_param; }
4914
4915 /**
4916 * @brief Sets the parameter set of the distribution.
4917 * @param __param The new parameter set of the distribution.
4918 */
4919 void
4920 param(const param_type& __param)
4921 { _M_param = __param; }
4922
4923 /**
4924 * @brief Returns the greatest lower bound value of the distribution.
4925 */
4926 result_type
4927 min() const
4928 { return result_type(0); }
4929
4930 /**
4931 * @brief Returns the least upper bound value of the distribution.
4932 */
4933 result_type
4934 max() const
4935 { return std::numeric_limits<result_type>::max(); }
4936
4937 /**
4938 * @brief Generating functions.
4939 */
4940 template<typename _UniformRandomNumberGenerator>
4941 result_type
4942 operator()(_UniformRandomNumberGenerator& __urng)
4943 { return this->operator()(__urng, this->param()); }
4944
4945 template<typename _UniformRandomNumberGenerator>
4946 result_type
4947 operator()(_UniformRandomNumberGenerator& __urng,
4948 const param_type& __p);
4949
4950 template<typename _ForwardIterator,
4951 typename _UniformRandomNumberGenerator>
4952 void
4953 __generate(_ForwardIterator __f, _ForwardIterator __t,
4954 _UniformRandomNumberGenerator& __urng)
4955 { this->__generate(__f, __t, __urng, this->param()); }
4956
4957 template<typename _ForwardIterator,
4958 typename _UniformRandomNumberGenerator>
4959 void
4960 __generate(_ForwardIterator __f, _ForwardIterator __t,
4961 _UniformRandomNumberGenerator& __urng,
4962 const param_type& __p)
4963 { this->__generate_impl(__f, __t, __urng, __p); }
4964
4965 template<typename _UniformRandomNumberGenerator>
4966 void
4967 __generate(result_type* __f, result_type* __t,
4968 _UniformRandomNumberGenerator& __urng,
4969 const param_type& __p)
4970 { this->__generate_impl(__f, __t, __urng, __p); }
4971
4972 private:
4973 template<typename _ForwardIterator,
4974 typename _UniformRandomNumberGenerator>
4975 void
4976 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
4977 _UniformRandomNumberGenerator& __urng,
4978 const param_type& __p);
4979
4980 param_type _M_param;
4981 };
4982
4983 /**
4984 * @brief Return true if two Weibull distributions have the same
4985 * parameters.
4986 */
4987 template<typename _RealType>
4988 inline bool
4989 operator==(const std::weibull_distribution<_RealType>& __d1,
4990 const std::weibull_distribution<_RealType>& __d2)
4991 { return __d1.param() == __d2.param(); }
4992
4993 /**
4994 * @brief Return true if two Weibull distributions have different
4995 * parameters.
4996 */
4997 template<typename _RealType>
4998 inline bool
4999 operator!=(const std::weibull_distribution<_RealType>& __d1,
5000 const std::weibull_distribution<_RealType>& __d2)
5001 { return !(__d1 == __d2); }
5002
5003 /**
5004 * @brief Inserts a %weibull_distribution random number distribution
5005 * @p __x into the output stream @p __os.
5006 *
5007 * @param __os An output stream.
5008 * @param __x A %weibull_distribution random number distribution.
5009 *
5010 * @returns The output stream with the state of @p __x inserted or in
5011 * an error state.
5012 */
5013 template<typename _RealType, typename _CharT, typename _Traits>
5014 std::basic_ostream<_CharT, _Traits>&
5015 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
5016 const std::weibull_distribution<_RealType>& __x);
5017
5018 /**
5019 * @brief Extracts a %weibull_distribution random number distribution
5020 * @p __x from the input stream @p __is.
5021 *
5022 * @param __is An input stream.
5023 * @param __x A %weibull_distribution random number
5024 * generator engine.
5025 *
5026 * @returns The input stream with @p __x extracted or in an error state.
5027 */
5028 template<typename _RealType, typename _CharT, typename _Traits>
5029 std::basic_istream<_CharT, _Traits>&
5030 operator>>(std::basic_istream<_CharT, _Traits>& __is,
5031 std::weibull_distribution<_RealType>& __x);
5032
5033
5034 /**
5035 * @brief A extreme_value_distribution random number distribution.
5036 *
5037 * The formula for the normal probability mass function is
5038 * @f[
5039 * p(x|a,b) = \frac{1}{b}
5040 * \exp( \frac{a-x}{b} - \exp(\frac{a-x}{b}))
5041 * @f]
5042 */
5043 template<typename _RealType = double>
5044 class extreme_value_distribution
5045 {
5046 static_assert(std::is_floating_point<_RealType>::value,
5047 "template argument not a floating point type");
5048
5049 public:
5050 /** The type of the range of the distribution. */
5051 typedef _RealType result_type;
5052 /** Parameter type. */
5053 struct param_type
5054 {
5055 typedef extreme_value_distribution<_RealType> distribution_type;
5056
5057 explicit
5058 param_type(_RealType __a = _RealType(0),
5059 _RealType __b = _RealType(1))
5060 : _M_a(__a), _M_b(__b)
5061 { }
5062
5063 _RealType
5064 a() const
5065 { return _M_a; }
5066
5067 _RealType
5068 b() const
5069 { return _M_b; }
5070
5071 friend bool
5072 operator==(const param_type& __p1, const param_type& __p2)
5073 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
5074
5075 private:
5076 _RealType _M_a;
5077 _RealType _M_b;
5078 };
5079
5080 explicit
5081 extreme_value_distribution(_RealType __a = _RealType(0),
5082 _RealType __b = _RealType(1))
5083 : _M_param(__a, __b)
5084 { }
5085
5086 explicit
5087 extreme_value_distribution(const param_type& __p)
5088 : _M_param(__p)
5089 { }
5090
5091 /**
5092 * @brief Resets the distribution state.
5093 */
5094 void
5095 reset()
5096 { }
5097
5098 /**
5099 * @brief Return the @f$a@f$ parameter of the distribution.
5100 */
5101 _RealType
5102 a() const
5103 { return _M_param.a(); }
5104
5105 /**
5106 * @brief Return the @f$b@f$ parameter of the distribution.
5107 */
5108 _RealType
5109 b() const
5110 { return _M_param.b(); }
5111
5112 /**
5113 * @brief Returns the parameter set of the distribution.
5114 */
5115 param_type
5116 param() const
5117 { return _M_param; }
5118
5119 /**
5120 * @brief Sets the parameter set of the distribution.
5121 * @param __param The new parameter set of the distribution.
5122 */
5123 void
5124 param(const param_type& __param)
5125 { _M_param = __param; }
5126
5127 /**
5128 * @brief Returns the greatest lower bound value of the distribution.
5129 */
5130 result_type
5131 min() const
5132 { return std::numeric_limits<result_type>::min(); }
5133
5134 /**
5135 * @brief Returns the least upper bound value of the distribution.
5136 */
5137 result_type
5138 max() const
5139 { return std::numeric_limits<result_type>::max(); }
5140
5141 /**
5142 * @brief Generating functions.
5143 */
5144 template<typename _UniformRandomNumberGenerator>
5145 result_type
5146 operator()(_UniformRandomNumberGenerator& __urng)
5147 { return this->operator()(__urng, this->param()); }
5148
5149 template<typename _UniformRandomNumberGenerator>
5150 result_type
5151 operator()(_UniformRandomNumberGenerator& __urng,
5152 const param_type& __p);
5153
5154 template<typename _ForwardIterator,
5155 typename _UniformRandomNumberGenerator>
5156 void
5157 __generate(_ForwardIterator __f, _ForwardIterator __t,
5158 _UniformRandomNumberGenerator& __urng)
5159 { this->__generate(__f, __t, __urng, this->param()); }
5160
5161 template<typename _ForwardIterator,
5162 typename _UniformRandomNumberGenerator>
5163 void
5164 __generate(_ForwardIterator __f, _ForwardIterator __t,
5165 _UniformRandomNumberGenerator& __urng,
5166 const param_type& __p)
5167 { this->__generate_impl(__f, __t, __urng, __p); }
5168
5169 template<typename _UniformRandomNumberGenerator>
5170 void
5171 __generate(result_type* __f, result_type* __t,
5172 _UniformRandomNumberGenerator& __urng,
5173 const param_type& __p)
5174 { this->__generate_impl(__f, __t, __urng, __p); }
5175
5176 private:
5177 template<typename _ForwardIterator,
5178 typename _UniformRandomNumberGenerator>
5179 void
5180 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
5181 _UniformRandomNumberGenerator& __urng,
5182 const param_type& __p);
5183
5184 param_type _M_param;
5185 };
5186
5187 /**
5188 * @brief Return true if two extreme value distributions have the same
5189 * parameters.
5190 */
5191 template<typename _RealType>
5192 inline bool
5193 operator==(const std::extreme_value_distribution<_RealType>& __d1,
5194 const std::extreme_value_distribution<_RealType>& __d2)
5195 { return __d1.param() == __d2.param(); }
5196
5197 /**
5198 * @brief Return true if two extreme value distributions have different
5199 * parameters.
5200 */
5201 template<typename _RealType>
5202 inline bool
5203 operator!=(const std::extreme_value_distribution<_RealType>& __d1,
5204 const std::extreme_value_distribution<_RealType>& __d2)
5205 { return !(__d1 == __d2); }
5206
5207 /**
5208 * @brief Inserts a %extreme_value_distribution random number distribution
5209 * @p __x into the output stream @p __os.
5210 *
5211 * @param __os An output stream.
5212 * @param __x A %extreme_value_distribution random number distribution.
5213 *
5214 * @returns The output stream with the state of @p __x inserted or in
5215 * an error state.
5216 */
5217 template<typename _RealType, typename _CharT, typename _Traits>
5218 std::basic_ostream<_CharT, _Traits>&
5219 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
5220 const std::extreme_value_distribution<_RealType>& __x);
5221
5222 /**
5223 * @brief Extracts a %extreme_value_distribution random number
5224 * distribution @p __x from the input stream @p __is.
5225 *
5226 * @param __is An input stream.
5227 * @param __x A %extreme_value_distribution random number
5228 * generator engine.
5229 *
5230 * @returns The input stream with @p __x extracted or in an error state.
5231 */
5232 template<typename _RealType, typename _CharT, typename _Traits>
5233 std::basic_istream<_CharT, _Traits>&
5234 operator>>(std::basic_istream<_CharT, _Traits>& __is,
5235 std::extreme_value_distribution<_RealType>& __x);
5236
5237
5238 /**
5239 * @brief A discrete_distribution random number distribution.
5240 *
5241 * The formula for the discrete probability mass function is
5242 *
5243 */
5244 template<typename _IntType = int>
5245 class discrete_distribution
5246 {
5247 static_assert(std::is_integral<_IntType>::value,
5248 "template argument not an integral type");
5249
5250 public:
5251 /** The type of the range of the distribution. */
5252 typedef _IntType result_type;
5253 /** Parameter type. */
5254 struct param_type
5255 {
5256 typedef discrete_distribution<_IntType> distribution_type;
5257 friend class discrete_distribution<_IntType>;
5258
5259 param_type()
5260 : _M_prob(), _M_cp()
5261 { }
5262
5263 template<typename _InputIterator>
5264 param_type(_InputIterator __wbegin,
5265 _InputIterator __wend)
5266 : _M_prob(__wbegin, __wend), _M_cp()
5267 { _M_initialize(); }
5268
5269 param_type(initializer_list<double> __wil)
5270 : _M_prob(__wil.begin(), __wil.end()), _M_cp()
5271 { _M_initialize(); }
5272
5273 template<typename _Func>
5274 param_type(size_t __nw, double __xmin, double __xmax,
5275 _Func __fw);
5276
5277 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5278 param_type(const param_type&) = default;
5279 param_type& operator=(const param_type&) = default;
5280
5281 std::vector<double>
5282 probabilities() const
5283 { return _M_prob.empty() ? std::vector<double>(1, 1.0) : _M_prob; }
5284
5285 friend bool
5286 operator==(const param_type& __p1, const param_type& __p2)
5287 { return __p1._M_prob == __p2._M_prob; }
5288
5289 private:
5290 void
5291 _M_initialize();
5292
5293 std::vector<double> _M_prob;
5294 std::vector<double> _M_cp;
5295 };
5296
5297 discrete_distribution()
5298 : _M_param()
5299 { }
5300
5301 template<typename _InputIterator>
5302 discrete_distribution(_InputIterator __wbegin,
5303 _InputIterator __wend)
5304 : _M_param(__wbegin, __wend)
5305 { }
5306
5307 discrete_distribution(initializer_list<double> __wl)
5308 : _M_param(__wl)
5309 { }
5310
5311 template<typename _Func>
5312 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5313 _Func __fw)
5314 : _M_param(__nw, __xmin, __xmax, __fw)
5315 { }
5316
5317 explicit
5318 discrete_distribution(const param_type& __p)
5319 : _M_param(__p)
5320 { }
5321
5322 /**
5323 * @brief Resets the distribution state.
5324 */
5325 void
5326 reset()
5327 { }
5328
5329 /**
5330 * @brief Returns the probabilities of the distribution.
5331 */
5332 std::vector<double>
5333 probabilities() const
5334 {
5335 return _M_param._M_prob.empty()
5336 ? std::vector<double>(1, 1.0) : _M_param._M_prob;
5337 }
5338
5339 /**
5340 * @brief Returns the parameter set of the distribution.
5341 */
5342 param_type
5343 param() const
5344 { return _M_param; }
5345
5346 /**
5347 * @brief Sets the parameter set of the distribution.
5348 * @param __param The new parameter set of the distribution.
5349 */
5350 void
5351 param(const param_type& __param)
5352 { _M_param = __param; }
5353
5354 /**
5355 * @brief Returns the greatest lower bound value of the distribution.
5356 */
5357 result_type
5358 min() const
5359 { return result_type(0); }
5360
5361 /**
5362 * @brief Returns the least upper bound value of the distribution.
5363 */
5364 result_type
5365 max() const
5366 {
5367 return _M_param._M_prob.empty()
5368 ? result_type(0) : result_type(_M_param._M_prob.size() - 1);
5369 }
5370
5371 /**
5372 * @brief Generating functions.
5373 */
5374 template<typename _UniformRandomNumberGenerator>
5375 result_type
5376 operator()(_UniformRandomNumberGenerator& __urng)
5377 { return this->operator()(__urng, this->param()); }
5378
5379 template<typename _UniformRandomNumberGenerator>
5380 result_type
5381 operator()(_UniformRandomNumberGenerator& __urng,
5382 const param_type& __p);
5383
5384 template<typename _ForwardIterator,
5385 typename _UniformRandomNumberGenerator>
5386 void
5387 __generate(_ForwardIterator __f, _ForwardIterator __t,
5388 _UniformRandomNumberGenerator& __urng)
5389 { this->__generate(__f, __t, __urng, this->param()); }
5390
5391 template<typename _ForwardIterator,
5392 typename _UniformRandomNumberGenerator>
5393 void
5394 __generate(_ForwardIterator __f, _ForwardIterator __t,
5395 _UniformRandomNumberGenerator& __urng,
5396 const param_type& __p)
5397 { this->__generate_impl(__f, __t, __urng, __p); }
5398
5399 template<typename _UniformRandomNumberGenerator>
5400 void
5401 __generate(result_type* __f, result_type* __t,
5402 _UniformRandomNumberGenerator& __urng,
5403 const param_type& __p)
5404 { this->__generate_impl(__f, __t, __urng, __p); }
5405
5406 /**
5407 * @brief Inserts a %discrete_distribution random number distribution
5408 * @p __x into the output stream @p __os.
5409 *
5410 * @param __os An output stream.
5411 * @param __x A %discrete_distribution random number distribution.
5412 *
5413 * @returns The output stream with the state of @p __x inserted or in
5414 * an error state.
5415 */
5416 template<typename _IntType1, typename _CharT, typename _Traits>
5417 friend std::basic_ostream<_CharT, _Traits>&
5418 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
5419 const std::discrete_distribution<_IntType1>& __x);
5420
5421 /**
5422 * @brief Extracts a %discrete_distribution random number distribution
5423 * @p __x from the input stream @p __is.
5424 *
5425 * @param __is An input stream.
5426 * @param __x A %discrete_distribution random number
5427 * generator engine.
5428 *
5429 * @returns The input stream with @p __x extracted or in an error
5430 * state.
5431 */
5432 template<typename _IntType1, typename _CharT, typename _Traits>
5433 friend std::basic_istream<_CharT, _Traits>&
5434 operator>>(std::basic_istream<_CharT, _Traits>& __is,
5435 std::discrete_distribution<_IntType1>& __x);
5436
5437 private:
5438 template<typename _ForwardIterator,
5439 typename _UniformRandomNumberGenerator>
5440 void
5441 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
5442 _UniformRandomNumberGenerator& __urng,
5443 const param_type& __p);
5444
5445 param_type _M_param;
5446 };
5447
5448 /**
5449 * @brief Return true if two discrete distributions have the same
5450 * parameters.
5451 */
5452 template<typename _IntType>
5453 inline bool
5454 operator==(const std::discrete_distribution<_IntType>& __d1,
5455 const std::discrete_distribution<_IntType>& __d2)
5456 { return __d1.param() == __d2.param(); }
5457
5458 /**
5459 * @brief Return true if two discrete distributions have different
5460 * parameters.
5461 */
5462 template<typename _IntType>
5463 inline bool
5464 operator!=(const std::discrete_distribution<_IntType>& __d1,
5465 const std::discrete_distribution<_IntType>& __d2)
5466 { return !(__d1 == __d2); }
5467
5468
5469 /**
5470 * @brief A piecewise_constant_distribution random number distribution.
5471 *
5472 * The formula for the piecewise constant probability mass function is
5473 *
5474 */
5475 template<typename _RealType = double>
5476 class piecewise_constant_distribution
5477 {
5478 static_assert(std::is_floating_point<_RealType>::value,
5479 "template argument not a floating point type");
5480
5481 public:
5482 /** The type of the range of the distribution. */
5483 typedef _RealType result_type;
5484 /** Parameter type. */
5485 struct param_type
5486 {
5487 typedef piecewise_constant_distribution<_RealType> distribution_type;
5488 friend class piecewise_constant_distribution<_RealType>;
5489
5490 param_type()
5491 : _M_int(), _M_den(), _M_cp()
5492 { }
5493
5494 template<typename _InputIteratorB, typename _InputIteratorW>
5495 param_type(_InputIteratorB __bfirst,
5496 _InputIteratorB __bend,
5497 _InputIteratorW __wbegin);
5498
5499 template<typename _Func>
5500 param_type(initializer_list<_RealType> __bi, _Func __fw);
5501
5502 template<typename _Func>
5503 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
5504 _Func __fw);
5505
5506 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5507 param_type(const param_type&) = default;
5508 param_type& operator=(const param_type&) = default;
5509
5510 std::vector<_RealType>
5511 intervals() const
5512 {
5513 if (_M_int.empty())
5514 {
5515 std::vector<_RealType> __tmp(2);
5516 __tmp[1] = _RealType(1);
5517 return __tmp;
5518 }
5519 else
5520 return _M_int;
5521 }
5522
5523 std::vector<double>
5524 densities() const
5525 { return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; }
5526
5527 friend bool
5528 operator==(const param_type& __p1, const param_type& __p2)
5529 { return __p1._M_int == __p2._M_int && __p1._M_den == __p2._M_den; }
5530
5531 private:
5532 void
5533 _M_initialize();
5534
5535 std::vector<_RealType> _M_int;
5536 std::vector<double> _M_den;
5537 std::vector<double> _M_cp;
5538 };
5539
5540 explicit
5541 piecewise_constant_distribution()
5542 : _M_param()
5543 { }
5544
5545 template<typename _InputIteratorB, typename _InputIteratorW>
5546 piecewise_constant_distribution(_InputIteratorB __bfirst,
5547 _InputIteratorB __bend,
5548 _InputIteratorW __wbegin)
5549 : _M_param(__bfirst, __bend, __wbegin)
5550 { }
5551
5552 template<typename _Func>
5553 piecewise_constant_distribution(initializer_list<_RealType> __bl,
5554 _Func __fw)
5555 : _M_param(__bl, __fw)
5556 { }
5557
5558 template<typename _Func>
5559 piecewise_constant_distribution(size_t __nw,
5560 _RealType __xmin, _RealType __xmax,
5561 _Func __fw)
5562 : _M_param(__nw, __xmin, __xmax, __fw)
5563 { }
5564
5565 explicit
5566 piecewise_constant_distribution(const param_type& __p)
5567 : _M_param(__p)
5568 { }
5569
5570 /**
5571 * @brief Resets the distribution state.
5572 */
5573 void
5574 reset()
5575 { }
5576
5577 /**
5578 * @brief Returns a vector of the intervals.
5579 */
5580 std::vector<_RealType>
5581 intervals() const
5582 {
5583 if (_M_param._M_int.empty())
5584 {
5585 std::vector<_RealType> __tmp(2);
5586 __tmp[1] = _RealType(1);
5587 return __tmp;
5588 }
5589 else
5590 return _M_param._M_int;
5591 }
5592
5593 /**
5594 * @brief Returns a vector of the probability densities.
5595 */
5596 std::vector<double>
5597 densities() const
5598 {
5599 return _M_param._M_den.empty()
5600 ? std::vector<double>(1, 1.0) : _M_param._M_den;
5601 }
5602
5603 /**
5604 * @brief Returns the parameter set of the distribution.
5605 */
5606 param_type
5607 param() const
5608 { return _M_param; }
5609
5610 /**
5611 * @brief Sets the parameter set of the distribution.
5612 * @param __param The new parameter set of the distribution.
5613 */
5614 void
5615 param(const param_type& __param)
5616 { _M_param = __param; }
5617
5618 /**
5619 * @brief Returns the greatest lower bound value of the distribution.
5620 */
5621 result_type
5622 min() const
5623 {
5624 return _M_param._M_int.empty()
5625 ? result_type(0) : _M_param._M_int.front();
5626 }
5627
5628 /**
5629 * @brief Returns the least upper bound value of the distribution.
5630 */
5631 result_type
5632 max() const
5633 {
5634 return _M_param._M_int.empty()
5635 ? result_type(1) : _M_param._M_int.back();
5636 }
5637
5638 /**
5639 * @brief Generating functions.
5640 */
5641 template<typename _UniformRandomNumberGenerator>
5642 result_type
5643 operator()(_UniformRandomNumberGenerator& __urng)
5644 { return this->operator()(__urng, this->param()); }
5645
5646 template<typename _UniformRandomNumberGenerator>
5647 result_type
5648 operator()(_UniformRandomNumberGenerator& __urng,
5649 const param_type& __p);
5650
5651 template<typename _ForwardIterator,
5652 typename _UniformRandomNumberGenerator>
5653 void
5654 __generate(_ForwardIterator __f, _ForwardIterator __t,
5655 _UniformRandomNumberGenerator& __urng)
5656 { this->__generate(__f, __t, __urng, this->param()); }
5657
5658 template<typename _ForwardIterator,
5659 typename _UniformRandomNumberGenerator>
5660 void
5661 __generate(_ForwardIterator __f, _ForwardIterator __t,
5662 _UniformRandomNumberGenerator& __urng,
5663 const param_type& __p)
5664 { this->__generate_impl(__f, __t, __urng, __p); }
5665
5666 template<typename _UniformRandomNumberGenerator>
5667 void
5668 __generate(result_type* __f, result_type* __t,
5669 _UniformRandomNumberGenerator& __urng,
5670 const param_type& __p)
5671 { this->__generate_impl(__f, __t, __urng, __p); }
5672
5673 /**
5674 * @brief Inserts a %piecewise_constan_distribution random
5675 * number distribution @p __x into the output stream @p __os.
5676 *
5677 * @param __os An output stream.
5678 * @param __x A %piecewise_constan_distribution random number
5679 * distribution.
5680 *
5681 * @returns The output stream with the state of @p __x inserted or in
5682 * an error state.
5683 */
5684 template<typename _RealType1, typename _CharT, typename _Traits>
5685 friend std::basic_ostream<_CharT, _Traits>&
5686 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
5687 const std::piecewise_constant_distribution<_RealType1>& __x);
5688
5689 /**
5690 * @brief Extracts a %piecewise_constan_distribution random
5691 * number distribution @p __x from the input stream @p __is.
5692 *
5693 * @param __is An input stream.
5694 * @param __x A %piecewise_constan_distribution random number
5695 * generator engine.
5696 *
5697 * @returns The input stream with @p __x extracted or in an error
5698 * state.
5699 */
5700 template<typename _RealType1, typename _CharT, typename _Traits>
5701 friend std::basic_istream<_CharT, _Traits>&
5702 operator>>(std::basic_istream<_CharT, _Traits>& __is,
5703 std::piecewise_constant_distribution<_RealType1>& __x);
5704
5705 private:
5706 template<typename _ForwardIterator,
5707 typename _UniformRandomNumberGenerator>
5708 void
5709 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
5710 _UniformRandomNumberGenerator& __urng,
5711 const param_type& __p);
5712
5713 param_type _M_param;
5714 };
5715
5716 /**
5717 * @brief Return true if two piecewise constant distributions have the
5718 * same parameters.
5719 */
5720 template<typename _RealType>
5721 inline bool
5722 operator==(const std::piecewise_constant_distribution<_RealType>& __d1,
5723 const std::piecewise_constant_distribution<_RealType>& __d2)
5724 { return __d1.param() == __d2.param(); }
5725
5726 /**
5727 * @brief Return true if two piecewise constant distributions have
5728 * different parameters.
5729 */
5730 template<typename _RealType>
5731 inline bool
5732 operator!=(const std::piecewise_constant_distribution<_RealType>& __d1,
5733 const std::piecewise_constant_distribution<_RealType>& __d2)
5734 { return !(__d1 == __d2); }
5735
5736
5737 /**
5738 * @brief A piecewise_linear_distribution random number distribution.
5739 *
5740 * The formula for the piecewise linear probability mass function is
5741 *
5742 */
5743 template<typename _RealType = double>
5744 class piecewise_linear_distribution
5745 {
5746 static_assert(std::is_floating_point<_RealType>::value,
5747 "template argument not a floating point type");
5748
5749 public:
5750 /** The type of the range of the distribution. */
5751 typedef _RealType result_type;
5752 /** Parameter type. */
5753 struct param_type
5754 {
5755 typedef piecewise_linear_distribution<_RealType> distribution_type;
5756 friend class piecewise_linear_distribution<_RealType>;
5757
5758 param_type()
5759 : _M_int(), _M_den(), _M_cp(), _M_m()
5760 { }
5761
5762 template<typename _InputIteratorB, typename _InputIteratorW>
5763 param_type(_InputIteratorB __bfirst,
5764 _InputIteratorB __bend,
5765 _InputIteratorW __wbegin);
5766
5767 template<typename _Func>
5768 param_type(initializer_list<_RealType> __bl, _Func __fw);
5769
5770 template<typename _Func>
5771 param_type(size_t __nw, _RealType __xmin, _RealType __xmax,
5772 _Func __fw);
5773
5774 // See: http://cpp-next.com/archive/2010/10/implicit-move-must-go/
5775 param_type(const param_type&) = default;
5776 param_type& operator=(const param_type&) = default;
5777
5778 std::vector<_RealType>
5779 intervals() const
5780 {
5781 if (_M_int.empty())
5782 {
5783 std::vector<_RealType> __tmp(2);
5784 __tmp[1] = _RealType(1);
5785 return __tmp;
5786 }
5787 else
5788 return _M_int;
5789 }
5790
5791 std::vector<double>
5792 densities() const
5793 { return _M_den.empty() ? std::vector<double>(2, 1.0) : _M_den; }
5794
5795 friend bool
5796 operator==(const param_type& __p1, const param_type& __p2)
5797 { return (__p1._M_int == __p2._M_int
5798 && __p1._M_den == __p2._M_den); }
5799
5800 private:
5801 void
5802 _M_initialize();
5803
5804 std::vector<_RealType> _M_int;
5805 std::vector<double> _M_den;
5806 std::vector<double> _M_cp;
5807 std::vector<double> _M_m;
5808 };
5809
5810 explicit
5811 piecewise_linear_distribution()
5812 : _M_param()
5813 { }
5814
5815 template<typename _InputIteratorB, typename _InputIteratorW>
5816 piecewise_linear_distribution(_InputIteratorB __bfirst,
5817 _InputIteratorB __bend,
5818 _InputIteratorW __wbegin)
5819 : _M_param(__bfirst, __bend, __wbegin)
5820 { }
5821
5822 template<typename _Func>
5823 piecewise_linear_distribution(initializer_list<_RealType> __bl,
5824 _Func __fw)
5825 : _M_param(__bl, __fw)
5826 { }
5827
5828 template<typename _Func>
5829 piecewise_linear_distribution(size_t __nw,
5830 _RealType __xmin, _RealType __xmax,
5831 _Func __fw)
5832 : _M_param(__nw, __xmin, __xmax, __fw)
5833 { }
5834
5835 explicit
5836 piecewise_linear_distribution(const param_type& __p)
5837 : _M_param(__p)
5838 { }
5839
5840 /**
5841 * Resets the distribution state.
5842 */
5843 void
5844 reset()
5845 { }
5846
5847 /**
5848 * @brief Return the intervals of the distribution.
5849 */
5850 std::vector<_RealType>
5851 intervals() const
5852 {
5853 if (_M_param._M_int.empty())
5854 {
5855 std::vector<_RealType> __tmp(2);
5856 __tmp[1] = _RealType(1);
5857 return __tmp;
5858 }
5859 else
5860 return _M_param._M_int;
5861 }
5862
5863 /**
5864 * @brief Return a vector of the probability densities of the
5865 * distribution.
5866 */
5867 std::vector<double>
5868 densities() const
5869 {
5870 return _M_param._M_den.empty()
5871 ? std::vector<double>(2, 1.0) : _M_param._M_den;
5872 }
5873
5874 /**
5875 * @brief Returns the parameter set of the distribution.
5876 */
5877 param_type
5878 param() const
5879 { return _M_param; }
5880
5881 /**
5882 * @brief Sets the parameter set of the distribution.
5883 * @param __param The new parameter set of the distribution.
5884 */
5885 void
5886 param(const param_type& __param)
5887 { _M_param = __param; }
5888
5889 /**
5890 * @brief Returns the greatest lower bound value of the distribution.
5891 */
5892 result_type
5893 min() const
5894 {
5895 return _M_param._M_int.empty()
5896 ? result_type(0) : _M_param._M_int.front();
5897 }
5898
5899 /**
5900 * @brief Returns the least upper bound value of the distribution.
5901 */
5902 result_type
5903 max() const
5904 {
5905 return _M_param._M_int.empty()
5906 ? result_type(1) : _M_param._M_int.back();
5907 }
5908
5909 /**
5910 * @brief Generating functions.
5911 */
5912 template<typename _UniformRandomNumberGenerator>
5913 result_type
5914 operator()(_UniformRandomNumberGenerator& __urng)
5915 { return this->operator()(__urng, this->param()); }
5916
5917 template<typename _UniformRandomNumberGenerator>
5918 result_type
5919 operator()(_UniformRandomNumberGenerator& __urng,
5920 const param_type& __p);
5921
5922 template<typename _ForwardIterator,
5923 typename _UniformRandomNumberGenerator>
5924 void
5925 __generate(_ForwardIterator __f, _ForwardIterator __t,
5926 _UniformRandomNumberGenerator& __urng)
5927 { this->__generate(__f, __t, __urng, this->param()); }
5928
5929 template<typename _ForwardIterator,
5930 typename _UniformRandomNumberGenerator>
5931 void
5932 __generate(_ForwardIterator __f, _ForwardIterator __t,
5933 _UniformRandomNumberGenerator& __urng,
5934 const param_type& __p)
5935 { this->__generate_impl(__f, __t, __urng, __p); }
5936
5937 template<typename _UniformRandomNumberGenerator>
5938 void
5939 __generate(result_type* __f, result_type* __t,
5940 _UniformRandomNumberGenerator& __urng,
5941 const param_type& __p)
5942 { this->__generate_impl(__f, __t, __urng, __p); }
5943
5944 /**
5945 * @brief Inserts a %piecewise_linear_distribution random number
5946 * distribution @p __x into the output stream @p __os.
5947 *
5948 * @param __os An output stream.
5949 * @param __x A %piecewise_linear_distribution random number
5950 * distribution.
5951 *
5952 * @returns The output stream with the state of @p __x inserted or in
5953 * an error state.
5954 */
5955 template<typename _RealType1, typename _CharT, typename _Traits>
5956 friend std::basic_ostream<_CharT, _Traits>&
5957 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
5958 const std::piecewise_linear_distribution<_RealType1>& __x);
5959
5960 /**
5961 * @brief Extracts a %piecewise_linear_distribution random number
5962 * distribution @p __x from the input stream @p __is.
5963 *
5964 * @param __is An input stream.
5965 * @param __x A %piecewise_linear_distribution random number
5966 * generator engine.
5967 *
5968 * @returns The input stream with @p __x extracted or in an error
5969 * state.
5970 */
5971 template<typename _RealType1, typename _CharT, typename _Traits>
5972 friend std::basic_istream<_CharT, _Traits>&
5973 operator>>(std::basic_istream<_CharT, _Traits>& __is,
5974 std::piecewise_linear_distribution<_RealType1>& __x);
5975
5976 private:
5977 template<typename _ForwardIterator,
5978 typename _UniformRandomNumberGenerator>
5979 void
5980 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
5981 _UniformRandomNumberGenerator& __urng,
5982 const param_type& __p);
5983
5984 param_type _M_param;
5985 };
5986
5987 /**
5988 * @brief Return true if two piecewise linear distributions have the
5989 * same parameters.
5990 */
5991 template<typename _RealType>
5992 inline bool
5993 operator==(const std::piecewise_linear_distribution<_RealType>& __d1,
5994 const std::piecewise_linear_distribution<_RealType>& __d2)
5995 { return __d1.param() == __d2.param(); }
5996
5997 /**
5998 * @brief Return true if two piecewise linear distributions have
5999 * different parameters.
6000 */
6001 template<typename _RealType>
6002 inline bool
6003 operator!=(const std::piecewise_linear_distribution<_RealType>& __d1,
6004 const std::piecewise_linear_distribution<_RealType>& __d2)
6005 { return !(__d1 == __d2); }
6006
6007
6008 /* @} */ // group random_distributions_poisson
6009
6010 /* @} */ // group random_distributions
6011
6012 /**
6013 * @addtogroup random_utilities Random Number Utilities
6014 * @ingroup random
6015 * @{
6016 */
6017
6018 /**
6019 * @brief The seed_seq class generates sequences of seeds for random
6020 * number generators.
6021 */
6022 class seed_seq
6023 {
6024
6025 public:
6026 /** The type of the seed vales. */
6027 typedef uint_least32_t result_type;
6028
6029 /** Default constructor. */
6030 seed_seq()
6031 : _M_v()
6032 { }
6033
6034 template<typename _IntType>
6035 seed_seq(std::initializer_list<_IntType> il);
6036
6037 template<typename _InputIterator>
6038 seed_seq(_InputIterator __begin, _InputIterator __end);
6039
6040 // generating functions
6041 template<typename _RandomAccessIterator>
6042 void
6043 generate(_RandomAccessIterator __begin, _RandomAccessIterator __end);
6044
6045 // property functions
6046 size_t size() const
6047 { return _M_v.size(); }
6048
6049 template<typename OutputIterator>
6050 void
6051 param(OutputIterator __dest) const
6052 { std::copy(_M_v.begin(), _M_v.end(), __dest); }
6053
6054 private:
6055 ///
6056 std::vector<result_type> _M_v;
6057 };
6058
6059 /* @} */ // group random_utilities
6060
6061 /* @} */ // group random
6062
6063 _GLIBCXX_END_NAMESPACE_VERSION
6064 } // namespace std
6065
6066 #endif