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