libstdc++
random.h
Go to the documentation of this file.
1// random number generation -*- C++ -*-
2
3// Copyright (C) 2009-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/**
26 * @file bits/random.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{random}
29 */
30
31#ifndef _RANDOM_H
32#define _RANDOM_H 1
33
34#include <vector>
36
37namespace std _GLIBCXX_VISIBILITY(default)
38{
39_GLIBCXX_BEGIN_NAMESPACE_VERSION
40
41 // [26.4] Random number generation
42
43 /**
44 * @defgroup random Random Number Generation
45 * @ingroup numerics
46 *
47 * A facility for generating random numbers on selected distributions.
48 * @{
49 */
50
51 // std::uniform_random_bit_generator is defined in <bits/uniform_int_dist.h>
52
53 /**
54 * @brief A function template for converting the output of a (integral)
55 * uniform random number generator to a floatng point result in the range
56 * [0-1).
57 */
58 template<typename _RealType, size_t __bits,
59 typename _UniformRandomNumberGenerator>
60 _RealType
61 generate_canonical(_UniformRandomNumberGenerator& __g);
62
63 /// @cond undocumented
64 // Implementation-space details.
65 namespace __detail
66 {
67#pragma GCC diagnostic push
68#pragma GCC diagnostic ignored "-Wc++17-extensions"
69
70 template<typename _UIntType, size_t __w,
71 bool = __w < static_cast<size_t>
73 struct _Shift
74 { static constexpr _UIntType __value = 0; };
75
76 template<typename _UIntType, size_t __w>
77 struct _Shift<_UIntType, __w, true>
78 { static constexpr _UIntType __value = _UIntType(1) << __w; };
79
80 template<int __s,
81 int __which = ((__s <= __CHAR_BIT__ * sizeof (int))
82 + (__s <= __CHAR_BIT__ * sizeof (long))
83 + (__s <= __CHAR_BIT__ * sizeof (long long))
84 /* assume long long no bigger than __int128 */
85 + (__s <= 128))>
86 struct _Select_uint_least_t
87 {
88 static_assert(__which < 0, /* needs to be dependent */
89 "sorry, would be too much trouble for a slow result");
90 };
91
92 template<int __s>
93 struct _Select_uint_least_t<__s, 4>
94 { using type = unsigned int; };
95
96 template<int __s>
97 struct _Select_uint_least_t<__s, 3>
98 { using type = unsigned long; };
99
100 template<int __s>
101 struct _Select_uint_least_t<__s, 2>
102 { using type = unsigned long long; };
103
104#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
105 template<int __s>
106 struct _Select_uint_least_t<__s, 1>
107 { __extension__ using type = unsigned __int128; };
108#elif __has_builtin(__builtin_add_overflow) \
109 && __has_builtin(__builtin_sub_overflow) \
110 && defined __UINT64_TYPE__
111 template<int __s>
112 struct _Select_uint_least_t<__s, 1>
113 {
114 // This is NOT a general-purpose 128-bit integer type.
115 // It only supports (type(a) * x + c) % m as needed by __mod.
116 struct type
117 {
118 explicit
119 type(uint64_t __a) noexcept : _M_lo(__a), _M_hi(0) { }
120
121 // pre: __l._M_hi == 0
122 friend type
123 operator*(type __l, uint64_t __x) noexcept
124 {
125 // Split 64-bit values __l._M_lo and __x into high and low 32-bit
126 // limbs and multiply those individually.
127 // l * x = (l0 + l1) * (x0 + x1) = l0x0 + l0x1 + l1x0 + l1x1
128
129 constexpr uint64_t __mask = 0xffffffff;
130 uint64_t __ll[2] = { __l._M_lo >> 32, __l._M_lo & __mask };
131 uint64_t __xx[2] = { __x >> 32, __x & __mask };
132 uint64_t __l0x0 = __ll[0] * __xx[0];
133 uint64_t __l0x1 = __ll[0] * __xx[1];
134 uint64_t __l1x0 = __ll[1] * __xx[0];
135 uint64_t __l1x1 = __ll[1] * __xx[1];
136 // These bits are the low half of __l._M_hi
137 // and the high half of __l._M_lo.
138 uint64_t __mid
139 = (__l0x1 & __mask) + (__l1x0 & __mask) + (__l1x1 >> 32);
140 __l._M_hi = __l0x0 + (__l0x1 >> 32) + (__l1x0 >> 32) + (__mid >> 32);
141 __l._M_lo = (__mid << 32) + (__l1x1 & __mask);
142 return __l;
143 }
144
145 friend type
146 operator+(type __l, uint64_t __c) noexcept
147 {
148 __l._M_hi += __builtin_add_overflow(__l._M_lo, __c, &__l._M_lo);
149 return __l;
150 }
151
152 friend type
153 operator%(type __l, uint64_t __m) noexcept
154 {
155 if (__builtin_expect(__l._M_hi == 0, 0))
156 {
157 __l._M_lo %= __m;
158 return __l;
159 }
160
161 int __shift = __builtin_clzll(__m) + 64
162 - __builtin_clzll(__l._M_hi);
163 type __x(0);
164 if (__shift >= 64)
165 {
166 __x._M_hi = __m << (__shift - 64);
167 __x._M_lo = 0;
168 }
169 else
170 {
171 __x._M_hi = __m >> (64 - __shift);
172 __x._M_lo = __m << __shift;
173 }
174
175 while (__l._M_hi != 0 || __l._M_lo >= __m)
176 {
177 if (__x <= __l)
178 {
179 __l._M_hi -= __x._M_hi;
180 __l._M_hi -= __builtin_sub_overflow(__l._M_lo, __x._M_lo,
181 &__l._M_lo);
182 }
183 __x._M_lo = (__x._M_lo >> 1) | (__x._M_hi << 63);
184 __x._M_hi >>= 1;
185 }
186 return __l;
187 }
188
189 // pre: __l._M_hi == 0
190 explicit operator uint64_t() const noexcept
191 { return _M_lo; }
192
193 friend bool operator<(const type& __l, const type& __r) noexcept
194 {
195 if (__l._M_hi < __r._M_hi)
196 return true;
197 else if (__l._M_hi == __r._M_hi)
198 return __l._M_lo < __r._M_lo;
199 else
200 return false;
201 }
202
203 friend bool operator<=(const type& __l, const type& __r) noexcept
204 { return !(__r < __l); }
205
206 uint64_t _M_lo;
207 uint64_t _M_hi;
208 };
209 };
210#endif
211
212 // Assume a != 0, a < m, c < m, x < m.
213 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c,
214 bool __big_enough = (!(__m & (__m - 1))
215 || (_Tp(-1) - __c) / __a >= __m - 1),
216 bool __schrage_ok = __m % __a < __m / __a>
217 struct _Mod
218 {
219 static _Tp
220 __calc(_Tp __x)
221 {
222 using _Tp2
223 = typename _Select_uint_least_t<std::__lg(__a)
224 + std::__lg(__m) + 2>::type;
225 return static_cast<_Tp>((_Tp2(__a) * __x + __c) % __m);
226 }
227 };
228
229 // Schrage.
230 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
231 struct _Mod<_Tp, __m, __a, __c, false, true>
232 {
233 static _Tp
234 __calc(_Tp __x);
235 };
236
237 // Special cases:
238 // - for m == 2^n or m == 0, unsigned integer overflow is safe.
239 // - a * (m - 1) + c fits in _Tp, there is no overflow.
240 template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool __s>
241 struct _Mod<_Tp, __m, __a, __c, true, __s>
242 {
243 static _Tp
244 __calc(_Tp __x)
245 {
246 _Tp __res = __a * __x + __c;
247 if (__m)
248 __res %= __m;
249 return __res;
250 }
251 };
252
253 template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
254 inline _Tp
255 __mod(_Tp __x)
256 {
257 if constexpr (__a == 0)
258 return __c;
259 else // N.B. _Mod must not be instantiated with a == 0
260 return _Mod<_Tp, __m, __a, __c>::__calc(__x);
261 }
262
263 /*
264 * An adaptor class for converting the output of any Generator into
265 * the input for a specific Distribution.
266 */
267 template<typename _Engine, typename _DInputType>
268 struct _Adaptor
269 {
270 static_assert(std::is_floating_point<_DInputType>::value,
271 "template argument must be a floating point type");
272
273 public:
274 _Adaptor(_Engine& __g)
275 : _M_g(__g) { }
276
277 _DInputType
278 min() const
279 { return _DInputType(0); }
280
281 _DInputType
282 max() const
283 { return _DInputType(1); }
284
285 /*
286 * Converts a value generated by the adapted random number generator
287 * into a value in the input domain for the dependent random number
288 * distribution.
289 */
290 _DInputType
291 operator()()
292 {
293 return std::generate_canonical<_DInputType,
295 _Engine>(_M_g);
296 }
297
298 private:
299 _Engine& _M_g;
300 };
301
302 // Detect whether a template argument _Sseq is a valid seed sequence for
303 // a random number engine _Engine with result type _Res.
304 // Used to constrain _Engine::_Engine(_Sseq&) and _Engine::seed(_Sseq&)
305 // as required by [rand.eng.general].
306
307 template<typename _Sseq>
308 using __seed_seq_generate_t = decltype(
311
312 template<typename _Sseq, typename _Engine, typename _Res,
313 typename _GenerateCheck = __seed_seq_generate_t<_Sseq>>
314 using _If_seed_seq_for = _Require<
315 __not_<is_same<__remove_cvref_t<_Sseq>, _Engine>>,
316 is_unsigned<typename _Sseq::result_type>,
317 __not_<is_convertible<_Sseq, _Res>>
318 >;
319
320#pragma GCC diagnostic pop
321 } // namespace __detail
322 /// @endcond
323
324 /**
325 * @addtogroup random_generators Random Number Generators
326 * @ingroup random
327 *
328 * These classes define objects which provide random or pseudorandom
329 * numbers, either from a discrete or a continuous interval. The
330 * random number generator supplied as a part of this library are
331 * all uniform random number generators which provide a sequence of
332 * random number uniformly distributed over their range.
333 *
334 * A number generator is a function object with an operator() that
335 * takes zero arguments and returns a number.
336 *
337 * A compliant random number generator must satisfy the following
338 * requirements. <table border=1 cellpadding=10 cellspacing=0>
339 * <caption align=top>Random Number Generator Requirements</caption>
340 * <tr><td>To be documented.</td></tr> </table>
341 *
342 * @{
343 */
344
345 /**
346 * @brief A model of a linear congruential random number generator.
347 *
348 * A random number generator that produces pseudorandom numbers via
349 * linear function:
350 * @f[
351 * x_{i+1}\leftarrow(ax_{i} + c) \bmod m
352 * @f]
353 *
354 * The template parameter @p _UIntType must be an unsigned integral type
355 * large enough to store values up to (__m-1). If the template parameter
356 * @p __m is 0, the modulus @p __m used is
357 * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
358 * parameters @p __a and @p __c must be less than @p __m.
359 *
360 * The size of the state is @f$1@f$.
361 *
362 * @headerfile random
363 * @since C++11
364 */
365 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
367 {
369 "result_type must be an unsigned integral type");
370 static_assert(__m == 0u || (__a < __m && __c < __m),
371 "template argument substituting __m out of bounds");
372
373 template<typename _Sseq>
374 using _If_seed_seq
375 = __detail::_If_seed_seq_for<_Sseq, linear_congruential_engine,
376 _UIntType>;
377
378 public:
379 /** The type of the generated random value. */
380 typedef _UIntType result_type;
381
382 /** The multiplier. */
383 static constexpr result_type multiplier = __a;
384 /** An increment. */
385 static constexpr result_type increment = __c;
386 /** The modulus. */
387 static constexpr result_type modulus = __m;
388 static constexpr result_type default_seed = 1u;
389
390 /**
391 * @brief Constructs a %linear_congruential_engine random number
392 * generator engine with seed 1.
395 { }
396
397 /**
398 * @brief Constructs a %linear_congruential_engine random number
399 * generator engine with seed @p __s. The default seed value
400 * is 1.
401 *
402 * @param __s The initial seed value.
403 */
406 { seed(__s); }
407
408 /**
409 * @brief Constructs a %linear_congruential_engine random number
410 * generator engine seeded from the seed sequence @p __q.
411 *
412 * @param __q the seed sequence.
413 */
414 template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
415 explicit
417 { seed(__q); }
418
419 /**
420 * @brief Reseeds the %linear_congruential_engine random number generator
421 * engine sequence to the seed @p __s.
422 *
423 * @param __s The new seed.
424 */
425 void
426 seed(result_type __s = default_seed);
427
428 /**
429 * @brief Reseeds the %linear_congruential_engine random number generator
430 * engine
431 * sequence using values from the seed sequence @p __q.
432 *
433 * @param __q the seed sequence.
434 */
435 template<typename _Sseq>
436 _If_seed_seq<_Sseq>
437 seed(_Sseq& __q);
438
439 /**
440 * @brief Gets the smallest possible value in the output range.
441 *
442 * The minimum depends on the @p __c parameter: if it is zero, the
443 * minimum generated must be > 0, otherwise 0 is allowed.
444 */
445 static constexpr result_type
446 min()
447 { return __c == 0u ? 1u : 0u; }
448
449 /**
450 * @brief Gets the largest possible value in the output range.
451 */
452 static constexpr result_type
453 max()
454 { return __m - 1u; }
455
456 /**
457 * @brief Discard a sequence of random numbers.
458 */
459 void
460 discard(unsigned long long __z)
461 {
462 for (; __z != 0ULL; --__z)
463 (*this)();
464 }
465
466 /**
467 * @brief Gets the next random number in the sequence.
468 */
470 operator()()
471 {
472 _M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
473 return _M_x;
474 }
475
476 /**
477 * @brief Compares two linear congruential random number generator
478 * objects of the same type for equality.
479 *
480 * @param __lhs A linear congruential random number generator object.
481 * @param __rhs Another linear congruential random number generator
482 * object.
483 *
484 * @returns true if the infinite sequences of generated values
485 * would be equal, false otherwise.
486 */
487 friend bool
489 const linear_congruential_engine& __rhs)
490 { return __lhs._M_x == __rhs._M_x; }
491
492 /**
493 * @brief Writes the textual representation of the state x(i) of x to
494 * @p __os.
495 *
496 * @param __os The output stream.
497 * @param __lcr A % linear_congruential_engine random number generator.
498 * @returns __os.
499 */
500 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
501 _UIntType1 __m1, typename _CharT, typename _Traits>
504 const std::linear_congruential_engine<_UIntType1,
505 __a1, __c1, __m1>& __lcr);
506
507 /**
508 * @brief Sets the state of the engine by reading its textual
509 * representation from @p __is.
510 *
511 * The textual representation must have been previously written using
512 * an output stream whose imbued locale and whose type's template
513 * specialization arguments _CharT and _Traits were the same as those
514 * of @p __is.
515 *
516 * @param __is The input stream.
517 * @param __lcr A % linear_congruential_engine random number generator.
518 * @returns __is.
519 */
520 template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
521 _UIntType1 __m1, typename _CharT, typename _Traits>
524 std::linear_congruential_engine<_UIntType1, __a1,
525 __c1, __m1>& __lcr);
526
527 private:
528 _UIntType _M_x;
529 };
530
531#if __cpp_impl_three_way_comparison < 201907L
532 /**
533 * @brief Compares two linear congruential random number generator
534 * objects of the same type for inequality.
535 *
536 * @param __lhs A linear congruential random number generator object.
537 * @param __rhs Another linear congruential random number generator
538 * object.
539 *
540 * @returns true if the infinite sequences of generated values
541 * would be different, false otherwise.
542 */
543 template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
544 inline bool
545 operator!=(const std::linear_congruential_engine<_UIntType, __a,
546 __c, __m>& __lhs,
547 const std::linear_congruential_engine<_UIntType, __a,
548 __c, __m>& __rhs)
549 { return !(__lhs == __rhs); }
550#endif
551
552 /**
553 * A generalized feedback shift register discrete random number generator.
554 *
555 * This algorithm avoids multiplication and division and is designed to be
556 * friendly to a pipelined architecture. If the parameters are chosen
557 * correctly, this generator will produce numbers with a very long period and
558 * fairly good apparent entropy, although still not cryptographically strong.
559 *
560 * The best way to use this generator is with the predefined mt19937 class.
561 *
562 * This algorithm was originally invented by Makoto Matsumoto and
563 * Takuji Nishimura.
564 *
565 * @tparam __w Word size, the number of bits in each element of
566 * the state vector.
567 * @tparam __n The degree of recursion.
568 * @tparam __m The period parameter.
569 * @tparam __r The separation point bit index.
570 * @tparam __a The last row of the twist matrix.
571 * @tparam __u The first right-shift tempering matrix parameter.
572 * @tparam __d The first right-shift tempering matrix mask.
573 * @tparam __s The first left-shift tempering matrix parameter.
574 * @tparam __b The first left-shift tempering matrix mask.
575 * @tparam __t The second left-shift tempering matrix parameter.
576 * @tparam __c The second left-shift tempering matrix mask.
577 * @tparam __l The second right-shift tempering matrix parameter.
578 * @tparam __f Initialization multiplier.
579 *
580 * @headerfile random
581 * @since C++11
582 */
583 template<typename _UIntType, size_t __w,
584 size_t __n, size_t __m, size_t __r,
585 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
586 _UIntType __b, size_t __t,
587 _UIntType __c, size_t __l, _UIntType __f>
588 class mersenne_twister_engine
589 {
591 "result_type must be an unsigned integral type");
592 static_assert(1u <= __m && __m <= __n,
593 "template argument substituting __m out of bounds");
594 static_assert(__r <= __w, "template argument substituting "
595 "__r out of bound");
596 static_assert(__u <= __w, "template argument substituting "
597 "__u out of bound");
598 static_assert(__s <= __w, "template argument substituting "
599 "__s out of bound");
600 static_assert(__t <= __w, "template argument substituting "
601 "__t out of bound");
602 static_assert(__l <= __w, "template argument substituting "
603 "__l out of bound");
604 static_assert(__w <= std::numeric_limits<_UIntType>::digits,
605 "template argument substituting __w out of bound");
606 static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
607 "template argument substituting __a out of bound");
608 static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
609 "template argument substituting __b out of bound");
610 static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
611 "template argument substituting __c out of bound");
612 static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
613 "template argument substituting __d out of bound");
614 static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
615 "template argument substituting __f out of bound");
616
617 template<typename _Sseq>
618 using _If_seed_seq
619 = __detail::_If_seed_seq_for<_Sseq, mersenne_twister_engine,
620 _UIntType>;
621
622 public:
623 /** The type of the generated random value. */
624 typedef _UIntType result_type;
625
626 // parameter values
627 static constexpr size_t word_size = __w;
628 static constexpr size_t state_size = __n;
629 static constexpr size_t shift_size = __m;
630 static constexpr size_t mask_bits = __r;
631 static constexpr result_type xor_mask = __a;
632 static constexpr size_t tempering_u = __u;
633 static constexpr result_type tempering_d = __d;
634 static constexpr size_t tempering_s = __s;
635 static constexpr result_type tempering_b = __b;
636 static constexpr size_t tempering_t = __t;
637 static constexpr result_type tempering_c = __c;
638 static constexpr size_t tempering_l = __l;
639 static constexpr result_type initialization_multiplier = __f;
640 static constexpr result_type default_seed = 5489u;
641
642 // constructors and member functions
643
644 mersenne_twister_engine() : mersenne_twister_engine(default_seed) { }
645
646 explicit
647 mersenne_twister_engine(result_type __sd)
648 { seed(__sd); }
649
650 /**
651 * @brief Constructs a %mersenne_twister_engine random number generator
652 * engine seeded from the seed sequence @p __q.
653 *
654 * @param __q the seed sequence.
655 */
656 template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
657 explicit
658 mersenne_twister_engine(_Sseq& __q)
659 { seed(__q); }
660
661 void
662 seed(result_type __sd = default_seed);
663
664 template<typename _Sseq>
665 _If_seed_seq<_Sseq>
666 seed(_Sseq& __q);
667
668 /**
669 * @brief Gets the smallest possible value in the output range.
670 */
671 static constexpr result_type
672 min()
673 { return 0; }
674
675 /**
676 * @brief Gets the largest possible value in the output range.
677 */
678 static constexpr result_type
679 max()
680 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
681
682 /**
683 * @brief Discard a sequence of random numbers.
684 */
685 void
686 discard(unsigned long long __z);
687
689 operator()();
690
691 /**
692 * @brief Compares two % mersenne_twister_engine random number generator
693 * objects of the same type for equality.
694 *
695 * @param __lhs A % mersenne_twister_engine random number generator
696 * object.
697 * @param __rhs Another % mersenne_twister_engine random number
698 * generator object.
699 *
700 * @returns true if the infinite sequences of generated values
701 * would be equal, false otherwise.
702 */
703 friend bool
704 operator==(const mersenne_twister_engine& __lhs,
705 const mersenne_twister_engine& __rhs)
706 { return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x)
707 && __lhs._M_p == __rhs._M_p); }
708
709 /**
710 * @brief Inserts the current state of a % mersenne_twister_engine
711 * random number generator engine @p __x into the output stream
712 * @p __os.
713 *
714 * @param __os An output stream.
715 * @param __x A % mersenne_twister_engine random number generator
716 * engine.
717 *
718 * @returns The output stream with the state of @p __x inserted or in
719 * an error state.
720 */
721 template<typename _UIntType1,
722 size_t __w1, size_t __n1,
723 size_t __m1, size_t __r1,
724 _UIntType1 __a1, size_t __u1,
725 _UIntType1 __d1, size_t __s1,
726 _UIntType1 __b1, size_t __t1,
727 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
728 typename _CharT, typename _Traits>
731 const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
732 __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
733 __l1, __f1>& __x);
734
735 /**
736 * @brief Extracts the current state of a % mersenne_twister_engine
737 * random number generator engine @p __x from the input stream
738 * @p __is.
739 *
740 * @param __is An input stream.
741 * @param __x A % mersenne_twister_engine random number generator
742 * engine.
743 *
744 * @returns The input stream with the state of @p __x extracted or in
745 * an error state.
746 */
747 template<typename _UIntType1,
748 size_t __w1, size_t __n1,
749 size_t __m1, size_t __r1,
750 _UIntType1 __a1, size_t __u1,
751 _UIntType1 __d1, size_t __s1,
752 _UIntType1 __b1, size_t __t1,
753 _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
754 typename _CharT, typename _Traits>
757 std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
758 __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
759 __l1, __f1>& __x);
760
761 private:
762 void _M_gen_rand();
763
764 _UIntType _M_x[state_size];
765 size_t _M_p;
766 };
767
768#if __cpp_impl_three_way_comparison < 201907L
769 /**
770 * @brief Compares two % mersenne_twister_engine random number generator
771 * objects of the same type for inequality.
772 *
773 * @param __lhs A % mersenne_twister_engine random number generator
774 * object.
775 * @param __rhs Another % mersenne_twister_engine random number
776 * generator object.
777 *
778 * @returns true if the infinite sequences of generated values
779 * would be different, false otherwise.
780 */
781 template<typename _UIntType, size_t __w,
782 size_t __n, size_t __m, size_t __r,
783 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
784 _UIntType __b, size_t __t,
785 _UIntType __c, size_t __l, _UIntType __f>
786 inline bool
787 operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
788 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs,
789 const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
790 __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs)
791 { return !(__lhs == __rhs); }
792#endif
793
794 /**
795 * @brief The Marsaglia-Zaman generator.
796 *
797 * This is a model of a Generalized Fibonacci discrete random number
798 * generator, sometimes referred to as the SWC generator.
799 *
800 * A discrete random number generator that produces pseudorandom
801 * numbers using:
802 * @f[
803 * x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
804 * @f]
805 *
806 * The size of the state is @f$r@f$
807 * and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
808 *
809 * @headerfile random
810 * @since C++11
811 */
812 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
813 class subtract_with_carry_engine
814 {
816 "result_type must be an unsigned integral type");
817 static_assert(0u < __s && __s < __r,
818 "0 < s < r");
819 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
820 "template argument substituting __w out of bounds");
821
822 template<typename _Sseq>
823 using _If_seed_seq
824 = __detail::_If_seed_seq_for<_Sseq, subtract_with_carry_engine,
825 _UIntType>;
826
827 public:
828 /** The type of the generated random value. */
829 typedef _UIntType result_type;
830
831 // parameter values
832 static constexpr size_t word_size = __w;
833 static constexpr size_t short_lag = __s;
834 static constexpr size_t long_lag = __r;
835 static constexpr uint_least32_t default_seed = 19780503u;
836
837 subtract_with_carry_engine() : subtract_with_carry_engine(0u)
838 { }
839
840 /**
841 * @brief Constructs an explicitly seeded %subtract_with_carry_engine
842 * random number generator.
843 */
844 explicit
845 subtract_with_carry_engine(result_type __sd)
846 { seed(__sd); }
847
848 /**
849 * @brief Constructs a %subtract_with_carry_engine random number engine
850 * seeded from the seed sequence @p __q.
851 *
852 * @param __q the seed sequence.
853 */
854 template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
855 explicit
856 subtract_with_carry_engine(_Sseq& __q)
857 { seed(__q); }
858
859 /**
860 * @brief Seeds the initial state @f$x_0@f$ of the random number
861 * generator.
862 *
863 * N1688[4.19] modifies this as follows. If @p __value == 0,
864 * sets value to 19780503. In any case, with a linear
865 * congruential generator lcg(i) having parameters @f$ m_{lcg} =
866 * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
867 * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
868 * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
869 * set carry to 1, otherwise sets carry to 0.
870 */
871 void
872 seed(result_type __sd = 0u);
873
874 /**
875 * @brief Seeds the initial state @f$x_0@f$ of the
876 * % subtract_with_carry_engine random number generator.
877 */
878 template<typename _Sseq>
879 _If_seed_seq<_Sseq>
880 seed(_Sseq& __q);
881
882 /**
883 * @brief Gets the inclusive minimum value of the range of random
884 * integers returned by this generator.
885 */
886 static constexpr result_type
887 min()
888 { return 0; }
889
890 /**
891 * @brief Gets the inclusive maximum value of the range of random
892 * integers returned by this generator.
893 */
894 static constexpr result_type
895 max()
896 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
897
898 /**
899 * @brief Discard a sequence of random numbers.
900 */
901 void
902 discard(unsigned long long __z)
903 {
904 for (; __z != 0ULL; --__z)
905 (*this)();
906 }
907
908 /**
909 * @brief Gets the next random number in the sequence.
910 */
912 operator()();
913
914 /**
915 * @brief Compares two % subtract_with_carry_engine random number
916 * generator objects of the same type for equality.
917 *
918 * @param __lhs A % subtract_with_carry_engine random number generator
919 * object.
920 * @param __rhs Another % subtract_with_carry_engine random number
921 * generator object.
922 *
923 * @returns true if the infinite sequences of generated values
924 * would be equal, false otherwise.
925 */
926 friend bool
927 operator==(const subtract_with_carry_engine& __lhs,
928 const subtract_with_carry_engine& __rhs)
929 { return (std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x)
930 && __lhs._M_carry == __rhs._M_carry
931 && __lhs._M_p == __rhs._M_p); }
932
933 /**
934 * @brief Inserts the current state of a % subtract_with_carry_engine
935 * random number generator engine @p __x into the output stream
936 * @p __os.
937 *
938 * @param __os An output stream.
939 * @param __x A % subtract_with_carry_engine random number generator
940 * engine.
941 *
942 * @returns The output stream with the state of @p __x inserted or in
943 * an error state.
944 */
945 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
946 typename _CharT, typename _Traits>
949 const std::subtract_with_carry_engine<_UIntType1, __w1,
950 __s1, __r1>& __x);
951
952 /**
953 * @brief Extracts the current state of a % subtract_with_carry_engine
954 * random number generator engine @p __x from the input stream
955 * @p __is.
956 *
957 * @param __is An input stream.
958 * @param __x A % subtract_with_carry_engine random number generator
959 * engine.
960 *
961 * @returns The input stream with the state of @p __x extracted or in
962 * an error state.
963 */
964 template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
965 typename _CharT, typename _Traits>
968 std::subtract_with_carry_engine<_UIntType1, __w1,
969 __s1, __r1>& __x);
970
971 private:
972 /// The state of the generator. This is a ring buffer.
973 _UIntType _M_x[long_lag];
974 _UIntType _M_carry; ///< The carry
975 size_t _M_p; ///< Current index of x(i - r).
976 };
977
978#if __cpp_impl_three_way_comparison < 201907L
979 /**
980 * @brief Compares two % subtract_with_carry_engine random number
981 * generator objects of the same type for inequality.
982 *
983 * @param __lhs A % subtract_with_carry_engine random number generator
984 * object.
985 * @param __rhs Another % subtract_with_carry_engine random number
986 * generator object.
987 *
988 * @returns true if the infinite sequences of generated values
989 * would be different, false otherwise.
990 */
991 template<typename _UIntType, size_t __w, size_t __s, size_t __r>
992 inline bool
993 operator!=(const std::subtract_with_carry_engine<_UIntType, __w,
994 __s, __r>& __lhs,
995 const std::subtract_with_carry_engine<_UIntType, __w,
996 __s, __r>& __rhs)
997 { return !(__lhs == __rhs); }
998#endif
999
1000 /**
1001 * Produces random numbers from some base engine by discarding blocks of
1002 * data.
1003 *
1004 * @pre @f$ 0 \leq r \leq p @f$
1005 *
1006 * @headerfile random
1007 * @since C++11
1008 */
1009 template<typename _RandomNumberEngine, size_t __p, size_t __r>
1011 {
1012 static_assert(1 <= __r && __r <= __p,
1013 "template argument substituting __r out of bounds");
1014
1015 public:
1016 /** The type of the generated random value. */
1017 typedef typename _RandomNumberEngine::result_type result_type;
1018
1019 template<typename _Sseq>
1020 using _If_seed_seq
1021 = __detail::_If_seed_seq_for<_Sseq, discard_block_engine,
1022 result_type>;
1023
1024 // parameter values
1025 static constexpr size_t block_size = __p;
1026 static constexpr size_t used_block = __r;
1027
1028 /**
1029 * @brief Constructs a default %discard_block_engine engine.
1030 *
1031 * The underlying engine is default constructed as well.
1034 : _M_b(), _M_n(0) { }
1035
1036 /**
1037 * @brief Copy constructs a %discard_block_engine engine.
1038 *
1039 * Copies an existing base class random number generator.
1040 * @param __rng An existing (base class) engine object.
1041 */
1042 explicit
1043 discard_block_engine(const _RandomNumberEngine& __rng)
1044 : _M_b(__rng), _M_n(0) { }
1045
1046 /**
1047 * @brief Move constructs a %discard_block_engine engine.
1048 *
1049 * Copies an existing base class random number generator.
1050 * @param __rng An existing (base class) engine object.
1051 */
1052 explicit
1053 discard_block_engine(_RandomNumberEngine&& __rng)
1054 : _M_b(std::move(__rng)), _M_n(0) { }
1055
1056 /**
1057 * @brief Seed constructs a %discard_block_engine engine.
1058 *
1059 * Constructs the underlying generator engine seeded with @p __s.
1060 * @param __s A seed value for the base class engine.
1061 */
1064 : _M_b(__s), _M_n(0) { }
1065
1066 /**
1067 * @brief Generator construct a %discard_block_engine engine.
1068 *
1069 * @param __q A seed sequence.
1070 */
1071 template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
1072 explicit
1073 discard_block_engine(_Sseq& __q)
1074 : _M_b(__q), _M_n(0)
1075 { }
1076
1077 /**
1078 * @brief Reseeds the %discard_block_engine object with the default
1079 * seed for the underlying base class generator engine.
1080 */
1081 void
1082 seed()
1083 {
1084 _M_b.seed();
1085 _M_n = 0;
1086 }
1087
1088 /**
1089 * @brief Reseeds the %discard_block_engine object with the default
1090 * seed for the underlying base class generator engine.
1091 */
1092 void
1093 seed(result_type __s)
1094 {
1095 _M_b.seed(__s);
1096 _M_n = 0;
1097 }
1098
1099 /**
1100 * @brief Reseeds the %discard_block_engine object with the given seed
1101 * sequence.
1102 * @param __q A seed generator function.
1103 */
1104 template<typename _Sseq>
1105 _If_seed_seq<_Sseq>
1106 seed(_Sseq& __q)
1107 {
1108 _M_b.seed(__q);
1109 _M_n = 0;
1110 }
1111
1112 /**
1113 * @brief Gets a const reference to the underlying generator engine
1114 * object.
1115 */
1116 const _RandomNumberEngine&
1117 base() const noexcept
1118 { return _M_b; }
1119
1120 /**
1121 * @brief Gets the minimum value in the generated random number range.
1122 */
1123 static constexpr result_type
1124 min()
1125 { return _RandomNumberEngine::min(); }
1126
1127 /**
1128 * @brief Gets the maximum value in the generated random number range.
1129 */
1130 static constexpr result_type
1131 max()
1132 { return _RandomNumberEngine::max(); }
1133
1134 /**
1135 * @brief Discard a sequence of random numbers.
1136 */
1137 void
1138 discard(unsigned long long __z)
1139 {
1140 for (; __z != 0ULL; --__z)
1141 (*this)();
1142 }
1143
1144 /**
1145 * @brief Gets the next value in the generated random number sequence.
1146 */
1148 operator()();
1149
1150 /**
1151 * @brief Compares two %discard_block_engine random number generator
1152 * objects of the same type for equality.
1153 *
1154 * @param __lhs A %discard_block_engine random number generator object.
1155 * @param __rhs Another %discard_block_engine random number generator
1156 * object.
1157 *
1158 * @returns true if the infinite sequences of generated values
1159 * would be equal, false otherwise.
1160 */
1161 friend bool
1162 operator==(const discard_block_engine& __lhs,
1163 const discard_block_engine& __rhs)
1164 { return __lhs._M_b == __rhs._M_b && __lhs._M_n == __rhs._M_n; }
1165
1166 /**
1167 * @brief Inserts the current state of a %discard_block_engine random
1168 * number generator engine @p __x into the output stream
1169 * @p __os.
1170 *
1171 * @param __os An output stream.
1172 * @param __x A %discard_block_engine random number generator engine.
1173 *
1174 * @returns The output stream with the state of @p __x inserted or in
1175 * an error state.
1176 */
1177 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
1178 typename _CharT, typename _Traits>
1181 const std::discard_block_engine<_RandomNumberEngine1,
1182 __p1, __r1>& __x);
1183
1184 /**
1185 * @brief Extracts the current state of a % subtract_with_carry_engine
1186 * random number generator engine @p __x from the input stream
1187 * @p __is.
1188 *
1189 * @param __is An input stream.
1190 * @param __x A %discard_block_engine random number generator engine.
1191 *
1192 * @returns The input stream with the state of @p __x extracted or in
1193 * an error state.
1194 */
1195 template<typename _RandomNumberEngine1, size_t __p1, size_t __r1,
1196 typename _CharT, typename _Traits>
1199 std::discard_block_engine<_RandomNumberEngine1,
1200 __p1, __r1>& __x);
1201
1202 private:
1203 _RandomNumberEngine _M_b;
1204 size_t _M_n;
1205 };
1206
1207#if __cpp_impl_three_way_comparison < 201907L
1208 /**
1209 * @brief Compares two %discard_block_engine random number generator
1210 * objects of the same type for inequality.
1211 *
1212 * @param __lhs A %discard_block_engine random number generator object.
1213 * @param __rhs Another %discard_block_engine random number generator
1214 * object.
1215 *
1216 * @returns true if the infinite sequences of generated values
1217 * would be different, false otherwise.
1218 */
1219 template<typename _RandomNumberEngine, size_t __p, size_t __r>
1220 inline bool
1221 operator!=(const std::discard_block_engine<_RandomNumberEngine, __p,
1222 __r>& __lhs,
1223 const std::discard_block_engine<_RandomNumberEngine, __p,
1224 __r>& __rhs)
1225 { return !(__lhs == __rhs); }
1226#endif
1227
1228 /**
1229 * Produces random numbers by combining random numbers from some base
1230 * engine to produce random numbers with a specified number of bits @p __w.
1231 *
1232 * @headerfile random
1233 * @since C++11
1234 */
1235 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1237 {
1239 "result_type must be an unsigned integral type");
1240 static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
1241 "template argument substituting __w out of bounds");
1242
1243 template<typename _Sseq>
1244 using _If_seed_seq
1245 = __detail::_If_seed_seq_for<_Sseq, independent_bits_engine,
1246 _UIntType>;
1247
1248 public:
1249 /** The type of the generated random value. */
1250 typedef _UIntType result_type;
1251
1252 /**
1253 * @brief Constructs a default %independent_bits_engine engine.
1254 *
1255 * The underlying engine is default constructed as well.
1258 : _M_b() { }
1259
1260 /**
1261 * @brief Copy constructs a %independent_bits_engine engine.
1262 *
1263 * Copies an existing base class random number generator.
1264 * @param __rng An existing (base class) engine object.
1265 */
1266 explicit
1267 independent_bits_engine(const _RandomNumberEngine& __rng)
1268 : _M_b(__rng) { }
1269
1270 /**
1271 * @brief Move constructs a %independent_bits_engine engine.
1272 *
1273 * Copies an existing base class random number generator.
1274 * @param __rng An existing (base class) engine object.
1275 */
1276 explicit
1277 independent_bits_engine(_RandomNumberEngine&& __rng)
1278 : _M_b(std::move(__rng)) { }
1279
1280 /**
1281 * @brief Seed constructs a %independent_bits_engine engine.
1282 *
1283 * Constructs the underlying generator engine seeded with @p __s.
1284 * @param __s A seed value for the base class engine.
1285 */
1288 : _M_b(__s) { }
1289
1290 /**
1291 * @brief Generator construct a %independent_bits_engine engine.
1292 *
1293 * @param __q A seed sequence.
1294 */
1295 template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
1296 explicit
1297 independent_bits_engine(_Sseq& __q)
1298 : _M_b(__q)
1299 { }
1300
1301 /**
1302 * @brief Reseeds the %independent_bits_engine object with the default
1303 * seed for the underlying base class generator engine.
1304 */
1305 void
1306 seed()
1307 { _M_b.seed(); }
1308
1309 /**
1310 * @brief Reseeds the %independent_bits_engine object with the default
1311 * seed for the underlying base class generator engine.
1312 */
1313 void
1314 seed(result_type __s)
1315 { _M_b.seed(__s); }
1316
1317 /**
1318 * @brief Reseeds the %independent_bits_engine object with the given
1319 * seed sequence.
1320 * @param __q A seed generator function.
1321 */
1322 template<typename _Sseq>
1323 _If_seed_seq<_Sseq>
1324 seed(_Sseq& __q)
1325 { _M_b.seed(__q); }
1326
1327 /**
1328 * @brief Gets a const reference to the underlying generator engine
1329 * object.
1330 */
1331 const _RandomNumberEngine&
1332 base() const noexcept
1333 { return _M_b; }
1334
1335 /**
1336 * @brief Gets the minimum value in the generated random number range.
1337 */
1338 static constexpr result_type
1339 min()
1340 { return 0U; }
1341
1342 /**
1343 * @brief Gets the maximum value in the generated random number range.
1344 */
1345 static constexpr result_type
1346 max()
1347 { return __detail::_Shift<_UIntType, __w>::__value - 1; }
1348
1349 /**
1350 * @brief Discard a sequence of random numbers.
1351 */
1352 void
1353 discard(unsigned long long __z)
1354 {
1355 for (; __z != 0ULL; --__z)
1356 (*this)();
1357 }
1358
1359 /**
1360 * @brief Gets the next value in the generated random number sequence.
1361 */
1362 result_type
1363 operator()();
1364
1365 /**
1366 * @brief Compares two %independent_bits_engine random number generator
1367 * objects of the same type for equality.
1368 *
1369 * @param __lhs A %independent_bits_engine random number generator
1370 * object.
1371 * @param __rhs Another %independent_bits_engine random number generator
1372 * object.
1373 *
1374 * @returns true if the infinite sequences of generated values
1375 * would be equal, false otherwise.
1376 */
1377 friend bool
1379 const independent_bits_engine& __rhs)
1380 { return __lhs._M_b == __rhs._M_b; }
1381
1382 /**
1383 * @brief Extracts the current state of a % subtract_with_carry_engine
1384 * random number generator engine @p __x from the input stream
1385 * @p __is.
1386 *
1387 * @param __is An input stream.
1388 * @param __x A %independent_bits_engine random number generator
1389 * engine.
1390 *
1391 * @returns The input stream with the state of @p __x extracted or in
1392 * an error state.
1393 */
1394 template<typename _CharT, typename _Traits>
1397 std::independent_bits_engine<_RandomNumberEngine,
1398 __w, _UIntType>& __x)
1399 {
1400 __is >> __x._M_b;
1401 return __is;
1402 }
1403
1404 private:
1405 _RandomNumberEngine _M_b;
1406 };
1407
1408#if __cpp_impl_three_way_comparison < 201907L
1409 /**
1410 * @brief Compares two %independent_bits_engine random number generator
1411 * objects of the same type for inequality.
1412 *
1413 * @param __lhs A %independent_bits_engine random number generator
1414 * object.
1415 * @param __rhs Another %independent_bits_engine random number generator
1416 * object.
1417 *
1418 * @returns true if the infinite sequences of generated values
1419 * would be different, false otherwise.
1420 */
1421 template<typename _RandomNumberEngine, size_t __w, typename _UIntType>
1422 inline bool
1423 operator!=(const std::independent_bits_engine<_RandomNumberEngine, __w,
1424 _UIntType>& __lhs,
1425 const std::independent_bits_engine<_RandomNumberEngine, __w,
1426 _UIntType>& __rhs)
1427 { return !(__lhs == __rhs); }
1428#endif
1429
1430 /**
1431 * @brief Inserts the current state of a %independent_bits_engine random
1432 * number generator engine @p __x into the output stream @p __os.
1433 *
1434 * @param __os An output stream.
1435 * @param __x A %independent_bits_engine random number generator engine.
1436 *
1437 * @returns The output stream with the state of @p __x inserted or in
1438 * an error state.
1439 */
1440 template<typename _RandomNumberEngine, size_t __w, typename _UIntType,
1441 typename _CharT, typename _Traits>
1442 std::basic_ostream<_CharT, _Traits>&
1443 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1444 const std::independent_bits_engine<_RandomNumberEngine,
1445 __w, _UIntType>& __x)
1446 {
1447 __os << __x.base();
1448 return __os;
1449 }
1450
1451
1452 /**
1453 * @brief Produces random numbers by reordering random numbers from some
1454 * base engine.
1455 *
1456 * The values from the base engine are stored in a sequence of size @p __k
1457 * and shuffled by an algorithm that depends on those values.
1458 *
1459 * @headerfile random
1460 * @since C++11
1461 */
1462 template<typename _RandomNumberEngine, size_t __k>
1464 {
1465 static_assert(1u <= __k, "template argument substituting "
1466 "__k out of bound");
1467
1468 public:
1469 /** The type of the generated random value. */
1470 typedef typename _RandomNumberEngine::result_type result_type;
1471
1472 template<typename _Sseq>
1473 using _If_seed_seq
1474 = __detail::_If_seed_seq_for<_Sseq, shuffle_order_engine,
1475 result_type>;
1476
1477 static constexpr size_t table_size = __k;
1478
1479 /**
1480 * @brief Constructs a default %shuffle_order_engine engine.
1481 *
1482 * The underlying engine is default constructed as well.
1485 : _M_b()
1486 { _M_initialize(); }
1487
1488 /**
1489 * @brief Copy constructs a %shuffle_order_engine engine.
1490 *
1491 * Copies an existing base class random number generator.
1492 * @param __rng An existing (base class) engine object.
1493 */
1494 explicit
1495 shuffle_order_engine(const _RandomNumberEngine& __rng)
1496 : _M_b(__rng)
1497 { _M_initialize(); }
1498
1499 /**
1500 * @brief Move constructs a %shuffle_order_engine engine.
1501 *
1502 * Copies an existing base class random number generator.
1503 * @param __rng An existing (base class) engine object.
1504 */
1505 explicit
1506 shuffle_order_engine(_RandomNumberEngine&& __rng)
1507 : _M_b(std::move(__rng))
1508 { _M_initialize(); }
1509
1510 /**
1511 * @brief Seed constructs a %shuffle_order_engine engine.
1512 *
1513 * Constructs the underlying generator engine seeded with @p __s.
1514 * @param __s A seed value for the base class engine.
1515 */
1516 explicit
1518 : _M_b(__s)
1519 { _M_initialize(); }
1520
1521 /**
1522 * @brief Generator construct a %shuffle_order_engine engine.
1523 *
1524 * @param __q A seed sequence.
1525 */
1526 template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
1527 explicit
1528 shuffle_order_engine(_Sseq& __q)
1529 : _M_b(__q)
1530 { _M_initialize(); }
1531
1532 /**
1533 * @brief Reseeds the %shuffle_order_engine object with the default seed
1534 for the underlying base class generator engine.
1535 */
1536 void
1537 seed()
1538 {
1539 _M_b.seed();
1540 _M_initialize();
1541 }
1542
1543 /**
1544 * @brief Reseeds the %shuffle_order_engine object with the default seed
1545 * for the underlying base class generator engine.
1546 */
1547 void
1548 seed(result_type __s)
1549 {
1550 _M_b.seed(__s);
1551 _M_initialize();
1552 }
1553
1554 /**
1555 * @brief Reseeds the %shuffle_order_engine object with the given seed
1556 * sequence.
1557 * @param __q A seed generator function.
1558 */
1559 template<typename _Sseq>
1560 _If_seed_seq<_Sseq>
1561 seed(_Sseq& __q)
1562 {
1563 _M_b.seed(__q);
1564 _M_initialize();
1565 }
1566
1567 /**
1568 * Gets a const reference to the underlying generator engine object.
1569 */
1570 const _RandomNumberEngine&
1571 base() const noexcept
1572 { return _M_b; }
1573
1574 /**
1575 * Gets the minimum value in the generated random number range.
1576 */
1577 static constexpr result_type
1578 min()
1579 { return _RandomNumberEngine::min(); }
1580
1581 /**
1582 * Gets the maximum value in the generated random number range.
1583 */
1584 static constexpr result_type
1585 max()
1586 { return _RandomNumberEngine::max(); }
1587
1588 /**
1589 * Discard a sequence of random numbers.
1590 */
1591 void
1592 discard(unsigned long long __z)
1593 {
1594 for (; __z != 0ULL; --__z)
1595 (*this)();
1596 }
1597
1598 /**
1599 * Gets the next value in the generated random number sequence.
1600 */
1602 operator()();
1603
1604 /**
1605 * Compares two %shuffle_order_engine random number generator objects
1606 * of the same type for equality.
1607 *
1608 * @param __lhs A %shuffle_order_engine random number generator object.
1609 * @param __rhs Another %shuffle_order_engine random number generator
1610 * object.
1611 *
1612 * @returns true if the infinite sequences of generated values
1613 * would be equal, false otherwise.
1614 */
1615 friend bool
1616 operator==(const shuffle_order_engine& __lhs,
1617 const shuffle_order_engine& __rhs)
1618 { return (__lhs._M_b == __rhs._M_b
1619 && std::equal(__lhs._M_v, __lhs._M_v + __k, __rhs._M_v)
1620 && __lhs._M_y == __rhs._M_y); }
1621
1622 /**
1623 * @brief Inserts the current state of a %shuffle_order_engine random
1624 * number generator engine @p __x into the output stream
1625 @p __os.
1626 *
1627 * @param __os An output stream.
1628 * @param __x A %shuffle_order_engine random number generator engine.
1629 *
1630 * @returns The output stream with the state of @p __x inserted or in
1631 * an error state.
1632 */
1633 template<typename _RandomNumberEngine1, size_t __k1,
1634 typename _CharT, typename _Traits>
1637 const std::shuffle_order_engine<_RandomNumberEngine1,
1638 __k1>& __x);
1639
1640 /**
1641 * @brief Extracts the current state of a % subtract_with_carry_engine
1642 * random number generator engine @p __x from the input stream
1643 * @p __is.
1644 *
1645 * @param __is An input stream.
1646 * @param __x A %shuffle_order_engine random number generator engine.
1647 *
1648 * @returns The input stream with the state of @p __x extracted or in
1649 * an error state.
1650 */
1651 template<typename _RandomNumberEngine1, size_t __k1,
1652 typename _CharT, typename _Traits>
1656
1657 private:
1658 void _M_initialize()
1659 {
1660 for (size_t __i = 0; __i < __k; ++__i)
1661 _M_v[__i] = _M_b();
1662 _M_y = _M_b();
1663 }
1664
1665 _RandomNumberEngine _M_b;
1666 result_type _M_v[__k];
1667 result_type _M_y;
1668 };
1669
1670#if __cpp_impl_three_way_comparison < 201907L
1671 /**
1672 * Compares two %shuffle_order_engine random number generator objects
1673 * of the same type for inequality.
1674 *
1675 * @param __lhs A %shuffle_order_engine random number generator object.
1676 * @param __rhs Another %shuffle_order_engine random number generator
1677 * object.
1678 *
1679 * @returns true if the infinite sequences of generated values
1680 * would be different, false otherwise.
1681 */
1682 template<typename _RandomNumberEngine, size_t __k>
1683 inline bool
1684 operator!=(const std::shuffle_order_engine<_RandomNumberEngine,
1685 __k>& __lhs,
1686 const std::shuffle_order_engine<_RandomNumberEngine,
1687 __k>& __rhs)
1688 { return !(__lhs == __rhs); }
1689#endif
1690
1691 /**
1692 * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller.
1693 */
1696
1697 /**
1698 * An alternative LCR (Lehmer Generator function).
1699 */
1702
1703 /**
1704 * The classic Mersenne Twister.
1705 *
1706 * Reference:
1707 * M. Matsumoto and T. Nishimura, Mersenne Twister: A 623-Dimensionally
1708 * Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions
1709 * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
1710 */
1712 uint_fast32_t,
1713 32, 624, 397, 31,
1714 0x9908b0dfUL, 11,
1715 0xffffffffUL, 7,
1716 0x9d2c5680UL, 15,
1717 0xefc60000UL, 18, 1812433253UL> mt19937;
1718
1719 /**
1720 * An alternative Mersenne Twister.
1721 */
1723 uint_fast64_t,
1724 64, 312, 156, 31,
1725 0xb5026f5aa96619e9ULL, 29,
1726 0x5555555555555555ULL, 17,
1727 0x71d67fffeda60000ULL, 37,
1728 0xfff7eee000000000ULL, 43,
1729 6364136223846793005ULL> mt19937_64;
1730
1732 ranlux24_base;
1733
1735 ranlux48_base;
1736
1738
1740
1742
1743 typedef minstd_rand0 default_random_engine;
1744
1745 /**
1746 * A standard interface to a platform-specific non-deterministic
1747 * random number generator (if any are available).
1748 *
1749 * @headerfile random
1750 * @since C++11
1751 */
1752 class random_device
1753 {
1754 public:
1755 /** The type of the generated random value. */
1756 typedef unsigned int result_type;
1757
1758 // constructors, destructors and member functions
1759
1760 random_device() { _M_init("default"); }
1761
1762 explicit
1763 random_device(const std::string& __token) { _M_init(__token); }
1764
1765 ~random_device()
1766 { _M_fini(); }
1767
1768 static constexpr result_type
1769 min()
1771
1772 static constexpr result_type
1773 max()
1775
1776 double
1777 entropy() const noexcept
1778 { return this->_M_getentropy(); }
1779
1781 operator()()
1782 { return this->_M_getval(); }
1783
1784 // No copy functions.
1785 random_device(const random_device&) = delete;
1786 void operator=(const random_device&) = delete;
1787
1788 private:
1789
1790 void _M_init(const std::string& __token);
1791 void _M_init_pretr1(const std::string& __token);
1792 void _M_fini();
1793
1794 result_type _M_getval();
1795 result_type _M_getval_pretr1();
1796 double _M_getentropy() const noexcept;
1797
1798 void _M_init(const char*, size_t); // not exported from the shared library
1799
1800 __extension__ union
1801 {
1802 struct
1803 {
1804 void* _M_file;
1805 result_type (*_M_func)(void*);
1806 int _M_fd;
1807 };
1808 mt19937 _M_mt;
1809 };
1810 };
1811
1812 /// @} group random_generators
1813
1814 /**
1815 * @addtogroup random_distributions Random Number Distributions
1816 * @ingroup random
1817 * @{
1818 */
1819
1820 /**
1821 * @addtogroup random_distributions_uniform Uniform Distributions
1822 * @ingroup random_distributions
1823 * @{
1824 */
1825
1826 // std::uniform_int_distribution is defined in <bits/uniform_int_dist.h>
1827
1828#if __cpp_impl_three_way_comparison < 201907L
1829 /**
1830 * @brief Return true if two uniform integer distributions have
1831 * different parameters.
1832 */
1833 template<typename _IntType>
1834 inline bool
1835 operator!=(const std::uniform_int_distribution<_IntType>& __d1,
1836 const std::uniform_int_distribution<_IntType>& __d2)
1837 { return !(__d1 == __d2); }
1838#endif
1839
1840 /**
1841 * @brief Inserts a %uniform_int_distribution random number
1842 * distribution @p __x into the output stream @p os.
1843 *
1844 * @param __os An output stream.
1845 * @param __x A %uniform_int_distribution random number distribution.
1846 *
1847 * @returns The output stream with the state of @p __x inserted or in
1848 * an error state.
1849 */
1850 template<typename _IntType, typename _CharT, typename _Traits>
1851 std::basic_ostream<_CharT, _Traits>&
1852 operator<<(std::basic_ostream<_CharT, _Traits>&,
1853 const std::uniform_int_distribution<_IntType>&);
1854
1855 /**
1856 * @brief Extracts a %uniform_int_distribution random number distribution
1857 * @p __x from the input stream @p __is.
1858 *
1859 * @param __is An input stream.
1860 * @param __x A %uniform_int_distribution random number generator engine.
1861 *
1862 * @returns The input stream with @p __x extracted or in an error state.
1863 */
1864 template<typename _IntType, typename _CharT, typename _Traits>
1865 std::basic_istream<_CharT, _Traits>&
1866 operator>>(std::basic_istream<_CharT, _Traits>&,
1867 std::uniform_int_distribution<_IntType>&);
1868
1869
1870 /**
1871 * @brief Uniform continuous distribution for random numbers.
1872 *
1873 * A continuous random distribution on the range [min, max) with equal
1874 * probability throughout the range. The URNG should be real-valued and
1875 * deliver number in the range [0, 1).
1876 *
1877 * @headerfile random
1878 * @since C++11
1879 */
1880 template<typename _RealType = double>
1882 {
1884 "result_type must be a floating point type");
1885
1886 public:
1887 /** The type of the range of the distribution. */
1888 typedef _RealType result_type;
1889
1890 /** Parameter type. */
1891 struct param_type
1892 {
1893 typedef uniform_real_distribution<_RealType> distribution_type;
1894
1895 param_type() : param_type(0) { }
1896
1897 explicit
1898 param_type(_RealType __a, _RealType __b = _RealType(1))
1899 : _M_a(__a), _M_b(__b)
1900 {
1901 __glibcxx_assert(_M_a <= _M_b);
1902 }
1903
1905 a() const
1906 { return _M_a; }
1907
1909 b() const
1910 { return _M_b; }
1911
1912 friend bool
1913 operator==(const param_type& __p1, const param_type& __p2)
1914 { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
1915
1916#if __cpp_impl_three_way_comparison < 201907L
1917 friend bool
1918 operator!=(const param_type& __p1, const param_type& __p2)
1919 { return !(__p1 == __p2); }
1920#endif
1921
1922 private:
1923 _RealType _M_a;
1924 _RealType _M_b;
1925 };
1926
1927 public:
1928 /**
1929 * @brief Constructs a uniform_real_distribution object.
1930 *
1931 * The lower bound is set to 0.0 and the upper bound to 1.0
1932 */
1934
1935 /**
1936 * @brief Constructs a uniform_real_distribution object.
1937 *
1938 * @param __a [IN] The lower bound of the distribution.
1939 * @param __b [IN] The upper bound of the distribution.
1940 */
1941 explicit
1942 uniform_real_distribution(_RealType __a, _RealType __b = _RealType(1))
1943 : _M_param(__a, __b)
1944 { }
1945
1946 explicit
1947 uniform_real_distribution(const param_type& __p)
1948 : _M_param(__p)
1949 { }
1950
1951 /**
1952 * @brief Resets the distribution state.
1953 *
1954 * Does nothing for the uniform real distribution.
1955 */
1956 void
1957 reset() { }
1958
1959 result_type
1960 a() const
1961 { return _M_param.a(); }
1962
1963 result_type
1964 b() const
1965 { return _M_param.b(); }
1966
1967 /**
1968 * @brief Returns the parameter set of the distribution.
1969 */
1971 param() const
1972 { return _M_param; }
1973
1974 /**
1975 * @brief Sets the parameter set of the distribution.
1976 * @param __param The new parameter set of the distribution.
1977 */
1978 void
1979 param(const param_type& __param)
1980 { _M_param = __param; }
1981
1982 /**
1983 * @brief Returns the inclusive lower bound of the distribution range.
1984 */
1985 result_type
1986 min() const
1987 { return this->a(); }
1988
1989 /**
1990 * @brief Returns the inclusive upper bound of the distribution range.
1991 */
1992 result_type
1993 max() const
1994 { return this->b(); }
1995
1996 /**
1997 * @brief Generating functions.
1998 */
1999 template<typename _UniformRandomNumberGenerator>
2000 result_type
2001 operator()(_UniformRandomNumberGenerator& __urng)
2002 { return this->operator()(__urng, _M_param); }
2003
2004 template<typename _UniformRandomNumberGenerator>
2005 result_type
2006 operator()(_UniformRandomNumberGenerator& __urng,
2007 const param_type& __p)
2008 {
2009 __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
2010 __aurng(__urng);
2011 return (__aurng() * (__p.b() - __p.a())) + __p.a();
2012 }
2013
2014 template<typename _ForwardIterator,
2015 typename _UniformRandomNumberGenerator>
2016 void
2017 __generate(_ForwardIterator __f, _ForwardIterator __t,
2018 _UniformRandomNumberGenerator& __urng)
2019 { this->__generate(__f, __t, __urng, _M_param); }
2020
2021 template<typename _ForwardIterator,
2022 typename _UniformRandomNumberGenerator>
2023 void
2024 __generate(_ForwardIterator __f, _ForwardIterator __t,
2025 _UniformRandomNumberGenerator& __urng,
2026 const param_type& __p)
2027 { this->__generate_impl(__f, __t, __urng, __p); }
2028
2029 template<typename _UniformRandomNumberGenerator>
2030 void
2031 __generate(result_type* __f, result_type* __t,
2032 _UniformRandomNumberGenerator& __urng,
2033 const param_type& __p)
2034 { this->__generate_impl(__f, __t, __urng, __p); }
2035
2036 /**
2037 * @brief Return true if two uniform real distributions have
2038 * the same parameters.
2039 */
2040 friend bool
2042 const uniform_real_distribution& __d2)
2043 { return __d1._M_param == __d2._M_param; }
2044
2045 private:
2046 template<typename _ForwardIterator,
2047 typename _UniformRandomNumberGenerator>
2048 void
2049 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2050 _UniformRandomNumberGenerator& __urng,
2051 const param_type& __p);
2052
2053 param_type _M_param;
2054 };
2055
2056#if __cpp_impl_three_way_comparison < 201907L
2057 /**
2058 * @brief Return true if two uniform real distributions have
2059 * different parameters.
2060 */
2061 template<typename _IntType>
2062 inline bool
2063 operator!=(const std::uniform_real_distribution<_IntType>& __d1,
2065 { return !(__d1 == __d2); }
2066#endif
2067
2068 /**
2069 * @brief Inserts a %uniform_real_distribution random number
2070 * distribution @p __x into the output stream @p __os.
2071 *
2072 * @param __os An output stream.
2073 * @param __x A %uniform_real_distribution random number distribution.
2074 *
2075 * @returns The output stream with the state of @p __x inserted or in
2076 * an error state.
2077 */
2078 template<typename _RealType, typename _CharT, typename _Traits>
2079 std::basic_ostream<_CharT, _Traits>&
2080 operator<<(std::basic_ostream<_CharT, _Traits>&,
2081 const std::uniform_real_distribution<_RealType>&);
2082
2083 /**
2084 * @brief Extracts a %uniform_real_distribution random number distribution
2085 * @p __x from the input stream @p __is.
2086 *
2087 * @param __is An input stream.
2088 * @param __x A %uniform_real_distribution random number generator engine.
2089 *
2090 * @returns The input stream with @p __x extracted or in an error state.
2091 */
2092 template<typename _RealType, typename _CharT, typename _Traits>
2093 std::basic_istream<_CharT, _Traits>&
2094 operator>>(std::basic_istream<_CharT, _Traits>&,
2095 std::uniform_real_distribution<_RealType>&);
2096
2097 /// @} group random_distributions_uniform
2098
2099 /**
2100 * @addtogroup random_distributions_normal Normal Distributions
2101 * @ingroup random_distributions
2102 * @{
2103 */
2104
2105 /**
2106 * @brief A normal continuous distribution for random numbers.
2107 *
2108 * The formula for the normal probability density function is
2109 * @f[
2110 * p(x|\mu,\sigma) = \frac{1}{\sigma \sqrt{2 \pi}}
2111 * e^{- \frac{{x - \mu}^ {2}}{2 \sigma ^ {2}} }
2112 * @f]
2113 *
2114 * @headerfile random
2115 * @since C++11
2116 */
2117 template<typename _RealType = double>
2118 class normal_distribution
2119 {
2121 "result_type must be a floating point type");
2122
2123 public:
2124 /** The type of the range of the distribution. */
2125 typedef _RealType result_type;
2126
2127 /** Parameter type. */
2128 struct param_type
2129 {
2130 typedef normal_distribution<_RealType> distribution_type;
2131
2132 param_type() : param_type(0.0) { }
2133
2134 explicit
2135 param_type(_RealType __mean, _RealType __stddev = _RealType(1))
2136 : _M_mean(__mean), _M_stddev(__stddev)
2137 {
2138 __glibcxx_assert(_M_stddev > _RealType(0));
2139 }
2140
2141 _RealType
2142 mean() const
2143 { return _M_mean; }
2144
2145 _RealType
2146 stddev() const
2147 { return _M_stddev; }
2148
2149 friend bool
2150 operator==(const param_type& __p1, const param_type& __p2)
2151 { return (__p1._M_mean == __p2._M_mean
2152 && __p1._M_stddev == __p2._M_stddev); }
2153
2154#if __cpp_impl_three_way_comparison < 201907L
2155 friend bool
2156 operator!=(const param_type& __p1, const param_type& __p2)
2157 { return !(__p1 == __p2); }
2158#endif
2159
2160 private:
2161 _RealType _M_mean;
2162 _RealType _M_stddev;
2163 };
2164
2165 public:
2166 normal_distribution() : normal_distribution(0.0) { }
2167
2168 /**
2169 * Constructs a normal distribution with parameters @f$mean@f$ and
2170 * standard deviation.
2171 */
2172 explicit
2174 result_type __stddev = result_type(1))
2175 : _M_param(__mean, __stddev)
2176 { }
2177
2178 explicit
2179 normal_distribution(const param_type& __p)
2180 : _M_param(__p)
2181 { }
2182
2183 /**
2184 * @brief Resets the distribution state.
2185 */
2186 void
2188 { _M_saved_available = false; }
2189
2190 /**
2191 * @brief Returns the mean of the distribution.
2192 */
2193 _RealType
2194 mean() const
2195 { return _M_param.mean(); }
2196
2197 /**
2198 * @brief Returns the standard deviation of the distribution.
2199 */
2200 _RealType
2201 stddev() const
2202 { return _M_param.stddev(); }
2203
2204 /**
2205 * @brief Returns the parameter set of the distribution.
2206 */
2207 param_type
2208 param() const
2209 { return _M_param; }
2210
2211 /**
2212 * @brief Sets the parameter set of the distribution.
2213 * @param __param The new parameter set of the distribution.
2214 */
2215 void
2216 param(const param_type& __param)
2217 { _M_param = __param; }
2218
2219 /**
2220 * @brief Returns the greatest lower bound value of the distribution.
2221 */
2222 result_type
2225
2226 /**
2227 * @brief Returns the least upper bound value of the distribution.
2228 */
2229 result_type
2230 max() const
2232
2233 /**
2234 * @brief Generating functions.
2235 */
2236 template<typename _UniformRandomNumberGenerator>
2237 result_type
2238 operator()(_UniformRandomNumberGenerator& __urng)
2239 { return this->operator()(__urng, _M_param); }
2240
2241 template<typename _UniformRandomNumberGenerator>
2242 result_type
2243 operator()(_UniformRandomNumberGenerator& __urng,
2244 const param_type& __p);
2245
2246 template<typename _ForwardIterator,
2247 typename _UniformRandomNumberGenerator>
2248 void
2249 __generate(_ForwardIterator __f, _ForwardIterator __t,
2250 _UniformRandomNumberGenerator& __urng)
2251 { this->__generate(__f, __t, __urng, _M_param); }
2252
2253 template<typename _ForwardIterator,
2254 typename _UniformRandomNumberGenerator>
2255 void
2256 __generate(_ForwardIterator __f, _ForwardIterator __t,
2257 _UniformRandomNumberGenerator& __urng,
2258 const param_type& __p)
2259 { this->__generate_impl(__f, __t, __urng, __p); }
2260
2261 template<typename _UniformRandomNumberGenerator>
2262 void
2263 __generate(result_type* __f, result_type* __t,
2264 _UniformRandomNumberGenerator& __urng,
2265 const param_type& __p)
2266 { this->__generate_impl(__f, __t, __urng, __p); }
2267
2268 /**
2269 * @brief Return true if two normal distributions have
2270 * the same parameters and the sequences that would
2271 * be generated are equal.
2272 */
2273 template<typename _RealType1>
2274 friend bool
2277
2278 /**
2279 * @brief Inserts a %normal_distribution random number distribution
2280 * @p __x into the output stream @p __os.
2281 *
2282 * @param __os An output stream.
2283 * @param __x A %normal_distribution random number distribution.
2284 *
2285 * @returns The output stream with the state of @p __x inserted or in
2286 * an error state.
2287 */
2288 template<typename _RealType1, typename _CharT, typename _Traits>
2292
2293 /**
2294 * @brief Extracts a %normal_distribution random number distribution
2295 * @p __x from the input stream @p __is.
2296 *
2297 * @param __is An input stream.
2298 * @param __x A %normal_distribution random number generator engine.
2299 *
2300 * @returns The input stream with @p __x extracted or in an error
2301 * state.
2302 */
2303 template<typename _RealType1, typename _CharT, typename _Traits>
2307
2308 private:
2309 template<typename _ForwardIterator,
2310 typename _UniformRandomNumberGenerator>
2311 void
2312 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2313 _UniformRandomNumberGenerator& __urng,
2314 const param_type& __p);
2315
2316 param_type _M_param;
2317 result_type _M_saved = 0;
2318 bool _M_saved_available = false;
2319 };
2320
2321#if __cpp_impl_three_way_comparison < 201907L
2322 /**
2323 * @brief Return true if two normal distributions are different.
2324 */
2325 template<typename _RealType>
2326 inline bool
2327 operator!=(const std::normal_distribution<_RealType>& __d1,
2329 { return !(__d1 == __d2); }
2330#endif
2331
2332 /**
2333 * @brief A lognormal_distribution random number distribution.
2334 *
2335 * The formula for the normal probability mass function is
2336 * @f[
2337 * p(x|m,s) = \frac{1}{sx\sqrt{2\pi}}
2338 * \exp{-\frac{(\ln{x} - m)^2}{2s^2}}
2339 * @f]
2340 *
2341 * @headerfile random
2342 * @since C++11
2343 */
2344 template<typename _RealType = double>
2345 class lognormal_distribution
2346 {
2348 "result_type must be a floating point type");
2349
2350 public:
2351 /** The type of the range of the distribution. */
2352 typedef _RealType result_type;
2353
2354 /** Parameter type. */
2355 struct param_type
2356 {
2357 typedef lognormal_distribution<_RealType> distribution_type;
2358
2359 param_type() : param_type(0.0) { }
2360
2361 explicit
2362 param_type(_RealType __m, _RealType __s = _RealType(1))
2363 : _M_m(__m), _M_s(__s)
2364 { }
2365
2366 _RealType
2367 m() const
2368 { return _M_m; }
2369
2370 _RealType
2371 s() const
2372 { return _M_s; }
2373
2374 friend bool
2375 operator==(const param_type& __p1, const param_type& __p2)
2376 { return __p1._M_m == __p2._M_m && __p1._M_s == __p2._M_s; }
2377
2378#if __cpp_impl_three_way_comparison < 201907L
2379 friend bool
2380 operator!=(const param_type& __p1, const param_type& __p2)
2381 { return !(__p1 == __p2); }
2382#endif
2383
2384 private:
2385 _RealType _M_m;
2386 _RealType _M_s;
2387 };
2388
2389 lognormal_distribution() : lognormal_distribution(0.0) { }
2390
2391 explicit
2392 lognormal_distribution(_RealType __m, _RealType __s = _RealType(1))
2393 : _M_param(__m, __s), _M_nd()
2394 { }
2395
2396 explicit
2397 lognormal_distribution(const param_type& __p)
2398 : _M_param(__p), _M_nd()
2399 { }
2400
2401 /**
2402 * Resets the distribution state.
2403 */
2404 void
2406 { _M_nd.reset(); }
2407
2408 /**
2409 *
2410 */
2411 _RealType
2412 m() const
2413 { return _M_param.m(); }
2414
2415 _RealType
2416 s() const
2417 { return _M_param.s(); }
2418
2419 /**
2420 * @brief Returns the parameter set of the distribution.
2421 */
2423 param() const
2424 { return _M_param; }
2425
2426 /**
2427 * @brief Sets the parameter set of the distribution.
2428 * @param __param The new parameter set of the distribution.
2429 */
2430 void
2431 param(const param_type& __param)
2432 { _M_param = __param; }
2433
2434 /**
2435 * @brief Returns the greatest lower bound value of the distribution.
2436 */
2437 result_type
2438 min() const
2439 { return result_type(0); }
2440
2441 /**
2442 * @brief Returns the least upper bound value of the distribution.
2443 */
2444 result_type
2445 max() const
2447
2448 /**
2449 * @brief Generating functions.
2450 */
2451 template<typename _UniformRandomNumberGenerator>
2452 result_type
2453 operator()(_UniformRandomNumberGenerator& __urng)
2454 { return this->operator()(__urng, _M_param); }
2455
2456 template<typename _UniformRandomNumberGenerator>
2457 result_type
2458 operator()(_UniformRandomNumberGenerator& __urng,
2459 const param_type& __p)
2460 { return std::exp(__p.s() * _M_nd(__urng) + __p.m()); }
2461
2462 template<typename _ForwardIterator,
2463 typename _UniformRandomNumberGenerator>
2464 void
2465 __generate(_ForwardIterator __f, _ForwardIterator __t,
2466 _UniformRandomNumberGenerator& __urng)
2467 { this->__generate(__f, __t, __urng, _M_param); }
2468
2469 template<typename _ForwardIterator,
2470 typename _UniformRandomNumberGenerator>
2471 void
2472 __generate(_ForwardIterator __f, _ForwardIterator __t,
2473 _UniformRandomNumberGenerator& __urng,
2474 const param_type& __p)
2475 { this->__generate_impl(__f, __t, __urng, __p); }
2476
2477 template<typename _UniformRandomNumberGenerator>
2478 void
2479 __generate(result_type* __f, result_type* __t,
2480 _UniformRandomNumberGenerator& __urng,
2481 const param_type& __p)
2482 { this->__generate_impl(__f, __t, __urng, __p); }
2483
2484 /**
2485 * @brief Return true if two lognormal distributions have
2486 * the same parameters and the sequences that would
2487 * be generated are equal.
2488 */
2489 friend bool
2490 operator==(const lognormal_distribution& __d1,
2491 const lognormal_distribution& __d2)
2492 { return (__d1._M_param == __d2._M_param
2493 && __d1._M_nd == __d2._M_nd); }
2494
2495 /**
2496 * @brief Inserts a %lognormal_distribution random number distribution
2497 * @p __x into the output stream @p __os.
2498 *
2499 * @param __os An output stream.
2500 * @param __x A %lognormal_distribution random number distribution.
2501 *
2502 * @returns The output stream with the state of @p __x inserted or in
2503 * an error state.
2504 */
2505 template<typename _RealType1, typename _CharT, typename _Traits>
2509
2510 /**
2511 * @brief Extracts a %lognormal_distribution random number distribution
2512 * @p __x from the input stream @p __is.
2513 *
2514 * @param __is An input stream.
2515 * @param __x A %lognormal_distribution random number
2516 * generator engine.
2517 *
2518 * @returns The input stream with @p __x extracted or in an error state.
2519 */
2520 template<typename _RealType1, typename _CharT, typename _Traits>
2524
2525 private:
2526 template<typename _ForwardIterator,
2527 typename _UniformRandomNumberGenerator>
2528 void
2529 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2530 _UniformRandomNumberGenerator& __urng,
2531 const param_type& __p);
2532
2533 param_type _M_param;
2534
2536 };
2537
2538#if __cpp_impl_three_way_comparison < 201907L
2539 /**
2540 * @brief Return true if two lognormal distributions are different.
2541 */
2542 template<typename _RealType>
2543 inline bool
2544 operator!=(const std::lognormal_distribution<_RealType>& __d1,
2546 { return !(__d1 == __d2); }
2547#endif
2548
2549 /// @} group random_distributions_normal
2550
2551 /**
2552 * @addtogroup random_distributions_poisson Poisson Distributions
2553 * @ingroup random_distributions
2554 * @{
2555 */
2556
2557 /**
2558 * @brief A gamma continuous distribution for random numbers.
2559 *
2560 * The formula for the gamma probability density function is:
2561 * @f[
2562 * p(x|\alpha,\beta) = \frac{1}{\beta\Gamma(\alpha)}
2563 * (x/\beta)^{\alpha - 1} e^{-x/\beta}
2564 * @f]
2565 *
2566 * @headerfile random
2567 * @since C++11
2568 */
2569 template<typename _RealType = double>
2571 {
2573 "result_type must be a floating point type");
2574
2575 public:
2576 /** The type of the range of the distribution. */
2577 typedef _RealType result_type;
2578
2579 /** Parameter type. */
2580 struct param_type
2581 {
2582 typedef gamma_distribution<_RealType> distribution_type;
2583 friend class gamma_distribution<_RealType>;
2584
2585 param_type() : param_type(1.0) { }
2586
2587 explicit
2588 param_type(_RealType __alpha_val, _RealType __beta_val = _RealType(1))
2589 : _M_alpha(__alpha_val), _M_beta(__beta_val)
2590 {
2591 __glibcxx_assert(_M_alpha > _RealType(0));
2592 _M_initialize();
2593 }
2594
2595 _RealType
2596 alpha() const
2597 { return _M_alpha; }
2598
2599 _RealType
2600 beta() const
2601 { return _M_beta; }
2602
2603 friend bool
2604 operator==(const param_type& __p1, const param_type& __p2)
2605 { return (__p1._M_alpha == __p2._M_alpha
2606 && __p1._M_beta == __p2._M_beta); }
2607
2608#if __cpp_impl_three_way_comparison < 201907L
2609 friend bool
2610 operator!=(const param_type& __p1, const param_type& __p2)
2611 { return !(__p1 == __p2); }
2612#endif
2613
2614 private:
2615 void
2616 _M_initialize();
2617
2618 _RealType _M_alpha;
2619 _RealType _M_beta;
2620
2621 _RealType _M_malpha, _M_a2;
2622 };
2623
2624 public:
2625 /**
2626 * @brief Constructs a gamma distribution with parameters 1 and 1.
2627 */
2629
2630 /**
2631 * @brief Constructs a gamma distribution with parameters
2632 * @f$\alpha@f$ and @f$\beta@f$.
2633 */
2634 explicit
2635 gamma_distribution(_RealType __alpha_val,
2636 _RealType __beta_val = _RealType(1))
2637 : _M_param(__alpha_val, __beta_val), _M_nd()
2638 { }
2639
2640 explicit
2641 gamma_distribution(const param_type& __p)
2642 : _M_param(__p), _M_nd()
2643 { }
2644
2645 /**
2646 * @brief Resets the distribution state.
2647 */
2648 void
2650 { _M_nd.reset(); }
2651
2652 /**
2653 * @brief Returns the @f$\alpha@f$ of the distribution.
2654 */
2655 _RealType
2656 alpha() const
2657 { return _M_param.alpha(); }
2658
2659 /**
2660 * @brief Returns the @f$\beta@f$ of the distribution.
2661 */
2662 _RealType
2663 beta() const
2664 { return _M_param.beta(); }
2665
2666 /**
2667 * @brief Returns the parameter set of the distribution.
2668 */
2669 param_type
2670 param() const
2671 { return _M_param; }
2672
2673 /**
2674 * @brief Sets the parameter set of the distribution.
2675 * @param __param The new parameter set of the distribution.
2676 */
2677 void
2678 param(const param_type& __param)
2679 { _M_param = __param; }
2680
2681 /**
2682 * @brief Returns the greatest lower bound value of the distribution.
2683 */
2684 result_type
2685 min() const
2686 { return result_type(0); }
2687
2688 /**
2689 * @brief Returns the least upper bound value of the distribution.
2690 */
2691 result_type
2692 max() const
2694
2695 /**
2696 * @brief Generating functions.
2697 */
2698 template<typename _UniformRandomNumberGenerator>
2699 result_type
2700 operator()(_UniformRandomNumberGenerator& __urng)
2701 { return this->operator()(__urng, _M_param); }
2702
2703 template<typename _UniformRandomNumberGenerator>
2704 result_type
2705 operator()(_UniformRandomNumberGenerator& __urng,
2706 const param_type& __p);
2707
2708 template<typename _ForwardIterator,
2709 typename _UniformRandomNumberGenerator>
2710 void
2711 __generate(_ForwardIterator __f, _ForwardIterator __t,
2712 _UniformRandomNumberGenerator& __urng)
2713 { this->__generate(__f, __t, __urng, _M_param); }
2714
2715 template<typename _ForwardIterator,
2716 typename _UniformRandomNumberGenerator>
2717 void
2718 __generate(_ForwardIterator __f, _ForwardIterator __t,
2719 _UniformRandomNumberGenerator& __urng,
2720 const param_type& __p)
2721 { this->__generate_impl(__f, __t, __urng, __p); }
2722
2723 template<typename _UniformRandomNumberGenerator>
2724 void
2725 __generate(result_type* __f, result_type* __t,
2726 _UniformRandomNumberGenerator& __urng,
2727 const param_type& __p)
2728 { this->__generate_impl(__f, __t, __urng, __p); }
2729
2730 /**
2731 * @brief Return true if two gamma distributions have the same
2732 * parameters and the sequences that would be generated
2733 * are equal.
2734 */
2735 friend bool
2737 const gamma_distribution& __d2)
2738 { return (__d1._M_param == __d2._M_param
2739 && __d1._M_nd == __d2._M_nd); }
2740
2741 /**
2742 * @brief Inserts a %gamma_distribution random number distribution
2743 * @p __x into the output stream @p __os.
2744 *
2745 * @param __os An output stream.
2746 * @param __x A %gamma_distribution random number distribution.
2747 *
2748 * @returns The output stream with the state of @p __x inserted or in
2749 * an error state.
2750 */
2751 template<typename _RealType1, typename _CharT, typename _Traits>
2755
2756 /**
2757 * @brief Extracts a %gamma_distribution random number distribution
2758 * @p __x from the input stream @p __is.
2759 *
2760 * @param __is An input stream.
2761 * @param __x A %gamma_distribution random number generator engine.
2762 *
2763 * @returns The input stream with @p __x extracted or in an error state.
2764 */
2765 template<typename _RealType1, typename _CharT, typename _Traits>
2769
2770 private:
2771 template<typename _ForwardIterator,
2772 typename _UniformRandomNumberGenerator>
2773 void
2774 __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
2775 _UniformRandomNumberGenerator& __urng,
2776 const param_type& __p);
2777
2778 param_type _M_param;
2779
2781 };
2782
2783#if __cpp_impl_three_way_comparison < 201907L
2784 /**
2785 * @brief Return true if two gamma distributions are different.
2786 */
2787 template<typename _RealType>
2788 inline bool
2789 operator!=(const std::gamma_distribution<_RealType>& __d1,
2791 { return !(__d1 == __d2); }
2792#endif
2793
2794 /// @} group random_distributions_poisson
2795
2796 /**
2797 * @addtogroup random_distributions_normal Normal Distributions
2798 * @ingroup random_distributions
2799 * @{
2800 */
2801
2802 /**
2803 * @brief A chi_squared_distribution random number distribution.
2804 *
2805 * The formula for the normal probability mass function is
2806 * @f$p(x|n) = \frac{x^{(n/2) - 1}e^{-x/2}}{\Gamma(n/2) 2^{n/2}}@f$
2807 *
2808 * @headerfile random
2809 * @since C++11
2810 */
2811 template<typename _RealType = double>
2812 class chi_squared_distribution
2813 {
2815 "result_type must be a floating point type");
2816
2817 public:
2818 /** The type of the range of the distribution. */
2819 typedef _RealType result_type;
2820
2821 /** Parameter type. */
2822 struct param_type
2823 {
2824 typedef chi_squared_distribution<_RealType> distribution_type;
2825
2826 param_type() : param_type(1) { }
2827
2828 explicit
2829 param_type(_RealType __n)
2830 : _M_n(__n)
2831 { }
2832
2833 _RealType
2834 n() const
2835 { return _M_n; }
2836
2837 friend bool
2838 operator==(const param_type& __p1, const param_type& __p2)
2839 { return __p1._M_n == __p2._M_n; }
2840
2841#if __cpp_impl_three_way_comparison < 201907L
2842 friend bool
2843 operator!=(const param_type& __p1, const param_type& __p2)
2844 { return !(__p1 == __p2); }
2845#endif
2846
2847 private:
2848 _RealType _M_n;
2849 };
2850
2851 chi_squared_distribution() : chi_squared_distribution(1) { }
2852
2853 explicit
2854 chi_squared_distribution(_RealType __n)
2855 : _M_param(__n), _M_gd(__n / 2)
2856 { }
2857
2858 explicit
2859 chi_squared_distribution(const param_type& __p)
2860 : _M_param(__p), _M_gd(__p.n() / 2)
2861 { }
2862
2863 /**
2864 * @brief Resets the distribution state.
2865 */
2866 void
2868 { _M_gd.reset(); }
2869
2870 /**
2871 *
2872 */
2873 _RealType
2874 n() const
2875 { return _M_param.n(); }
2876
2877 /**
2878 * @brief Returns the parameter set of the distribution.
2879 */
2880 param_type
2881 param() const
2882 { return _M_param; }
2883
2884 /**
2885 * @brief Sets the parameter set of the distribution.
2886 * @param __param The new parameter set of the distribution.
2887 */
2888 void
2889 param(const param_type& __param)
2890 {
2891 _M_param = __param;
2893 param_type;
2894 _M_gd.param(param_type{__param.n() / 2});
2895 }
2896
2897 /**
2898 * @brief Returns the greatest lower bound value of the distribution.
2899 */
2900 result_type
2901 min() const
2902 { return result_type(0); }
2903
2904 /**
2905 * @brief Returns the least upper bound value of the distribution.
2906 */
2907 result_type