blob: 3298cd34f0ac77dc4fd34418cbef8af34b94acda [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_RANDOM
12#define _LIBCPP_RANDOM
13
14/*
15 random synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22// Engines
23
24template <class UIntType, UIntType a, UIntType c, UIntType m>
25class linear_congruential_engine
26{
27public:
28 // types
29 typedef UIntType result_type;
30
31 // engine characteristics
32 static constexpr result_type multiplier = a;
33 static constexpr result_type increment = c;
34 static constexpr result_type modulus = m;
35 static constexpr result_type min() { return c == 0u ? 1u: 0u;}
36 static constexpr result_type max() { return m - 1u;}
37 static constexpr result_type default_seed = 1u;
38
39 // constructors and seeding functions
40 explicit linear_congruential_engine(result_type s = default_seed);
41 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
42 void seed(result_type s = default_seed);
43 template<class Sseq> void seed(Sseq& q);
44
45 // generating functions
46 result_type operator()();
47 void discard(unsigned long long z);
48};
49
50template <class UIntType, UIntType a, UIntType c, UIntType m>
51bool
52operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
53 const linear_congruential_engine<UIntType, a, c, m>& y);
54
55template <class UIntType, UIntType a, UIntType c, UIntType m>
56bool
57operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
58 const linear_congruential_engine<UIntType, a, c, m>& y);
59
60template <class charT, class traits,
61 class UIntType, UIntType a, UIntType c, UIntType m>
62basic_ostream<charT, traits>&
63operator<<(basic_ostream<charT, traits>& os,
64 const linear_congruential_engine<UIntType, a, c, m>& x);
65
66template <class charT, class traits,
67 class UIntType, UIntType a, UIntType c, UIntType m>
68basic_istream<charT, traits>&
69operator>>(basic_istream<charT, traits>& is,
70 linear_congruential_engine<UIntType, a, c, m>& x);
71
72template <class UIntType, size_t w, size_t n, size_t m, size_t r,
73 UIntType a, size_t u, UIntType d, size_t s,
74 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
75class mersenne_twister_engine
76{
77public:
78 // types
79 typedef UIntType result_type;
80
81 // engine characteristics
82 static constexpr size_t word_size = w;
83 static constexpr size_t state_size = n;
84 static constexpr size_t shift_size = m;
85 static constexpr size_t mask_bits = r;
86 static constexpr result_type xor_mask = a;
87 static constexpr size_t tempering_u = u;
88 static constexpr result_type tempering_d = d;
89 static constexpr size_t tempering_s = s;
90 static constexpr result_type tempering_b = b;
91 static constexpr size_t tempering_t = t;
92 static constexpr result_type tempering_c = c;
93 static constexpr size_t tempering_l = l;
94 static constexpr result_type initialization_multiplier = f;
95 static constexpr result_type min () { return 0; }
96 static constexpr result_type max() { return 2^w - 1; }
97 static constexpr result_type default_seed = 5489u;
98
99 // constructors and seeding functions
100 explicit mersenne_twister_engine(result_type value = default_seed);
101 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
102 void seed(result_type value = default_seed);
103 template<class Sseq> void seed(Sseq& q);
104
105 // generating functions
106 result_type operator()();
107 void discard(unsigned long long z);
108};
109
110template <class UIntType, size_t w, size_t n, size_t m, size_t r,
111 UIntType a, size_t u, UIntType d, size_t s,
112 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
113bool
114operator==(
115 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
116 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
117
118template <class UIntType, size_t w, size_t n, size_t m, size_t r,
119 UIntType a, size_t u, UIntType d, size_t s,
120 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
121bool
122operator!=(
123 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
124 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
125
126template <class charT, class traits,
127 class UIntType, size_t w, size_t n, size_t m, size_t r,
128 UIntType a, size_t u, UIntType d, size_t s,
129 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
130basic_ostream<charT, traits>&
131operator<<(basic_ostream<charT, traits>& os,
132 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
133
134template <class charT, class traits,
135 class UIntType, size_t w, size_t n, size_t m, size_t r,
136 UIntType a, size_t u, UIntType d, size_t s,
137 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
138basic_istream<charT, traits>&
139operator>>(basic_istream<charT, traits>& is,
140 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
141
142template<class UIntType, size_t w, size_t s, size_t r>
143class subtract_with_carry_engine
144{
145public:
146 // types
147 typedef UIntType result_type;
148
149 // engine characteristics
150 static constexpr size_t word_size = w;
151 static constexpr size_t short_lag = s;
152 static constexpr size_t long_lag = r;
153 static constexpr result_type min() { return 0; }
154 static constexpr result_type max() { return m-1; }
155 static constexpr result_type default_seed = 19780503u;
156
157 // constructors and seeding functions
158 explicit subtract_with_carry_engine(result_type value = default_seed);
159 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
160 void seed(result_type value = default_seed);
161 template<class Sseq> void seed(Sseq& q);
162
163 // generating functions
164 result_type operator()();
165 void discard(unsigned long long z);
166};
167
168template<class UIntType, size_t w, size_t s, size_t r>
169bool
170operator==(
171 const subtract_with_carry_engine<UIntType, w, s, r>& x,
172 const subtract_with_carry_engine<UIntType, w, s, r>& y);
173
174template<class UIntType, size_t w, size_t s, size_t r>
175bool
176operator!=(
177 const subtract_with_carry_engine<UIntType, w, s, r>& x,
178 const subtract_with_carry_engine<UIntType, w, s, r>& y);
179
180template <class charT, class traits,
181 class UIntType, size_t w, size_t s, size_t r>
182basic_ostream<charT, traits>&
183operator<<(basic_ostream<charT, traits>& os,
184 const subtract_with_carry_engine<UIntType, w, s, r>& x);
185
186template <class charT, class traits,
187 class UIntType, size_t w, size_t s, size_t r>
188basic_istream<charT, traits>&
189operator>>(basic_istream<charT, traits>& is,
190 subtract_with_carry_engine<UIntType, w, s, r>& x);
191
192template<class Engine, size_t p, size_t r>
193class discard_block_engine
194{
195public:
196 // types
197 typedef typename Engine::result_type result_type;
198
199 // engine characteristics
200 static constexpr size_t block_size = p;
201 static constexpr size_t used_block = r;
202 static constexpr result_type min() { return Engine::min(); }
203 static constexpr result_type max() { return Engine::max(); }
204
205 // constructors and seeding functions
206 discard_block_engine();
207 explicit discard_block_engine(const Engine& e);
208 explicit discard_block_engine(Engine&& e);
209 explicit discard_block_engine(result_type s);
210 template<class Sseq> explicit discard_block_engine(Sseq& q);
211 void seed();
212 void seed(result_type s);
213 template<class Sseq> void seed(Sseq& q);
214
215 // generating functions
216 result_type operator()();
217 void discard(unsigned long long z);
218
219 // property functions
220 const Engine& base() const;
221};
222
223template<class Engine, size_t p, size_t r>
224bool
225operator==(
226 const discard_block_engine<Engine, p, r>& x,
227 const discard_block_engine<Engine, p, r>& y);
228
229template<class Engine, size_t p, size_t r>
230bool
231operator!=(
232 const discard_block_engine<Engine, p, r>& x,
233 const discard_block_engine<Engine, p, r>& y);
234
235template <class charT, class traits,
236 class Engine, size_t p, size_t r>
237basic_ostream<charT, traits>&
238operator<<(basic_ostream<charT, traits>& os,
239 const discard_block_engine<Engine, p, r>& x);
240
241template <class charT, class traits,
242 class Engine, size_t p, size_t r>
243basic_istream<charT, traits>&
244operator>>(basic_istream<charT, traits>& is,
245 discard_block_engine<Engine, p, r>& x);
246
247template<class Engine, size_t w, class UIntType>
248class independent_bits_engine
249{
250public:
251 // types
252 typedef UIntType result_type;
253
254 // engine characteristics
255 static constexpr result_type min() { return 0; }
256 static constexpr result_type max() { return 2^w - 1; }
257
258 // constructors and seeding functions
259 independent_bits_engine();
260 explicit independent_bits_engine(const Engine& e);
261 explicit independent_bits_engine(Engine&& e);
262 explicit independent_bits_engine(result_type s);
263 template<class Sseq> explicit independent_bits_engine(Sseq& q);
264 void seed();
265 void seed(result_type s);
266 template<class Sseq> void seed(Sseq& q);
267
268 // generating functions
269 result_type operator()(); void discard(unsigned long long z);
270
271 // property functions
272 const Engine& base() const;
273};
274
275template<class Engine, size_t w, class UIntType>
276bool
277operator==(
278 const independent_bits_engine<Engine, w, UIntType>& x,
279 const independent_bits_engine<Engine, w, UIntType>& y);
280
281template<class Engine, size_t w, class UIntType>
282bool
283operator!=(
284 const independent_bits_engine<Engine, w, UIntType>& x,
285 const independent_bits_engine<Engine, w, UIntType>& y);
286
287template <class charT, class traits,
288 class Engine, size_t w, class UIntType>
289basic_ostream<charT, traits>&
290operator<<(basic_ostream<charT, traits>& os,
291 const independent_bits_engine<Engine, w, UIntType>& x);
292
293template <class charT, class traits,
294 class Engine, size_t w, class UIntType>
295basic_istream<charT, traits>&
296operator>>(basic_istream<charT, traits>& is,
297 independent_bits_engine<Engine, w, UIntType>& x);
298
299template<class Engine, size_t k>
300class shuffle_order_engine
301{
302public:
303 // types
304 typedef typename Engine::result_type result_type;
305
306 // engine characteristics
307 static constexpr size_t table_size = k;
308 static constexpr result_type min() { return Engine::min; }
309 static constexpr result_type max() { return Engine::max; }
310
311 // constructors and seeding functions
312 shuffle_order_engine();
313 explicit shuffle_order_engine(const Engine& e);
314 explicit shuffle_order_engine(Engine&& e);
315 explicit shuffle_order_engine(result_type s);
316 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
317 void seed();
318 void seed(result_type s);
319 template<class Sseq> void seed(Sseq& q);
320
321 // generating functions
322 result_type operator()();
323 void discard(unsigned long long z);
324
325 // property functions
326 const Engine& base() const;
327};
328
329template<class Engine, size_t k>
330bool
331operator==(
332 const shuffle_order_engine<Engine, k>& x,
333 const shuffle_order_engine<Engine, k>& y);
334
335template<class Engine, size_t k>
336bool
337operator!=(
338 const shuffle_order_engine<Engine, k>& x,
339 const shuffle_order_engine<Engine, k>& y);
340
341template <class charT, class traits,
342 class Engine, size_t k>
343basic_ostream<charT, traits>&
344operator<<(basic_ostream<charT, traits>& os,
345 const shuffle_order_engine<Engine, k>& x);
346
347template <class charT, class traits,
348 class Engine, size_t k>
349basic_istream<charT, traits>&
350operator>>(basic_istream<charT, traits>& is,
351 shuffle_order_engine<Engine, k>& x);
352
353typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
354 minstd_rand0;
355typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
356 minstd_rand;
357typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
358 0x9908b0df,
359 11, 0xffffffff,
360 7, 0x9d2c5680,
361 15, 0xefc60000,
362 18, 1812433253> mt19937;
363typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
364 0xb5026f5aa96619e9,
365 29, 0x5555555555555555,
366 17, 0x71d67fffeda60000,
367 37, 0xfff7eee000000000,
368 43, 6364136223846793005> mt19937_64;
369typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
370typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
371typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
372typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
373typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
374typedef minstd_rand0 default_random_engine;
375
376// Generators
377
378class random_device
379{
380public:
381 // types
382 typedef unsigned int result_type;
383
384 // generator characteristics
385 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
386 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
387
388 // constructors
389 explicit random_device(const string& token = "/dev/urandom");
390
391 // generating functions
392 result_type operator()();
393
394 // property functions
395 double entropy() const;
396
397 // no copy functions
398 random_device(const random_device& ) = delete;
399 void operator=(const random_device& ) = delete;
400};
401
402// Utilities
403
404class seed_seq
405{
406public:
407 // types
408 typedef uint_least32_t result_type;
409
410 // constructors
411 seed_seq();
412 template<class T>
413 seed_seq(initializer_list<T> il);
414 template<class InputIterator>
415 seed_seq(InputIterator begin, InputIterator end);
416
417 // generating functions
418 template<class RandomAccessIterator>
419 void generate(RandomAccessIterator begin, RandomAccessIterator end);
420
421 // property functions
422 size_t size() const;
423 template<class OutputIterator>
424 void param(OutputIterator dest) const;
425
426 // no copy functions
427 seed_seq(const seed_seq&) = delete;
428 void operator=(const seed_seq& ) = delete;
429};
430
431template<class RealType, size_t bits, class URNG>
432 RealType generate_canonical(URNG& g);
433
434// Distributions
435
436template<class IntType = int>
437class uniform_int_distribution
438{
439public:
440 // types
441 typedef IntType result_type;
442
443 class param_type
444 {
445 public:
446 typedef uniform_int_distribution distribution_type;
447
448 explicit param_type(IntType a = 0,
449 IntType b = numeric_limits<IntType>::max());
450
451 result_type a() const;
452 result_type b() const;
453
454 friend bool operator==(const param_type& x, const param_type& y);
455 friend bool operator!=(const param_type& x, const param_type& y);
456 };
457
458 // constructors and reset functions
459 explicit uniform_int_distribution(IntType a = 0,
460 IntType b = numeric_limits<IntType>::max());
461 explicit uniform_int_distribution(const param_type& parm);
462 void reset();
463
464 // generating functions
465 template<class URNG> result_type operator()(URNG& g);
466 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
467
468 // property functions
469 result_type a() const;
470 result_type b() const;
471
472 param_type param() const;
473 void param(const param_type& parm);
474
475 result_type min() const;
476 result_type max() const;
477
478 friend bool operator==(const uniform_int_distribution& x,
479 const uniform_int_distribution& y);
480 friend bool operator!=(const uniform_int_distribution& x,
481 const uniform_int_distribution& y);
482
483 template <class charT, class traits>
484 friend
485 basic_ostream<charT, traits>&
486 operator<<(basic_ostream<charT, traits>& os,
487 const uniform_int_distribution& x);
488
489 template <class charT, class traits>
490 friend
491 basic_istream<charT, traits>&
492 operator>>(basic_istream<charT, traits>& is,
493 uniform_int_distribution& x);
494};
495
496template<class RealType = double>
497class uniform_real_distribution
498{
499public:
500 // types
501 typedef RealType result_type;
502
503 class param_type
504 {
505 public:
506 typedef uniform_real_distribution distribution_type;
507
508 explicit param_type(RealType a = 0,
509 RealType b = 1);
510
511 result_type a() const;
512 result_type b() const;
513
514 friend bool operator==(const param_type& x, const param_type& y);
515 friend bool operator!=(const param_type& x, const param_type& y);
516 };
517
518 // constructors and reset functions
519 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
520 explicit uniform_real_distribution(const param_type& parm);
521 void reset();
522
523 // generating functions
524 template<class URNG> result_type operator()(URNG& g);
525 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
526
527 // property functions
528 result_type a() const;
529 result_type b() const;
530
531 param_type param() const;
532 void param(const param_type& parm);
533
534 result_type min() const;
535 result_type max() const;
536
537 friend bool operator==(const uniform_real_distribution& x,
538 const uniform_real_distribution& y);
539 friend bool operator!=(const uniform_real_distribution& x,
540 const uniform_real_distribution& y);
541
542 template <class charT, class traits>
543 friend
544 basic_ostream<charT, traits>&
545 operator<<(basic_ostream<charT, traits>& os,
546 const uniform_real_distribution& x);
547
548 template <class charT, class traits>
549 friend
550 basic_istream<charT, traits>&
551 operator>>(basic_istream<charT, traits>& is,
552 uniform_real_distribution& x);
553};
554
555class bernoulli_distribution
556{
557public:
558 // types
559 typedef bool result_type;
560
561 class param_type
562 {
563 public:
564 typedef bernoulli_distribution distribution_type;
565
566 explicit param_type(double p = 0.5);
567
568 double p() const;
569
570 friend bool operator==(const param_type& x, const param_type& y);
571 friend bool operator!=(const param_type& x, const param_type& y);
572 };
573
574 // constructors and reset functions
575 explicit bernoulli_distribution(double p = 0.5);
576 explicit bernoulli_distribution(const param_type& parm);
577 void reset();
578
579 // generating functions
580 template<class URNG> result_type operator()(URNG& g);
581 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
582
583 // property functions
584 double p() const;
585
586 param_type param() const;
587 void param(const param_type& parm);
588
589 result_type min() const;
590 result_type max() const;
591
592 friend bool operator==(const bernoulli_distribution& x,
593 const bernoulli_distribution& y);
594 friend bool operator!=(const bernoulli_distribution& x,
595 const bernoulli_distribution& y);
596
597 template <class charT, class traits>
598 friend
599 basic_ostream<charT, traits>&
600 operator<<(basic_ostream<charT, traits>& os,
601 const bernoulli_distribution& x);
602
603 template <class charT, class traits>
604 friend
605 basic_istream<charT, traits>&
606 operator>>(basic_istream<charT, traits>& is,
607 bernoulli_distribution& x);
608};
609
610template<class IntType = int>
Howard Hinnant03aad812010-05-11 23:26:59 +0000611class binomial_distribution
612{
613public:
614 // types
615 typedef IntType result_type;
616
617 class param_type
618 {
619 public:
620 typedef binomial_distribution distribution_type;
621
622 explicit param_type(IntType t = 1, double p = 0.5);
623
624 IntType t() const;
625 double p() const;
626
627 friend bool operator==(const param_type& x, const param_type& y);
628 friend bool operator!=(const param_type& x, const param_type& y);
629 };
630
631 // constructors and reset functions
632 explicit binomial_distribution(IntType t = 1, double p = 0.5);
633 explicit binomial_distribution(const param_type& parm);
634 void reset();
635
636 // generating functions
637 template<class URNG> result_type operator()(URNG& g);
638 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
639
640 // property functions
641 IntType t() const;
642 double p() const;
643
644 param_type param() const;
645 void param(const param_type& parm);
646
647 result_type min() const;
648 result_type max() const;
649
650 friend bool operator==(const binomial_distribution& x,
651 const binomial_distribution& y);
652 friend bool operator!=(const binomial_distribution& x,
653 const binomial_distribution& y);
654
655 template <class charT, class traits>
656 friend
657 basic_ostream<charT, traits>&
658 operator<<(basic_ostream<charT, traits>& os,
659 const binomial_distribution& x);
660
661 template <class charT, class traits>
662 friend
663 basic_istream<charT, traits>&
664 operator>>(basic_istream<charT, traits>& is,
665 binomial_distribution& x);
666};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668template<class IntType = int>
669 class geometric_distribution;
670
671template<class IntType = int>
Howard Hinnantf2fe5d52010-05-17 00:09:38 +0000672class negative_binomial_distribution
673{
674public:
675 // types
676 typedef IntType result_type;
677
678 class param_type
679 {
680 public:
681 typedef negative_binomial_distribution distribution_type;
682
683 explicit param_type(result_type k = 1, double p = 0.5);
684
685 result_type k() const;
686 double p() const;
687
688 friend bool operator==(const param_type& x, const param_type& y);
689 friend bool operator!=(const param_type& x, const param_type& y);
690 };
691
692 // constructor and reset functions
693 explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
694 explicit negative_binomial_distribution(const param_type& parm);
695 void reset();
696
697 // generating functions
698 template<class URNG> result_type operator()(URNG& g);
699 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
700
701 // property functions
702 result_type k() const;
703 double p() const;
704
705 param_type param() const;
706 void param(const param_type& parm);
707
708 result_type min() const;
709 result_type max() const;
710
711 friend bool operator==(const negative_binomial_distribution& x,
712 const negative_binomial_distribution& y);
713 friend bool operator!=(const negative_binomial_distribution& x,
714 const negative_binomial_distribution& y);
715
716 template <class charT, class traits>
717 friend
718 basic_ostream<charT, traits>&
719 operator<<(basic_ostream<charT, traits>& os,
720 const negative_binomial_distribution& x);
721
722 template <class charT, class traits>
723 friend
724 basic_istream<charT, traits>&
725 operator>>(basic_istream<charT, traits>& is,
726 negative_binomial_distribution& x);
727};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728
729template<class IntType = int>
Howard Hinnant4ff556c2010-05-14 21:38:54 +0000730class poisson_distribution
731{
732public:
733 // types
734 typedef IntType result_type;
735
736 class param_type
737 {
738 public:
739 typedef poisson_distribution distribution_type;
740
741 explicit param_type(double mean = 1.0);
742
743 double mean() const;
744
745 friend bool operator==(const param_type& x, const param_type& y);
746 friend bool operator!=(const param_type& x, const param_type& y);
747 };
748
749 // constructors and reset functions
750 explicit poisson_distribution(double mean = 1.0);
751 explicit poisson_distribution(const param_type& parm);
752 void reset();
753
754 // generating functions
755 template<class URNG> result_type operator()(URNG& g);
756 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
757
758 // property functions
759 double mean() const;
760
761 param_type param() const;
762 void param(const param_type& parm);
763
764 result_type min() const;
765 result_type max() const;
766
767 friend bool operator==(const poisson_distribution& x,
768 const poisson_distribution& y);
769 friend bool operator!=(const poisson_distribution& x,
770 const poisson_distribution& y);
771
772 template <class charT, class traits>
773 friend
774 basic_ostream<charT, traits>&
775 operator<<(basic_ostream<charT, traits>& os,
776 const poisson_distribution& x);
777
778 template <class charT, class traits>
779 friend
780 basic_istream<charT, traits>&
781 operator>>(basic_istream<charT, traits>& is,
782 poisson_distribution& x);
783};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784
785template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000786class exponential_distribution
787{
788public:
789 // types
790 typedef RealType result_type;
791
792 class param_type
793 {
794 public:
795 typedef exponential_distribution distribution_type;
796
Howard Hinnanta64111c2010-05-12 21:02:31 +0000797 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000798
Howard Hinnanta64111c2010-05-12 21:02:31 +0000799 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000800
801 friend bool operator==(const param_type& x, const param_type& y);
802 friend bool operator!=(const param_type& x, const param_type& y);
803 };
804
805 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000806 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000807 explicit exponential_distribution(const param_type& parm);
808 void reset();
809
810 // generating functions
811 template<class URNG> result_type operator()(URNG& g);
812 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
813
814 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000815 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000816
817 param_type param() const;
818 void param(const param_type& parm);
819
820 result_type min() const;
821 result_type max() const;
822
823 friend bool operator==(const exponential_distribution& x,
824 const exponential_distribution& y);
825 friend bool operator!=(const exponential_distribution& x,
826 const exponential_distribution& y);
827
828 template <class charT, class traits>
829 friend
830 basic_ostream<charT, traits>&
831 operator<<(basic_ostream<charT, traits>& os,
832 const exponential_distribution& x);
833
834 template <class charT, class traits>
835 friend
836 basic_istream<charT, traits>&
837 operator>>(basic_istream<charT, traits>& is,
838 exponential_distribution& x);
839};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840
841template<class RealType = double>
Howard Hinnantc7c49132010-05-13 17:58:28 +0000842class gamma_distribution
843{
844public:
845 // types
846 typedef RealType result_type;
847
848 class param_type
849 {
850 public:
851 typedef gamma_distribution distribution_type;
852
853 explicit param_type(result_type alpha = 1, result_type beta = 1);
854
855 result_type alpha() const;
856 result_type beta() const;
857
858 friend bool operator==(const param_type& x, const param_type& y);
859 friend bool operator!=(const param_type& x, const param_type& y);
860 };
861
862 // constructors and reset functions
863 explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
864 explicit gamma_distribution(const param_type& parm);
865 void reset();
866
867 // generating functions
868 template<class URNG> result_type operator()(URNG& g);
869 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
870
871 // property functions
872 result_type alpha() const;
873 result_type beta() const;
874
875 param_type param() const;
876 void param(const param_type& parm);
877
878 result_type min() const;
879 result_type max() const;
880
881 friend bool operator==(const gamma_distribution& x,
882 const gamma_distribution& y);
883 friend bool operator!=(const gamma_distribution& x,
884 const gamma_distribution& y);
885
886 template <class charT, class traits>
887 friend
888 basic_ostream<charT, traits>&
889 operator<<(basic_ostream<charT, traits>& os,
890 const gamma_distribution& x);
891
892 template <class charT, class traits>
893 friend
894 basic_istream<charT, traits>&
895 operator>>(basic_istream<charT, traits>& is,
896 gamma_distribution& x);
897};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898
899template<class RealType = double>
Howard Hinnant9de6e302010-05-16 01:09:02 +0000900class weibull_distribution
901{
902public:
903 // types
904 typedef RealType result_type;
905
906 class param_type
907 {
908 public:
909 typedef weibull_distribution distribution_type;
910
911 explicit param_type(result_type alpha = 1, result_type beta = 1);
912
913 result_type a() const;
914 result_type b() const;
915
916 friend bool operator==(const param_type& x, const param_type& y);
917 friend bool operator!=(const param_type& x, const param_type& y);
918 };
919
920 // constructor and reset functions
921 explicit weibull_distribution(result_type a = 1, result_type b = 1);
922 explicit weibull_distribution(const param_type& parm);
923 void reset();
924
925 // generating functions
926 template<class URNG> result_type operator()(URNG& g);
927 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
928
929 // property functions
930 result_type a() const;
931 result_type b() const;
932
933 param_type param() const;
934 void param(const param_type& parm);
935
936 result_type min() const;
937 result_type max() const;
938
939
940 friend bool operator==(const weibull_distribution& x,
941 const weibull_distribution& y);
942 friend bool operator!=(const weibull_distribution& x,
943 const weibull_distribution& y);
944
945 template <class charT, class traits>
946 friend
947 basic_ostream<charT, traits>&
948 operator<<(basic_ostream<charT, traits>& os,
949 const weibull_distribution& x);
950
951 template <class charT, class traits>
952 friend
953 basic_istream<charT, traits>&
954 operator>>(basic_istream<charT, traits>& is,
955 weibull_distribution& x);
956};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957
958template<class RealType = double>
959 class extreme_value_distribution;
960
961template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +0000962class normal_distribution
963{
964public:
965 // types
966 typedef RealType result_type;
967
968 class param_type
969 {
970 public:
971 typedef normal_distribution distribution_type;
972
973 explicit param_type(result_type mean = 0, result_type stddev = 1);
974
975 result_type mean() const;
976 result_type stddev() const;
977
978 friend bool operator==(const param_type& x, const param_type& y);
979 friend bool operator!=(const param_type& x, const param_type& y);
980 };
981
982 // constructors and reset functions
983 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
984 explicit normal_distribution(const param_type& parm);
985 void reset();
986
987 // generating functions
988 template<class URNG> result_type operator()(URNG& g);
989 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
990
991 // property functions
992 result_type mean() const;
993 result_type stddev() const;
994
995 param_type param() const;
996 void param(const param_type& parm);
997
998 result_type min() const;
999 result_type max() const;
1000
1001 friend bool operator==(const normal_distribution& x,
1002 const normal_distribution& y);
1003 friend bool operator!=(const normal_distribution& x,
1004 const normal_distribution& y);
1005
1006 template <class charT, class traits>
1007 friend
1008 basic_ostream<charT, traits>&
1009 operator<<(basic_ostream<charT, traits>& os,
1010 const normal_distribution& x);
1011
1012 template <class charT, class traits>
1013 friend
1014 basic_istream<charT, traits>&
1015 operator>>(basic_istream<charT, traits>& is,
1016 normal_distribution& x);
1017};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018
1019template<class RealType = double>
1020 class lognormal_distribution;
1021
1022template<class RealType = double>
Howard Hinnant97dc2f32010-05-15 23:36:00 +00001023class chi_squared_distribution
1024{
1025public:
1026 // types
1027 typedef RealType result_type;
1028
1029 class param_type
1030 {
1031 public:
1032 typedef chi_squared_distribution distribution_type;
1033
1034 explicit param_type(result_type n = 1);
1035
1036 result_type n() const;
1037
1038 friend bool operator==(const param_type& x, const param_type& y);
1039 friend bool operator!=(const param_type& x, const param_type& y);
1040 };
1041
1042 // constructor and reset functions
1043 explicit chi_squared_distribution(result_type n = 1);
1044 explicit chi_squared_distribution(const param_type& parm);
1045 void reset();
1046
1047 // generating functions
1048 template<class URNG> result_type operator()(URNG& g);
1049 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1050
1051 // property functions
1052 result_type n() const;
1053
1054 param_type param() const;
1055 void param(const param_type& parm);
1056
1057 result_type min() const;
1058 result_type max() const;
1059
1060
1061 friend bool operator==(const chi_squared_distribution& x,
1062 const chi_squared_distribution& y);
1063 friend bool operator!=(const chi_squared_distribution& x,
1064 const chi_squared_distribution& y);
1065
1066 template <class charT, class traits>
1067 friend
1068 basic_ostream<charT, traits>&
1069 operator<<(basic_ostream<charT, traits>& os,
1070 const chi_squared_distribution& x);
1071
1072 template <class charT, class traits>
1073 friend
1074 basic_istream<charT, traits>&
1075 operator>>(basic_istream<charT, traits>& is,
1076 chi_squared_distribution& x);
1077};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078
1079template<class RealType = double>
1080 class cauchy_distribution;
1081
1082template<class RealType = double>
1083 class fisher_f_distribution;
1084
1085template<class RealType = double>
1086 class student_t_distribution;
1087
1088template<class IntType = int>
1089 class discrete_distribution;
1090
1091template<class RealType = double>
1092 class piecewise_constant_distribution;
1093
1094template<class RealType = double>
1095 class piecewise_linear_distribution;
1096
1097} // std
1098*/
1099
1100#include <__config>
1101#include <cstddef>
1102#include <type_traits>
1103#include <initializer_list>
1104#include <cstdint>
1105#include <limits>
1106#include <algorithm>
1107#include <vector>
1108#include <string>
1109#include <istream>
1110#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +00001111#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112
1113#pragma GCC system_header
1114
1115_LIBCPP_BEGIN_NAMESPACE_STD
1116
1117// linear_congruential_engine
1118
1119template <unsigned long long __a, unsigned long long __c,
1120 unsigned long long __m, unsigned long long _M,
1121 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
1122struct __lce_ta;
1123
1124// 64
1125
1126template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1127struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1128{
1129 typedef unsigned long long result_type;
1130 static result_type next(result_type __x)
1131 {
1132 // Schrage's algorithm
1133 const result_type __q = __m / __a;
1134 const result_type __r = __m % __a;
1135 const result_type __t0 = __a * (__x % __q);
1136 const result_type __t1 = __r * (__x / __q);
1137 __x = __t0 + (__t0 < __t1) * __m - __t1;
1138 __x += __c - (__x >= __m - __c) * __m;
1139 return __x;
1140 }
1141};
1142
1143template <unsigned long long __a, unsigned long long __m>
1144struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1145{
1146 typedef unsigned long long result_type;
1147 static result_type next(result_type __x)
1148 {
1149 // Schrage's algorithm
1150 const result_type __q = __m / __a;
1151 const result_type __r = __m % __a;
1152 const result_type __t0 = __a * (__x % __q);
1153 const result_type __t1 = __r * (__x / __q);
1154 __x = __t0 + (__t0 < __t1) * __m - __t1;
1155 return __x;
1156 }
1157};
1158
1159template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1160struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1161{
1162 typedef unsigned long long result_type;
1163 static result_type next(result_type __x)
1164 {
1165 return (__a * __x + __c) % __m;
1166 }
1167};
1168
1169template <unsigned long long __a, unsigned long long __c>
1170struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1171{
1172 typedef unsigned long long result_type;
1173 static result_type next(result_type __x)
1174 {
1175 return __a * __x + __c;
1176 }
1177};
1178
1179// 32
1180
1181template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1182struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1183{
1184 typedef unsigned result_type;
1185 static result_type next(result_type __x)
1186 {
1187 const result_type __a = static_cast<result_type>(_A);
1188 const result_type __c = static_cast<result_type>(_C);
1189 const result_type __m = static_cast<result_type>(_M);
1190 // Schrage's algorithm
1191 const result_type __q = __m / __a;
1192 const result_type __r = __m % __a;
1193 const result_type __t0 = __a * (__x % __q);
1194 const result_type __t1 = __r * (__x / __q);
1195 __x = __t0 + (__t0 < __t1) * __m - __t1;
1196 __x += __c - (__x >= __m - __c) * __m;
1197 return __x;
1198 }
1199};
1200
1201template <unsigned long long _A, unsigned long long _M>
1202struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1203{
1204 typedef unsigned result_type;
1205 static result_type next(result_type __x)
1206 {
1207 const result_type __a = static_cast<result_type>(_A);
1208 const result_type __m = static_cast<result_type>(_M);
1209 // Schrage's algorithm
1210 const result_type __q = __m / __a;
1211 const result_type __r = __m % __a;
1212 const result_type __t0 = __a * (__x % __q);
1213 const result_type __t1 = __r * (__x / __q);
1214 __x = __t0 + (__t0 < __t1) * __m - __t1;
1215 return __x;
1216 }
1217};
1218
1219template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1220struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1221{
1222 typedef unsigned result_type;
1223 static result_type next(result_type __x)
1224 {
1225 const result_type __a = static_cast<result_type>(_A);
1226 const result_type __c = static_cast<result_type>(_C);
1227 const result_type __m = static_cast<result_type>(_M);
1228 return (__a * __x + __c) % __m;
1229 }
1230};
1231
1232template <unsigned long long _A, unsigned long long _C>
1233struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1234{
1235 typedef unsigned result_type;
1236 static result_type next(result_type __x)
1237 {
1238 const result_type __a = static_cast<result_type>(_A);
1239 const result_type __c = static_cast<result_type>(_C);
1240 return __a * __x + __c;
1241 }
1242};
1243
1244// 16
1245
1246template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1247struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1248{
1249 typedef unsigned short result_type;
1250 static result_type next(result_type __x)
1251 {
1252 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1253 }
1254};
1255
1256template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1257class linear_congruential_engine;
1258
1259template <class _CharT, class _Traits,
1260 class _U, _U _A, _U _C, _U _N>
1261basic_ostream<_CharT, _Traits>&
1262operator<<(basic_ostream<_CharT, _Traits>& __os,
1263 const linear_congruential_engine<_U, _A, _C, _N>&);
1264
1265template <class _CharT, class _Traits,
1266 class _U, _U _A, _U _C, _U _N>
1267basic_istream<_CharT, _Traits>&
1268operator>>(basic_istream<_CharT, _Traits>& __is,
1269 linear_congruential_engine<_U, _A, _C, _N>& __x);
1270
1271template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1272class linear_congruential_engine
1273{
1274public:
1275 // types
1276 typedef _UIntType result_type;
1277
1278private:
1279 result_type __x_;
1280
1281 static const result_type _M = result_type(~0);
1282
1283 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1284 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1285public:
1286 static const result_type _Min = __c == 0u ? 1u: 0u;
1287 static const result_type _Max = __m - 1u;
1288 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1289
1290 // engine characteristics
1291 static const/*expr*/ result_type multiplier = __a;
1292 static const/*expr*/ result_type increment = __c;
1293 static const/*expr*/ result_type modulus = __m;
1294 static const/*expr*/ result_type min() {return _Min;}
1295 static const/*expr*/ result_type max() {return _Max;}
1296 static const/*expr*/ result_type default_seed = 1u;
1297
1298 // constructors and seeding functions
1299 explicit linear_congruential_engine(result_type __s = default_seed)
1300 {seed(__s);}
1301 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
1302 {seed(__q);}
1303 void seed(result_type __s = default_seed)
1304 {seed(integral_constant<bool, __m == 0>(),
1305 integral_constant<bool, __c == 0>(), __s);}
1306 template<class _Sseq>
1307 typename enable_if
1308 <
1309 !is_convertible<_Sseq, result_type>::value,
1310 void
1311 >::type
1312 seed(_Sseq& __q)
1313 {__seed(__q, integral_constant<unsigned,
1314 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1315 : (__m-1) / 0x100000000ull)>());}
1316
1317 // generating functions
1318 result_type operator()()
1319 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1320 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1321
1322 friend bool operator==(const linear_congruential_engine& __x,
1323 const linear_congruential_engine& __y)
1324 {return __x.__x_ == __y.__x_;}
1325 friend bool operator!=(const linear_congruential_engine& __x,
1326 const linear_congruential_engine& __y)
1327 {return !(__x == __y);}
1328
1329private:
1330
1331 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1332 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1333 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1334 1 : __s % __m;}
1335 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1336
1337 template<class _Sseq>
1338 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1339 template<class _Sseq>
1340 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1341
1342 template <class _CharT, class _Traits,
1343 class _U, _U _A, _U _C, _U _N>
1344 friend
1345 basic_ostream<_CharT, _Traits>&
1346 operator<<(basic_ostream<_CharT, _Traits>& __os,
1347 const linear_congruential_engine<_U, _A, _C, _N>&);
1348
1349 template <class _CharT, class _Traits,
1350 class _U, _U _A, _U _C, _U _N>
1351 friend
1352 basic_istream<_CharT, _Traits>&
1353 operator>>(basic_istream<_CharT, _Traits>& __is,
1354 linear_congruential_engine<_U, _A, _C, _N>& __x);
1355};
1356
1357template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1358template<class _Sseq>
1359void
1360linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1361 integral_constant<unsigned, 1>)
1362{
1363 const unsigned __k = 1;
1364 uint32_t __ar[__k+3];
1365 __q.generate(__ar, __ar + __k + 3);
1366 result_type __s = static_cast<result_type>(__ar[3] % __m);
1367 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1368}
1369
1370template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1371template<class _Sseq>
1372void
1373linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1374 integral_constant<unsigned, 2>)
1375{
1376 const unsigned __k = 2;
1377 uint32_t __ar[__k+3];
1378 __q.generate(__ar, __ar + __k + 3);
1379 result_type __s = static_cast<result_type>((__ar[3] +
1380 (uint64_t)__ar[4] << 32) % __m);
1381 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1382}
1383
1384template <class _CharT, class _Traits>
1385class __save_flags
1386{
1387 typedef basic_ios<_CharT, _Traits> __stream_type;
1388 typedef typename __stream_type::fmtflags fmtflags;
1389
1390 __stream_type& __stream_;
1391 fmtflags __fmtflags_;
1392 _CharT __fill_;
1393
1394 __save_flags(const __save_flags&);
1395 __save_flags& operator=(const __save_flags&);
1396public:
1397 explicit __save_flags(__stream_type& __stream)
1398 : __stream_(__stream),
1399 __fmtflags_(__stream.flags()),
1400 __fill_(__stream.fill())
1401 {}
1402 ~__save_flags()
1403 {
1404 __stream_.flags(__fmtflags_);
1405 __stream_.fill(__fill_);
1406 }
1407};
1408
1409template <class _CharT, class _Traits,
1410 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1411inline
1412basic_ostream<_CharT, _Traits>&
1413operator<<(basic_ostream<_CharT, _Traits>& __os,
1414 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1415{
1416 __save_flags<_CharT, _Traits> _(__os);
1417 __os.flags(ios_base::dec | ios_base::left);
1418 __os.fill(__os.widen(' '));
1419 return __os << __x.__x_;
1420}
1421
1422template <class _CharT, class _Traits,
1423 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1424basic_istream<_CharT, _Traits>&
1425operator>>(basic_istream<_CharT, _Traits>& __is,
1426 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1427{
1428 __save_flags<_CharT, _Traits> _(__is);
1429 __is.flags(ios_base::dec | ios_base::skipws);
1430 _UIntType __t;
1431 __is >> __t;
1432 if (!__is.fail())
1433 __x.__x_ = __t;
1434 return __is;
1435}
1436
1437typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1438 minstd_rand0;
1439typedef minstd_rand0 default_random_engine;
1440typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1441 minstd_rand;
1442// mersenne_twister_engine
1443
1444template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1445 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1446 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1447class mersenne_twister_engine;
1448
1449template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1450 _UI _A, size_t _U, _UI _D, size_t _S,
1451 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1452bool
1453operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1454 _B, _T, _C, _L, _F>& __x,
1455 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1456 _B, _T, _C, _L, _F>& __y);
1457
1458template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1459 _UI _A, size_t _U, _UI _D, size_t _S,
1460 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1461bool
1462operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1463 _B, _T, _C, _L, _F>& __x,
1464 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1465 _B, _T, _C, _L, _F>& __y);
1466
1467template <class _CharT, class _Traits,
1468 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1469 _UI _A, size_t _U, _UI _D, size_t _S,
1470 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1471basic_ostream<_CharT, _Traits>&
1472operator<<(basic_ostream<_CharT, _Traits>& __os,
1473 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1474 _B, _T, _C, _L, _F>& __x);
1475
1476template <class _CharT, class _Traits,
1477 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1478 _UI _A, size_t _U, _UI _D, size_t _S,
1479 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1480basic_istream<_CharT, _Traits>&
1481operator>>(basic_istream<_CharT, _Traits>& __is,
1482 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1483 _B, _T, _C, _L, _F>& __x);
1484
1485template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1486 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1487 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1488class mersenne_twister_engine
1489{
1490public:
1491 // types
1492 typedef _UIntType result_type;
1493
1494private:
1495 result_type __x_[__n];
1496 size_t __i_;
1497
1498 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1499 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1500 static const result_type _Dt = numeric_limits<result_type>::digits;
1501 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1502 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1503 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1504 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1505 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1506 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1507 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1508public:
1509 static const result_type _Min = 0;
1510 static const result_type _Max = __w == _Dt ? result_type(~0) :
1511 (result_type(1) << __w) - result_type(1);
1512 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1513 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1514 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1515 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1516 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1517 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1518
1519 // engine characteristics
1520 static const/*expr*/ size_t word_size = __w;
1521 static const/*expr*/ size_t state_size = __n;
1522 static const/*expr*/ size_t shift_size = __m;
1523 static const/*expr*/ size_t mask_bits = __r;
1524 static const/*expr*/ result_type xor_mask = __a;
1525 static const/*expr*/ size_t tempering_u = __u;
1526 static const/*expr*/ result_type tempering_d = __d;
1527 static const/*expr*/ size_t tempering_s = __s;
1528 static const/*expr*/ result_type tempering_b = __b;
1529 static const/*expr*/ size_t tempering_t = __t;
1530 static const/*expr*/ result_type tempering_c = __c;
1531 static const/*expr*/ size_t tempering_l = __l;
1532 static const/*expr*/ result_type initialization_multiplier = __f;
1533 static const/*expr*/ result_type min() { return _Min; }
1534 static const/*expr*/ result_type max() { return _Max; }
1535 static const/*expr*/ result_type default_seed = 5489u;
1536
1537 // constructors and seeding functions
1538 explicit mersenne_twister_engine(result_type __sd = default_seed)
1539 {seed(__sd);}
1540 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
1541 {seed(__q);}
1542 void seed(result_type __sd = default_seed);
1543 template<class _Sseq>
1544 typename enable_if
1545 <
1546 !is_convertible<_Sseq, result_type>::value,
1547 void
1548 >::type
1549 seed(_Sseq& __q)
1550 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1551
1552 // generating functions
1553 result_type operator()();
1554 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1555
1556 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1557 _UI _A, size_t _U, _UI _D, size_t _S,
1558 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1559 friend
1560 bool
1561 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1562 _B, _T, _C, _L, _F>& __x,
1563 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1564 _B, _T, _C, _L, _F>& __y);
1565
1566 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1567 _UI _A, size_t _U, _UI _D, size_t _S,
1568 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1569 friend
1570 bool
1571 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1572 _B, _T, _C, _L, _F>& __x,
1573 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1574 _B, _T, _C, _L, _F>& __y);
1575
1576 template <class _CharT, class _Traits,
1577 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1578 _UI _A, size_t _U, _UI _D, size_t _S,
1579 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1580 friend
1581 basic_ostream<_CharT, _Traits>&
1582 operator<<(basic_ostream<_CharT, _Traits>& __os,
1583 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1584 _B, _T, _C, _L, _F>& __x);
1585
1586 template <class _CharT, class _Traits,
1587 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1588 _UI _A, size_t _U, _UI _D, size_t _S,
1589 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1590 friend
1591 basic_istream<_CharT, _Traits>&
1592 operator>>(basic_istream<_CharT, _Traits>& __is,
1593 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1594 _B, _T, _C, _L, _F>& __x);
1595private:
1596
1597 template<class _Sseq>
1598 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1599 template<class _Sseq>
1600 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1601
1602 template <size_t __count>
1603 static
1604 typename enable_if
1605 <
1606 __count < __w,
1607 result_type
1608 >::type
1609 __lshift(result_type __x) {return (__x << __count) & _Max;}
1610
1611 template <size_t __count>
1612 static
1613 typename enable_if
1614 <
1615 (__count >= __w),
1616 result_type
1617 >::type
1618 __lshift(result_type __x) {return result_type(0);}
1619
1620 template <size_t __count>
1621 static
1622 typename enable_if
1623 <
1624 __count < _Dt,
1625 result_type
1626 >::type
1627 __rshift(result_type __x) {return __x >> __count;}
1628
1629 template <size_t __count>
1630 static
1631 typename enable_if
1632 <
1633 (__count >= _Dt),
1634 result_type
1635 >::type
1636 __rshift(result_type __x) {return result_type(0);}
1637};
1638
1639template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1640 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1641 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1642void
1643mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1644 __t, __c, __l, __f>::seed(result_type __sd)
1645{ // __w >= 2
1646 __x_[0] = __sd & _Max;
1647 for (size_t __i = 1; __i < __n; ++__i)
1648 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
1649 __i_ = 0;
1650}
1651
1652template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1653 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1654 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1655template<class _Sseq>
1656void
1657mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1658 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
1659{
1660 const unsigned __k = 1;
1661 uint32_t __ar[__n * __k];
1662 __q.generate(__ar, __ar + __n * __k);
1663 for (size_t __i = 0; __i < __n; ++__i)
1664 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1665 const result_type __mask = __r == _Dt ? result_type(~0) :
1666 (result_type(1) << __r) - result_type(1);
1667 __i_ = 0;
1668 if ((__x_[0] & ~__mask) == 0)
1669 {
1670 for (size_t __i = 1; __i < __n; ++__i)
1671 if (__x_[__i] != 0)
1672 return;
1673 __x_[0] = _Max;
1674 }
1675}
1676
1677template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1678 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1679 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1680template<class _Sseq>
1681void
1682mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1683 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
1684{
1685 const unsigned __k = 2;
1686 uint32_t __ar[__n * __k];
1687 __q.generate(__ar, __ar + __n * __k);
1688 for (size_t __i = 0; __i < __n; ++__i)
1689 __x_[__i] = static_cast<result_type>(
1690 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1691 const result_type __mask = __r == _Dt ? result_type(~0) :
1692 (result_type(1) << __r) - result_type(1);
1693 __i_ = 0;
1694 if ((__x_[0] & ~__mask) == 0)
1695 {
1696 for (size_t __i = 1; __i < __n; ++__i)
1697 if (__x_[__i] != 0)
1698 return;
1699 __x_[0] = _Max;
1700 }
1701}
1702
1703template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1704 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1705 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1706_UIntType
1707mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1708 __t, __c, __l, __f>::operator()()
1709{
1710 const size_t __j = (__i_ + 1) % __n;
1711 const result_type __mask = __r == _Dt ? result_type(~0) :
1712 (result_type(1) << __r) - result_type(1);
1713 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
1714 const size_t __k = (__i_ + __m) % __n;
1715 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
1716 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
1717 __i_ = __j;
1718 __z ^= __lshift<__s>(__z) & __b;
1719 __z ^= __lshift<__t>(__z) & __c;
1720 return __z ^ __rshift<__l>(__z);
1721}
1722
1723template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1724 _UI _A, size_t _U, _UI _D, size_t _S,
1725 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1726bool
1727operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1728 _B, _T, _C, _L, _F>& __x,
1729 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1730 _B, _T, _C, _L, _F>& __y)
1731{
1732 if (__x.__i_ == __y.__i_)
1733 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
1734 if (__x.__i_ == 0 || __y.__i_ == 0)
1735 {
1736 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
1737 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1738 __y.__x_ + __y.__i_))
1739 return false;
1740 if (__x.__i_ == 0)
1741 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
1742 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
1743 }
1744 if (__x.__i_ < __y.__i_)
1745 {
1746 size_t __j = _N - __y.__i_;
1747 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1748 __y.__x_ + __y.__i_))
1749 return false;
1750 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
1751 __y.__x_))
1752 return false;
1753 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1754 __y.__x_ + (_N - (__x.__i_ + __j)));
1755 }
1756 size_t __j = _N - __x.__i_;
1757 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1758 __x.__x_ + __x.__i_))
1759 return false;
1760 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
1761 __x.__x_))
1762 return false;
1763 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1764 __x.__x_ + (_N - (__y.__i_ + __j)));
1765}
1766
1767template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1768 _UI _A, size_t _U, _UI _D, size_t _S,
1769 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1770inline
1771bool
1772operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1773 _B, _T, _C, _L, _F>& __x,
1774 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1775 _B, _T, _C, _L, _F>& __y)
1776{
1777 return !(__x == __y);
1778}
1779
1780template <class _CharT, class _Traits,
1781 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1782 _UI _A, size_t _U, _UI _D, size_t _S,
1783 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1784basic_ostream<_CharT, _Traits>&
1785operator<<(basic_ostream<_CharT, _Traits>& __os,
1786 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1787 _B, _T, _C, _L, _F>& __x)
1788{
1789 __save_flags<_CharT, _Traits> _(__os);
1790 __os.flags(ios_base::dec | ios_base::left);
1791 _CharT __sp = __os.widen(' ');
1792 __os.fill(__sp);
1793 __os << __x.__x_[__x.__i_];
1794 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
1795 __os << __sp << __x.__x_[__j];
1796 for (size_t __j = 0; __j < __x.__i_; ++__j)
1797 __os << __sp << __x.__x_[__j];
1798 return __os;
1799}
1800
1801template <class _CharT, class _Traits,
1802 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1803 _UI _A, size_t _U, _UI _D, size_t _S,
1804 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1805basic_istream<_CharT, _Traits>&
1806operator>>(basic_istream<_CharT, _Traits>& __is,
1807 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1808 _B, _T, _C, _L, _F>& __x)
1809{
1810 __save_flags<_CharT, _Traits> _(__is);
1811 __is.flags(ios_base::dec | ios_base::skipws);
1812 _UI __t[_N];
1813 for (size_t __i = 0; __i < _N; ++__i)
1814 __is >> __t[__i];
1815 if (!__is.fail())
1816 {
1817 for (size_t __i = 0; __i < _N; ++__i)
1818 __x.__x_[__i] = __t[__i];
1819 __x.__i_ = 0;
1820 }
1821 return __is;
1822}
1823
1824typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
1825 0x9908b0df, 11, 0xffffffff,
1826 7, 0x9d2c5680,
1827 15, 0xefc60000,
1828 18, 1812433253> mt19937;
1829typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
1830 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
1831 17, 0x71d67fffeda60000ULL,
1832 37, 0xfff7eee000000000ULL,
1833 43, 6364136223846793005ULL> mt19937_64;
1834
1835// subtract_with_carry_engine
1836
1837template<class _UIntType, size_t __w, size_t __s, size_t __r>
1838class subtract_with_carry_engine;
1839
1840template<class _UI, size_t _W, size_t _S, size_t _R>
1841bool
1842operator==(
1843 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1844 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1845
1846template<class _UI, size_t _W, size_t _S, size_t _R>
1847bool
1848operator!=(
1849 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1850 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1851
1852template <class _CharT, class _Traits,
1853 class _UI, size_t _W, size_t _S, size_t _R>
1854basic_ostream<_CharT, _Traits>&
1855operator<<(basic_ostream<_CharT, _Traits>& __os,
1856 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1857
1858template <class _CharT, class _Traits,
1859 class _UI, size_t _W, size_t _S, size_t _R>
1860basic_istream<_CharT, _Traits>&
1861operator>>(basic_istream<_CharT, _Traits>& __is,
1862 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1863
1864template<class _UIntType, size_t __w, size_t __s, size_t __r>
1865class subtract_with_carry_engine
1866{
1867public:
1868 // types
1869 typedef _UIntType result_type;
1870
1871private:
1872 result_type __x_[__r];
1873 result_type __c_;
1874 size_t __i_;
1875
1876 static const result_type _Dt = numeric_limits<result_type>::digits;
1877 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
1878 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
1879 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
1880 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
1881public:
1882 static const result_type _Min = 0;
1883 static const result_type _Max = __w == _Dt ? result_type(~0) :
1884 (result_type(1) << __w) - result_type(1);
1885 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
1886
1887 // engine characteristics
1888 static const/*expr*/ size_t word_size = __w;
1889 static const/*expr*/ size_t short_lag = __s;
1890 static const/*expr*/ size_t long_lag = __r;
1891 static const/*expr*/ result_type min() { return _Min; }
1892 static const/*expr*/ result_type max() { return _Max; }
1893 static const/*expr*/ result_type default_seed = 19780503u;
1894
1895 // constructors and seeding functions
1896 explicit subtract_with_carry_engine(result_type __sd = default_seed)
1897 {seed(__sd);}
1898 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
1899 {seed(__q);}
1900 void seed(result_type __sd = default_seed)
1901 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1902 template<class _Sseq>
1903 typename enable_if
1904 <
1905 !is_convertible<_Sseq, result_type>::value,
1906 void
1907 >::type
1908 seed(_Sseq& __q)
1909 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1910
1911 // generating functions
1912 result_type operator()();
1913 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1914
1915 template<class _UI, size_t _W, size_t _S, size_t _R>
1916 friend
1917 bool
1918 operator==(
1919 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1920 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1921
1922 template<class _UI, size_t _W, size_t _S, size_t _R>
1923 friend
1924 bool
1925 operator!=(
1926 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1927 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1928
1929 template <class _CharT, class _Traits,
1930 class _UI, size_t _W, size_t _S, size_t _R>
1931 friend
1932 basic_ostream<_CharT, _Traits>&
1933 operator<<(basic_ostream<_CharT, _Traits>& __os,
1934 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1935
1936 template <class _CharT, class _Traits,
1937 class _UI, size_t _W, size_t _S, size_t _R>
1938 friend
1939 basic_istream<_CharT, _Traits>&
1940 operator>>(basic_istream<_CharT, _Traits>& __is,
1941 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1942
1943private:
1944
1945 void seed(result_type __sd, integral_constant<unsigned, 1>);
1946 void seed(result_type __sd, integral_constant<unsigned, 2>);
1947 template<class _Sseq>
1948 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1949 template<class _Sseq>
1950 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1951};
1952
1953template<class _UIntType, size_t __w, size_t __s, size_t __r>
1954void
1955subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1956 integral_constant<unsigned, 1>)
1957{
1958 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1959 __e(__sd == 0u ? default_seed : __sd);
1960 for (size_t __i = 0; __i < __r; ++__i)
1961 __x_[__i] = static_cast<result_type>(__e() & _Max);
1962 __c_ = __x_[__r-1] == 0;
1963 __i_ = 0;
1964}
1965
1966template<class _UIntType, size_t __w, size_t __s, size_t __r>
1967void
1968subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1969 integral_constant<unsigned, 2>)
1970{
1971 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1972 __e(__sd == 0u ? default_seed : __sd);
1973 for (size_t __i = 0; __i < __r; ++__i)
1974 __x_[__i] = static_cast<result_type>(
1975 (__e() + ((uint64_t)__e() << 32)) & _Max);
1976 __c_ = __x_[__r-1] == 0;
1977 __i_ = 0;
1978}
1979
1980template<class _UIntType, size_t __w, size_t __s, size_t __r>
1981template<class _Sseq>
1982void
1983subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1984 integral_constant<unsigned, 1>)
1985{
1986 const unsigned __k = 1;
1987 uint32_t __ar[__r * __k];
1988 __q.generate(__ar, __ar + __r * __k);
1989 for (size_t __i = 0; __i < __r; ++__i)
1990 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1991 __c_ = __x_[__r-1] == 0;
1992 __i_ = 0;
1993}
1994
1995template<class _UIntType, size_t __w, size_t __s, size_t __r>
1996template<class _Sseq>
1997void
1998subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1999 integral_constant<unsigned, 2>)
2000{
2001 const unsigned __k = 2;
2002 uint32_t __ar[__r * __k];
2003 __q.generate(__ar, __ar + __r * __k);
2004 for (size_t __i = 0; __i < __r; ++__i)
2005 __x_[__i] = static_cast<result_type>(
2006 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2007 __c_ = __x_[__r-1] == 0;
2008 __i_ = 0;
2009}
2010
2011template<class _UIntType, size_t __w, size_t __s, size_t __r>
2012_UIntType
2013subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2014{
2015 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2016 result_type& __xr = __x_[__i_];
2017 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2018 __xr = (__xs - __xr - __c_) & _Max;
2019 __c_ = __new_c;
2020 __i_ = (__i_ + 1) % __r;
2021 return __xr;
2022}
2023
2024template<class _UI, size_t _W, size_t _S, size_t _R>
2025bool
2026operator==(
2027 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2028 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2029{
2030 if (__x.__c_ != __y.__c_)
2031 return false;
2032 if (__x.__i_ == __y.__i_)
2033 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
2034 if (__x.__i_ == 0 || __y.__i_ == 0)
2035 {
2036 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
2037 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2038 __y.__x_ + __y.__i_))
2039 return false;
2040 if (__x.__i_ == 0)
2041 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2042 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
2043 }
2044 if (__x.__i_ < __y.__i_)
2045 {
2046 size_t __j = _R - __y.__i_;
2047 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2048 __y.__x_ + __y.__i_))
2049 return false;
2050 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
2051 __y.__x_))
2052 return false;
2053 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2054 __y.__x_ + (_R - (__x.__i_ + __j)));
2055 }
2056 size_t __j = _R - __x.__i_;
2057 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2058 __x.__x_ + __x.__i_))
2059 return false;
2060 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
2061 __x.__x_))
2062 return false;
2063 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2064 __x.__x_ + (_R - (__y.__i_ + __j)));
2065}
2066
2067template<class _UI, size_t _W, size_t _S, size_t _R>
2068inline
2069bool
2070operator!=(
2071 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2072 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2073{
2074 return !(__x == __y);
2075}
2076
2077template <class _CharT, class _Traits,
2078 class _UI, size_t _W, size_t _S, size_t _R>
2079basic_ostream<_CharT, _Traits>&
2080operator<<(basic_ostream<_CharT, _Traits>& __os,
2081 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2082{
2083 __save_flags<_CharT, _Traits> _(__os);
2084 __os.flags(ios_base::dec | ios_base::left);
2085 _CharT __sp = __os.widen(' ');
2086 __os.fill(__sp);
2087 __os << __x.__x_[__x.__i_];
2088 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
2089 __os << __sp << __x.__x_[__j];
2090 for (size_t __j = 0; __j < __x.__i_; ++__j)
2091 __os << __sp << __x.__x_[__j];
2092 __os << __sp << __x.__c_;
2093 return __os;
2094}
2095
2096template <class _CharT, class _Traits,
2097 class _UI, size_t _W, size_t _S, size_t _R>
2098basic_istream<_CharT, _Traits>&
2099operator>>(basic_istream<_CharT, _Traits>& __is,
2100 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2101{
2102 __save_flags<_CharT, _Traits> _(__is);
2103 __is.flags(ios_base::dec | ios_base::skipws);
2104 _UI __t[_R+1];
2105 for (size_t __i = 0; __i < _R+1; ++__i)
2106 __is >> __t[__i];
2107 if (!__is.fail())
2108 {
2109 for (size_t __i = 0; __i < _R; ++__i)
2110 __x.__x_[__i] = __t[__i];
2111 __x.__c_ = __t[_R];
2112 __x.__i_ = 0;
2113 }
2114 return __is;
2115}
2116
2117typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2118typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2119
2120// discard_block_engine
2121
2122template<class _Engine, size_t __p, size_t __r>
2123class discard_block_engine
2124{
2125 _Engine __e_;
2126 int __n_;
2127
2128 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2129 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2130public:
2131 // types
2132 typedef typename _Engine::result_type result_type;
2133
2134 // engine characteristics
2135 static const/*expr*/ size_t block_size = __p;
2136 static const/*expr*/ size_t used_block = __r;
2137
2138 // Temporary work around for lack of constexpr
2139 static const result_type _Min = _Engine::_Min;
2140 static const result_type _Max = _Engine::_Max;
2141
2142 static const/*expr*/ result_type min() { return _Engine::min(); }
2143 static const/*expr*/ result_type max() { return _Engine::max(); }
2144
2145 // constructors and seeding functions
2146 discard_block_engine() : __n_(0) {}
2147// explicit discard_block_engine(const _Engine& __e);
2148// explicit discard_block_engine(_Engine&& __e);
2149 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
2150 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
2151 : __e_(__q), __n_(0) {}
2152 void seed() {__e_.seed(); __n_ = 0;}
2153 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
2154 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
2155
2156 // generating functions
2157 result_type operator()();
2158 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2159
2160 // property functions
2161 const _Engine& base() const {return __e_;}
2162
2163 template<class _Eng, size_t _P, size_t _R>
2164 friend
2165 bool
2166 operator==(
2167 const discard_block_engine<_Eng, _P, _R>& __x,
2168 const discard_block_engine<_Eng, _P, _R>& __y);
2169
2170 template<class _Eng, size_t _P, size_t _R>
2171 friend
2172 bool
2173 operator!=(
2174 const discard_block_engine<_Eng, _P, _R>& __x,
2175 const discard_block_engine<_Eng, _P, _R>& __y);
2176
2177 template <class _CharT, class _Traits,
2178 class _Eng, size_t _P, size_t _R>
2179 friend
2180 basic_ostream<_CharT, _Traits>&
2181 operator<<(basic_ostream<_CharT, _Traits>& __os,
2182 const discard_block_engine<_Eng, _P, _R>& __x);
2183
2184 template <class _CharT, class _Traits,
2185 class _Eng, size_t _P, size_t _R>
2186 friend
2187 basic_istream<_CharT, _Traits>&
2188 operator>>(basic_istream<_CharT, _Traits>& __is,
2189 discard_block_engine<_Eng, _P, _R>& __x);
2190};
2191
2192template<class _Engine, size_t __p, size_t __r>
2193typename discard_block_engine<_Engine, __p, __r>::result_type
2194discard_block_engine<_Engine, __p, __r>::operator()()
2195{
2196 if (__n_ >= __r)
2197 {
2198 __e_.discard(__p - __r);
2199 __n_ = 0;
2200 }
2201 ++__n_;
2202 return __e_();
2203}
2204
2205template<class _Eng, size_t _P, size_t _R>
2206inline
2207bool
2208operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2209 const discard_block_engine<_Eng, _P, _R>& __y)
2210{
2211 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2212}
2213
2214template<class _Eng, size_t _P, size_t _R>
2215inline
2216bool
2217operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2218 const discard_block_engine<_Eng, _P, _R>& __y)
2219{
2220 return !(__x == __y);
2221}
2222
2223template <class _CharT, class _Traits,
2224 class _Eng, size_t _P, size_t _R>
2225basic_ostream<_CharT, _Traits>&
2226operator<<(basic_ostream<_CharT, _Traits>& __os,
2227 const discard_block_engine<_Eng, _P, _R>& __x)
2228{
2229 __save_flags<_CharT, _Traits> _(__os);
2230 __os.flags(ios_base::dec | ios_base::left);
2231 _CharT __sp = __os.widen(' ');
2232 __os.fill(__sp);
2233 return __os << __x.__e_ << __sp << __x.__n_;
2234}
2235
2236template <class _CharT, class _Traits,
2237 class _Eng, size_t _P, size_t _R>
2238basic_istream<_CharT, _Traits>&
2239operator>>(basic_istream<_CharT, _Traits>& __is,
2240 discard_block_engine<_Eng, _P, _R>& __x)
2241{
2242 __save_flags<_CharT, _Traits> _(__is);
2243 __is.flags(ios_base::dec | ios_base::skipws);
2244 _Eng __e;
2245 int __n;
2246 __is >> __e >> __n;
2247 if (!__is.fail())
2248 {
2249 __x.__e_ = __e;
2250 __x.__n_ = __n;
2251 }
2252 return __is;
2253}
2254
2255typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2256typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2257
2258// independent_bits_engine
2259
2260template <unsigned long long _X, size_t _R>
2261struct __log2_imp
2262{
2263 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2264 : __log2_imp<_X, _R - 1>::value;
2265};
2266
2267template <unsigned long long _X>
2268struct __log2_imp<_X, 0>
2269{
2270 static const size_t value = 0;
2271};
2272
2273template <size_t _R>
2274struct __log2_imp<0, _R>
2275{
2276 static const size_t value = _R + 1;
2277};
2278
2279template <class _UI, _UI _X>
2280struct __log2
2281{
2282 static const size_t value = __log2_imp<_X,
2283 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2284};
2285
2286template<class _Engine, size_t __w, class _UIntType>
2287class independent_bits_engine
2288{
2289 template <class _UI, _UI _R0, size_t _W, size_t _M>
2290 class __get_n
2291 {
2292 static const size_t _Dt = numeric_limits<_UI>::digits;
2293 static const size_t _N = _W / _M + (_W % _M != 0);
2294 static const size_t _W0 = _W / _N;
2295 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2296 public:
2297 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2298 };
2299public:
2300 // types
2301 typedef _UIntType result_type;
2302
2303private:
2304 _Engine __e_;
2305
2306 static const result_type _Dt = numeric_limits<result_type>::digits;
2307 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2308 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2309
2310 typedef typename _Engine::result_type _Engine_result_type;
2311 typedef typename conditional
2312 <
2313 sizeof(_Engine_result_type) <= sizeof(result_type),
2314 result_type,
2315 _Engine_result_type
2316 >::type _Working_result_type;
2317 // Temporary work around for lack of constexpr
2318 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2319 + _Working_result_type(1);
2320 static const size_t __m = __log2<_Working_result_type, _R>::value;
2321 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2322 static const size_t __w0 = __w / __n;
2323 static const size_t __n0 = __n - __w % __n;
2324 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2325 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2326 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2327 (_R >> __w0) << __w0;
2328 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2329 (_R >> (__w0+1)) << (__w0+1);
2330 static const _Engine_result_type __mask0 = __w0 > 0 ?
2331 _Engine_result_type(~0) >> (_EDt - __w0) :
2332 _Engine_result_type(0);
2333 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2334 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2335 _Engine_result_type(~0);
2336public:
2337 static const result_type _Min = 0;
2338 static const result_type _Max = __w == _Dt ? result_type(~0) :
2339 (result_type(1) << __w) - result_type(1);
2340 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2341
2342 // engine characteristics
2343 static const/*expr*/ result_type min() { return _Min; }
2344 static const/*expr*/ result_type max() { return _Max; }
2345
2346 // constructors and seeding functions
2347 independent_bits_engine() {}
2348// explicit independent_bits_engine(const _Engine& __e);
2349// explicit independent_bits_engine(_Engine&& __e);
2350 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2351 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2352 : __e_(__q) {}
2353 void seed() {__e_.seed();}
2354 void seed(result_type __sd) {__e_.seed(__sd);}
2355 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2356
2357 // generating functions
2358 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2359 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2360
2361 // property functions
2362 const _Engine& base() const {return __e_;}
2363
2364 template<class _Eng, size_t _W, class _UI>
2365 friend
2366 bool
2367 operator==(
2368 const independent_bits_engine<_Eng, _W, _UI>& __x,
2369 const independent_bits_engine<_Eng, _W, _UI>& __y);
2370
2371 template<class _Eng, size_t _W, class _UI>
2372 friend
2373 bool
2374 operator!=(
2375 const independent_bits_engine<_Eng, _W, _UI>& __x,
2376 const independent_bits_engine<_Eng, _W, _UI>& __y);
2377
2378 template <class _CharT, class _Traits,
2379 class _Eng, size_t _W, class _UI>
2380 friend
2381 basic_ostream<_CharT, _Traits>&
2382 operator<<(basic_ostream<_CharT, _Traits>& __os,
2383 const independent_bits_engine<_Eng, _W, _UI>& __x);
2384
2385 template <class _CharT, class _Traits,
2386 class _Eng, size_t _W, class _UI>
2387 friend
2388 basic_istream<_CharT, _Traits>&
2389 operator>>(basic_istream<_CharT, _Traits>& __is,
2390 independent_bits_engine<_Eng, _W, _UI>& __x);
2391
2392private:
2393 result_type __eval(false_type);
2394 result_type __eval(true_type);
2395
2396 template <size_t __count>
2397 static
2398 typename enable_if
2399 <
2400 __count < _Dt,
2401 result_type
2402 >::type
2403 __lshift(result_type __x) {return __x << __count;}
2404
2405 template <size_t __count>
2406 static
2407 typename enable_if
2408 <
2409 (__count >= _Dt),
2410 result_type
2411 >::type
2412 __lshift(result_type __x) {return result_type(0);}
2413};
2414
2415template<class _Engine, size_t __w, class _UIntType>
2416inline
2417_UIntType
2418independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2419{
2420 return static_cast<result_type>(__e_() & __mask0);
2421}
2422
2423template<class _Engine, size_t __w, class _UIntType>
2424_UIntType
2425independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2426{
2427 result_type _S = 0;
2428 for (size_t __k = 0; __k < __n0; ++__k)
2429 {
2430 _Engine_result_type __u;
2431 do
2432 {
2433 __u = __e_() - _Engine::min();
2434 } while (__u >= __y0);
2435 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2436 }
2437 for (size_t __k = __n0; __k < __n; ++__k)
2438 {
2439 _Engine_result_type __u;
2440 do
2441 {
2442 __u = __e_() - _Engine::min();
2443 } while (__u >= __y1);
2444 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2445 }
2446 return _S;
2447}
2448
2449template<class _Eng, size_t _W, class _UI>
2450inline
2451bool
2452operator==(
2453 const independent_bits_engine<_Eng, _W, _UI>& __x,
2454 const independent_bits_engine<_Eng, _W, _UI>& __y)
2455{
2456 return __x.base() == __y.base();
2457}
2458
2459template<class _Eng, size_t _W, class _UI>
2460inline
2461bool
2462operator!=(
2463 const independent_bits_engine<_Eng, _W, _UI>& __x,
2464 const independent_bits_engine<_Eng, _W, _UI>& __y)
2465{
2466 return !(__x == __y);
2467}
2468
2469template <class _CharT, class _Traits,
2470 class _Eng, size_t _W, class _UI>
2471basic_ostream<_CharT, _Traits>&
2472operator<<(basic_ostream<_CharT, _Traits>& __os,
2473 const independent_bits_engine<_Eng, _W, _UI>& __x)
2474{
2475 return __os << __x.base();
2476}
2477
2478template <class _CharT, class _Traits,
2479 class _Eng, size_t _W, class _UI>
2480basic_istream<_CharT, _Traits>&
2481operator>>(basic_istream<_CharT, _Traits>& __is,
2482 independent_bits_engine<_Eng, _W, _UI>& __x)
2483{
2484 _Eng __e;
2485 __is >> __e;
2486 if (!__is.fail())
2487 __x.__e_ = __e;
2488 return __is;
2489}
2490
2491// shuffle_order_engine
2492
2493template <uint64_t _Xp, uint64_t _Yp>
2494struct __ugcd
2495{
2496 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2497};
2498
2499template <uint64_t _Xp>
2500struct __ugcd<_Xp, 0>
2501{
2502 static const uint64_t value = _Xp;
2503};
2504
2505template <uint64_t _N, uint64_t _D>
2506class __uratio
2507{
2508 static_assert(_D != 0, "__uratio divide by 0");
2509 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2510public:
2511 static const uint64_t num = _N / __gcd;
2512 static const uint64_t den = _D / __gcd;
2513
2514 typedef __uratio<num, den> type;
2515};
2516
2517template<class _Engine, size_t __k>
2518class shuffle_order_engine
2519{
2520 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2521public:
2522 // types
2523 typedef typename _Engine::result_type result_type;
2524
2525private:
2526 _Engine __e_;
2527 result_type _V_[__k];
2528 result_type _Y_;
2529
2530public:
2531 // engine characteristics
2532 static const/*expr*/ size_t table_size = __k;
2533
2534 static const result_type _Min = _Engine::_Min;
2535 static const result_type _Max = _Engine::_Max;
2536 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2537 static const/*expr*/ result_type min() { return _Min; }
2538 static const/*expr*/ result_type max() { return _Max; }
2539
2540 static const unsigned long long _R = _Max - _Min + 1ull;
2541
2542 // constructors and seeding functions
2543 shuffle_order_engine() {__init();}
2544// explicit shuffle_order_engine(const _Engine& __e);
2545// explicit shuffle_order_engine(_Engine&& e);
2546 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
2547 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
2548 : __e_(__q) {__init();}
2549 void seed() {__e_.seed(); __init();}
2550 void seed(result_type __sd) {__e_.seed(__sd); __init();}
2551 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
2552
2553 // generating functions
2554 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2555 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2556
2557 // property functions
2558 const _Engine& base() const {return __e_;}
2559
2560private:
2561 template<class _Eng, size_t _K>
2562 friend
2563 bool
2564 operator==(
2565 const shuffle_order_engine<_Eng, _K>& __x,
2566 const shuffle_order_engine<_Eng, _K>& __y);
2567
2568 template<class _Eng, size_t _K>
2569 friend
2570 bool
2571 operator!=(
2572 const shuffle_order_engine<_Eng, _K>& __x,
2573 const shuffle_order_engine<_Eng, _K>& __y);
2574
2575 template <class _CharT, class _Traits,
2576 class _Eng, size_t _K>
2577 friend
2578 basic_ostream<_CharT, _Traits>&
2579 operator<<(basic_ostream<_CharT, _Traits>& __os,
2580 const shuffle_order_engine<_Eng, _K>& __x);
2581
2582 template <class _CharT, class _Traits,
2583 class _Eng, size_t _K>
2584 friend
2585 basic_istream<_CharT, _Traits>&
2586 operator>>(basic_istream<_CharT, _Traits>& __is,
2587 shuffle_order_engine<_Eng, _K>& __x);
2588
2589 void __init()
2590 {
2591 for (size_t __i = 0; __i < __k; ++__i)
2592 _V_[__i] = __e_();
2593 _Y_ = __e_();
2594 }
2595
2596 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
2597 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
2598
2599 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
2600 result_type __eval2(true_type) {return __evalf<__k, 0>();}
2601
2602 template <uint64_t _N, uint64_t _D>
2603 typename enable_if
2604 <
2605 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
2606 result_type
2607 >::type
2608 __eval(__uratio<_N, _D>)
2609 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
2610
2611 template <uint64_t _N, uint64_t _D>
2612 typename enable_if
2613 <
2614 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
2615 result_type
2616 >::type
2617 __eval(__uratio<_N, _D>)
2618 {
2619 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
2620 / __uratio<_N, _D>::den);
2621 _Y_ = _V_[__j];
2622 _V_[__j] = __e_();
2623 return _Y_;
2624 }
2625
2626 template <uint64_t __n, uint64_t __d>
2627 result_type __evalf()
2628 {
2629 const double _F = __d == 0 ?
2630 __n / (2. * 0x8000000000000000ull) :
2631 __n / (double)__d;
2632 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
2633 _Y_ = _V_[__j];
2634 _V_[__j] = __e_();
2635 return _Y_;
2636 }
2637};
2638
2639template<class _Eng, size_t _K>
2640bool
2641operator==(
2642 const shuffle_order_engine<_Eng, _K>& __x,
2643 const shuffle_order_engine<_Eng, _K>& __y)
2644{
2645 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
2646 __x.__e_ == __y.__e_;
2647}
2648
2649template<class _Eng, size_t _K>
2650inline
2651bool
2652operator!=(
2653 const shuffle_order_engine<_Eng, _K>& __x,
2654 const shuffle_order_engine<_Eng, _K>& __y)
2655{
2656 return !(__x == __y);
2657}
2658
2659template <class _CharT, class _Traits,
2660 class _Eng, size_t _K>
2661basic_ostream<_CharT, _Traits>&
2662operator<<(basic_ostream<_CharT, _Traits>& __os,
2663 const shuffle_order_engine<_Eng, _K>& __x)
2664{
2665 __save_flags<_CharT, _Traits> _(__os);
2666 __os.flags(ios_base::dec | ios_base::left);
2667 _CharT __sp = __os.widen(' ');
2668 __os.fill(__sp);
2669 __os << __x.__e_ << __sp << __x._V_[0];
2670 for (size_t __i = 1; __i < _K; ++__i)
2671 __os << __sp << __x._V_[__i];
2672 return __os << __sp << __x._Y_;
2673}
2674
2675template <class _CharT, class _Traits,
2676 class _Eng, size_t _K>
2677basic_istream<_CharT, _Traits>&
2678operator>>(basic_istream<_CharT, _Traits>& __is,
2679 shuffle_order_engine<_Eng, _K>& __x)
2680{
2681 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
2682 __save_flags<_CharT, _Traits> _(__is);
2683 __is.flags(ios_base::dec | ios_base::skipws);
2684 _Eng __e;
2685 result_type _V[_K+1];
2686 __is >> __e;
2687 for (size_t __i = 0; __i < _K+1; ++__i)
2688 __is >> _V[__i];
2689 if (!__is.fail())
2690 {
2691 __x.__e_ = __e;
2692 for (size_t __i = 0; __i < _K; ++__i)
2693 __x._V_[__i] = _V[__i];
2694 __x._Y_ = _V[_K];
2695 }
2696 return __is;
2697}
2698
2699typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
2700
2701// random_device
2702
2703class random_device
2704{
2705 int __f_;
2706public:
2707 // types
2708 typedef unsigned result_type;
2709
2710 // generator characteristics
2711 static const result_type _Min = 0;
2712 static const result_type _Max = 0xFFFFFFFFu;
2713
2714 static const/*expr*/ result_type min() { return _Min;}
2715 static const/*expr*/ result_type max() { return _Max;}
2716
2717 // constructors
2718 explicit random_device(const string& __token = "/dev/urandom");
2719 ~random_device();
2720
2721 // generating functions
2722 result_type operator()();
2723
2724 // property functions
2725 double entropy() const;
2726
2727private:
2728 // no copy functions
2729 random_device(const random_device&); // = delete;
2730 random_device& operator=(const random_device&); // = delete;
2731};
2732
2733// seed_seq
2734
2735class seed_seq
2736{
2737public:
2738 // types
2739 typedef uint32_t result_type;
2740
2741private:
2742 vector<result_type> __v_;
2743
2744 template<class _InputIterator>
2745 void init(_InputIterator __first, _InputIterator __last);
2746public:
2747 // constructors
2748 seed_seq() {}
2749 template<class _Tp>
2750 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
2751
2752 template<class _InputIterator>
2753 seed_seq(_InputIterator __first, _InputIterator __last)
2754 {init(__first, __last);}
2755
2756 // generating functions
2757 template<class _RandomAccessIterator>
2758 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
2759
2760 // property functions
2761 size_t size() const {return __v_.size();}
2762 template<class _OutputIterator>
2763 void param(_OutputIterator __dest) const
2764 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
2765
2766private:
2767 // no copy functions
2768 seed_seq(const seed_seq&); // = delete;
2769 void operator=(const seed_seq&); // = delete;
2770
2771 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
2772};
2773
2774template<class _InputIterator>
2775void
2776seed_seq::init(_InputIterator __first, _InputIterator __last)
2777{
2778 for (_InputIterator __s = __first; __s != __last; ++__s)
2779 __v_.push_back(*__s & 0xFFFFFFFF);
2780}
2781
2782template<class _RandomAccessIterator>
2783void
2784seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
2785{
2786 if (__first != __last)
2787 {
2788 _STD::fill(__first, __last, 0x8b8b8b8b);
2789 const size_t __n = static_cast<size_t>(__last - __first);
2790 const size_t __s = __v_.size();
2791 const size_t __t = (__n >= 623) ? 11
2792 : (__n >= 68) ? 7
2793 : (__n >= 39) ? 5
2794 : (__n >= 7) ? 3
2795 : (__n - 1) / 2;
2796 const size_t __p = (__n - __t) / 2;
2797 const size_t __q = __p + __t;
2798 const size_t __m = _STD::max(__s + 1, __n);
2799 // __k = 0;
2800 {
2801 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
2802 ^ __first[__n - 1]);
2803 __first[__p] += __r;
2804 __r += __s;
2805 __first[__q] += __r;
2806 __first[0] = __r;
2807 }
2808 for (size_t __k = 1; __k <= __s; ++__k)
2809 {
2810 const size_t __kmodn = __k % __n;
2811 const size_t __kpmodn = (__k + __p) % __n;
2812 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2813 ^ __first[(__k - 1) % __n]);
2814 __first[__kpmodn] += __r;
2815 __r += __kmodn + __v_[__k-1];
2816 __first[(__k + __q) % __n] += __r;
2817 __first[__kmodn] = __r;
2818 }
2819 for (size_t __k = __s + 1; __k < __m; ++__k)
2820 {
2821 const size_t __kmodn = __k % __n;
2822 const size_t __kpmodn = (__k + __p) % __n;
2823 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2824 ^ __first[(__k - 1) % __n]);
2825 __first[__kpmodn] += __r;
2826 __r += __kmodn;
2827 __first[(__k + __q) % __n] += __r;
2828 __first[__kmodn] = __r;
2829 }
2830 for (size_t __k = __m; __k < __m + __n; ++__k)
2831 {
2832 const size_t __kmodn = __k % __n;
2833 const size_t __kpmodn = (__k + __p) % __n;
2834 result_type __r = 1566083941 * _T(__first[__kmodn] +
2835 __first[__kpmodn] +
2836 __first[(__k - 1) % __n]);
2837 __first[__kpmodn] ^= __r;
2838 __r -= __kmodn;
2839 __first[(__k + __q) % __n] ^= __r;
2840 __first[__kmodn] = __r;
2841 }
2842 }
2843}
2844
Howard Hinnant30a840f2010-05-12 17:08:57 +00002845// generate_canonical
2846
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002847template<class _RealType, size_t __bits, class _URNG>
2848_RealType
2849generate_canonical(_URNG& __g)
2850{
2851 const size_t _Dt = numeric_limits<_RealType>::digits;
2852 const size_t __b = _Dt < __bits ? _Dt : __bits;
2853 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
2854 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
2855 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
2856 _RealType __base = _R;
2857 _RealType _S = __g() - _URNG::_Min;
2858 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
2859 _S += (__g() - _URNG::_Min) * __base;
2860 return _S / __base;
2861}
2862
2863// __independent_bits_engine
2864
2865template<class _Engine, class _UIntType>
2866class __independent_bits_engine
2867{
2868public:
2869 // types
2870 typedef _UIntType result_type;
2871
2872private:
2873 typedef typename _Engine::result_type _Engine_result_type;
2874 typedef typename conditional
2875 <
2876 sizeof(_Engine_result_type) <= sizeof(result_type),
2877 result_type,
2878 _Engine_result_type
2879 >::type _Working_result_type;
2880
2881 _Engine& __e_;
2882 size_t __w_;
2883 size_t __w0_;
2884 size_t __n_;
2885 size_t __n0_;
2886 _Working_result_type __y0_;
2887 _Working_result_type __y1_;
2888 _Engine_result_type __mask0_;
2889 _Engine_result_type __mask1_;
2890
2891 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2892 + _Working_result_type(1);
2893 static const size_t __m = __log2<_Working_result_type, _R>::value;
2894 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2895 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2896
2897public:
2898 // constructors and seeding functions
2899 __independent_bits_engine(_Engine& __e, size_t __w);
2900
2901 // generating functions
2902 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2903
2904private:
2905 result_type __eval(false_type);
2906 result_type __eval(true_type);
2907};
2908
2909template<class _Engine, class _UIntType>
2910__independent_bits_engine<_Engine, _UIntType>
2911 ::__independent_bits_engine(_Engine& __e, size_t __w)
2912 : __e_(__e),
2913 __w_(__w)
2914{
2915 __n_ = __w_ / __m + (__w_ % __m != 0);
2916 __w0_ = __w_ / __n_;
2917 if (_R == 0)
2918 __y0_ = _R;
2919 else if (__w0_ < _WDt)
2920 __y0_ = (_R >> __w0_) << __w0_;
2921 else
2922 __y0_ = 0;
2923 if (_R - __y0_ > __y0_ / __n_)
2924 {
2925 ++__n_;
2926 __w0_ = __w_ / __n_;
2927 if (__w0_ < _WDt)
2928 __y0_ = (_R >> __w0_) << __w0_;
2929 else
2930 __y0_ = 0;
2931 }
2932 __n0_ = __n_ - __w_ % __n_;
2933 if (__w0_ < _WDt - 1)
2934 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2935 else
2936 __y1_ = 0;
2937 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2938 _Engine_result_type(0);
2939 __mask1_ = __w0_ < _EDt - 1 ?
2940 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2941 _Engine_result_type(~0);
2942}
2943
2944template<class _Engine, class _UIntType>
2945inline
2946_UIntType
2947__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2948{
2949 return static_cast<result_type>(__e_() & __mask0_);
2950}
2951
2952template<class _Engine, class _UIntType>
2953_UIntType
2954__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2955{
2956 result_type _S = 0;
2957 for (size_t __k = 0; __k < __n0_; ++__k)
2958 {
2959 _Engine_result_type __u;
2960 do
2961 {
2962 __u = __e_() - _Engine::min();
2963 } while (__u >= __y0_);
2964 if (__w0_ < _EDt)
2965 _S <<= __w0_;
2966 else
2967 _S = 0;
2968 _S += __u & __mask0_;
2969 }
2970 for (size_t __k = __n0_; __k < __n_; ++__k)
2971 {
2972 _Engine_result_type __u;
2973 do
2974 {
2975 __u = __e_() - _Engine::min();
2976 } while (__u >= __y1_);
2977 if (__w0_ < _EDt - 1)
2978 _S <<= __w0_ + 1;
2979 else
2980 _S = 0;
2981 _S += __u & __mask1_;
2982 }
2983 return _S;
2984}
2985
Howard Hinnant30a840f2010-05-12 17:08:57 +00002986// uniform_int_distribution
2987
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002988template<class _IntType = int>
2989class uniform_int_distribution
2990{
2991public:
2992 // types
2993 typedef _IntType result_type;
2994
2995 class param_type
2996 {
2997 result_type __a_;
2998 result_type __b_;
2999 public:
3000 typedef uniform_int_distribution distribution_type;
3001
3002 explicit param_type(result_type __a = 0,
3003 result_type __b = numeric_limits<result_type>::max())
3004 : __a_(__a), __b_(__b) {}
3005
3006 result_type a() const {return __a_;}
3007 result_type b() const {return __b_;}
3008
3009 friend bool operator==(const param_type& __x, const param_type& __y)
3010 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3011 friend bool operator!=(const param_type& __x, const param_type& __y)
3012 {return !(__x == __y);}
3013 };
3014
3015private:
3016 param_type __p_;
3017
3018public:
3019 // constructors and reset functions
3020 explicit uniform_int_distribution(result_type __a = 0,
3021 result_type __b = numeric_limits<result_type>::max())
3022 : __p_(param_type(__a, __b)) {}
3023 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3024 void reset() {}
3025
3026 // generating functions
3027 template<class _URNG> result_type operator()(_URNG& __g)
3028 {return (*this)(__g, __p_);}
3029 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3030
3031 // property functions
3032 result_type a() const {return __p_.a();}
3033 result_type b() const {return __p_.b();}
3034
3035 param_type param() const {return __p_;}
3036 void param(const param_type& __p) {__p_ = __p;}
3037
3038 result_type min() const {return a();}
3039 result_type max() const {return b();}
3040
3041 friend bool operator==(const uniform_int_distribution& __x,
3042 const uniform_int_distribution& __y)
3043 {return __x.__p_ == __y.__p_;}
3044 friend bool operator!=(const uniform_int_distribution& __x,
3045 const uniform_int_distribution& __y)
3046 {return !(__x == __y);}
3047};
3048
3049template<class _IntType>
3050template<class _URNG>
3051typename uniform_int_distribution<_IntType>::result_type
3052uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3053{
3054 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3055 uint32_t, uint64_t>::type _UIntType;
3056 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
3057 if (_R == 1)
3058 return __p.a();
3059 const size_t _Dt = numeric_limits<_UIntType>::digits;
3060 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3061 if (_R == 0)
3062 return static_cast<result_type>(_Eng(__g, _Dt)());
3063 size_t __w = _Dt - __clz(_R) - 1;
3064 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
3065 ++__w;
3066 _Eng __e(__g, __w);
3067 _UIntType __u;
3068 do
3069 {
3070 __u = __e();
3071 } while (__u >= _R);
3072 return static_cast<result_type>(__u + __p.a());
3073}
3074
3075template <class _CharT, class _Traits, class _IT>
3076basic_ostream<_CharT, _Traits>&
3077operator<<(basic_ostream<_CharT, _Traits>& __os,
3078 const uniform_int_distribution<_IT>& __x)
3079{
3080 __save_flags<_CharT, _Traits> _(__os);
3081 __os.flags(ios_base::dec | ios_base::left);
3082 _CharT __sp = __os.widen(' ');
3083 __os.fill(__sp);
3084 return __os << __x.a() << __sp << __x.b();
3085}
3086
3087template <class _CharT, class _Traits, class _IT>
3088basic_istream<_CharT, _Traits>&
3089operator>>(basic_istream<_CharT, _Traits>& __is,
3090 uniform_int_distribution<_IT>& __x)
3091{
3092 typedef uniform_int_distribution<_IT> _Eng;
3093 typedef typename _Eng::result_type result_type;
3094 typedef typename _Eng::param_type param_type;
3095 __save_flags<_CharT, _Traits> _(__is);
3096 __is.flags(ios_base::dec | ios_base::skipws);
3097 result_type __a;
3098 result_type __b;
3099 __is >> __a >> __b;
3100 if (!__is.fail())
3101 __x.param(param_type(__a, __b));
3102 return __is;
3103}
3104
Howard Hinnant30a840f2010-05-12 17:08:57 +00003105// uniform_real_distribution
3106
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003107template<class _RealType = double>
3108class uniform_real_distribution
3109{
3110public:
3111 // types
3112 typedef _RealType result_type;
3113
3114 class param_type
3115 {
3116 result_type __a_;
3117 result_type __b_;
3118 public:
3119 typedef uniform_real_distribution distribution_type;
3120
3121 explicit param_type(result_type __a = 0,
3122 result_type __b = 1)
3123 : __a_(__a), __b_(__b) {}
3124
3125 result_type a() const {return __a_;}
3126 result_type b() const {return __b_;}
3127
3128 friend bool operator==(const param_type& __x, const param_type& __y)
3129 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3130 friend bool operator!=(const param_type& __x, const param_type& __y)
3131 {return !(__x == __y);}
3132 };
3133
3134private:
3135 param_type __p_;
3136
3137public:
3138 // constructors and reset functions
3139 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3140 : __p_(param_type(__a, __b)) {}
3141 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
3142 void reset() {}
3143
3144 // generating functions
3145 template<class _URNG> result_type operator()(_URNG& __g)
3146 {return (*this)(__g, __p_);}
3147 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3148
3149 // property functions
3150 result_type a() const {return __p_.a();}
3151 result_type b() const {return __p_.b();}
3152
3153 param_type param() const {return __p_;}
3154 void param(const param_type& __p) {__p_ = __p;}
3155
3156 result_type min() const {return a();}
3157 result_type max() const {return b();}
3158
3159 friend bool operator==(const uniform_real_distribution& __x,
3160 const uniform_real_distribution& __y)
3161 {return __x.__p_ == __y.__p_;}
3162 friend bool operator!=(const uniform_real_distribution& __x,
3163 const uniform_real_distribution& __y)
3164 {return !(__x == __y);}
3165};
3166
3167template<class _RealType>
3168template<class _URNG>
3169inline
3170typename uniform_real_distribution<_RealType>::result_type
3171uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3172{
3173 return (__p.b() - __p.a())
3174 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3175 + __p.a();
3176}
3177
3178template <class _CharT, class _Traits, class _RT>
3179basic_ostream<_CharT, _Traits>&
3180operator<<(basic_ostream<_CharT, _Traits>& __os,
3181 const uniform_real_distribution<_RT>& __x)
3182{
3183 __save_flags<_CharT, _Traits> _(__os);
3184 __os.flags(ios_base::dec | ios_base::left);
3185 _CharT __sp = __os.widen(' ');
3186 __os.fill(__sp);
3187 return __os << __x.a() << __sp << __x.b();
3188}
3189
3190template <class _CharT, class _Traits, class _RT>
3191basic_istream<_CharT, _Traits>&
3192operator>>(basic_istream<_CharT, _Traits>& __is,
3193 uniform_real_distribution<_RT>& __x)
3194{
3195 typedef uniform_real_distribution<_RT> _Eng;
3196 typedef typename _Eng::result_type result_type;
3197 typedef typename _Eng::param_type param_type;
3198 __save_flags<_CharT, _Traits> _(__is);
3199 __is.flags(ios_base::dec | ios_base::skipws);
3200 result_type __a;
3201 result_type __b;
3202 __is >> __a >> __b;
3203 if (!__is.fail())
3204 __x.param(param_type(__a, __b));
3205 return __is;
3206}
3207
Howard Hinnant30a840f2010-05-12 17:08:57 +00003208// bernoulli_distribution
3209
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003210class bernoulli_distribution
3211{
3212public:
3213 // types
3214 typedef bool result_type;
3215
3216 class param_type
3217 {
3218 double __p_;
3219 public:
3220 typedef bernoulli_distribution distribution_type;
3221
3222 explicit param_type(double __p = 0.5) : __p_(__p) {}
3223
3224 double p() const {return __p_;}
3225
3226 friend bool operator==(const param_type& __x, const param_type& __y)
3227 {return __x.__p_ == __y.__p_;}
3228 friend bool operator!=(const param_type& __x, const param_type& __y)
3229 {return !(__x == __y);}
3230 };
3231
3232private:
3233 param_type __p_;
3234
3235public:
3236 // constructors and reset functions
3237 explicit bernoulli_distribution(double __p = 0.5)
3238 : __p_(param_type(__p)) {}
3239 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3240 void reset() {}
3241
3242 // generating functions
3243 template<class _URNG> result_type operator()(_URNG& __g)
3244 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003245 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003246
3247 // property functions
3248 double p() const {return __p_.p();}
3249
3250 param_type param() const {return __p_;}
3251 void param(const param_type& __p) {__p_ = __p;}
3252
3253 result_type min() const {return false;}
3254 result_type max() const {return true;}
3255
3256 friend bool operator==(const bernoulli_distribution& __x,
3257 const bernoulli_distribution& __y)
3258 {return __x.__p_ == __y.__p_;}
3259 friend bool operator!=(const bernoulli_distribution& __x,
3260 const bernoulli_distribution& __y)
3261 {return !(__x == __y);}
3262};
3263
Howard Hinnant03aad812010-05-11 23:26:59 +00003264template<class _URNG>
3265inline
3266bernoulli_distribution::result_type
3267bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3268{
3269 return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.);
3270}
3271
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003272template <class _CharT, class _Traits>
3273basic_ostream<_CharT, _Traits>&
3274operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3275{
3276 __save_flags<_CharT, _Traits> _(__os);
3277 __os.flags(ios_base::dec | ios_base::left);
3278 _CharT __sp = __os.widen(' ');
3279 __os.fill(__sp);
3280 return __os << __x.p();
3281}
3282
3283template <class _CharT, class _Traits>
3284basic_istream<_CharT, _Traits>&
3285operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3286{
3287 typedef bernoulli_distribution _Eng;
3288 typedef typename _Eng::param_type param_type;
3289 __save_flags<_CharT, _Traits> _(__is);
3290 __is.flags(ios_base::dec | ios_base::skipws);
3291 double __p;
3292 __is >> __p;
3293 if (!__is.fail())
3294 __x.param(param_type(__p));
3295 return __is;
3296}
3297
Howard Hinnant30a840f2010-05-12 17:08:57 +00003298// binomial_distribution
3299
Howard Hinnant03aad812010-05-11 23:26:59 +00003300template<class _IntType = int>
3301class binomial_distribution
3302{
3303public:
3304 // types
3305 typedef _IntType result_type;
3306
3307 class param_type
3308 {
3309 result_type __t_;
3310 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003311 double __pr_;
3312 double __odds_ratio_;
3313 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003314 public:
3315 typedef binomial_distribution distribution_type;
3316
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003317 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003318
3319 result_type t() const {return __t_;}
3320 double p() const {return __p_;}
3321
3322 friend bool operator==(const param_type& __x, const param_type& __y)
3323 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3324 friend bool operator!=(const param_type& __x, const param_type& __y)
3325 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003326
3327 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003328 };
3329
3330private:
3331 param_type __p_;
3332
3333public:
3334 // constructors and reset functions
3335 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3336 : __p_(param_type(__t, __p)) {}
3337 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3338 void reset() {}
3339
3340 // generating functions
3341 template<class _URNG> result_type operator()(_URNG& __g)
3342 {return (*this)(__g, __p_);}
3343 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3344
3345 // property functions
3346 result_type t() const {return __p_.t();}
3347 double p() const {return __p_.p();}
3348
3349 param_type param() const {return __p_;}
3350 void param(const param_type& __p) {__p_ = __p;}
3351
3352 result_type min() const {return 0;}
3353 result_type max() const {return t();}
3354
3355 friend bool operator==(const binomial_distribution& __x,
3356 const binomial_distribution& __y)
3357 {return __x.__p_ == __y.__p_;}
3358 friend bool operator!=(const binomial_distribution& __x,
3359 const binomial_distribution& __y)
3360 {return !(__x == __y);}
3361};
3362
3363template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003364binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3365 : __t_(__t), __p_(__p)
3366{
3367 if (0 < __p_ && __p_ < 1)
3368 {
3369 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3370 __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) -
3371 _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) +
3372 (__t_ - __r0_) * _STD::log(1 - __p_));
3373 __odds_ratio_ = __p_ / (1 - __p_);
3374 }
3375}
3376
3377template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003378template<class _URNG>
3379_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003380binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003381{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003382 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3383 return 0;
3384 if (__pr.__p_ == 1)
3385 return __pr.__t_;
3386 uniform_real_distribution<double> __gen;
3387 double __u = __gen(__g) - __pr.__pr_;
3388 if (__u < 0)
3389 return __pr.__r0_;
3390 double __pu = __pr.__pr_;
3391 double __pd = __pu;
3392 result_type __ru = __pr.__r0_;
3393 result_type __rd = __ru;
3394 while (true)
3395 {
3396 if (__rd >= 1)
3397 {
3398 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3399 __u -= __pd;
3400 if (__u < 0)
3401 return __rd - 1;
3402 }
3403 --__rd;
3404 ++__ru;
3405 if (__ru <= __pr.__t_)
3406 {
3407 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3408 __u -= __pu;
3409 if (__u < 0)
3410 return __ru;
3411 }
3412 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003413}
3414
3415template <class _CharT, class _Traits, class _IntType>
3416basic_ostream<_CharT, _Traits>&
3417operator<<(basic_ostream<_CharT, _Traits>& __os,
3418 const binomial_distribution<_IntType>& __x)
3419{
3420 __save_flags<_CharT, _Traits> _(__os);
3421 __os.flags(ios_base::dec | ios_base::left);
3422 _CharT __sp = __os.widen(' ');
3423 __os.fill(__sp);
3424 return __os << __x.t() << __sp << __x.p();
3425}
3426
3427template <class _CharT, class _Traits, class _IntType>
3428basic_istream<_CharT, _Traits>&
3429operator>>(basic_istream<_CharT, _Traits>& __is,
3430 binomial_distribution<_IntType>& __x)
3431{
3432 typedef binomial_distribution<_IntType> _Eng;
3433 typedef typename _Eng::result_type result_type;
3434 typedef typename _Eng::param_type param_type;
3435 __save_flags<_CharT, _Traits> _(__is);
3436 __is.flags(ios_base::dec | ios_base::skipws);
3437 result_type __t;
3438 double __p;
3439 __is >> __t >> __p;
3440 if (!__is.fail())
3441 __x.param(param_type(__t, __p));
3442 return __is;
3443}
3444
Howard Hinnant30a840f2010-05-12 17:08:57 +00003445// exponential_distribution
3446
3447template<class _RealType = double>
3448class exponential_distribution
3449{
3450public:
3451 // types
3452 typedef _RealType result_type;
3453
3454 class param_type
3455 {
3456 result_type __lambda_;
3457 public:
3458 typedef exponential_distribution distribution_type;
3459
3460 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3461
3462 result_type lambda() const {return __lambda_;}
3463
3464 friend bool operator==(const param_type& __x, const param_type& __y)
3465 {return __x.__lambda_ == __y.__lambda_;}
3466 friend bool operator!=(const param_type& __x, const param_type& __y)
3467 {return !(__x == __y);}
3468 };
3469
3470private:
3471 param_type __p_;
3472
3473public:
3474 // constructors and reset functions
3475 explicit exponential_distribution(result_type __lambda = 1)
3476 : __p_(param_type(__lambda)) {}
3477 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3478 void reset() {}
3479
3480 // generating functions
3481 template<class _URNG> result_type operator()(_URNG& __g)
3482 {return (*this)(__g, __p_);}
3483 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3484
3485 // property functions
3486 result_type lambda() const {return __p_.lambda();}
3487
3488 param_type param() const {return __p_;}
3489 void param(const param_type& __p) {__p_ = __p;}
3490
3491 result_type min() const {return 0;}
Howard Hinnantdf40dc62010-05-16 17:56:20 +00003492 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00003493
3494 friend bool operator==(const exponential_distribution& __x,
3495 const exponential_distribution& __y)
3496 {return __x.__p_ == __y.__p_;}
3497 friend bool operator!=(const exponential_distribution& __x,
3498 const exponential_distribution& __y)
3499 {return !(__x == __y);}
3500};
3501
3502template <class _RealType>
3503template<class _URNG>
3504_RealType
3505exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3506{
3507 return -_STD::log
3508 (
3509 result_type(1) -
3510 _STD::generate_canonical<result_type,
3511 numeric_limits<result_type>::digits>(__g)
3512 )
3513 / __p.lambda();
3514}
3515
3516template <class _CharT, class _Traits, class _RealType>
3517basic_ostream<_CharT, _Traits>&
3518operator<<(basic_ostream<_CharT, _Traits>& __os,
3519 const exponential_distribution<_RealType>& __x)
3520{
3521 __save_flags<_CharT, _Traits> _(__os);
3522 __os.flags(ios_base::dec | ios_base::left);
3523 return __os << __x.lambda();
3524}
3525
3526template <class _CharT, class _Traits, class _RealType>
3527basic_istream<_CharT, _Traits>&
3528operator>>(basic_istream<_CharT, _Traits>& __is,
3529 exponential_distribution<_RealType>& __x)
3530{
3531 typedef exponential_distribution<_RealType> _Eng;
3532 typedef typename _Eng::result_type result_type;
3533 typedef typename _Eng::param_type param_type;
3534 __save_flags<_CharT, _Traits> _(__is);
3535 __is.flags(ios_base::dec | ios_base::skipws);
3536 result_type __lambda;
3537 __is >> __lambda;
3538 if (!__is.fail())
3539 __x.param(param_type(__lambda));
3540 return __is;
3541}
3542
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003543// normal_distribution
3544
3545template<class _RealType = double>
3546class normal_distribution
3547{
3548public:
3549 // types
3550 typedef _RealType result_type;
3551
3552 class param_type
3553 {
3554 result_type __mean_;
3555 result_type __stddev_;
3556 public:
3557 typedef normal_distribution distribution_type;
3558
3559 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
3560 : __mean_(__mean), __stddev_(__stddev) {}
3561
3562 result_type mean() const {return __mean_;}
3563 result_type stddev() const {return __stddev_;}
3564
3565 friend bool operator==(const param_type& __x, const param_type& __y)
3566 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
3567 friend bool operator!=(const param_type& __x, const param_type& __y)
3568 {return !(__x == __y);}
3569 };
3570
3571private:
3572 param_type __p_;
3573 result_type _V_;
3574 bool _V_hot_;
3575
3576public:
3577 // constructors and reset functions
3578 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
3579 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
3580 explicit normal_distribution(const param_type& __p)
3581 : __p_(__p), _V_hot_(false) {}
3582 void reset() {_V_hot_ = false;}
3583
3584 // generating functions
3585 template<class _URNG> result_type operator()(_URNG& __g)
3586 {return (*this)(__g, __p_);}
3587 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3588
3589 // property functions
3590 result_type mean() const {return __p_.mean();}
3591 result_type stddev() const {return __p_.stddev();}
3592
3593 param_type param() const {return __p_;}
3594 void param(const param_type& __p) {__p_ = __p;}
3595
3596 result_type min() const {return -numeric_limits<result_type>::infinity();}
3597 result_type max() const {return numeric_limits<result_type>::infinity();}
3598
3599 friend bool operator==(const normal_distribution& __x,
3600 const normal_distribution& __y)
3601 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
3602 (!__x._V_hot_ || __x._V_ == __y._V_);}
3603 friend bool operator!=(const normal_distribution& __x,
3604 const normal_distribution& __y)
3605 {return !(__x == __y);}
3606
3607 template <class _CharT, class _Traits, class _RT>
3608 friend
3609 basic_ostream<_CharT, _Traits>&
3610 operator<<(basic_ostream<_CharT, _Traits>& __os,
3611 const normal_distribution<_RT>& __x);
3612
3613 template <class _CharT, class _Traits, class _RT>
3614 friend
3615 basic_istream<_CharT, _Traits>&
3616 operator>>(basic_istream<_CharT, _Traits>& __is,
3617 normal_distribution<_RT>& __x);
3618};
3619
3620template <class _RealType>
3621template<class _URNG>
3622_RealType
3623normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3624{
3625 result_type _U;
3626 if (_V_hot_)
3627 {
3628 _V_hot_ = false;
3629 _U = _V_;
3630 }
3631 else
3632 {
3633 uniform_real_distribution<result_type> _Uni(-1, 1);
3634 result_type __u;
3635 result_type __v;
3636 result_type __s;
3637 do
3638 {
3639 __u = _Uni(__g);
3640 __v = _Uni(__g);
3641 __s = __u * __u + __v * __v;
3642 } while (__s > 1 || __s == 0);
3643 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
3644 _V_ = __v * _F;
3645 _V_hot_ = true;
3646 _U = __u * _F;
3647 }
3648 return _U * __p.stddev() + __p.mean();
3649}
3650
3651template <class _CharT, class _Traits, class _RT>
3652basic_ostream<_CharT, _Traits>&
3653operator<<(basic_ostream<_CharT, _Traits>& __os,
3654 const normal_distribution<_RT>& __x)
3655{
3656 __save_flags<_CharT, _Traits> _(__os);
3657 __os.flags(ios_base::dec | ios_base::left);
3658 _CharT __sp = __os.widen(' ');
3659 __os.fill(__sp);
3660 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
3661 if (__x._V_hot_)
3662 __os << __sp << __x._V_;
3663 return __os;
3664}
3665
3666template <class _CharT, class _Traits, class _RT>
3667basic_istream<_CharT, _Traits>&
3668operator>>(basic_istream<_CharT, _Traits>& __is,
3669 normal_distribution<_RT>& __x)
3670{
3671 typedef normal_distribution<_RT> _Eng;
3672 typedef typename _Eng::result_type result_type;
3673 typedef typename _Eng::param_type param_type;
3674 __save_flags<_CharT, _Traits> _(__is);
3675 __is.flags(ios_base::dec | ios_base::skipws);
3676 result_type __mean;
3677 result_type __stddev;
3678 result_type _V = 0;
3679 bool _V_hot = false;
3680 __is >> __mean >> __stddev >> _V_hot;
3681 if (_V_hot)
3682 __is >> _V;
3683 if (!__is.fail())
3684 {
3685 __x.param(param_type(__mean, __stddev));
3686 __x._V_hot_ = _V_hot;
3687 __x._V_ = _V;
3688 }
3689 return __is;
3690}
3691
3692// poisson_distribution
3693
3694template<class _IntType = int>
3695class poisson_distribution
3696{
3697public:
3698 // types
3699 typedef _IntType result_type;
3700
3701 class param_type
3702 {
3703 double __mean_;
3704 double __s_;
3705 double __d_;
3706 double __l_;
3707 double __omega_;
3708 double __c0_;
3709 double __c1_;
3710 double __c2_;
3711 double __c3_;
3712 double __c_;
3713
3714 public:
3715 typedef poisson_distribution distribution_type;
3716
3717 explicit param_type(double __mean = 1.0);
3718
3719 double mean() const {return __mean_;}
3720
3721 friend bool operator==(const param_type& __x, const param_type& __y)
3722 {return __x.__mean_ == __y.__mean_;}
3723 friend bool operator!=(const param_type& __x, const param_type& __y)
3724 {return !(__x == __y);}
3725
3726 friend class poisson_distribution;
3727 };
3728
3729private:
3730 param_type __p_;
3731
3732public:
3733 // constructors and reset functions
3734 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
3735 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
3736 void reset() {}
3737
3738 // generating functions
3739 template<class _URNG> result_type operator()(_URNG& __g)
3740 {return (*this)(__g, __p_);}
3741 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3742
3743 // property functions
3744 double mean() const {return __p_.mean();}
3745
3746 param_type param() const {return __p_;}
3747 void param(const param_type& __p) {__p_ = __p;}
3748
3749 result_type min() const {return 0;}
3750 result_type max() const {return numeric_limits<result_type>::max();}
3751
3752 friend bool operator==(const poisson_distribution& __x,
3753 const poisson_distribution& __y)
3754 {return __x.__p_ == __y.__p_;}
3755 friend bool operator!=(const poisson_distribution& __x,
3756 const poisson_distribution& __y)
3757 {return !(__x == __y);}
3758};
3759
3760template<class _IntType>
3761poisson_distribution<_IntType>::param_type::param_type(double __mean)
3762 : __mean_(__mean)
3763{
3764 if (__mean_ < 10)
3765 {
3766 __s_ = 0;
3767 __d_ = 0;
3768 __l_ = _STD::exp(-__mean_);
3769 __omega_ = 0;
3770 __c3_ = 0;
3771 __c2_ = 0;
3772 __c1_ = 0;
3773 __c0_ = 0;
3774 __c_ = 0;
3775 }
3776 else
3777 {
3778 __s_ = _STD::sqrt(__mean_);
3779 __d_ = 6 * __mean_ * __mean_;
3780 __l_ = static_cast<result_type>(__mean_ - 1.1484);
3781 __omega_ = .3989423 / __s_;
3782 double __b1_ = .4166667E-1 / __mean_;
3783 double __b2_ = .3 * __b1_ * __b1_;
3784 __c3_ = .1428571 * __b1_ * __b2_;
3785 __c2_ = __b2_ - 15. * __c3_;
3786 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
3787 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
3788 __c_ = .1069 / __mean_;
3789 }
3790}
3791
3792template <class _IntType>
3793template<class _URNG>
3794_IntType
3795poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
3796{
3797 result_type __x;
3798 uniform_real_distribution<double> __urd;
3799 if (__pr.__mean_ <= 10)
3800 {
3801 __x = 0;
3802 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
3803 __p *= __urd(__urng);
3804 }
3805 else
3806 {
3807 double __difmuk;
3808 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
3809 double __u;
3810 if (__g > 0)
3811 {
3812 __x = static_cast<result_type>(__g);
3813 if (__x >= __pr.__l_)
3814 return __x;
3815 __difmuk = __pr.__mean_ - __x;
3816 __u = __urd(__urng);
3817 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
3818 return __x;
3819 }
3820 exponential_distribution<double> __edist;
3821 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
3822 {
3823 double __e;
3824 if (__using_exp_dist || __g < 0)
3825 {
3826 double __t;
3827 do
3828 {
3829 __e = __edist(__urng);
3830 __u = __urd(__urng);
3831 __u += __u - 1;
3832 __t = 1.8 + (__u < 0 ? -__e : __e);
3833 } while (__t <= -.6744);
3834 __x = __pr.__mean_ + __pr.__s_ * __t;
3835 __difmuk = __pr.__mean_ - __x;
3836 __using_exp_dist = true;
3837 }
3838 double __px;
3839 double __py;
3840 if (__x < 10)
3841 {
3842 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
3843 40320, 362880};
3844 __px = -__pr.__mean_;
3845 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x];
3846 }
3847 else
3848 {
3849 double __del = .8333333E-1 / __x;
3850 __del -= 4.8 * __del * __del * __del;
3851 double __v = __difmuk / __x;
3852 if (_STD::abs(__v) > 0.25)
3853 __px = __x * _STD::log(1 + __v) - __difmuk - __del;
3854 else
3855 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
3856 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
3857 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
3858 __py = .3989423 / _STD::sqrt(__x);
3859 }
3860 double __r = (0.5 - __difmuk) / __pr.__s_;
3861 double __r2 = __r * __r;
3862 double __fx = -0.5 * __r2;
3863 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
3864 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
3865 if (__using_exp_dist)
3866 {
3867 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) -
3868 __fy * _STD::exp(__fx + __e))
3869 break;
3870 }
3871 else
3872 {
3873 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx))
3874 break;
3875 }
3876 }
3877 }
3878 return __x;
3879}
3880
3881template <class _CharT, class _Traits, class _IntType>
3882basic_ostream<_CharT, _Traits>&
3883operator<<(basic_ostream<_CharT, _Traits>& __os,
3884 const poisson_distribution<_IntType>& __x)
3885{
3886 __save_flags<_CharT, _Traits> _(__os);
3887 __os.flags(ios_base::dec | ios_base::left);
3888 return __os << __x.mean();
3889}
3890
3891template <class _CharT, class _Traits, class _IntType>
3892basic_istream<_CharT, _Traits>&
3893operator>>(basic_istream<_CharT, _Traits>& __is,
3894 poisson_distribution<_IntType>& __x)
3895{
3896 typedef poisson_distribution<_IntType> _Eng;
3897 typedef typename _Eng::param_type param_type;
3898 __save_flags<_CharT, _Traits> _(__is);
3899 __is.flags(ios_base::dec | ios_base::skipws);
3900 double __mean;
3901 __is >> __mean;
3902 if (!__is.fail())
3903 __x.param(param_type(__mean));
3904 return __is;
3905}
3906
Howard Hinnant9de6e302010-05-16 01:09:02 +00003907// weibull_distribution
3908
3909template<class _RealType = double>
3910class weibull_distribution
3911{
3912public:
3913 // types
3914 typedef _RealType result_type;
3915
3916 class param_type
3917 {
3918 result_type __a_;
3919 result_type __b_;
3920 public:
3921 typedef weibull_distribution distribution_type;
3922
3923 explicit param_type(result_type __a = 1, result_type __b = 1)
3924 : __a_(__a), __b_(__b) {}
3925
3926 result_type a() const {return __a_;}
3927 result_type b() const {return __b_;}
3928
3929 friend bool operator==(const param_type& __x, const param_type& __y)
3930 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3931 friend bool operator!=(const param_type& __x, const param_type& __y)
3932 {return !(__x == __y);}
3933 };
3934
3935private:
3936 param_type __p_;
3937
3938public:
3939 // constructor and reset functions
3940 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
3941 : __p_(param_type(__a, __b)) {}
3942 explicit weibull_distribution(const param_type& __p)
3943 : __p_(__p) {}
3944 void reset() {}
3945
3946 // generating functions
3947 template<class _URNG> result_type operator()(_URNG& __g)
3948 {return (*this)(__g, __p_);}
3949 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
3950 {return __p.b() *
3951 _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
3952
3953 // property functions
3954 result_type a() const {return __p_.a();}
3955 result_type b() const {return __p_.b();}
3956
3957 param_type param() const {return __p_;}
3958 void param(const param_type& __p) {__p_ = __p;}
3959
3960 result_type min() const {return 0;}
3961 result_type max() const {return numeric_limits<result_type>::infinity();}
3962
3963
3964 friend bool operator==(const weibull_distribution& __x,
3965 const weibull_distribution& __y)
3966 {return __x.__p_ == __y.__p_;}
3967 friend bool operator!=(const weibull_distribution& __x,
3968 const weibull_distribution& __y)
3969 {return !(__x == __y);}
3970};
3971
3972template <class _CharT, class _Traits, class _RT>
3973basic_ostream<_CharT, _Traits>&
3974operator<<(basic_ostream<_CharT, _Traits>& __os,
3975 const weibull_distribution<_RT>& __x)
3976{
3977 __save_flags<_CharT, _Traits> _(__os);
3978 __os.flags(ios_base::dec | ios_base::left);
3979 _CharT __sp = __os.widen(' ');
3980 __os.fill(__sp);
3981 __os << __x.a() << __sp << __x.b();
3982 return __os;
3983}
3984
3985template <class _CharT, class _Traits, class _RT>
3986basic_istream<_CharT, _Traits>&
3987operator>>(basic_istream<_CharT, _Traits>& __is,
3988 weibull_distribution<_RT>& __x)
3989{
3990 typedef weibull_distribution<_RT> _Eng;
3991 typedef typename _Eng::result_type result_type;
3992 typedef typename _Eng::param_type param_type;
3993 __save_flags<_CharT, _Traits> _(__is);
3994 __is.flags(ios_base::dec | ios_base::skipws);
3995 result_type __a;
3996 result_type __b;
3997 __is >> __a >> __b;
3998 if (!__is.fail())
3999 __x.param(param_type(__a, __b));
4000 return __is;
4001}
4002
Howard Hinnantc7c49132010-05-13 17:58:28 +00004003// gamma_distribution
4004
4005template<class _RealType = double>
4006class gamma_distribution
4007{
4008public:
4009 // types
4010 typedef _RealType result_type;
4011
4012 class param_type
4013 {
4014 result_type __alpha_;
4015 result_type __beta_;
4016 public:
4017 typedef gamma_distribution distribution_type;
4018
4019 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4020 : __alpha_(__alpha), __beta_(__beta) {}
4021
4022 result_type alpha() const {return __alpha_;}
4023 result_type beta() const {return __beta_;}
4024
4025 friend bool operator==(const param_type& __x, const param_type& __y)
4026 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
4027 friend bool operator!=(const param_type& __x, const param_type& __y)
4028 {return !(__x == __y);}
4029 };
4030
4031private:
4032 param_type __p_;
4033
4034public:
4035 // constructors and reset functions
4036 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4037 : __p_(param_type(__alpha, __beta)) {}
4038 explicit gamma_distribution(const param_type& __p)
4039 : __p_(__p) {}
4040 void reset() {}
4041
4042 // generating functions
4043 template<class _URNG> result_type operator()(_URNG& __g)
4044 {return (*this)(__g, __p_);}
4045 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4046
4047 // property functions
4048 result_type alpha() const {return __p_.alpha();}
4049 result_type beta() const {return __p_.beta();}
4050
4051 param_type param() const {return __p_;}
4052 void param(const param_type& __p) {__p_ = __p;}
4053
4054 result_type min() const {return 0;}
4055 result_type max() const {return numeric_limits<result_type>::infinity();}
4056
4057 friend bool operator==(const gamma_distribution& __x,
4058 const gamma_distribution& __y)
4059 {return __x.__p_ == __y.__p_;}
4060 friend bool operator!=(const gamma_distribution& __x,
4061 const gamma_distribution& __y)
4062 {return !(__x == __y);}
4063};
4064
4065template <class _RealType>
4066template<class _URNG>
4067_RealType
4068gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4069{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004070 result_type __a = __p.alpha();
4071 uniform_real_distribution<result_type> __gen(0, 1);
4072 exponential_distribution<result_type> __egen;
4073 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004074 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004075 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004076 else if (__a > 1)
4077 {
4078 const result_type __b = __a - 1;
4079 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004080 while (true)
4081 {
4082 const result_type __u = __gen(__g);
4083 const result_type __v = __gen(__g);
4084 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004085 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004086 {
4087 const result_type __y = _STD::sqrt(__c / __w) *
4088 (__u - result_type(0.5));
4089 __x = __b + __y;
4090 if (__x >= 0)
4091 {
4092 const result_type __z = 64 * __w * __w * __w * __v * __v;
4093 if (__z <= 1 - 2 * __y * __y / __x)
4094 break;
4095 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
4096 break;
4097 }
4098 }
4099 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004100 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004101 else // __a < 1
4102 {
4103 while (true)
4104 {
4105 const result_type __u = __gen(__g);
4106 const result_type __es = __egen(__g);
4107 if (__u <= 1 - __a)
4108 {
4109 __x = _STD::pow(__u, 1 / __a);
4110 if (__x <= __es)
4111 break;
4112 }
4113 else
4114 {
4115 const result_type __e = -_STD::log((1-__u)/__a);
4116 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
4117 if (__x <= __e + __es)
4118 break;
4119 }
4120 }
4121 }
4122 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004123}
4124
4125template <class _CharT, class _Traits, class _RT>
4126basic_ostream<_CharT, _Traits>&
4127operator<<(basic_ostream<_CharT, _Traits>& __os,
4128 const gamma_distribution<_RT>& __x)
4129{
4130 __save_flags<_CharT, _Traits> _(__os);
4131 __os.flags(ios_base::dec | ios_base::left);
4132 _CharT __sp = __os.widen(' ');
4133 __os.fill(__sp);
4134 __os << __x.alpha() << __sp << __x.beta();
4135 return __os;
4136}
4137
4138template <class _CharT, class _Traits, class _RT>
4139basic_istream<_CharT, _Traits>&
4140operator>>(basic_istream<_CharT, _Traits>& __is,
4141 gamma_distribution<_RT>& __x)
4142{
4143 typedef gamma_distribution<_RT> _Eng;
4144 typedef typename _Eng::result_type result_type;
4145 typedef typename _Eng::param_type param_type;
4146 __save_flags<_CharT, _Traits> _(__is);
4147 __is.flags(ios_base::dec | ios_base::skipws);
4148 result_type __alpha;
4149 result_type __beta;
4150 __is >> __alpha >> __beta;
4151 if (!__is.fail())
4152 __x.param(param_type(__alpha, __beta));
4153 return __is;
4154}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004155
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004156// negative_binomial_distribution
4157
4158template<class _IntType = int>
4159class negative_binomial_distribution
4160{
4161public:
4162 // types
4163 typedef _IntType result_type;
4164
4165 class param_type
4166 {
4167 result_type __k_;
4168 double __p_;
4169 public:
4170 typedef negative_binomial_distribution distribution_type;
4171
4172 explicit param_type(result_type __k = 1, double __p = 0.5)
4173 : __k_(__k), __p_(__p) {}
4174
4175 result_type k() const {return __k_;}
4176 double p() const {return __p_;}
4177
4178 friend bool operator==(const param_type& __x, const param_type& __y)
4179 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
4180 friend bool operator!=(const param_type& __x, const param_type& __y)
4181 {return !(__x == __y);}
4182 };
4183
4184private:
4185 param_type __p_;
4186
4187public:
4188 // constructor and reset functions
4189 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
4190 : __p_(__k, __p) {}
4191 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
4192 void reset() {}
4193
4194 // generating functions
4195 template<class _URNG> result_type operator()(_URNG& __g)
4196 {return (*this)(__g, __p_);}
4197 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4198
4199 // property functions
4200 result_type k() const {return __p_.k();}
4201 double p() const {return __p_.p();}
4202
4203 param_type param() const {return __p_;}
4204 void param(const param_type& __p) {__p_ = __p;}
4205
4206 result_type min() const {return 0;}
4207 result_type max() const {return numeric_limits<result_type>::max();}
4208
4209 friend bool operator==(const negative_binomial_distribution& __x,
4210 const negative_binomial_distribution& __y)
4211 {return __x.__p_ == __y.__p_;}
4212 friend bool operator!=(const negative_binomial_distribution& __x,
4213 const negative_binomial_distribution& __y)
4214 {return !(__x == __y);}
4215};
4216
4217template <class _IntType>
4218template<class _URNG>
4219_IntType
4220negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4221{
4222 result_type __k = __pr.k();
4223 double __p = __pr.p();
4224 if (__k <= 21 * __p)
4225 {
4226 bernoulli_distribution __gen(__p);
4227 result_type __f = 0;
4228 result_type __s = 0;
4229 while (__s < __k)
4230 {
4231 if (__gen(__urng))
4232 ++__s;
4233 else
4234 ++__f;
4235 }
4236 return __f;
4237 }
4238 return poisson_distribution<result_type>(gamma_distribution<double>
4239 (__k, (1-__p)/__p)(__urng))(__urng);
4240}
4241
4242template <class _CharT, class _Traits, class _IntType>
4243basic_ostream<_CharT, _Traits>&
4244operator<<(basic_ostream<_CharT, _Traits>& __os,
4245 const negative_binomial_distribution<_IntType>& __x)
4246{
4247 __save_flags<_CharT, _Traits> _(__os);
4248 __os.flags(ios_base::dec | ios_base::left);
4249 _CharT __sp = __os.widen(' ');
4250 __os.fill(__sp);
4251 return __os << __x.k() << __sp << __x.p();
4252}
4253
4254template <class _CharT, class _Traits, class _IntType>
4255basic_istream<_CharT, _Traits>&
4256operator>>(basic_istream<_CharT, _Traits>& __is,
4257 negative_binomial_distribution<_IntType>& __x)
4258{
4259 typedef negative_binomial_distribution<_IntType> _Eng;
4260 typedef typename _Eng::result_type result_type;
4261 typedef typename _Eng::param_type param_type;
4262 __save_flags<_CharT, _Traits> _(__is);
4263 __is.flags(ios_base::dec | ios_base::skipws);
4264 result_type __k;
4265 double __p;
4266 __is >> __k >> __p;
4267 if (!__is.fail())
4268 __x.param(param_type(__k, __p));
4269 return __is;
4270}
4271
Howard Hinnant97dc2f32010-05-15 23:36:00 +00004272// chi_squared_distribution
4273
4274template<class _RealType = double>
4275class chi_squared_distribution
4276{
4277public:
4278 // types
4279 typedef _RealType result_type;
4280
4281 class param_type
4282 {
4283 result_type __n_;
4284 public:
4285 typedef chi_squared_distribution distribution_type;
4286
4287 explicit param_type(result_type __n = 1) : __n_(__n) {}
4288
4289 result_type n() const {return __n_;}
4290
4291 friend bool operator==(const param_type& __x, const param_type& __y)
4292 {return __x.__n_ == __y.__n_;}
4293 friend bool operator!=(const param_type& __x, const param_type& __y)
4294 {return !(__x == __y);}
4295 };
4296
4297private:
4298 param_type __p_;
4299
4300public:
4301 // constructor and reset functions
4302 explicit chi_squared_distribution(result_type __n = 1)
4303 : __p_(param_type(__n)) {}
4304 explicit chi_squared_distribution(const param_type& __p)
4305 : __p_(__p) {}
4306 void reset() {}
4307
4308 // generating functions
4309 template<class _URNG> result_type operator()(_URNG& __g)
4310 {return (*this)(__g, __p_);}
4311 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4312 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
4313
4314 // property functions
4315 result_type n() const {return __p_.n();}
4316
4317 param_type param() const {return __p_;}
4318 void param(const param_type& __p) {__p_ = __p;}
4319
4320 result_type min() const {return 0;}
4321 result_type max() const {return numeric_limits<result_type>::infinity();}
4322
4323
4324 friend bool operator==(const chi_squared_distribution& __x,
4325 const chi_squared_distribution& __y)
4326 {return __x.__p_ == __y.__p_;}
4327 friend bool operator!=(const chi_squared_distribution& __x,
4328 const chi_squared_distribution& __y)
4329 {return !(__x == __y);}
4330};
4331
4332template <class _CharT, class _Traits, class _RT>
4333basic_ostream<_CharT, _Traits>&
4334operator<<(basic_ostream<_CharT, _Traits>& __os,
4335 const chi_squared_distribution<_RT>& __x)
4336{
4337 __save_flags<_CharT, _Traits> _(__os);
4338 __os.flags(ios_base::dec | ios_base::left);
4339 __os << __x.n();
4340 return __os;
4341}
4342
4343template <class _CharT, class _Traits, class _RT>
4344basic_istream<_CharT, _Traits>&
4345operator>>(basic_istream<_CharT, _Traits>& __is,
4346 chi_squared_distribution<_RT>& __x)
4347{
4348 typedef chi_squared_distribution<_RT> _Eng;
4349 typedef typename _Eng::result_type result_type;
4350 typedef typename _Eng::param_type param_type;
4351 __save_flags<_CharT, _Traits> _(__is);
4352 __is.flags(ios_base::dec | ios_base::skipws);
4353 result_type __n;
4354 __is >> __n;
4355 if (!__is.fail())
4356 __x.param(param_type(__n));
4357 return __is;
4358}
4359
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004360_LIBCPP_END_NAMESPACE_STD
4361
4362#endif // _LIBCPP_RANDOM