blob: 811bc26e179420b5ee8f4e4ebe71df439afca11d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_RANDOM
12#define _LIBCPP_RANDOM
13
14/*
15 random synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22// Engines
23
24template <class UIntType, UIntType a, UIntType c, UIntType m>
25class linear_congruential_engine
26{
27public:
28 // types
29 typedef UIntType result_type;
30
31 // engine characteristics
32 static constexpr result_type multiplier = a;
33 static constexpr result_type increment = c;
34 static constexpr result_type modulus = m;
35 static constexpr result_type min() { return c == 0u ? 1u: 0u;}
36 static constexpr result_type max() { return m - 1u;}
37 static constexpr result_type default_seed = 1u;
38
39 // constructors and seeding functions
40 explicit linear_congruential_engine(result_type s = default_seed);
41 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
42 void seed(result_type s = default_seed);
43 template<class Sseq> void seed(Sseq& q);
44
45 // generating functions
46 result_type operator()();
47 void discard(unsigned long long z);
48};
49
50template <class UIntType, UIntType a, UIntType c, UIntType m>
51bool
52operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
53 const linear_congruential_engine<UIntType, a, c, m>& y);
54
55template <class UIntType, UIntType a, UIntType c, UIntType m>
56bool
57operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
58 const linear_congruential_engine<UIntType, a, c, m>& y);
59
60template <class charT, class traits,
61 class UIntType, UIntType a, UIntType c, UIntType m>
62basic_ostream<charT, traits>&
63operator<<(basic_ostream<charT, traits>& os,
64 const linear_congruential_engine<UIntType, a, c, m>& x);
65
66template <class charT, class traits,
67 class UIntType, UIntType a, UIntType c, UIntType m>
68basic_istream<charT, traits>&
69operator>>(basic_istream<charT, traits>& is,
70 linear_congruential_engine<UIntType, a, c, m>& x);
71
72template <class UIntType, size_t w, size_t n, size_t m, size_t r,
73 UIntType a, size_t u, UIntType d, size_t s,
74 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
75class mersenne_twister_engine
76{
77public:
78 // types
79 typedef UIntType result_type;
80
81 // engine characteristics
82 static constexpr size_t word_size = w;
83 static constexpr size_t state_size = n;
84 static constexpr size_t shift_size = m;
85 static constexpr size_t mask_bits = r;
86 static constexpr result_type xor_mask = a;
87 static constexpr size_t tempering_u = u;
88 static constexpr result_type tempering_d = d;
89 static constexpr size_t tempering_s = s;
90 static constexpr result_type tempering_b = b;
91 static constexpr size_t tempering_t = t;
92 static constexpr result_type tempering_c = c;
93 static constexpr size_t tempering_l = l;
94 static constexpr result_type initialization_multiplier = f;
95 static constexpr result_type min () { return 0; }
96 static constexpr result_type max() { return 2^w - 1; }
97 static constexpr result_type default_seed = 5489u;
98
99 // constructors and seeding functions
100 explicit mersenne_twister_engine(result_type value = default_seed);
101 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
102 void seed(result_type value = default_seed);
103 template<class Sseq> void seed(Sseq& q);
104
105 // generating functions
106 result_type operator()();
107 void discard(unsigned long long z);
108};
109
110template <class UIntType, size_t w, size_t n, size_t m, size_t r,
111 UIntType a, size_t u, UIntType d, size_t s,
112 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
113bool
114operator==(
115 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
116 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
117
118template <class UIntType, size_t w, size_t n, size_t m, size_t r,
119 UIntType a, size_t u, UIntType d, size_t s,
120 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
121bool
122operator!=(
123 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
124 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
125
126template <class charT, class traits,
127 class UIntType, size_t w, size_t n, size_t m, size_t r,
128 UIntType a, size_t u, UIntType d, size_t s,
129 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
130basic_ostream<charT, traits>&
131operator<<(basic_ostream<charT, traits>& os,
132 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
133
134template <class charT, class traits,
135 class UIntType, size_t w, size_t n, size_t m, size_t r,
136 UIntType a, size_t u, UIntType d, size_t s,
137 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
138basic_istream<charT, traits>&
139operator>>(basic_istream<charT, traits>& is,
140 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
141
142template<class UIntType, size_t w, size_t s, size_t r>
143class subtract_with_carry_engine
144{
145public:
146 // types
147 typedef UIntType result_type;
148
149 // engine characteristics
150 static constexpr size_t word_size = w;
151 static constexpr size_t short_lag = s;
152 static constexpr size_t long_lag = r;
153 static constexpr result_type min() { return 0; }
154 static constexpr result_type max() { return m-1; }
155 static constexpr result_type default_seed = 19780503u;
156
157 // constructors and seeding functions
158 explicit subtract_with_carry_engine(result_type value = default_seed);
159 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
160 void seed(result_type value = default_seed);
161 template<class Sseq> void seed(Sseq& q);
162
163 // generating functions
164 result_type operator()();
165 void discard(unsigned long long z);
166};
167
168template<class UIntType, size_t w, size_t s, size_t r>
169bool
170operator==(
171 const subtract_with_carry_engine<UIntType, w, s, r>& x,
172 const subtract_with_carry_engine<UIntType, w, s, r>& y);
173
174template<class UIntType, size_t w, size_t s, size_t r>
175bool
176operator!=(
177 const subtract_with_carry_engine<UIntType, w, s, r>& x,
178 const subtract_with_carry_engine<UIntType, w, s, r>& y);
179
180template <class charT, class traits,
181 class UIntType, size_t w, size_t s, size_t r>
182basic_ostream<charT, traits>&
183operator<<(basic_ostream<charT, traits>& os,
184 const subtract_with_carry_engine<UIntType, w, s, r>& x);
185
186template <class charT, class traits,
187 class UIntType, size_t w, size_t s, size_t r>
188basic_istream<charT, traits>&
189operator>>(basic_istream<charT, traits>& is,
190 subtract_with_carry_engine<UIntType, w, s, r>& x);
191
192template<class Engine, size_t p, size_t r>
193class discard_block_engine
194{
195public:
196 // types
197 typedef typename Engine::result_type result_type;
198
199 // engine characteristics
200 static constexpr size_t block_size = p;
201 static constexpr size_t used_block = r;
202 static constexpr result_type min() { return Engine::min(); }
203 static constexpr result_type max() { return Engine::max(); }
204
205 // constructors and seeding functions
206 discard_block_engine();
207 explicit discard_block_engine(const Engine& e);
208 explicit discard_block_engine(Engine&& e);
209 explicit discard_block_engine(result_type s);
210 template<class Sseq> explicit discard_block_engine(Sseq& q);
211 void seed();
212 void seed(result_type s);
213 template<class Sseq> void seed(Sseq& q);
214
215 // generating functions
216 result_type operator()();
217 void discard(unsigned long long z);
218
219 // property functions
220 const Engine& base() const;
221};
222
223template<class Engine, size_t p, size_t r>
224bool
225operator==(
226 const discard_block_engine<Engine, p, r>& x,
227 const discard_block_engine<Engine, p, r>& y);
228
229template<class Engine, size_t p, size_t r>
230bool
231operator!=(
232 const discard_block_engine<Engine, p, r>& x,
233 const discard_block_engine<Engine, p, r>& y);
234
235template <class charT, class traits,
236 class Engine, size_t p, size_t r>
237basic_ostream<charT, traits>&
238operator<<(basic_ostream<charT, traits>& os,
239 const discard_block_engine<Engine, p, r>& x);
240
241template <class charT, class traits,
242 class Engine, size_t p, size_t r>
243basic_istream<charT, traits>&
244operator>>(basic_istream<charT, traits>& is,
245 discard_block_engine<Engine, p, r>& x);
246
247template<class Engine, size_t w, class UIntType>
248class independent_bits_engine
249{
250public:
251 // types
252 typedef UIntType result_type;
253
254 // engine characteristics
255 static constexpr result_type min() { return 0; }
256 static constexpr result_type max() { return 2^w - 1; }
257
258 // constructors and seeding functions
259 independent_bits_engine();
260 explicit independent_bits_engine(const Engine& e);
261 explicit independent_bits_engine(Engine&& e);
262 explicit independent_bits_engine(result_type s);
263 template<class Sseq> explicit independent_bits_engine(Sseq& q);
264 void seed();
265 void seed(result_type s);
266 template<class Sseq> void seed(Sseq& q);
267
268 // generating functions
269 result_type operator()(); void discard(unsigned long long z);
270
271 // property functions
272 const Engine& base() const;
273};
274
275template<class Engine, size_t w, class UIntType>
276bool
277operator==(
278 const independent_bits_engine<Engine, w, UIntType>& x,
279 const independent_bits_engine<Engine, w, UIntType>& y);
280
281template<class Engine, size_t w, class UIntType>
282bool
283operator!=(
284 const independent_bits_engine<Engine, w, UIntType>& x,
285 const independent_bits_engine<Engine, w, UIntType>& y);
286
287template <class charT, class traits,
288 class Engine, size_t w, class UIntType>
289basic_ostream<charT, traits>&
290operator<<(basic_ostream<charT, traits>& os,
291 const independent_bits_engine<Engine, w, UIntType>& x);
292
293template <class charT, class traits,
294 class Engine, size_t w, class UIntType>
295basic_istream<charT, traits>&
296operator>>(basic_istream<charT, traits>& is,
297 independent_bits_engine<Engine, w, UIntType>& x);
298
299template<class Engine, size_t k>
300class shuffle_order_engine
301{
302public:
303 // types
304 typedef typename Engine::result_type result_type;
305
306 // engine characteristics
307 static constexpr size_t table_size = k;
308 static constexpr result_type min() { return Engine::min; }
309 static constexpr result_type max() { return Engine::max; }
310
311 // constructors and seeding functions
312 shuffle_order_engine();
313 explicit shuffle_order_engine(const Engine& e);
314 explicit shuffle_order_engine(Engine&& e);
315 explicit shuffle_order_engine(result_type s);
316 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
317 void seed();
318 void seed(result_type s);
319 template<class Sseq> void seed(Sseq& q);
320
321 // generating functions
322 result_type operator()();
323 void discard(unsigned long long z);
324
325 // property functions
326 const Engine& base() const;
327};
328
329template<class Engine, size_t k>
330bool
331operator==(
332 const shuffle_order_engine<Engine, k>& x,
333 const shuffle_order_engine<Engine, k>& y);
334
335template<class Engine, size_t k>
336bool
337operator!=(
338 const shuffle_order_engine<Engine, k>& x,
339 const shuffle_order_engine<Engine, k>& y);
340
341template <class charT, class traits,
342 class Engine, size_t k>
343basic_ostream<charT, traits>&
344operator<<(basic_ostream<charT, traits>& os,
345 const shuffle_order_engine<Engine, k>& x);
346
347template <class charT, class traits,
348 class Engine, size_t k>
349basic_istream<charT, traits>&
350operator>>(basic_istream<charT, traits>& is,
351 shuffle_order_engine<Engine, k>& x);
352
353typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
354 minstd_rand0;
355typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
356 minstd_rand;
357typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
358 0x9908b0df,
359 11, 0xffffffff,
360 7, 0x9d2c5680,
361 15, 0xefc60000,
362 18, 1812433253> mt19937;
363typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
364 0xb5026f5aa96619e9,
365 29, 0x5555555555555555,
366 17, 0x71d67fffeda60000,
367 37, 0xfff7eee000000000,
368 43, 6364136223846793005> mt19937_64;
369typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
370typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
371typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
372typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
373typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
374typedef minstd_rand0 default_random_engine;
375
376// Generators
377
378class random_device
379{
380public:
381 // types
382 typedef unsigned int result_type;
383
384 // generator characteristics
385 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
386 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
387
388 // constructors
389 explicit random_device(const string& token = "/dev/urandom");
390
391 // generating functions
392 result_type operator()();
393
394 // property functions
395 double entropy() const;
396
397 // no copy functions
398 random_device(const random_device& ) = delete;
399 void operator=(const random_device& ) = delete;
400};
401
402// Utilities
403
404class seed_seq
405{
406public:
407 // types
408 typedef uint_least32_t result_type;
409
410 // constructors
411 seed_seq();
412 template<class T>
413 seed_seq(initializer_list<T> il);
414 template<class InputIterator>
415 seed_seq(InputIterator begin, InputIterator end);
416
417 // generating functions
418 template<class RandomAccessIterator>
419 void generate(RandomAccessIterator begin, RandomAccessIterator end);
420
421 // property functions
422 size_t size() const;
423 template<class OutputIterator>
424 void param(OutputIterator dest) const;
425
426 // no copy functions
427 seed_seq(const seed_seq&) = delete;
428 void operator=(const seed_seq& ) = delete;
429};
430
431template<class RealType, size_t bits, class URNG>
432 RealType generate_canonical(URNG& g);
433
434// Distributions
435
436template<class IntType = int>
437class uniform_int_distribution
438{
439public:
440 // types
441 typedef IntType result_type;
442
443 class param_type
444 {
445 public:
446 typedef uniform_int_distribution distribution_type;
447
448 explicit param_type(IntType a = 0,
449 IntType b = numeric_limits<IntType>::max());
450
451 result_type a() const;
452 result_type b() const;
453
454 friend bool operator==(const param_type& x, const param_type& y);
455 friend bool operator!=(const param_type& x, const param_type& y);
456 };
457
458 // constructors and reset functions
459 explicit uniform_int_distribution(IntType a = 0,
460 IntType b = numeric_limits<IntType>::max());
461 explicit uniform_int_distribution(const param_type& parm);
462 void reset();
463
464 // generating functions
465 template<class URNG> result_type operator()(URNG& g);
466 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
467
468 // property functions
469 result_type a() const;
470 result_type b() const;
471
472 param_type param() const;
473 void param(const param_type& parm);
474
475 result_type min() const;
476 result_type max() const;
477
478 friend bool operator==(const uniform_int_distribution& x,
479 const uniform_int_distribution& y);
480 friend bool operator!=(const uniform_int_distribution& x,
481 const uniform_int_distribution& y);
482
483 template <class charT, class traits>
484 friend
485 basic_ostream<charT, traits>&
486 operator<<(basic_ostream<charT, traits>& os,
487 const uniform_int_distribution& x);
488
489 template <class charT, class traits>
490 friend
491 basic_istream<charT, traits>&
492 operator>>(basic_istream<charT, traits>& is,
493 uniform_int_distribution& x);
494};
495
496template<class RealType = double>
497class uniform_real_distribution
498{
499public:
500 // types
501 typedef RealType result_type;
502
503 class param_type
504 {
505 public:
506 typedef uniform_real_distribution distribution_type;
507
508 explicit param_type(RealType a = 0,
509 RealType b = 1);
510
511 result_type a() const;
512 result_type b() const;
513
514 friend bool operator==(const param_type& x, const param_type& y);
515 friend bool operator!=(const param_type& x, const param_type& y);
516 };
517
518 // constructors and reset functions
519 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
520 explicit uniform_real_distribution(const param_type& parm);
521 void reset();
522
523 // generating functions
524 template<class URNG> result_type operator()(URNG& g);
525 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
526
527 // property functions
528 result_type a() const;
529 result_type b() const;
530
531 param_type param() const;
532 void param(const param_type& parm);
533
534 result_type min() const;
535 result_type max() const;
536
537 friend bool operator==(const uniform_real_distribution& x,
538 const uniform_real_distribution& y);
539 friend bool operator!=(const uniform_real_distribution& x,
540 const uniform_real_distribution& y);
541
542 template <class charT, class traits>
543 friend
544 basic_ostream<charT, traits>&
545 operator<<(basic_ostream<charT, traits>& os,
546 const uniform_real_distribution& x);
547
548 template <class charT, class traits>
549 friend
550 basic_istream<charT, traits>&
551 operator>>(basic_istream<charT, traits>& is,
552 uniform_real_distribution& x);
553};
554
555class bernoulli_distribution
556{
557public:
558 // types
559 typedef bool result_type;
560
561 class param_type
562 {
563 public:
564 typedef bernoulli_distribution distribution_type;
565
566 explicit param_type(double p = 0.5);
567
568 double p() const;
569
570 friend bool operator==(const param_type& x, const param_type& y);
571 friend bool operator!=(const param_type& x, const param_type& y);
572 };
573
574 // constructors and reset functions
575 explicit bernoulli_distribution(double p = 0.5);
576 explicit bernoulli_distribution(const param_type& parm);
577 void reset();
578
579 // generating functions
580 template<class URNG> result_type operator()(URNG& g);
581 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
582
583 // property functions
584 double p() const;
585
586 param_type param() const;
587 void param(const param_type& parm);
588
589 result_type min() const;
590 result_type max() const;
591
592 friend bool operator==(const bernoulli_distribution& x,
593 const bernoulli_distribution& y);
594 friend bool operator!=(const bernoulli_distribution& x,
595 const bernoulli_distribution& y);
596
597 template <class charT, class traits>
598 friend
599 basic_ostream<charT, traits>&
600 operator<<(basic_ostream<charT, traits>& os,
601 const bernoulli_distribution& x);
602
603 template <class charT, class traits>
604 friend
605 basic_istream<charT, traits>&
606 operator>>(basic_istream<charT, traits>& is,
607 bernoulli_distribution& x);
608};
609
610template<class IntType = int>
611 class binomial_distribution;
612
613template<class IntType = int>
614 class geometric_distribution;
615
616template<class IntType = int>
617 class negative_binomial_distribution;
618
619template<class IntType = int>
620 class poisson_distribution;
621
622template<class RealType = double>
623 class exponential_distribution;
624
625template<class RealType = double>
626 class gamma_distribution;
627
628template<class RealType = double>
629 class weibull_distribution;
630
631template<class RealType = double>
632 class extreme_value_distribution;
633
634template<class RealType = double>
635 class normal_distribution;
636
637template<class RealType = double>
638 class lognormal_distribution;
639
640template<class RealType = double>
641 class chi_squared_distribution;
642
643template<class RealType = double>
644 class cauchy_distribution;
645
646template<class RealType = double>
647 class fisher_f_distribution;
648
649template<class RealType = double>
650 class student_t_distribution;
651
652template<class IntType = int>
653 class discrete_distribution;
654
655template<class RealType = double>
656 class piecewise_constant_distribution;
657
658template<class RealType = double>
659 class piecewise_linear_distribution;
660
661} // std
662*/
663
664#include <__config>
665#include <cstddef>
666#include <type_traits>
667#include <initializer_list>
668#include <cstdint>
669#include <limits>
670#include <algorithm>
671#include <vector>
672#include <string>
673#include <istream>
674#include <ostream>
675
676#pragma GCC system_header
677
678_LIBCPP_BEGIN_NAMESPACE_STD
679
680// linear_congruential_engine
681
682template <unsigned long long __a, unsigned long long __c,
683 unsigned long long __m, unsigned long long _M,
684 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
685struct __lce_ta;
686
687// 64
688
689template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
690struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
691{
692 typedef unsigned long long result_type;
693 static result_type next(result_type __x)
694 {
695 // Schrage's algorithm
696 const result_type __q = __m / __a;
697 const result_type __r = __m % __a;
698 const result_type __t0 = __a * (__x % __q);
699 const result_type __t1 = __r * (__x / __q);
700 __x = __t0 + (__t0 < __t1) * __m - __t1;
701 __x += __c - (__x >= __m - __c) * __m;
702 return __x;
703 }
704};
705
706template <unsigned long long __a, unsigned long long __m>
707struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
708{
709 typedef unsigned long long result_type;
710 static result_type next(result_type __x)
711 {
712 // Schrage's algorithm
713 const result_type __q = __m / __a;
714 const result_type __r = __m % __a;
715 const result_type __t0 = __a * (__x % __q);
716 const result_type __t1 = __r * (__x / __q);
717 __x = __t0 + (__t0 < __t1) * __m - __t1;
718 return __x;
719 }
720};
721
722template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
723struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
724{
725 typedef unsigned long long result_type;
726 static result_type next(result_type __x)
727 {
728 return (__a * __x + __c) % __m;
729 }
730};
731
732template <unsigned long long __a, unsigned long long __c>
733struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
734{
735 typedef unsigned long long result_type;
736 static result_type next(result_type __x)
737 {
738 return __a * __x + __c;
739 }
740};
741
742// 32
743
744template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
745struct __lce_ta<_A, _C, _M, unsigned(~0), true>
746{
747 typedef unsigned result_type;
748 static result_type next(result_type __x)
749 {
750 const result_type __a = static_cast<result_type>(_A);
751 const result_type __c = static_cast<result_type>(_C);
752 const result_type __m = static_cast<result_type>(_M);
753 // Schrage's algorithm
754 const result_type __q = __m / __a;
755 const result_type __r = __m % __a;
756 const result_type __t0 = __a * (__x % __q);
757 const result_type __t1 = __r * (__x / __q);
758 __x = __t0 + (__t0 < __t1) * __m - __t1;
759 __x += __c - (__x >= __m - __c) * __m;
760 return __x;
761 }
762};
763
764template <unsigned long long _A, unsigned long long _M>
765struct __lce_ta<_A, 0, _M, unsigned(~0), true>
766{
767 typedef unsigned result_type;
768 static result_type next(result_type __x)
769 {
770 const result_type __a = static_cast<result_type>(_A);
771 const result_type __m = static_cast<result_type>(_M);
772 // Schrage's algorithm
773 const result_type __q = __m / __a;
774 const result_type __r = __m % __a;
775 const result_type __t0 = __a * (__x % __q);
776 const result_type __t1 = __r * (__x / __q);
777 __x = __t0 + (__t0 < __t1) * __m - __t1;
778 return __x;
779 }
780};
781
782template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
783struct __lce_ta<_A, _C, _M, unsigned(~0), false>
784{
785 typedef unsigned result_type;
786 static result_type next(result_type __x)
787 {
788 const result_type __a = static_cast<result_type>(_A);
789 const result_type __c = static_cast<result_type>(_C);
790 const result_type __m = static_cast<result_type>(_M);
791 return (__a * __x + __c) % __m;
792 }
793};
794
795template <unsigned long long _A, unsigned long long _C>
796struct __lce_ta<_A, _C, 0, unsigned(~0), false>
797{
798 typedef unsigned result_type;
799 static result_type next(result_type __x)
800 {
801 const result_type __a = static_cast<result_type>(_A);
802 const result_type __c = static_cast<result_type>(_C);
803 return __a * __x + __c;
804 }
805};
806
807// 16
808
809template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
810struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
811{
812 typedef unsigned short result_type;
813 static result_type next(result_type __x)
814 {
815 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
816 }
817};
818
819template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
820class linear_congruential_engine;
821
822template <class _CharT, class _Traits,
823 class _U, _U _A, _U _C, _U _N>
824basic_ostream<_CharT, _Traits>&
825operator<<(basic_ostream<_CharT, _Traits>& __os,
826 const linear_congruential_engine<_U, _A, _C, _N>&);
827
828template <class _CharT, class _Traits,
829 class _U, _U _A, _U _C, _U _N>
830basic_istream<_CharT, _Traits>&
831operator>>(basic_istream<_CharT, _Traits>& __is,
832 linear_congruential_engine<_U, _A, _C, _N>& __x);
833
834template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
835class linear_congruential_engine
836{
837public:
838 // types
839 typedef _UIntType result_type;
840
841private:
842 result_type __x_;
843
844 static const result_type _M = result_type(~0);
845
846 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
847 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
848public:
849 static const result_type _Min = __c == 0u ? 1u: 0u;
850 static const result_type _Max = __m - 1u;
851 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
852
853 // engine characteristics
854 static const/*expr*/ result_type multiplier = __a;
855 static const/*expr*/ result_type increment = __c;
856 static const/*expr*/ result_type modulus = __m;
857 static const/*expr*/ result_type min() {return _Min;}
858 static const/*expr*/ result_type max() {return _Max;}
859 static const/*expr*/ result_type default_seed = 1u;
860
861 // constructors and seeding functions
862 explicit linear_congruential_engine(result_type __s = default_seed)
863 {seed(__s);}
864 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
865 {seed(__q);}
866 void seed(result_type __s = default_seed)
867 {seed(integral_constant<bool, __m == 0>(),
868 integral_constant<bool, __c == 0>(), __s);}
869 template<class _Sseq>
870 typename enable_if
871 <
872 !is_convertible<_Sseq, result_type>::value,
873 void
874 >::type
875 seed(_Sseq& __q)
876 {__seed(__q, integral_constant<unsigned,
877 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
878 : (__m-1) / 0x100000000ull)>());}
879
880 // generating functions
881 result_type operator()()
882 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
883 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
884
885 friend bool operator==(const linear_congruential_engine& __x,
886 const linear_congruential_engine& __y)
887 {return __x.__x_ == __y.__x_;}
888 friend bool operator!=(const linear_congruential_engine& __x,
889 const linear_congruential_engine& __y)
890 {return !(__x == __y);}
891
892private:
893
894 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
895 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
896 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
897 1 : __s % __m;}
898 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
899
900 template<class _Sseq>
901 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
902 template<class _Sseq>
903 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
904
905 template <class _CharT, class _Traits,
906 class _U, _U _A, _U _C, _U _N>
907 friend
908 basic_ostream<_CharT, _Traits>&
909 operator<<(basic_ostream<_CharT, _Traits>& __os,
910 const linear_congruential_engine<_U, _A, _C, _N>&);
911
912 template <class _CharT, class _Traits,
913 class _U, _U _A, _U _C, _U _N>
914 friend
915 basic_istream<_CharT, _Traits>&
916 operator>>(basic_istream<_CharT, _Traits>& __is,
917 linear_congruential_engine<_U, _A, _C, _N>& __x);
918};
919
920template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
921template<class _Sseq>
922void
923linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
924 integral_constant<unsigned, 1>)
925{
926 const unsigned __k = 1;
927 uint32_t __ar[__k+3];
928 __q.generate(__ar, __ar + __k + 3);
929 result_type __s = static_cast<result_type>(__ar[3] % __m);
930 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
931}
932
933template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
934template<class _Sseq>
935void
936linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
937 integral_constant<unsigned, 2>)
938{
939 const unsigned __k = 2;
940 uint32_t __ar[__k+3];
941 __q.generate(__ar, __ar + __k + 3);
942 result_type __s = static_cast<result_type>((__ar[3] +
943 (uint64_t)__ar[4] << 32) % __m);
944 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
945}
946
947template <class _CharT, class _Traits>
948class __save_flags
949{
950 typedef basic_ios<_CharT, _Traits> __stream_type;
951 typedef typename __stream_type::fmtflags fmtflags;
952
953 __stream_type& __stream_;
954 fmtflags __fmtflags_;
955 _CharT __fill_;
956
957 __save_flags(const __save_flags&);
958 __save_flags& operator=(const __save_flags&);
959public:
960 explicit __save_flags(__stream_type& __stream)
961 : __stream_(__stream),
962 __fmtflags_(__stream.flags()),
963 __fill_(__stream.fill())
964 {}
965 ~__save_flags()
966 {
967 __stream_.flags(__fmtflags_);
968 __stream_.fill(__fill_);
969 }
970};
971
972template <class _CharT, class _Traits,
973 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
974inline
975basic_ostream<_CharT, _Traits>&
976operator<<(basic_ostream<_CharT, _Traits>& __os,
977 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
978{
979 __save_flags<_CharT, _Traits> _(__os);
980 __os.flags(ios_base::dec | ios_base::left);
981 __os.fill(__os.widen(' '));
982 return __os << __x.__x_;
983}
984
985template <class _CharT, class _Traits,
986 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
987basic_istream<_CharT, _Traits>&
988operator>>(basic_istream<_CharT, _Traits>& __is,
989 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
990{
991 __save_flags<_CharT, _Traits> _(__is);
992 __is.flags(ios_base::dec | ios_base::skipws);
993 _UIntType __t;
994 __is >> __t;
995 if (!__is.fail())
996 __x.__x_ = __t;
997 return __is;
998}
999
1000typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1001 minstd_rand0;
1002typedef minstd_rand0 default_random_engine;
1003typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1004 minstd_rand;
1005// mersenne_twister_engine
1006
1007template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1008 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1009 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1010class mersenne_twister_engine;
1011
1012template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1013 _UI _A, size_t _U, _UI _D, size_t _S,
1014 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1015bool
1016operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1017 _B, _T, _C, _L, _F>& __x,
1018 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1019 _B, _T, _C, _L, _F>& __y);
1020
1021template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1022 _UI _A, size_t _U, _UI _D, size_t _S,
1023 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1024bool
1025operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1026 _B, _T, _C, _L, _F>& __x,
1027 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1028 _B, _T, _C, _L, _F>& __y);
1029
1030template <class _CharT, class _Traits,
1031 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1032 _UI _A, size_t _U, _UI _D, size_t _S,
1033 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1034basic_ostream<_CharT, _Traits>&
1035operator<<(basic_ostream<_CharT, _Traits>& __os,
1036 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1037 _B, _T, _C, _L, _F>& __x);
1038
1039template <class _CharT, class _Traits,
1040 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1041 _UI _A, size_t _U, _UI _D, size_t _S,
1042 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1043basic_istream<_CharT, _Traits>&
1044operator>>(basic_istream<_CharT, _Traits>& __is,
1045 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1046 _B, _T, _C, _L, _F>& __x);
1047
1048template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1049 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1050 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1051class mersenne_twister_engine
1052{
1053public:
1054 // types
1055 typedef _UIntType result_type;
1056
1057private:
1058 result_type __x_[__n];
1059 size_t __i_;
1060
1061 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1062 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1063 static const result_type _Dt = numeric_limits<result_type>::digits;
1064 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1065 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1066 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1067 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1068 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1069 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1070 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1071public:
1072 static const result_type _Min = 0;
1073 static const result_type _Max = __w == _Dt ? result_type(~0) :
1074 (result_type(1) << __w) - result_type(1);
1075 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1076 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1077 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1078 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1079 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1080 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1081
1082 // engine characteristics
1083 static const/*expr*/ size_t word_size = __w;
1084 static const/*expr*/ size_t state_size = __n;
1085 static const/*expr*/ size_t shift_size = __m;
1086 static const/*expr*/ size_t mask_bits = __r;
1087 static const/*expr*/ result_type xor_mask = __a;
1088 static const/*expr*/ size_t tempering_u = __u;
1089 static const/*expr*/ result_type tempering_d = __d;
1090 static const/*expr*/ size_t tempering_s = __s;
1091 static const/*expr*/ result_type tempering_b = __b;
1092 static const/*expr*/ size_t tempering_t = __t;
1093 static const/*expr*/ result_type tempering_c = __c;
1094 static const/*expr*/ size_t tempering_l = __l;
1095 static const/*expr*/ result_type initialization_multiplier = __f;
1096 static const/*expr*/ result_type min() { return _Min; }
1097 static const/*expr*/ result_type max() { return _Max; }
1098 static const/*expr*/ result_type default_seed = 5489u;
1099
1100 // constructors and seeding functions
1101 explicit mersenne_twister_engine(result_type __sd = default_seed)
1102 {seed(__sd);}
1103 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
1104 {seed(__q);}
1105 void seed(result_type __sd = default_seed);
1106 template<class _Sseq>
1107 typename enable_if
1108 <
1109 !is_convertible<_Sseq, result_type>::value,
1110 void
1111 >::type
1112 seed(_Sseq& __q)
1113 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1114
1115 // generating functions
1116 result_type operator()();
1117 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1118
1119 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1120 _UI _A, size_t _U, _UI _D, size_t _S,
1121 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1122 friend
1123 bool
1124 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1125 _B, _T, _C, _L, _F>& __x,
1126 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1127 _B, _T, _C, _L, _F>& __y);
1128
1129 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1130 _UI _A, size_t _U, _UI _D, size_t _S,
1131 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1132 friend
1133 bool
1134 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1135 _B, _T, _C, _L, _F>& __x,
1136 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1137 _B, _T, _C, _L, _F>& __y);
1138
1139 template <class _CharT, class _Traits,
1140 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1141 _UI _A, size_t _U, _UI _D, size_t _S,
1142 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1143 friend
1144 basic_ostream<_CharT, _Traits>&
1145 operator<<(basic_ostream<_CharT, _Traits>& __os,
1146 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1147 _B, _T, _C, _L, _F>& __x);
1148
1149 template <class _CharT, class _Traits,
1150 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1151 _UI _A, size_t _U, _UI _D, size_t _S,
1152 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1153 friend
1154 basic_istream<_CharT, _Traits>&
1155 operator>>(basic_istream<_CharT, _Traits>& __is,
1156 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1157 _B, _T, _C, _L, _F>& __x);
1158private:
1159
1160 template<class _Sseq>
1161 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1162 template<class _Sseq>
1163 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1164
1165 template <size_t __count>
1166 static
1167 typename enable_if
1168 <
1169 __count < __w,
1170 result_type
1171 >::type
1172 __lshift(result_type __x) {return (__x << __count) & _Max;}
1173
1174 template <size_t __count>
1175 static
1176 typename enable_if
1177 <
1178 (__count >= __w),
1179 result_type
1180 >::type
1181 __lshift(result_type __x) {return result_type(0);}
1182
1183 template <size_t __count>
1184 static
1185 typename enable_if
1186 <
1187 __count < _Dt,
1188 result_type
1189 >::type
1190 __rshift(result_type __x) {return __x >> __count;}
1191
1192 template <size_t __count>
1193 static
1194 typename enable_if
1195 <
1196 (__count >= _Dt),
1197 result_type
1198 >::type
1199 __rshift(result_type __x) {return result_type(0);}
1200};
1201
1202template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1203 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1204 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1205void
1206mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1207 __t, __c, __l, __f>::seed(result_type __sd)
1208{ // __w >= 2
1209 __x_[0] = __sd & _Max;
1210 for (size_t __i = 1; __i < __n; ++__i)
1211 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
1212 __i_ = 0;
1213}
1214
1215template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1216 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1217 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1218template<class _Sseq>
1219void
1220mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1221 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
1222{
1223 const unsigned __k = 1;
1224 uint32_t __ar[__n * __k];
1225 __q.generate(__ar, __ar + __n * __k);
1226 for (size_t __i = 0; __i < __n; ++__i)
1227 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1228 const result_type __mask = __r == _Dt ? result_type(~0) :
1229 (result_type(1) << __r) - result_type(1);
1230 __i_ = 0;
1231 if ((__x_[0] & ~__mask) == 0)
1232 {
1233 for (size_t __i = 1; __i < __n; ++__i)
1234 if (__x_[__i] != 0)
1235 return;
1236 __x_[0] = _Max;
1237 }
1238}
1239
1240template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1241 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1242 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1243template<class _Sseq>
1244void
1245mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1246 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
1247{
1248 const unsigned __k = 2;
1249 uint32_t __ar[__n * __k];
1250 __q.generate(__ar, __ar + __n * __k);
1251 for (size_t __i = 0; __i < __n; ++__i)
1252 __x_[__i] = static_cast<result_type>(
1253 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1254 const result_type __mask = __r == _Dt ? result_type(~0) :
1255 (result_type(1) << __r) - result_type(1);
1256 __i_ = 0;
1257 if ((__x_[0] & ~__mask) == 0)
1258 {
1259 for (size_t __i = 1; __i < __n; ++__i)
1260 if (__x_[__i] != 0)
1261 return;
1262 __x_[0] = _Max;
1263 }
1264}
1265
1266template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1267 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1268 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1269_UIntType
1270mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1271 __t, __c, __l, __f>::operator()()
1272{
1273 const size_t __j = (__i_ + 1) % __n;
1274 const result_type __mask = __r == _Dt ? result_type(~0) :
1275 (result_type(1) << __r) - result_type(1);
1276 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
1277 const size_t __k = (__i_ + __m) % __n;
1278 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
1279 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
1280 __i_ = __j;
1281 __z ^= __lshift<__s>(__z) & __b;
1282 __z ^= __lshift<__t>(__z) & __c;
1283 return __z ^ __rshift<__l>(__z);
1284}
1285
1286template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1287 _UI _A, size_t _U, _UI _D, size_t _S,
1288 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1289bool
1290operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1291 _B, _T, _C, _L, _F>& __x,
1292 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1293 _B, _T, _C, _L, _F>& __y)
1294{
1295 if (__x.__i_ == __y.__i_)
1296 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
1297 if (__x.__i_ == 0 || __y.__i_ == 0)
1298 {
1299 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
1300 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1301 __y.__x_ + __y.__i_))
1302 return false;
1303 if (__x.__i_ == 0)
1304 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
1305 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
1306 }
1307 if (__x.__i_ < __y.__i_)
1308 {
1309 size_t __j = _N - __y.__i_;
1310 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1311 __y.__x_ + __y.__i_))
1312 return false;
1313 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
1314 __y.__x_))
1315 return false;
1316 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1317 __y.__x_ + (_N - (__x.__i_ + __j)));
1318 }
1319 size_t __j = _N - __x.__i_;
1320 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1321 __x.__x_ + __x.__i_))
1322 return false;
1323 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
1324 __x.__x_))
1325 return false;
1326 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1327 __x.__x_ + (_N - (__y.__i_ + __j)));
1328}
1329
1330template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1331 _UI _A, size_t _U, _UI _D, size_t _S,
1332 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1333inline
1334bool
1335operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1336 _B, _T, _C, _L, _F>& __x,
1337 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1338 _B, _T, _C, _L, _F>& __y)
1339{
1340 return !(__x == __y);
1341}
1342
1343template <class _CharT, class _Traits,
1344 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1345 _UI _A, size_t _U, _UI _D, size_t _S,
1346 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1347basic_ostream<_CharT, _Traits>&
1348operator<<(basic_ostream<_CharT, _Traits>& __os,
1349 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1350 _B, _T, _C, _L, _F>& __x)
1351{
1352 __save_flags<_CharT, _Traits> _(__os);
1353 __os.flags(ios_base::dec | ios_base::left);
1354 _CharT __sp = __os.widen(' ');
1355 __os.fill(__sp);
1356 __os << __x.__x_[__x.__i_];
1357 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
1358 __os << __sp << __x.__x_[__j];
1359 for (size_t __j = 0; __j < __x.__i_; ++__j)
1360 __os << __sp << __x.__x_[__j];
1361 return __os;
1362}
1363
1364template <class _CharT, class _Traits,
1365 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1366 _UI _A, size_t _U, _UI _D, size_t _S,
1367 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1368basic_istream<_CharT, _Traits>&
1369operator>>(basic_istream<_CharT, _Traits>& __is,
1370 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1371 _B, _T, _C, _L, _F>& __x)
1372{
1373 __save_flags<_CharT, _Traits> _(__is);
1374 __is.flags(ios_base::dec | ios_base::skipws);
1375 _UI __t[_N];
1376 for (size_t __i = 0; __i < _N; ++__i)
1377 __is >> __t[__i];
1378 if (!__is.fail())
1379 {
1380 for (size_t __i = 0; __i < _N; ++__i)
1381 __x.__x_[__i] = __t[__i];
1382 __x.__i_ = 0;
1383 }
1384 return __is;
1385}
1386
1387typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
1388 0x9908b0df, 11, 0xffffffff,
1389 7, 0x9d2c5680,
1390 15, 0xefc60000,
1391 18, 1812433253> mt19937;
1392typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
1393 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
1394 17, 0x71d67fffeda60000ULL,
1395 37, 0xfff7eee000000000ULL,
1396 43, 6364136223846793005ULL> mt19937_64;
1397
1398// subtract_with_carry_engine
1399
1400template<class _UIntType, size_t __w, size_t __s, size_t __r>
1401class subtract_with_carry_engine;
1402
1403template<class _UI, size_t _W, size_t _S, size_t _R>
1404bool
1405operator==(
1406 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1407 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1408
1409template<class _UI, size_t _W, size_t _S, size_t _R>
1410bool
1411operator!=(
1412 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1413 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1414
1415template <class _CharT, class _Traits,
1416 class _UI, size_t _W, size_t _S, size_t _R>
1417basic_ostream<_CharT, _Traits>&
1418operator<<(basic_ostream<_CharT, _Traits>& __os,
1419 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1420
1421template <class _CharT, class _Traits,
1422 class _UI, size_t _W, size_t _S, size_t _R>
1423basic_istream<_CharT, _Traits>&
1424operator>>(basic_istream<_CharT, _Traits>& __is,
1425 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1426
1427template<class _UIntType, size_t __w, size_t __s, size_t __r>
1428class subtract_with_carry_engine
1429{
1430public:
1431 // types
1432 typedef _UIntType result_type;
1433
1434private:
1435 result_type __x_[__r];
1436 result_type __c_;
1437 size_t __i_;
1438
1439 static const result_type _Dt = numeric_limits<result_type>::digits;
1440 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
1441 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
1442 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
1443 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
1444public:
1445 static const result_type _Min = 0;
1446 static const result_type _Max = __w == _Dt ? result_type(~0) :
1447 (result_type(1) << __w) - result_type(1);
1448 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
1449
1450 // engine characteristics
1451 static const/*expr*/ size_t word_size = __w;
1452 static const/*expr*/ size_t short_lag = __s;
1453 static const/*expr*/ size_t long_lag = __r;
1454 static const/*expr*/ result_type min() { return _Min; }
1455 static const/*expr*/ result_type max() { return _Max; }
1456 static const/*expr*/ result_type default_seed = 19780503u;
1457
1458 // constructors and seeding functions
1459 explicit subtract_with_carry_engine(result_type __sd = default_seed)
1460 {seed(__sd);}
1461 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
1462 {seed(__q);}
1463 void seed(result_type __sd = default_seed)
1464 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1465 template<class _Sseq>
1466 typename enable_if
1467 <
1468 !is_convertible<_Sseq, result_type>::value,
1469 void
1470 >::type
1471 seed(_Sseq& __q)
1472 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1473
1474 // generating functions
1475 result_type operator()();
1476 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1477
1478 template<class _UI, size_t _W, size_t _S, size_t _R>
1479 friend
1480 bool
1481 operator==(
1482 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1483 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1484
1485 template<class _UI, size_t _W, size_t _S, size_t _R>
1486 friend
1487 bool
1488 operator!=(
1489 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1490 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1491
1492 template <class _CharT, class _Traits,
1493 class _UI, size_t _W, size_t _S, size_t _R>
1494 friend
1495 basic_ostream<_CharT, _Traits>&
1496 operator<<(basic_ostream<_CharT, _Traits>& __os,
1497 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1498
1499 template <class _CharT, class _Traits,
1500 class _UI, size_t _W, size_t _S, size_t _R>
1501 friend
1502 basic_istream<_CharT, _Traits>&
1503 operator>>(basic_istream<_CharT, _Traits>& __is,
1504 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1505
1506private:
1507
1508 void seed(result_type __sd, integral_constant<unsigned, 1>);
1509 void seed(result_type __sd, integral_constant<unsigned, 2>);
1510 template<class _Sseq>
1511 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1512 template<class _Sseq>
1513 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1514};
1515
1516template<class _UIntType, size_t __w, size_t __s, size_t __r>
1517void
1518subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1519 integral_constant<unsigned, 1>)
1520{
1521 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1522 __e(__sd == 0u ? default_seed : __sd);
1523 for (size_t __i = 0; __i < __r; ++__i)
1524 __x_[__i] = static_cast<result_type>(__e() & _Max);
1525 __c_ = __x_[__r-1] == 0;
1526 __i_ = 0;
1527}
1528
1529template<class _UIntType, size_t __w, size_t __s, size_t __r>
1530void
1531subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1532 integral_constant<unsigned, 2>)
1533{
1534 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1535 __e(__sd == 0u ? default_seed : __sd);
1536 for (size_t __i = 0; __i < __r; ++__i)
1537 __x_[__i] = static_cast<result_type>(
1538 (__e() + ((uint64_t)__e() << 32)) & _Max);
1539 __c_ = __x_[__r-1] == 0;
1540 __i_ = 0;
1541}
1542
1543template<class _UIntType, size_t __w, size_t __s, size_t __r>
1544template<class _Sseq>
1545void
1546subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1547 integral_constant<unsigned, 1>)
1548{
1549 const unsigned __k = 1;
1550 uint32_t __ar[__r * __k];
1551 __q.generate(__ar, __ar + __r * __k);
1552 for (size_t __i = 0; __i < __r; ++__i)
1553 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1554 __c_ = __x_[__r-1] == 0;
1555 __i_ = 0;
1556}
1557
1558template<class _UIntType, size_t __w, size_t __s, size_t __r>
1559template<class _Sseq>
1560void
1561subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1562 integral_constant<unsigned, 2>)
1563{
1564 const unsigned __k = 2;
1565 uint32_t __ar[__r * __k];
1566 __q.generate(__ar, __ar + __r * __k);
1567 for (size_t __i = 0; __i < __r; ++__i)
1568 __x_[__i] = static_cast<result_type>(
1569 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1570 __c_ = __x_[__r-1] == 0;
1571 __i_ = 0;
1572}
1573
1574template<class _UIntType, size_t __w, size_t __s, size_t __r>
1575_UIntType
1576subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
1577{
1578 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
1579 result_type& __xr = __x_[__i_];
1580 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
1581 __xr = (__xs - __xr - __c_) & _Max;
1582 __c_ = __new_c;
1583 __i_ = (__i_ + 1) % __r;
1584 return __xr;
1585}
1586
1587template<class _UI, size_t _W, size_t _S, size_t _R>
1588bool
1589operator==(
1590 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1591 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1592{
1593 if (__x.__c_ != __y.__c_)
1594 return false;
1595 if (__x.__i_ == __y.__i_)
1596 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
1597 if (__x.__i_ == 0 || __y.__i_ == 0)
1598 {
1599 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
1600 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1601 __y.__x_ + __y.__i_))
1602 return false;
1603 if (__x.__i_ == 0)
1604 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
1605 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
1606 }
1607 if (__x.__i_ < __y.__i_)
1608 {
1609 size_t __j = _R - __y.__i_;
1610 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1611 __y.__x_ + __y.__i_))
1612 return false;
1613 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
1614 __y.__x_))
1615 return false;
1616 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1617 __y.__x_ + (_R - (__x.__i_ + __j)));
1618 }
1619 size_t __j = _R - __x.__i_;
1620 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1621 __x.__x_ + __x.__i_))
1622 return false;
1623 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
1624 __x.__x_))
1625 return false;
1626 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1627 __x.__x_ + (_R - (__y.__i_ + __j)));
1628}
1629
1630template<class _UI, size_t _W, size_t _S, size_t _R>
1631inline
1632bool
1633operator!=(
1634 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1635 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1636{
1637 return !(__x == __y);
1638}
1639
1640template <class _CharT, class _Traits,
1641 class _UI, size_t _W, size_t _S, size_t _R>
1642basic_ostream<_CharT, _Traits>&
1643operator<<(basic_ostream<_CharT, _Traits>& __os,
1644 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1645{
1646 __save_flags<_CharT, _Traits> _(__os);
1647 __os.flags(ios_base::dec | ios_base::left);
1648 _CharT __sp = __os.widen(' ');
1649 __os.fill(__sp);
1650 __os << __x.__x_[__x.__i_];
1651 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
1652 __os << __sp << __x.__x_[__j];
1653 for (size_t __j = 0; __j < __x.__i_; ++__j)
1654 __os << __sp << __x.__x_[__j];
1655 __os << __sp << __x.__c_;
1656 return __os;
1657}
1658
1659template <class _CharT, class _Traits,
1660 class _UI, size_t _W, size_t _S, size_t _R>
1661basic_istream<_CharT, _Traits>&
1662operator>>(basic_istream<_CharT, _Traits>& __is,
1663 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1664{
1665 __save_flags<_CharT, _Traits> _(__is);
1666 __is.flags(ios_base::dec | ios_base::skipws);
1667 _UI __t[_R+1];
1668 for (size_t __i = 0; __i < _R+1; ++__i)
1669 __is >> __t[__i];
1670 if (!__is.fail())
1671 {
1672 for (size_t __i = 0; __i < _R; ++__i)
1673 __x.__x_[__i] = __t[__i];
1674 __x.__c_ = __t[_R];
1675 __x.__i_ = 0;
1676 }
1677 return __is;
1678}
1679
1680typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
1681typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
1682
1683// discard_block_engine
1684
1685template<class _Engine, size_t __p, size_t __r>
1686class discard_block_engine
1687{
1688 _Engine __e_;
1689 int __n_;
1690
1691 static_assert( 0 < __r, "discard_block_engine invalid parameters");
1692 static_assert(__r <= __p, "discard_block_engine invalid parameters");
1693public:
1694 // types
1695 typedef typename _Engine::result_type result_type;
1696
1697 // engine characteristics
1698 static const/*expr*/ size_t block_size = __p;
1699 static const/*expr*/ size_t used_block = __r;
1700
1701 // Temporary work around for lack of constexpr
1702 static const result_type _Min = _Engine::_Min;
1703 static const result_type _Max = _Engine::_Max;
1704
1705 static const/*expr*/ result_type min() { return _Engine::min(); }
1706 static const/*expr*/ result_type max() { return _Engine::max(); }
1707
1708 // constructors and seeding functions
1709 discard_block_engine() : __n_(0) {}
1710// explicit discard_block_engine(const _Engine& __e);
1711// explicit discard_block_engine(_Engine&& __e);
1712 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
1713 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
1714 : __e_(__q), __n_(0) {}
1715 void seed() {__e_.seed(); __n_ = 0;}
1716 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
1717 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
1718
1719 // generating functions
1720 result_type operator()();
1721 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1722
1723 // property functions
1724 const _Engine& base() const {return __e_;}
1725
1726 template<class _Eng, size_t _P, size_t _R>
1727 friend
1728 bool
1729 operator==(
1730 const discard_block_engine<_Eng, _P, _R>& __x,
1731 const discard_block_engine<_Eng, _P, _R>& __y);
1732
1733 template<class _Eng, size_t _P, size_t _R>
1734 friend
1735 bool
1736 operator!=(
1737 const discard_block_engine<_Eng, _P, _R>& __x,
1738 const discard_block_engine<_Eng, _P, _R>& __y);
1739
1740 template <class _CharT, class _Traits,
1741 class _Eng, size_t _P, size_t _R>
1742 friend
1743 basic_ostream<_CharT, _Traits>&
1744 operator<<(basic_ostream<_CharT, _Traits>& __os,
1745 const discard_block_engine<_Eng, _P, _R>& __x);
1746
1747 template <class _CharT, class _Traits,
1748 class _Eng, size_t _P, size_t _R>
1749 friend
1750 basic_istream<_CharT, _Traits>&
1751 operator>>(basic_istream<_CharT, _Traits>& __is,
1752 discard_block_engine<_Eng, _P, _R>& __x);
1753};
1754
1755template<class _Engine, size_t __p, size_t __r>
1756typename discard_block_engine<_Engine, __p, __r>::result_type
1757discard_block_engine<_Engine, __p, __r>::operator()()
1758{
1759 if (__n_ >= __r)
1760 {
1761 __e_.discard(__p - __r);
1762 __n_ = 0;
1763 }
1764 ++__n_;
1765 return __e_();
1766}
1767
1768template<class _Eng, size_t _P, size_t _R>
1769inline
1770bool
1771operator==(const discard_block_engine<_Eng, _P, _R>& __x,
1772 const discard_block_engine<_Eng, _P, _R>& __y)
1773{
1774 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
1775}
1776
1777template<class _Eng, size_t _P, size_t _R>
1778inline
1779bool
1780operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
1781 const discard_block_engine<_Eng, _P, _R>& __y)
1782{
1783 return !(__x == __y);
1784}
1785
1786template <class _CharT, class _Traits,
1787 class _Eng, size_t _P, size_t _R>
1788basic_ostream<_CharT, _Traits>&
1789operator<<(basic_ostream<_CharT, _Traits>& __os,
1790 const discard_block_engine<_Eng, _P, _R>& __x)
1791{
1792 __save_flags<_CharT, _Traits> _(__os);
1793 __os.flags(ios_base::dec | ios_base::left);
1794 _CharT __sp = __os.widen(' ');
1795 __os.fill(__sp);
1796 return __os << __x.__e_ << __sp << __x.__n_;
1797}
1798
1799template <class _CharT, class _Traits,
1800 class _Eng, size_t _P, size_t _R>
1801basic_istream<_CharT, _Traits>&
1802operator>>(basic_istream<_CharT, _Traits>& __is,
1803 discard_block_engine<_Eng, _P, _R>& __x)
1804{
1805 __save_flags<_CharT, _Traits> _(__is);
1806 __is.flags(ios_base::dec | ios_base::skipws);
1807 _Eng __e;
1808 int __n;
1809 __is >> __e >> __n;
1810 if (!__is.fail())
1811 {
1812 __x.__e_ = __e;
1813 __x.__n_ = __n;
1814 }
1815 return __is;
1816}
1817
1818typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1819typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1820
1821// independent_bits_engine
1822
1823template <unsigned long long _X, size_t _R>
1824struct __log2_imp
1825{
1826 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
1827 : __log2_imp<_X, _R - 1>::value;
1828};
1829
1830template <unsigned long long _X>
1831struct __log2_imp<_X, 0>
1832{
1833 static const size_t value = 0;
1834};
1835
1836template <size_t _R>
1837struct __log2_imp<0, _R>
1838{
1839 static const size_t value = _R + 1;
1840};
1841
1842template <class _UI, _UI _X>
1843struct __log2
1844{
1845 static const size_t value = __log2_imp<_X,
1846 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
1847};
1848
1849template<class _Engine, size_t __w, class _UIntType>
1850class independent_bits_engine
1851{
1852 template <class _UI, _UI _R0, size_t _W, size_t _M>
1853 class __get_n
1854 {
1855 static const size_t _Dt = numeric_limits<_UI>::digits;
1856 static const size_t _N = _W / _M + (_W % _M != 0);
1857 static const size_t _W0 = _W / _N;
1858 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
1859 public:
1860 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
1861 };
1862public:
1863 // types
1864 typedef _UIntType result_type;
1865
1866private:
1867 _Engine __e_;
1868
1869 static const result_type _Dt = numeric_limits<result_type>::digits;
1870 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
1871 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
1872
1873 typedef typename _Engine::result_type _Engine_result_type;
1874 typedef typename conditional
1875 <
1876 sizeof(_Engine_result_type) <= sizeof(result_type),
1877 result_type,
1878 _Engine_result_type
1879 >::type _Working_result_type;
1880 // Temporary work around for lack of constexpr
1881 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
1882 + _Working_result_type(1);
1883 static const size_t __m = __log2<_Working_result_type, _R>::value;
1884 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
1885 static const size_t __w0 = __w / __n;
1886 static const size_t __n0 = __n - __w % __n;
1887 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
1888 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
1889 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
1890 (_R >> __w0) << __w0;
1891 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
1892 (_R >> (__w0+1)) << (__w0+1);
1893 static const _Engine_result_type __mask0 = __w0 > 0 ?
1894 _Engine_result_type(~0) >> (_EDt - __w0) :
1895 _Engine_result_type(0);
1896 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
1897 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
1898 _Engine_result_type(~0);
1899public:
1900 static const result_type _Min = 0;
1901 static const result_type _Max = __w == _Dt ? result_type(~0) :
1902 (result_type(1) << __w) - result_type(1);
1903 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
1904
1905 // engine characteristics
1906 static const/*expr*/ result_type min() { return _Min; }
1907 static const/*expr*/ result_type max() { return _Max; }
1908
1909 // constructors and seeding functions
1910 independent_bits_engine() {}
1911// explicit independent_bits_engine(const _Engine& __e);
1912// explicit independent_bits_engine(_Engine&& __e);
1913 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
1914 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
1915 : __e_(__q) {}
1916 void seed() {__e_.seed();}
1917 void seed(result_type __sd) {__e_.seed(__sd);}
1918 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
1919
1920 // generating functions
1921 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
1922 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1923
1924 // property functions
1925 const _Engine& base() const {return __e_;}
1926
1927 template<class _Eng, size_t _W, class _UI>
1928 friend
1929 bool
1930 operator==(
1931 const independent_bits_engine<_Eng, _W, _UI>& __x,
1932 const independent_bits_engine<_Eng, _W, _UI>& __y);
1933
1934 template<class _Eng, size_t _W, class _UI>
1935 friend
1936 bool
1937 operator!=(
1938 const independent_bits_engine<_Eng, _W, _UI>& __x,
1939 const independent_bits_engine<_Eng, _W, _UI>& __y);
1940
1941 template <class _CharT, class _Traits,
1942 class _Eng, size_t _W, class _UI>
1943 friend
1944 basic_ostream<_CharT, _Traits>&
1945 operator<<(basic_ostream<_CharT, _Traits>& __os,
1946 const independent_bits_engine<_Eng, _W, _UI>& __x);
1947
1948 template <class _CharT, class _Traits,
1949 class _Eng, size_t _W, class _UI>
1950 friend
1951 basic_istream<_CharT, _Traits>&
1952 operator>>(basic_istream<_CharT, _Traits>& __is,
1953 independent_bits_engine<_Eng, _W, _UI>& __x);
1954
1955private:
1956 result_type __eval(false_type);
1957 result_type __eval(true_type);
1958
1959 template <size_t __count>
1960 static
1961 typename enable_if
1962 <
1963 __count < _Dt,
1964 result_type
1965 >::type
1966 __lshift(result_type __x) {return __x << __count;}
1967
1968 template <size_t __count>
1969 static
1970 typename enable_if
1971 <
1972 (__count >= _Dt),
1973 result_type
1974 >::type
1975 __lshift(result_type __x) {return result_type(0);}
1976};
1977
1978template<class _Engine, size_t __w, class _UIntType>
1979inline
1980_UIntType
1981independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
1982{
1983 return static_cast<result_type>(__e_() & __mask0);
1984}
1985
1986template<class _Engine, size_t __w, class _UIntType>
1987_UIntType
1988independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
1989{
1990 result_type _S = 0;
1991 for (size_t __k = 0; __k < __n0; ++__k)
1992 {
1993 _Engine_result_type __u;
1994 do
1995 {
1996 __u = __e_() - _Engine::min();
1997 } while (__u >= __y0);
1998 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
1999 }
2000 for (size_t __k = __n0; __k < __n; ++__k)
2001 {
2002 _Engine_result_type __u;
2003 do
2004 {
2005 __u = __e_() - _Engine::min();
2006 } while (__u >= __y1);
2007 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2008 }
2009 return _S;
2010}
2011
2012template<class _Eng, size_t _W, class _UI>
2013inline
2014bool
2015operator==(
2016 const independent_bits_engine<_Eng, _W, _UI>& __x,
2017 const independent_bits_engine<_Eng, _W, _UI>& __y)
2018{
2019 return __x.base() == __y.base();
2020}
2021
2022template<class _Eng, size_t _W, class _UI>
2023inline
2024bool
2025operator!=(
2026 const independent_bits_engine<_Eng, _W, _UI>& __x,
2027 const independent_bits_engine<_Eng, _W, _UI>& __y)
2028{
2029 return !(__x == __y);
2030}
2031
2032template <class _CharT, class _Traits,
2033 class _Eng, size_t _W, class _UI>
2034basic_ostream<_CharT, _Traits>&
2035operator<<(basic_ostream<_CharT, _Traits>& __os,
2036 const independent_bits_engine<_Eng, _W, _UI>& __x)
2037{
2038 return __os << __x.base();
2039}
2040
2041template <class _CharT, class _Traits,
2042 class _Eng, size_t _W, class _UI>
2043basic_istream<_CharT, _Traits>&
2044operator>>(basic_istream<_CharT, _Traits>& __is,
2045 independent_bits_engine<_Eng, _W, _UI>& __x)
2046{
2047 _Eng __e;
2048 __is >> __e;
2049 if (!__is.fail())
2050 __x.__e_ = __e;
2051 return __is;
2052}
2053
2054// shuffle_order_engine
2055
2056template <uint64_t _Xp, uint64_t _Yp>
2057struct __ugcd
2058{
2059 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2060};
2061
2062template <uint64_t _Xp>
2063struct __ugcd<_Xp, 0>
2064{
2065 static const uint64_t value = _Xp;
2066};
2067
2068template <uint64_t _N, uint64_t _D>
2069class __uratio
2070{
2071 static_assert(_D != 0, "__uratio divide by 0");
2072 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2073public:
2074 static const uint64_t num = _N / __gcd;
2075 static const uint64_t den = _D / __gcd;
2076
2077 typedef __uratio<num, den> type;
2078};
2079
2080template<class _Engine, size_t __k>
2081class shuffle_order_engine
2082{
2083 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2084public:
2085 // types
2086 typedef typename _Engine::result_type result_type;
2087
2088private:
2089 _Engine __e_;
2090 result_type _V_[__k];
2091 result_type _Y_;
2092
2093public:
2094 // engine characteristics
2095 static const/*expr*/ size_t table_size = __k;
2096
2097 static const result_type _Min = _Engine::_Min;
2098 static const result_type _Max = _Engine::_Max;
2099 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2100 static const/*expr*/ result_type min() { return _Min; }
2101 static const/*expr*/ result_type max() { return _Max; }
2102
2103 static const unsigned long long _R = _Max - _Min + 1ull;
2104
2105 // constructors and seeding functions
2106 shuffle_order_engine() {__init();}
2107// explicit shuffle_order_engine(const _Engine& __e);
2108// explicit shuffle_order_engine(_Engine&& e);
2109 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
2110 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
2111 : __e_(__q) {__init();}
2112 void seed() {__e_.seed(); __init();}
2113 void seed(result_type __sd) {__e_.seed(__sd); __init();}
2114 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
2115
2116 // generating functions
2117 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2118 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2119
2120 // property functions
2121 const _Engine& base() const {return __e_;}
2122
2123private:
2124 template<class _Eng, size_t _K>
2125 friend
2126 bool
2127 operator==(
2128 const shuffle_order_engine<_Eng, _K>& __x,
2129 const shuffle_order_engine<_Eng, _K>& __y);
2130
2131 template<class _Eng, size_t _K>
2132 friend
2133 bool
2134 operator!=(
2135 const shuffle_order_engine<_Eng, _K>& __x,
2136 const shuffle_order_engine<_Eng, _K>& __y);
2137
2138 template <class _CharT, class _Traits,
2139 class _Eng, size_t _K>
2140 friend
2141 basic_ostream<_CharT, _Traits>&
2142 operator<<(basic_ostream<_CharT, _Traits>& __os,
2143 const shuffle_order_engine<_Eng, _K>& __x);
2144
2145 template <class _CharT, class _Traits,
2146 class _Eng, size_t _K>
2147 friend
2148 basic_istream<_CharT, _Traits>&
2149 operator>>(basic_istream<_CharT, _Traits>& __is,
2150 shuffle_order_engine<_Eng, _K>& __x);
2151
2152 void __init()
2153 {
2154 for (size_t __i = 0; __i < __k; ++__i)
2155 _V_[__i] = __e_();
2156 _Y_ = __e_();
2157 }
2158
2159 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
2160 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
2161
2162 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
2163 result_type __eval2(true_type) {return __evalf<__k, 0>();}
2164
2165 template <uint64_t _N, uint64_t _D>
2166 typename enable_if
2167 <
2168 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
2169 result_type
2170 >::type
2171 __eval(__uratio<_N, _D>)
2172 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
2173
2174 template <uint64_t _N, uint64_t _D>
2175 typename enable_if
2176 <
2177 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
2178 result_type
2179 >::type
2180 __eval(__uratio<_N, _D>)
2181 {
2182 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
2183 / __uratio<_N, _D>::den);
2184 _Y_ = _V_[__j];
2185 _V_[__j] = __e_();
2186 return _Y_;
2187 }
2188
2189 template <uint64_t __n, uint64_t __d>
2190 result_type __evalf()
2191 {
2192 const double _F = __d == 0 ?
2193 __n / (2. * 0x8000000000000000ull) :
2194 __n / (double)__d;
2195 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
2196 _Y_ = _V_[__j];
2197 _V_[__j] = __e_();
2198 return _Y_;
2199 }
2200};
2201
2202template<class _Eng, size_t _K>
2203bool
2204operator==(
2205 const shuffle_order_engine<_Eng, _K>& __x,
2206 const shuffle_order_engine<_Eng, _K>& __y)
2207{
2208 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
2209 __x.__e_ == __y.__e_;
2210}
2211
2212template<class _Eng, size_t _K>
2213inline
2214bool
2215operator!=(
2216 const shuffle_order_engine<_Eng, _K>& __x,
2217 const shuffle_order_engine<_Eng, _K>& __y)
2218{
2219 return !(__x == __y);
2220}
2221
2222template <class _CharT, class _Traits,
2223 class _Eng, size_t _K>
2224basic_ostream<_CharT, _Traits>&
2225operator<<(basic_ostream<_CharT, _Traits>& __os,
2226 const shuffle_order_engine<_Eng, _K>& __x)
2227{
2228 __save_flags<_CharT, _Traits> _(__os);
2229 __os.flags(ios_base::dec | ios_base::left);
2230 _CharT __sp = __os.widen(' ');
2231 __os.fill(__sp);
2232 __os << __x.__e_ << __sp << __x._V_[0];
2233 for (size_t __i = 1; __i < _K; ++__i)
2234 __os << __sp << __x._V_[__i];
2235 return __os << __sp << __x._Y_;
2236}
2237
2238template <class _CharT, class _Traits,
2239 class _Eng, size_t _K>
2240basic_istream<_CharT, _Traits>&
2241operator>>(basic_istream<_CharT, _Traits>& __is,
2242 shuffle_order_engine<_Eng, _K>& __x)
2243{
2244 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
2245 __save_flags<_CharT, _Traits> _(__is);
2246 __is.flags(ios_base::dec | ios_base::skipws);
2247 _Eng __e;
2248 result_type _V[_K+1];
2249 __is >> __e;
2250 for (size_t __i = 0; __i < _K+1; ++__i)
2251 __is >> _V[__i];
2252 if (!__is.fail())
2253 {
2254 __x.__e_ = __e;
2255 for (size_t __i = 0; __i < _K; ++__i)
2256 __x._V_[__i] = _V[__i];
2257 __x._Y_ = _V[_K];
2258 }
2259 return __is;
2260}
2261
2262typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
2263
2264// random_device
2265
2266class random_device
2267{
2268 int __f_;
2269public:
2270 // types
2271 typedef unsigned result_type;
2272
2273 // generator characteristics
2274 static const result_type _Min = 0;
2275 static const result_type _Max = 0xFFFFFFFFu;
2276
2277 static const/*expr*/ result_type min() { return _Min;}
2278 static const/*expr*/ result_type max() { return _Max;}
2279
2280 // constructors
2281 explicit random_device(const string& __token = "/dev/urandom");
2282 ~random_device();
2283
2284 // generating functions
2285 result_type operator()();
2286
2287 // property functions
2288 double entropy() const;
2289
2290private:
2291 // no copy functions
2292 random_device(const random_device&); // = delete;
2293 random_device& operator=(const random_device&); // = delete;
2294};
2295
2296// seed_seq
2297
2298class seed_seq
2299{
2300public:
2301 // types
2302 typedef uint32_t result_type;
2303
2304private:
2305 vector<result_type> __v_;
2306
2307 template<class _InputIterator>
2308 void init(_InputIterator __first, _InputIterator __last);
2309public:
2310 // constructors
2311 seed_seq() {}
2312 template<class _Tp>
2313 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
2314
2315 template<class _InputIterator>
2316 seed_seq(_InputIterator __first, _InputIterator __last)
2317 {init(__first, __last);}
2318
2319 // generating functions
2320 template<class _RandomAccessIterator>
2321 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
2322
2323 // property functions
2324 size_t size() const {return __v_.size();}
2325 template<class _OutputIterator>
2326 void param(_OutputIterator __dest) const
2327 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
2328
2329private:
2330 // no copy functions
2331 seed_seq(const seed_seq&); // = delete;
2332 void operator=(const seed_seq&); // = delete;
2333
2334 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
2335};
2336
2337template<class _InputIterator>
2338void
2339seed_seq::init(_InputIterator __first, _InputIterator __last)
2340{
2341 for (_InputIterator __s = __first; __s != __last; ++__s)
2342 __v_.push_back(*__s & 0xFFFFFFFF);
2343}
2344
2345template<class _RandomAccessIterator>
2346void
2347seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
2348{
2349 if (__first != __last)
2350 {
2351 _STD::fill(__first, __last, 0x8b8b8b8b);
2352 const size_t __n = static_cast<size_t>(__last - __first);
2353 const size_t __s = __v_.size();
2354 const size_t __t = (__n >= 623) ? 11
2355 : (__n >= 68) ? 7
2356 : (__n >= 39) ? 5
2357 : (__n >= 7) ? 3
2358 : (__n - 1) / 2;
2359 const size_t __p = (__n - __t) / 2;
2360 const size_t __q = __p + __t;
2361 const size_t __m = _STD::max(__s + 1, __n);
2362 // __k = 0;
2363 {
2364 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
2365 ^ __first[__n - 1]);
2366 __first[__p] += __r;
2367 __r += __s;
2368 __first[__q] += __r;
2369 __first[0] = __r;
2370 }
2371 for (size_t __k = 1; __k <= __s; ++__k)
2372 {
2373 const size_t __kmodn = __k % __n;
2374 const size_t __kpmodn = (__k + __p) % __n;
2375 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2376 ^ __first[(__k - 1) % __n]);
2377 __first[__kpmodn] += __r;
2378 __r += __kmodn + __v_[__k-1];
2379 __first[(__k + __q) % __n] += __r;
2380 __first[__kmodn] = __r;
2381 }
2382 for (size_t __k = __s + 1; __k < __m; ++__k)
2383 {
2384 const size_t __kmodn = __k % __n;
2385 const size_t __kpmodn = (__k + __p) % __n;
2386 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2387 ^ __first[(__k - 1) % __n]);
2388 __first[__kpmodn] += __r;
2389 __r += __kmodn;
2390 __first[(__k + __q) % __n] += __r;
2391 __first[__kmodn] = __r;
2392 }
2393 for (size_t __k = __m; __k < __m + __n; ++__k)
2394 {
2395 const size_t __kmodn = __k % __n;
2396 const size_t __kpmodn = (__k + __p) % __n;
2397 result_type __r = 1566083941 * _T(__first[__kmodn] +
2398 __first[__kpmodn] +
2399 __first[(__k - 1) % __n]);
2400 __first[__kpmodn] ^= __r;
2401 __r -= __kmodn;
2402 __first[(__k + __q) % __n] ^= __r;
2403 __first[__kmodn] = __r;
2404 }
2405 }
2406}
2407
2408template<class _RealType, size_t __bits, class _URNG>
2409_RealType
2410generate_canonical(_URNG& __g)
2411{
2412 const size_t _Dt = numeric_limits<_RealType>::digits;
2413 const size_t __b = _Dt < __bits ? _Dt : __bits;
2414 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
2415 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
2416 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
2417 _RealType __base = _R;
2418 _RealType _S = __g() - _URNG::_Min;
2419 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
2420 _S += (__g() - _URNG::_Min) * __base;
2421 return _S / __base;
2422}
2423
2424// __independent_bits_engine
2425
2426template<class _Engine, class _UIntType>
2427class __independent_bits_engine
2428{
2429public:
2430 // types
2431 typedef _UIntType result_type;
2432
2433private:
2434 typedef typename _Engine::result_type _Engine_result_type;
2435 typedef typename conditional
2436 <
2437 sizeof(_Engine_result_type) <= sizeof(result_type),
2438 result_type,
2439 _Engine_result_type
2440 >::type _Working_result_type;
2441
2442 _Engine& __e_;
2443 size_t __w_;
2444 size_t __w0_;
2445 size_t __n_;
2446 size_t __n0_;
2447 _Working_result_type __y0_;
2448 _Working_result_type __y1_;
2449 _Engine_result_type __mask0_;
2450 _Engine_result_type __mask1_;
2451
2452 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2453 + _Working_result_type(1);
2454 static const size_t __m = __log2<_Working_result_type, _R>::value;
2455 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2456 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2457
2458public:
2459 // constructors and seeding functions
2460 __independent_bits_engine(_Engine& __e, size_t __w);
2461
2462 // generating functions
2463 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2464
2465private:
2466 result_type __eval(false_type);
2467 result_type __eval(true_type);
2468};
2469
2470template<class _Engine, class _UIntType>
2471__independent_bits_engine<_Engine, _UIntType>
2472 ::__independent_bits_engine(_Engine& __e, size_t __w)
2473 : __e_(__e),
2474 __w_(__w)
2475{
2476 __n_ = __w_ / __m + (__w_ % __m != 0);
2477 __w0_ = __w_ / __n_;
2478 if (_R == 0)
2479 __y0_ = _R;
2480 else if (__w0_ < _WDt)
2481 __y0_ = (_R >> __w0_) << __w0_;
2482 else
2483 __y0_ = 0;
2484 if (_R - __y0_ > __y0_ / __n_)
2485 {
2486 ++__n_;
2487 __w0_ = __w_ / __n_;
2488 if (__w0_ < _WDt)
2489 __y0_ = (_R >> __w0_) << __w0_;
2490 else
2491 __y0_ = 0;
2492 }
2493 __n0_ = __n_ - __w_ % __n_;
2494 if (__w0_ < _WDt - 1)
2495 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2496 else
2497 __y1_ = 0;
2498 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2499 _Engine_result_type(0);
2500 __mask1_ = __w0_ < _EDt - 1 ?
2501 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2502 _Engine_result_type(~0);
2503}
2504
2505template<class _Engine, class _UIntType>
2506inline
2507_UIntType
2508__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2509{
2510 return static_cast<result_type>(__e_() & __mask0_);
2511}
2512
2513template<class _Engine, class _UIntType>
2514_UIntType
2515__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2516{
2517 result_type _S = 0;
2518 for (size_t __k = 0; __k < __n0_; ++__k)
2519 {
2520 _Engine_result_type __u;
2521 do
2522 {
2523 __u = __e_() - _Engine::min();
2524 } while (__u >= __y0_);
2525 if (__w0_ < _EDt)
2526 _S <<= __w0_;
2527 else
2528 _S = 0;
2529 _S += __u & __mask0_;
2530 }
2531 for (size_t __k = __n0_; __k < __n_; ++__k)
2532 {
2533 _Engine_result_type __u;
2534 do
2535 {
2536 __u = __e_() - _Engine::min();
2537 } while (__u >= __y1_);
2538 if (__w0_ < _EDt - 1)
2539 _S <<= __w0_ + 1;
2540 else
2541 _S = 0;
2542 _S += __u & __mask1_;
2543 }
2544 return _S;
2545}
2546
2547template<class _IntType = int>
2548class uniform_int_distribution
2549{
2550public:
2551 // types
2552 typedef _IntType result_type;
2553
2554 class param_type
2555 {
2556 result_type __a_;
2557 result_type __b_;
2558 public:
2559 typedef uniform_int_distribution distribution_type;
2560
2561 explicit param_type(result_type __a = 0,
2562 result_type __b = numeric_limits<result_type>::max())
2563 : __a_(__a), __b_(__b) {}
2564
2565 result_type a() const {return __a_;}
2566 result_type b() const {return __b_;}
2567
2568 friend bool operator==(const param_type& __x, const param_type& __y)
2569 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2570 friend bool operator!=(const param_type& __x, const param_type& __y)
2571 {return !(__x == __y);}
2572 };
2573
2574private:
2575 param_type __p_;
2576
2577public:
2578 // constructors and reset functions
2579 explicit uniform_int_distribution(result_type __a = 0,
2580 result_type __b = numeric_limits<result_type>::max())
2581 : __p_(param_type(__a, __b)) {}
2582 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2583 void reset() {}
2584
2585 // generating functions
2586 template<class _URNG> result_type operator()(_URNG& __g)
2587 {return (*this)(__g, __p_);}
2588 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2589
2590 // property functions
2591 result_type a() const {return __p_.a();}
2592 result_type b() const {return __p_.b();}
2593
2594 param_type param() const {return __p_;}
2595 void param(const param_type& __p) {__p_ = __p;}
2596
2597 result_type min() const {return a();}
2598 result_type max() const {return b();}
2599
2600 friend bool operator==(const uniform_int_distribution& __x,
2601 const uniform_int_distribution& __y)
2602 {return __x.__p_ == __y.__p_;}
2603 friend bool operator!=(const uniform_int_distribution& __x,
2604 const uniform_int_distribution& __y)
2605 {return !(__x == __y);}
2606};
2607
2608template<class _IntType>
2609template<class _URNG>
2610typename uniform_int_distribution<_IntType>::result_type
2611uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2612{
2613 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2614 uint32_t, uint64_t>::type _UIntType;
2615 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2616 if (_R == 1)
2617 return __p.a();
2618 const size_t _Dt = numeric_limits<_UIntType>::digits;
2619 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2620 if (_R == 0)
2621 return static_cast<result_type>(_Eng(__g, _Dt)());
2622 size_t __w = _Dt - __clz(_R) - 1;
2623 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2624 ++__w;
2625 _Eng __e(__g, __w);
2626 _UIntType __u;
2627 do
2628 {
2629 __u = __e();
2630 } while (__u >= _R);
2631 return static_cast<result_type>(__u + __p.a());
2632}
2633
2634template <class _CharT, class _Traits, class _IT>
2635basic_ostream<_CharT, _Traits>&
2636operator<<(basic_ostream<_CharT, _Traits>& __os,
2637 const uniform_int_distribution<_IT>& __x)
2638{
2639 __save_flags<_CharT, _Traits> _(__os);
2640 __os.flags(ios_base::dec | ios_base::left);
2641 _CharT __sp = __os.widen(' ');
2642 __os.fill(__sp);
2643 return __os << __x.a() << __sp << __x.b();
2644}
2645
2646template <class _CharT, class _Traits, class _IT>
2647basic_istream<_CharT, _Traits>&
2648operator>>(basic_istream<_CharT, _Traits>& __is,
2649 uniform_int_distribution<_IT>& __x)
2650{
2651 typedef uniform_int_distribution<_IT> _Eng;
2652 typedef typename _Eng::result_type result_type;
2653 typedef typename _Eng::param_type param_type;
2654 __save_flags<_CharT, _Traits> _(__is);
2655 __is.flags(ios_base::dec | ios_base::skipws);
2656 result_type __a;
2657 result_type __b;
2658 __is >> __a >> __b;
2659 if (!__is.fail())
2660 __x.param(param_type(__a, __b));
2661 return __is;
2662}
2663
2664template<class _RealType = double>
2665class uniform_real_distribution
2666{
2667public:
2668 // types
2669 typedef _RealType result_type;
2670
2671 class param_type
2672 {
2673 result_type __a_;
2674 result_type __b_;
2675 public:
2676 typedef uniform_real_distribution distribution_type;
2677
2678 explicit param_type(result_type __a = 0,
2679 result_type __b = 1)
2680 : __a_(__a), __b_(__b) {}
2681
2682 result_type a() const {return __a_;}
2683 result_type b() const {return __b_;}
2684
2685 friend bool operator==(const param_type& __x, const param_type& __y)
2686 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2687 friend bool operator!=(const param_type& __x, const param_type& __y)
2688 {return !(__x == __y);}
2689 };
2690
2691private:
2692 param_type __p_;
2693
2694public:
2695 // constructors and reset functions
2696 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
2697 : __p_(param_type(__a, __b)) {}
2698 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
2699 void reset() {}
2700
2701 // generating functions
2702 template<class _URNG> result_type operator()(_URNG& __g)
2703 {return (*this)(__g, __p_);}
2704 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2705
2706 // property functions
2707 result_type a() const {return __p_.a();}
2708 result_type b() const {return __p_.b();}
2709
2710 param_type param() const {return __p_;}
2711 void param(const param_type& __p) {__p_ = __p;}
2712
2713 result_type min() const {return a();}
2714 result_type max() const {return b();}
2715
2716 friend bool operator==(const uniform_real_distribution& __x,
2717 const uniform_real_distribution& __y)
2718 {return __x.__p_ == __y.__p_;}
2719 friend bool operator!=(const uniform_real_distribution& __x,
2720 const uniform_real_distribution& __y)
2721 {return !(__x == __y);}
2722};
2723
2724template<class _RealType>
2725template<class _URNG>
2726inline
2727typename uniform_real_distribution<_RealType>::result_type
2728uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
2729{
2730 return (__p.b() - __p.a())
2731 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
2732 + __p.a();
2733}
2734
2735template <class _CharT, class _Traits, class _RT>
2736basic_ostream<_CharT, _Traits>&
2737operator<<(basic_ostream<_CharT, _Traits>& __os,
2738 const uniform_real_distribution<_RT>& __x)
2739{
2740 __save_flags<_CharT, _Traits> _(__os);
2741 __os.flags(ios_base::dec | ios_base::left);
2742 _CharT __sp = __os.widen(' ');
2743 __os.fill(__sp);
2744 return __os << __x.a() << __sp << __x.b();
2745}
2746
2747template <class _CharT, class _Traits, class _RT>
2748basic_istream<_CharT, _Traits>&
2749operator>>(basic_istream<_CharT, _Traits>& __is,
2750 uniform_real_distribution<_RT>& __x)
2751{
2752 typedef uniform_real_distribution<_RT> _Eng;
2753 typedef typename _Eng::result_type result_type;
2754 typedef typename _Eng::param_type param_type;
2755 __save_flags<_CharT, _Traits> _(__is);
2756 __is.flags(ios_base::dec | ios_base::skipws);
2757 result_type __a;
2758 result_type __b;
2759 __is >> __a >> __b;
2760 if (!__is.fail())
2761 __x.param(param_type(__a, __b));
2762 return __is;
2763}
2764
2765class bernoulli_distribution
2766{
2767public:
2768 // types
2769 typedef bool result_type;
2770
2771 class param_type
2772 {
2773 double __p_;
2774 public:
2775 typedef bernoulli_distribution distribution_type;
2776
2777 explicit param_type(double __p = 0.5) : __p_(__p) {}
2778
2779 double p() const {return __p_;}
2780
2781 friend bool operator==(const param_type& __x, const param_type& __y)
2782 {return __x.__p_ == __y.__p_;}
2783 friend bool operator!=(const param_type& __x, const param_type& __y)
2784 {return !(__x == __y);}
2785 };
2786
2787private:
2788 param_type __p_;
2789
2790public:
2791 // constructors and reset functions
2792 explicit bernoulli_distribution(double __p = 0.5)
2793 : __p_(param_type(__p)) {}
2794 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
2795 void reset() {}
2796
2797 // generating functions
2798 template<class _URNG> result_type operator()(_URNG& __g)
2799 {return (*this)(__g, __p_);}
2800 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
2801 {
2802 return _STD::generate_canonical<double, numeric_limits<double>::digits>(__g)
2803 < __p.p();
2804 }
2805
2806 // property functions
2807 double p() const {return __p_.p();}
2808
2809 param_type param() const {return __p_;}
2810 void param(const param_type& __p) {__p_ = __p;}
2811
2812 result_type min() const {return false;}
2813 result_type max() const {return true;}
2814
2815 friend bool operator==(const bernoulli_distribution& __x,
2816 const bernoulli_distribution& __y)
2817 {return __x.__p_ == __y.__p_;}
2818 friend bool operator!=(const bernoulli_distribution& __x,
2819 const bernoulli_distribution& __y)
2820 {return !(__x == __y);}
2821};
2822
2823template <class _CharT, class _Traits>
2824basic_ostream<_CharT, _Traits>&
2825operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
2826{
2827 __save_flags<_CharT, _Traits> _(__os);
2828 __os.flags(ios_base::dec | ios_base::left);
2829 _CharT __sp = __os.widen(' ');
2830 __os.fill(__sp);
2831 return __os << __x.p();
2832}
2833
2834template <class _CharT, class _Traits>
2835basic_istream<_CharT, _Traits>&
2836operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
2837{
2838 typedef bernoulli_distribution _Eng;
2839 typedef typename _Eng::param_type param_type;
2840 __save_flags<_CharT, _Traits> _(__is);
2841 __is.flags(ios_base::dec | ios_base::skipws);
2842 double __p;
2843 __is >> __p;
2844 if (!__is.fail())
2845 __x.param(param_type(__p));
2846 return __is;
2847}
2848
2849_LIBCPP_END_NAMESPACE_STD
2850
2851#endif // _LIBCPP_RANDOM