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