mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
random.h
6406 lines (5553 loc) · 182 KB
/
random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// random number generation -*- C++ -*-
// Copyright (C) 2009-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/**
* @file bits/random.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{random}
*/
#ifndef _RANDOM_H
#define _RANDOM_H 1
#include <vector>
#include <bits/uniform_int_dist.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
// [26.4] Random number generation
/**
* @defgroup random Random Number Generation
* @ingroup numerics
*
* A facility for generating random numbers on selected distributions.
* @{
*/
// std::uniform_random_bit_generator is defined in <bits/uniform_int_dist.h>
/**
* @brief A function template for converting the output of a (integral)
* uniform random number generator to a floatng point result in the range
* [0-1).
*/
template<typename _RealType, size_t __bits,
typename _UniformRandomNumberGenerator>
_RealType
generate_canonical(_UniformRandomNumberGenerator& __g);
/// @cond undocumented
// Implementation-space details.
namespace __detail
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wc++17-extensions"
template<typename _UIntType, size_t __w,
bool = __w < static_cast<size_t>
(std::numeric_limits<_UIntType>::digits)>
struct _Shift
{ static constexpr _UIntType __value = 0; };
template<typename _UIntType, size_t __w>
struct _Shift<_UIntType, __w, true>
{ static constexpr _UIntType __value = _UIntType(1) << __w; };
template<int __s,
int __which = ((__s <= __CHAR_BIT__ * sizeof (int))
+ (__s <= __CHAR_BIT__ * sizeof (long))
+ (__s <= __CHAR_BIT__ * sizeof (long long))
/* assume long long no bigger than __int128 */
+ (__s <= 128))>
struct _Select_uint_least_t
{
static_assert(__which < 0, /* needs to be dependent */
"sorry, would be too much trouble for a slow result");
};
template<int __s>
struct _Select_uint_least_t<__s, 4>
{ using type = unsigned int; };
template<int __s>
struct _Select_uint_least_t<__s, 3>
{ using type = unsigned long; };
template<int __s>
struct _Select_uint_least_t<__s, 2>
{ using type = unsigned long long; };
#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
template<int __s>
struct _Select_uint_least_t<__s, 1>
{ __extension__ using type = unsigned __int128; };
#elif __has_builtin(__builtin_add_overflow) \
&& __has_builtin(__builtin_sub_overflow) \
&& defined __UINT64_TYPE__
template<int __s>
struct _Select_uint_least_t<__s, 1>
{
// This is NOT a general-purpose 128-bit integer type.
// It only supports (type(a) * x + c) % m as needed by __mod.
struct type
{
explicit
type(uint64_t __a) noexcept : _M_lo(__a), _M_hi(0) { }
// pre: __l._M_hi == 0
friend type
operator*(type __l, uint64_t __x) noexcept
{
// Split 64-bit values __l._M_lo and __x into high and low 32-bit
// limbs and multiply those individually.
// l * x = (l0 + l1) * (x0 + x1) = l0x0 + l0x1 + l1x0 + l1x1
constexpr uint64_t __mask = 0xffffffff;
uint64_t __ll[2] = { __l._M_lo >> 32, __l._M_lo & __mask };
uint64_t __xx[2] = { __x >> 32, __x & __mask };
uint64_t __l0x0 = __ll[0] * __xx[0];
uint64_t __l0x1 = __ll[0] * __xx[1];
uint64_t __l1x0 = __ll[1] * __xx[0];
uint64_t __l1x1 = __ll[1] * __xx[1];
// These bits are the low half of __l._M_hi
// and the high half of __l._M_lo.
uint64_t __mid
= (__l0x1 & __mask) + (__l1x0 & __mask) + (__l1x1 >> 32);
__l._M_hi = __l0x0 + (__l0x1 >> 32) + (__l1x0 >> 32) + (__mid >> 32);
__l._M_lo = (__mid << 32) + (__l1x1 & __mask);
return __l;
}
friend type
operator+(type __l, uint64_t __c) noexcept
{
__l._M_hi += __builtin_add_overflow(__l._M_lo, __c, &__l._M_lo);
return __l;
}
friend type
operator%(type __l, uint64_t __m) noexcept
{
if (__builtin_expect(__l._M_hi == 0, 0))
{
__l._M_lo %= __m;
return __l;
}
int __shift = __builtin_clzll(__m) + 64
- __builtin_clzll(__l._M_hi);
type __x(0);
if (__shift >= 64)
{
__x._M_hi = __m << (__shift - 64);
__x._M_lo = 0;
}
else
{
__x._M_hi = __m >> (64 - __shift);
__x._M_lo = __m << __shift;
}
while (__l._M_hi != 0 || __l._M_lo >= __m)
{
if (__x <= __l)
{
__l._M_hi -= __x._M_hi;
__l._M_hi -= __builtin_sub_overflow(__l._M_lo, __x._M_lo,
&__l._M_lo);
}
__x._M_lo = (__x._M_lo >> 1) | (__x._M_hi << 63);
__x._M_hi >>= 1;
}
return __l;
}
// pre: __l._M_hi == 0
explicit operator uint64_t() const noexcept
{ return _M_lo; }
friend bool operator<(const type& __l, const type& __r) noexcept
{
if (__l._M_hi < __r._M_hi)
return true;
else if (__l._M_hi == __r._M_hi)
return __l._M_lo < __r._M_lo;
else
return false;
}
friend bool operator<=(const type& __l, const type& __r) noexcept
{ return !(__r < __l); }
uint64_t _M_lo;
uint64_t _M_hi;
};
};
#endif
// Assume a != 0, a < m, c < m, x < m.
template<typename _Tp, _Tp __m, _Tp __a, _Tp __c,
bool __big_enough = (!(__m & (__m - 1))
|| (_Tp(-1) - __c) / __a >= __m - 1),
bool __schrage_ok = __m % __a < __m / __a>
struct _Mod
{
static _Tp
__calc(_Tp __x)
{
using _Tp2
= typename _Select_uint_least_t<std::__lg(__a)
+ std::__lg(__m) + 2>::type;
return static_cast<_Tp>((_Tp2(__a) * __x + __c) % __m);
}
};
// Schrage.
template<typename _Tp, _Tp __m, _Tp __a, _Tp __c>
struct _Mod<_Tp, __m, __a, __c, false, true>
{
static _Tp
__calc(_Tp __x);
};
// Special cases:
// - for m == 2^n or m == 0, unsigned integer overflow is safe.
// - a * (m - 1) + c fits in _Tp, there is no overflow.
template<typename _Tp, _Tp __m, _Tp __a, _Tp __c, bool __s>
struct _Mod<_Tp, __m, __a, __c, true, __s>
{
static _Tp
__calc(_Tp __x)
{
_Tp __res = __a * __x + __c;
if (__m)
__res %= __m;
return __res;
}
};
template<typename _Tp, _Tp __m, _Tp __a = 1, _Tp __c = 0>
inline _Tp
__mod(_Tp __x)
{
if constexpr (__a == 0)
return __c;
else // N.B. _Mod must not be instantiated with a == 0
return _Mod<_Tp, __m, __a, __c>::__calc(__x);
}
/*
* An adaptor class for converting the output of any Generator into
* the input for a specific Distribution.
*/
template<typename _Engine, typename _DInputType>
struct _Adaptor
{
static_assert(std::is_floating_point<_DInputType>::value,
"template argument must be a floating point type");
public:
_Adaptor(_Engine& __g)
: _M_g(__g) { }
_DInputType
min() const
{ return _DInputType(0); }
_DInputType
max() const
{ return _DInputType(1); }
/*
* Converts a value generated by the adapted random number generator
* into a value in the input domain for the dependent random number
* distribution.
*/
_DInputType
operator()()
{
return std::generate_canonical<_DInputType,
std::numeric_limits<_DInputType>::digits,
_Engine>(_M_g);
}
private:
_Engine& _M_g;
};
// Detect whether a template argument _Sseq is a valid seed sequence for
// a random number engine _Engine with result type _Res.
// Used to constrain _Engine::_Engine(_Sseq&) and _Engine::seed(_Sseq&)
// as required by [rand.eng.general].
template<typename _Sseq>
using __seed_seq_generate_t = decltype(
std::declval<_Sseq&>().generate(std::declval<uint_least32_t*>(),
std::declval<uint_least32_t*>()));
template<typename _Sseq, typename _Engine, typename _Res,
typename _GenerateCheck = __seed_seq_generate_t<_Sseq>>
using _If_seed_seq_for = _Require<
__not_<is_same<__remove_cvref_t<_Sseq>, _Engine>>,
is_unsigned<typename _Sseq::result_type>,
__not_<is_convertible<_Sseq, _Res>>
>;
#pragma GCC diagnostic pop
} // namespace __detail
/// @endcond
/**
* @addtogroup random_generators Random Number Generators
* @ingroup random
*
* These classes define objects which provide random or pseudorandom
* numbers, either from a discrete or a continuous interval. The
* random number generator supplied as a part of this library are
* all uniform random number generators which provide a sequence of
* random number uniformly distributed over their range.
*
* A number generator is a function object with an operator() that
* takes zero arguments and returns a number.
*
* A compliant random number generator must satisfy the following
* requirements. <table border=1 cellpadding=10 cellspacing=0>
* <caption align=top>Random Number Generator Requirements</caption>
* <tr><td>To be documented.</td></tr> </table>
*
* @{
*/
/**
* @brief A model of a linear congruential random number generator.
*
* A random number generator that produces pseudorandom numbers via
* linear function:
* @f[
* x_{i+1}\leftarrow(ax_{i} + c) \bmod m
* @f]
*
* The template parameter @p _UIntType must be an unsigned integral type
* large enough to store values up to (__m-1). If the template parameter
* @p __m is 0, the modulus @p __m used is
* std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template
* parameters @p __a and @p __c must be less than @p __m.
*
* The size of the state is @f$1@f$.
*
* @headerfile random
* @since C++11
*/
template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
class linear_congruential_engine
{
static_assert(std::is_unsigned<_UIntType>::value,
"result_type must be an unsigned integral type");
static_assert(__m == 0u || (__a < __m && __c < __m),
"template argument substituting __m out of bounds");
template<typename _Sseq>
using _If_seed_seq
= __detail::_If_seed_seq_for<_Sseq, linear_congruential_engine,
_UIntType>;
public:
/** The type of the generated random value. */
typedef _UIntType result_type;
/** The multiplier. */
static constexpr result_type multiplier = __a;
/** An increment. */
static constexpr result_type increment = __c;
/** The modulus. */
static constexpr result_type modulus = __m;
static constexpr result_type default_seed = 1u;
/**
* @brief Constructs a %linear_congruential_engine random number
* generator engine with seed 1.
*/
linear_congruential_engine() : linear_congruential_engine(default_seed)
{ }
/**
* @brief Constructs a %linear_congruential_engine random number
* generator engine with seed @p __s. The default seed value
* is 1.
*
* @param __s The initial seed value.
*/
explicit
linear_congruential_engine(result_type __s)
{ seed(__s); }
/**
* @brief Constructs a %linear_congruential_engine random number
* generator engine seeded from the seed sequence @p __q.
*
* @param __q the seed sequence.
*/
template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
explicit
linear_congruential_engine(_Sseq& __q)
{ seed(__q); }
/**
* @brief Reseeds the %linear_congruential_engine random number generator
* engine sequence to the seed @p __s.
*
* @param __s The new seed.
*/
void
seed(result_type __s = default_seed);
/**
* @brief Reseeds the %linear_congruential_engine random number generator
* engine
* sequence using values from the seed sequence @p __q.
*
* @param __q the seed sequence.
*/
template<typename _Sseq>
_If_seed_seq<_Sseq>
seed(_Sseq& __q);
/**
* @brief Gets the smallest possible value in the output range.
*
* The minimum depends on the @p __c parameter: if it is zero, the
* minimum generated must be > 0, otherwise 0 is allowed.
*/
static constexpr result_type
min()
{ return __c == 0u ? 1u : 0u; }
/**
* @brief Gets the largest possible value in the output range.
*/
static constexpr result_type
max()
{ return __m - 1u; }
/**
* @brief Discard a sequence of random numbers.
*/
void
discard(unsigned long long __z)
{
for (; __z != 0ULL; --__z)
(*this)();
}
/**
* @brief Gets the next random number in the sequence.
*/
result_type
operator()()
{
_M_x = __detail::__mod<_UIntType, __m, __a, __c>(_M_x);
return _M_x;
}
/**
* @brief Compares two linear congruential random number generator
* objects of the same type for equality.
*
* @param __lhs A linear congruential random number generator object.
* @param __rhs Another linear congruential random number generator
* object.
*
* @returns true if the infinite sequences of generated values
* would be equal, false otherwise.
*/
friend bool
operator==(const linear_congruential_engine& __lhs,
const linear_congruential_engine& __rhs)
{ return __lhs._M_x == __rhs._M_x; }
/**
* @brief Writes the textual representation of the state x(i) of x to
* @p __os.
*
* @param __os The output stream.
* @param __lcr A % linear_congruential_engine random number generator.
* @returns __os.
*/
template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
_UIntType1 __m1, typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const std::linear_congruential_engine<_UIntType1,
__a1, __c1, __m1>& __lcr);
/**
* @brief Sets the state of the engine by reading its textual
* representation from @p __is.
*
* The textual representation must have been previously written using
* an output stream whose imbued locale and whose type's template
* specialization arguments _CharT and _Traits were the same as those
* of @p __is.
*
* @param __is The input stream.
* @param __lcr A % linear_congruential_engine random number generator.
* @returns __is.
*/
template<typename _UIntType1, _UIntType1 __a1, _UIntType1 __c1,
_UIntType1 __m1, typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
std::linear_congruential_engine<_UIntType1, __a1,
__c1, __m1>& __lcr);
private:
_UIntType _M_x;
};
#if __cpp_impl_three_way_comparison < 201907L
/**
* @brief Compares two linear congruential random number generator
* objects of the same type for inequality.
*
* @param __lhs A linear congruential random number generator object.
* @param __rhs Another linear congruential random number generator
* object.
*
* @returns true if the infinite sequences of generated values
* would be different, false otherwise.
*/
template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
inline bool
operator!=(const std::linear_congruential_engine<_UIntType, __a,
__c, __m>& __lhs,
const std::linear_congruential_engine<_UIntType, __a,
__c, __m>& __rhs)
{ return !(__lhs == __rhs); }
#endif
/**
* A generalized feedback shift register discrete random number generator.
*
* This algorithm avoids multiplication and division and is designed to be
* friendly to a pipelined architecture. If the parameters are chosen
* correctly, this generator will produce numbers with a very long period and
* fairly good apparent entropy, although still not cryptographically strong.
*
* The best way to use this generator is with the predefined mt19937 class.
*
* This algorithm was originally invented by Makoto Matsumoto and
* Takuji Nishimura.
*
* @tparam __w Word size, the number of bits in each element of
* the state vector.
* @tparam __n The degree of recursion.
* @tparam __m The period parameter.
* @tparam __r The separation point bit index.
* @tparam __a The last row of the twist matrix.
* @tparam __u The first right-shift tempering matrix parameter.
* @tparam __d The first right-shift tempering matrix mask.
* @tparam __s The first left-shift tempering matrix parameter.
* @tparam __b The first left-shift tempering matrix mask.
* @tparam __t The second left-shift tempering matrix parameter.
* @tparam __c The second left-shift tempering matrix mask.
* @tparam __l The second right-shift tempering matrix parameter.
* @tparam __f Initialization multiplier.
*
* @headerfile random
* @since C++11
*/
template<typename _UIntType, size_t __w,
size_t __n, size_t __m, size_t __r,
_UIntType __a, size_t __u, _UIntType __d, size_t __s,
_UIntType __b, size_t __t,
_UIntType __c, size_t __l, _UIntType __f>
class mersenne_twister_engine
{
static_assert(std::is_unsigned<_UIntType>::value,
"result_type must be an unsigned integral type");
static_assert(1u <= __m && __m <= __n,
"template argument substituting __m out of bounds");
static_assert(__r <= __w, "template argument substituting "
"__r out of bound");
static_assert(__u <= __w, "template argument substituting "
"__u out of bound");
static_assert(__s <= __w, "template argument substituting "
"__s out of bound");
static_assert(__t <= __w, "template argument substituting "
"__t out of bound");
static_assert(__l <= __w, "template argument substituting "
"__l out of bound");
static_assert(__w <= std::numeric_limits<_UIntType>::digits,
"template argument substituting __w out of bound");
static_assert(__a <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"template argument substituting __a out of bound");
static_assert(__b <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"template argument substituting __b out of bound");
static_assert(__c <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"template argument substituting __c out of bound");
static_assert(__d <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"template argument substituting __d out of bound");
static_assert(__f <= (__detail::_Shift<_UIntType, __w>::__value - 1),
"template argument substituting __f out of bound");
template<typename _Sseq>
using _If_seed_seq
= __detail::_If_seed_seq_for<_Sseq, mersenne_twister_engine,
_UIntType>;
public:
/** The type of the generated random value. */
typedef _UIntType result_type;
// parameter values
static constexpr size_t word_size = __w;
static constexpr size_t state_size = __n;
static constexpr size_t shift_size = __m;
static constexpr size_t mask_bits = __r;
static constexpr result_type xor_mask = __a;
static constexpr size_t tempering_u = __u;
static constexpr result_type tempering_d = __d;
static constexpr size_t tempering_s = __s;
static constexpr result_type tempering_b = __b;
static constexpr size_t tempering_t = __t;
static constexpr result_type tempering_c = __c;
static constexpr size_t tempering_l = __l;
static constexpr result_type initialization_multiplier = __f;
static constexpr result_type default_seed = 5489u;
// constructors and member functions
mersenne_twister_engine() : mersenne_twister_engine(default_seed) { }
explicit
mersenne_twister_engine(result_type __sd)
{ seed(__sd); }
/**
* @brief Constructs a %mersenne_twister_engine random number generator
* engine seeded from the seed sequence @p __q.
*
* @param __q the seed sequence.
*/
template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
explicit
mersenne_twister_engine(_Sseq& __q)
{ seed(__q); }
void
seed(result_type __sd = default_seed);
template<typename _Sseq>
_If_seed_seq<_Sseq>
seed(_Sseq& __q);
/**
* @brief Gets the smallest possible value in the output range.
*/
static constexpr result_type
min()
{ return 0; }
/**
* @brief Gets the largest possible value in the output range.
*/
static constexpr result_type
max()
{ return __detail::_Shift<_UIntType, __w>::__value - 1; }
/**
* @brief Discard a sequence of random numbers.
*/
void
discard(unsigned long long __z);
result_type
operator()();
/**
* @brief Compares two % mersenne_twister_engine random number generator
* objects of the same type for equality.
*
* @param __lhs A % mersenne_twister_engine random number generator
* object.
* @param __rhs Another % mersenne_twister_engine random number
* generator object.
*
* @returns true if the infinite sequences of generated values
* would be equal, false otherwise.
*/
friend bool
operator==(const mersenne_twister_engine& __lhs,
const mersenne_twister_engine& __rhs)
{ return (std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x)
&& __lhs._M_p == __rhs._M_p); }
/**
* @brief Inserts the current state of a % mersenne_twister_engine
* random number generator engine @p __x into the output stream
* @p __os.
*
* @param __os An output stream.
* @param __x A % mersenne_twister_engine random number generator
* engine.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _UIntType1,
size_t __w1, size_t __n1,
size_t __m1, size_t __r1,
_UIntType1 __a1, size_t __u1,
_UIntType1 __d1, size_t __s1,
_UIntType1 __b1, size_t __t1,
_UIntType1 __c1, size_t __l1, _UIntType1 __f1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
__m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
__l1, __f1>& __x);
/**
* @brief Extracts the current state of a % mersenne_twister_engine
* random number generator engine @p __x from the input stream
* @p __is.
*
* @param __is An input stream.
* @param __x A % mersenne_twister_engine random number generator
* engine.
*
* @returns The input stream with the state of @p __x extracted or in
* an error state.
*/
template<typename _UIntType1,
size_t __w1, size_t __n1,
size_t __m1, size_t __r1,
_UIntType1 __a1, size_t __u1,
_UIntType1 __d1, size_t __s1,
_UIntType1 __b1, size_t __t1,
_UIntType1 __c1, size_t __l1, _UIntType1 __f1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
std::mersenne_twister_engine<_UIntType1, __w1, __n1, __m1,
__r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
__l1, __f1>& __x);
private:
void _M_gen_rand();
_UIntType _M_x[state_size];
size_t _M_p;
};
#if __cpp_impl_three_way_comparison < 201907L
/**
* @brief Compares two % mersenne_twister_engine random number generator
* objects of the same type for inequality.
*
* @param __lhs A % mersenne_twister_engine random number generator
* object.
* @param __rhs Another % mersenne_twister_engine random number
* generator object.
*
* @returns true if the infinite sequences of generated values
* would be different, false otherwise.
*/
template<typename _UIntType, size_t __w,
size_t __n, size_t __m, size_t __r,
_UIntType __a, size_t __u, _UIntType __d, size_t __s,
_UIntType __b, size_t __t,
_UIntType __c, size_t __l, _UIntType __f>
inline bool
operator!=(const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
__r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __lhs,
const std::mersenne_twister_engine<_UIntType, __w, __n, __m,
__r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __rhs)
{ return !(__lhs == __rhs); }
#endif
/**
* @brief The Marsaglia-Zaman generator.
*
* This is a model of a Generalized Fibonacci discrete random number
* generator, sometimes referred to as the SWC generator.
*
* A discrete random number generator that produces pseudorandom
* numbers using:
* @f[
* x_{i}\leftarrow(x_{i - s} - x_{i - r} - carry_{i-1}) \bmod m
* @f]
*
* The size of the state is @f$r@f$
* and the maximum period of the generator is @f$(m^r - m^s - 1)@f$.
*
* @headerfile random
* @since C++11
*/
template<typename _UIntType, size_t __w, size_t __s, size_t __r>
class subtract_with_carry_engine
{
static_assert(std::is_unsigned<_UIntType>::value,
"result_type must be an unsigned integral type");
static_assert(0u < __s && __s < __r,
"0 < s < r");
static_assert(0u < __w && __w <= std::numeric_limits<_UIntType>::digits,
"template argument substituting __w out of bounds");
template<typename _Sseq>
using _If_seed_seq
= __detail::_If_seed_seq_for<_Sseq, subtract_with_carry_engine,
_UIntType>;
public:
/** The type of the generated random value. */
typedef _UIntType result_type;
// parameter values
static constexpr size_t word_size = __w;
static constexpr size_t short_lag = __s;
static constexpr size_t long_lag = __r;
static constexpr uint_least32_t default_seed = 19780503u;
subtract_with_carry_engine() : subtract_with_carry_engine(0u)
{ }
/**
* @brief Constructs an explicitly seeded %subtract_with_carry_engine
* random number generator.
*/
explicit
subtract_with_carry_engine(result_type __sd)
{ seed(__sd); }
/**
* @brief Constructs a %subtract_with_carry_engine random number engine
* seeded from the seed sequence @p __q.
*
* @param __q the seed sequence.
*/
template<typename _Sseq, typename = _If_seed_seq<_Sseq>>
explicit
subtract_with_carry_engine(_Sseq& __q)
{ seed(__q); }
/**
* @brief Seeds the initial state @f$x_0@f$ of the random number
* generator.
*
* N1688[4.19] modifies this as follows. If @p __value == 0,
* sets value to 19780503. In any case, with a linear
* congruential generator lcg(i) having parameters @f$ m_{lcg} =
* 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value
* @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m
* \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$
* set carry to 1, otherwise sets carry to 0.
*/
void
seed(result_type __sd = 0u);
/**
* @brief Seeds the initial state @f$x_0@f$ of the
* % subtract_with_carry_engine random number generator.
*/
template<typename _Sseq>
_If_seed_seq<_Sseq>
seed(_Sseq& __q);
/**
* @brief Gets the inclusive minimum value of the range of random
* integers returned by this generator.
*/
static constexpr result_type
min()
{ return 0; }
/**
* @brief Gets the inclusive maximum value of the range of random
* integers returned by this generator.
*/
static constexpr result_type
max()
{ return __detail::_Shift<_UIntType, __w>::__value - 1; }
/**
* @brief Discard a sequence of random numbers.
*/
void
discard(unsigned long long __z)
{
for (; __z != 0ULL; --__z)
(*this)();
}
/**
* @brief Gets the next random number in the sequence.
*/
result_type
operator()();
/**
* @brief Compares two % subtract_with_carry_engine random number
* generator objects of the same type for equality.
*
* @param __lhs A % subtract_with_carry_engine random number generator
* object.
* @param __rhs Another % subtract_with_carry_engine random number
* generator object.
*
* @returns true if the infinite sequences of generated values
* would be equal, false otherwise.
*/
friend bool
operator==(const subtract_with_carry_engine& __lhs,
const subtract_with_carry_engine& __rhs)
{ return (std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x)
&& __lhs._M_carry == __rhs._M_carry
&& __lhs._M_p == __rhs._M_p); }
/**
* @brief Inserts the current state of a % subtract_with_carry_engine
* random number generator engine @p __x into the output stream
* @p __os.
*
* @param __os An output stream.
* @param __x A % subtract_with_carry_engine random number generator
* engine.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const std::subtract_with_carry_engine<_UIntType1, __w1,
__s1, __r1>& __x);
/**
* @brief Extracts the current state of a % subtract_with_carry_engine
* random number generator engine @p __x from the input stream
* @p __is.
*
* @param __is An input stream.
* @param __x A % subtract_with_carry_engine random number generator
* engine.
*
* @returns The input stream with the state of @p __x extracted or in
* an error state.
*/
template<typename _UIntType1, size_t __w1, size_t __s1, size_t __r1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
std::subtract_with_carry_engine<_UIntType1, __w1,
__s1, __r1>& __x);
private:
/// The state of the generator. This is a ring buffer.
_UIntType _M_x[long_lag];
_UIntType _M_carry; ///< The carry
size_t _M_p; ///< Current index of x(i - r).
};
#if __cpp_impl_three_way_comparison < 201907L
/**
* @brief Compares two % subtract_with_carry_engine random number
* generator objects of the same type for inequality.
*
* @param __lhs A % subtract_with_carry_engine random number generator
* object.
* @param __rhs Another % subtract_with_carry_engine random number
* generator object.
*
* @returns true if the infinite sequences of generated values
* would be different, false otherwise.
*/
template<typename _UIntType, size_t __w, size_t __s, size_t __r>
inline bool
operator!=(const std::subtract_with_carry_engine<_UIntType, __w,
__s, __r>& __lhs,
const std::subtract_with_carry_engine<_UIntType, __w,
__s, __r>& __rhs)
{ return !(__lhs == __rhs); }
#endif
/**