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