blob: d5dafb1a814e3185465b70a68853aa027c190efc [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>
Howard Hinnant34e8a572010-05-17 13:44:27 +0000669class geometric_distribution
670{
671public:
672 // types
673 typedef IntType result_type;
674
675 class param_type
676 {
677 public:
678 typedef geometric_distribution distribution_type;
679
680 explicit param_type(double p = 0.5);
681
682 double p() const;
683
684 friend bool operator==(const param_type& x, const param_type& y);
685 friend bool operator!=(const param_type& x, const param_type& y);
686 };
687
688 // constructors and reset functions
689 explicit geometric_distribution(double p = 0.5);
690 explicit geometric_distribution(const param_type& parm);
691 void reset();
692
693 // generating functions
694 template<class URNG> result_type operator()(URNG& g);
695 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
696
697 // property functions
698 double p() const;
699
700 param_type param() const;
701 void param(const param_type& parm);
702
703 result_type min() const;
704 result_type max() const;
705
706 friend bool operator==(const geometric_distribution& x,
707 const geometric_distribution& y);
708 friend bool operator!=(const geometric_distribution& x,
709 const geometric_distribution& y);
710
711 template <class charT, class traits>
712 friend
713 basic_ostream<charT, traits>&
714 operator<<(basic_ostream<charT, traits>& os,
715 const geometric_distribution& x);
716
717 template <class charT, class traits>
718 friend
719 basic_istream<charT, traits>&
720 operator>>(basic_istream<charT, traits>& is,
721 geometric_distribution& x);
722};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
724template<class IntType = int>
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000725class negative_binomial_distribution
726{
727public:
728 // types
729 typedef IntType result_type;
730
731 class param_type
732 {
733 public:
734 typedef negative_binomial_distribution distribution_type;
735
736 explicit param_type(result_type k = 1, double p = 0.5);
737
738 result_type k() const;
739 double p() const;
740
741 friend bool operator==(const param_type& x, const param_type& y);
742 friend bool operator!=(const param_type& x, const param_type& y);
743 };
744
745 // constructor and reset functions
746 explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
747 explicit negative_binomial_distribution(const param_type& parm);
748 void reset();
749
750 // generating functions
751 template<class URNG> result_type operator()(URNG& g);
752 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
753
754 // property functions
755 result_type k() const;
756 double p() const;
757
758 param_type param() const;
759 void param(const param_type& parm);
760
761 result_type min() const;
762 result_type max() const;
763
764 friend bool operator==(const negative_binomial_distribution& x,
765 const negative_binomial_distribution& y);
766 friend bool operator!=(const negative_binomial_distribution& x,
767 const negative_binomial_distribution& y);
768
769 template <class charT, class traits>
770 friend
771 basic_ostream<charT, traits>&
772 operator<<(basic_ostream<charT, traits>& os,
773 const negative_binomial_distribution& x);
774
775 template <class charT, class traits>
776 friend
777 basic_istream<charT, traits>&
778 operator>>(basic_istream<charT, traits>& is,
779 negative_binomial_distribution& x);
780};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000781
782template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000783class poisson_distribution
784{
785public:
786 // types
787 typedef IntType result_type;
788
789 class param_type
790 {
791 public:
792 typedef poisson_distribution distribution_type;
793
794 explicit param_type(double mean = 1.0);
795
796 double mean() const;
797
798 friend bool operator==(const param_type& x, const param_type& y);
799 friend bool operator!=(const param_type& x, const param_type& y);
800 };
801
802 // constructors and reset functions
803 explicit poisson_distribution(double mean = 1.0);
804 explicit poisson_distribution(const param_type& parm);
805 void reset();
806
807 // generating functions
808 template<class URNG> result_type operator()(URNG& g);
809 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
810
811 // property functions
812 double mean() const;
813
814 param_type param() const;
815 void param(const param_type& parm);
816
817 result_type min() const;
818 result_type max() const;
819
820 friend bool operator==(const poisson_distribution& x,
821 const poisson_distribution& y);
822 friend bool operator!=(const poisson_distribution& x,
823 const poisson_distribution& y);
824
825 template <class charT, class traits>
826 friend
827 basic_ostream<charT, traits>&
828 operator<<(basic_ostream<charT, traits>& os,
829 const poisson_distribution& x);
830
831 template <class charT, class traits>
832 friend
833 basic_istream<charT, traits>&
834 operator>>(basic_istream<charT, traits>& is,
835 poisson_distribution& x);
836};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
838template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000839class exponential_distribution
840{
841public:
842 // types
843 typedef RealType result_type;
844
845 class param_type
846 {
847 public:
848 typedef exponential_distribution distribution_type;
849
Howard Hinnanta64111c2010-05-12 21:02:31 +0000850 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000851
Howard Hinnanta64111c2010-05-12 21:02:31 +0000852 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000853
854 friend bool operator==(const param_type& x, const param_type& y);
855 friend bool operator!=(const param_type& x, const param_type& y);
856 };
857
858 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000859 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000860 explicit exponential_distribution(const param_type& parm);
861 void reset();
862
863 // generating functions
864 template<class URNG> result_type operator()(URNG& g);
865 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
866
867 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000868 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000869
870 param_type param() const;
871 void param(const param_type& parm);
872
873 result_type min() const;
874 result_type max() const;
875
876 friend bool operator==(const exponential_distribution& x,
877 const exponential_distribution& y);
878 friend bool operator!=(const exponential_distribution& x,
879 const exponential_distribution& y);
880
881 template <class charT, class traits>
882 friend
883 basic_ostream<charT, traits>&
884 operator<<(basic_ostream<charT, traits>& os,
885 const exponential_distribution& x);
886
887 template <class charT, class traits>
888 friend
889 basic_istream<charT, traits>&
890 operator>>(basic_istream<charT, traits>& is,
891 exponential_distribution& x);
892};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893
894template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000895class gamma_distribution
896{
897public:
898 // types
899 typedef RealType result_type;
900
901 class param_type
902 {
903 public:
904 typedef gamma_distribution distribution_type;
905
906 explicit param_type(result_type alpha = 1, result_type beta = 1);
907
908 result_type alpha() const;
909 result_type beta() const;
910
911 friend bool operator==(const param_type& x, const param_type& y);
912 friend bool operator!=(const param_type& x, const param_type& y);
913 };
914
915 // constructors and reset functions
916 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
917 explicit gamma_distribution(const param_type& parm);
918 void reset();
919
920 // generating functions
921 template<class URNG> result_type operator()(URNG& g);
922 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
923
924 // property functions
925 result_type alpha() const;
926 result_type beta() const;
927
928 param_type param() const;
929 void param(const param_type& parm);
930
931 result_type min() const;
932 result_type max() const;
933
934 friend bool operator==(const gamma_distribution& x,
935 const gamma_distribution& y);
936 friend bool operator!=(const gamma_distribution& x,
937 const gamma_distribution& y);
938
939 template <class charT, class traits>
940 friend
941 basic_ostream<charT, traits>&
942 operator<<(basic_ostream<charT, traits>& os,
943 const gamma_distribution& x);
944
945 template <class charT, class traits>
946 friend
947 basic_istream<charT, traits>&
948 operator>>(basic_istream<charT, traits>& is,
949 gamma_distribution& x);
950};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951
952template<class RealType = double>
Howard Hinnant9de6e302010-05-16 01:09:02 +0000953class weibull_distribution
954{
955public:
956 // types
957 typedef RealType result_type;
958
959 class param_type
960 {
961 public:
962 typedef weibull_distribution distribution_type;
963
964 explicit param_type(result_type alpha = 1, result_type beta = 1);
965
966 result_type a() const;
967 result_type b() const;
968
969 friend bool operator==(const param_type& x, const param_type& y);
970 friend bool operator!=(const param_type& x, const param_type& y);
971 };
972
973 // constructor and reset functions
974 explicit weibull_distribution(result_type a = 1, result_type b = 1);
975 explicit weibull_distribution(const param_type& parm);
976 void reset();
977
978 // generating functions
979 template<class URNG> result_type operator()(URNG& g);
980 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
981
982 // property functions
983 result_type a() const;
984 result_type b() const;
985
986 param_type param() const;
987 void param(const param_type& parm);
988
989 result_type min() const;
990 result_type max() const;
991
992
993 friend bool operator==(const weibull_distribution& x,
994 const weibull_distribution& y);
995 friend bool operator!=(const weibull_distribution& x,
996 const weibull_distribution& y);
997
998 template <class charT, class traits>
999 friend
1000 basic_ostream<charT, traits>&
1001 operator<<(basic_ostream<charT, traits>& os,
1002 const weibull_distribution& x);
1003
1004 template <class charT, class traits>
1005 friend
1006 basic_istream<charT, traits>&
1007 operator>>(basic_istream<charT, traits>& is,
1008 weibull_distribution& x);
1009};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010
1011template<class RealType = double>
1012 class extreme_value_distribution;
1013
1014template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +00001015class normal_distribution
1016{
1017public:
1018 // types
1019 typedef RealType result_type;
1020
1021 class param_type
1022 {
1023 public:
1024 typedef normal_distribution distribution_type;
1025
1026 explicit param_type(result_type mean = 0, result_type stddev = 1);
1027
1028 result_type mean() const;
1029 result_type stddev() const;
1030
1031 friend bool operator==(const param_type& x, const param_type& y);
1032 friend bool operator!=(const param_type& x, const param_type& y);
1033 };
1034
1035 // constructors and reset functions
1036 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
1037 explicit normal_distribution(const param_type& parm);
1038 void reset();
1039
1040 // generating functions
1041 template<class URNG> result_type operator()(URNG& g);
1042 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1043
1044 // property functions
1045 result_type mean() const;
1046 result_type stddev() const;
1047
1048 param_type param() const;
1049 void param(const param_type& parm);
1050
1051 result_type min() const;
1052 result_type max() const;
1053
1054 friend bool operator==(const normal_distribution& x,
1055 const normal_distribution& y);
1056 friend bool operator!=(const normal_distribution& x,
1057 const normal_distribution& y);
1058
1059 template <class charT, class traits>
1060 friend
1061 basic_ostream<charT, traits>&
1062 operator<<(basic_ostream<charT, traits>& os,
1063 const normal_distribution& x);
1064
1065 template <class charT, class traits>
1066 friend
1067 basic_istream<charT, traits>&
1068 operator>>(basic_istream<charT, traits>& is,
1069 normal_distribution& x);
1070};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071
1072template<class RealType = double>
1073 class lognormal_distribution;
1074
1075template<class RealType = double>
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001076class chi_squared_distribution
1077{
1078public:
1079 // types
1080 typedef RealType result_type;
1081
1082 class param_type
1083 {
1084 public:
1085 typedef chi_squared_distribution distribution_type;
1086
1087 explicit param_type(result_type n = 1);
1088
1089 result_type n() const;
1090
1091 friend bool operator==(const param_type& x, const param_type& y);
1092 friend bool operator!=(const param_type& x, const param_type& y);
1093 };
1094
1095 // constructor and reset functions
1096 explicit chi_squared_distribution(result_type n = 1);
1097 explicit chi_squared_distribution(const param_type& parm);
1098 void reset();
1099
1100 // generating functions
1101 template<class URNG> result_type operator()(URNG& g);
1102 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1103
1104 // property functions
1105 result_type n() const;
1106
1107 param_type param() const;
1108 void param(const param_type& parm);
1109
1110 result_type min() const;
1111 result_type max() const;
1112
1113
1114 friend bool operator==(const chi_squared_distribution& x,
1115 const chi_squared_distribution& y);
1116 friend bool operator!=(const chi_squared_distribution& x,
1117 const chi_squared_distribution& y);
1118
1119 template <class charT, class traits>
1120 friend
1121 basic_ostream<charT, traits>&
1122 operator<<(basic_ostream<charT, traits>& os,
1123 const chi_squared_distribution& x);
1124
1125 template <class charT, class traits>
1126 friend
1127 basic_istream<charT, traits>&
1128 operator>>(basic_istream<charT, traits>& is,
1129 chi_squared_distribution& x);
1130};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131
1132template<class RealType = double>
1133 class cauchy_distribution;
1134
1135template<class RealType = double>
1136 class fisher_f_distribution;
1137
1138template<class RealType = double>
1139 class student_t_distribution;
1140
1141template<class IntType = int>
1142 class discrete_distribution;
1143
1144template<class RealType = double>
1145 class piecewise_constant_distribution;
1146
1147template<class RealType = double>
1148 class piecewise_linear_distribution;
1149
1150} // std
1151*/
1152
1153#include <__config>
1154#include <cstddef>
1155#include <type_traits>
1156#include <initializer_list>
1157#include <cstdint>
1158#include <limits>
1159#include <algorithm>
1160#include <vector>
1161#include <string>
1162#include <istream>
1163#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +00001164#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165
1166#pragma GCC system_header
1167
1168_LIBCPP_BEGIN_NAMESPACE_STD
1169
1170// linear_congruential_engine
1171
1172template <unsigned long long __a, unsigned long long __c,
1173 unsigned long long __m, unsigned long long _M,
1174 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
1175struct __lce_ta;
1176
1177// 64
1178
1179template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1180struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1181{
1182 typedef unsigned long long result_type;
1183 static result_type next(result_type __x)
1184 {
1185 // Schrage's algorithm
1186 const result_type __q = __m / __a;
1187 const result_type __r = __m % __a;
1188 const result_type __t0 = __a * (__x % __q);
1189 const result_type __t1 = __r * (__x / __q);
1190 __x = __t0 + (__t0 < __t1) * __m - __t1;
1191 __x += __c - (__x >= __m - __c) * __m;
1192 return __x;
1193 }
1194};
1195
1196template <unsigned long long __a, unsigned long long __m>
1197struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1198{
1199 typedef unsigned long long result_type;
1200 static result_type next(result_type __x)
1201 {
1202 // Schrage's algorithm
1203 const result_type __q = __m / __a;
1204 const result_type __r = __m % __a;
1205 const result_type __t0 = __a * (__x % __q);
1206 const result_type __t1 = __r * (__x / __q);
1207 __x = __t0 + (__t0 < __t1) * __m - __t1;
1208 return __x;
1209 }
1210};
1211
1212template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1213struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1214{
1215 typedef unsigned long long result_type;
1216 static result_type next(result_type __x)
1217 {
1218 return (__a * __x + __c) % __m;
1219 }
1220};
1221
1222template <unsigned long long __a, unsigned long long __c>
1223struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1224{
1225 typedef unsigned long long result_type;
1226 static result_type next(result_type __x)
1227 {
1228 return __a * __x + __c;
1229 }
1230};
1231
1232// 32
1233
1234template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1235struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1236{
1237 typedef unsigned result_type;
1238 static result_type next(result_type __x)
1239 {
1240 const result_type __a = static_cast<result_type>(_A);
1241 const result_type __c = static_cast<result_type>(_C);
1242 const result_type __m = static_cast<result_type>(_M);
1243 // Schrage's algorithm
1244 const result_type __q = __m / __a;
1245 const result_type __r = __m % __a;
1246 const result_type __t0 = __a * (__x % __q);
1247 const result_type __t1 = __r * (__x / __q);
1248 __x = __t0 + (__t0 < __t1) * __m - __t1;
1249 __x += __c - (__x >= __m - __c) * __m;
1250 return __x;
1251 }
1252};
1253
1254template <unsigned long long _A, unsigned long long _M>
1255struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1256{
1257 typedef unsigned result_type;
1258 static result_type next(result_type __x)
1259 {
1260 const result_type __a = static_cast<result_type>(_A);
1261 const result_type __m = static_cast<result_type>(_M);
1262 // Schrage's algorithm
1263 const result_type __q = __m / __a;
1264 const result_type __r = __m % __a;
1265 const result_type __t0 = __a * (__x % __q);
1266 const result_type __t1 = __r * (__x / __q);
1267 __x = __t0 + (__t0 < __t1) * __m - __t1;
1268 return __x;
1269 }
1270};
1271
1272template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1273struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1274{
1275 typedef unsigned result_type;
1276 static result_type next(result_type __x)
1277 {
1278 const result_type __a = static_cast<result_type>(_A);
1279 const result_type __c = static_cast<result_type>(_C);
1280 const result_type __m = static_cast<result_type>(_M);
1281 return (__a * __x + __c) % __m;
1282 }
1283};
1284
1285template <unsigned long long _A, unsigned long long _C>
1286struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1287{
1288 typedef unsigned result_type;
1289 static result_type next(result_type __x)
1290 {
1291 const result_type __a = static_cast<result_type>(_A);
1292 const result_type __c = static_cast<result_type>(_C);
1293 return __a * __x + __c;
1294 }
1295};
1296
1297// 16
1298
1299template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1300struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1301{
1302 typedef unsigned short result_type;
1303 static result_type next(result_type __x)
1304 {
1305 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1306 }
1307};
1308
1309template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1310class linear_congruential_engine;
1311
1312template <class _CharT, class _Traits,
1313 class _U, _U _A, _U _C, _U _N>
1314basic_ostream<_CharT, _Traits>&
1315operator<<(basic_ostream<_CharT, _Traits>& __os,
1316 const linear_congruential_engine<_U, _A, _C, _N>&);
1317
1318template <class _CharT, class _Traits,
1319 class _U, _U _A, _U _C, _U _N>
1320basic_istream<_CharT, _Traits>&
1321operator>>(basic_istream<_CharT, _Traits>& __is,
1322 linear_congruential_engine<_U, _A, _C, _N>& __x);
1323
1324template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1325class linear_congruential_engine
1326{
1327public:
1328 // types
1329 typedef _UIntType result_type;
1330
1331private:
1332 result_type __x_;
1333
1334 static const result_type _M = result_type(~0);
1335
1336 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1337 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1338public:
1339 static const result_type _Min = __c == 0u ? 1u: 0u;
1340 static const result_type _Max = __m - 1u;
1341 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1342
1343 // engine characteristics
1344 static const/*expr*/ result_type multiplier = __a;
1345 static const/*expr*/ result_type increment = __c;
1346 static const/*expr*/ result_type modulus = __m;
1347 static const/*expr*/ result_type min() {return _Min;}
1348 static const/*expr*/ result_type max() {return _Max;}
1349 static const/*expr*/ result_type default_seed = 1u;
1350
1351 // constructors and seeding functions
1352 explicit linear_congruential_engine(result_type __s = default_seed)
1353 {seed(__s);}
1354 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
1355 {seed(__q);}
1356 void seed(result_type __s = default_seed)
1357 {seed(integral_constant<bool, __m == 0>(),
1358 integral_constant<bool, __c == 0>(), __s);}
1359 template<class _Sseq>
1360 typename enable_if
1361 <
1362 !is_convertible<_Sseq, result_type>::value,
1363 void
1364 >::type
1365 seed(_Sseq& __q)
1366 {__seed(__q, integral_constant<unsigned,
1367 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1368 : (__m-1) / 0x100000000ull)>());}
1369
1370 // generating functions
1371 result_type operator()()
1372 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1373 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1374
1375 friend bool operator==(const linear_congruential_engine& __x,
1376 const linear_congruential_engine& __y)
1377 {return __x.__x_ == __y.__x_;}
1378 friend bool operator!=(const linear_congruential_engine& __x,
1379 const linear_congruential_engine& __y)
1380 {return !(__x == __y);}
1381
1382private:
1383
1384 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1385 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1386 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1387 1 : __s % __m;}
1388 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1389
1390 template<class _Sseq>
1391 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1392 template<class _Sseq>
1393 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1394
1395 template <class _CharT, class _Traits,
1396 class _U, _U _A, _U _C, _U _N>
1397 friend
1398 basic_ostream<_CharT, _Traits>&
1399 operator<<(basic_ostream<_CharT, _Traits>& __os,
1400 const linear_congruential_engine<_U, _A, _C, _N>&);
1401
1402 template <class _CharT, class _Traits,
1403 class _U, _U _A, _U _C, _U _N>
1404 friend
1405 basic_istream<_CharT, _Traits>&
1406 operator>>(basic_istream<_CharT, _Traits>& __is,
1407 linear_congruential_engine<_U, _A, _C, _N>& __x);
1408};
1409
1410template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1411template<class _Sseq>
1412void
1413linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1414 integral_constant<unsigned, 1>)
1415{
1416 const unsigned __k = 1;
1417 uint32_t __ar[__k+3];
1418 __q.generate(__ar, __ar + __k + 3);
1419 result_type __s = static_cast<result_type>(__ar[3] % __m);
1420 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1421}
1422
1423template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1424template<class _Sseq>
1425void
1426linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1427 integral_constant<unsigned, 2>)
1428{
1429 const unsigned __k = 2;
1430 uint32_t __ar[__k+3];
1431 __q.generate(__ar, __ar + __k + 3);
1432 result_type __s = static_cast<result_type>((__ar[3] +
1433 (uint64_t)__ar[4] << 32) % __m);
1434 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1435}
1436
1437template <class _CharT, class _Traits>
1438class __save_flags
1439{
1440 typedef basic_ios<_CharT, _Traits> __stream_type;
1441 typedef typename __stream_type::fmtflags fmtflags;
1442
1443 __stream_type& __stream_;
1444 fmtflags __fmtflags_;
1445 _CharT __fill_;
1446
1447 __save_flags(const __save_flags&);
1448 __save_flags& operator=(const __save_flags&);
1449public:
1450 explicit __save_flags(__stream_type& __stream)
1451 : __stream_(__stream),
1452 __fmtflags_(__stream.flags()),
1453 __fill_(__stream.fill())
1454 {}
1455 ~__save_flags()
1456 {
1457 __stream_.flags(__fmtflags_);
1458 __stream_.fill(__fill_);
1459 }
1460};
1461
1462template <class _CharT, class _Traits,
1463 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1464inline
1465basic_ostream<_CharT, _Traits>&
1466operator<<(basic_ostream<_CharT, _Traits>& __os,
1467 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1468{
1469 __save_flags<_CharT, _Traits> _(__os);
1470 __os.flags(ios_base::dec | ios_base::left);
1471 __os.fill(__os.widen(' '));
1472 return __os << __x.__x_;
1473}
1474
1475template <class _CharT, class _Traits,
1476 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1477basic_istream<_CharT, _Traits>&
1478operator>>(basic_istream<_CharT, _Traits>& __is,
1479 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1480{
1481 __save_flags<_CharT, _Traits> _(__is);
1482 __is.flags(ios_base::dec | ios_base::skipws);
1483 _UIntType __t;
1484 __is >> __t;
1485 if (!__is.fail())
1486 __x.__x_ = __t;
1487 return __is;
1488}
1489
1490typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1491 minstd_rand0;
1492typedef minstd_rand0 default_random_engine;
1493typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1494 minstd_rand;
1495// mersenne_twister_engine
1496
1497template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1498 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1499 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1500class mersenne_twister_engine;
1501
1502template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1503 _UI _A, size_t _U, _UI _D, size_t _S,
1504 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1505bool
1506operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1507 _B, _T, _C, _L, _F>& __x,
1508 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1509 _B, _T, _C, _L, _F>& __y);
1510
1511template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1512 _UI _A, size_t _U, _UI _D, size_t _S,
1513 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1514bool
1515operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1516 _B, _T, _C, _L, _F>& __x,
1517 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1518 _B, _T, _C, _L, _F>& __y);
1519
1520template <class _CharT, class _Traits,
1521 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1522 _UI _A, size_t _U, _UI _D, size_t _S,
1523 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1524basic_ostream<_CharT, _Traits>&
1525operator<<(basic_ostream<_CharT, _Traits>& __os,
1526 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1527 _B, _T, _C, _L, _F>& __x);
1528
1529template <class _CharT, class _Traits,
1530 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1531 _UI _A, size_t _U, _UI _D, size_t _S,
1532 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1533basic_istream<_CharT, _Traits>&
1534operator>>(basic_istream<_CharT, _Traits>& __is,
1535 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1536 _B, _T, _C, _L, _F>& __x);
1537
1538template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1539 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1540 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1541class mersenne_twister_engine
1542{
1543public:
1544 // types
1545 typedef _UIntType result_type;
1546
1547private:
1548 result_type __x_[__n];
1549 size_t __i_;
1550
1551 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1552 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1553 static const result_type _Dt = numeric_limits<result_type>::digits;
1554 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1555 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1556 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1557 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1558 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1559 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1560 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1561public:
1562 static const result_type _Min = 0;
1563 static const result_type _Max = __w == _Dt ? result_type(~0) :
1564 (result_type(1) << __w) - result_type(1);
1565 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1566 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1567 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1568 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1569 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1570 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1571
1572 // engine characteristics
1573 static const/*expr*/ size_t word_size = __w;
1574 static const/*expr*/ size_t state_size = __n;
1575 static const/*expr*/ size_t shift_size = __m;
1576 static const/*expr*/ size_t mask_bits = __r;
1577 static const/*expr*/ result_type xor_mask = __a;
1578 static const/*expr*/ size_t tempering_u = __u;
1579 static const/*expr*/ result_type tempering_d = __d;
1580 static const/*expr*/ size_t tempering_s = __s;
1581 static const/*expr*/ result_type tempering_b = __b;
1582 static const/*expr*/ size_t tempering_t = __t;
1583 static const/*expr*/ result_type tempering_c = __c;
1584 static const/*expr*/ size_t tempering_l = __l;
1585 static const/*expr*/ result_type initialization_multiplier = __f;
1586 static const/*expr*/ result_type min() { return _Min; }
1587 static const/*expr*/ result_type max() { return _Max; }
1588 static const/*expr*/ result_type default_seed = 5489u;
1589
1590 // constructors and seeding functions
1591 explicit mersenne_twister_engine(result_type __sd = default_seed)
1592 {seed(__sd);}
1593 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
1594 {seed(__q);}
1595 void seed(result_type __sd = default_seed);
1596 template<class _Sseq>
1597 typename enable_if
1598 <
1599 !is_convertible<_Sseq, result_type>::value,
1600 void
1601 >::type
1602 seed(_Sseq& __q)
1603 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1604
1605 // generating functions
1606 result_type operator()();
1607 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1608
1609 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1610 _UI _A, size_t _U, _UI _D, size_t _S,
1611 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1612 friend
1613 bool
1614 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1615 _B, _T, _C, _L, _F>& __x,
1616 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1617 _B, _T, _C, _L, _F>& __y);
1618
1619 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1620 _UI _A, size_t _U, _UI _D, size_t _S,
1621 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1622 friend
1623 bool
1624 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1625 _B, _T, _C, _L, _F>& __x,
1626 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1627 _B, _T, _C, _L, _F>& __y);
1628
1629 template <class _CharT, class _Traits,
1630 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1631 _UI _A, size_t _U, _UI _D, size_t _S,
1632 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1633 friend
1634 basic_ostream<_CharT, _Traits>&
1635 operator<<(basic_ostream<_CharT, _Traits>& __os,
1636 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1637 _B, _T, _C, _L, _F>& __x);
1638
1639 template <class _CharT, class _Traits,
1640 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1641 _UI _A, size_t _U, _UI _D, size_t _S,
1642 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1643 friend
1644 basic_istream<_CharT, _Traits>&
1645 operator>>(basic_istream<_CharT, _Traits>& __is,
1646 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1647 _B, _T, _C, _L, _F>& __x);
1648private:
1649
1650 template<class _Sseq>
1651 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1652 template<class _Sseq>
1653 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1654
1655 template <size_t __count>
1656 static
1657 typename enable_if
1658 <
1659 __count < __w,
1660 result_type
1661 >::type
1662 __lshift(result_type __x) {return (__x << __count) & _Max;}
1663
1664 template <size_t __count>
1665 static
1666 typename enable_if
1667 <
1668 (__count >= __w),
1669 result_type
1670 >::type
1671 __lshift(result_type __x) {return result_type(0);}
1672
1673 template <size_t __count>
1674 static
1675 typename enable_if
1676 <
1677 __count < _Dt,
1678 result_type
1679 >::type
1680 __rshift(result_type __x) {return __x >> __count;}
1681
1682 template <size_t __count>
1683 static
1684 typename enable_if
1685 <
1686 (__count >= _Dt),
1687 result_type
1688 >::type
1689 __rshift(result_type __x) {return result_type(0);}
1690};
1691
1692template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1693 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1694 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1695void
1696mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1697 __t, __c, __l, __f>::seed(result_type __sd)
1698{ // __w >= 2
1699 __x_[0] = __sd & _Max;
1700 for (size_t __i = 1; __i < __n; ++__i)
1701 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
1702 __i_ = 0;
1703}
1704
1705template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1706 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1707 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1708template<class _Sseq>
1709void
1710mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1711 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
1712{
1713 const unsigned __k = 1;
1714 uint32_t __ar[__n * __k];
1715 __q.generate(__ar, __ar + __n * __k);
1716 for (size_t __i = 0; __i < __n; ++__i)
1717 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1718 const result_type __mask = __r == _Dt ? result_type(~0) :
1719 (result_type(1) << __r) - result_type(1);
1720 __i_ = 0;
1721 if ((__x_[0] & ~__mask) == 0)
1722 {
1723 for (size_t __i = 1; __i < __n; ++__i)
1724 if (__x_[__i] != 0)
1725 return;
1726 __x_[0] = _Max;
1727 }
1728}
1729
1730template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1731 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1732 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1733template<class _Sseq>
1734void
1735mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1736 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
1737{
1738 const unsigned __k = 2;
1739 uint32_t __ar[__n * __k];
1740 __q.generate(__ar, __ar + __n * __k);
1741 for (size_t __i = 0; __i < __n; ++__i)
1742 __x_[__i] = static_cast<result_type>(
1743 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1744 const result_type __mask = __r == _Dt ? result_type(~0) :
1745 (result_type(1) << __r) - result_type(1);
1746 __i_ = 0;
1747 if ((__x_[0] & ~__mask) == 0)
1748 {
1749 for (size_t __i = 1; __i < __n; ++__i)
1750 if (__x_[__i] != 0)
1751 return;
1752 __x_[0] = _Max;
1753 }
1754}
1755
1756template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1757 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1758 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1759_UIntType
1760mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1761 __t, __c, __l, __f>::operator()()
1762{
1763 const size_t __j = (__i_ + 1) % __n;
1764 const result_type __mask = __r == _Dt ? result_type(~0) :
1765 (result_type(1) << __r) - result_type(1);
1766 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
1767 const size_t __k = (__i_ + __m) % __n;
1768 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
1769 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
1770 __i_ = __j;
1771 __z ^= __lshift<__s>(__z) & __b;
1772 __z ^= __lshift<__t>(__z) & __c;
1773 return __z ^ __rshift<__l>(__z);
1774}
1775
1776template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1777 _UI _A, size_t _U, _UI _D, size_t _S,
1778 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1779bool
1780operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1781 _B, _T, _C, _L, _F>& __x,
1782 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1783 _B, _T, _C, _L, _F>& __y)
1784{
1785 if (__x.__i_ == __y.__i_)
1786 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
1787 if (__x.__i_ == 0 || __y.__i_ == 0)
1788 {
1789 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
1790 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1791 __y.__x_ + __y.__i_))
1792 return false;
1793 if (__x.__i_ == 0)
1794 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
1795 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
1796 }
1797 if (__x.__i_ < __y.__i_)
1798 {
1799 size_t __j = _N - __y.__i_;
1800 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1801 __y.__x_ + __y.__i_))
1802 return false;
1803 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
1804 __y.__x_))
1805 return false;
1806 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1807 __y.__x_ + (_N - (__x.__i_ + __j)));
1808 }
1809 size_t __j = _N - __x.__i_;
1810 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1811 __x.__x_ + __x.__i_))
1812 return false;
1813 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
1814 __x.__x_))
1815 return false;
1816 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1817 __x.__x_ + (_N - (__y.__i_ + __j)));
1818}
1819
1820template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1821 _UI _A, size_t _U, _UI _D, size_t _S,
1822 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1823inline
1824bool
1825operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1826 _B, _T, _C, _L, _F>& __x,
1827 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1828 _B, _T, _C, _L, _F>& __y)
1829{
1830 return !(__x == __y);
1831}
1832
1833template <class _CharT, class _Traits,
1834 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1835 _UI _A, size_t _U, _UI _D, size_t _S,
1836 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1837basic_ostream<_CharT, _Traits>&
1838operator<<(basic_ostream<_CharT, _Traits>& __os,
1839 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1840 _B, _T, _C, _L, _F>& __x)
1841{
1842 __save_flags<_CharT, _Traits> _(__os);
1843 __os.flags(ios_base::dec | ios_base::left);
1844 _CharT __sp = __os.widen(' ');
1845 __os.fill(__sp);
1846 __os << __x.__x_[__x.__i_];
1847 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
1848 __os << __sp << __x.__x_[__j];
1849 for (size_t __j = 0; __j < __x.__i_; ++__j)
1850 __os << __sp << __x.__x_[__j];
1851 return __os;
1852}
1853
1854template <class _CharT, class _Traits,
1855 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1856 _UI _A, size_t _U, _UI _D, size_t _S,
1857 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1858basic_istream<_CharT, _Traits>&
1859operator>>(basic_istream<_CharT, _Traits>& __is,
1860 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1861 _B, _T, _C, _L, _F>& __x)
1862{
1863 __save_flags<_CharT, _Traits> _(__is);
1864 __is.flags(ios_base::dec | ios_base::skipws);
1865 _UI __t[_N];
1866 for (size_t __i = 0; __i < _N; ++__i)
1867 __is >> __t[__i];
1868 if (!__is.fail())
1869 {
1870 for (size_t __i = 0; __i < _N; ++__i)
1871 __x.__x_[__i] = __t[__i];
1872 __x.__i_ = 0;
1873 }
1874 return __is;
1875}
1876
1877typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
1878 0x9908b0df, 11, 0xffffffff,
1879 7, 0x9d2c5680,
1880 15, 0xefc60000,
1881 18, 1812433253> mt19937;
1882typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
1883 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
1884 17, 0x71d67fffeda60000ULL,
1885 37, 0xfff7eee000000000ULL,
1886 43, 6364136223846793005ULL> mt19937_64;
1887
1888// subtract_with_carry_engine
1889
1890template<class _UIntType, size_t __w, size_t __s, size_t __r>
1891class subtract_with_carry_engine;
1892
1893template<class _UI, size_t _W, size_t _S, size_t _R>
1894bool
1895operator==(
1896 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1897 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1898
1899template<class _UI, size_t _W, size_t _S, size_t _R>
1900bool
1901operator!=(
1902 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1903 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1904
1905template <class _CharT, class _Traits,
1906 class _UI, size_t _W, size_t _S, size_t _R>
1907basic_ostream<_CharT, _Traits>&
1908operator<<(basic_ostream<_CharT, _Traits>& __os,
1909 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1910
1911template <class _CharT, class _Traits,
1912 class _UI, size_t _W, size_t _S, size_t _R>
1913basic_istream<_CharT, _Traits>&
1914operator>>(basic_istream<_CharT, _Traits>& __is,
1915 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1916
1917template<class _UIntType, size_t __w, size_t __s, size_t __r>
1918class subtract_with_carry_engine
1919{
1920public:
1921 // types
1922 typedef _UIntType result_type;
1923
1924private:
1925 result_type __x_[__r];
1926 result_type __c_;
1927 size_t __i_;
1928
1929 static const result_type _Dt = numeric_limits<result_type>::digits;
1930 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
1931 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
1932 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
1933 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
1934public:
1935 static const result_type _Min = 0;
1936 static const result_type _Max = __w == _Dt ? result_type(~0) :
1937 (result_type(1) << __w) - result_type(1);
1938 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
1939
1940 // engine characteristics
1941 static const/*expr*/ size_t word_size = __w;
1942 static const/*expr*/ size_t short_lag = __s;
1943 static const/*expr*/ size_t long_lag = __r;
1944 static const/*expr*/ result_type min() { return _Min; }
1945 static const/*expr*/ result_type max() { return _Max; }
1946 static const/*expr*/ result_type default_seed = 19780503u;
1947
1948 // constructors and seeding functions
1949 explicit subtract_with_carry_engine(result_type __sd = default_seed)
1950 {seed(__sd);}
1951 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
1952 {seed(__q);}
1953 void seed(result_type __sd = default_seed)
1954 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1955 template<class _Sseq>
1956 typename enable_if
1957 <
1958 !is_convertible<_Sseq, result_type>::value,
1959 void
1960 >::type
1961 seed(_Sseq& __q)
1962 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1963
1964 // generating functions
1965 result_type operator()();
1966 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1967
1968 template<class _UI, size_t _W, size_t _S, size_t _R>
1969 friend
1970 bool
1971 operator==(
1972 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1973 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1974
1975 template<class _UI, size_t _W, size_t _S, size_t _R>
1976 friend
1977 bool
1978 operator!=(
1979 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1980 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1981
1982 template <class _CharT, class _Traits,
1983 class _UI, size_t _W, size_t _S, size_t _R>
1984 friend
1985 basic_ostream<_CharT, _Traits>&
1986 operator<<(basic_ostream<_CharT, _Traits>& __os,
1987 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1988
1989 template <class _CharT, class _Traits,
1990 class _UI, size_t _W, size_t _S, size_t _R>
1991 friend
1992 basic_istream<_CharT, _Traits>&
1993 operator>>(basic_istream<_CharT, _Traits>& __is,
1994 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1995
1996private:
1997
1998 void seed(result_type __sd, integral_constant<unsigned, 1>);
1999 void seed(result_type __sd, integral_constant<unsigned, 2>);
2000 template<class _Sseq>
2001 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2002 template<class _Sseq>
2003 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2004};
2005
2006template<class _UIntType, size_t __w, size_t __s, size_t __r>
2007void
2008subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2009 integral_constant<unsigned, 1>)
2010{
2011 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2012 __e(__sd == 0u ? default_seed : __sd);
2013 for (size_t __i = 0; __i < __r; ++__i)
2014 __x_[__i] = static_cast<result_type>(__e() & _Max);
2015 __c_ = __x_[__r-1] == 0;
2016 __i_ = 0;
2017}
2018
2019template<class _UIntType, size_t __w, size_t __s, size_t __r>
2020void
2021subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2022 integral_constant<unsigned, 2>)
2023{
2024 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2025 __e(__sd == 0u ? default_seed : __sd);
2026 for (size_t __i = 0; __i < __r; ++__i)
2027 __x_[__i] = static_cast<result_type>(
2028 (__e() + ((uint64_t)__e() << 32)) & _Max);
2029 __c_ = __x_[__r-1] == 0;
2030 __i_ = 0;
2031}
2032
2033template<class _UIntType, size_t __w, size_t __s, size_t __r>
2034template<class _Sseq>
2035void
2036subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2037 integral_constant<unsigned, 1>)
2038{
2039 const unsigned __k = 1;
2040 uint32_t __ar[__r * __k];
2041 __q.generate(__ar, __ar + __r * __k);
2042 for (size_t __i = 0; __i < __r; ++__i)
2043 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2044 __c_ = __x_[__r-1] == 0;
2045 __i_ = 0;
2046}
2047
2048template<class _UIntType, size_t __w, size_t __s, size_t __r>
2049template<class _Sseq>
2050void
2051subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2052 integral_constant<unsigned, 2>)
2053{
2054 const unsigned __k = 2;
2055 uint32_t __ar[__r * __k];
2056 __q.generate(__ar, __ar + __r * __k);
2057 for (size_t __i = 0; __i < __r; ++__i)
2058 __x_[__i] = static_cast<result_type>(
2059 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2060 __c_ = __x_[__r-1] == 0;
2061 __i_ = 0;
2062}
2063
2064template<class _UIntType, size_t __w, size_t __s, size_t __r>
2065_UIntType
2066subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2067{
2068 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2069 result_type& __xr = __x_[__i_];
2070 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2071 __xr = (__xs - __xr - __c_) & _Max;
2072 __c_ = __new_c;
2073 __i_ = (__i_ + 1) % __r;
2074 return __xr;
2075}
2076
2077template<class _UI, size_t _W, size_t _S, size_t _R>
2078bool
2079operator==(
2080 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2081 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2082{
2083 if (__x.__c_ != __y.__c_)
2084 return false;
2085 if (__x.__i_ == __y.__i_)
2086 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
2087 if (__x.__i_ == 0 || __y.__i_ == 0)
2088 {
2089 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
2090 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2091 __y.__x_ + __y.__i_))
2092 return false;
2093 if (__x.__i_ == 0)
2094 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2095 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
2096 }
2097 if (__x.__i_ < __y.__i_)
2098 {
2099 size_t __j = _R - __y.__i_;
2100 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2101 __y.__x_ + __y.__i_))
2102 return false;
2103 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
2104 __y.__x_))
2105 return false;
2106 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2107 __y.__x_ + (_R - (__x.__i_ + __j)));
2108 }
2109 size_t __j = _R - __x.__i_;
2110 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2111 __x.__x_ + __x.__i_))
2112 return false;
2113 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
2114 __x.__x_))
2115 return false;
2116 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2117 __x.__x_ + (_R - (__y.__i_ + __j)));
2118}
2119
2120template<class _UI, size_t _W, size_t _S, size_t _R>
2121inline
2122bool
2123operator!=(
2124 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2125 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2126{
2127 return !(__x == __y);
2128}
2129
2130template <class _CharT, class _Traits,
2131 class _UI, size_t _W, size_t _S, size_t _R>
2132basic_ostream<_CharT, _Traits>&
2133operator<<(basic_ostream<_CharT, _Traits>& __os,
2134 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2135{
2136 __save_flags<_CharT, _Traits> _(__os);
2137 __os.flags(ios_base::dec | ios_base::left);
2138 _CharT __sp = __os.widen(' ');
2139 __os.fill(__sp);
2140 __os << __x.__x_[__x.__i_];
2141 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
2142 __os << __sp << __x.__x_[__j];
2143 for (size_t __j = 0; __j < __x.__i_; ++__j)
2144 __os << __sp << __x.__x_[__j];
2145 __os << __sp << __x.__c_;
2146 return __os;
2147}
2148
2149template <class _CharT, class _Traits,
2150 class _UI, size_t _W, size_t _S, size_t _R>
2151basic_istream<_CharT, _Traits>&
2152operator>>(basic_istream<_CharT, _Traits>& __is,
2153 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2154{
2155 __save_flags<_CharT, _Traits> _(__is);
2156 __is.flags(ios_base::dec | ios_base::skipws);
2157 _UI __t[_R+1];
2158 for (size_t __i = 0; __i < _R+1; ++__i)
2159 __is >> __t[__i];
2160 if (!__is.fail())
2161 {
2162 for (size_t __i = 0; __i < _R; ++__i)
2163 __x.__x_[__i] = __t[__i];
2164 __x.__c_ = __t[_R];
2165 __x.__i_ = 0;
2166 }
2167 return __is;
2168}
2169
2170typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2171typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2172
2173// discard_block_engine
2174
2175template<class _Engine, size_t __p, size_t __r>
2176class discard_block_engine
2177{
2178 _Engine __e_;
2179 int __n_;
2180
2181 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2182 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2183public:
2184 // types
2185 typedef typename _Engine::result_type result_type;
2186
2187 // engine characteristics
2188 static const/*expr*/ size_t block_size = __p;
2189 static const/*expr*/ size_t used_block = __r;
2190
2191 // Temporary work around for lack of constexpr
2192 static const result_type _Min = _Engine::_Min;
2193 static const result_type _Max = _Engine::_Max;
2194
2195 static const/*expr*/ result_type min() { return _Engine::min(); }
2196 static const/*expr*/ result_type max() { return _Engine::max(); }
2197
2198 // constructors and seeding functions
2199 discard_block_engine() : __n_(0) {}
2200// explicit discard_block_engine(const _Engine& __e);
2201// explicit discard_block_engine(_Engine&& __e);
2202 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
2203 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
2204 : __e_(__q), __n_(0) {}
2205 void seed() {__e_.seed(); __n_ = 0;}
2206 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
2207 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
2208
2209 // generating functions
2210 result_type operator()();
2211 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2212
2213 // property functions
2214 const _Engine& base() const {return __e_;}
2215
2216 template<class _Eng, size_t _P, size_t _R>
2217 friend
2218 bool
2219 operator==(
2220 const discard_block_engine<_Eng, _P, _R>& __x,
2221 const discard_block_engine<_Eng, _P, _R>& __y);
2222
2223 template<class _Eng, size_t _P, size_t _R>
2224 friend
2225 bool
2226 operator!=(
2227 const discard_block_engine<_Eng, _P, _R>& __x,
2228 const discard_block_engine<_Eng, _P, _R>& __y);
2229
2230 template <class _CharT, class _Traits,
2231 class _Eng, size_t _P, size_t _R>
2232 friend
2233 basic_ostream<_CharT, _Traits>&
2234 operator<<(basic_ostream<_CharT, _Traits>& __os,
2235 const discard_block_engine<_Eng, _P, _R>& __x);
2236
2237 template <class _CharT, class _Traits,
2238 class _Eng, size_t _P, size_t _R>
2239 friend
2240 basic_istream<_CharT, _Traits>&
2241 operator>>(basic_istream<_CharT, _Traits>& __is,
2242 discard_block_engine<_Eng, _P, _R>& __x);
2243};
2244
2245template<class _Engine, size_t __p, size_t __r>
2246typename discard_block_engine<_Engine, __p, __r>::result_type
2247discard_block_engine<_Engine, __p, __r>::operator()()
2248{
2249 if (__n_ >= __r)
2250 {
2251 __e_.discard(__p - __r);
2252 __n_ = 0;
2253 }
2254 ++__n_;
2255 return __e_();
2256}
2257
2258template<class _Eng, size_t _P, size_t _R>
2259inline
2260bool
2261operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2262 const discard_block_engine<_Eng, _P, _R>& __y)
2263{
2264 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2265}
2266
2267template<class _Eng, size_t _P, size_t _R>
2268inline
2269bool
2270operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2271 const discard_block_engine<_Eng, _P, _R>& __y)
2272{
2273 return !(__x == __y);
2274}
2275
2276template <class _CharT, class _Traits,
2277 class _Eng, size_t _P, size_t _R>
2278basic_ostream<_CharT, _Traits>&
2279operator<<(basic_ostream<_CharT, _Traits>& __os,
2280 const discard_block_engine<_Eng, _P, _R>& __x)
2281{
2282 __save_flags<_CharT, _Traits> _(__os);
2283 __os.flags(ios_base::dec | ios_base::left);
2284 _CharT __sp = __os.widen(' ');
2285 __os.fill(__sp);
2286 return __os << __x.__e_ << __sp << __x.__n_;
2287}
2288
2289template <class _CharT, class _Traits,
2290 class _Eng, size_t _P, size_t _R>
2291basic_istream<_CharT, _Traits>&
2292operator>>(basic_istream<_CharT, _Traits>& __is,
2293 discard_block_engine<_Eng, _P, _R>& __x)
2294{
2295 __save_flags<_CharT, _Traits> _(__is);
2296 __is.flags(ios_base::dec | ios_base::skipws);
2297 _Eng __e;
2298 int __n;
2299 __is >> __e >> __n;
2300 if (!__is.fail())
2301 {
2302 __x.__e_ = __e;
2303 __x.__n_ = __n;
2304 }
2305 return __is;
2306}
2307
2308typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2309typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2310
2311// independent_bits_engine
2312
2313template <unsigned long long _X, size_t _R>
2314struct __log2_imp
2315{
2316 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2317 : __log2_imp<_X, _R - 1>::value;
2318};
2319
2320template <unsigned long long _X>
2321struct __log2_imp<_X, 0>
2322{
2323 static const size_t value = 0;
2324};
2325
2326template <size_t _R>
2327struct __log2_imp<0, _R>
2328{
2329 static const size_t value = _R + 1;
2330};
2331
2332template <class _UI, _UI _X>
2333struct __log2
2334{
2335 static const size_t value = __log2_imp<_X,
2336 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2337};
2338
2339template<class _Engine, size_t __w, class _UIntType>
2340class independent_bits_engine
2341{
2342 template <class _UI, _UI _R0, size_t _W, size_t _M>
2343 class __get_n
2344 {
2345 static const size_t _Dt = numeric_limits<_UI>::digits;
2346 static const size_t _N = _W / _M + (_W % _M != 0);
2347 static const size_t _W0 = _W / _N;
2348 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2349 public:
2350 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2351 };
2352public:
2353 // types
2354 typedef _UIntType result_type;
2355
2356private:
2357 _Engine __e_;
2358
2359 static const result_type _Dt = numeric_limits<result_type>::digits;
2360 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2361 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2362
2363 typedef typename _Engine::result_type _Engine_result_type;
2364 typedef typename conditional
2365 <
2366 sizeof(_Engine_result_type) <= sizeof(result_type),
2367 result_type,
2368 _Engine_result_type
2369 >::type _Working_result_type;
2370 // Temporary work around for lack of constexpr
2371 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2372 + _Working_result_type(1);
2373 static const size_t __m = __log2<_Working_result_type, _R>::value;
2374 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2375 static const size_t __w0 = __w / __n;
2376 static const size_t __n0 = __n - __w % __n;
2377 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2378 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2379 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2380 (_R >> __w0) << __w0;
2381 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2382 (_R >> (__w0+1)) << (__w0+1);
2383 static const _Engine_result_type __mask0 = __w0 > 0 ?
2384 _Engine_result_type(~0) >> (_EDt - __w0) :
2385 _Engine_result_type(0);
2386 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2387 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2388 _Engine_result_type(~0);
2389public:
2390 static const result_type _Min = 0;
2391 static const result_type _Max = __w == _Dt ? result_type(~0) :
2392 (result_type(1) << __w) - result_type(1);
2393 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2394
2395 // engine characteristics
2396 static const/*expr*/ result_type min() { return _Min; }
2397 static const/*expr*/ result_type max() { return _Max; }
2398
2399 // constructors and seeding functions
2400 independent_bits_engine() {}
2401// explicit independent_bits_engine(const _Engine& __e);
2402// explicit independent_bits_engine(_Engine&& __e);
2403 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2404 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2405 : __e_(__q) {}
2406 void seed() {__e_.seed();}
2407 void seed(result_type __sd) {__e_.seed(__sd);}
2408 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2409
2410 // generating functions
2411 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2412 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2413
2414 // property functions
2415 const _Engine& base() const {return __e_;}
2416
2417 template<class _Eng, size_t _W, class _UI>
2418 friend
2419 bool
2420 operator==(
2421 const independent_bits_engine<_Eng, _W, _UI>& __x,
2422 const independent_bits_engine<_Eng, _W, _UI>& __y);
2423
2424 template<class _Eng, size_t _W, class _UI>
2425 friend
2426 bool
2427 operator!=(
2428 const independent_bits_engine<_Eng, _W, _UI>& __x,
2429 const independent_bits_engine<_Eng, _W, _UI>& __y);
2430
2431 template <class _CharT, class _Traits,
2432 class _Eng, size_t _W, class _UI>
2433 friend
2434 basic_ostream<_CharT, _Traits>&
2435 operator<<(basic_ostream<_CharT, _Traits>& __os,
2436 const independent_bits_engine<_Eng, _W, _UI>& __x);
2437
2438 template <class _CharT, class _Traits,
2439 class _Eng, size_t _W, class _UI>
2440 friend
2441 basic_istream<_CharT, _Traits>&
2442 operator>>(basic_istream<_CharT, _Traits>& __is,
2443 independent_bits_engine<_Eng, _W, _UI>& __x);
2444
2445private:
2446 result_type __eval(false_type);
2447 result_type __eval(true_type);
2448
2449 template <size_t __count>
2450 static
2451 typename enable_if
2452 <
2453 __count < _Dt,
2454 result_type
2455 >::type
2456 __lshift(result_type __x) {return __x << __count;}
2457
2458 template <size_t __count>
2459 static
2460 typename enable_if
2461 <
2462 (__count >= _Dt),
2463 result_type
2464 >::type
2465 __lshift(result_type __x) {return result_type(0);}
2466};
2467
2468template<class _Engine, size_t __w, class _UIntType>
2469inline
2470_UIntType
2471independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2472{
2473 return static_cast<result_type>(__e_() & __mask0);
2474}
2475
2476template<class _Engine, size_t __w, class _UIntType>
2477_UIntType
2478independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2479{
2480 result_type _S = 0;
2481 for (size_t __k = 0; __k < __n0; ++__k)
2482 {
2483 _Engine_result_type __u;
2484 do
2485 {
2486 __u = __e_() - _Engine::min();
2487 } while (__u >= __y0);
2488 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2489 }
2490 for (size_t __k = __n0; __k < __n; ++__k)
2491 {
2492 _Engine_result_type __u;
2493 do
2494 {
2495 __u = __e_() - _Engine::min();
2496 } while (__u >= __y1);
2497 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2498 }
2499 return _S;
2500}
2501
2502template<class _Eng, size_t _W, class _UI>
2503inline
2504bool
2505operator==(
2506 const independent_bits_engine<_Eng, _W, _UI>& __x,
2507 const independent_bits_engine<_Eng, _W, _UI>& __y)
2508{
2509 return __x.base() == __y.base();
2510}
2511
2512template<class _Eng, size_t _W, class _UI>
2513inline
2514bool
2515operator!=(
2516 const independent_bits_engine<_Eng, _W, _UI>& __x,
2517 const independent_bits_engine<_Eng, _W, _UI>& __y)
2518{
2519 return !(__x == __y);
2520}
2521
2522template <class _CharT, class _Traits,
2523 class _Eng, size_t _W, class _UI>
2524basic_ostream<_CharT, _Traits>&
2525operator<<(basic_ostream<_CharT, _Traits>& __os,
2526 const independent_bits_engine<_Eng, _W, _UI>& __x)
2527{
2528 return __os << __x.base();
2529}
2530
2531template <class _CharT, class _Traits,
2532 class _Eng, size_t _W, class _UI>
2533basic_istream<_CharT, _Traits>&
2534operator>>(basic_istream<_CharT, _Traits>& __is,
2535 independent_bits_engine<_Eng, _W, _UI>& __x)
2536{
2537 _Eng __e;
2538 __is >> __e;
2539 if (!__is.fail())
2540 __x.__e_ = __e;
2541 return __is;
2542}
2543
2544// shuffle_order_engine
2545
2546template <uint64_t _Xp, uint64_t _Yp>
2547struct __ugcd
2548{
2549 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2550};
2551
2552template <uint64_t _Xp>
2553struct __ugcd<_Xp, 0>
2554{
2555 static const uint64_t value = _Xp;
2556};
2557
2558template <uint64_t _N, uint64_t _D>
2559class __uratio
2560{
2561 static_assert(_D != 0, "__uratio divide by 0");
2562 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2563public:
2564 static const uint64_t num = _N / __gcd;
2565 static const uint64_t den = _D / __gcd;
2566
2567 typedef __uratio<num, den> type;
2568};
2569
2570template<class _Engine, size_t __k>
2571class shuffle_order_engine
2572{
2573 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2574public:
2575 // types
2576 typedef typename _Engine::result_type result_type;
2577
2578private:
2579 _Engine __e_;
2580 result_type _V_[__k];
2581 result_type _Y_;
2582
2583public:
2584 // engine characteristics
2585 static const/*expr*/ size_t table_size = __k;
2586
2587 static const result_type _Min = _Engine::_Min;
2588 static const result_type _Max = _Engine::_Max;
2589 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2590 static const/*expr*/ result_type min() { return _Min; }
2591 static const/*expr*/ result_type max() { return _Max; }
2592
2593 static const unsigned long long _R = _Max - _Min + 1ull;
2594
2595 // constructors and seeding functions
2596 shuffle_order_engine() {__init();}
2597// explicit shuffle_order_engine(const _Engine& __e);
2598// explicit shuffle_order_engine(_Engine&& e);
2599 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
2600 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
2601 : __e_(__q) {__init();}
2602 void seed() {__e_.seed(); __init();}
2603 void seed(result_type __sd) {__e_.seed(__sd); __init();}
2604 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
2605
2606 // generating functions
2607 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2608 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2609
2610 // property functions
2611 const _Engine& base() const {return __e_;}
2612
2613private:
2614 template<class _Eng, size_t _K>
2615 friend
2616 bool
2617 operator==(
2618 const shuffle_order_engine<_Eng, _K>& __x,
2619 const shuffle_order_engine<_Eng, _K>& __y);
2620
2621 template<class _Eng, size_t _K>
2622 friend
2623 bool
2624 operator!=(
2625 const shuffle_order_engine<_Eng, _K>& __x,
2626 const shuffle_order_engine<_Eng, _K>& __y);
2627
2628 template <class _CharT, class _Traits,
2629 class _Eng, size_t _K>
2630 friend
2631 basic_ostream<_CharT, _Traits>&
2632 operator<<(basic_ostream<_CharT, _Traits>& __os,
2633 const shuffle_order_engine<_Eng, _K>& __x);
2634
2635 template <class _CharT, class _Traits,
2636 class _Eng, size_t _K>
2637 friend
2638 basic_istream<_CharT, _Traits>&
2639 operator>>(basic_istream<_CharT, _Traits>& __is,
2640 shuffle_order_engine<_Eng, _K>& __x);
2641
2642 void __init()
2643 {
2644 for (size_t __i = 0; __i < __k; ++__i)
2645 _V_[__i] = __e_();
2646 _Y_ = __e_();
2647 }
2648
2649 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
2650 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
2651
2652 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
2653 result_type __eval2(true_type) {return __evalf<__k, 0>();}
2654
2655 template <uint64_t _N, uint64_t _D>
2656 typename enable_if
2657 <
2658 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
2659 result_type
2660 >::type
2661 __eval(__uratio<_N, _D>)
2662 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
2663
2664 template <uint64_t _N, uint64_t _D>
2665 typename enable_if
2666 <
2667 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
2668 result_type
2669 >::type
2670 __eval(__uratio<_N, _D>)
2671 {
2672 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
2673 / __uratio<_N, _D>::den);
2674 _Y_ = _V_[__j];
2675 _V_[__j] = __e_();
2676 return _Y_;
2677 }
2678
2679 template <uint64_t __n, uint64_t __d>
2680 result_type __evalf()
2681 {
2682 const double _F = __d == 0 ?
2683 __n / (2. * 0x8000000000000000ull) :
2684 __n / (double)__d;
2685 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
2686 _Y_ = _V_[__j];
2687 _V_[__j] = __e_();
2688 return _Y_;
2689 }
2690};
2691
2692template<class _Eng, size_t _K>
2693bool
2694operator==(
2695 const shuffle_order_engine<_Eng, _K>& __x,
2696 const shuffle_order_engine<_Eng, _K>& __y)
2697{
2698 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
2699 __x.__e_ == __y.__e_;
2700}
2701
2702template<class _Eng, size_t _K>
2703inline
2704bool
2705operator!=(
2706 const shuffle_order_engine<_Eng, _K>& __x,
2707 const shuffle_order_engine<_Eng, _K>& __y)
2708{
2709 return !(__x == __y);
2710}
2711
2712template <class _CharT, class _Traits,
2713 class _Eng, size_t _K>
2714basic_ostream<_CharT, _Traits>&
2715operator<<(basic_ostream<_CharT, _Traits>& __os,
2716 const shuffle_order_engine<_Eng, _K>& __x)
2717{
2718 __save_flags<_CharT, _Traits> _(__os);
2719 __os.flags(ios_base::dec | ios_base::left);
2720 _CharT __sp = __os.widen(' ');
2721 __os.fill(__sp);
2722 __os << __x.__e_ << __sp << __x._V_[0];
2723 for (size_t __i = 1; __i < _K; ++__i)
2724 __os << __sp << __x._V_[__i];
2725 return __os << __sp << __x._Y_;
2726}
2727
2728template <class _CharT, class _Traits,
2729 class _Eng, size_t _K>
2730basic_istream<_CharT, _Traits>&
2731operator>>(basic_istream<_CharT, _Traits>& __is,
2732 shuffle_order_engine<_Eng, _K>& __x)
2733{
2734 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
2735 __save_flags<_CharT, _Traits> _(__is);
2736 __is.flags(ios_base::dec | ios_base::skipws);
2737 _Eng __e;
2738 result_type _V[_K+1];
2739 __is >> __e;
2740 for (size_t __i = 0; __i < _K+1; ++__i)
2741 __is >> _V[__i];
2742 if (!__is.fail())
2743 {
2744 __x.__e_ = __e;
2745 for (size_t __i = 0; __i < _K; ++__i)
2746 __x._V_[__i] = _V[__i];
2747 __x._Y_ = _V[_K];
2748 }
2749 return __is;
2750}
2751
2752typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
2753
2754// random_device
2755
2756class random_device
2757{
2758 int __f_;
2759public:
2760 // types
2761 typedef unsigned result_type;
2762
2763 // generator characteristics
2764 static const result_type _Min = 0;
2765 static const result_type _Max = 0xFFFFFFFFu;
2766
2767 static const/*expr*/ result_type min() { return _Min;}
2768 static const/*expr*/ result_type max() { return _Max;}
2769
2770 // constructors
2771 explicit random_device(const string& __token = "/dev/urandom");
2772 ~random_device();
2773
2774 // generating functions
2775 result_type operator()();
2776
2777 // property functions
2778 double entropy() const;
2779
2780private:
2781 // no copy functions
2782 random_device(const random_device&); // = delete;
2783 random_device& operator=(const random_device&); // = delete;
2784};
2785
2786// seed_seq
2787
2788class seed_seq
2789{
2790public:
2791 // types
2792 typedef uint32_t result_type;
2793
2794private:
2795 vector<result_type> __v_;
2796
2797 template<class _InputIterator>
2798 void init(_InputIterator __first, _InputIterator __last);
2799public:
2800 // constructors
2801 seed_seq() {}
2802 template<class _Tp>
2803 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
2804
2805 template<class _InputIterator>
2806 seed_seq(_InputIterator __first, _InputIterator __last)
2807 {init(__first, __last);}
2808
2809 // generating functions
2810 template<class _RandomAccessIterator>
2811 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
2812
2813 // property functions
2814 size_t size() const {return __v_.size();}
2815 template<class _OutputIterator>
2816 void param(_OutputIterator __dest) const
2817 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
2818
2819private:
2820 // no copy functions
2821 seed_seq(const seed_seq&); // = delete;
2822 void operator=(const seed_seq&); // = delete;
2823
2824 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
2825};
2826
2827template<class _InputIterator>
2828void
2829seed_seq::init(_InputIterator __first, _InputIterator __last)
2830{
2831 for (_InputIterator __s = __first; __s != __last; ++__s)
2832 __v_.push_back(*__s & 0xFFFFFFFF);
2833}
2834
2835template<class _RandomAccessIterator>
2836void
2837seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
2838{
2839 if (__first != __last)
2840 {
2841 _STD::fill(__first, __last, 0x8b8b8b8b);
2842 const size_t __n = static_cast<size_t>(__last - __first);
2843 const size_t __s = __v_.size();
2844 const size_t __t = (__n >= 623) ? 11
2845 : (__n >= 68) ? 7
2846 : (__n >= 39) ? 5
2847 : (__n >= 7) ? 3
2848 : (__n - 1) / 2;
2849 const size_t __p = (__n - __t) / 2;
2850 const size_t __q = __p + __t;
2851 const size_t __m = _STD::max(__s + 1, __n);
2852 // __k = 0;
2853 {
2854 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
2855 ^ __first[__n - 1]);
2856 __first[__p] += __r;
2857 __r += __s;
2858 __first[__q] += __r;
2859 __first[0] = __r;
2860 }
2861 for (size_t __k = 1; __k <= __s; ++__k)
2862 {
2863 const size_t __kmodn = __k % __n;
2864 const size_t __kpmodn = (__k + __p) % __n;
2865 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2866 ^ __first[(__k - 1) % __n]);
2867 __first[__kpmodn] += __r;
2868 __r += __kmodn + __v_[__k-1];
2869 __first[(__k + __q) % __n] += __r;
2870 __first[__kmodn] = __r;
2871 }
2872 for (size_t __k = __s + 1; __k < __m; ++__k)
2873 {
2874 const size_t __kmodn = __k % __n;
2875 const size_t __kpmodn = (__k + __p) % __n;
2876 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2877 ^ __first[(__k - 1) % __n]);
2878 __first[__kpmodn] += __r;
2879 __r += __kmodn;
2880 __first[(__k + __q) % __n] += __r;
2881 __first[__kmodn] = __r;
2882 }
2883 for (size_t __k = __m; __k < __m + __n; ++__k)
2884 {
2885 const size_t __kmodn = __k % __n;
2886 const size_t __kpmodn = (__k + __p) % __n;
2887 result_type __r = 1566083941 * _T(__first[__kmodn] +
2888 __first[__kpmodn] +
2889 __first[(__k - 1) % __n]);
2890 __first[__kpmodn] ^= __r;
2891 __r -= __kmodn;
2892 __first[(__k + __q) % __n] ^= __r;
2893 __first[__kmodn] = __r;
2894 }
2895 }
2896}
2897
Howard Hinnant30a840f2010-05-12 17:08:57 +00002898// generate_canonical
2899
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002900template<class _RealType, size_t __bits, class _URNG>
2901_RealType
2902generate_canonical(_URNG& __g)
2903{
2904 const size_t _Dt = numeric_limits<_RealType>::digits;
2905 const size_t __b = _Dt < __bits ? _Dt : __bits;
2906 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
2907 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
2908 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
2909 _RealType __base = _R;
2910 _RealType _S = __g() - _URNG::_Min;
2911 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
2912 _S += (__g() - _URNG::_Min) * __base;
2913 return _S / __base;
2914}
2915
2916// __independent_bits_engine
2917
2918template<class _Engine, class _UIntType>
2919class __independent_bits_engine
2920{
2921public:
2922 // types
2923 typedef _UIntType result_type;
2924
2925private:
2926 typedef typename _Engine::result_type _Engine_result_type;
2927 typedef typename conditional
2928 <
2929 sizeof(_Engine_result_type) <= sizeof(result_type),
2930 result_type,
2931 _Engine_result_type
2932 >::type _Working_result_type;
2933
2934 _Engine& __e_;
2935 size_t __w_;
2936 size_t __w0_;
2937 size_t __n_;
2938 size_t __n0_;
2939 _Working_result_type __y0_;
2940 _Working_result_type __y1_;
2941 _Engine_result_type __mask0_;
2942 _Engine_result_type __mask1_;
2943
2944 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2945 + _Working_result_type(1);
2946 static const size_t __m = __log2<_Working_result_type, _R>::value;
2947 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2948 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2949
2950public:
2951 // constructors and seeding functions
2952 __independent_bits_engine(_Engine& __e, size_t __w);
2953
2954 // generating functions
2955 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2956
2957private:
2958 result_type __eval(false_type);
2959 result_type __eval(true_type);
2960};
2961
2962template<class _Engine, class _UIntType>
2963__independent_bits_engine<_Engine, _UIntType>
2964 ::__independent_bits_engine(_Engine& __e, size_t __w)
2965 : __e_(__e),
2966 __w_(__w)
2967{
2968 __n_ = __w_ / __m + (__w_ % __m != 0);
2969 __w0_ = __w_ / __n_;
2970 if (_R == 0)
2971 __y0_ = _R;
2972 else if (__w0_ < _WDt)
2973 __y0_ = (_R >> __w0_) << __w0_;
2974 else
2975 __y0_ = 0;
2976 if (_R - __y0_ > __y0_ / __n_)
2977 {
2978 ++__n_;
2979 __w0_ = __w_ / __n_;
2980 if (__w0_ < _WDt)
2981 __y0_ = (_R >> __w0_) << __w0_;
2982 else
2983 __y0_ = 0;
2984 }
2985 __n0_ = __n_ - __w_ % __n_;
2986 if (__w0_ < _WDt - 1)
2987 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2988 else
2989 __y1_ = 0;
2990 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2991 _Engine_result_type(0);
2992 __mask1_ = __w0_ < _EDt - 1 ?
2993 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2994 _Engine_result_type(~0);
2995}
2996
2997template<class _Engine, class _UIntType>
2998inline
2999_UIntType
3000__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
3001{
3002 return static_cast<result_type>(__e_() & __mask0_);
3003}
3004
3005template<class _Engine, class _UIntType>
3006_UIntType
3007__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
3008{
3009 result_type _S = 0;
3010 for (size_t __k = 0; __k < __n0_; ++__k)
3011 {
3012 _Engine_result_type __u;
3013 do
3014 {
3015 __u = __e_() - _Engine::min();
3016 } while (__u >= __y0_);
3017 if (__w0_ < _EDt)
3018 _S <<= __w0_;
3019 else
3020 _S = 0;
3021 _S += __u & __mask0_;
3022 }
3023 for (size_t __k = __n0_; __k < __n_; ++__k)
3024 {
3025 _Engine_result_type __u;
3026 do
3027 {
3028 __u = __e_() - _Engine::min();
3029 } while (__u >= __y1_);
3030 if (__w0_ < _EDt - 1)
3031 _S <<= __w0_ + 1;
3032 else
3033 _S = 0;
3034 _S += __u & __mask1_;
3035 }
3036 return _S;
3037}
3038
Howard Hinnant30a840f2010-05-12 17:08:57 +00003039// uniform_int_distribution
3040
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041template<class _IntType = int>
3042class uniform_int_distribution
3043{
3044public:
3045 // types
3046 typedef _IntType result_type;
3047
3048 class param_type
3049 {
3050 result_type __a_;
3051 result_type __b_;
3052 public:
3053 typedef uniform_int_distribution distribution_type;
3054
3055 explicit param_type(result_type __a = 0,
3056 result_type __b = numeric_limits<result_type>::max())
3057 : __a_(__a), __b_(__b) {}
3058
3059 result_type a() const {return __a_;}
3060 result_type b() const {return __b_;}
3061
3062 friend bool operator==(const param_type& __x, const param_type& __y)
3063 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3064 friend bool operator!=(const param_type& __x, const param_type& __y)
3065 {return !(__x == __y);}
3066 };
3067
3068private:
3069 param_type __p_;
3070
3071public:
3072 // constructors and reset functions
3073 explicit uniform_int_distribution(result_type __a = 0,
3074 result_type __b = numeric_limits<result_type>::max())
3075 : __p_(param_type(__a, __b)) {}
3076 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3077 void reset() {}
3078
3079 // generating functions
3080 template<class _URNG> result_type operator()(_URNG& __g)
3081 {return (*this)(__g, __p_);}
3082 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3083
3084 // property functions
3085 result_type a() const {return __p_.a();}
3086 result_type b() const {return __p_.b();}
3087
3088 param_type param() const {return __p_;}
3089 void param(const param_type& __p) {__p_ = __p;}
3090
3091 result_type min() const {return a();}
3092 result_type max() const {return b();}
3093
3094 friend bool operator==(const uniform_int_distribution& __x,
3095 const uniform_int_distribution& __y)
3096 {return __x.__p_ == __y.__p_;}
3097 friend bool operator!=(const uniform_int_distribution& __x,
3098 const uniform_int_distribution& __y)
3099 {return !(__x == __y);}
3100};
3101
3102template<class _IntType>
3103template<class _URNG>
3104typename uniform_int_distribution<_IntType>::result_type
3105uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3106{
3107 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3108 uint32_t, uint64_t>::type _UIntType;
3109 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
3110 if (_R == 1)
3111 return __p.a();
3112 const size_t _Dt = numeric_limits<_UIntType>::digits;
3113 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3114 if (_R == 0)
3115 return static_cast<result_type>(_Eng(__g, _Dt)());
3116 size_t __w = _Dt - __clz(_R) - 1;
3117 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
3118 ++__w;
3119 _Eng __e(__g, __w);
3120 _UIntType __u;
3121 do
3122 {
3123 __u = __e();
3124 } while (__u >= _R);
3125 return static_cast<result_type>(__u + __p.a());
3126}
3127
3128template <class _CharT, class _Traits, class _IT>
3129basic_ostream<_CharT, _Traits>&
3130operator<<(basic_ostream<_CharT, _Traits>& __os,
3131 const uniform_int_distribution<_IT>& __x)
3132{
3133 __save_flags<_CharT, _Traits> _(__os);
3134 __os.flags(ios_base::dec | ios_base::left);
3135 _CharT __sp = __os.widen(' ');
3136 __os.fill(__sp);
3137 return __os << __x.a() << __sp << __x.b();
3138}
3139
3140template <class _CharT, class _Traits, class _IT>
3141basic_istream<_CharT, _Traits>&
3142operator>>(basic_istream<_CharT, _Traits>& __is,
3143 uniform_int_distribution<_IT>& __x)
3144{
3145 typedef uniform_int_distribution<_IT> _Eng;
3146 typedef typename _Eng::result_type result_type;
3147 typedef typename _Eng::param_type param_type;
3148 __save_flags<_CharT, _Traits> _(__is);
3149 __is.flags(ios_base::dec | ios_base::skipws);
3150 result_type __a;
3151 result_type __b;
3152 __is >> __a >> __b;
3153 if (!__is.fail())
3154 __x.param(param_type(__a, __b));
3155 return __is;
3156}
3157
Howard Hinnant30a840f2010-05-12 17:08:57 +00003158// uniform_real_distribution
3159
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003160template<class _RealType = double>
3161class uniform_real_distribution
3162{
3163public:
3164 // types
3165 typedef _RealType result_type;
3166
3167 class param_type
3168 {
3169 result_type __a_;
3170 result_type __b_;
3171 public:
3172 typedef uniform_real_distribution distribution_type;
3173
3174 explicit param_type(result_type __a = 0,
3175 result_type __b = 1)
3176 : __a_(__a), __b_(__b) {}
3177
3178 result_type a() const {return __a_;}
3179 result_type b() const {return __b_;}
3180
3181 friend bool operator==(const param_type& __x, const param_type& __y)
3182 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3183 friend bool operator!=(const param_type& __x, const param_type& __y)
3184 {return !(__x == __y);}
3185 };
3186
3187private:
3188 param_type __p_;
3189
3190public:
3191 // constructors and reset functions
3192 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3193 : __p_(param_type(__a, __b)) {}
3194 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
3195 void reset() {}
3196
3197 // generating functions
3198 template<class _URNG> result_type operator()(_URNG& __g)
3199 {return (*this)(__g, __p_);}
3200 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3201
3202 // property functions
3203 result_type a() const {return __p_.a();}
3204 result_type b() const {return __p_.b();}
3205
3206 param_type param() const {return __p_;}
3207 void param(const param_type& __p) {__p_ = __p;}
3208
3209 result_type min() const {return a();}
3210 result_type max() const {return b();}
3211
3212 friend bool operator==(const uniform_real_distribution& __x,
3213 const uniform_real_distribution& __y)
3214 {return __x.__p_ == __y.__p_;}
3215 friend bool operator!=(const uniform_real_distribution& __x,
3216 const uniform_real_distribution& __y)
3217 {return !(__x == __y);}
3218};
3219
3220template<class _RealType>
3221template<class _URNG>
3222inline
3223typename uniform_real_distribution<_RealType>::result_type
3224uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3225{
3226 return (__p.b() - __p.a())
3227 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3228 + __p.a();
3229}
3230
3231template <class _CharT, class _Traits, class _RT>
3232basic_ostream<_CharT, _Traits>&
3233operator<<(basic_ostream<_CharT, _Traits>& __os,
3234 const uniform_real_distribution<_RT>& __x)
3235{
3236 __save_flags<_CharT, _Traits> _(__os);
3237 __os.flags(ios_base::dec | ios_base::left);
3238 _CharT __sp = __os.widen(' ');
3239 __os.fill(__sp);
3240 return __os << __x.a() << __sp << __x.b();
3241}
3242
3243template <class _CharT, class _Traits, class _RT>
3244basic_istream<_CharT, _Traits>&
3245operator>>(basic_istream<_CharT, _Traits>& __is,
3246 uniform_real_distribution<_RT>& __x)
3247{
3248 typedef uniform_real_distribution<_RT> _Eng;
3249 typedef typename _Eng::result_type result_type;
3250 typedef typename _Eng::param_type param_type;
3251 __save_flags<_CharT, _Traits> _(__is);
3252 __is.flags(ios_base::dec | ios_base::skipws);
3253 result_type __a;
3254 result_type __b;
3255 __is >> __a >> __b;
3256 if (!__is.fail())
3257 __x.param(param_type(__a, __b));
3258 return __is;
3259}
3260
Howard Hinnant30a840f2010-05-12 17:08:57 +00003261// bernoulli_distribution
3262
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263class bernoulli_distribution
3264{
3265public:
3266 // types
3267 typedef bool result_type;
3268
3269 class param_type
3270 {
3271 double __p_;
3272 public:
3273 typedef bernoulli_distribution distribution_type;
3274
3275 explicit param_type(double __p = 0.5) : __p_(__p) {}
3276
3277 double p() const {return __p_;}
3278
3279 friend bool operator==(const param_type& __x, const param_type& __y)
3280 {return __x.__p_ == __y.__p_;}
3281 friend bool operator!=(const param_type& __x, const param_type& __y)
3282 {return !(__x == __y);}
3283 };
3284
3285private:
3286 param_type __p_;
3287
3288public:
3289 // constructors and reset functions
3290 explicit bernoulli_distribution(double __p = 0.5)
3291 : __p_(param_type(__p)) {}
3292 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3293 void reset() {}
3294
3295 // generating functions
3296 template<class _URNG> result_type operator()(_URNG& __g)
3297 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003298 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003299
3300 // property functions
3301 double p() const {return __p_.p();}
3302
3303 param_type param() const {return __p_;}
3304 void param(const param_type& __p) {__p_ = __p;}
3305
3306 result_type min() const {return false;}
3307 result_type max() const {return true;}
3308
3309 friend bool operator==(const bernoulli_distribution& __x,
3310 const bernoulli_distribution& __y)
3311 {return __x.__p_ == __y.__p_;}
3312 friend bool operator!=(const bernoulli_distribution& __x,
3313 const bernoulli_distribution& __y)
3314 {return !(__x == __y);}
3315};
3316
Howard Hinnant03aad812010-05-11 23:26:59 +00003317template<class _URNG>
3318inline
3319bernoulli_distribution::result_type
3320bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3321{
3322 return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.);
3323}
3324
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003325template <class _CharT, class _Traits>
3326basic_ostream<_CharT, _Traits>&
3327operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3328{
3329 __save_flags<_CharT, _Traits> _(__os);
3330 __os.flags(ios_base::dec | ios_base::left);
3331 _CharT __sp = __os.widen(' ');
3332 __os.fill(__sp);
3333 return __os << __x.p();
3334}
3335
3336template <class _CharT, class _Traits>
3337basic_istream<_CharT, _Traits>&
3338operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3339{
3340 typedef bernoulli_distribution _Eng;
3341 typedef typename _Eng::param_type param_type;
3342 __save_flags<_CharT, _Traits> _(__is);
3343 __is.flags(ios_base::dec | ios_base::skipws);
3344 double __p;
3345 __is >> __p;
3346 if (!__is.fail())
3347 __x.param(param_type(__p));
3348 return __is;
3349}
3350
Howard Hinnant30a840f2010-05-12 17:08:57 +00003351// binomial_distribution
3352
Howard Hinnant03aad812010-05-11 23:26:59 +00003353template<class _IntType = int>
3354class binomial_distribution
3355{
3356public:
3357 // types
3358 typedef _IntType result_type;
3359
3360 class param_type
3361 {
3362 result_type __t_;
3363 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003364 double __pr_;
3365 double __odds_ratio_;
3366 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003367 public:
3368 typedef binomial_distribution distribution_type;
3369
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003370 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003371
3372 result_type t() const {return __t_;}
3373 double p() const {return __p_;}
3374
3375 friend bool operator==(const param_type& __x, const param_type& __y)
3376 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3377 friend bool operator!=(const param_type& __x, const param_type& __y)
3378 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003379
3380 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003381 };
3382
3383private:
3384 param_type __p_;
3385
3386public:
3387 // constructors and reset functions
3388 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3389 : __p_(param_type(__t, __p)) {}
3390 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3391 void reset() {}
3392
3393 // generating functions
3394 template<class _URNG> result_type operator()(_URNG& __g)
3395 {return (*this)(__g, __p_);}
3396 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3397
3398 // property functions
3399 result_type t() const {return __p_.t();}
3400 double p() const {return __p_.p();}
3401
3402 param_type param() const {return __p_;}
3403 void param(const param_type& __p) {__p_ = __p;}
3404
3405 result_type min() const {return 0;}
3406 result_type max() const {return t();}
3407
3408 friend bool operator==(const binomial_distribution& __x,
3409 const binomial_distribution& __y)
3410 {return __x.__p_ == __y.__p_;}
3411 friend bool operator!=(const binomial_distribution& __x,
3412 const binomial_distribution& __y)
3413 {return !(__x == __y);}
3414};
3415
3416template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003417binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3418 : __t_(__t), __p_(__p)
3419{
3420 if (0 < __p_ && __p_ < 1)
3421 {
3422 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3423 __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) -
3424 _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) +
3425 (__t_ - __r0_) * _STD::log(1 - __p_));
3426 __odds_ratio_ = __p_ / (1 - __p_);
3427 }
3428}
3429
3430template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003431template<class _URNG>
3432_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003433binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003434{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003435 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3436 return 0;
3437 if (__pr.__p_ == 1)
3438 return __pr.__t_;
3439 uniform_real_distribution<double> __gen;
3440 double __u = __gen(__g) - __pr.__pr_;
3441 if (__u < 0)
3442 return __pr.__r0_;
3443 double __pu = __pr.__pr_;
3444 double __pd = __pu;
3445 result_type __ru = __pr.__r0_;
3446 result_type __rd = __ru;
3447 while (true)
3448 {
3449 if (__rd >= 1)
3450 {
3451 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3452 __u -= __pd;
3453 if (__u < 0)
3454 return __rd - 1;
3455 }
3456 --__rd;
3457 ++__ru;
3458 if (__ru <= __pr.__t_)
3459 {
3460 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3461 __u -= __pu;
3462 if (__u < 0)
3463 return __ru;
3464 }
3465 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003466}
3467
3468template <class _CharT, class _Traits, class _IntType>
3469basic_ostream<_CharT, _Traits>&
3470operator<<(basic_ostream<_CharT, _Traits>& __os,
3471 const binomial_distribution<_IntType>& __x)
3472{
3473 __save_flags<_CharT, _Traits> _(__os);
3474 __os.flags(ios_base::dec | ios_base::left);
3475 _CharT __sp = __os.widen(' ');
3476 __os.fill(__sp);
3477 return __os << __x.t() << __sp << __x.p();
3478}
3479
3480template <class _CharT, class _Traits, class _IntType>
3481basic_istream<_CharT, _Traits>&
3482operator>>(basic_istream<_CharT, _Traits>& __is,
3483 binomial_distribution<_IntType>& __x)
3484{
3485 typedef binomial_distribution<_IntType> _Eng;
3486 typedef typename _Eng::result_type result_type;
3487 typedef typename _Eng::param_type param_type;
3488 __save_flags<_CharT, _Traits> _(__is);
3489 __is.flags(ios_base::dec | ios_base::skipws);
3490 result_type __t;
3491 double __p;
3492 __is >> __t >> __p;
3493 if (!__is.fail())
3494 __x.param(param_type(__t, __p));
3495 return __is;
3496}
3497
Howard Hinnant30a840f2010-05-12 17:08:57 +00003498// exponential_distribution
3499
3500template<class _RealType = double>
3501class exponential_distribution
3502{
3503public:
3504 // types
3505 typedef _RealType result_type;
3506
3507 class param_type
3508 {
3509 result_type __lambda_;
3510 public:
3511 typedef exponential_distribution distribution_type;
3512
3513 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3514
3515 result_type lambda() const {return __lambda_;}
3516
3517 friend bool operator==(const param_type& __x, const param_type& __y)
3518 {return __x.__lambda_ == __y.__lambda_;}
3519 friend bool operator!=(const param_type& __x, const param_type& __y)
3520 {return !(__x == __y);}
3521 };
3522
3523private:
3524 param_type __p_;
3525
3526public:
3527 // constructors and reset functions
3528 explicit exponential_distribution(result_type __lambda = 1)
3529 : __p_(param_type(__lambda)) {}
3530 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3531 void reset() {}
3532
3533 // generating functions
3534 template<class _URNG> result_type operator()(_URNG& __g)
3535 {return (*this)(__g, __p_);}
3536 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3537
3538 // property functions
3539 result_type lambda() const {return __p_.lambda();}
3540
3541 param_type param() const {return __p_;}
3542 void param(const param_type& __p) {__p_ = __p;}
3543
3544 result_type min() const {return 0;}
Howard Hinnantdf40dc62010-05-16 17:56:20 +00003545 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00003546
3547 friend bool operator==(const exponential_distribution& __x,
3548 const exponential_distribution& __y)
3549 {return __x.__p_ == __y.__p_;}
3550 friend bool operator!=(const exponential_distribution& __x,
3551 const exponential_distribution& __y)
3552 {return !(__x == __y);}
3553};
3554
3555template <class _RealType>
3556template<class _URNG>
3557_RealType
3558exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3559{
3560 return -_STD::log
3561 (
3562 result_type(1) -
3563 _STD::generate_canonical<result_type,
3564 numeric_limits<result_type>::digits>(__g)
3565 )
3566 / __p.lambda();
3567}
3568
3569template <class _CharT, class _Traits, class _RealType>
3570basic_ostream<_CharT, _Traits>&
3571operator<<(basic_ostream<_CharT, _Traits>& __os,
3572 const exponential_distribution<_RealType>& __x)
3573{
3574 __save_flags<_CharT, _Traits> _(__os);
3575 __os.flags(ios_base::dec | ios_base::left);
3576 return __os << __x.lambda();
3577}
3578
3579template <class _CharT, class _Traits, class _RealType>
3580basic_istream<_CharT, _Traits>&
3581operator>>(basic_istream<_CharT, _Traits>& __is,
3582 exponential_distribution<_RealType>& __x)
3583{
3584 typedef exponential_distribution<_RealType> _Eng;
3585 typedef typename _Eng::result_type result_type;
3586 typedef typename _Eng::param_type param_type;
3587 __save_flags<_CharT, _Traits> _(__is);
3588 __is.flags(ios_base::dec | ios_base::skipws);
3589 result_type __lambda;
3590 __is >> __lambda;
3591 if (!__is.fail())
3592 __x.param(param_type(__lambda));
3593 return __is;
3594}
3595
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003596// normal_distribution
3597
3598template<class _RealType = double>
3599class normal_distribution
3600{
3601public:
3602 // types
3603 typedef _RealType result_type;
3604
3605 class param_type
3606 {
3607 result_type __mean_;
3608 result_type __stddev_;
3609 public:
3610 typedef normal_distribution distribution_type;
3611
3612 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
3613 : __mean_(__mean), __stddev_(__stddev) {}
3614
3615 result_type mean() const {return __mean_;}
3616 result_type stddev() const {return __stddev_;}
3617
3618 friend bool operator==(const param_type& __x, const param_type& __y)
3619 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
3620 friend bool operator!=(const param_type& __x, const param_type& __y)
3621 {return !(__x == __y);}
3622 };
3623
3624private:
3625 param_type __p_;
3626 result_type _V_;
3627 bool _V_hot_;
3628
3629public:
3630 // constructors and reset functions
3631 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
3632 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
3633 explicit normal_distribution(const param_type& __p)
3634 : __p_(__p), _V_hot_(false) {}
3635 void reset() {_V_hot_ = false;}
3636
3637 // generating functions
3638 template<class _URNG> result_type operator()(_URNG& __g)
3639 {return (*this)(__g, __p_);}
3640 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3641
3642 // property functions
3643 result_type mean() const {return __p_.mean();}
3644 result_type stddev() const {return __p_.stddev();}
3645
3646 param_type param() const {return __p_;}
3647 void param(const param_type& __p) {__p_ = __p;}
3648
3649 result_type min() const {return -numeric_limits<result_type>::infinity();}
3650 result_type max() const {return numeric_limits<result_type>::infinity();}
3651
3652 friend bool operator==(const normal_distribution& __x,
3653 const normal_distribution& __y)
3654 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
3655 (!__x._V_hot_ || __x._V_ == __y._V_);}
3656 friend bool operator!=(const normal_distribution& __x,
3657 const normal_distribution& __y)
3658 {return !(__x == __y);}
3659
3660 template <class _CharT, class _Traits, class _RT>
3661 friend
3662 basic_ostream<_CharT, _Traits>&
3663 operator<<(basic_ostream<_CharT, _Traits>& __os,
3664 const normal_distribution<_RT>& __x);
3665
3666 template <class _CharT, class _Traits, class _RT>
3667 friend
3668 basic_istream<_CharT, _Traits>&
3669 operator>>(basic_istream<_CharT, _Traits>& __is,
3670 normal_distribution<_RT>& __x);
3671};
3672
3673template <class _RealType>
3674template<class _URNG>
3675_RealType
3676normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3677{
3678 result_type _U;
3679 if (_V_hot_)
3680 {
3681 _V_hot_ = false;
3682 _U = _V_;
3683 }
3684 else
3685 {
3686 uniform_real_distribution<result_type> _Uni(-1, 1);
3687 result_type __u;
3688 result_type __v;
3689 result_type __s;
3690 do
3691 {
3692 __u = _Uni(__g);
3693 __v = _Uni(__g);
3694 __s = __u * __u + __v * __v;
3695 } while (__s > 1 || __s == 0);
3696 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
3697 _V_ = __v * _F;
3698 _V_hot_ = true;
3699 _U = __u * _F;
3700 }
3701 return _U * __p.stddev() + __p.mean();
3702}
3703
3704template <class _CharT, class _Traits, class _RT>
3705basic_ostream<_CharT, _Traits>&
3706operator<<(basic_ostream<_CharT, _Traits>& __os,
3707 const normal_distribution<_RT>& __x)
3708{
3709 __save_flags<_CharT, _Traits> _(__os);
3710 __os.flags(ios_base::dec | ios_base::left);
3711 _CharT __sp = __os.widen(' ');
3712 __os.fill(__sp);
3713 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
3714 if (__x._V_hot_)
3715 __os << __sp << __x._V_;
3716 return __os;
3717}
3718
3719template <class _CharT, class _Traits, class _RT>
3720basic_istream<_CharT, _Traits>&
3721operator>>(basic_istream<_CharT, _Traits>& __is,
3722 normal_distribution<_RT>& __x)
3723{
3724 typedef normal_distribution<_RT> _Eng;
3725 typedef typename _Eng::result_type result_type;
3726 typedef typename _Eng::param_type param_type;
3727 __save_flags<_CharT, _Traits> _(__is);
3728 __is.flags(ios_base::dec | ios_base::skipws);
3729 result_type __mean;
3730 result_type __stddev;
3731 result_type _V = 0;
3732 bool _V_hot = false;
3733 __is >> __mean >> __stddev >> _V_hot;
3734 if (_V_hot)
3735 __is >> _V;
3736 if (!__is.fail())
3737 {
3738 __x.param(param_type(__mean, __stddev));
3739 __x._V_hot_ = _V_hot;
3740 __x._V_ = _V;
3741 }
3742 return __is;
3743}
3744
3745// poisson_distribution
3746
3747template<class _IntType = int>
3748class poisson_distribution
3749{
3750public:
3751 // types
3752 typedef _IntType result_type;
3753
3754 class param_type
3755 {
3756 double __mean_;
3757 double __s_;
3758 double __d_;
3759 double __l_;
3760 double __omega_;
3761 double __c0_;
3762 double __c1_;
3763 double __c2_;
3764 double __c3_;
3765 double __c_;
3766
3767 public:
3768 typedef poisson_distribution distribution_type;
3769
3770 explicit param_type(double __mean = 1.0);
3771
3772 double mean() const {return __mean_;}
3773
3774 friend bool operator==(const param_type& __x, const param_type& __y)
3775 {return __x.__mean_ == __y.__mean_;}
3776 friend bool operator!=(const param_type& __x, const param_type& __y)
3777 {return !(__x == __y);}
3778
3779 friend class poisson_distribution;
3780 };
3781
3782private:
3783 param_type __p_;
3784
3785public:
3786 // constructors and reset functions
3787 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
3788 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
3789 void reset() {}
3790
3791 // generating functions
3792 template<class _URNG> result_type operator()(_URNG& __g)
3793 {return (*this)(__g, __p_);}
3794 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3795
3796 // property functions
3797 double mean() const {return __p_.mean();}
3798
3799 param_type param() const {return __p_;}
3800 void param(const param_type& __p) {__p_ = __p;}
3801
3802 result_type min() const {return 0;}
3803 result_type max() const {return numeric_limits<result_type>::max();}
3804
3805 friend bool operator==(const poisson_distribution& __x,
3806 const poisson_distribution& __y)
3807 {return __x.__p_ == __y.__p_;}
3808 friend bool operator!=(const poisson_distribution& __x,
3809 const poisson_distribution& __y)
3810 {return !(__x == __y);}
3811};
3812
3813template<class _IntType>
3814poisson_distribution<_IntType>::param_type::param_type(double __mean)
3815 : __mean_(__mean)
3816{
3817 if (__mean_ < 10)
3818 {
3819 __s_ = 0;
3820 __d_ = 0;
3821 __l_ = _STD::exp(-__mean_);
3822 __omega_ = 0;
3823 __c3_ = 0;
3824 __c2_ = 0;
3825 __c1_ = 0;
3826 __c0_ = 0;
3827 __c_ = 0;
3828 }
3829 else
3830 {
3831 __s_ = _STD::sqrt(__mean_);
3832 __d_ = 6 * __mean_ * __mean_;
3833 __l_ = static_cast<result_type>(__mean_ - 1.1484);
3834 __omega_ = .3989423 / __s_;
3835 double __b1_ = .4166667E-1 / __mean_;
3836 double __b2_ = .3 * __b1_ * __b1_;
3837 __c3_ = .1428571 * __b1_ * __b2_;
3838 __c2_ = __b2_ - 15. * __c3_;
3839 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
3840 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
3841 __c_ = .1069 / __mean_;
3842 }
3843}
3844
3845template <class _IntType>
3846template<class _URNG>
3847_IntType
3848poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
3849{
3850 result_type __x;
3851 uniform_real_distribution<double> __urd;
3852 if (__pr.__mean_ <= 10)
3853 {
3854 __x = 0;
3855 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
3856 __p *= __urd(__urng);
3857 }
3858 else
3859 {
3860 double __difmuk;
3861 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
3862 double __u;
3863 if (__g > 0)
3864 {
3865 __x = static_cast<result_type>(__g);
3866 if (__x >= __pr.__l_)
3867 return __x;
3868 __difmuk = __pr.__mean_ - __x;
3869 __u = __urd(__urng);
3870 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
3871 return __x;
3872 }
3873 exponential_distribution<double> __edist;
3874 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
3875 {
3876 double __e;
3877 if (__using_exp_dist || __g < 0)
3878 {
3879 double __t;
3880 do
3881 {
3882 __e = __edist(__urng);
3883 __u = __urd(__urng);
3884 __u += __u - 1;
3885 __t = 1.8 + (__u < 0 ? -__e : __e);
3886 } while (__t <= -.6744);
3887 __x = __pr.__mean_ + __pr.__s_ * __t;
3888 __difmuk = __pr.__mean_ - __x;
3889 __using_exp_dist = true;
3890 }
3891 double __px;
3892 double __py;
3893 if (__x < 10)
3894 {
3895 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
3896 40320, 362880};
3897 __px = -__pr.__mean_;
3898 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x];
3899 }
3900 else
3901 {
3902 double __del = .8333333E-1 / __x;
3903 __del -= 4.8 * __del * __del * __del;
3904 double __v = __difmuk / __x;
3905 if (_STD::abs(__v) > 0.25)
3906 __px = __x * _STD::log(1 + __v) - __difmuk - __del;
3907 else
3908 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
3909 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
3910 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
3911 __py = .3989423 / _STD::sqrt(__x);
3912 }
3913 double __r = (0.5 - __difmuk) / __pr.__s_;
3914 double __r2 = __r * __r;
3915 double __fx = -0.5 * __r2;
3916 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
3917 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
3918 if (__using_exp_dist)
3919 {
3920 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) -
3921 __fy * _STD::exp(__fx + __e))
3922 break;
3923 }
3924 else
3925 {
3926 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx))
3927 break;
3928 }
3929 }
3930 }
3931 return __x;
3932}
3933
3934template <class _CharT, class _Traits, class _IntType>
3935basic_ostream<_CharT, _Traits>&
3936operator<<(basic_ostream<_CharT, _Traits>& __os,
3937 const poisson_distribution<_IntType>& __x)
3938{
3939 __save_flags<_CharT, _Traits> _(__os);
3940 __os.flags(ios_base::dec | ios_base::left);
3941 return __os << __x.mean();
3942}
3943
3944template <class _CharT, class _Traits, class _IntType>
3945basic_istream<_CharT, _Traits>&
3946operator>>(basic_istream<_CharT, _Traits>& __is,
3947 poisson_distribution<_IntType>& __x)
3948{
3949 typedef poisson_distribution<_IntType> _Eng;
3950 typedef typename _Eng::param_type param_type;
3951 __save_flags<_CharT, _Traits> _(__is);
3952 __is.flags(ios_base::dec | ios_base::skipws);
3953 double __mean;
3954 __is >> __mean;
3955 if (!__is.fail())
3956 __x.param(param_type(__mean));
3957 return __is;
3958}
3959
Howard Hinnant9de6e302010-05-16 01:09:02 +00003960// weibull_distribution
3961
3962template<class _RealType = double>
3963class weibull_distribution
3964{
3965public:
3966 // types
3967 typedef _RealType result_type;
3968
3969 class param_type
3970 {
3971 result_type __a_;
3972 result_type __b_;
3973 public:
3974 typedef weibull_distribution distribution_type;
3975
3976 explicit param_type(result_type __a = 1, result_type __b = 1)
3977 : __a_(__a), __b_(__b) {}
3978
3979 result_type a() const {return __a_;}
3980 result_type b() const {return __b_;}
3981
3982 friend bool operator==(const param_type& __x, const param_type& __y)
3983 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3984 friend bool operator!=(const param_type& __x, const param_type& __y)
3985 {return !(__x == __y);}
3986 };
3987
3988private:
3989 param_type __p_;
3990
3991public:
3992 // constructor and reset functions
3993 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
3994 : __p_(param_type(__a, __b)) {}
3995 explicit weibull_distribution(const param_type& __p)
3996 : __p_(__p) {}
3997 void reset() {}
3998
3999 // generating functions
4000 template<class _URNG> result_type operator()(_URNG& __g)
4001 {return (*this)(__g, __p_);}
4002 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4003 {return __p.b() *
4004 _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
4005
4006 // property functions
4007 result_type a() const {return __p_.a();}
4008 result_type b() const {return __p_.b();}
4009
4010 param_type param() const {return __p_;}
4011 void param(const param_type& __p) {__p_ = __p;}
4012
4013 result_type min() const {return 0;}
4014 result_type max() const {return numeric_limits<result_type>::infinity();}
4015
4016
4017 friend bool operator==(const weibull_distribution& __x,
4018 const weibull_distribution& __y)
4019 {return __x.__p_ == __y.__p_;}
4020 friend bool operator!=(const weibull_distribution& __x,
4021 const weibull_distribution& __y)
4022 {return !(__x == __y);}
4023};
4024
4025template <class _CharT, class _Traits, class _RT>
4026basic_ostream<_CharT, _Traits>&
4027operator<<(basic_ostream<_CharT, _Traits>& __os,
4028 const weibull_distribution<_RT>& __x)
4029{
4030 __save_flags<_CharT, _Traits> _(__os);
4031 __os.flags(ios_base::dec | ios_base::left);
4032 _CharT __sp = __os.widen(' ');
4033 __os.fill(__sp);
4034 __os << __x.a() << __sp << __x.b();
4035 return __os;
4036}
4037
4038template <class _CharT, class _Traits, class _RT>
4039basic_istream<_CharT, _Traits>&
4040operator>>(basic_istream<_CharT, _Traits>& __is,
4041 weibull_distribution<_RT>& __x)
4042{
4043 typedef weibull_distribution<_RT> _Eng;
4044 typedef typename _Eng::result_type result_type;
4045 typedef typename _Eng::param_type param_type;
4046 __save_flags<_CharT, _Traits> _(__is);
4047 __is.flags(ios_base::dec | ios_base::skipws);
4048 result_type __a;
4049 result_type __b;
4050 __is >> __a >> __b;
4051 if (!__is.fail())
4052 __x.param(param_type(__a, __b));
4053 return __is;
4054}
4055
Howard Hinnantc7c49132010-05-13 17:58:28 +00004056// gamma_distribution
4057
4058template<class _RealType = double>
4059class gamma_distribution
4060{
4061public:
4062 // types
4063 typedef _RealType result_type;
4064
4065 class param_type
4066 {
4067 result_type __alpha_;
4068 result_type __beta_;
4069 public:
4070 typedef gamma_distribution distribution_type;
4071
4072 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4073 : __alpha_(__alpha), __beta_(__beta) {}
4074
4075 result_type alpha() const {return __alpha_;}
4076 result_type beta() const {return __beta_;}
4077
4078 friend bool operator==(const param_type& __x, const param_type& __y)
4079 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
4080 friend bool operator!=(const param_type& __x, const param_type& __y)
4081 {return !(__x == __y);}
4082 };
4083
4084private:
4085 param_type __p_;
4086
4087public:
4088 // constructors and reset functions
4089 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4090 : __p_(param_type(__alpha, __beta)) {}
4091 explicit gamma_distribution(const param_type& __p)
4092 : __p_(__p) {}
4093 void reset() {}
4094
4095 // generating functions
4096 template<class _URNG> result_type operator()(_URNG& __g)
4097 {return (*this)(__g, __p_);}
4098 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4099
4100 // property functions
4101 result_type alpha() const {return __p_.alpha();}
4102 result_type beta() const {return __p_.beta();}
4103
4104 param_type param() const {return __p_;}
4105 void param(const param_type& __p) {__p_ = __p;}
4106
4107 result_type min() const {return 0;}
4108 result_type max() const {return numeric_limits<result_type>::infinity();}
4109
4110 friend bool operator==(const gamma_distribution& __x,
4111 const gamma_distribution& __y)
4112 {return __x.__p_ == __y.__p_;}
4113 friend bool operator!=(const gamma_distribution& __x,
4114 const gamma_distribution& __y)
4115 {return !(__x == __y);}
4116};
4117
4118template <class _RealType>
4119template<class _URNG>
4120_RealType
4121gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4122{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004123 result_type __a = __p.alpha();
4124 uniform_real_distribution<result_type> __gen(0, 1);
4125 exponential_distribution<result_type> __egen;
4126 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004127 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004128 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004129 else if (__a > 1)
4130 {
4131 const result_type __b = __a - 1;
4132 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004133 while (true)
4134 {
4135 const result_type __u = __gen(__g);
4136 const result_type __v = __gen(__g);
4137 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004138 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004139 {
4140 const result_type __y = _STD::sqrt(__c / __w) *
4141 (__u - result_type(0.5));
4142 __x = __b + __y;
4143 if (__x >= 0)
4144 {
4145 const result_type __z = 64 * __w * __w * __w * __v * __v;
4146 if (__z <= 1 - 2 * __y * __y / __x)
4147 break;
4148 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
4149 break;
4150 }
4151 }
4152 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004153 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004154 else // __a < 1
4155 {
4156 while (true)
4157 {
4158 const result_type __u = __gen(__g);
4159 const result_type __es = __egen(__g);
4160 if (__u <= 1 - __a)
4161 {
4162 __x = _STD::pow(__u, 1 / __a);
4163 if (__x <= __es)
4164 break;
4165 }
4166 else
4167 {
4168 const result_type __e = -_STD::log((1-__u)/__a);
4169 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
4170 if (__x <= __e + __es)
4171 break;
4172 }
4173 }
4174 }
4175 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004176}
4177
4178template <class _CharT, class _Traits, class _RT>
4179basic_ostream<_CharT, _Traits>&
4180operator<<(basic_ostream<_CharT, _Traits>& __os,
4181 const gamma_distribution<_RT>& __x)
4182{
4183 __save_flags<_CharT, _Traits> _(__os);
4184 __os.flags(ios_base::dec | ios_base::left);
4185 _CharT __sp = __os.widen(' ');
4186 __os.fill(__sp);
4187 __os << __x.alpha() << __sp << __x.beta();
4188 return __os;
4189}
4190
4191template <class _CharT, class _Traits, class _RT>
4192basic_istream<_CharT, _Traits>&
4193operator>>(basic_istream<_CharT, _Traits>& __is,
4194 gamma_distribution<_RT>& __x)
4195{
4196 typedef gamma_distribution<_RT> _Eng;
4197 typedef typename _Eng::result_type result_type;
4198 typedef typename _Eng::param_type param_type;
4199 __save_flags<_CharT, _Traits> _(__is);
4200 __is.flags(ios_base::dec | ios_base::skipws);
4201 result_type __alpha;
4202 result_type __beta;
4203 __is >> __alpha >> __beta;
4204 if (!__is.fail())
4205 __x.param(param_type(__alpha, __beta));
4206 return __is;
4207}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004208
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004209// negative_binomial_distribution
4210
4211template<class _IntType = int>
4212class negative_binomial_distribution
4213{
4214public:
4215 // types
4216 typedef _IntType result_type;
4217
4218 class param_type
4219 {
4220 result_type __k_;
4221 double __p_;
4222 public:
4223 typedef negative_binomial_distribution distribution_type;
4224
4225 explicit param_type(result_type __k = 1, double __p = 0.5)
4226 : __k_(__k), __p_(__p) {}
4227
4228 result_type k() const {return __k_;}
4229 double p() const {return __p_;}
4230
4231 friend bool operator==(const param_type& __x, const param_type& __y)
4232 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
4233 friend bool operator!=(const param_type& __x, const param_type& __y)
4234 {return !(__x == __y);}
4235 };
4236
4237private:
4238 param_type __p_;
4239
4240public:
4241 // constructor and reset functions
4242 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
4243 : __p_(__k, __p) {}
4244 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
4245 void reset() {}
4246
4247 // generating functions
4248 template<class _URNG> result_type operator()(_URNG& __g)
4249 {return (*this)(__g, __p_);}
4250 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4251
4252 // property functions
4253 result_type k() const {return __p_.k();}
4254 double p() const {return __p_.p();}
4255
4256 param_type param() const {return __p_;}
4257 void param(const param_type& __p) {__p_ = __p;}
4258
4259 result_type min() const {return 0;}
4260 result_type max() const {return numeric_limits<result_type>::max();}
4261
4262 friend bool operator==(const negative_binomial_distribution& __x,
4263 const negative_binomial_distribution& __y)
4264 {return __x.__p_ == __y.__p_;}
4265 friend bool operator!=(const negative_binomial_distribution& __x,
4266 const negative_binomial_distribution& __y)
4267 {return !(__x == __y);}
4268};
4269
4270template <class _IntType>
4271template<class _URNG>
4272_IntType
4273negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4274{
4275 result_type __k = __pr.k();
4276 double __p = __pr.p();
4277 if (__k <= 21 * __p)
4278 {
4279 bernoulli_distribution __gen(__p);
4280 result_type __f = 0;
4281 result_type __s = 0;
4282 while (__s < __k)
4283 {
4284 if (__gen(__urng))
4285 ++__s;
4286 else
4287 ++__f;
4288 }
4289 return __f;
4290 }
4291 return poisson_distribution<result_type>(gamma_distribution<double>
4292 (__k, (1-__p)/__p)(__urng))(__urng);
4293}
4294
4295template <class _CharT, class _Traits, class _IntType>
4296basic_ostream<_CharT, _Traits>&
4297operator<<(basic_ostream<_CharT, _Traits>& __os,
4298 const negative_binomial_distribution<_IntType>& __x)
4299{
4300 __save_flags<_CharT, _Traits> _(__os);
4301 __os.flags(ios_base::dec | ios_base::left);
4302 _CharT __sp = __os.widen(' ');
4303 __os.fill(__sp);
4304 return __os << __x.k() << __sp << __x.p();
4305}
4306
4307template <class _CharT, class _Traits, class _IntType>
4308basic_istream<_CharT, _Traits>&
4309operator>>(basic_istream<_CharT, _Traits>& __is,
4310 negative_binomial_distribution<_IntType>& __x)
4311{
4312 typedef negative_binomial_distribution<_IntType> _Eng;
4313 typedef typename _Eng::result_type result_type;
4314 typedef typename _Eng::param_type param_type;
4315 __save_flags<_CharT, _Traits> _(__is);
4316 __is.flags(ios_base::dec | ios_base::skipws);
4317 result_type __k;
4318 double __p;
4319 __is >> __k >> __p;
4320 if (!__is.fail())
4321 __x.param(param_type(__k, __p));
4322 return __is;
4323}
4324
Howard Hinnant34e8a572010-05-17 13:44:27 +00004325// geometric_distribution
4326
4327template<class _IntType = int>
4328class geometric_distribution
4329{
4330public:
4331 // types
4332 typedef _IntType result_type;
4333
4334 class param_type
4335 {
4336 double __p_;
4337 public:
4338 typedef geometric_distribution distribution_type;
4339
4340 explicit param_type(double __p = 0.5) : __p_(__p) {}
4341
4342 double p() const {return __p_;}
4343
4344 friend bool operator==(const param_type& __x, const param_type& __y)
4345 {return __x.__p_ == __y.__p_;}
4346 friend bool operator!=(const param_type& __x, const param_type& __y)
4347 {return !(__x == __y);}
4348 };
4349
4350private:
4351 param_type __p_;
4352
4353public:
4354 // constructors and reset functions
4355 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
4356 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
4357 void reset() {}
4358
4359 // generating functions
4360 template<class _URNG> result_type operator()(_URNG& __g)
4361 {return (*this)(__g, __p_);}
4362 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4363 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
4364
4365 // property functions
4366 double p() const {return __p_.p();}
4367
4368 param_type param() const {return __p_;}
4369 void param(const param_type& __p) {__p_ = __p;}
4370
4371 result_type min() const {return 0;}
4372 result_type max() const {return numeric_limits<result_type>::max();}
4373
4374 friend bool operator==(const geometric_distribution& __x,
4375 const geometric_distribution& __y)
4376 {return __x.__p_ == __y.__p_;}
4377 friend bool operator!=(const geometric_distribution& __x,
4378 const geometric_distribution& __y)
4379 {return !(__x == __y);}
4380};
4381
4382template <class _CharT, class _Traits, class _IntType>
4383basic_ostream<_CharT, _Traits>&
4384operator<<(basic_ostream<_CharT, _Traits>& __os,
4385 const geometric_distribution<_IntType>& __x)
4386{
4387 __save_flags<_CharT, _Traits> _(__os);
4388 __os.flags(ios_base::dec | ios_base::left);
4389 return __os << __x.p();
4390}
4391
4392template <class _CharT, class _Traits, class _IntType>
4393basic_istream<_CharT, _Traits>&
4394operator>>(basic_istream<_CharT, _Traits>& __is,
4395 geometric_distribution<_IntType>& __x)
4396{
4397 typedef geometric_distribution<_IntType> _Eng;
4398 typedef typename _Eng::param_type param_type;
4399 __save_flags<_CharT, _Traits> _(__is);
4400 __is.flags(ios_base::dec | ios_base::skipws);
4401 double __p;
4402 __is >> __p;
4403 if (!__is.fail())
4404 __x.param(param_type(__p));
4405 return __is;
4406}
4407
Howard Hinnant97dc2f32010-05-15 23:36:00 +00004408// chi_squared_distribution
4409
4410template<class _RealType = double>
4411class chi_squared_distribution
4412{
4413public:
4414 // types
4415 typedef _RealType result_type;
4416
4417 class param_type
4418 {
4419 result_type __n_;
4420 public:
4421 typedef chi_squared_distribution distribution_type;
4422
4423 explicit param_type(result_type __n = 1) : __n_(__n) {}
4424
4425 result_type n() const {return __n_;}
4426
4427 friend bool operator==(const param_type& __x, const param_type& __y)
4428 {return __x.__n_ == __y.__n_;}
4429 friend bool operator!=(const param_type& __x, const param_type& __y)
4430 {return !(__x == __y);}
4431 };
4432
4433private:
4434 param_type __p_;
4435
4436public:
4437 // constructor and reset functions
4438 explicit chi_squared_distribution(result_type __n = 1)
4439 : __p_(param_type(__n)) {}
4440 explicit chi_squared_distribution(const param_type& __p)
4441 : __p_(__p) {}
4442 void reset() {}
4443
4444 // generating functions
4445 template<class _URNG> result_type operator()(_URNG& __g)
4446 {return (*this)(__g, __p_);}
4447 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4448 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
4449
4450 // property functions
4451 result_type n() const {return __p_.n();}
4452
4453 param_type param() const {return __p_;}
4454 void param(const param_type& __p) {__p_ = __p;}
4455
4456 result_type min() const {return 0;}
4457 result_type max() const {return numeric_limits<result_type>::infinity();}
4458
4459
4460 friend bool operator==(const chi_squared_distribution& __x,
4461 const chi_squared_distribution& __y)
4462 {return __x.__p_ == __y.__p_;}
4463 friend bool operator!=(const chi_squared_distribution& __x,
4464 const chi_squared_distribution& __y)
4465 {return !(__x == __y);}
4466};
4467
4468template <class _CharT, class _Traits, class _RT>
4469basic_ostream<_CharT, _Traits>&
4470operator<<(basic_ostream<_CharT, _Traits>& __os,
4471 const chi_squared_distribution<_RT>& __x)
4472{
4473 __save_flags<_CharT, _Traits> _(__os);
4474 __os.flags(ios_base::dec | ios_base::left);
4475 __os << __x.n();
4476 return __os;
4477}
4478
4479template <class _CharT, class _Traits, class _RT>
4480basic_istream<_CharT, _Traits>&
4481operator>>(basic_istream<_CharT, _Traits>& __is,
4482 chi_squared_distribution<_RT>& __x)
4483{
4484 typedef chi_squared_distribution<_RT> _Eng;
4485 typedef typename _Eng::result_type result_type;
4486 typedef typename _Eng::param_type param_type;
4487 __save_flags<_CharT, _Traits> _(__is);
4488 __is.flags(ios_base::dec | ios_base::skipws);
4489 result_type __n;
4490 __is >> __n;
4491 if (!__is.fail())
4492 __x.param(param_type(__n));
4493 return __is;
4494}
4495
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004496_LIBCPP_END_NAMESPACE_STD
4497
4498#endif // _LIBCPP_RANDOM