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