blob: 948bb9aa9b37f492e93d6e0087ab3bc85c21f35f [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>
Howard Hinnant03aad812010-05-11 23:26:59 +0000611class binomial_distribution
612{
613public:
614 // types
615 typedef IntType result_type;
616
617 class param_type
618 {
619 public:
620 typedef binomial_distribution distribution_type;
621
622 explicit param_type(IntType t = 1, double p = 0.5);
623
624 IntType t() const;
625 double p() const;
626
627 friend bool operator==(const param_type& x, const param_type& y);
628 friend bool operator!=(const param_type& x, const param_type& y);
629 };
630
631 // constructors and reset functions
632 explicit binomial_distribution(IntType t = 1, double p = 0.5);
633 explicit binomial_distribution(const param_type& parm);
634 void reset();
635
636 // generating functions
637 template<class URNG> result_type operator()(URNG& g);
638 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
639
640 // property functions
641 IntType t() const;
642 double p() const;
643
644 param_type param() const;
645 void param(const param_type& parm);
646
647 result_type min() const;
648 result_type max() const;
649
650 friend bool operator==(const binomial_distribution& x,
651 const binomial_distribution& y);
652 friend bool operator!=(const binomial_distribution& x,
653 const binomial_distribution& y);
654
655 template <class charT, class traits>
656 friend
657 basic_ostream<charT, traits>&
658 operator<<(basic_ostream<charT, traits>& os,
659 const binomial_distribution& x);
660
661 template <class charT, class traits>
662 friend
663 basic_istream<charT, traits>&
664 operator>>(basic_istream<charT, traits>& is,
665 binomial_distribution& x);
666};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668template<class IntType = int>
669 class geometric_distribution;
670
671template<class IntType = int>
672 class negative_binomial_distribution;
673
674template<class IntType = int>
675 class poisson_distribution;
676
677template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000678class exponential_distribution
679{
680public:
681 // types
682 typedef RealType result_type;
683
684 class param_type
685 {
686 public:
687 typedef exponential_distribution distribution_type;
688
Howard Hinnanta64111c2010-05-12 21:02:31 +0000689 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000690
Howard Hinnanta64111c2010-05-12 21:02:31 +0000691 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000692
693 friend bool operator==(const param_type& x, const param_type& y);
694 friend bool operator!=(const param_type& x, const param_type& y);
695 };
696
697 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000698 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000699 explicit exponential_distribution(const param_type& parm);
700 void reset();
701
702 // generating functions
703 template<class URNG> result_type operator()(URNG& g);
704 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
705
706 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000707 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000708
709 param_type param() const;
710 void param(const param_type& parm);
711
712 result_type min() const;
713 result_type max() const;
714
715 friend bool operator==(const exponential_distribution& x,
716 const exponential_distribution& y);
717 friend bool operator!=(const exponential_distribution& x,
718 const exponential_distribution& y);
719
720 template <class charT, class traits>
721 friend
722 basic_ostream<charT, traits>&
723 operator<<(basic_ostream<charT, traits>& os,
724 const exponential_distribution& x);
725
726 template <class charT, class traits>
727 friend
728 basic_istream<charT, traits>&
729 operator>>(basic_istream<charT, traits>& is,
730 exponential_distribution& x);
731};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
733template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000734class gamma_distribution
735{
736public:
737 // types
738 typedef RealType result_type;
739
740 class param_type
741 {
742 public:
743 typedef gamma_distribution distribution_type;
744
745 explicit param_type(result_type alpha = 1, result_type beta = 1);
746
747 result_type alpha() const;
748 result_type beta() const;
749
750 friend bool operator==(const param_type& x, const param_type& y);
751 friend bool operator!=(const param_type& x, const param_type& y);
752 };
753
754 // constructors and reset functions
755 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
756 explicit gamma_distribution(const param_type& parm);
757 void reset();
758
759 // generating functions
760 template<class URNG> result_type operator()(URNG& g);
761 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
762
763 // property functions
764 result_type alpha() const;
765 result_type beta() const;
766
767 param_type param() const;
768 void param(const param_type& parm);
769
770 result_type min() const;
771 result_type max() const;
772
773 friend bool operator==(const gamma_distribution& x,
774 const gamma_distribution& y);
775 friend bool operator!=(const gamma_distribution& x,
776 const gamma_distribution& y);
777
778 template <class charT, class traits>
779 friend
780 basic_ostream<charT, traits>&
781 operator<<(basic_ostream<charT, traits>& os,
782 const gamma_distribution& x);
783
784 template <class charT, class traits>
785 friend
786 basic_istream<charT, traits>&
787 operator>>(basic_istream<charT, traits>& is,
788 gamma_distribution& x);
789};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790
791template<class RealType = double>
792 class weibull_distribution;
793
794template<class RealType = double>
795 class extreme_value_distribution;
796
797template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +0000798class normal_distribution
799{
800public:
801 // types
802 typedef RealType result_type;
803
804 class param_type
805 {
806 public:
807 typedef normal_distribution distribution_type;
808
809 explicit param_type(result_type mean = 0, result_type stddev = 1);
810
811 result_type mean() const;
812 result_type stddev() const;
813
814 friend bool operator==(const param_type& x, const param_type& y);
815 friend bool operator!=(const param_type& x, const param_type& y);
816 };
817
818 // constructors and reset functions
819 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
820 explicit normal_distribution(const param_type& parm);
821 void reset();
822
823 // generating functions
824 template<class URNG> result_type operator()(URNG& g);
825 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
826
827 // property functions
828 result_type mean() const;
829 result_type stddev() const;
830
831 param_type param() const;
832 void param(const param_type& parm);
833
834 result_type min() const;
835 result_type max() const;
836
837 friend bool operator==(const normal_distribution& x,
838 const normal_distribution& y);
839 friend bool operator!=(const normal_distribution& x,
840 const normal_distribution& y);
841
842 template <class charT, class traits>
843 friend
844 basic_ostream<charT, traits>&
845 operator<<(basic_ostream<charT, traits>& os,
846 const normal_distribution& x);
847
848 template <class charT, class traits>
849 friend
850 basic_istream<charT, traits>&
851 operator>>(basic_istream<charT, traits>& is,
852 normal_distribution& x);
853};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854
855template<class RealType = double>
856 class lognormal_distribution;
857
858template<class RealType = double>
859 class chi_squared_distribution;
860
861template<class RealType = double>
862 class cauchy_distribution;
863
864template<class RealType = double>
865 class fisher_f_distribution;
866
867template<class RealType = double>
868 class student_t_distribution;
869
870template<class IntType = int>
871 class discrete_distribution;
872
873template<class RealType = double>
874 class piecewise_constant_distribution;
875
876template<class RealType = double>
877 class piecewise_linear_distribution;
878
879} // std
880*/
881
882#include <__config>
883#include <cstddef>
884#include <type_traits>
885#include <initializer_list>
886#include <cstdint>
887#include <limits>
888#include <algorithm>
889#include <vector>
890#include <string>
891#include <istream>
892#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000893#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894
895#pragma GCC system_header
896
897_LIBCPP_BEGIN_NAMESPACE_STD
898
899// linear_congruential_engine
900
901template <unsigned long long __a, unsigned long long __c,
902 unsigned long long __m, unsigned long long _M,
903 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
904struct __lce_ta;
905
906// 64
907
908template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
909struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
910{
911 typedef unsigned long long result_type;
912 static result_type next(result_type __x)
913 {
914 // Schrage's algorithm
915 const result_type __q = __m / __a;
916 const result_type __r = __m % __a;
917 const result_type __t0 = __a * (__x % __q);
918 const result_type __t1 = __r * (__x / __q);
919 __x = __t0 + (__t0 < __t1) * __m - __t1;
920 __x += __c - (__x >= __m - __c) * __m;
921 return __x;
922 }
923};
924
925template <unsigned long long __a, unsigned long long __m>
926struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
927{
928 typedef unsigned long long result_type;
929 static result_type next(result_type __x)
930 {
931 // Schrage's algorithm
932 const result_type __q = __m / __a;
933 const result_type __r = __m % __a;
934 const result_type __t0 = __a * (__x % __q);
935 const result_type __t1 = __r * (__x / __q);
936 __x = __t0 + (__t0 < __t1) * __m - __t1;
937 return __x;
938 }
939};
940
941template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
942struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
943{
944 typedef unsigned long long result_type;
945 static result_type next(result_type __x)
946 {
947 return (__a * __x + __c) % __m;
948 }
949};
950
951template <unsigned long long __a, unsigned long long __c>
952struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
953{
954 typedef unsigned long long result_type;
955 static result_type next(result_type __x)
956 {
957 return __a * __x + __c;
958 }
959};
960
961// 32
962
963template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
964struct __lce_ta<_A, _C, _M, unsigned(~0), true>
965{
966 typedef unsigned result_type;
967 static result_type next(result_type __x)
968 {
969 const result_type __a = static_cast<result_type>(_A);
970 const result_type __c = static_cast<result_type>(_C);
971 const result_type __m = static_cast<result_type>(_M);
972 // Schrage's algorithm
973 const result_type __q = __m / __a;
974 const result_type __r = __m % __a;
975 const result_type __t0 = __a * (__x % __q);
976 const result_type __t1 = __r * (__x / __q);
977 __x = __t0 + (__t0 < __t1) * __m - __t1;
978 __x += __c - (__x >= __m - __c) * __m;
979 return __x;
980 }
981};
982
983template <unsigned long long _A, unsigned long long _M>
984struct __lce_ta<_A, 0, _M, unsigned(~0), true>
985{
986 typedef unsigned result_type;
987 static result_type next(result_type __x)
988 {
989 const result_type __a = static_cast<result_type>(_A);
990 const result_type __m = static_cast<result_type>(_M);
991 // Schrage's algorithm
992 const result_type __q = __m / __a;
993 const result_type __r = __m % __a;
994 const result_type __t0 = __a * (__x % __q);
995 const result_type __t1 = __r * (__x / __q);
996 __x = __t0 + (__t0 < __t1) * __m - __t1;
997 return __x;
998 }
999};
1000
1001template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1002struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1003{
1004 typedef unsigned result_type;
1005 static result_type next(result_type __x)
1006 {
1007 const result_type __a = static_cast<result_type>(_A);
1008 const result_type __c = static_cast<result_type>(_C);
1009 const result_type __m = static_cast<result_type>(_M);
1010 return (__a * __x + __c) % __m;
1011 }
1012};
1013
1014template <unsigned long long _A, unsigned long long _C>
1015struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1016{
1017 typedef unsigned result_type;
1018 static result_type next(result_type __x)
1019 {
1020 const result_type __a = static_cast<result_type>(_A);
1021 const result_type __c = static_cast<result_type>(_C);
1022 return __a * __x + __c;
1023 }
1024};
1025
1026// 16
1027
1028template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1029struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1030{
1031 typedef unsigned short result_type;
1032 static result_type next(result_type __x)
1033 {
1034 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1035 }
1036};
1037
1038template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1039class linear_congruential_engine;
1040
1041template <class _CharT, class _Traits,
1042 class _U, _U _A, _U _C, _U _N>
1043basic_ostream<_CharT, _Traits>&
1044operator<<(basic_ostream<_CharT, _Traits>& __os,
1045 const linear_congruential_engine<_U, _A, _C, _N>&);
1046
1047template <class _CharT, class _Traits,
1048 class _U, _U _A, _U _C, _U _N>
1049basic_istream<_CharT, _Traits>&
1050operator>>(basic_istream<_CharT, _Traits>& __is,
1051 linear_congruential_engine<_U, _A, _C, _N>& __x);
1052
1053template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1054class linear_congruential_engine
1055{
1056public:
1057 // types
1058 typedef _UIntType result_type;
1059
1060private:
1061 result_type __x_;
1062
1063 static const result_type _M = result_type(~0);
1064
1065 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1066 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1067public:
1068 static const result_type _Min = __c == 0u ? 1u: 0u;
1069 static const result_type _Max = __m - 1u;
1070 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1071
1072 // engine characteristics
1073 static const/*expr*/ result_type multiplier = __a;
1074 static const/*expr*/ result_type increment = __c;
1075 static const/*expr*/ result_type modulus = __m;
1076 static const/*expr*/ result_type min() {return _Min;}
1077 static const/*expr*/ result_type max() {return _Max;}
1078 static const/*expr*/ result_type default_seed = 1u;
1079
1080 // constructors and seeding functions
1081 explicit linear_congruential_engine(result_type __s = default_seed)
1082 {seed(__s);}
1083 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
1084 {seed(__q);}
1085 void seed(result_type __s = default_seed)
1086 {seed(integral_constant<bool, __m == 0>(),
1087 integral_constant<bool, __c == 0>(), __s);}
1088 template<class _Sseq>
1089 typename enable_if
1090 <
1091 !is_convertible<_Sseq, result_type>::value,
1092 void
1093 >::type
1094 seed(_Sseq& __q)
1095 {__seed(__q, integral_constant<unsigned,
1096 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1097 : (__m-1) / 0x100000000ull)>());}
1098
1099 // generating functions
1100 result_type operator()()
1101 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1102 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1103
1104 friend bool operator==(const linear_congruential_engine& __x,
1105 const linear_congruential_engine& __y)
1106 {return __x.__x_ == __y.__x_;}
1107 friend bool operator!=(const linear_congruential_engine& __x,
1108 const linear_congruential_engine& __y)
1109 {return !(__x == __y);}
1110
1111private:
1112
1113 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1114 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1115 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1116 1 : __s % __m;}
1117 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1118
1119 template<class _Sseq>
1120 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1121 template<class _Sseq>
1122 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1123
1124 template <class _CharT, class _Traits,
1125 class _U, _U _A, _U _C, _U _N>
1126 friend
1127 basic_ostream<_CharT, _Traits>&
1128 operator<<(basic_ostream<_CharT, _Traits>& __os,
1129 const linear_congruential_engine<_U, _A, _C, _N>&);
1130
1131 template <class _CharT, class _Traits,
1132 class _U, _U _A, _U _C, _U _N>
1133 friend
1134 basic_istream<_CharT, _Traits>&
1135 operator>>(basic_istream<_CharT, _Traits>& __is,
1136 linear_congruential_engine<_U, _A, _C, _N>& __x);
1137};
1138
1139template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1140template<class _Sseq>
1141void
1142linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1143 integral_constant<unsigned, 1>)
1144{
1145 const unsigned __k = 1;
1146 uint32_t __ar[__k+3];
1147 __q.generate(__ar, __ar + __k + 3);
1148 result_type __s = static_cast<result_type>(__ar[3] % __m);
1149 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1150}
1151
1152template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1153template<class _Sseq>
1154void
1155linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1156 integral_constant<unsigned, 2>)
1157{
1158 const unsigned __k = 2;
1159 uint32_t __ar[__k+3];
1160 __q.generate(__ar, __ar + __k + 3);
1161 result_type __s = static_cast<result_type>((__ar[3] +
1162 (uint64_t)__ar[4] << 32) % __m);
1163 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1164}
1165
1166template <class _CharT, class _Traits>
1167class __save_flags
1168{
1169 typedef basic_ios<_CharT, _Traits> __stream_type;
1170 typedef typename __stream_type::fmtflags fmtflags;
1171
1172 __stream_type& __stream_;
1173 fmtflags __fmtflags_;
1174 _CharT __fill_;
1175
1176 __save_flags(const __save_flags&);
1177 __save_flags& operator=(const __save_flags&);
1178public:
1179 explicit __save_flags(__stream_type& __stream)
1180 : __stream_(__stream),
1181 __fmtflags_(__stream.flags()),
1182 __fill_(__stream.fill())
1183 {}
1184 ~__save_flags()
1185 {
1186 __stream_.flags(__fmtflags_);
1187 __stream_.fill(__fill_);
1188 }
1189};
1190
1191template <class _CharT, class _Traits,
1192 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1193inline
1194basic_ostream<_CharT, _Traits>&
1195operator<<(basic_ostream<_CharT, _Traits>& __os,
1196 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1197{
1198 __save_flags<_CharT, _Traits> _(__os);
1199 __os.flags(ios_base::dec | ios_base::left);
1200 __os.fill(__os.widen(' '));
1201 return __os << __x.__x_;
1202}
1203
1204template <class _CharT, class _Traits,
1205 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1206basic_istream<_CharT, _Traits>&
1207operator>>(basic_istream<_CharT, _Traits>& __is,
1208 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1209{
1210 __save_flags<_CharT, _Traits> _(__is);
1211 __is.flags(ios_base::dec | ios_base::skipws);
1212 _UIntType __t;
1213 __is >> __t;
1214 if (!__is.fail())
1215 __x.__x_ = __t;
1216 return __is;
1217}
1218
1219typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1220 minstd_rand0;
1221typedef minstd_rand0 default_random_engine;
1222typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1223 minstd_rand;
1224// mersenne_twister_engine
1225
1226template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1227 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1228 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1229class mersenne_twister_engine;
1230
1231template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1232 _UI _A, size_t _U, _UI _D, size_t _S,
1233 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1234bool
1235operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1236 _B, _T, _C, _L, _F>& __x,
1237 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1238 _B, _T, _C, _L, _F>& __y);
1239
1240template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1241 _UI _A, size_t _U, _UI _D, size_t _S,
1242 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1243bool
1244operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1245 _B, _T, _C, _L, _F>& __x,
1246 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1247 _B, _T, _C, _L, _F>& __y);
1248
1249template <class _CharT, class _Traits,
1250 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1251 _UI _A, size_t _U, _UI _D, size_t _S,
1252 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1253basic_ostream<_CharT, _Traits>&
1254operator<<(basic_ostream<_CharT, _Traits>& __os,
1255 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1256 _B, _T, _C, _L, _F>& __x);
1257
1258template <class _CharT, class _Traits,
1259 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1260 _UI _A, size_t _U, _UI _D, size_t _S,
1261 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1262basic_istream<_CharT, _Traits>&
1263operator>>(basic_istream<_CharT, _Traits>& __is,
1264 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1265 _B, _T, _C, _L, _F>& __x);
1266
1267template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1268 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1269 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1270class mersenne_twister_engine
1271{
1272public:
1273 // types
1274 typedef _UIntType result_type;
1275
1276private:
1277 result_type __x_[__n];
1278 size_t __i_;
1279
1280 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1281 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1282 static const result_type _Dt = numeric_limits<result_type>::digits;
1283 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1284 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1285 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1286 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1287 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1288 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1289 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1290public:
1291 static const result_type _Min = 0;
1292 static const result_type _Max = __w == _Dt ? result_type(~0) :
1293 (result_type(1) << __w) - result_type(1);
1294 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1295 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1296 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1297 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1298 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1299 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1300
1301 // engine characteristics
1302 static const/*expr*/ size_t word_size = __w;
1303 static const/*expr*/ size_t state_size = __n;
1304 static const/*expr*/ size_t shift_size = __m;
1305 static const/*expr*/ size_t mask_bits = __r;
1306 static const/*expr*/ result_type xor_mask = __a;
1307 static const/*expr*/ size_t tempering_u = __u;
1308 static const/*expr*/ result_type tempering_d = __d;
1309 static const/*expr*/ size_t tempering_s = __s;
1310 static const/*expr*/ result_type tempering_b = __b;
1311 static const/*expr*/ size_t tempering_t = __t;
1312 static const/*expr*/ result_type tempering_c = __c;
1313 static const/*expr*/ size_t tempering_l = __l;
1314 static const/*expr*/ result_type initialization_multiplier = __f;
1315 static const/*expr*/ result_type min() { return _Min; }
1316 static const/*expr*/ result_type max() { return _Max; }
1317 static const/*expr*/ result_type default_seed = 5489u;
1318
1319 // constructors and seeding functions
1320 explicit mersenne_twister_engine(result_type __sd = default_seed)
1321 {seed(__sd);}
1322 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
1323 {seed(__q);}
1324 void seed(result_type __sd = default_seed);
1325 template<class _Sseq>
1326 typename enable_if
1327 <
1328 !is_convertible<_Sseq, result_type>::value,
1329 void
1330 >::type
1331 seed(_Sseq& __q)
1332 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1333
1334 // generating functions
1335 result_type operator()();
1336 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1337
1338 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1339 _UI _A, size_t _U, _UI _D, size_t _S,
1340 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1341 friend
1342 bool
1343 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1344 _B, _T, _C, _L, _F>& __x,
1345 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1346 _B, _T, _C, _L, _F>& __y);
1347
1348 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1349 _UI _A, size_t _U, _UI _D, size_t _S,
1350 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1351 friend
1352 bool
1353 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1354 _B, _T, _C, _L, _F>& __x,
1355 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1356 _B, _T, _C, _L, _F>& __y);
1357
1358 template <class _CharT, class _Traits,
1359 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1360 _UI _A, size_t _U, _UI _D, size_t _S,
1361 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1362 friend
1363 basic_ostream<_CharT, _Traits>&
1364 operator<<(basic_ostream<_CharT, _Traits>& __os,
1365 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1366 _B, _T, _C, _L, _F>& __x);
1367
1368 template <class _CharT, class _Traits,
1369 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1370 _UI _A, size_t _U, _UI _D, size_t _S,
1371 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1372 friend
1373 basic_istream<_CharT, _Traits>&
1374 operator>>(basic_istream<_CharT, _Traits>& __is,
1375 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1376 _B, _T, _C, _L, _F>& __x);
1377private:
1378
1379 template<class _Sseq>
1380 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1381 template<class _Sseq>
1382 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1383
1384 template <size_t __count>
1385 static
1386 typename enable_if
1387 <
1388 __count < __w,
1389 result_type
1390 >::type
1391 __lshift(result_type __x) {return (__x << __count) & _Max;}
1392
1393 template <size_t __count>
1394 static
1395 typename enable_if
1396 <
1397 (__count >= __w),
1398 result_type
1399 >::type
1400 __lshift(result_type __x) {return result_type(0);}
1401
1402 template <size_t __count>
1403 static
1404 typename enable_if
1405 <
1406 __count < _Dt,
1407 result_type
1408 >::type
1409 __rshift(result_type __x) {return __x >> __count;}
1410
1411 template <size_t __count>
1412 static
1413 typename enable_if
1414 <
1415 (__count >= _Dt),
1416 result_type
1417 >::type
1418 __rshift(result_type __x) {return result_type(0);}
1419};
1420
1421template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1422 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1423 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1424void
1425mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1426 __t, __c, __l, __f>::seed(result_type __sd)
1427{ // __w >= 2
1428 __x_[0] = __sd & _Max;
1429 for (size_t __i = 1; __i < __n; ++__i)
1430 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
1431 __i_ = 0;
1432}
1433
1434template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1435 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1436 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1437template<class _Sseq>
1438void
1439mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1440 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
1441{
1442 const unsigned __k = 1;
1443 uint32_t __ar[__n * __k];
1444 __q.generate(__ar, __ar + __n * __k);
1445 for (size_t __i = 0; __i < __n; ++__i)
1446 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1447 const result_type __mask = __r == _Dt ? result_type(~0) :
1448 (result_type(1) << __r) - result_type(1);
1449 __i_ = 0;
1450 if ((__x_[0] & ~__mask) == 0)
1451 {
1452 for (size_t __i = 1; __i < __n; ++__i)
1453 if (__x_[__i] != 0)
1454 return;
1455 __x_[0] = _Max;
1456 }
1457}
1458
1459template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1460 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1461 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1462template<class _Sseq>
1463void
1464mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1465 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
1466{
1467 const unsigned __k = 2;
1468 uint32_t __ar[__n * __k];
1469 __q.generate(__ar, __ar + __n * __k);
1470 for (size_t __i = 0; __i < __n; ++__i)
1471 __x_[__i] = static_cast<result_type>(
1472 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1473 const result_type __mask = __r == _Dt ? result_type(~0) :
1474 (result_type(1) << __r) - result_type(1);
1475 __i_ = 0;
1476 if ((__x_[0] & ~__mask) == 0)
1477 {
1478 for (size_t __i = 1; __i < __n; ++__i)
1479 if (__x_[__i] != 0)
1480 return;
1481 __x_[0] = _Max;
1482 }
1483}
1484
1485template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1486 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1487 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1488_UIntType
1489mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1490 __t, __c, __l, __f>::operator()()
1491{
1492 const size_t __j = (__i_ + 1) % __n;
1493 const result_type __mask = __r == _Dt ? result_type(~0) :
1494 (result_type(1) << __r) - result_type(1);
1495 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
1496 const size_t __k = (__i_ + __m) % __n;
1497 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
1498 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
1499 __i_ = __j;
1500 __z ^= __lshift<__s>(__z) & __b;
1501 __z ^= __lshift<__t>(__z) & __c;
1502 return __z ^ __rshift<__l>(__z);
1503}
1504
1505template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1506 _UI _A, size_t _U, _UI _D, size_t _S,
1507 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1508bool
1509operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1510 _B, _T, _C, _L, _F>& __x,
1511 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1512 _B, _T, _C, _L, _F>& __y)
1513{
1514 if (__x.__i_ == __y.__i_)
1515 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
1516 if (__x.__i_ == 0 || __y.__i_ == 0)
1517 {
1518 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
1519 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1520 __y.__x_ + __y.__i_))
1521 return false;
1522 if (__x.__i_ == 0)
1523 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
1524 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
1525 }
1526 if (__x.__i_ < __y.__i_)
1527 {
1528 size_t __j = _N - __y.__i_;
1529 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1530 __y.__x_ + __y.__i_))
1531 return false;
1532 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
1533 __y.__x_))
1534 return false;
1535 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1536 __y.__x_ + (_N - (__x.__i_ + __j)));
1537 }
1538 size_t __j = _N - __x.__i_;
1539 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1540 __x.__x_ + __x.__i_))
1541 return false;
1542 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
1543 __x.__x_))
1544 return false;
1545 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1546 __x.__x_ + (_N - (__y.__i_ + __j)));
1547}
1548
1549template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1550 _UI _A, size_t _U, _UI _D, size_t _S,
1551 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1552inline
1553bool
1554operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1555 _B, _T, _C, _L, _F>& __x,
1556 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1557 _B, _T, _C, _L, _F>& __y)
1558{
1559 return !(__x == __y);
1560}
1561
1562template <class _CharT, class _Traits,
1563 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1564 _UI _A, size_t _U, _UI _D, size_t _S,
1565 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1566basic_ostream<_CharT, _Traits>&
1567operator<<(basic_ostream<_CharT, _Traits>& __os,
1568 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1569 _B, _T, _C, _L, _F>& __x)
1570{
1571 __save_flags<_CharT, _Traits> _(__os);
1572 __os.flags(ios_base::dec | ios_base::left);
1573 _CharT __sp = __os.widen(' ');
1574 __os.fill(__sp);
1575 __os << __x.__x_[__x.__i_];
1576 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
1577 __os << __sp << __x.__x_[__j];
1578 for (size_t __j = 0; __j < __x.__i_; ++__j)
1579 __os << __sp << __x.__x_[__j];
1580 return __os;
1581}
1582
1583template <class _CharT, class _Traits,
1584 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1585 _UI _A, size_t _U, _UI _D, size_t _S,
1586 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1587basic_istream<_CharT, _Traits>&
1588operator>>(basic_istream<_CharT, _Traits>& __is,
1589 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1590 _B, _T, _C, _L, _F>& __x)
1591{
1592 __save_flags<_CharT, _Traits> _(__is);
1593 __is.flags(ios_base::dec | ios_base::skipws);
1594 _UI __t[_N];
1595 for (size_t __i = 0; __i < _N; ++__i)
1596 __is >> __t[__i];
1597 if (!__is.fail())
1598 {
1599 for (size_t __i = 0; __i < _N; ++__i)
1600 __x.__x_[__i] = __t[__i];
1601 __x.__i_ = 0;
1602 }
1603 return __is;
1604}
1605
1606typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
1607 0x9908b0df, 11, 0xffffffff,
1608 7, 0x9d2c5680,
1609 15, 0xefc60000,
1610 18, 1812433253> mt19937;
1611typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
1612 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
1613 17, 0x71d67fffeda60000ULL,
1614 37, 0xfff7eee000000000ULL,
1615 43, 6364136223846793005ULL> mt19937_64;
1616
1617// subtract_with_carry_engine
1618
1619template<class _UIntType, size_t __w, size_t __s, size_t __r>
1620class subtract_with_carry_engine;
1621
1622template<class _UI, size_t _W, size_t _S, size_t _R>
1623bool
1624operator==(
1625 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1626 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1627
1628template<class _UI, size_t _W, size_t _S, size_t _R>
1629bool
1630operator!=(
1631 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1632 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1633
1634template <class _CharT, class _Traits,
1635 class _UI, size_t _W, size_t _S, size_t _R>
1636basic_ostream<_CharT, _Traits>&
1637operator<<(basic_ostream<_CharT, _Traits>& __os,
1638 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1639
1640template <class _CharT, class _Traits,
1641 class _UI, size_t _W, size_t _S, size_t _R>
1642basic_istream<_CharT, _Traits>&
1643operator>>(basic_istream<_CharT, _Traits>& __is,
1644 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1645
1646template<class _UIntType, size_t __w, size_t __s, size_t __r>
1647class subtract_with_carry_engine
1648{
1649public:
1650 // types
1651 typedef _UIntType result_type;
1652
1653private:
1654 result_type __x_[__r];
1655 result_type __c_;
1656 size_t __i_;
1657
1658 static const result_type _Dt = numeric_limits<result_type>::digits;
1659 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
1660 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
1661 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
1662 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
1663public:
1664 static const result_type _Min = 0;
1665 static const result_type _Max = __w == _Dt ? result_type(~0) :
1666 (result_type(1) << __w) - result_type(1);
1667 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
1668
1669 // engine characteristics
1670 static const/*expr*/ size_t word_size = __w;
1671 static const/*expr*/ size_t short_lag = __s;
1672 static const/*expr*/ size_t long_lag = __r;
1673 static const/*expr*/ result_type min() { return _Min; }
1674 static const/*expr*/ result_type max() { return _Max; }
1675 static const/*expr*/ result_type default_seed = 19780503u;
1676
1677 // constructors and seeding functions
1678 explicit subtract_with_carry_engine(result_type __sd = default_seed)
1679 {seed(__sd);}
1680 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
1681 {seed(__q);}
1682 void seed(result_type __sd = default_seed)
1683 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1684 template<class _Sseq>
1685 typename enable_if
1686 <
1687 !is_convertible<_Sseq, result_type>::value,
1688 void
1689 >::type
1690 seed(_Sseq& __q)
1691 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1692
1693 // generating functions
1694 result_type operator()();
1695 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1696
1697 template<class _UI, size_t _W, size_t _S, size_t _R>
1698 friend
1699 bool
1700 operator==(
1701 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1702 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1703
1704 template<class _UI, size_t _W, size_t _S, size_t _R>
1705 friend
1706 bool
1707 operator!=(
1708 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1709 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1710
1711 template <class _CharT, class _Traits,
1712 class _UI, size_t _W, size_t _S, size_t _R>
1713 friend
1714 basic_ostream<_CharT, _Traits>&
1715 operator<<(basic_ostream<_CharT, _Traits>& __os,
1716 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1717
1718 template <class _CharT, class _Traits,
1719 class _UI, size_t _W, size_t _S, size_t _R>
1720 friend
1721 basic_istream<_CharT, _Traits>&
1722 operator>>(basic_istream<_CharT, _Traits>& __is,
1723 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1724
1725private:
1726
1727 void seed(result_type __sd, integral_constant<unsigned, 1>);
1728 void seed(result_type __sd, integral_constant<unsigned, 2>);
1729 template<class _Sseq>
1730 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1731 template<class _Sseq>
1732 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1733};
1734
1735template<class _UIntType, size_t __w, size_t __s, size_t __r>
1736void
1737subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1738 integral_constant<unsigned, 1>)
1739{
1740 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1741 __e(__sd == 0u ? default_seed : __sd);
1742 for (size_t __i = 0; __i < __r; ++__i)
1743 __x_[__i] = static_cast<result_type>(__e() & _Max);
1744 __c_ = __x_[__r-1] == 0;
1745 __i_ = 0;
1746}
1747
1748template<class _UIntType, size_t __w, size_t __s, size_t __r>
1749void
1750subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1751 integral_constant<unsigned, 2>)
1752{
1753 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1754 __e(__sd == 0u ? default_seed : __sd);
1755 for (size_t __i = 0; __i < __r; ++__i)
1756 __x_[__i] = static_cast<result_type>(
1757 (__e() + ((uint64_t)__e() << 32)) & _Max);
1758 __c_ = __x_[__r-1] == 0;
1759 __i_ = 0;
1760}
1761
1762template<class _UIntType, size_t __w, size_t __s, size_t __r>
1763template<class _Sseq>
1764void
1765subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1766 integral_constant<unsigned, 1>)
1767{
1768 const unsigned __k = 1;
1769 uint32_t __ar[__r * __k];
1770 __q.generate(__ar, __ar + __r * __k);
1771 for (size_t __i = 0; __i < __r; ++__i)
1772 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1773 __c_ = __x_[__r-1] == 0;
1774 __i_ = 0;
1775}
1776
1777template<class _UIntType, size_t __w, size_t __s, size_t __r>
1778template<class _Sseq>
1779void
1780subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1781 integral_constant<unsigned, 2>)
1782{
1783 const unsigned __k = 2;
1784 uint32_t __ar[__r * __k];
1785 __q.generate(__ar, __ar + __r * __k);
1786 for (size_t __i = 0; __i < __r; ++__i)
1787 __x_[__i] = static_cast<result_type>(
1788 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1789 __c_ = __x_[__r-1] == 0;
1790 __i_ = 0;
1791}
1792
1793template<class _UIntType, size_t __w, size_t __s, size_t __r>
1794_UIntType
1795subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
1796{
1797 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
1798 result_type& __xr = __x_[__i_];
1799 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
1800 __xr = (__xs - __xr - __c_) & _Max;
1801 __c_ = __new_c;
1802 __i_ = (__i_ + 1) % __r;
1803 return __xr;
1804}
1805
1806template<class _UI, size_t _W, size_t _S, size_t _R>
1807bool
1808operator==(
1809 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1810 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1811{
1812 if (__x.__c_ != __y.__c_)
1813 return false;
1814 if (__x.__i_ == __y.__i_)
1815 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
1816 if (__x.__i_ == 0 || __y.__i_ == 0)
1817 {
1818 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
1819 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1820 __y.__x_ + __y.__i_))
1821 return false;
1822 if (__x.__i_ == 0)
1823 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
1824 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
1825 }
1826 if (__x.__i_ < __y.__i_)
1827 {
1828 size_t __j = _R - __y.__i_;
1829 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1830 __y.__x_ + __y.__i_))
1831 return false;
1832 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
1833 __y.__x_))
1834 return false;
1835 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1836 __y.__x_ + (_R - (__x.__i_ + __j)));
1837 }
1838 size_t __j = _R - __x.__i_;
1839 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1840 __x.__x_ + __x.__i_))
1841 return false;
1842 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
1843 __x.__x_))
1844 return false;
1845 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1846 __x.__x_ + (_R - (__y.__i_ + __j)));
1847}
1848
1849template<class _UI, size_t _W, size_t _S, size_t _R>
1850inline
1851bool
1852operator!=(
1853 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1854 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1855{
1856 return !(__x == __y);
1857}
1858
1859template <class _CharT, class _Traits,
1860 class _UI, size_t _W, size_t _S, size_t _R>
1861basic_ostream<_CharT, _Traits>&
1862operator<<(basic_ostream<_CharT, _Traits>& __os,
1863 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1864{
1865 __save_flags<_CharT, _Traits> _(__os);
1866 __os.flags(ios_base::dec | ios_base::left);
1867 _CharT __sp = __os.widen(' ');
1868 __os.fill(__sp);
1869 __os << __x.__x_[__x.__i_];
1870 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
1871 __os << __sp << __x.__x_[__j];
1872 for (size_t __j = 0; __j < __x.__i_; ++__j)
1873 __os << __sp << __x.__x_[__j];
1874 __os << __sp << __x.__c_;
1875 return __os;
1876}
1877
1878template <class _CharT, class _Traits,
1879 class _UI, size_t _W, size_t _S, size_t _R>
1880basic_istream<_CharT, _Traits>&
1881operator>>(basic_istream<_CharT, _Traits>& __is,
1882 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1883{
1884 __save_flags<_CharT, _Traits> _(__is);
1885 __is.flags(ios_base::dec | ios_base::skipws);
1886 _UI __t[_R+1];
1887 for (size_t __i = 0; __i < _R+1; ++__i)
1888 __is >> __t[__i];
1889 if (!__is.fail())
1890 {
1891 for (size_t __i = 0; __i < _R; ++__i)
1892 __x.__x_[__i] = __t[__i];
1893 __x.__c_ = __t[_R];
1894 __x.__i_ = 0;
1895 }
1896 return __is;
1897}
1898
1899typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
1900typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
1901
1902// discard_block_engine
1903
1904template<class _Engine, size_t __p, size_t __r>
1905class discard_block_engine
1906{
1907 _Engine __e_;
1908 int __n_;
1909
1910 static_assert( 0 < __r, "discard_block_engine invalid parameters");
1911 static_assert(__r <= __p, "discard_block_engine invalid parameters");
1912public:
1913 // types
1914 typedef typename _Engine::result_type result_type;
1915
1916 // engine characteristics
1917 static const/*expr*/ size_t block_size = __p;
1918 static const/*expr*/ size_t used_block = __r;
1919
1920 // Temporary work around for lack of constexpr
1921 static const result_type _Min = _Engine::_Min;
1922 static const result_type _Max = _Engine::_Max;
1923
1924 static const/*expr*/ result_type min() { return _Engine::min(); }
1925 static const/*expr*/ result_type max() { return _Engine::max(); }
1926
1927 // constructors and seeding functions
1928 discard_block_engine() : __n_(0) {}
1929// explicit discard_block_engine(const _Engine& __e);
1930// explicit discard_block_engine(_Engine&& __e);
1931 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
1932 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
1933 : __e_(__q), __n_(0) {}
1934 void seed() {__e_.seed(); __n_ = 0;}
1935 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
1936 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
1937
1938 // generating functions
1939 result_type operator()();
1940 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1941
1942 // property functions
1943 const _Engine& base() const {return __e_;}
1944
1945 template<class _Eng, size_t _P, size_t _R>
1946 friend
1947 bool
1948 operator==(
1949 const discard_block_engine<_Eng, _P, _R>& __x,
1950 const discard_block_engine<_Eng, _P, _R>& __y);
1951
1952 template<class _Eng, size_t _P, size_t _R>
1953 friend
1954 bool
1955 operator!=(
1956 const discard_block_engine<_Eng, _P, _R>& __x,
1957 const discard_block_engine<_Eng, _P, _R>& __y);
1958
1959 template <class _CharT, class _Traits,
1960 class _Eng, size_t _P, size_t _R>
1961 friend
1962 basic_ostream<_CharT, _Traits>&
1963 operator<<(basic_ostream<_CharT, _Traits>& __os,
1964 const discard_block_engine<_Eng, _P, _R>& __x);
1965
1966 template <class _CharT, class _Traits,
1967 class _Eng, size_t _P, size_t _R>
1968 friend
1969 basic_istream<_CharT, _Traits>&
1970 operator>>(basic_istream<_CharT, _Traits>& __is,
1971 discard_block_engine<_Eng, _P, _R>& __x);
1972};
1973
1974template<class _Engine, size_t __p, size_t __r>
1975typename discard_block_engine<_Engine, __p, __r>::result_type
1976discard_block_engine<_Engine, __p, __r>::operator()()
1977{
1978 if (__n_ >= __r)
1979 {
1980 __e_.discard(__p - __r);
1981 __n_ = 0;
1982 }
1983 ++__n_;
1984 return __e_();
1985}
1986
1987template<class _Eng, size_t _P, size_t _R>
1988inline
1989bool
1990operator==(const discard_block_engine<_Eng, _P, _R>& __x,
1991 const discard_block_engine<_Eng, _P, _R>& __y)
1992{
1993 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
1994}
1995
1996template<class _Eng, size_t _P, size_t _R>
1997inline
1998bool
1999operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2000 const discard_block_engine<_Eng, _P, _R>& __y)
2001{
2002 return !(__x == __y);
2003}
2004
2005template <class _CharT, class _Traits,
2006 class _Eng, size_t _P, size_t _R>
2007basic_ostream<_CharT, _Traits>&
2008operator<<(basic_ostream<_CharT, _Traits>& __os,
2009 const discard_block_engine<_Eng, _P, _R>& __x)
2010{
2011 __save_flags<_CharT, _Traits> _(__os);
2012 __os.flags(ios_base::dec | ios_base::left);
2013 _CharT __sp = __os.widen(' ');
2014 __os.fill(__sp);
2015 return __os << __x.__e_ << __sp << __x.__n_;
2016}
2017
2018template <class _CharT, class _Traits,
2019 class _Eng, size_t _P, size_t _R>
2020basic_istream<_CharT, _Traits>&
2021operator>>(basic_istream<_CharT, _Traits>& __is,
2022 discard_block_engine<_Eng, _P, _R>& __x)
2023{
2024 __save_flags<_CharT, _Traits> _(__is);
2025 __is.flags(ios_base::dec | ios_base::skipws);
2026 _Eng __e;
2027 int __n;
2028 __is >> __e >> __n;
2029 if (!__is.fail())
2030 {
2031 __x.__e_ = __e;
2032 __x.__n_ = __n;
2033 }
2034 return __is;
2035}
2036
2037typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2038typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2039
2040// independent_bits_engine
2041
2042template <unsigned long long _X, size_t _R>
2043struct __log2_imp
2044{
2045 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2046 : __log2_imp<_X, _R - 1>::value;
2047};
2048
2049template <unsigned long long _X>
2050struct __log2_imp<_X, 0>
2051{
2052 static const size_t value = 0;
2053};
2054
2055template <size_t _R>
2056struct __log2_imp<0, _R>
2057{
2058 static const size_t value = _R + 1;
2059};
2060
2061template <class _UI, _UI _X>
2062struct __log2
2063{
2064 static const size_t value = __log2_imp<_X,
2065 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2066};
2067
2068template<class _Engine, size_t __w, class _UIntType>
2069class independent_bits_engine
2070{
2071 template <class _UI, _UI _R0, size_t _W, size_t _M>
2072 class __get_n
2073 {
2074 static const size_t _Dt = numeric_limits<_UI>::digits;
2075 static const size_t _N = _W / _M + (_W % _M != 0);
2076 static const size_t _W0 = _W / _N;
2077 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2078 public:
2079 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2080 };
2081public:
2082 // types
2083 typedef _UIntType result_type;
2084
2085private:
2086 _Engine __e_;
2087
2088 static const result_type _Dt = numeric_limits<result_type>::digits;
2089 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2090 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2091
2092 typedef typename _Engine::result_type _Engine_result_type;
2093 typedef typename conditional
2094 <
2095 sizeof(_Engine_result_type) <= sizeof(result_type),
2096 result_type,
2097 _Engine_result_type
2098 >::type _Working_result_type;
2099 // Temporary work around for lack of constexpr
2100 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2101 + _Working_result_type(1);
2102 static const size_t __m = __log2<_Working_result_type, _R>::value;
2103 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2104 static const size_t __w0 = __w / __n;
2105 static const size_t __n0 = __n - __w % __n;
2106 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2107 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2108 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2109 (_R >> __w0) << __w0;
2110 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2111 (_R >> (__w0+1)) << (__w0+1);
2112 static const _Engine_result_type __mask0 = __w0 > 0 ?
2113 _Engine_result_type(~0) >> (_EDt - __w0) :
2114 _Engine_result_type(0);
2115 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2116 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2117 _Engine_result_type(~0);
2118public:
2119 static const result_type _Min = 0;
2120 static const result_type _Max = __w == _Dt ? result_type(~0) :
2121 (result_type(1) << __w) - result_type(1);
2122 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2123
2124 // engine characteristics
2125 static const/*expr*/ result_type min() { return _Min; }
2126 static const/*expr*/ result_type max() { return _Max; }
2127
2128 // constructors and seeding functions
2129 independent_bits_engine() {}
2130// explicit independent_bits_engine(const _Engine& __e);
2131// explicit independent_bits_engine(_Engine&& __e);
2132 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2133 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2134 : __e_(__q) {}
2135 void seed() {__e_.seed();}
2136 void seed(result_type __sd) {__e_.seed(__sd);}
2137 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2138
2139 // generating functions
2140 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2141 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2142
2143 // property functions
2144 const _Engine& base() const {return __e_;}
2145
2146 template<class _Eng, size_t _W, class _UI>
2147 friend
2148 bool
2149 operator==(
2150 const independent_bits_engine<_Eng, _W, _UI>& __x,
2151 const independent_bits_engine<_Eng, _W, _UI>& __y);
2152
2153 template<class _Eng, size_t _W, class _UI>
2154 friend
2155 bool
2156 operator!=(
2157 const independent_bits_engine<_Eng, _W, _UI>& __x,
2158 const independent_bits_engine<_Eng, _W, _UI>& __y);
2159
2160 template <class _CharT, class _Traits,
2161 class _Eng, size_t _W, class _UI>
2162 friend
2163 basic_ostream<_CharT, _Traits>&
2164 operator<<(basic_ostream<_CharT, _Traits>& __os,
2165 const independent_bits_engine<_Eng, _W, _UI>& __x);
2166
2167 template <class _CharT, class _Traits,
2168 class _Eng, size_t _W, class _UI>
2169 friend
2170 basic_istream<_CharT, _Traits>&
2171 operator>>(basic_istream<_CharT, _Traits>& __is,
2172 independent_bits_engine<_Eng, _W, _UI>& __x);
2173
2174private:
2175 result_type __eval(false_type);
2176 result_type __eval(true_type);
2177
2178 template <size_t __count>
2179 static
2180 typename enable_if
2181 <
2182 __count < _Dt,
2183 result_type
2184 >::type
2185 __lshift(result_type __x) {return __x << __count;}
2186
2187 template <size_t __count>
2188 static
2189 typename enable_if
2190 <
2191 (__count >= _Dt),
2192 result_type
2193 >::type
2194 __lshift(result_type __x) {return result_type(0);}
2195};
2196
2197template<class _Engine, size_t __w, class _UIntType>
2198inline
2199_UIntType
2200independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2201{
2202 return static_cast<result_type>(__e_() & __mask0);
2203}
2204
2205template<class _Engine, size_t __w, class _UIntType>
2206_UIntType
2207independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2208{
2209 result_type _S = 0;
2210 for (size_t __k = 0; __k < __n0; ++__k)
2211 {
2212 _Engine_result_type __u;
2213 do
2214 {
2215 __u = __e_() - _Engine::min();
2216 } while (__u >= __y0);
2217 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2218 }
2219 for (size_t __k = __n0; __k < __n; ++__k)
2220 {
2221 _Engine_result_type __u;
2222 do
2223 {
2224 __u = __e_() - _Engine::min();
2225 } while (__u >= __y1);
2226 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2227 }
2228 return _S;
2229}
2230
2231template<class _Eng, size_t _W, class _UI>
2232inline
2233bool
2234operator==(
2235 const independent_bits_engine<_Eng, _W, _UI>& __x,
2236 const independent_bits_engine<_Eng, _W, _UI>& __y)
2237{
2238 return __x.base() == __y.base();
2239}
2240
2241template<class _Eng, size_t _W, class _UI>
2242inline
2243bool
2244operator!=(
2245 const independent_bits_engine<_Eng, _W, _UI>& __x,
2246 const independent_bits_engine<_Eng, _W, _UI>& __y)
2247{
2248 return !(__x == __y);
2249}
2250
2251template <class _CharT, class _Traits,
2252 class _Eng, size_t _W, class _UI>
2253basic_ostream<_CharT, _Traits>&
2254operator<<(basic_ostream<_CharT, _Traits>& __os,
2255 const independent_bits_engine<_Eng, _W, _UI>& __x)
2256{
2257 return __os << __x.base();
2258}
2259
2260template <class _CharT, class _Traits,
2261 class _Eng, size_t _W, class _UI>
2262basic_istream<_CharT, _Traits>&
2263operator>>(basic_istream<_CharT, _Traits>& __is,
2264 independent_bits_engine<_Eng, _W, _UI>& __x)
2265{
2266 _Eng __e;
2267 __is >> __e;
2268 if (!__is.fail())
2269 __x.__e_ = __e;
2270 return __is;
2271}
2272
2273// shuffle_order_engine
2274
2275template <uint64_t _Xp, uint64_t _Yp>
2276struct __ugcd
2277{
2278 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2279};
2280
2281template <uint64_t _Xp>
2282struct __ugcd<_Xp, 0>
2283{
2284 static const uint64_t value = _Xp;
2285};
2286
2287template <uint64_t _N, uint64_t _D>
2288class __uratio
2289{
2290 static_assert(_D != 0, "__uratio divide by 0");
2291 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2292public:
2293 static const uint64_t num = _N / __gcd;
2294 static const uint64_t den = _D / __gcd;
2295
2296 typedef __uratio<num, den> type;
2297};
2298
2299template<class _Engine, size_t __k>
2300class shuffle_order_engine
2301{
2302 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2303public:
2304 // types
2305 typedef typename _Engine::result_type result_type;
2306
2307private:
2308 _Engine __e_;
2309 result_type _V_[__k];
2310 result_type _Y_;
2311
2312public:
2313 // engine characteristics
2314 static const/*expr*/ size_t table_size = __k;
2315
2316 static const result_type _Min = _Engine::_Min;
2317 static const result_type _Max = _Engine::_Max;
2318 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2319 static const/*expr*/ result_type min() { return _Min; }
2320 static const/*expr*/ result_type max() { return _Max; }
2321
2322 static const unsigned long long _R = _Max - _Min + 1ull;
2323
2324 // constructors and seeding functions
2325 shuffle_order_engine() {__init();}
2326// explicit shuffle_order_engine(const _Engine& __e);
2327// explicit shuffle_order_engine(_Engine&& e);
2328 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
2329 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
2330 : __e_(__q) {__init();}
2331 void seed() {__e_.seed(); __init();}
2332 void seed(result_type __sd) {__e_.seed(__sd); __init();}
2333 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
2334
2335 // generating functions
2336 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2337 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2338
2339 // property functions
2340 const _Engine& base() const {return __e_;}
2341
2342private:
2343 template<class _Eng, size_t _K>
2344 friend
2345 bool
2346 operator==(
2347 const shuffle_order_engine<_Eng, _K>& __x,
2348 const shuffle_order_engine<_Eng, _K>& __y);
2349
2350 template<class _Eng, size_t _K>
2351 friend
2352 bool
2353 operator!=(
2354 const shuffle_order_engine<_Eng, _K>& __x,
2355 const shuffle_order_engine<_Eng, _K>& __y);
2356
2357 template <class _CharT, class _Traits,
2358 class _Eng, size_t _K>
2359 friend
2360 basic_ostream<_CharT, _Traits>&
2361 operator<<(basic_ostream<_CharT, _Traits>& __os,
2362 const shuffle_order_engine<_Eng, _K>& __x);
2363
2364 template <class _CharT, class _Traits,
2365 class _Eng, size_t _K>
2366 friend
2367 basic_istream<_CharT, _Traits>&
2368 operator>>(basic_istream<_CharT, _Traits>& __is,
2369 shuffle_order_engine<_Eng, _K>& __x);
2370
2371 void __init()
2372 {
2373 for (size_t __i = 0; __i < __k; ++__i)
2374 _V_[__i] = __e_();
2375 _Y_ = __e_();
2376 }
2377
2378 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
2379 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
2380
2381 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
2382 result_type __eval2(true_type) {return __evalf<__k, 0>();}
2383
2384 template <uint64_t _N, uint64_t _D>
2385 typename enable_if
2386 <
2387 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
2388 result_type
2389 >::type
2390 __eval(__uratio<_N, _D>)
2391 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
2392
2393 template <uint64_t _N, uint64_t _D>
2394 typename enable_if
2395 <
2396 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
2397 result_type
2398 >::type
2399 __eval(__uratio<_N, _D>)
2400 {
2401 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
2402 / __uratio<_N, _D>::den);
2403 _Y_ = _V_[__j];
2404 _V_[__j] = __e_();
2405 return _Y_;
2406 }
2407
2408 template <uint64_t __n, uint64_t __d>
2409 result_type __evalf()
2410 {
2411 const double _F = __d == 0 ?
2412 __n / (2. * 0x8000000000000000ull) :
2413 __n / (double)__d;
2414 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
2415 _Y_ = _V_[__j];
2416 _V_[__j] = __e_();
2417 return _Y_;
2418 }
2419};
2420
2421template<class _Eng, size_t _K>
2422bool
2423operator==(
2424 const shuffle_order_engine<_Eng, _K>& __x,
2425 const shuffle_order_engine<_Eng, _K>& __y)
2426{
2427 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
2428 __x.__e_ == __y.__e_;
2429}
2430
2431template<class _Eng, size_t _K>
2432inline
2433bool
2434operator!=(
2435 const shuffle_order_engine<_Eng, _K>& __x,
2436 const shuffle_order_engine<_Eng, _K>& __y)
2437{
2438 return !(__x == __y);
2439}
2440
2441template <class _CharT, class _Traits,
2442 class _Eng, size_t _K>
2443basic_ostream<_CharT, _Traits>&
2444operator<<(basic_ostream<_CharT, _Traits>& __os,
2445 const shuffle_order_engine<_Eng, _K>& __x)
2446{
2447 __save_flags<_CharT, _Traits> _(__os);
2448 __os.flags(ios_base::dec | ios_base::left);
2449 _CharT __sp = __os.widen(' ');
2450 __os.fill(__sp);
2451 __os << __x.__e_ << __sp << __x._V_[0];
2452 for (size_t __i = 1; __i < _K; ++__i)
2453 __os << __sp << __x._V_[__i];
2454 return __os << __sp << __x._Y_;
2455}
2456
2457template <class _CharT, class _Traits,
2458 class _Eng, size_t _K>
2459basic_istream<_CharT, _Traits>&
2460operator>>(basic_istream<_CharT, _Traits>& __is,
2461 shuffle_order_engine<_Eng, _K>& __x)
2462{
2463 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
2464 __save_flags<_CharT, _Traits> _(__is);
2465 __is.flags(ios_base::dec | ios_base::skipws);
2466 _Eng __e;
2467 result_type _V[_K+1];
2468 __is >> __e;
2469 for (size_t __i = 0; __i < _K+1; ++__i)
2470 __is >> _V[__i];
2471 if (!__is.fail())
2472 {
2473 __x.__e_ = __e;
2474 for (size_t __i = 0; __i < _K; ++__i)
2475 __x._V_[__i] = _V[__i];
2476 __x._Y_ = _V[_K];
2477 }
2478 return __is;
2479}
2480
2481typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
2482
2483// random_device
2484
2485class random_device
2486{
2487 int __f_;
2488public:
2489 // types
2490 typedef unsigned result_type;
2491
2492 // generator characteristics
2493 static const result_type _Min = 0;
2494 static const result_type _Max = 0xFFFFFFFFu;
2495
2496 static const/*expr*/ result_type min() { return _Min;}
2497 static const/*expr*/ result_type max() { return _Max;}
2498
2499 // constructors
2500 explicit random_device(const string& __token = "/dev/urandom");
2501 ~random_device();
2502
2503 // generating functions
2504 result_type operator()();
2505
2506 // property functions
2507 double entropy() const;
2508
2509private:
2510 // no copy functions
2511 random_device(const random_device&); // = delete;
2512 random_device& operator=(const random_device&); // = delete;
2513};
2514
2515// seed_seq
2516
2517class seed_seq
2518{
2519public:
2520 // types
2521 typedef uint32_t result_type;
2522
2523private:
2524 vector<result_type> __v_;
2525
2526 template<class _InputIterator>
2527 void init(_InputIterator __first, _InputIterator __last);
2528public:
2529 // constructors
2530 seed_seq() {}
2531 template<class _Tp>
2532 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
2533
2534 template<class _InputIterator>
2535 seed_seq(_InputIterator __first, _InputIterator __last)
2536 {init(__first, __last);}
2537
2538 // generating functions
2539 template<class _RandomAccessIterator>
2540 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
2541
2542 // property functions
2543 size_t size() const {return __v_.size();}
2544 template<class _OutputIterator>
2545 void param(_OutputIterator __dest) const
2546 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
2547
2548private:
2549 // no copy functions
2550 seed_seq(const seed_seq&); // = delete;
2551 void operator=(const seed_seq&); // = delete;
2552
2553 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
2554};
2555
2556template<class _InputIterator>
2557void
2558seed_seq::init(_InputIterator __first, _InputIterator __last)
2559{
2560 for (_InputIterator __s = __first; __s != __last; ++__s)
2561 __v_.push_back(*__s & 0xFFFFFFFF);
2562}
2563
2564template<class _RandomAccessIterator>
2565void
2566seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
2567{
2568 if (__first != __last)
2569 {
2570 _STD::fill(__first, __last, 0x8b8b8b8b);
2571 const size_t __n = static_cast<size_t>(__last - __first);
2572 const size_t __s = __v_.size();
2573 const size_t __t = (__n >= 623) ? 11
2574 : (__n >= 68) ? 7
2575 : (__n >= 39) ? 5
2576 : (__n >= 7) ? 3
2577 : (__n - 1) / 2;
2578 const size_t __p = (__n - __t) / 2;
2579 const size_t __q = __p + __t;
2580 const size_t __m = _STD::max(__s + 1, __n);
2581 // __k = 0;
2582 {
2583 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
2584 ^ __first[__n - 1]);
2585 __first[__p] += __r;
2586 __r += __s;
2587 __first[__q] += __r;
2588 __first[0] = __r;
2589 }
2590 for (size_t __k = 1; __k <= __s; ++__k)
2591 {
2592 const size_t __kmodn = __k % __n;
2593 const size_t __kpmodn = (__k + __p) % __n;
2594 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2595 ^ __first[(__k - 1) % __n]);
2596 __first[__kpmodn] += __r;
2597 __r += __kmodn + __v_[__k-1];
2598 __first[(__k + __q) % __n] += __r;
2599 __first[__kmodn] = __r;
2600 }
2601 for (size_t __k = __s + 1; __k < __m; ++__k)
2602 {
2603 const size_t __kmodn = __k % __n;
2604 const size_t __kpmodn = (__k + __p) % __n;
2605 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2606 ^ __first[(__k - 1) % __n]);
2607 __first[__kpmodn] += __r;
2608 __r += __kmodn;
2609 __first[(__k + __q) % __n] += __r;
2610 __first[__kmodn] = __r;
2611 }
2612 for (size_t __k = __m; __k < __m + __n; ++__k)
2613 {
2614 const size_t __kmodn = __k % __n;
2615 const size_t __kpmodn = (__k + __p) % __n;
2616 result_type __r = 1566083941 * _T(__first[__kmodn] +
2617 __first[__kpmodn] +
2618 __first[(__k - 1) % __n]);
2619 __first[__kpmodn] ^= __r;
2620 __r -= __kmodn;
2621 __first[(__k + __q) % __n] ^= __r;
2622 __first[__kmodn] = __r;
2623 }
2624 }
2625}
2626
Howard Hinnant30a840f2010-05-12 17:08:57 +00002627// generate_canonical
2628
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629template<class _RealType, size_t __bits, class _URNG>
2630_RealType
2631generate_canonical(_URNG& __g)
2632{
2633 const size_t _Dt = numeric_limits<_RealType>::digits;
2634 const size_t __b = _Dt < __bits ? _Dt : __bits;
2635 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
2636 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
2637 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
2638 _RealType __base = _R;
2639 _RealType _S = __g() - _URNG::_Min;
2640 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
2641 _S += (__g() - _URNG::_Min) * __base;
2642 return _S / __base;
2643}
2644
2645// __independent_bits_engine
2646
2647template<class _Engine, class _UIntType>
2648class __independent_bits_engine
2649{
2650public:
2651 // types
2652 typedef _UIntType result_type;
2653
2654private:
2655 typedef typename _Engine::result_type _Engine_result_type;
2656 typedef typename conditional
2657 <
2658 sizeof(_Engine_result_type) <= sizeof(result_type),
2659 result_type,
2660 _Engine_result_type
2661 >::type _Working_result_type;
2662
2663 _Engine& __e_;
2664 size_t __w_;
2665 size_t __w0_;
2666 size_t __n_;
2667 size_t __n0_;
2668 _Working_result_type __y0_;
2669 _Working_result_type __y1_;
2670 _Engine_result_type __mask0_;
2671 _Engine_result_type __mask1_;
2672
2673 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2674 + _Working_result_type(1);
2675 static const size_t __m = __log2<_Working_result_type, _R>::value;
2676 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2677 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2678
2679public:
2680 // constructors and seeding functions
2681 __independent_bits_engine(_Engine& __e, size_t __w);
2682
2683 // generating functions
2684 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2685
2686private:
2687 result_type __eval(false_type);
2688 result_type __eval(true_type);
2689};
2690
2691template<class _Engine, class _UIntType>
2692__independent_bits_engine<_Engine, _UIntType>
2693 ::__independent_bits_engine(_Engine& __e, size_t __w)
2694 : __e_(__e),
2695 __w_(__w)
2696{
2697 __n_ = __w_ / __m + (__w_ % __m != 0);
2698 __w0_ = __w_ / __n_;
2699 if (_R == 0)
2700 __y0_ = _R;
2701 else if (__w0_ < _WDt)
2702 __y0_ = (_R >> __w0_) << __w0_;
2703 else
2704 __y0_ = 0;
2705 if (_R - __y0_ > __y0_ / __n_)
2706 {
2707 ++__n_;
2708 __w0_ = __w_ / __n_;
2709 if (__w0_ < _WDt)
2710 __y0_ = (_R >> __w0_) << __w0_;
2711 else
2712 __y0_ = 0;
2713 }
2714 __n0_ = __n_ - __w_ % __n_;
2715 if (__w0_ < _WDt - 1)
2716 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2717 else
2718 __y1_ = 0;
2719 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2720 _Engine_result_type(0);
2721 __mask1_ = __w0_ < _EDt - 1 ?
2722 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2723 _Engine_result_type(~0);
2724}
2725
2726template<class _Engine, class _UIntType>
2727inline
2728_UIntType
2729__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2730{
2731 return static_cast<result_type>(__e_() & __mask0_);
2732}
2733
2734template<class _Engine, class _UIntType>
2735_UIntType
2736__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2737{
2738 result_type _S = 0;
2739 for (size_t __k = 0; __k < __n0_; ++__k)
2740 {
2741 _Engine_result_type __u;
2742 do
2743 {
2744 __u = __e_() - _Engine::min();
2745 } while (__u >= __y0_);
2746 if (__w0_ < _EDt)
2747 _S <<= __w0_;
2748 else
2749 _S = 0;
2750 _S += __u & __mask0_;
2751 }
2752 for (size_t __k = __n0_; __k < __n_; ++__k)
2753 {
2754 _Engine_result_type __u;
2755 do
2756 {
2757 __u = __e_() - _Engine::min();
2758 } while (__u >= __y1_);
2759 if (__w0_ < _EDt - 1)
2760 _S <<= __w0_ + 1;
2761 else
2762 _S = 0;
2763 _S += __u & __mask1_;
2764 }
2765 return _S;
2766}
2767
Howard Hinnant30a840f2010-05-12 17:08:57 +00002768// uniform_int_distribution
2769
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770template<class _IntType = int>
2771class uniform_int_distribution
2772{
2773public:
2774 // types
2775 typedef _IntType result_type;
2776
2777 class param_type
2778 {
2779 result_type __a_;
2780 result_type __b_;
2781 public:
2782 typedef uniform_int_distribution distribution_type;
2783
2784 explicit param_type(result_type __a = 0,
2785 result_type __b = numeric_limits<result_type>::max())
2786 : __a_(__a), __b_(__b) {}
2787
2788 result_type a() const {return __a_;}
2789 result_type b() const {return __b_;}
2790
2791 friend bool operator==(const param_type& __x, const param_type& __y)
2792 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2793 friend bool operator!=(const param_type& __x, const param_type& __y)
2794 {return !(__x == __y);}
2795 };
2796
2797private:
2798 param_type __p_;
2799
2800public:
2801 // constructors and reset functions
2802 explicit uniform_int_distribution(result_type __a = 0,
2803 result_type __b = numeric_limits<result_type>::max())
2804 : __p_(param_type(__a, __b)) {}
2805 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2806 void reset() {}
2807
2808 // generating functions
2809 template<class _URNG> result_type operator()(_URNG& __g)
2810 {return (*this)(__g, __p_);}
2811 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2812
2813 // property functions
2814 result_type a() const {return __p_.a();}
2815 result_type b() const {return __p_.b();}
2816
2817 param_type param() const {return __p_;}
2818 void param(const param_type& __p) {__p_ = __p;}
2819
2820 result_type min() const {return a();}
2821 result_type max() const {return b();}
2822
2823 friend bool operator==(const uniform_int_distribution& __x,
2824 const uniform_int_distribution& __y)
2825 {return __x.__p_ == __y.__p_;}
2826 friend bool operator!=(const uniform_int_distribution& __x,
2827 const uniform_int_distribution& __y)
2828 {return !(__x == __y);}
2829};
2830
2831template<class _IntType>
2832template<class _URNG>
2833typename uniform_int_distribution<_IntType>::result_type
2834uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2835{
2836 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2837 uint32_t, uint64_t>::type _UIntType;
2838 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2839 if (_R == 1)
2840 return __p.a();
2841 const size_t _Dt = numeric_limits<_UIntType>::digits;
2842 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2843 if (_R == 0)
2844 return static_cast<result_type>(_Eng(__g, _Dt)());
2845 size_t __w = _Dt - __clz(_R) - 1;
2846 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2847 ++__w;
2848 _Eng __e(__g, __w);
2849 _UIntType __u;
2850 do
2851 {
2852 __u = __e();
2853 } while (__u >= _R);
2854 return static_cast<result_type>(__u + __p.a());
2855}
2856
2857template <class _CharT, class _Traits, class _IT>
2858basic_ostream<_CharT, _Traits>&
2859operator<<(basic_ostream<_CharT, _Traits>& __os,
2860 const uniform_int_distribution<_IT>& __x)
2861{
2862 __save_flags<_CharT, _Traits> _(__os);
2863 __os.flags(ios_base::dec | ios_base::left);
2864 _CharT __sp = __os.widen(' ');
2865 __os.fill(__sp);
2866 return __os << __x.a() << __sp << __x.b();
2867}
2868
2869template <class _CharT, class _Traits, class _IT>
2870basic_istream<_CharT, _Traits>&
2871operator>>(basic_istream<_CharT, _Traits>& __is,
2872 uniform_int_distribution<_IT>& __x)
2873{
2874 typedef uniform_int_distribution<_IT> _Eng;
2875 typedef typename _Eng::result_type result_type;
2876 typedef typename _Eng::param_type param_type;
2877 __save_flags<_CharT, _Traits> _(__is);
2878 __is.flags(ios_base::dec | ios_base::skipws);
2879 result_type __a;
2880 result_type __b;
2881 __is >> __a >> __b;
2882 if (!__is.fail())
2883 __x.param(param_type(__a, __b));
2884 return __is;
2885}
2886
Howard Hinnant30a840f2010-05-12 17:08:57 +00002887// uniform_real_distribution
2888
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002889template<class _RealType = double>
2890class uniform_real_distribution
2891{
2892public:
2893 // types
2894 typedef _RealType result_type;
2895
2896 class param_type
2897 {
2898 result_type __a_;
2899 result_type __b_;
2900 public:
2901 typedef uniform_real_distribution distribution_type;
2902
2903 explicit param_type(result_type __a = 0,
2904 result_type __b = 1)
2905 : __a_(__a), __b_(__b) {}
2906
2907 result_type a() const {return __a_;}
2908 result_type b() const {return __b_;}
2909
2910 friend bool operator==(const param_type& __x, const param_type& __y)
2911 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2912 friend bool operator!=(const param_type& __x, const param_type& __y)
2913 {return !(__x == __y);}
2914 };
2915
2916private:
2917 param_type __p_;
2918
2919public:
2920 // constructors and reset functions
2921 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
2922 : __p_(param_type(__a, __b)) {}
2923 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
2924 void reset() {}
2925
2926 // generating functions
2927 template<class _URNG> result_type operator()(_URNG& __g)
2928 {return (*this)(__g, __p_);}
2929 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2930
2931 // property functions
2932 result_type a() const {return __p_.a();}
2933 result_type b() const {return __p_.b();}
2934
2935 param_type param() const {return __p_;}
2936 void param(const param_type& __p) {__p_ = __p;}
2937
2938 result_type min() const {return a();}
2939 result_type max() const {return b();}
2940
2941 friend bool operator==(const uniform_real_distribution& __x,
2942 const uniform_real_distribution& __y)
2943 {return __x.__p_ == __y.__p_;}
2944 friend bool operator!=(const uniform_real_distribution& __x,
2945 const uniform_real_distribution& __y)
2946 {return !(__x == __y);}
2947};
2948
2949template<class _RealType>
2950template<class _URNG>
2951inline
2952typename uniform_real_distribution<_RealType>::result_type
2953uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
2954{
2955 return (__p.b() - __p.a())
2956 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
2957 + __p.a();
2958}
2959
2960template <class _CharT, class _Traits, class _RT>
2961basic_ostream<_CharT, _Traits>&
2962operator<<(basic_ostream<_CharT, _Traits>& __os,
2963 const uniform_real_distribution<_RT>& __x)
2964{
2965 __save_flags<_CharT, _Traits> _(__os);
2966 __os.flags(ios_base::dec | ios_base::left);
2967 _CharT __sp = __os.widen(' ');
2968 __os.fill(__sp);
2969 return __os << __x.a() << __sp << __x.b();
2970}
2971
2972template <class _CharT, class _Traits, class _RT>
2973basic_istream<_CharT, _Traits>&
2974operator>>(basic_istream<_CharT, _Traits>& __is,
2975 uniform_real_distribution<_RT>& __x)
2976{
2977 typedef uniform_real_distribution<_RT> _Eng;
2978 typedef typename _Eng::result_type result_type;
2979 typedef typename _Eng::param_type param_type;
2980 __save_flags<_CharT, _Traits> _(__is);
2981 __is.flags(ios_base::dec | ios_base::skipws);
2982 result_type __a;
2983 result_type __b;
2984 __is >> __a >> __b;
2985 if (!__is.fail())
2986 __x.param(param_type(__a, __b));
2987 return __is;
2988}
2989
Howard Hinnant30a840f2010-05-12 17:08:57 +00002990// bernoulli_distribution
2991
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002992class bernoulli_distribution
2993{
2994public:
2995 // types
2996 typedef bool result_type;
2997
2998 class param_type
2999 {
3000 double __p_;
3001 public:
3002 typedef bernoulli_distribution distribution_type;
3003
3004 explicit param_type(double __p = 0.5) : __p_(__p) {}
3005
3006 double p() const {return __p_;}
3007
3008 friend bool operator==(const param_type& __x, const param_type& __y)
3009 {return __x.__p_ == __y.__p_;}
3010 friend bool operator!=(const param_type& __x, const param_type& __y)
3011 {return !(__x == __y);}
3012 };
3013
3014private:
3015 param_type __p_;
3016
3017public:
3018 // constructors and reset functions
3019 explicit bernoulli_distribution(double __p = 0.5)
3020 : __p_(param_type(__p)) {}
3021 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3022 void reset() {}
3023
3024 // generating functions
3025 template<class _URNG> result_type operator()(_URNG& __g)
3026 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003027 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003028
3029 // property functions
3030 double p() const {return __p_.p();}
3031
3032 param_type param() const {return __p_;}
3033 void param(const param_type& __p) {__p_ = __p;}
3034
3035 result_type min() const {return false;}
3036 result_type max() const {return true;}
3037
3038 friend bool operator==(const bernoulli_distribution& __x,
3039 const bernoulli_distribution& __y)
3040 {return __x.__p_ == __y.__p_;}
3041 friend bool operator!=(const bernoulli_distribution& __x,
3042 const bernoulli_distribution& __y)
3043 {return !(__x == __y);}
3044};
3045
Howard Hinnant03aad812010-05-11 23:26:59 +00003046template<class _URNG>
3047inline
3048bernoulli_distribution::result_type
3049bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3050{
3051 return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.);
3052}
3053
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054template <class _CharT, class _Traits>
3055basic_ostream<_CharT, _Traits>&
3056operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3057{
3058 __save_flags<_CharT, _Traits> _(__os);
3059 __os.flags(ios_base::dec | ios_base::left);
3060 _CharT __sp = __os.widen(' ');
3061 __os.fill(__sp);
3062 return __os << __x.p();
3063}
3064
3065template <class _CharT, class _Traits>
3066basic_istream<_CharT, _Traits>&
3067operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3068{
3069 typedef bernoulli_distribution _Eng;
3070 typedef typename _Eng::param_type param_type;
3071 __save_flags<_CharT, _Traits> _(__is);
3072 __is.flags(ios_base::dec | ios_base::skipws);
3073 double __p;
3074 __is >> __p;
3075 if (!__is.fail())
3076 __x.param(param_type(__p));
3077 return __is;
3078}
3079
Howard Hinnant30a840f2010-05-12 17:08:57 +00003080// binomial_distribution
3081
Howard Hinnant03aad812010-05-11 23:26:59 +00003082template<class _IntType = int>
3083class binomial_distribution
3084{
3085public:
3086 // types
3087 typedef _IntType result_type;
3088
3089 class param_type
3090 {
3091 result_type __t_;
3092 double __p_;
3093 public:
3094 typedef binomial_distribution distribution_type;
3095
3096 explicit param_type(result_type __t = 1, double __p = 0.5)
3097 : __t_(__t), __p_(__p) {}
3098
3099 result_type t() const {return __t_;}
3100 double p() const {return __p_;}
3101
3102 friend bool operator==(const param_type& __x, const param_type& __y)
3103 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3104 friend bool operator!=(const param_type& __x, const param_type& __y)
3105 {return !(__x == __y);}
3106 };
3107
3108private:
3109 param_type __p_;
3110
3111public:
3112 // constructors and reset functions
3113 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3114 : __p_(param_type(__t, __p)) {}
3115 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3116 void reset() {}
3117
3118 // generating functions
3119 template<class _URNG> result_type operator()(_URNG& __g)
3120 {return (*this)(__g, __p_);}
3121 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3122
3123 // property functions
3124 result_type t() const {return __p_.t();}
3125 double p() const {return __p_.p();}
3126
3127 param_type param() const {return __p_;}
3128 void param(const param_type& __p) {__p_ = __p;}
3129
3130 result_type min() const {return 0;}
3131 result_type max() const {return t();}
3132
3133 friend bool operator==(const binomial_distribution& __x,
3134 const binomial_distribution& __y)
3135 {return __x.__p_ == __y.__p_;}
3136 friend bool operator!=(const binomial_distribution& __x,
3137 const binomial_distribution& __y)
3138 {return !(__x == __y);}
3139};
3140
3141template<class _IntType>
3142template<class _URNG>
3143_IntType
3144binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3145{
3146 bernoulli_distribution __bd(__p.p());
3147 _IntType __r = 0;
3148 _IntType __t = __p.t();
3149 for (_IntType __i = 0; __i < __t; ++__i)
3150 __r += __bd(__g);
3151 return __r;
3152}
3153
3154template <class _CharT, class _Traits, class _IntType>
3155basic_ostream<_CharT, _Traits>&
3156operator<<(basic_ostream<_CharT, _Traits>& __os,
3157 const binomial_distribution<_IntType>& __x)
3158{
3159 __save_flags<_CharT, _Traits> _(__os);
3160 __os.flags(ios_base::dec | ios_base::left);
3161 _CharT __sp = __os.widen(' ');
3162 __os.fill(__sp);
3163 return __os << __x.t() << __sp << __x.p();
3164}
3165
3166template <class _CharT, class _Traits, class _IntType>
3167basic_istream<_CharT, _Traits>&
3168operator>>(basic_istream<_CharT, _Traits>& __is,
3169 binomial_distribution<_IntType>& __x)
3170{
3171 typedef binomial_distribution<_IntType> _Eng;
3172 typedef typename _Eng::result_type result_type;
3173 typedef typename _Eng::param_type param_type;
3174 __save_flags<_CharT, _Traits> _(__is);
3175 __is.flags(ios_base::dec | ios_base::skipws);
3176 result_type __t;
3177 double __p;
3178 __is >> __t >> __p;
3179 if (!__is.fail())
3180 __x.param(param_type(__t, __p));
3181 return __is;
3182}
3183
Howard Hinnant30a840f2010-05-12 17:08:57 +00003184// exponential_distribution
3185
3186template<class _RealType = double>
3187class exponential_distribution
3188{
3189public:
3190 // types
3191 typedef _RealType result_type;
3192
3193 class param_type
3194 {
3195 result_type __lambda_;
3196 public:
3197 typedef exponential_distribution distribution_type;
3198
3199 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3200
3201 result_type lambda() const {return __lambda_;}
3202
3203 friend bool operator==(const param_type& __x, const param_type& __y)
3204 {return __x.__lambda_ == __y.__lambda_;}
3205 friend bool operator!=(const param_type& __x, const param_type& __y)
3206 {return !(__x == __y);}
3207 };
3208
3209private:
3210 param_type __p_;
3211
3212public:
3213 // constructors and reset functions
3214 explicit exponential_distribution(result_type __lambda = 1)
3215 : __p_(param_type(__lambda)) {}
3216 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3217 void reset() {}
3218
3219 // generating functions
3220 template<class _URNG> result_type operator()(_URNG& __g)
3221 {return (*this)(__g, __p_);}
3222 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3223
3224 // property functions
3225 result_type lambda() const {return __p_.lambda();}
3226
3227 param_type param() const {return __p_;}
3228 void param(const param_type& __p) {__p_ = __p;}
3229
3230 result_type min() const {return 0;}
3231 result_type max() const
3232 {return -std::log(1-std::nextafter(result_type(1), result_type(-1))) /
3233 __p_.lambda();}
3234
3235 friend bool operator==(const exponential_distribution& __x,
3236 const exponential_distribution& __y)
3237 {return __x.__p_ == __y.__p_;}
3238 friend bool operator!=(const exponential_distribution& __x,
3239 const exponential_distribution& __y)
3240 {return !(__x == __y);}
3241};
3242
3243template <class _RealType>
3244template<class _URNG>
3245_RealType
3246exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3247{
3248 return -_STD::log
3249 (
3250 result_type(1) -
3251 _STD::generate_canonical<result_type,
3252 numeric_limits<result_type>::digits>(__g)
3253 )
3254 / __p.lambda();
3255}
3256
3257template <class _CharT, class _Traits, class _RealType>
3258basic_ostream<_CharT, _Traits>&
3259operator<<(basic_ostream<_CharT, _Traits>& __os,
3260 const exponential_distribution<_RealType>& __x)
3261{
3262 __save_flags<_CharT, _Traits> _(__os);
3263 __os.flags(ios_base::dec | ios_base::left);
3264 return __os << __x.lambda();
3265}
3266
3267template <class _CharT, class _Traits, class _RealType>
3268basic_istream<_CharT, _Traits>&
3269operator>>(basic_istream<_CharT, _Traits>& __is,
3270 exponential_distribution<_RealType>& __x)
3271{
3272 typedef exponential_distribution<_RealType> _Eng;
3273 typedef typename _Eng::result_type result_type;
3274 typedef typename _Eng::param_type param_type;
3275 __save_flags<_CharT, _Traits> _(__is);
3276 __is.flags(ios_base::dec | ios_base::skipws);
3277 result_type __lambda;
3278 __is >> __lambda;
3279 if (!__is.fail())
3280 __x.param(param_type(__lambda));
3281 return __is;
3282}
3283
Howard Hinnantc7c49132010-05-13 17:58:28 +00003284// gamma_distribution
3285
3286template<class _RealType = double>
3287class gamma_distribution
3288{
3289public:
3290 // types
3291 typedef _RealType result_type;
3292
3293 class param_type
3294 {
3295 result_type __alpha_;
3296 result_type __beta_;
3297 public:
3298 typedef gamma_distribution distribution_type;
3299
3300 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
3301 : __alpha_(__alpha), __beta_(__beta) {}
3302
3303 result_type alpha() const {return __alpha_;}
3304 result_type beta() const {return __beta_;}
3305
3306 friend bool operator==(const param_type& __x, const param_type& __y)
3307 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
3308 friend bool operator!=(const param_type& __x, const param_type& __y)
3309 {return !(__x == __y);}
3310 };
3311
3312private:
3313 param_type __p_;
3314
3315public:
3316 // constructors and reset functions
3317 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
3318 : __p_(param_type(__alpha, __beta)) {}
3319 explicit gamma_distribution(const param_type& __p)
3320 : __p_(__p) {}
3321 void reset() {}
3322
3323 // generating functions
3324 template<class _URNG> result_type operator()(_URNG& __g)
3325 {return (*this)(__g, __p_);}
3326 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3327
3328 // property functions
3329 result_type alpha() const {return __p_.alpha();}
3330 result_type beta() const {return __p_.beta();}
3331
3332 param_type param() const {return __p_;}
3333 void param(const param_type& __p) {__p_ = __p;}
3334
3335 result_type min() const {return 0;}
3336 result_type max() const {return numeric_limits<result_type>::infinity();}
3337
3338 friend bool operator==(const gamma_distribution& __x,
3339 const gamma_distribution& __y)
3340 {return __x.__p_ == __y.__p_;}
3341 friend bool operator!=(const gamma_distribution& __x,
3342 const gamma_distribution& __y)
3343 {return !(__x == __y);}
3344};
3345
3346template <class _RealType>
3347template<class _URNG>
3348_RealType
3349gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3350{
Howard Hinnantf417abe2010-05-14 18:43:10 +00003351 result_type __a = __p.alpha();
3352 uniform_real_distribution<result_type> __gen(0, 1);
3353 exponential_distribution<result_type> __egen;
3354 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00003355 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00003356 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00003357 else if (__a > 1)
3358 {
3359 const result_type __b = __a - 1;
3360 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00003361 while (true)
3362 {
3363 const result_type __u = __gen(__g);
3364 const result_type __v = __gen(__g);
3365 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00003366 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00003367 {
3368 const result_type __y = _STD::sqrt(__c / __w) *
3369 (__u - result_type(0.5));
3370 __x = __b + __y;
3371 if (__x >= 0)
3372 {
3373 const result_type __z = 64 * __w * __w * __w * __v * __v;
3374 if (__z <= 1 - 2 * __y * __y / __x)
3375 break;
3376 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
3377 break;
3378 }
3379 }
3380 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00003381 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00003382 else // __a < 1
3383 {
3384 while (true)
3385 {
3386 const result_type __u = __gen(__g);
3387 const result_type __es = __egen(__g);
3388 if (__u <= 1 - __a)
3389 {
3390 __x = _STD::pow(__u, 1 / __a);
3391 if (__x <= __es)
3392 break;
3393 }
3394 else
3395 {
3396 const result_type __e = -_STD::log((1-__u)/__a);
3397 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
3398 if (__x <= __e + __es)
3399 break;
3400 }
3401 }
3402 }
3403 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00003404}
3405
3406template <class _CharT, class _Traits, class _RT>
3407basic_ostream<_CharT, _Traits>&
3408operator<<(basic_ostream<_CharT, _Traits>& __os,
3409 const gamma_distribution<_RT>& __x)
3410{
3411 __save_flags<_CharT, _Traits> _(__os);
3412 __os.flags(ios_base::dec | ios_base::left);
3413 _CharT __sp = __os.widen(' ');
3414 __os.fill(__sp);
3415 __os << __x.alpha() << __sp << __x.beta();
3416 return __os;
3417}
3418
3419template <class _CharT, class _Traits, class _RT>
3420basic_istream<_CharT, _Traits>&
3421operator>>(basic_istream<_CharT, _Traits>& __is,
3422 gamma_distribution<_RT>& __x)
3423{
3424 typedef gamma_distribution<_RT> _Eng;
3425 typedef typename _Eng::result_type result_type;
3426 typedef typename _Eng::param_type param_type;
3427 __save_flags<_CharT, _Traits> _(__is);
3428 __is.flags(ios_base::dec | ios_base::skipws);
3429 result_type __alpha;
3430 result_type __beta;
3431 __is >> __alpha >> __beta;
3432 if (!__is.fail())
3433 __x.param(param_type(__alpha, __beta));
3434 return __is;
3435}
Howard Hinnanta64111c2010-05-12 21:02:31 +00003436// normal_distribution
3437
3438template<class _RealType = double>
3439class normal_distribution
3440{
3441public:
3442 // types
3443 typedef _RealType result_type;
3444
3445 class param_type
3446 {
3447 result_type __mean_;
3448 result_type __stddev_;
3449 public:
3450 typedef normal_distribution distribution_type;
3451
3452 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
3453 : __mean_(__mean), __stddev_(__stddev) {}
3454
3455 result_type mean() const {return __mean_;}
3456 result_type stddev() const {return __stddev_;}
3457
3458 friend bool operator==(const param_type& __x, const param_type& __y)
3459 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
3460 friend bool operator!=(const param_type& __x, const param_type& __y)
3461 {return !(__x == __y);}
3462 };
3463
3464private:
3465 param_type __p_;
3466 result_type _V_;
3467 bool _V_hot_;
3468
3469public:
3470 // constructors and reset functions
3471 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
3472 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
3473 explicit normal_distribution(const param_type& __p)
3474 : __p_(__p), _V_hot_(false) {}
3475 void reset() {_V_hot_ = false;}
3476
3477 // generating functions
3478 template<class _URNG> result_type operator()(_URNG& __g)
3479 {return (*this)(__g, __p_);}
3480 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3481
3482 // property functions
3483 result_type mean() const {return __p_.mean();}
3484 result_type stddev() const {return __p_.stddev();}
3485
3486 param_type param() const {return __p_;}
3487 void param(const param_type& __p) {__p_ = __p;}
3488
3489 result_type min() const {return -numeric_limits<result_type>::infinity();}
3490 result_type max() const {return numeric_limits<result_type>::infinity();}
3491
3492 friend bool operator==(const normal_distribution& __x,
3493 const normal_distribution& __y)
3494 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
3495 (!__x._V_hot_ || __x._V_ == __y._V_);}
3496 friend bool operator!=(const normal_distribution& __x,
3497 const normal_distribution& __y)
Howard Hinnantc7c49132010-05-13 17:58:28 +00003498 {return !(__x == __y);}
Howard Hinnanta64111c2010-05-12 21:02:31 +00003499
3500 template <class _CharT, class _Traits, class _RT>
3501 friend
3502 basic_ostream<_CharT, _Traits>&
3503 operator<<(basic_ostream<_CharT, _Traits>& __os,
3504 const normal_distribution<_RT>& __x);
3505
3506 template <class _CharT, class _Traits, class _RT>
3507 friend
3508 basic_istream<_CharT, _Traits>&
3509 operator>>(basic_istream<_CharT, _Traits>& __is,
3510 normal_distribution<_RT>& __x);
3511};
3512
3513template <class _RealType>
3514template<class _URNG>
3515_RealType
3516normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3517{
3518 result_type _U;
3519 if (_V_hot_)
3520 {
3521 _V_hot_ = false;
3522 _U = _V_;
3523 }
3524 else
3525 {
3526 uniform_real_distribution<result_type> _Uni(-1, 1);
3527 result_type __u;
3528 result_type __v;
3529 result_type __s;
3530 do
3531 {
3532 __u = _Uni(__g);
3533 __v = _Uni(__g);
3534 __s = __u * __u + __v * __v;
3535 } while (__s > 1 || __s == 0);
3536 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
3537 _V_ = __v * _F;
3538 _V_hot_ = true;
3539 _U = __u * _F;
3540 }
3541 return _U * __p.stddev() + __p.mean();
3542}
3543
3544template <class _CharT, class _Traits, class _RT>
3545basic_ostream<_CharT, _Traits>&
3546operator<<(basic_ostream<_CharT, _Traits>& __os,
3547 const normal_distribution<_RT>& __x)
3548{
3549 __save_flags<_CharT, _Traits> _(__os);
3550 __os.flags(ios_base::dec | ios_base::left);
3551 _CharT __sp = __os.widen(' ');
3552 __os.fill(__sp);
3553 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
3554 if (__x._V_hot_)
3555 __os << __sp << __x._V_;
3556 return __os;
3557}
3558
3559template <class _CharT, class _Traits, class _RT>
3560basic_istream<_CharT, _Traits>&
3561operator>>(basic_istream<_CharT, _Traits>& __is,
3562 normal_distribution<_RT>& __x)
3563{
3564 typedef normal_distribution<_RT> _Eng;
3565 typedef typename _Eng::result_type result_type;
3566 typedef typename _Eng::param_type param_type;
3567 __save_flags<_CharT, _Traits> _(__is);
3568 __is.flags(ios_base::dec | ios_base::skipws);
3569 result_type __mean;
3570 result_type __stddev;
3571 result_type _V = 0;
3572 bool _V_hot = false;
3573 __is >> __mean >> __stddev >> _V_hot;
3574 if (_V_hot)
3575 __is >> _V;
3576 if (!__is.fail())
3577 {
3578 __x.param(param_type(__mean, __stddev));
3579 __x._V_hot_ = _V_hot;
3580 __x._V_ = _V;
3581 }
3582 return __is;
3583}
3584
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585_LIBCPP_END_NAMESPACE_STD
3586
3587#endif // _LIBCPP_RANDOM