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