blob: adefa8b93045242f071b1650e3a88ff89c2fde13 [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;
Howard Hinnantd6d11712010-05-20 15:11:46 +0000374typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375
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:
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001308 typedef fisher_f_distribution distribution_type;
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00001309
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>
Howard Hinnant321b4bb2010-05-18 20:08:04 +00001357class student_t_distribution
1358{
1359public:
1360 // types
1361 typedef RealType result_type;
1362
1363 class param_type
1364 {
1365 public:
1366 typedef student_t_distribution distribution_type;
1367
1368 explicit param_type(result_type n = 1);
1369
1370 result_type n() const;
1371
1372 friend bool operator==(const param_type& x, const param_type& y);
1373 friend bool operator!=(const param_type& x, const param_type& y);
1374 };
1375
1376 // constructor and reset functions
1377 explicit student_t_distribution(result_type n = 1);
1378 explicit student_t_distribution(const param_type& parm);
1379 void reset();
1380
1381 // generating functions
1382 template<class URNG> result_type operator()(URNG& g);
1383 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1384
1385 // property functions
1386 result_type n() const;
1387
1388 param_type param() const;
1389 void param(const param_type& parm);
1390
1391 result_type min() const;
1392 result_type max() const;
1393
1394 friend bool operator==(const student_t_distribution& x,
1395 const student_t_distribution& y);
1396 friend bool operator!=(const student_t_distribution& x,
1397 const student_t_distribution& y);
1398
1399 template <class charT, class traits>
1400 friend
1401 basic_ostream<charT, traits>&
1402 operator<<(basic_ostream<charT, traits>& os,
1403 const student_t_distribution& x);
1404
1405 template <class charT, class traits>
1406 friend
1407 basic_istream<charT, traits>&
1408 operator>>(basic_istream<charT, traits>& is,
1409 student_t_distribution& x);
1410};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411
1412template<class IntType = int>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001413class discrete_distribution
1414{
1415public:
1416 // types
1417 typedef IntType result_type;
1418
1419 class param_type
1420 {
1421 public:
1422 typedef discrete_distribution distribution_type;
1423
1424 param_type();
1425 template<class InputIterator>
1426 param_type(InputIterator firstW, InputIterator lastW);
1427 param_type(initializer_list<double> wl);
1428 template<class UnaryOperation>
1429 param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1430
1431 vector<double> probabilities() const;
1432
1433 friend bool operator==(const param_type& x, const param_type& y);
1434 friend bool operator!=(const param_type& x, const param_type& y);
1435 };
1436
1437 // constructor and reset functions
1438 discrete_distribution();
1439 template<class InputIterator>
1440 discrete_distribution(InputIterator firstW, InputIterator lastW);
1441 discrete_distribution(initializer_list<double> wl);
1442 template<class UnaryOperation>
1443 discrete_distribution(size_t nw, double xmin, double xmax,
1444 UnaryOperation fw);
1445 explicit discrete_distribution(const param_type& parm);
1446 void reset();
1447
1448 // generating functions
1449 template<class URNG> result_type operator()(URNG& g);
1450 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1451
1452 // property functions
1453 vector<double> probabilities() const;
1454
1455 param_type param() const;
1456 void param(const param_type& parm);
1457
1458 result_type min() const;
1459 result_type max() const;
1460
1461 friend bool operator==(const discrete_distribution& x,
1462 const discrete_distribution& y);
1463 friend bool operator!=(const discrete_distribution& x,
1464 const discrete_distribution& y);
1465
1466 template <class charT, class traits>
1467 friend
1468 basic_ostream<charT, traits>&
1469 operator<<(basic_ostream<charT, traits>& os,
1470 const discrete_distribution& x);
1471
1472 template <class charT, class traits>
1473 friend
1474 basic_istream<charT, traits>&
1475 operator>>(basic_istream<charT, traits>& is,
1476 discrete_distribution& x);
1477};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478
1479template<class RealType = double>
Howard Hinnantd6d11712010-05-20 15:11:46 +00001480class piecewise_constant_distribution
1481{
1482 // types
1483 typedef RealType result_type;
1484
1485 class param_type
1486 {
1487 public:
1488 typedef piecewise_constant_distribution distribution_type;
1489
1490 param_type();
1491 template<class InputIteratorB, class InputIteratorW>
1492 param_type(InputIteratorB firstB, InputIteratorB lastB,
1493 InputIteratorW firstW);
1494 template<class UnaryOperation>
1495 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1496 template<class UnaryOperation>
1497 param_type(size_t nw, result_type xmin, result_type xmax,
1498 UnaryOperation fw);
1499
1500 vector<result_type> intervals() const;
1501 vector<double> densities() const;
1502
1503 friend bool operator==(const param_type& x, const param_type& y);
1504 friend bool operator!=(const param_type& x, const param_type& y);
1505 };
1506
1507 // constructor and reset functions
1508 piecewise_constant_distribution();
1509 template<class InputIteratorB, class InputIteratorW>
1510 piecewise_constant_distribution(InputIteratorB firstB,
1511 InputIteratorB lastB,
1512 InputIteratorW firstW);
1513 template<class UnaryOperation>
1514 piecewise_constant_distribution(initializer_list<result_type> bl,
1515 UnaryOperation fw);
1516 template<class UnaryOperation>
1517 piecewise_constant_distribution(size_t nw, result_type xmin,
1518 result_type xmax, UnaryOperation fw);
1519 explicit piecewise_constant_distribution(const param_type& parm);
1520 void reset();
1521
1522 // generating functions
1523 template<class URNG> result_type operator()(URNG& g);
1524 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1525
1526 // property functions
1527 vector<result_type> intervals() const;
1528 vector<double> densities() const;
1529
1530 param_type param() const;
1531 void param(const param_type& parm);
1532
1533 result_type min() const;
1534 result_type max() const;
1535
1536 friend bool operator==(const piecewise_constant_distribution& x,
1537 const piecewise_constant_distribution& y);
1538 friend bool operator!=(const piecewise_constant_distribution& x,
1539 const piecewise_constant_distribution& y);
1540
1541 template <class charT, class traits>
1542 friend
1543 basic_ostream<charT, traits>&
1544 operator<<(basic_ostream<charT, traits>& os,
1545 const piecewise_constant_distribution& x);
1546
1547 template <class charT, class traits>
1548 friend
1549 basic_istream<charT, traits>&
1550 operator>>(basic_istream<charT, traits>& is,
1551 piecewise_constant_distribution& x);
1552};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553
1554template<class RealType = double>
Howard Hinnant54305402010-05-25 00:27:34 +00001555class piecewise_linear_distribution
1556{
1557 // types
1558 typedef RealType result_type;
1559
1560 class param_type
1561 {
1562 public:
1563 typedef piecewise_linear_distribution distribution_type;
1564
1565 param_type();
1566 template<class InputIteratorB, class InputIteratorW>
1567 param_type(InputIteratorB firstB, InputIteratorB lastB,
1568 InputIteratorW firstW);
1569 template<class UnaryOperation>
1570 param_type(initializer_list<result_type> bl, UnaryOperation fw);
1571 template<class UnaryOperation>
1572 param_type(size_t nw, result_type xmin, result_type xmax,
1573 UnaryOperation fw);
1574
1575 vector<result_type> intervals() const;
1576 vector<double> densities() const;
1577
1578 friend bool operator==(const param_type& x, const param_type& y);
1579 friend bool operator!=(const param_type& x, const param_type& y);
1580 };
1581
1582 // constructor and reset functions
1583 piecewise_linear_distribution();
1584 template<class InputIteratorB, class InputIteratorW>
1585 piecewise_linear_distribution(InputIteratorB firstB,
1586 InputIteratorB lastB,
1587 InputIteratorW firstW);
1588
1589 template<class UnaryOperation>
1590 piecewise_linear_distribution(initializer_list<result_type> bl,
1591 UnaryOperation fw);
1592
1593 template<class UnaryOperation>
1594 piecewise_linear_distribution(size_t nw, result_type xmin,
1595 result_type xmax, UnaryOperation fw);
1596
1597 explicit piecewise_linear_distribution(const param_type& parm);
1598 void reset();
1599
1600 // generating functions
1601 template<class URNG> result_type operator()(URNG& g);
1602 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1603
1604 // property functions
1605 vector<result_type> intervals() const;
1606 vector<double> densities() const;
1607
1608 param_type param() const;
1609 void param(const param_type& parm);
1610
1611 result_type min() const;
1612 result_type max() const;
1613
1614 friend bool operator==(const piecewise_linear_distribution& x,
1615 const piecewise_linear_distribution& y);
1616 friend bool operator!=(const piecewise_linear_distribution& x,
1617 const piecewise_linear_distribution& y);
1618
1619 template <class charT, class traits>
1620 friend
1621 basic_ostream<charT, traits>&
1622 operator<<(basic_ostream<charT, traits>& os,
1623 const piecewise_linear_distribution& x);
1624
1625 template <class charT, class traits>
1626 friend
1627 basic_istream<charT, traits>&
1628 operator>>(basic_istream<charT, traits>& is,
1629 piecewise_linear_distribution& x);
1630};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631
1632} // std
1633*/
1634
1635#include <__config>
1636#include <cstddef>
1637#include <type_traits>
1638#include <initializer_list>
1639#include <cstdint>
1640#include <limits>
1641#include <algorithm>
Howard Hinnant551d8e42010-05-19 01:53:57 +00001642#include <numeric>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643#include <vector>
1644#include <string>
1645#include <istream>
1646#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +00001647#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648
1649#pragma GCC system_header
1650
1651_LIBCPP_BEGIN_NAMESPACE_STD
1652
1653// linear_congruential_engine
1654
1655template <unsigned long long __a, unsigned long long __c,
1656 unsigned long long __m, unsigned long long _M,
1657 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
1658struct __lce_ta;
1659
1660// 64
1661
1662template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1663struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1664{
1665 typedef unsigned long long result_type;
1666 static result_type next(result_type __x)
1667 {
1668 // Schrage's algorithm
1669 const result_type __q = __m / __a;
1670 const result_type __r = __m % __a;
1671 const result_type __t0 = __a * (__x % __q);
1672 const result_type __t1 = __r * (__x / __q);
1673 __x = __t0 + (__t0 < __t1) * __m - __t1;
1674 __x += __c - (__x >= __m - __c) * __m;
1675 return __x;
1676 }
1677};
1678
1679template <unsigned long long __a, unsigned long long __m>
1680struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1681{
1682 typedef unsigned long long result_type;
1683 static result_type next(result_type __x)
1684 {
1685 // Schrage's algorithm
1686 const result_type __q = __m / __a;
1687 const result_type __r = __m % __a;
1688 const result_type __t0 = __a * (__x % __q);
1689 const result_type __t1 = __r * (__x / __q);
1690 __x = __t0 + (__t0 < __t1) * __m - __t1;
1691 return __x;
1692 }
1693};
1694
1695template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1696struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1697{
1698 typedef unsigned long long result_type;
1699 static result_type next(result_type __x)
1700 {
1701 return (__a * __x + __c) % __m;
1702 }
1703};
1704
1705template <unsigned long long __a, unsigned long long __c>
1706struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1707{
1708 typedef unsigned long long result_type;
1709 static result_type next(result_type __x)
1710 {
1711 return __a * __x + __c;
1712 }
1713};
1714
1715// 32
1716
1717template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1718struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1719{
1720 typedef unsigned result_type;
1721 static result_type next(result_type __x)
1722 {
1723 const result_type __a = static_cast<result_type>(_A);
1724 const result_type __c = static_cast<result_type>(_C);
1725 const result_type __m = static_cast<result_type>(_M);
1726 // Schrage's algorithm
1727 const result_type __q = __m / __a;
1728 const result_type __r = __m % __a;
1729 const result_type __t0 = __a * (__x % __q);
1730 const result_type __t1 = __r * (__x / __q);
1731 __x = __t0 + (__t0 < __t1) * __m - __t1;
1732 __x += __c - (__x >= __m - __c) * __m;
1733 return __x;
1734 }
1735};
1736
1737template <unsigned long long _A, unsigned long long _M>
1738struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1739{
1740 typedef unsigned result_type;
1741 static result_type next(result_type __x)
1742 {
1743 const result_type __a = static_cast<result_type>(_A);
1744 const result_type __m = static_cast<result_type>(_M);
1745 // Schrage's algorithm
1746 const result_type __q = __m / __a;
1747 const result_type __r = __m % __a;
1748 const result_type __t0 = __a * (__x % __q);
1749 const result_type __t1 = __r * (__x / __q);
1750 __x = __t0 + (__t0 < __t1) * __m - __t1;
1751 return __x;
1752 }
1753};
1754
1755template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1756struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1757{
1758 typedef unsigned result_type;
1759 static result_type next(result_type __x)
1760 {
1761 const result_type __a = static_cast<result_type>(_A);
1762 const result_type __c = static_cast<result_type>(_C);
1763 const result_type __m = static_cast<result_type>(_M);
1764 return (__a * __x + __c) % __m;
1765 }
1766};
1767
1768template <unsigned long long _A, unsigned long long _C>
1769struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1770{
1771 typedef unsigned result_type;
1772 static result_type next(result_type __x)
1773 {
1774 const result_type __a = static_cast<result_type>(_A);
1775 const result_type __c = static_cast<result_type>(_C);
1776 return __a * __x + __c;
1777 }
1778};
1779
1780// 16
1781
1782template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1783struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1784{
1785 typedef unsigned short result_type;
1786 static result_type next(result_type __x)
1787 {
1788 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1789 }
1790};
1791
1792template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1793class linear_congruential_engine;
1794
1795template <class _CharT, class _Traits,
1796 class _U, _U _A, _U _C, _U _N>
1797basic_ostream<_CharT, _Traits>&
1798operator<<(basic_ostream<_CharT, _Traits>& __os,
1799 const linear_congruential_engine<_U, _A, _C, _N>&);
1800
1801template <class _CharT, class _Traits,
1802 class _U, _U _A, _U _C, _U _N>
1803basic_istream<_CharT, _Traits>&
1804operator>>(basic_istream<_CharT, _Traits>& __is,
1805 linear_congruential_engine<_U, _A, _C, _N>& __x);
1806
1807template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1808class linear_congruential_engine
1809{
1810public:
1811 // types
1812 typedef _UIntType result_type;
1813
1814private:
1815 result_type __x_;
1816
1817 static const result_type _M = result_type(~0);
1818
1819 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1820 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1821public:
1822 static const result_type _Min = __c == 0u ? 1u: 0u;
1823 static const result_type _Max = __m - 1u;
1824 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1825
1826 // engine characteristics
1827 static const/*expr*/ result_type multiplier = __a;
1828 static const/*expr*/ result_type increment = __c;
1829 static const/*expr*/ result_type modulus = __m;
1830 static const/*expr*/ result_type min() {return _Min;}
1831 static const/*expr*/ result_type max() {return _Max;}
1832 static const/*expr*/ result_type default_seed = 1u;
1833
1834 // constructors and seeding functions
1835 explicit linear_congruential_engine(result_type __s = default_seed)
1836 {seed(__s);}
1837 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
1838 {seed(__q);}
1839 void seed(result_type __s = default_seed)
1840 {seed(integral_constant<bool, __m == 0>(),
1841 integral_constant<bool, __c == 0>(), __s);}
1842 template<class _Sseq>
1843 typename enable_if
1844 <
1845 !is_convertible<_Sseq, result_type>::value,
1846 void
1847 >::type
1848 seed(_Sseq& __q)
1849 {__seed(__q, integral_constant<unsigned,
1850 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1851 : (__m-1) / 0x100000000ull)>());}
1852
1853 // generating functions
1854 result_type operator()()
1855 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1856 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1857
1858 friend bool operator==(const linear_congruential_engine& __x,
1859 const linear_congruential_engine& __y)
1860 {return __x.__x_ == __y.__x_;}
1861 friend bool operator!=(const linear_congruential_engine& __x,
1862 const linear_congruential_engine& __y)
1863 {return !(__x == __y);}
1864
1865private:
1866
1867 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1868 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1869 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1870 1 : __s % __m;}
1871 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1872
1873 template<class _Sseq>
1874 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1875 template<class _Sseq>
1876 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1877
1878 template <class _CharT, class _Traits,
1879 class _U, _U _A, _U _C, _U _N>
1880 friend
1881 basic_ostream<_CharT, _Traits>&
1882 operator<<(basic_ostream<_CharT, _Traits>& __os,
1883 const linear_congruential_engine<_U, _A, _C, _N>&);
1884
1885 template <class _CharT, class _Traits,
1886 class _U, _U _A, _U _C, _U _N>
1887 friend
1888 basic_istream<_CharT, _Traits>&
1889 operator>>(basic_istream<_CharT, _Traits>& __is,
1890 linear_congruential_engine<_U, _A, _C, _N>& __x);
1891};
1892
1893template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1894template<class _Sseq>
1895void
1896linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1897 integral_constant<unsigned, 1>)
1898{
1899 const unsigned __k = 1;
1900 uint32_t __ar[__k+3];
1901 __q.generate(__ar, __ar + __k + 3);
1902 result_type __s = static_cast<result_type>(__ar[3] % __m);
1903 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1904}
1905
1906template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1907template<class _Sseq>
1908void
1909linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1910 integral_constant<unsigned, 2>)
1911{
1912 const unsigned __k = 2;
1913 uint32_t __ar[__k+3];
1914 __q.generate(__ar, __ar + __k + 3);
1915 result_type __s = static_cast<result_type>((__ar[3] +
1916 (uint64_t)__ar[4] << 32) % __m);
1917 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1918}
1919
1920template <class _CharT, class _Traits>
1921class __save_flags
1922{
1923 typedef basic_ios<_CharT, _Traits> __stream_type;
1924 typedef typename __stream_type::fmtflags fmtflags;
1925
1926 __stream_type& __stream_;
1927 fmtflags __fmtflags_;
1928 _CharT __fill_;
1929
1930 __save_flags(const __save_flags&);
1931 __save_flags& operator=(const __save_flags&);
1932public:
1933 explicit __save_flags(__stream_type& __stream)
1934 : __stream_(__stream),
1935 __fmtflags_(__stream.flags()),
1936 __fill_(__stream.fill())
1937 {}
1938 ~__save_flags()
1939 {
1940 __stream_.flags(__fmtflags_);
1941 __stream_.fill(__fill_);
1942 }
1943};
1944
1945template <class _CharT, class _Traits,
1946 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1947inline
1948basic_ostream<_CharT, _Traits>&
1949operator<<(basic_ostream<_CharT, _Traits>& __os,
1950 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1951{
1952 __save_flags<_CharT, _Traits> _(__os);
1953 __os.flags(ios_base::dec | ios_base::left);
1954 __os.fill(__os.widen(' '));
1955 return __os << __x.__x_;
1956}
1957
1958template <class _CharT, class _Traits,
1959 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1960basic_istream<_CharT, _Traits>&
1961operator>>(basic_istream<_CharT, _Traits>& __is,
1962 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1963{
1964 __save_flags<_CharT, _Traits> _(__is);
1965 __is.flags(ios_base::dec | ios_base::skipws);
1966 _UIntType __t;
1967 __is >> __t;
1968 if (!__is.fail())
1969 __x.__x_ = __t;
1970 return __is;
1971}
1972
1973typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1974 minstd_rand0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1976 minstd_rand;
Howard Hinnantd6d11712010-05-20 15:11:46 +00001977typedef minstd_rand default_random_engine;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978// mersenne_twister_engine
1979
1980template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1981 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1982 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1983class mersenne_twister_engine;
1984
1985template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1986 _UI _A, size_t _U, _UI _D, size_t _S,
1987 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1988bool
1989operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1990 _B, _T, _C, _L, _F>& __x,
1991 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1992 _B, _T, _C, _L, _F>& __y);
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
2003template <class _CharT, class _Traits,
2004 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2005 _UI _A, size_t _U, _UI _D, size_t _S,
2006 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2007basic_ostream<_CharT, _Traits>&
2008operator<<(basic_ostream<_CharT, _Traits>& __os,
2009 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2010 _B, _T, _C, _L, _F>& __x);
2011
2012template <class _CharT, class _Traits,
2013 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2014 _UI _A, size_t _U, _UI _D, size_t _S,
2015 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2016basic_istream<_CharT, _Traits>&
2017operator>>(basic_istream<_CharT, _Traits>& __is,
2018 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2019 _B, _T, _C, _L, _F>& __x);
2020
2021template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2022 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2023 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2024class mersenne_twister_engine
2025{
2026public:
2027 // types
2028 typedef _UIntType result_type;
2029
2030private:
2031 result_type __x_[__n];
2032 size_t __i_;
2033
2034 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
2035 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
2036 static const result_type _Dt = numeric_limits<result_type>::digits;
2037 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2038 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
2039 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2040 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2041 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2042 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2043 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2044public:
2045 static const result_type _Min = 0;
2046 static const result_type _Max = __w == _Dt ? result_type(~0) :
2047 (result_type(1) << __w) - result_type(1);
2048 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2049 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2050 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2051 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2052 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2053 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2054
2055 // engine characteristics
2056 static const/*expr*/ size_t word_size = __w;
2057 static const/*expr*/ size_t state_size = __n;
2058 static const/*expr*/ size_t shift_size = __m;
2059 static const/*expr*/ size_t mask_bits = __r;
2060 static const/*expr*/ result_type xor_mask = __a;
2061 static const/*expr*/ size_t tempering_u = __u;
2062 static const/*expr*/ result_type tempering_d = __d;
2063 static const/*expr*/ size_t tempering_s = __s;
2064 static const/*expr*/ result_type tempering_b = __b;
2065 static const/*expr*/ size_t tempering_t = __t;
2066 static const/*expr*/ result_type tempering_c = __c;
2067 static const/*expr*/ size_t tempering_l = __l;
2068 static const/*expr*/ result_type initialization_multiplier = __f;
2069 static const/*expr*/ result_type min() { return _Min; }
2070 static const/*expr*/ result_type max() { return _Max; }
2071 static const/*expr*/ result_type default_seed = 5489u;
2072
2073 // constructors and seeding functions
2074 explicit mersenne_twister_engine(result_type __sd = default_seed)
2075 {seed(__sd);}
2076 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
2077 {seed(__q);}
2078 void seed(result_type __sd = default_seed);
2079 template<class _Sseq>
2080 typename enable_if
2081 <
2082 !is_convertible<_Sseq, result_type>::value,
2083 void
2084 >::type
2085 seed(_Sseq& __q)
2086 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2087
2088 // generating functions
2089 result_type operator()();
2090 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2091
2092 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2093 _UI _A, size_t _U, _UI _D, size_t _S,
2094 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2095 friend
2096 bool
2097 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2098 _B, _T, _C, _L, _F>& __x,
2099 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2100 _B, _T, _C, _L, _F>& __y);
2101
2102 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2103 _UI _A, size_t _U, _UI _D, size_t _S,
2104 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2105 friend
2106 bool
2107 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2108 _B, _T, _C, _L, _F>& __x,
2109 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2110 _B, _T, _C, _L, _F>& __y);
2111
2112 template <class _CharT, class _Traits,
2113 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2114 _UI _A, size_t _U, _UI _D, size_t _S,
2115 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2116 friend
2117 basic_ostream<_CharT, _Traits>&
2118 operator<<(basic_ostream<_CharT, _Traits>& __os,
2119 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2120 _B, _T, _C, _L, _F>& __x);
2121
2122 template <class _CharT, class _Traits,
2123 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2124 _UI _A, size_t _U, _UI _D, size_t _S,
2125 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2126 friend
2127 basic_istream<_CharT, _Traits>&
2128 operator>>(basic_istream<_CharT, _Traits>& __is,
2129 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2130 _B, _T, _C, _L, _F>& __x);
2131private:
2132
2133 template<class _Sseq>
2134 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2135 template<class _Sseq>
2136 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2137
2138 template <size_t __count>
2139 static
2140 typename enable_if
2141 <
2142 __count < __w,
2143 result_type
2144 >::type
2145 __lshift(result_type __x) {return (__x << __count) & _Max;}
2146
2147 template <size_t __count>
2148 static
2149 typename enable_if
2150 <
2151 (__count >= __w),
2152 result_type
2153 >::type
2154 __lshift(result_type __x) {return result_type(0);}
2155
2156 template <size_t __count>
2157 static
2158 typename enable_if
2159 <
2160 __count < _Dt,
2161 result_type
2162 >::type
2163 __rshift(result_type __x) {return __x >> __count;}
2164
2165 template <size_t __count>
2166 static
2167 typename enable_if
2168 <
2169 (__count >= _Dt),
2170 result_type
2171 >::type
2172 __rshift(result_type __x) {return result_type(0);}
2173};
2174
2175template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2176 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2177 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2178void
2179mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2180 __t, __c, __l, __f>::seed(result_type __sd)
2181{ // __w >= 2
2182 __x_[0] = __sd & _Max;
2183 for (size_t __i = 1; __i < __n; ++__i)
2184 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2185 __i_ = 0;
2186}
2187
2188template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2189 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2190 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2191template<class _Sseq>
2192void
2193mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2194 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2195{
2196 const unsigned __k = 1;
2197 uint32_t __ar[__n * __k];
2198 __q.generate(__ar, __ar + __n * __k);
2199 for (size_t __i = 0; __i < __n; ++__i)
2200 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2201 const result_type __mask = __r == _Dt ? result_type(~0) :
2202 (result_type(1) << __r) - result_type(1);
2203 __i_ = 0;
2204 if ((__x_[0] & ~__mask) == 0)
2205 {
2206 for (size_t __i = 1; __i < __n; ++__i)
2207 if (__x_[__i] != 0)
2208 return;
2209 __x_[0] = _Max;
2210 }
2211}
2212
2213template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2214 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2215 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2216template<class _Sseq>
2217void
2218mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2219 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2220{
2221 const unsigned __k = 2;
2222 uint32_t __ar[__n * __k];
2223 __q.generate(__ar, __ar + __n * __k);
2224 for (size_t __i = 0; __i < __n; ++__i)
2225 __x_[__i] = static_cast<result_type>(
2226 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2227 const result_type __mask = __r == _Dt ? result_type(~0) :
2228 (result_type(1) << __r) - result_type(1);
2229 __i_ = 0;
2230 if ((__x_[0] & ~__mask) == 0)
2231 {
2232 for (size_t __i = 1; __i < __n; ++__i)
2233 if (__x_[__i] != 0)
2234 return;
2235 __x_[0] = _Max;
2236 }
2237}
2238
2239template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2240 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2241 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2242_UIntType
2243mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2244 __t, __c, __l, __f>::operator()()
2245{
2246 const size_t __j = (__i_ + 1) % __n;
2247 const result_type __mask = __r == _Dt ? result_type(~0) :
2248 (result_type(1) << __r) - result_type(1);
2249 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
2250 const size_t __k = (__i_ + __m) % __n;
2251 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
2252 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2253 __i_ = __j;
2254 __z ^= __lshift<__s>(__z) & __b;
2255 __z ^= __lshift<__t>(__z) & __c;
2256 return __z ^ __rshift<__l>(__z);
2257}
2258
2259template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2260 _UI _A, size_t _U, _UI _D, size_t _S,
2261 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2262bool
2263operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2264 _B, _T, _C, _L, _F>& __x,
2265 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2266 _B, _T, _C, _L, _F>& __y)
2267{
2268 if (__x.__i_ == __y.__i_)
2269 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
2270 if (__x.__i_ == 0 || __y.__i_ == 0)
2271 {
2272 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
2273 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2274 __y.__x_ + __y.__i_))
2275 return false;
2276 if (__x.__i_ == 0)
2277 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
2278 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
2279 }
2280 if (__x.__i_ < __y.__i_)
2281 {
2282 size_t __j = _N - __y.__i_;
2283 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2284 __y.__x_ + __y.__i_))
2285 return false;
2286 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
2287 __y.__x_))
2288 return false;
2289 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2290 __y.__x_ + (_N - (__x.__i_ + __j)));
2291 }
2292 size_t __j = _N - __x.__i_;
2293 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2294 __x.__x_ + __x.__i_))
2295 return false;
2296 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
2297 __x.__x_))
2298 return false;
2299 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2300 __x.__x_ + (_N - (__y.__i_ + __j)));
2301}
2302
2303template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2304 _UI _A, size_t _U, _UI _D, size_t _S,
2305 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2306inline
2307bool
2308operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2309 _B, _T, _C, _L, _F>& __x,
2310 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2311 _B, _T, _C, _L, _F>& __y)
2312{
2313 return !(__x == __y);
2314}
2315
2316template <class _CharT, class _Traits,
2317 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2318 _UI _A, size_t _U, _UI _D, size_t _S,
2319 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2320basic_ostream<_CharT, _Traits>&
2321operator<<(basic_ostream<_CharT, _Traits>& __os,
2322 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2323 _B, _T, _C, _L, _F>& __x)
2324{
2325 __save_flags<_CharT, _Traits> _(__os);
2326 __os.flags(ios_base::dec | ios_base::left);
2327 _CharT __sp = __os.widen(' ');
2328 __os.fill(__sp);
2329 __os << __x.__x_[__x.__i_];
2330 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
2331 __os << __sp << __x.__x_[__j];
2332 for (size_t __j = 0; __j < __x.__i_; ++__j)
2333 __os << __sp << __x.__x_[__j];
2334 return __os;
2335}
2336
2337template <class _CharT, class _Traits,
2338 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2339 _UI _A, size_t _U, _UI _D, size_t _S,
2340 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2341basic_istream<_CharT, _Traits>&
2342operator>>(basic_istream<_CharT, _Traits>& __is,
2343 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2344 _B, _T, _C, _L, _F>& __x)
2345{
2346 __save_flags<_CharT, _Traits> _(__is);
2347 __is.flags(ios_base::dec | ios_base::skipws);
2348 _UI __t[_N];
2349 for (size_t __i = 0; __i < _N; ++__i)
2350 __is >> __t[__i];
2351 if (!__is.fail())
2352 {
2353 for (size_t __i = 0; __i < _N; ++__i)
2354 __x.__x_[__i] = __t[__i];
2355 __x.__i_ = 0;
2356 }
2357 return __is;
2358}
2359
2360typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2361 0x9908b0df, 11, 0xffffffff,
2362 7, 0x9d2c5680,
2363 15, 0xefc60000,
2364 18, 1812433253> mt19937;
2365typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2366 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2367 17, 0x71d67fffeda60000ULL,
2368 37, 0xfff7eee000000000ULL,
2369 43, 6364136223846793005ULL> mt19937_64;
2370
2371// subtract_with_carry_engine
2372
2373template<class _UIntType, size_t __w, size_t __s, size_t __r>
2374class subtract_with_carry_engine;
2375
2376template<class _UI, size_t _W, size_t _S, size_t _R>
2377bool
2378operator==(
2379 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2380 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2381
2382template<class _UI, size_t _W, size_t _S, size_t _R>
2383bool
2384operator!=(
2385 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2386 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2387
2388template <class _CharT, class _Traits,
2389 class _UI, size_t _W, size_t _S, size_t _R>
2390basic_ostream<_CharT, _Traits>&
2391operator<<(basic_ostream<_CharT, _Traits>& __os,
2392 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2393
2394template <class _CharT, class _Traits,
2395 class _UI, size_t _W, size_t _S, size_t _R>
2396basic_istream<_CharT, _Traits>&
2397operator>>(basic_istream<_CharT, _Traits>& __is,
2398 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2399
2400template<class _UIntType, size_t __w, size_t __s, size_t __r>
2401class subtract_with_carry_engine
2402{
2403public:
2404 // types
2405 typedef _UIntType result_type;
2406
2407private:
2408 result_type __x_[__r];
2409 result_type __c_;
2410 size_t __i_;
2411
2412 static const result_type _Dt = numeric_limits<result_type>::digits;
2413 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
2414 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2415 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
2416 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
2417public:
2418 static const result_type _Min = 0;
2419 static const result_type _Max = __w == _Dt ? result_type(~0) :
2420 (result_type(1) << __w) - result_type(1);
2421 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2422
2423 // engine characteristics
2424 static const/*expr*/ size_t word_size = __w;
2425 static const/*expr*/ size_t short_lag = __s;
2426 static const/*expr*/ size_t long_lag = __r;
2427 static const/*expr*/ result_type min() { return _Min; }
2428 static const/*expr*/ result_type max() { return _Max; }
2429 static const/*expr*/ result_type default_seed = 19780503u;
2430
2431 // constructors and seeding functions
2432 explicit subtract_with_carry_engine(result_type __sd = default_seed)
2433 {seed(__sd);}
2434 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
2435 {seed(__q);}
2436 void seed(result_type __sd = default_seed)
2437 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2438 template<class _Sseq>
2439 typename enable_if
2440 <
2441 !is_convertible<_Sseq, result_type>::value,
2442 void
2443 >::type
2444 seed(_Sseq& __q)
2445 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2446
2447 // generating functions
2448 result_type operator()();
2449 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2450
2451 template<class _UI, size_t _W, size_t _S, size_t _R>
2452 friend
2453 bool
2454 operator==(
2455 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2456 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2457
2458 template<class _UI, size_t _W, size_t _S, size_t _R>
2459 friend
2460 bool
2461 operator!=(
2462 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2463 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2464
2465 template <class _CharT, class _Traits,
2466 class _UI, size_t _W, size_t _S, size_t _R>
2467 friend
2468 basic_ostream<_CharT, _Traits>&
2469 operator<<(basic_ostream<_CharT, _Traits>& __os,
2470 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2471
2472 template <class _CharT, class _Traits,
2473 class _UI, size_t _W, size_t _S, size_t _R>
2474 friend
2475 basic_istream<_CharT, _Traits>&
2476 operator>>(basic_istream<_CharT, _Traits>& __is,
2477 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2478
2479private:
2480
2481 void seed(result_type __sd, integral_constant<unsigned, 1>);
2482 void seed(result_type __sd, integral_constant<unsigned, 2>);
2483 template<class _Sseq>
2484 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2485 template<class _Sseq>
2486 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2487};
2488
2489template<class _UIntType, size_t __w, size_t __s, size_t __r>
2490void
2491subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2492 integral_constant<unsigned, 1>)
2493{
2494 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2495 __e(__sd == 0u ? default_seed : __sd);
2496 for (size_t __i = 0; __i < __r; ++__i)
2497 __x_[__i] = static_cast<result_type>(__e() & _Max);
2498 __c_ = __x_[__r-1] == 0;
2499 __i_ = 0;
2500}
2501
2502template<class _UIntType, size_t __w, size_t __s, size_t __r>
2503void
2504subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2505 integral_constant<unsigned, 2>)
2506{
2507 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2508 __e(__sd == 0u ? default_seed : __sd);
2509 for (size_t __i = 0; __i < __r; ++__i)
2510 __x_[__i] = static_cast<result_type>(
2511 (__e() + ((uint64_t)__e() << 32)) & _Max);
2512 __c_ = __x_[__r-1] == 0;
2513 __i_ = 0;
2514}
2515
2516template<class _UIntType, size_t __w, size_t __s, size_t __r>
2517template<class _Sseq>
2518void
2519subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2520 integral_constant<unsigned, 1>)
2521{
2522 const unsigned __k = 1;
2523 uint32_t __ar[__r * __k];
2524 __q.generate(__ar, __ar + __r * __k);
2525 for (size_t __i = 0; __i < __r; ++__i)
2526 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2527 __c_ = __x_[__r-1] == 0;
2528 __i_ = 0;
2529}
2530
2531template<class _UIntType, size_t __w, size_t __s, size_t __r>
2532template<class _Sseq>
2533void
2534subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2535 integral_constant<unsigned, 2>)
2536{
2537 const unsigned __k = 2;
2538 uint32_t __ar[__r * __k];
2539 __q.generate(__ar, __ar + __r * __k);
2540 for (size_t __i = 0; __i < __r; ++__i)
2541 __x_[__i] = static_cast<result_type>(
2542 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2543 __c_ = __x_[__r-1] == 0;
2544 __i_ = 0;
2545}
2546
2547template<class _UIntType, size_t __w, size_t __s, size_t __r>
2548_UIntType
2549subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2550{
2551 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2552 result_type& __xr = __x_[__i_];
2553 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2554 __xr = (__xs - __xr - __c_) & _Max;
2555 __c_ = __new_c;
2556 __i_ = (__i_ + 1) % __r;
2557 return __xr;
2558}
2559
2560template<class _UI, size_t _W, size_t _S, size_t _R>
2561bool
2562operator==(
2563 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2564 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2565{
2566 if (__x.__c_ != __y.__c_)
2567 return false;
2568 if (__x.__i_ == __y.__i_)
2569 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
2570 if (__x.__i_ == 0 || __y.__i_ == 0)
2571 {
2572 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
2573 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2574 __y.__x_ + __y.__i_))
2575 return false;
2576 if (__x.__i_ == 0)
2577 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2578 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
2579 }
2580 if (__x.__i_ < __y.__i_)
2581 {
2582 size_t __j = _R - __y.__i_;
2583 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2584 __y.__x_ + __y.__i_))
2585 return false;
2586 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
2587 __y.__x_))
2588 return false;
2589 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
2590 __y.__x_ + (_R - (__x.__i_ + __j)));
2591 }
2592 size_t __j = _R - __x.__i_;
2593 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2594 __x.__x_ + __x.__i_))
2595 return false;
2596 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
2597 __x.__x_))
2598 return false;
2599 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
2600 __x.__x_ + (_R - (__y.__i_ + __j)));
2601}
2602
2603template<class _UI, size_t _W, size_t _S, size_t _R>
2604inline
2605bool
2606operator!=(
2607 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2608 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2609{
2610 return !(__x == __y);
2611}
2612
2613template <class _CharT, class _Traits,
2614 class _UI, size_t _W, size_t _S, size_t _R>
2615basic_ostream<_CharT, _Traits>&
2616operator<<(basic_ostream<_CharT, _Traits>& __os,
2617 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2618{
2619 __save_flags<_CharT, _Traits> _(__os);
2620 __os.flags(ios_base::dec | ios_base::left);
2621 _CharT __sp = __os.widen(' ');
2622 __os.fill(__sp);
2623 __os << __x.__x_[__x.__i_];
2624 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
2625 __os << __sp << __x.__x_[__j];
2626 for (size_t __j = 0; __j < __x.__i_; ++__j)
2627 __os << __sp << __x.__x_[__j];
2628 __os << __sp << __x.__c_;
2629 return __os;
2630}
2631
2632template <class _CharT, class _Traits,
2633 class _UI, size_t _W, size_t _S, size_t _R>
2634basic_istream<_CharT, _Traits>&
2635operator>>(basic_istream<_CharT, _Traits>& __is,
2636 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2637{
2638 __save_flags<_CharT, _Traits> _(__is);
2639 __is.flags(ios_base::dec | ios_base::skipws);
2640 _UI __t[_R+1];
2641 for (size_t __i = 0; __i < _R+1; ++__i)
2642 __is >> __t[__i];
2643 if (!__is.fail())
2644 {
2645 for (size_t __i = 0; __i < _R; ++__i)
2646 __x.__x_[__i] = __t[__i];
2647 __x.__c_ = __t[_R];
2648 __x.__i_ = 0;
2649 }
2650 return __is;
2651}
2652
2653typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
2654typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
2655
2656// discard_block_engine
2657
2658template<class _Engine, size_t __p, size_t __r>
2659class discard_block_engine
2660{
2661 _Engine __e_;
2662 int __n_;
2663
2664 static_assert( 0 < __r, "discard_block_engine invalid parameters");
2665 static_assert(__r <= __p, "discard_block_engine invalid parameters");
2666public:
2667 // types
2668 typedef typename _Engine::result_type result_type;
2669
2670 // engine characteristics
2671 static const/*expr*/ size_t block_size = __p;
2672 static const/*expr*/ size_t used_block = __r;
2673
2674 // Temporary work around for lack of constexpr
2675 static const result_type _Min = _Engine::_Min;
2676 static const result_type _Max = _Engine::_Max;
2677
2678 static const/*expr*/ result_type min() { return _Engine::min(); }
2679 static const/*expr*/ result_type max() { return _Engine::max(); }
2680
2681 // constructors and seeding functions
2682 discard_block_engine() : __n_(0) {}
2683// explicit discard_block_engine(const _Engine& __e);
2684// explicit discard_block_engine(_Engine&& __e);
2685 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
2686 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
2687 : __e_(__q), __n_(0) {}
2688 void seed() {__e_.seed(); __n_ = 0;}
2689 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
2690 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
2691
2692 // generating functions
2693 result_type operator()();
2694 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2695
2696 // property functions
2697 const _Engine& base() const {return __e_;}
2698
2699 template<class _Eng, size_t _P, size_t _R>
2700 friend
2701 bool
2702 operator==(
2703 const discard_block_engine<_Eng, _P, _R>& __x,
2704 const discard_block_engine<_Eng, _P, _R>& __y);
2705
2706 template<class _Eng, size_t _P, size_t _R>
2707 friend
2708 bool
2709 operator!=(
2710 const discard_block_engine<_Eng, _P, _R>& __x,
2711 const discard_block_engine<_Eng, _P, _R>& __y);
2712
2713 template <class _CharT, class _Traits,
2714 class _Eng, size_t _P, size_t _R>
2715 friend
2716 basic_ostream<_CharT, _Traits>&
2717 operator<<(basic_ostream<_CharT, _Traits>& __os,
2718 const discard_block_engine<_Eng, _P, _R>& __x);
2719
2720 template <class _CharT, class _Traits,
2721 class _Eng, size_t _P, size_t _R>
2722 friend
2723 basic_istream<_CharT, _Traits>&
2724 operator>>(basic_istream<_CharT, _Traits>& __is,
2725 discard_block_engine<_Eng, _P, _R>& __x);
2726};
2727
2728template<class _Engine, size_t __p, size_t __r>
2729typename discard_block_engine<_Engine, __p, __r>::result_type
2730discard_block_engine<_Engine, __p, __r>::operator()()
2731{
2732 if (__n_ >= __r)
2733 {
2734 __e_.discard(__p - __r);
2735 __n_ = 0;
2736 }
2737 ++__n_;
2738 return __e_();
2739}
2740
2741template<class _Eng, size_t _P, size_t _R>
2742inline
2743bool
2744operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2745 const discard_block_engine<_Eng, _P, _R>& __y)
2746{
2747 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2748}
2749
2750template<class _Eng, size_t _P, size_t _R>
2751inline
2752bool
2753operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2754 const discard_block_engine<_Eng, _P, _R>& __y)
2755{
2756 return !(__x == __y);
2757}
2758
2759template <class _CharT, class _Traits,
2760 class _Eng, size_t _P, size_t _R>
2761basic_ostream<_CharT, _Traits>&
2762operator<<(basic_ostream<_CharT, _Traits>& __os,
2763 const discard_block_engine<_Eng, _P, _R>& __x)
2764{
2765 __save_flags<_CharT, _Traits> _(__os);
2766 __os.flags(ios_base::dec | ios_base::left);
2767 _CharT __sp = __os.widen(' ');
2768 __os.fill(__sp);
2769 return __os << __x.__e_ << __sp << __x.__n_;
2770}
2771
2772template <class _CharT, class _Traits,
2773 class _Eng, size_t _P, size_t _R>
2774basic_istream<_CharT, _Traits>&
2775operator>>(basic_istream<_CharT, _Traits>& __is,
2776 discard_block_engine<_Eng, _P, _R>& __x)
2777{
2778 __save_flags<_CharT, _Traits> _(__is);
2779 __is.flags(ios_base::dec | ios_base::skipws);
2780 _Eng __e;
2781 int __n;
2782 __is >> __e >> __n;
2783 if (!__is.fail())
2784 {
2785 __x.__e_ = __e;
2786 __x.__n_ = __n;
2787 }
2788 return __is;
2789}
2790
2791typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2792typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2793
2794// independent_bits_engine
2795
2796template <unsigned long long _X, size_t _R>
2797struct __log2_imp
2798{
2799 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2800 : __log2_imp<_X, _R - 1>::value;
2801};
2802
2803template <unsigned long long _X>
2804struct __log2_imp<_X, 0>
2805{
2806 static const size_t value = 0;
2807};
2808
2809template <size_t _R>
2810struct __log2_imp<0, _R>
2811{
2812 static const size_t value = _R + 1;
2813};
2814
2815template <class _UI, _UI _X>
2816struct __log2
2817{
2818 static const size_t value = __log2_imp<_X,
2819 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2820};
2821
2822template<class _Engine, size_t __w, class _UIntType>
2823class independent_bits_engine
2824{
2825 template <class _UI, _UI _R0, size_t _W, size_t _M>
2826 class __get_n
2827 {
2828 static const size_t _Dt = numeric_limits<_UI>::digits;
2829 static const size_t _N = _W / _M + (_W % _M != 0);
2830 static const size_t _W0 = _W / _N;
2831 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2832 public:
2833 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2834 };
2835public:
2836 // types
2837 typedef _UIntType result_type;
2838
2839private:
2840 _Engine __e_;
2841
2842 static const result_type _Dt = numeric_limits<result_type>::digits;
2843 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2844 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2845
2846 typedef typename _Engine::result_type _Engine_result_type;
2847 typedef typename conditional
2848 <
2849 sizeof(_Engine_result_type) <= sizeof(result_type),
2850 result_type,
2851 _Engine_result_type
2852 >::type _Working_result_type;
2853 // Temporary work around for lack of constexpr
2854 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2855 + _Working_result_type(1);
2856 static const size_t __m = __log2<_Working_result_type, _R>::value;
2857 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2858 static const size_t __w0 = __w / __n;
2859 static const size_t __n0 = __n - __w % __n;
2860 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2861 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2862 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2863 (_R >> __w0) << __w0;
2864 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2865 (_R >> (__w0+1)) << (__w0+1);
2866 static const _Engine_result_type __mask0 = __w0 > 0 ?
2867 _Engine_result_type(~0) >> (_EDt - __w0) :
2868 _Engine_result_type(0);
2869 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2870 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2871 _Engine_result_type(~0);
2872public:
2873 static const result_type _Min = 0;
2874 static const result_type _Max = __w == _Dt ? result_type(~0) :
2875 (result_type(1) << __w) - result_type(1);
2876 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2877
2878 // engine characteristics
2879 static const/*expr*/ result_type min() { return _Min; }
2880 static const/*expr*/ result_type max() { return _Max; }
2881
2882 // constructors and seeding functions
2883 independent_bits_engine() {}
2884// explicit independent_bits_engine(const _Engine& __e);
2885// explicit independent_bits_engine(_Engine&& __e);
2886 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2887 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2888 : __e_(__q) {}
2889 void seed() {__e_.seed();}
2890 void seed(result_type __sd) {__e_.seed(__sd);}
2891 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2892
2893 // generating functions
2894 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2895 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2896
2897 // property functions
2898 const _Engine& base() const {return __e_;}
2899
2900 template<class _Eng, size_t _W, class _UI>
2901 friend
2902 bool
2903 operator==(
2904 const independent_bits_engine<_Eng, _W, _UI>& __x,
2905 const independent_bits_engine<_Eng, _W, _UI>& __y);
2906
2907 template<class _Eng, size_t _W, class _UI>
2908 friend
2909 bool
2910 operator!=(
2911 const independent_bits_engine<_Eng, _W, _UI>& __x,
2912 const independent_bits_engine<_Eng, _W, _UI>& __y);
2913
2914 template <class _CharT, class _Traits,
2915 class _Eng, size_t _W, class _UI>
2916 friend
2917 basic_ostream<_CharT, _Traits>&
2918 operator<<(basic_ostream<_CharT, _Traits>& __os,
2919 const independent_bits_engine<_Eng, _W, _UI>& __x);
2920
2921 template <class _CharT, class _Traits,
2922 class _Eng, size_t _W, class _UI>
2923 friend
2924 basic_istream<_CharT, _Traits>&
2925 operator>>(basic_istream<_CharT, _Traits>& __is,
2926 independent_bits_engine<_Eng, _W, _UI>& __x);
2927
2928private:
2929 result_type __eval(false_type);
2930 result_type __eval(true_type);
2931
2932 template <size_t __count>
2933 static
2934 typename enable_if
2935 <
2936 __count < _Dt,
2937 result_type
2938 >::type
2939 __lshift(result_type __x) {return __x << __count;}
2940
2941 template <size_t __count>
2942 static
2943 typename enable_if
2944 <
2945 (__count >= _Dt),
2946 result_type
2947 >::type
2948 __lshift(result_type __x) {return result_type(0);}
2949};
2950
2951template<class _Engine, size_t __w, class _UIntType>
2952inline
2953_UIntType
2954independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2955{
2956 return static_cast<result_type>(__e_() & __mask0);
2957}
2958
2959template<class _Engine, size_t __w, class _UIntType>
2960_UIntType
2961independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2962{
2963 result_type _S = 0;
2964 for (size_t __k = 0; __k < __n0; ++__k)
2965 {
2966 _Engine_result_type __u;
2967 do
2968 {
2969 __u = __e_() - _Engine::min();
2970 } while (__u >= __y0);
2971 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2972 }
2973 for (size_t __k = __n0; __k < __n; ++__k)
2974 {
2975 _Engine_result_type __u;
2976 do
2977 {
2978 __u = __e_() - _Engine::min();
2979 } while (__u >= __y1);
2980 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2981 }
2982 return _S;
2983}
2984
2985template<class _Eng, size_t _W, class _UI>
2986inline
2987bool
2988operator==(
2989 const independent_bits_engine<_Eng, _W, _UI>& __x,
2990 const independent_bits_engine<_Eng, _W, _UI>& __y)
2991{
2992 return __x.base() == __y.base();
2993}
2994
2995template<class _Eng, size_t _W, class _UI>
2996inline
2997bool
2998operator!=(
2999 const independent_bits_engine<_Eng, _W, _UI>& __x,
3000 const independent_bits_engine<_Eng, _W, _UI>& __y)
3001{
3002 return !(__x == __y);
3003}
3004
3005template <class _CharT, class _Traits,
3006 class _Eng, size_t _W, class _UI>
3007basic_ostream<_CharT, _Traits>&
3008operator<<(basic_ostream<_CharT, _Traits>& __os,
3009 const independent_bits_engine<_Eng, _W, _UI>& __x)
3010{
3011 return __os << __x.base();
3012}
3013
3014template <class _CharT, class _Traits,
3015 class _Eng, size_t _W, class _UI>
3016basic_istream<_CharT, _Traits>&
3017operator>>(basic_istream<_CharT, _Traits>& __is,
3018 independent_bits_engine<_Eng, _W, _UI>& __x)
3019{
3020 _Eng __e;
3021 __is >> __e;
3022 if (!__is.fail())
3023 __x.__e_ = __e;
3024 return __is;
3025}
3026
3027// shuffle_order_engine
3028
3029template <uint64_t _Xp, uint64_t _Yp>
3030struct __ugcd
3031{
3032 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
3033};
3034
3035template <uint64_t _Xp>
3036struct __ugcd<_Xp, 0>
3037{
3038 static const uint64_t value = _Xp;
3039};
3040
3041template <uint64_t _N, uint64_t _D>
3042class __uratio
3043{
3044 static_assert(_D != 0, "__uratio divide by 0");
3045 static const uint64_t __gcd = __ugcd<_N, _D>::value;
3046public:
3047 static const uint64_t num = _N / __gcd;
3048 static const uint64_t den = _D / __gcd;
3049
3050 typedef __uratio<num, den> type;
3051};
3052
3053template<class _Engine, size_t __k>
3054class shuffle_order_engine
3055{
3056 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3057public:
3058 // types
3059 typedef typename _Engine::result_type result_type;
3060
3061private:
3062 _Engine __e_;
3063 result_type _V_[__k];
3064 result_type _Y_;
3065
3066public:
3067 // engine characteristics
3068 static const/*expr*/ size_t table_size = __k;
3069
3070 static const result_type _Min = _Engine::_Min;
3071 static const result_type _Max = _Engine::_Max;
3072 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
3073 static const/*expr*/ result_type min() { return _Min; }
3074 static const/*expr*/ result_type max() { return _Max; }
3075
3076 static const unsigned long long _R = _Max - _Min + 1ull;
3077
3078 // constructors and seeding functions
3079 shuffle_order_engine() {__init();}
3080// explicit shuffle_order_engine(const _Engine& __e);
3081// explicit shuffle_order_engine(_Engine&& e);
3082 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
3083 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
3084 : __e_(__q) {__init();}
3085 void seed() {__e_.seed(); __init();}
3086 void seed(result_type __sd) {__e_.seed(__sd); __init();}
3087 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
3088
3089 // generating functions
3090 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
3091 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3092
3093 // property functions
3094 const _Engine& base() const {return __e_;}
3095
3096private:
3097 template<class _Eng, size_t _K>
3098 friend
3099 bool
3100 operator==(
3101 const shuffle_order_engine<_Eng, _K>& __x,
3102 const shuffle_order_engine<_Eng, _K>& __y);
3103
3104 template<class _Eng, size_t _K>
3105 friend
3106 bool
3107 operator!=(
3108 const shuffle_order_engine<_Eng, _K>& __x,
3109 const shuffle_order_engine<_Eng, _K>& __y);
3110
3111 template <class _CharT, class _Traits,
3112 class _Eng, size_t _K>
3113 friend
3114 basic_ostream<_CharT, _Traits>&
3115 operator<<(basic_ostream<_CharT, _Traits>& __os,
3116 const shuffle_order_engine<_Eng, _K>& __x);
3117
3118 template <class _CharT, class _Traits,
3119 class _Eng, size_t _K>
3120 friend
3121 basic_istream<_CharT, _Traits>&
3122 operator>>(basic_istream<_CharT, _Traits>& __is,
3123 shuffle_order_engine<_Eng, _K>& __x);
3124
3125 void __init()
3126 {
3127 for (size_t __i = 0; __i < __k; ++__i)
3128 _V_[__i] = __e_();
3129 _Y_ = __e_();
3130 }
3131
3132 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
3133 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
3134
3135 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
3136 result_type __eval2(true_type) {return __evalf<__k, 0>();}
3137
3138 template <uint64_t _N, uint64_t _D>
3139 typename enable_if
3140 <
3141 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
3142 result_type
3143 >::type
3144 __eval(__uratio<_N, _D>)
3145 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
3146
3147 template <uint64_t _N, uint64_t _D>
3148 typename enable_if
3149 <
3150 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
3151 result_type
3152 >::type
3153 __eval(__uratio<_N, _D>)
3154 {
3155 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
3156 / __uratio<_N, _D>::den);
3157 _Y_ = _V_[__j];
3158 _V_[__j] = __e_();
3159 return _Y_;
3160 }
3161
3162 template <uint64_t __n, uint64_t __d>
3163 result_type __evalf()
3164 {
3165 const double _F = __d == 0 ?
3166 __n / (2. * 0x8000000000000000ull) :
3167 __n / (double)__d;
3168 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
3169 _Y_ = _V_[__j];
3170 _V_[__j] = __e_();
3171 return _Y_;
3172 }
3173};
3174
3175template<class _Eng, size_t _K>
3176bool
3177operator==(
3178 const shuffle_order_engine<_Eng, _K>& __x,
3179 const shuffle_order_engine<_Eng, _K>& __y)
3180{
3181 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
3182 __x.__e_ == __y.__e_;
3183}
3184
3185template<class _Eng, size_t _K>
3186inline
3187bool
3188operator!=(
3189 const shuffle_order_engine<_Eng, _K>& __x,
3190 const shuffle_order_engine<_Eng, _K>& __y)
3191{
3192 return !(__x == __y);
3193}
3194
3195template <class _CharT, class _Traits,
3196 class _Eng, size_t _K>
3197basic_ostream<_CharT, _Traits>&
3198operator<<(basic_ostream<_CharT, _Traits>& __os,
3199 const shuffle_order_engine<_Eng, _K>& __x)
3200{
3201 __save_flags<_CharT, _Traits> _(__os);
3202 __os.flags(ios_base::dec | ios_base::left);
3203 _CharT __sp = __os.widen(' ');
3204 __os.fill(__sp);
3205 __os << __x.__e_ << __sp << __x._V_[0];
3206 for (size_t __i = 1; __i < _K; ++__i)
3207 __os << __sp << __x._V_[__i];
3208 return __os << __sp << __x._Y_;
3209}
3210
3211template <class _CharT, class _Traits,
3212 class _Eng, size_t _K>
3213basic_istream<_CharT, _Traits>&
3214operator>>(basic_istream<_CharT, _Traits>& __is,
3215 shuffle_order_engine<_Eng, _K>& __x)
3216{
3217 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
3218 __save_flags<_CharT, _Traits> _(__is);
3219 __is.flags(ios_base::dec | ios_base::skipws);
3220 _Eng __e;
3221 result_type _V[_K+1];
3222 __is >> __e;
3223 for (size_t __i = 0; __i < _K+1; ++__i)
3224 __is >> _V[__i];
3225 if (!__is.fail())
3226 {
3227 __x.__e_ = __e;
3228 for (size_t __i = 0; __i < _K; ++__i)
3229 __x._V_[__i] = _V[__i];
3230 __x._Y_ = _V[_K];
3231 }
3232 return __is;
3233}
3234
3235typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
3236
3237// random_device
3238
3239class random_device
3240{
3241 int __f_;
3242public:
3243 // types
3244 typedef unsigned result_type;
3245
3246 // generator characteristics
3247 static const result_type _Min = 0;
3248 static const result_type _Max = 0xFFFFFFFFu;
3249
3250 static const/*expr*/ result_type min() { return _Min;}
3251 static const/*expr*/ result_type max() { return _Max;}
3252
3253 // constructors
3254 explicit random_device(const string& __token = "/dev/urandom");
3255 ~random_device();
3256
3257 // generating functions
3258 result_type operator()();
3259
3260 // property functions
3261 double entropy() const;
3262
3263private:
3264 // no copy functions
3265 random_device(const random_device&); // = delete;
3266 random_device& operator=(const random_device&); // = delete;
3267};
3268
3269// seed_seq
3270
3271class seed_seq
3272{
3273public:
3274 // types
3275 typedef uint32_t result_type;
3276
3277private:
3278 vector<result_type> __v_;
3279
3280 template<class _InputIterator>
3281 void init(_InputIterator __first, _InputIterator __last);
3282public:
3283 // constructors
3284 seed_seq() {}
3285 template<class _Tp>
3286 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
3287
3288 template<class _InputIterator>
3289 seed_seq(_InputIterator __first, _InputIterator __last)
3290 {init(__first, __last);}
3291
3292 // generating functions
3293 template<class _RandomAccessIterator>
3294 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3295
3296 // property functions
3297 size_t size() const {return __v_.size();}
3298 template<class _OutputIterator>
3299 void param(_OutputIterator __dest) const
3300 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
3301
3302private:
3303 // no copy functions
3304 seed_seq(const seed_seq&); // = delete;
3305 void operator=(const seed_seq&); // = delete;
3306
3307 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
3308};
3309
3310template<class _InputIterator>
3311void
3312seed_seq::init(_InputIterator __first, _InputIterator __last)
3313{
3314 for (_InputIterator __s = __first; __s != __last; ++__s)
3315 __v_.push_back(*__s & 0xFFFFFFFF);
3316}
3317
3318template<class _RandomAccessIterator>
3319void
3320seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3321{
3322 if (__first != __last)
3323 {
3324 _STD::fill(__first, __last, 0x8b8b8b8b);
3325 const size_t __n = static_cast<size_t>(__last - __first);
3326 const size_t __s = __v_.size();
3327 const size_t __t = (__n >= 623) ? 11
3328 : (__n >= 68) ? 7
3329 : (__n >= 39) ? 5
3330 : (__n >= 7) ? 3
3331 : (__n - 1) / 2;
3332 const size_t __p = (__n - __t) / 2;
3333 const size_t __q = __p + __t;
3334 const size_t __m = _STD::max(__s + 1, __n);
3335 // __k = 0;
3336 {
3337 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
3338 ^ __first[__n - 1]);
3339 __first[__p] += __r;
3340 __r += __s;
3341 __first[__q] += __r;
3342 __first[0] = __r;
3343 }
3344 for (size_t __k = 1; __k <= __s; ++__k)
3345 {
3346 const size_t __kmodn = __k % __n;
3347 const size_t __kpmodn = (__k + __p) % __n;
3348 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3349 ^ __first[(__k - 1) % __n]);
3350 __first[__kpmodn] += __r;
3351 __r += __kmodn + __v_[__k-1];
3352 __first[(__k + __q) % __n] += __r;
3353 __first[__kmodn] = __r;
3354 }
3355 for (size_t __k = __s + 1; __k < __m; ++__k)
3356 {
3357 const size_t __kmodn = __k % __n;
3358 const size_t __kpmodn = (__k + __p) % __n;
3359 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3360 ^ __first[(__k - 1) % __n]);
3361 __first[__kpmodn] += __r;
3362 __r += __kmodn;
3363 __first[(__k + __q) % __n] += __r;
3364 __first[__kmodn] = __r;
3365 }
3366 for (size_t __k = __m; __k < __m + __n; ++__k)
3367 {
3368 const size_t __kmodn = __k % __n;
3369 const size_t __kpmodn = (__k + __p) % __n;
3370 result_type __r = 1566083941 * _T(__first[__kmodn] +
3371 __first[__kpmodn] +
3372 __first[(__k - 1) % __n]);
3373 __first[__kpmodn] ^= __r;
3374 __r -= __kmodn;
3375 __first[(__k + __q) % __n] ^= __r;
3376 __first[__kmodn] = __r;
3377 }
3378 }
3379}
3380
Howard Hinnant30a840f2010-05-12 17:08:57 +00003381// generate_canonical
3382
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003383template<class _RealType, size_t __bits, class _URNG>
3384_RealType
3385generate_canonical(_URNG& __g)
3386{
3387 const size_t _Dt = numeric_limits<_RealType>::digits;
3388 const size_t __b = _Dt < __bits ? _Dt : __bits;
3389 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
3390 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
3391 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
3392 _RealType __base = _R;
3393 _RealType _S = __g() - _URNG::_Min;
3394 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
3395 _S += (__g() - _URNG::_Min) * __base;
3396 return _S / __base;
3397}
3398
3399// __independent_bits_engine
3400
3401template<class _Engine, class _UIntType>
3402class __independent_bits_engine
3403{
3404public:
3405 // types
3406 typedef _UIntType result_type;
3407
3408private:
3409 typedef typename _Engine::result_type _Engine_result_type;
3410 typedef typename conditional
3411 <
3412 sizeof(_Engine_result_type) <= sizeof(result_type),
3413 result_type,
3414 _Engine_result_type
3415 >::type _Working_result_type;
3416
3417 _Engine& __e_;
3418 size_t __w_;
3419 size_t __w0_;
3420 size_t __n_;
3421 size_t __n0_;
3422 _Working_result_type __y0_;
3423 _Working_result_type __y1_;
3424 _Engine_result_type __mask0_;
3425 _Engine_result_type __mask1_;
3426
3427 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
3428 + _Working_result_type(1);
3429 static const size_t __m = __log2<_Working_result_type, _R>::value;
3430 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
3431 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
3432
3433public:
3434 // constructors and seeding functions
3435 __independent_bits_engine(_Engine& __e, size_t __w);
3436
3437 // generating functions
3438 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
3439
3440private:
3441 result_type __eval(false_type);
3442 result_type __eval(true_type);
3443};
3444
3445template<class _Engine, class _UIntType>
3446__independent_bits_engine<_Engine, _UIntType>
3447 ::__independent_bits_engine(_Engine& __e, size_t __w)
3448 : __e_(__e),
3449 __w_(__w)
3450{
3451 __n_ = __w_ / __m + (__w_ % __m != 0);
3452 __w0_ = __w_ / __n_;
3453 if (_R == 0)
3454 __y0_ = _R;
3455 else if (__w0_ < _WDt)
3456 __y0_ = (_R >> __w0_) << __w0_;
3457 else
3458 __y0_ = 0;
3459 if (_R - __y0_ > __y0_ / __n_)
3460 {
3461 ++__n_;
3462 __w0_ = __w_ / __n_;
3463 if (__w0_ < _WDt)
3464 __y0_ = (_R >> __w0_) << __w0_;
3465 else
3466 __y0_ = 0;
3467 }
3468 __n0_ = __n_ - __w_ % __n_;
3469 if (__w0_ < _WDt - 1)
3470 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
3471 else
3472 __y1_ = 0;
3473 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
3474 _Engine_result_type(0);
3475 __mask1_ = __w0_ < _EDt - 1 ?
3476 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
3477 _Engine_result_type(~0);
3478}
3479
3480template<class _Engine, class _UIntType>
3481inline
3482_UIntType
3483__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
3484{
3485 return static_cast<result_type>(__e_() & __mask0_);
3486}
3487
3488template<class _Engine, class _UIntType>
3489_UIntType
3490__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
3491{
3492 result_type _S = 0;
3493 for (size_t __k = 0; __k < __n0_; ++__k)
3494 {
3495 _Engine_result_type __u;
3496 do
3497 {
3498 __u = __e_() - _Engine::min();
3499 } while (__u >= __y0_);
3500 if (__w0_ < _EDt)
3501 _S <<= __w0_;
3502 else
3503 _S = 0;
3504 _S += __u & __mask0_;
3505 }
3506 for (size_t __k = __n0_; __k < __n_; ++__k)
3507 {
3508 _Engine_result_type __u;
3509 do
3510 {
3511 __u = __e_() - _Engine::min();
3512 } while (__u >= __y1_);
3513 if (__w0_ < _EDt - 1)
3514 _S <<= __w0_ + 1;
3515 else
3516 _S = 0;
3517 _S += __u & __mask1_;
3518 }
3519 return _S;
3520}
3521
Howard Hinnant30a840f2010-05-12 17:08:57 +00003522// uniform_int_distribution
3523
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003524template<class _IntType = int>
3525class uniform_int_distribution
3526{
3527public:
3528 // types
3529 typedef _IntType result_type;
3530
3531 class param_type
3532 {
3533 result_type __a_;
3534 result_type __b_;
3535 public:
3536 typedef uniform_int_distribution distribution_type;
3537
3538 explicit param_type(result_type __a = 0,
3539 result_type __b = numeric_limits<result_type>::max())
3540 : __a_(__a), __b_(__b) {}
3541
3542 result_type a() const {return __a_;}
3543 result_type b() const {return __b_;}
3544
3545 friend bool operator==(const param_type& __x, const param_type& __y)
3546 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3547 friend bool operator!=(const param_type& __x, const param_type& __y)
3548 {return !(__x == __y);}
3549 };
3550
3551private:
3552 param_type __p_;
3553
3554public:
3555 // constructors and reset functions
3556 explicit uniform_int_distribution(result_type __a = 0,
3557 result_type __b = numeric_limits<result_type>::max())
3558 : __p_(param_type(__a, __b)) {}
3559 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3560 void reset() {}
3561
3562 // generating functions
3563 template<class _URNG> result_type operator()(_URNG& __g)
3564 {return (*this)(__g, __p_);}
3565 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3566
3567 // property functions
3568 result_type a() const {return __p_.a();}
3569 result_type b() const {return __p_.b();}
3570
3571 param_type param() const {return __p_;}
3572 void param(const param_type& __p) {__p_ = __p;}
3573
3574 result_type min() const {return a();}
3575 result_type max() const {return b();}
3576
3577 friend bool operator==(const uniform_int_distribution& __x,
3578 const uniform_int_distribution& __y)
3579 {return __x.__p_ == __y.__p_;}
3580 friend bool operator!=(const uniform_int_distribution& __x,
3581 const uniform_int_distribution& __y)
3582 {return !(__x == __y);}
3583};
3584
3585template<class _IntType>
3586template<class _URNG>
3587typename uniform_int_distribution<_IntType>::result_type
3588uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3589{
3590 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3591 uint32_t, uint64_t>::type _UIntType;
3592 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
3593 if (_R == 1)
3594 return __p.a();
3595 const size_t _Dt = numeric_limits<_UIntType>::digits;
3596 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
3597 if (_R == 0)
3598 return static_cast<result_type>(_Eng(__g, _Dt)());
3599 size_t __w = _Dt - __clz(_R) - 1;
3600 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
3601 ++__w;
3602 _Eng __e(__g, __w);
3603 _UIntType __u;
3604 do
3605 {
3606 __u = __e();
3607 } while (__u >= _R);
3608 return static_cast<result_type>(__u + __p.a());
3609}
3610
3611template <class _CharT, class _Traits, class _IT>
3612basic_ostream<_CharT, _Traits>&
3613operator<<(basic_ostream<_CharT, _Traits>& __os,
3614 const uniform_int_distribution<_IT>& __x)
3615{
3616 __save_flags<_CharT, _Traits> _(__os);
3617 __os.flags(ios_base::dec | ios_base::left);
3618 _CharT __sp = __os.widen(' ');
3619 __os.fill(__sp);
3620 return __os << __x.a() << __sp << __x.b();
3621}
3622
3623template <class _CharT, class _Traits, class _IT>
3624basic_istream<_CharT, _Traits>&
3625operator>>(basic_istream<_CharT, _Traits>& __is,
3626 uniform_int_distribution<_IT>& __x)
3627{
3628 typedef uniform_int_distribution<_IT> _Eng;
3629 typedef typename _Eng::result_type result_type;
3630 typedef typename _Eng::param_type param_type;
3631 __save_flags<_CharT, _Traits> _(__is);
3632 __is.flags(ios_base::dec | ios_base::skipws);
3633 result_type __a;
3634 result_type __b;
3635 __is >> __a >> __b;
3636 if (!__is.fail())
3637 __x.param(param_type(__a, __b));
3638 return __is;
3639}
3640
Howard Hinnant30a840f2010-05-12 17:08:57 +00003641// uniform_real_distribution
3642
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643template<class _RealType = double>
3644class uniform_real_distribution
3645{
3646public:
3647 // types
3648 typedef _RealType result_type;
3649
3650 class param_type
3651 {
3652 result_type __a_;
3653 result_type __b_;
3654 public:
3655 typedef uniform_real_distribution distribution_type;
3656
3657 explicit param_type(result_type __a = 0,
3658 result_type __b = 1)
3659 : __a_(__a), __b_(__b) {}
3660
3661 result_type a() const {return __a_;}
3662 result_type b() const {return __b_;}
3663
3664 friend bool operator==(const param_type& __x, const param_type& __y)
3665 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3666 friend bool operator!=(const param_type& __x, const param_type& __y)
3667 {return !(__x == __y);}
3668 };
3669
3670private:
3671 param_type __p_;
3672
3673public:
3674 // constructors and reset functions
3675 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3676 : __p_(param_type(__a, __b)) {}
3677 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
3678 void reset() {}
3679
3680 // generating functions
3681 template<class _URNG> result_type operator()(_URNG& __g)
3682 {return (*this)(__g, __p_);}
3683 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3684
3685 // property functions
3686 result_type a() const {return __p_.a();}
3687 result_type b() const {return __p_.b();}
3688
3689 param_type param() const {return __p_;}
3690 void param(const param_type& __p) {__p_ = __p;}
3691
3692 result_type min() const {return a();}
3693 result_type max() const {return b();}
3694
3695 friend bool operator==(const uniform_real_distribution& __x,
3696 const uniform_real_distribution& __y)
3697 {return __x.__p_ == __y.__p_;}
3698 friend bool operator!=(const uniform_real_distribution& __x,
3699 const uniform_real_distribution& __y)
3700 {return !(__x == __y);}
3701};
3702
3703template<class _RealType>
3704template<class _URNG>
3705inline
3706typename uniform_real_distribution<_RealType>::result_type
3707uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3708{
3709 return (__p.b() - __p.a())
3710 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3711 + __p.a();
3712}
3713
3714template <class _CharT, class _Traits, class _RT>
3715basic_ostream<_CharT, _Traits>&
3716operator<<(basic_ostream<_CharT, _Traits>& __os,
3717 const uniform_real_distribution<_RT>& __x)
3718{
3719 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003720 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3721 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003722 _CharT __sp = __os.widen(' ');
3723 __os.fill(__sp);
3724 return __os << __x.a() << __sp << __x.b();
3725}
3726
3727template <class _CharT, class _Traits, class _RT>
3728basic_istream<_CharT, _Traits>&
3729operator>>(basic_istream<_CharT, _Traits>& __is,
3730 uniform_real_distribution<_RT>& __x)
3731{
3732 typedef uniform_real_distribution<_RT> _Eng;
3733 typedef typename _Eng::result_type result_type;
3734 typedef typename _Eng::param_type param_type;
3735 __save_flags<_CharT, _Traits> _(__is);
3736 __is.flags(ios_base::dec | ios_base::skipws);
3737 result_type __a;
3738 result_type __b;
3739 __is >> __a >> __b;
3740 if (!__is.fail())
3741 __x.param(param_type(__a, __b));
3742 return __is;
3743}
3744
Howard Hinnant30a840f2010-05-12 17:08:57 +00003745// bernoulli_distribution
3746
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747class bernoulli_distribution
3748{
3749public:
3750 // types
3751 typedef bool result_type;
3752
3753 class param_type
3754 {
3755 double __p_;
3756 public:
3757 typedef bernoulli_distribution distribution_type;
3758
3759 explicit param_type(double __p = 0.5) : __p_(__p) {}
3760
3761 double p() const {return __p_;}
3762
3763 friend bool operator==(const param_type& __x, const param_type& __y)
3764 {return __x.__p_ == __y.__p_;}
3765 friend bool operator!=(const param_type& __x, const param_type& __y)
3766 {return !(__x == __y);}
3767 };
3768
3769private:
3770 param_type __p_;
3771
3772public:
3773 // constructors and reset functions
3774 explicit bernoulli_distribution(double __p = 0.5)
3775 : __p_(param_type(__p)) {}
3776 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3777 void reset() {}
3778
3779 // generating functions
3780 template<class _URNG> result_type operator()(_URNG& __g)
3781 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00003782 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783
3784 // property functions
3785 double p() const {return __p_.p();}
3786
3787 param_type param() const {return __p_;}
3788 void param(const param_type& __p) {__p_ = __p;}
3789
3790 result_type min() const {return false;}
3791 result_type max() const {return true;}
3792
3793 friend bool operator==(const bernoulli_distribution& __x,
3794 const bernoulli_distribution& __y)
3795 {return __x.__p_ == __y.__p_;}
3796 friend bool operator!=(const bernoulli_distribution& __x,
3797 const bernoulli_distribution& __y)
3798 {return !(__x == __y);}
3799};
3800
Howard Hinnant03aad812010-05-11 23:26:59 +00003801template<class _URNG>
3802inline
3803bernoulli_distribution::result_type
3804bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3805{
Howard Hinnantd6d11712010-05-20 15:11:46 +00003806 uniform_real_distribution<double> __gen;
3807 return __gen(__g) < __p.p();
Howard Hinnant03aad812010-05-11 23:26:59 +00003808}
3809
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003810template <class _CharT, class _Traits>
3811basic_ostream<_CharT, _Traits>&
3812operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3813{
3814 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003815 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3816 ios_base::scientific);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817 _CharT __sp = __os.widen(' ');
3818 __os.fill(__sp);
3819 return __os << __x.p();
3820}
3821
3822template <class _CharT, class _Traits>
3823basic_istream<_CharT, _Traits>&
3824operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3825{
3826 typedef bernoulli_distribution _Eng;
3827 typedef typename _Eng::param_type param_type;
3828 __save_flags<_CharT, _Traits> _(__is);
3829 __is.flags(ios_base::dec | ios_base::skipws);
3830 double __p;
3831 __is >> __p;
3832 if (!__is.fail())
3833 __x.param(param_type(__p));
3834 return __is;
3835}
3836
Howard Hinnant30a840f2010-05-12 17:08:57 +00003837// binomial_distribution
3838
Howard Hinnant03aad812010-05-11 23:26:59 +00003839template<class _IntType = int>
3840class binomial_distribution
3841{
3842public:
3843 // types
3844 typedef _IntType result_type;
3845
3846 class param_type
3847 {
3848 result_type __t_;
3849 double __p_;
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003850 double __pr_;
3851 double __odds_ratio_;
3852 result_type __r0_;
Howard Hinnant03aad812010-05-11 23:26:59 +00003853 public:
3854 typedef binomial_distribution distribution_type;
3855
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003856 explicit param_type(result_type __t = 1, double __p = 0.5);
Howard Hinnant03aad812010-05-11 23:26:59 +00003857
3858 result_type t() const {return __t_;}
3859 double p() const {return __p_;}
3860
3861 friend bool operator==(const param_type& __x, const param_type& __y)
3862 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3863 friend bool operator!=(const param_type& __x, const param_type& __y)
3864 {return !(__x == __y);}
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003865
3866 friend class binomial_distribution;
Howard Hinnant03aad812010-05-11 23:26:59 +00003867 };
3868
3869private:
3870 param_type __p_;
3871
3872public:
3873 // constructors and reset functions
3874 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3875 : __p_(param_type(__t, __p)) {}
3876 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3877 void reset() {}
3878
3879 // generating functions
3880 template<class _URNG> result_type operator()(_URNG& __g)
3881 {return (*this)(__g, __p_);}
3882 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3883
3884 // property functions
3885 result_type t() const {return __p_.t();}
3886 double p() const {return __p_.p();}
3887
3888 param_type param() const {return __p_;}
3889 void param(const param_type& __p) {__p_ = __p;}
3890
3891 result_type min() const {return 0;}
3892 result_type max() const {return t();}
3893
3894 friend bool operator==(const binomial_distribution& __x,
3895 const binomial_distribution& __y)
3896 {return __x.__p_ == __y.__p_;}
3897 friend bool operator!=(const binomial_distribution& __x,
3898 const binomial_distribution& __y)
3899 {return !(__x == __y);}
3900};
3901
3902template<class _IntType>
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003903binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3904 : __t_(__t), __p_(__p)
3905{
3906 if (0 < __p_ && __p_ < 1)
3907 {
3908 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3909 __pr_ = _STD::exp(_STD::lgamma(__t_ + 1.) - _STD::lgamma(__r0_ + 1.) -
3910 _STD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _STD::log(__p_) +
3911 (__t_ - __r0_) * _STD::log(1 - __p_));
3912 __odds_ratio_ = __p_ / (1 - __p_);
3913 }
3914}
3915
3916template<class _IntType>
Howard Hinnant03aad812010-05-11 23:26:59 +00003917template<class _URNG>
3918_IntType
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003919binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
Howard Hinnant03aad812010-05-11 23:26:59 +00003920{
Howard Hinnant6add8dd2010-05-15 21:36:23 +00003921 if (__pr.__t_ == 0 || __pr.__p_ == 0)
3922 return 0;
3923 if (__pr.__p_ == 1)
3924 return __pr.__t_;
3925 uniform_real_distribution<double> __gen;
3926 double __u = __gen(__g) - __pr.__pr_;
3927 if (__u < 0)
3928 return __pr.__r0_;
3929 double __pu = __pr.__pr_;
3930 double __pd = __pu;
3931 result_type __ru = __pr.__r0_;
3932 result_type __rd = __ru;
3933 while (true)
3934 {
3935 if (__rd >= 1)
3936 {
3937 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3938 __u -= __pd;
3939 if (__u < 0)
3940 return __rd - 1;
3941 }
3942 --__rd;
3943 ++__ru;
3944 if (__ru <= __pr.__t_)
3945 {
3946 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3947 __u -= __pu;
3948 if (__u < 0)
3949 return __ru;
3950 }
3951 }
Howard Hinnant03aad812010-05-11 23:26:59 +00003952}
3953
3954template <class _CharT, class _Traits, class _IntType>
3955basic_ostream<_CharT, _Traits>&
3956operator<<(basic_ostream<_CharT, _Traits>& __os,
3957 const binomial_distribution<_IntType>& __x)
3958{
3959 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00003960 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3961 ios_base::scientific);
Howard Hinnant03aad812010-05-11 23:26:59 +00003962 _CharT __sp = __os.widen(' ');
3963 __os.fill(__sp);
3964 return __os << __x.t() << __sp << __x.p();
3965}
3966
3967template <class _CharT, class _Traits, class _IntType>
3968basic_istream<_CharT, _Traits>&
3969operator>>(basic_istream<_CharT, _Traits>& __is,
3970 binomial_distribution<_IntType>& __x)
3971{
3972 typedef binomial_distribution<_IntType> _Eng;
3973 typedef typename _Eng::result_type result_type;
3974 typedef typename _Eng::param_type param_type;
3975 __save_flags<_CharT, _Traits> _(__is);
3976 __is.flags(ios_base::dec | ios_base::skipws);
3977 result_type __t;
3978 double __p;
3979 __is >> __t >> __p;
3980 if (!__is.fail())
3981 __x.param(param_type(__t, __p));
3982 return __is;
3983}
3984
Howard Hinnant30a840f2010-05-12 17:08:57 +00003985// exponential_distribution
3986
3987template<class _RealType = double>
3988class exponential_distribution
3989{
3990public:
3991 // types
3992 typedef _RealType result_type;
3993
3994 class param_type
3995 {
3996 result_type __lambda_;
3997 public:
3998 typedef exponential_distribution distribution_type;
3999
4000 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
4001
4002 result_type lambda() const {return __lambda_;}
4003
4004 friend bool operator==(const param_type& __x, const param_type& __y)
4005 {return __x.__lambda_ == __y.__lambda_;}
4006 friend bool operator!=(const param_type& __x, const param_type& __y)
4007 {return !(__x == __y);}
4008 };
4009
4010private:
4011 param_type __p_;
4012
4013public:
4014 // constructors and reset functions
4015 explicit exponential_distribution(result_type __lambda = 1)
4016 : __p_(param_type(__lambda)) {}
4017 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
4018 void reset() {}
4019
4020 // generating functions
4021 template<class _URNG> result_type operator()(_URNG& __g)
4022 {return (*this)(__g, __p_);}
4023 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4024
4025 // property functions
4026 result_type lambda() const {return __p_.lambda();}
4027
4028 param_type param() const {return __p_;}
4029 void param(const param_type& __p) {__p_ = __p;}
4030
4031 result_type min() const {return 0;}
Howard Hinnantdf40dc62010-05-16 17:56:20 +00004032 result_type max() const {return numeric_limits<result_type>::infinity();}
Howard Hinnant30a840f2010-05-12 17:08:57 +00004033
4034 friend bool operator==(const exponential_distribution& __x,
4035 const exponential_distribution& __y)
4036 {return __x.__p_ == __y.__p_;}
4037 friend bool operator!=(const exponential_distribution& __x,
4038 const exponential_distribution& __y)
4039 {return !(__x == __y);}
4040};
4041
4042template <class _RealType>
4043template<class _URNG>
4044_RealType
4045exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4046{
4047 return -_STD::log
4048 (
4049 result_type(1) -
4050 _STD::generate_canonical<result_type,
4051 numeric_limits<result_type>::digits>(__g)
4052 )
4053 / __p.lambda();
4054}
4055
4056template <class _CharT, class _Traits, class _RealType>
4057basic_ostream<_CharT, _Traits>&
4058operator<<(basic_ostream<_CharT, _Traits>& __os,
4059 const exponential_distribution<_RealType>& __x)
4060{
4061 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004062 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4063 ios_base::scientific);
Howard Hinnant30a840f2010-05-12 17:08:57 +00004064 return __os << __x.lambda();
4065}
4066
4067template <class _CharT, class _Traits, class _RealType>
4068basic_istream<_CharT, _Traits>&
4069operator>>(basic_istream<_CharT, _Traits>& __is,
4070 exponential_distribution<_RealType>& __x)
4071{
4072 typedef exponential_distribution<_RealType> _Eng;
4073 typedef typename _Eng::result_type result_type;
4074 typedef typename _Eng::param_type param_type;
4075 __save_flags<_CharT, _Traits> _(__is);
4076 __is.flags(ios_base::dec | ios_base::skipws);
4077 result_type __lambda;
4078 __is >> __lambda;
4079 if (!__is.fail())
4080 __x.param(param_type(__lambda));
4081 return __is;
4082}
4083
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004084// normal_distribution
4085
4086template<class _RealType = double>
4087class normal_distribution
4088{
4089public:
4090 // types
4091 typedef _RealType result_type;
4092
4093 class param_type
4094 {
4095 result_type __mean_;
4096 result_type __stddev_;
4097 public:
4098 typedef normal_distribution distribution_type;
4099
4100 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4101 : __mean_(__mean), __stddev_(__stddev) {}
4102
4103 result_type mean() const {return __mean_;}
4104 result_type stddev() const {return __stddev_;}
4105
4106 friend bool operator==(const param_type& __x, const param_type& __y)
4107 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
4108 friend bool operator!=(const param_type& __x, const param_type& __y)
4109 {return !(__x == __y);}
4110 };
4111
4112private:
4113 param_type __p_;
4114 result_type _V_;
4115 bool _V_hot_;
4116
4117public:
4118 // constructors and reset functions
4119 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4120 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
4121 explicit normal_distribution(const param_type& __p)
4122 : __p_(__p), _V_hot_(false) {}
4123 void reset() {_V_hot_ = false;}
4124
4125 // generating functions
4126 template<class _URNG> result_type operator()(_URNG& __g)
4127 {return (*this)(__g, __p_);}
4128 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4129
4130 // property functions
4131 result_type mean() const {return __p_.mean();}
4132 result_type stddev() const {return __p_.stddev();}
4133
4134 param_type param() const {return __p_;}
4135 void param(const param_type& __p) {__p_ = __p;}
4136
4137 result_type min() const {return -numeric_limits<result_type>::infinity();}
4138 result_type max() const {return numeric_limits<result_type>::infinity();}
4139
4140 friend bool operator==(const normal_distribution& __x,
4141 const normal_distribution& __y)
4142 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4143 (!__x._V_hot_ || __x._V_ == __y._V_);}
4144 friend bool operator!=(const normal_distribution& __x,
4145 const normal_distribution& __y)
4146 {return !(__x == __y);}
4147
4148 template <class _CharT, class _Traits, class _RT>
4149 friend
4150 basic_ostream<_CharT, _Traits>&
4151 operator<<(basic_ostream<_CharT, _Traits>& __os,
4152 const normal_distribution<_RT>& __x);
4153
4154 template <class _CharT, class _Traits, class _RT>
4155 friend
4156 basic_istream<_CharT, _Traits>&
4157 operator>>(basic_istream<_CharT, _Traits>& __is,
4158 normal_distribution<_RT>& __x);
4159};
4160
4161template <class _RealType>
4162template<class _URNG>
4163_RealType
4164normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4165{
4166 result_type _U;
4167 if (_V_hot_)
4168 {
4169 _V_hot_ = false;
4170 _U = _V_;
4171 }
4172 else
4173 {
4174 uniform_real_distribution<result_type> _Uni(-1, 1);
4175 result_type __u;
4176 result_type __v;
4177 result_type __s;
4178 do
4179 {
4180 __u = _Uni(__g);
4181 __v = _Uni(__g);
4182 __s = __u * __u + __v * __v;
4183 } while (__s > 1 || __s == 0);
4184 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
4185 _V_ = __v * _F;
4186 _V_hot_ = true;
4187 _U = __u * _F;
4188 }
4189 return _U * __p.stddev() + __p.mean();
4190}
4191
4192template <class _CharT, class _Traits, class _RT>
4193basic_ostream<_CharT, _Traits>&
4194operator<<(basic_ostream<_CharT, _Traits>& __os,
4195 const normal_distribution<_RT>& __x)
4196{
4197 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004198 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4199 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004200 _CharT __sp = __os.widen(' ');
4201 __os.fill(__sp);
4202 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4203 if (__x._V_hot_)
4204 __os << __sp << __x._V_;
4205 return __os;
4206}
4207
4208template <class _CharT, class _Traits, class _RT>
4209basic_istream<_CharT, _Traits>&
4210operator>>(basic_istream<_CharT, _Traits>& __is,
4211 normal_distribution<_RT>& __x)
4212{
4213 typedef normal_distribution<_RT> _Eng;
4214 typedef typename _Eng::result_type result_type;
4215 typedef typename _Eng::param_type param_type;
4216 __save_flags<_CharT, _Traits> _(__is);
4217 __is.flags(ios_base::dec | ios_base::skipws);
4218 result_type __mean;
4219 result_type __stddev;
4220 result_type _V = 0;
4221 bool _V_hot = false;
4222 __is >> __mean >> __stddev >> _V_hot;
4223 if (_V_hot)
4224 __is >> _V;
4225 if (!__is.fail())
4226 {
4227 __x.param(param_type(__mean, __stddev));
4228 __x._V_hot_ = _V_hot;
4229 __x._V_ = _V;
4230 }
4231 return __is;
4232}
4233
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004234// lognormal_distribution
4235
4236template<class _RealType = double>
4237class lognormal_distribution
4238{
4239public:
4240 // types
4241 typedef _RealType result_type;
4242
4243 class param_type
4244 {
4245 normal_distribution<result_type> __nd_;
4246 public:
4247 typedef lognormal_distribution distribution_type;
4248
4249 explicit param_type(result_type __m = 0, result_type __s = 1)
4250 : __nd_(__m, __s) {}
4251
4252 result_type m() const {return __nd_.mean();}
4253 result_type s() const {return __nd_.stddev();}
4254
4255 friend bool operator==(const param_type& __x, const param_type& __y)
4256 {return __x.__nd_ == __y.__nd_;}
4257 friend bool operator!=(const param_type& __x, const param_type& __y)
4258 {return !(__x == __y);}
4259 friend class lognormal_distribution;
4260
4261 template <class _CharT, class _Traits, class _RT>
4262 friend
4263 basic_ostream<_CharT, _Traits>&
4264 operator<<(basic_ostream<_CharT, _Traits>& __os,
4265 const lognormal_distribution<_RT>& __x);
4266
4267 template <class _CharT, class _Traits, class _RT>
4268 friend
4269 basic_istream<_CharT, _Traits>&
4270 operator>>(basic_istream<_CharT, _Traits>& __is,
4271 lognormal_distribution<_RT>& __x);
4272 };
4273
4274private:
4275 param_type __p_;
4276
4277public:
4278 // constructor and reset functions
4279 explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4280 : __p_(param_type(__m, __s)) {}
4281 explicit lognormal_distribution(const param_type& __p)
4282 : __p_(__p) {}
4283 void reset() {__p_.__nd_.reset();}
4284
4285 // generating functions
4286 template<class _URNG> result_type operator()(_URNG& __g)
4287 {return (*this)(__g, __p_);}
4288 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4289 {return _STD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
4290
4291 // property functions
4292 result_type m() const {return __p_.m();}
4293 result_type s() const {return __p_.s();}
4294
4295 param_type param() const {return __p_;}
Howard Hinnant551d8e42010-05-19 01:53:57 +00004296 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant2bc36fc2010-05-17 18:31:53 +00004297
4298 result_type min() const {return 0;}
4299 result_type max() const {return numeric_limits<result_type>::infinity();}
4300
4301 friend bool operator==(const lognormal_distribution& __x,
4302 const lognormal_distribution& __y)
4303 {return __x.__p_ == __y.__p_;}
4304 friend bool operator!=(const lognormal_distribution& __x,
4305 const lognormal_distribution& __y)
4306 {return !(__x == __y);}
4307
4308 template <class _CharT, class _Traits, class _RT>
4309 friend
4310 basic_ostream<_CharT, _Traits>&
4311 operator<<(basic_ostream<_CharT, _Traits>& __os,
4312 const lognormal_distribution<_RT>& __x);
4313
4314 template <class _CharT, class _Traits, class _RT>
4315 friend
4316 basic_istream<_CharT, _Traits>&
4317 operator>>(basic_istream<_CharT, _Traits>& __is,
4318 lognormal_distribution<_RT>& __x);
4319};
4320
4321template <class _CharT, class _Traits, class _RT>
4322inline
4323basic_ostream<_CharT, _Traits>&
4324operator<<(basic_ostream<_CharT, _Traits>& __os,
4325 const lognormal_distribution<_RT>& __x)
4326{
4327 return __os << __x.__p_.__nd_;
4328}
4329
4330template <class _CharT, class _Traits, class _RT>
4331inline
4332basic_istream<_CharT, _Traits>&
4333operator>>(basic_istream<_CharT, _Traits>& __is,
4334 lognormal_distribution<_RT>& __x)
4335{
4336 return __is >> __x.__p_.__nd_;
4337}
4338
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004339// poisson_distribution
4340
4341template<class _IntType = int>
4342class poisson_distribution
4343{
4344public:
4345 // types
4346 typedef _IntType result_type;
4347
4348 class param_type
4349 {
4350 double __mean_;
4351 double __s_;
4352 double __d_;
4353 double __l_;
4354 double __omega_;
4355 double __c0_;
4356 double __c1_;
4357 double __c2_;
4358 double __c3_;
4359 double __c_;
4360
4361 public:
4362 typedef poisson_distribution distribution_type;
4363
4364 explicit param_type(double __mean = 1.0);
4365
4366 double mean() const {return __mean_;}
4367
4368 friend bool operator==(const param_type& __x, const param_type& __y)
4369 {return __x.__mean_ == __y.__mean_;}
4370 friend bool operator!=(const param_type& __x, const param_type& __y)
4371 {return !(__x == __y);}
4372
4373 friend class poisson_distribution;
4374 };
4375
4376private:
4377 param_type __p_;
4378
4379public:
4380 // constructors and reset functions
4381 explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
4382 explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
4383 void reset() {}
4384
4385 // generating functions
4386 template<class _URNG> result_type operator()(_URNG& __g)
4387 {return (*this)(__g, __p_);}
4388 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4389
4390 // property functions
4391 double mean() const {return __p_.mean();}
4392
4393 param_type param() const {return __p_;}
4394 void param(const param_type& __p) {__p_ = __p;}
4395
4396 result_type min() const {return 0;}
4397 result_type max() const {return numeric_limits<result_type>::max();}
4398
4399 friend bool operator==(const poisson_distribution& __x,
4400 const poisson_distribution& __y)
4401 {return __x.__p_ == __y.__p_;}
4402 friend bool operator!=(const poisson_distribution& __x,
4403 const poisson_distribution& __y)
4404 {return !(__x == __y);}
4405};
4406
4407template<class _IntType>
4408poisson_distribution<_IntType>::param_type::param_type(double __mean)
4409 : __mean_(__mean)
4410{
4411 if (__mean_ < 10)
4412 {
4413 __s_ = 0;
4414 __d_ = 0;
4415 __l_ = _STD::exp(-__mean_);
4416 __omega_ = 0;
4417 __c3_ = 0;
4418 __c2_ = 0;
4419 __c1_ = 0;
4420 __c0_ = 0;
4421 __c_ = 0;
4422 }
4423 else
4424 {
4425 __s_ = _STD::sqrt(__mean_);
4426 __d_ = 6 * __mean_ * __mean_;
4427 __l_ = static_cast<result_type>(__mean_ - 1.1484);
4428 __omega_ = .3989423 / __s_;
4429 double __b1_ = .4166667E-1 / __mean_;
4430 double __b2_ = .3 * __b1_ * __b1_;
4431 __c3_ = .1428571 * __b1_ * __b2_;
4432 __c2_ = __b2_ - 15. * __c3_;
4433 __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4434 __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4435 __c_ = .1069 / __mean_;
4436 }
4437}
4438
4439template <class _IntType>
4440template<class _URNG>
4441_IntType
4442poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4443{
4444 result_type __x;
4445 uniform_real_distribution<double> __urd;
4446 if (__pr.__mean_ <= 10)
4447 {
4448 __x = 0;
4449 for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4450 __p *= __urd(__urng);
4451 }
4452 else
4453 {
4454 double __difmuk;
4455 double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4456 double __u;
4457 if (__g > 0)
4458 {
4459 __x = static_cast<result_type>(__g);
4460 if (__x >= __pr.__l_)
4461 return __x;
4462 __difmuk = __pr.__mean_ - __x;
4463 __u = __urd(__urng);
4464 if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4465 return __x;
4466 }
4467 exponential_distribution<double> __edist;
4468 for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4469 {
4470 double __e;
4471 if (__using_exp_dist || __g < 0)
4472 {
4473 double __t;
4474 do
4475 {
4476 __e = __edist(__urng);
4477 __u = __urd(__urng);
4478 __u += __u - 1;
4479 __t = 1.8 + (__u < 0 ? -__e : __e);
4480 } while (__t <= -.6744);
4481 __x = __pr.__mean_ + __pr.__s_ * __t;
4482 __difmuk = __pr.__mean_ - __x;
4483 __using_exp_dist = true;
4484 }
4485 double __px;
4486 double __py;
4487 if (__x < 10)
4488 {
4489 const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4490 40320, 362880};
4491 __px = -__pr.__mean_;
4492 __py = _STD::pow(__pr.__mean_, (double)__x) / __fac[__x];
4493 }
4494 else
4495 {
4496 double __del = .8333333E-1 / __x;
4497 __del -= 4.8 * __del * __del * __del;
4498 double __v = __difmuk / __x;
4499 if (_STD::abs(__v) > 0.25)
4500 __px = __x * _STD::log(1 + __v) - __difmuk - __del;
4501 else
4502 __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4503 __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4504 __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
4505 __py = .3989423 / _STD::sqrt(__x);
4506 }
4507 double __r = (0.5 - __difmuk) / __pr.__s_;
4508 double __r2 = __r * __r;
4509 double __fx = -0.5 * __r2;
4510 double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4511 __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4512 if (__using_exp_dist)
4513 {
4514 if (__pr.__c_ * _STD::abs(__u) <= __py * _STD::exp(__px + __e) -
4515 __fy * _STD::exp(__fx + __e))
4516 break;
4517 }
4518 else
4519 {
4520 if (__fy - __u * __fy <= __py * _STD::exp(__px - __fx))
4521 break;
4522 }
4523 }
4524 }
4525 return __x;
4526}
4527
4528template <class _CharT, class _Traits, class _IntType>
4529basic_ostream<_CharT, _Traits>&
4530operator<<(basic_ostream<_CharT, _Traits>& __os,
4531 const poisson_distribution<_IntType>& __x)
4532{
4533 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004534 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4535 ios_base::scientific);
Howard Hinnant6add8dd2010-05-15 21:36:23 +00004536 return __os << __x.mean();
4537}
4538
4539template <class _CharT, class _Traits, class _IntType>
4540basic_istream<_CharT, _Traits>&
4541operator>>(basic_istream<_CharT, _Traits>& __is,
4542 poisson_distribution<_IntType>& __x)
4543{
4544 typedef poisson_distribution<_IntType> _Eng;
4545 typedef typename _Eng::param_type param_type;
4546 __save_flags<_CharT, _Traits> _(__is);
4547 __is.flags(ios_base::dec | ios_base::skipws);
4548 double __mean;
4549 __is >> __mean;
4550 if (!__is.fail())
4551 __x.param(param_type(__mean));
4552 return __is;
4553}
4554
Howard Hinnant9de6e302010-05-16 01:09:02 +00004555// weibull_distribution
4556
4557template<class _RealType = double>
4558class weibull_distribution
4559{
4560public:
4561 // types
4562 typedef _RealType result_type;
4563
4564 class param_type
4565 {
4566 result_type __a_;
4567 result_type __b_;
4568 public:
4569 typedef weibull_distribution distribution_type;
4570
4571 explicit param_type(result_type __a = 1, result_type __b = 1)
4572 : __a_(__a), __b_(__b) {}
4573
4574 result_type a() const {return __a_;}
4575 result_type b() const {return __b_;}
4576
4577 friend bool operator==(const param_type& __x, const param_type& __y)
4578 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4579 friend bool operator!=(const param_type& __x, const param_type& __y)
4580 {return !(__x == __y);}
4581 };
4582
4583private:
4584 param_type __p_;
4585
4586public:
4587 // constructor and reset functions
4588 explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4589 : __p_(param_type(__a, __b)) {}
4590 explicit weibull_distribution(const param_type& __p)
4591 : __p_(__p) {}
4592 void reset() {}
4593
4594 // generating functions
4595 template<class _URNG> result_type operator()(_URNG& __g)
4596 {return (*this)(__g, __p_);}
4597 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
4598 {return __p.b() *
4599 _STD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
4600
4601 // property functions
4602 result_type a() const {return __p_.a();}
4603 result_type b() const {return __p_.b();}
4604
4605 param_type param() const {return __p_;}
4606 void param(const param_type& __p) {__p_ = __p;}
4607
4608 result_type min() const {return 0;}
4609 result_type max() const {return numeric_limits<result_type>::infinity();}
4610
4611
4612 friend bool operator==(const weibull_distribution& __x,
4613 const weibull_distribution& __y)
4614 {return __x.__p_ == __y.__p_;}
4615 friend bool operator!=(const weibull_distribution& __x,
4616 const weibull_distribution& __y)
4617 {return !(__x == __y);}
4618};
4619
4620template <class _CharT, class _Traits, class _RT>
4621basic_ostream<_CharT, _Traits>&
4622operator<<(basic_ostream<_CharT, _Traits>& __os,
4623 const weibull_distribution<_RT>& __x)
4624{
4625 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004626 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4627 ios_base::scientific);
Howard Hinnant9de6e302010-05-16 01:09:02 +00004628 _CharT __sp = __os.widen(' ');
4629 __os.fill(__sp);
4630 __os << __x.a() << __sp << __x.b();
4631 return __os;
4632}
4633
4634template <class _CharT, class _Traits, class _RT>
4635basic_istream<_CharT, _Traits>&
4636operator>>(basic_istream<_CharT, _Traits>& __is,
4637 weibull_distribution<_RT>& __x)
4638{
4639 typedef weibull_distribution<_RT> _Eng;
4640 typedef typename _Eng::result_type result_type;
4641 typedef typename _Eng::param_type param_type;
4642 __save_flags<_CharT, _Traits> _(__is);
4643 __is.flags(ios_base::dec | ios_base::skipws);
4644 result_type __a;
4645 result_type __b;
4646 __is >> __a >> __b;
4647 if (!__is.fail())
4648 __x.param(param_type(__a, __b));
4649 return __is;
4650}
4651
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004652template<class _RealType = double>
4653class extreme_value_distribution
4654{
4655public:
4656 // types
4657 typedef _RealType result_type;
4658
4659 class param_type
4660 {
4661 result_type __a_;
4662 result_type __b_;
4663 public:
4664 typedef extreme_value_distribution distribution_type;
4665
4666 explicit param_type(result_type __a = 0, result_type __b = 1)
4667 : __a_(__a), __b_(__b) {}
4668
4669 result_type a() const {return __a_;}
4670 result_type b() const {return __b_;}
4671
4672 friend bool operator==(const param_type& __x, const param_type& __y)
4673 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4674 friend bool operator!=(const param_type& __x, const param_type& __y)
4675 {return !(__x == __y);}
4676 };
4677
4678private:
4679 param_type __p_;
4680
4681public:
4682 // constructor and reset functions
4683 explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4684 : __p_(param_type(__a, __b)) {}
4685 explicit extreme_value_distribution(const param_type& __p)
4686 : __p_(__p) {}
4687 void reset() {}
4688
4689 // generating functions
4690 template<class _URNG> result_type operator()(_URNG& __g)
4691 {return (*this)(__g, __p_);}
4692 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4693
4694 // property functions
4695 result_type a() const {return __p_.a();}
4696 result_type b() const {return __p_.b();}
4697
4698 param_type param() const {return __p_;}
4699 void param(const param_type& __p) {__p_ = __p;}
4700
4701 result_type min() const {return -numeric_limits<result_type>::infinity();}
4702 result_type max() const {return numeric_limits<result_type>::infinity();}
4703
4704 friend bool operator==(const extreme_value_distribution& __x,
4705 const extreme_value_distribution& __y)
4706 {return __x.__p_ == __y.__p_;}
4707 friend bool operator!=(const extreme_value_distribution& __x,
4708 const extreme_value_distribution& __y)
4709 {return !(__x == __y);}
4710};
4711
4712template<class _RealType>
4713template<class _URNG>
4714_RealType
4715extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4716{
4717 return __p.a() - __p.b() *
4718 _STD::log(-_STD::log(1-uniform_real_distribution<result_type>()(__g)));
4719}
4720
4721template <class _CharT, class _Traits, class _RT>
4722basic_ostream<_CharT, _Traits>&
4723operator<<(basic_ostream<_CharT, _Traits>& __os,
4724 const extreme_value_distribution<_RT>& __x)
4725{
4726 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004727 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4728 ios_base::scientific);
Howard Hinnantc2b0dc72010-05-17 16:21:56 +00004729 _CharT __sp = __os.widen(' ');
4730 __os.fill(__sp);
4731 __os << __x.a() << __sp << __x.b();
4732 return __os;
4733}
4734
4735template <class _CharT, class _Traits, class _RT>
4736basic_istream<_CharT, _Traits>&
4737operator>>(basic_istream<_CharT, _Traits>& __is,
4738 extreme_value_distribution<_RT>& __x)
4739{
4740 typedef extreme_value_distribution<_RT> _Eng;
4741 typedef typename _Eng::result_type result_type;
4742 typedef typename _Eng::param_type param_type;
4743 __save_flags<_CharT, _Traits> _(__is);
4744 __is.flags(ios_base::dec | ios_base::skipws);
4745 result_type __a;
4746 result_type __b;
4747 __is >> __a >> __b;
4748 if (!__is.fail())
4749 __x.param(param_type(__a, __b));
4750 return __is;
4751}
4752
Howard Hinnantc7c49132010-05-13 17:58:28 +00004753// gamma_distribution
4754
4755template<class _RealType = double>
4756class gamma_distribution
4757{
4758public:
4759 // types
4760 typedef _RealType result_type;
4761
4762 class param_type
4763 {
4764 result_type __alpha_;
4765 result_type __beta_;
4766 public:
4767 typedef gamma_distribution distribution_type;
4768
4769 explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4770 : __alpha_(__alpha), __beta_(__beta) {}
4771
4772 result_type alpha() const {return __alpha_;}
4773 result_type beta() const {return __beta_;}
4774
4775 friend bool operator==(const param_type& __x, const param_type& __y)
4776 {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
4777 friend bool operator!=(const param_type& __x, const param_type& __y)
4778 {return !(__x == __y);}
4779 };
4780
4781private:
4782 param_type __p_;
4783
4784public:
4785 // constructors and reset functions
4786 explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4787 : __p_(param_type(__alpha, __beta)) {}
4788 explicit gamma_distribution(const param_type& __p)
4789 : __p_(__p) {}
4790 void reset() {}
4791
4792 // generating functions
4793 template<class _URNG> result_type operator()(_URNG& __g)
4794 {return (*this)(__g, __p_);}
4795 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4796
4797 // property functions
4798 result_type alpha() const {return __p_.alpha();}
4799 result_type beta() const {return __p_.beta();}
4800
4801 param_type param() const {return __p_;}
4802 void param(const param_type& __p) {__p_ = __p;}
4803
4804 result_type min() const {return 0;}
4805 result_type max() const {return numeric_limits<result_type>::infinity();}
4806
4807 friend bool operator==(const gamma_distribution& __x,
4808 const gamma_distribution& __y)
4809 {return __x.__p_ == __y.__p_;}
4810 friend bool operator!=(const gamma_distribution& __x,
4811 const gamma_distribution& __y)
4812 {return !(__x == __y);}
4813};
4814
4815template <class _RealType>
4816template<class _URNG>
4817_RealType
4818gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4819{
Howard Hinnantf417abe2010-05-14 18:43:10 +00004820 result_type __a = __p.alpha();
4821 uniform_real_distribution<result_type> __gen(0, 1);
4822 exponential_distribution<result_type> __egen;
4823 result_type __x;
Howard Hinnantc7c49132010-05-13 17:58:28 +00004824 if (__a == 1)
Howard Hinnantf417abe2010-05-14 18:43:10 +00004825 __x = __egen(__g);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004826 else if (__a > 1)
4827 {
4828 const result_type __b = __a - 1;
4829 const result_type __c = 3 * __a - result_type(0.75);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004830 while (true)
4831 {
4832 const result_type __u = __gen(__g);
4833 const result_type __v = __gen(__g);
4834 const result_type __w = __u * (1 - __u);
Howard Hinnantf417abe2010-05-14 18:43:10 +00004835 if (__w != 0)
Howard Hinnantc7c49132010-05-13 17:58:28 +00004836 {
4837 const result_type __y = _STD::sqrt(__c / __w) *
4838 (__u - result_type(0.5));
4839 __x = __b + __y;
4840 if (__x >= 0)
4841 {
4842 const result_type __z = 64 * __w * __w * __w * __v * __v;
4843 if (__z <= 1 - 2 * __y * __y / __x)
4844 break;
4845 if (_STD::log(__z) <= 2 * (__b * _STD::log(__x / __b) - __y))
4846 break;
4847 }
4848 }
4849 }
Howard Hinnantc7c49132010-05-13 17:58:28 +00004850 }
Howard Hinnantf417abe2010-05-14 18:43:10 +00004851 else // __a < 1
4852 {
4853 while (true)
4854 {
4855 const result_type __u = __gen(__g);
4856 const result_type __es = __egen(__g);
4857 if (__u <= 1 - __a)
4858 {
4859 __x = _STD::pow(__u, 1 / __a);
4860 if (__x <= __es)
4861 break;
4862 }
4863 else
4864 {
4865 const result_type __e = -_STD::log((1-__u)/__a);
4866 __x = _STD::pow(1 - __a + __a * __e, 1 / __a);
4867 if (__x <= __e + __es)
4868 break;
4869 }
4870 }
4871 }
4872 return __x * __p.beta();
Howard Hinnantc7c49132010-05-13 17:58:28 +00004873}
4874
4875template <class _CharT, class _Traits, class _RT>
4876basic_ostream<_CharT, _Traits>&
4877operator<<(basic_ostream<_CharT, _Traits>& __os,
4878 const gamma_distribution<_RT>& __x)
4879{
4880 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004881 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4882 ios_base::scientific);
Howard Hinnantc7c49132010-05-13 17:58:28 +00004883 _CharT __sp = __os.widen(' ');
4884 __os.fill(__sp);
4885 __os << __x.alpha() << __sp << __x.beta();
4886 return __os;
4887}
4888
4889template <class _CharT, class _Traits, class _RT>
4890basic_istream<_CharT, _Traits>&
4891operator>>(basic_istream<_CharT, _Traits>& __is,
4892 gamma_distribution<_RT>& __x)
4893{
4894 typedef gamma_distribution<_RT> _Eng;
4895 typedef typename _Eng::result_type result_type;
4896 typedef typename _Eng::param_type param_type;
4897 __save_flags<_CharT, _Traits> _(__is);
4898 __is.flags(ios_base::dec | ios_base::skipws);
4899 result_type __alpha;
4900 result_type __beta;
4901 __is >> __alpha >> __beta;
4902 if (!__is.fail())
4903 __x.param(param_type(__alpha, __beta));
4904 return __is;
4905}
Howard Hinnanta64111c2010-05-12 21:02:31 +00004906
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00004907// negative_binomial_distribution
4908
4909template<class _IntType = int>
4910class negative_binomial_distribution
4911{
4912public:
4913 // types
4914 typedef _IntType result_type;
4915
4916 class param_type
4917 {
4918 result_type __k_;
4919 double __p_;
4920 public:
4921 typedef negative_binomial_distribution distribution_type;
4922
4923 explicit param_type(result_type __k = 1, double __p = 0.5)
4924 : __k_(__k), __p_(__p) {}
4925
4926 result_type k() const {return __k_;}
4927 double p() const {return __p_;}
4928
4929 friend bool operator==(const param_type& __x, const param_type& __y)
4930 {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
4931 friend bool operator!=(const param_type& __x, const param_type& __y)
4932 {return !(__x == __y);}
4933 };
4934
4935private:
4936 param_type __p_;
4937
4938public:
4939 // constructor and reset functions
4940 explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
4941 : __p_(__k, __p) {}
4942 explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
4943 void reset() {}
4944
4945 // generating functions
4946 template<class _URNG> result_type operator()(_URNG& __g)
4947 {return (*this)(__g, __p_);}
4948 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4949
4950 // property functions
4951 result_type k() const {return __p_.k();}
4952 double p() const {return __p_.p();}
4953
4954 param_type param() const {return __p_;}
4955 void param(const param_type& __p) {__p_ = __p;}
4956
4957 result_type min() const {return 0;}
4958 result_type max() const {return numeric_limits<result_type>::max();}
4959
4960 friend bool operator==(const negative_binomial_distribution& __x,
4961 const negative_binomial_distribution& __y)
4962 {return __x.__p_ == __y.__p_;}
4963 friend bool operator!=(const negative_binomial_distribution& __x,
4964 const negative_binomial_distribution& __y)
4965 {return !(__x == __y);}
4966};
4967
4968template <class _IntType>
4969template<class _URNG>
4970_IntType
4971negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4972{
4973 result_type __k = __pr.k();
4974 double __p = __pr.p();
4975 if (__k <= 21 * __p)
4976 {
4977 bernoulli_distribution __gen(__p);
4978 result_type __f = 0;
4979 result_type __s = 0;
4980 while (__s < __k)
4981 {
4982 if (__gen(__urng))
4983 ++__s;
4984 else
4985 ++__f;
4986 }
4987 return __f;
4988 }
4989 return poisson_distribution<result_type>(gamma_distribution<double>
4990 (__k, (1-__p)/__p)(__urng))(__urng);
4991}
4992
4993template <class _CharT, class _Traits, class _IntType>
4994basic_ostream<_CharT, _Traits>&
4995operator<<(basic_ostream<_CharT, _Traits>& __os,
4996 const negative_binomial_distribution<_IntType>& __x)
4997{
4998 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00004999 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5000 ios_base::scientific);
Howard Hinnantf2fe5d52010-05-17 00:09:38 +00005001 _CharT __sp = __os.widen(' ');
5002 __os.fill(__sp);
5003 return __os << __x.k() << __sp << __x.p();
5004}
5005
5006template <class _CharT, class _Traits, class _IntType>
5007basic_istream<_CharT, _Traits>&
5008operator>>(basic_istream<_CharT, _Traits>& __is,
5009 negative_binomial_distribution<_IntType>& __x)
5010{
5011 typedef negative_binomial_distribution<_IntType> _Eng;
5012 typedef typename _Eng::result_type result_type;
5013 typedef typename _Eng::param_type param_type;
5014 __save_flags<_CharT, _Traits> _(__is);
5015 __is.flags(ios_base::dec | ios_base::skipws);
5016 result_type __k;
5017 double __p;
5018 __is >> __k >> __p;
5019 if (!__is.fail())
5020 __x.param(param_type(__k, __p));
5021 return __is;
5022}
5023
Howard Hinnant34e8a572010-05-17 13:44:27 +00005024// geometric_distribution
5025
5026template<class _IntType = int>
5027class geometric_distribution
5028{
5029public:
5030 // types
5031 typedef _IntType result_type;
5032
5033 class param_type
5034 {
5035 double __p_;
5036 public:
5037 typedef geometric_distribution distribution_type;
5038
5039 explicit param_type(double __p = 0.5) : __p_(__p) {}
5040
5041 double p() const {return __p_;}
5042
5043 friend bool operator==(const param_type& __x, const param_type& __y)
5044 {return __x.__p_ == __y.__p_;}
5045 friend bool operator!=(const param_type& __x, const param_type& __y)
5046 {return !(__x == __y);}
5047 };
5048
5049private:
5050 param_type __p_;
5051
5052public:
5053 // constructors and reset functions
5054 explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
5055 explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
5056 void reset() {}
5057
5058 // generating functions
5059 template<class _URNG> result_type operator()(_URNG& __g)
5060 {return (*this)(__g, __p_);}
5061 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
5062 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5063
5064 // property functions
5065 double p() const {return __p_.p();}
5066
5067 param_type param() const {return __p_;}
5068 void param(const param_type& __p) {__p_ = __p;}
5069
5070 result_type min() const {return 0;}
5071 result_type max() const {return numeric_limits<result_type>::max();}
5072
5073 friend bool operator==(const geometric_distribution& __x,
5074 const geometric_distribution& __y)
5075 {return __x.__p_ == __y.__p_;}
5076 friend bool operator!=(const geometric_distribution& __x,
5077 const geometric_distribution& __y)
5078 {return !(__x == __y);}
5079};
5080
5081template <class _CharT, class _Traits, class _IntType>
5082basic_ostream<_CharT, _Traits>&
5083operator<<(basic_ostream<_CharT, _Traits>& __os,
5084 const geometric_distribution<_IntType>& __x)
5085{
5086 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005087 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5088 ios_base::scientific);
Howard Hinnant34e8a572010-05-17 13:44:27 +00005089 return __os << __x.p();
5090}
5091
5092template <class _CharT, class _Traits, class _IntType>
5093basic_istream<_CharT, _Traits>&
5094operator>>(basic_istream<_CharT, _Traits>& __is,
5095 geometric_distribution<_IntType>& __x)
5096{
5097 typedef geometric_distribution<_IntType> _Eng;
5098 typedef typename _Eng::param_type param_type;
5099 __save_flags<_CharT, _Traits> _(__is);
5100 __is.flags(ios_base::dec | ios_base::skipws);
5101 double __p;
5102 __is >> __p;
5103 if (!__is.fail())
5104 __x.param(param_type(__p));
5105 return __is;
5106}
5107
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005108// chi_squared_distribution
5109
5110template<class _RealType = double>
5111class chi_squared_distribution
5112{
5113public:
5114 // types
5115 typedef _RealType result_type;
5116
5117 class param_type
5118 {
5119 result_type __n_;
5120 public:
5121 typedef chi_squared_distribution distribution_type;
5122
5123 explicit param_type(result_type __n = 1) : __n_(__n) {}
5124
5125 result_type n() const {return __n_;}
5126
5127 friend bool operator==(const param_type& __x, const param_type& __y)
5128 {return __x.__n_ == __y.__n_;}
5129 friend bool operator!=(const param_type& __x, const param_type& __y)
5130 {return !(__x == __y);}
5131 };
5132
5133private:
5134 param_type __p_;
5135
5136public:
5137 // constructor and reset functions
5138 explicit chi_squared_distribution(result_type __n = 1)
5139 : __p_(param_type(__n)) {}
5140 explicit chi_squared_distribution(const param_type& __p)
5141 : __p_(__p) {}
5142 void reset() {}
5143
5144 // generating functions
5145 template<class _URNG> result_type operator()(_URNG& __g)
5146 {return (*this)(__g, __p_);}
5147 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
5148 {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5149
5150 // property functions
5151 result_type n() const {return __p_.n();}
5152
5153 param_type param() const {return __p_;}
5154 void param(const param_type& __p) {__p_ = __p;}
5155
5156 result_type min() const {return 0;}
5157 result_type max() const {return numeric_limits<result_type>::infinity();}
5158
5159
5160 friend bool operator==(const chi_squared_distribution& __x,
5161 const chi_squared_distribution& __y)
5162 {return __x.__p_ == __y.__p_;}
5163 friend bool operator!=(const chi_squared_distribution& __x,
5164 const chi_squared_distribution& __y)
5165 {return !(__x == __y);}
5166};
5167
5168template <class _CharT, class _Traits, class _RT>
5169basic_ostream<_CharT, _Traits>&
5170operator<<(basic_ostream<_CharT, _Traits>& __os,
5171 const chi_squared_distribution<_RT>& __x)
5172{
5173 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005174 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5175 ios_base::scientific);
Howard Hinnant97dc2f32010-05-15 23:36:00 +00005176 __os << __x.n();
5177 return __os;
5178}
5179
5180template <class _CharT, class _Traits, class _RT>
5181basic_istream<_CharT, _Traits>&
5182operator>>(basic_istream<_CharT, _Traits>& __is,
5183 chi_squared_distribution<_RT>& __x)
5184{
5185 typedef chi_squared_distribution<_RT> _Eng;
5186 typedef typename _Eng::result_type result_type;
5187 typedef typename _Eng::param_type param_type;
5188 __save_flags<_CharT, _Traits> _(__is);
5189 __is.flags(ios_base::dec | ios_base::skipws);
5190 result_type __n;
5191 __is >> __n;
5192 if (!__is.fail())
5193 __x.param(param_type(__n));
5194 return __is;
5195}
5196
Howard Hinnantd7d01132010-05-17 21:55:46 +00005197// cauchy_distribution
5198
5199template<class _RealType = double>
5200class cauchy_distribution
5201{
5202public:
5203 // types
5204 typedef _RealType result_type;
5205
5206 class param_type
5207 {
5208 result_type __a_;
5209 result_type __b_;
5210 public:
5211 typedef cauchy_distribution distribution_type;
5212
5213 explicit param_type(result_type __a = 0, result_type __b = 1)
5214 : __a_(__a), __b_(__b) {}
5215
5216 result_type a() const {return __a_;}
5217 result_type b() const {return __b_;}
5218
5219 friend bool operator==(const param_type& __x, const param_type& __y)
5220 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
5221 friend bool operator!=(const param_type& __x, const param_type& __y)
5222 {return !(__x == __y);}
5223 };
5224
5225private:
5226 param_type __p_;
5227
5228public:
5229 // constructor and reset functions
5230 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5231 : __p_(param_type(__a, __b)) {}
5232 explicit cauchy_distribution(const param_type& __p)
5233 : __p_(__p) {}
5234 void reset() {}
5235
5236 // generating functions
5237 template<class _URNG> result_type operator()(_URNG& __g)
5238 {return (*this)(__g, __p_);}
5239 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5240
5241 // property functions
5242 result_type a() const {return __p_.a();}
5243 result_type b() const {return __p_.b();}
5244
5245 param_type param() const {return __p_;}
5246 void param(const param_type& __p) {__p_ = __p;}
5247
5248 result_type min() const {return -numeric_limits<result_type>::infinity();}
5249 result_type max() const {return numeric_limits<result_type>::infinity();}
5250
5251 friend bool operator==(const cauchy_distribution& __x,
5252 const cauchy_distribution& __y)
5253 {return __x.__p_ == __y.__p_;}
5254 friend bool operator!=(const cauchy_distribution& __x,
5255 const cauchy_distribution& __y)
5256 {return !(__x == __y);}
Howard Hinnantd7d01132010-05-17 21:55:46 +00005257};
5258
5259template <class _RealType>
5260template<class _URNG>
5261inline
5262_RealType
5263cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5264{
5265 uniform_real_distribution<result_type> __gen;
5266 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
5267 return __p.a() + __p.b() * _STD::tan(3.1415926535897932384626433832795 * __gen(__g));
5268}
5269
5270template <class _CharT, class _Traits, class _RT>
5271basic_ostream<_CharT, _Traits>&
5272operator<<(basic_ostream<_CharT, _Traits>& __os,
5273 const cauchy_distribution<_RT>& __x)
5274{
5275 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005276 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5277 ios_base::scientific);
Howard Hinnantd7d01132010-05-17 21:55:46 +00005278 _CharT __sp = __os.widen(' ');
5279 __os.fill(__sp);
5280 __os << __x.a() << __sp << __x.b();
5281 return __os;
5282}
5283
5284template <class _CharT, class _Traits, class _RT>
5285basic_istream<_CharT, _Traits>&
5286operator>>(basic_istream<_CharT, _Traits>& __is,
5287 cauchy_distribution<_RT>& __x)
5288{
5289 typedef cauchy_distribution<_RT> _Eng;
5290 typedef typename _Eng::result_type result_type;
5291 typedef typename _Eng::param_type param_type;
5292 __save_flags<_CharT, _Traits> _(__is);
5293 __is.flags(ios_base::dec | ios_base::skipws);
5294 result_type __a;
5295 result_type __b;
5296 __is >> __a >> __b;
5297 if (!__is.fail())
5298 __x.param(param_type(__a, __b));
5299 return __is;
5300}
5301
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005302// fisher_f_distribution
5303
5304template<class _RealType = double>
5305class fisher_f_distribution
5306{
5307public:
5308 // types
5309 typedef _RealType result_type;
5310
5311 class param_type
5312 {
5313 result_type __m_;
5314 result_type __n_;
5315 public:
5316 typedef fisher_f_distribution distribution_type;
5317
5318 explicit param_type(result_type __m = 1, result_type __n = 1)
5319 : __m_(__m), __n_(__n) {}
5320
5321 result_type m() const {return __m_;}
5322 result_type n() const {return __n_;}
5323
5324 friend bool operator==(const param_type& __x, const param_type& __y)
5325 {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
5326 friend bool operator!=(const param_type& __x, const param_type& __y)
5327 {return !(__x == __y);}
5328 };
5329
5330private:
5331 param_type __p_;
5332
5333public:
5334 // constructor and reset functions
5335 explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5336 : __p_(param_type(__m, __n)) {}
5337 explicit fisher_f_distribution(const param_type& __p)
5338 : __p_(__p) {}
5339 void reset() {}
5340
5341 // generating functions
5342 template<class _URNG> result_type operator()(_URNG& __g)
5343 {return (*this)(__g, __p_);}
5344 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5345
5346 // property functions
5347 result_type m() const {return __p_.m();}
5348 result_type n() const {return __p_.n();}
5349
5350 param_type param() const {return __p_;}
5351 void param(const param_type& __p) {__p_ = __p;}
5352
5353 result_type min() const {return 0;}
5354 result_type max() const {return numeric_limits<result_type>::infinity();}
5355
5356 friend bool operator==(const fisher_f_distribution& __x,
5357 const fisher_f_distribution& __y)
5358 {return __x.__p_ == __y.__p_;}
5359 friend bool operator!=(const fisher_f_distribution& __x,
5360 const fisher_f_distribution& __y)
5361 {return !(__x == __y);}
5362};
5363
5364template <class _RealType>
5365template<class _URNG>
5366_RealType
5367fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5368{
5369 gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5370 gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5371 return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5372}
5373
5374template <class _CharT, class _Traits, class _RT>
5375basic_ostream<_CharT, _Traits>&
5376operator<<(basic_ostream<_CharT, _Traits>& __os,
5377 const fisher_f_distribution<_RT>& __x)
5378{
5379 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005380 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5381 ios_base::scientific);
Howard Hinnantd8bc09b2010-05-18 17:32:30 +00005382 _CharT __sp = __os.widen(' ');
5383 __os.fill(__sp);
5384 __os << __x.m() << __sp << __x.n();
5385 return __os;
5386}
5387
5388template <class _CharT, class _Traits, class _RT>
5389basic_istream<_CharT, _Traits>&
5390operator>>(basic_istream<_CharT, _Traits>& __is,
5391 fisher_f_distribution<_RT>& __x)
5392{
5393 typedef fisher_f_distribution<_RT> _Eng;
5394 typedef typename _Eng::result_type result_type;
5395 typedef typename _Eng::param_type param_type;
5396 __save_flags<_CharT, _Traits> _(__is);
5397 __is.flags(ios_base::dec | ios_base::skipws);
5398 result_type __m;
5399 result_type __n;
5400 __is >> __m >> __n;
5401 if (!__is.fail())
5402 __x.param(param_type(__m, __n));
5403 return __is;
5404}
5405
Howard Hinnant551d8e42010-05-19 01:53:57 +00005406// student_t_distribution
5407
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005408template<class _RealType = double>
5409class student_t_distribution
5410{
5411public:
5412 // types
5413 typedef _RealType result_type;
5414
5415 class param_type
5416 {
5417 result_type __n_;
5418 public:
5419 typedef student_t_distribution distribution_type;
5420
5421 explicit param_type(result_type __n = 1) : __n_(__n) {}
5422
5423 result_type n() const {return __n_;}
5424
5425 friend bool operator==(const param_type& __x, const param_type& __y)
5426 {return __x.__n_ == __y.__n_;}
5427 friend bool operator!=(const param_type& __x, const param_type& __y)
5428 {return !(__x == __y);}
5429 };
5430
5431private:
5432 param_type __p_;
5433 normal_distribution<result_type> __nd_;
5434
5435public:
5436 // constructor and reset functions
5437 explicit student_t_distribution(result_type __n = 1)
5438 : __p_(param_type(__n)) {}
5439 explicit student_t_distribution(const param_type& __p)
5440 : __p_(__p) {}
5441 void reset() {__nd_.reset();}
5442
5443 // generating functions
5444 template<class _URNG> result_type operator()(_URNG& __g)
5445 {return (*this)(__g, __p_);}
5446 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5447
5448 // property functions
5449 result_type n() const {return __p_.n();}
5450
5451 param_type param() const {return __p_;}
Howard Hinnant551d8e42010-05-19 01:53:57 +00005452 void param(const param_type& __p) {__p_ = __p;}
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005453
5454 result_type min() const {return -numeric_limits<result_type>::infinity();}
5455 result_type max() const {return numeric_limits<result_type>::infinity();}
5456
5457 friend bool operator==(const student_t_distribution& __x,
5458 const student_t_distribution& __y)
5459 {return __x.__p_ == __y.__p_;}
5460 friend bool operator!=(const student_t_distribution& __x,
5461 const student_t_distribution& __y)
5462 {return !(__x == __y);}
5463};
5464
5465template <class _RealType>
5466template<class _URNG>
5467_RealType
5468student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5469{
5470 gamma_distribution<result_type> __gd(__p.n() * .5, 2);
5471 return __nd_(__g) * _STD::sqrt(__p.n()/__gd(__g));
5472}
5473
5474template <class _CharT, class _Traits, class _RT>
5475basic_ostream<_CharT, _Traits>&
5476operator<<(basic_ostream<_CharT, _Traits>& __os,
5477 const student_t_distribution<_RT>& __x)
5478{
5479 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005480 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5481 ios_base::scientific);
Howard Hinnant321b4bb2010-05-18 20:08:04 +00005482 __os << __x.n();
5483 return __os;
5484}
5485
5486template <class _CharT, class _Traits, class _RT>
5487basic_istream<_CharT, _Traits>&
5488operator>>(basic_istream<_CharT, _Traits>& __is,
5489 student_t_distribution<_RT>& __x)
5490{
5491 typedef student_t_distribution<_RT> _Eng;
5492 typedef typename _Eng::result_type result_type;
5493 typedef typename _Eng::param_type param_type;
5494 __save_flags<_CharT, _Traits> _(__is);
5495 __is.flags(ios_base::dec | ios_base::skipws);
5496 result_type __n;
5497 __is >> __n;
5498 if (!__is.fail())
5499 __x.param(param_type(__n));
5500 return __is;
5501}
5502
Howard Hinnant551d8e42010-05-19 01:53:57 +00005503// discrete_distribution
5504
5505template<class _IntType = int>
5506class discrete_distribution
5507{
5508public:
5509 // types
5510 typedef _IntType result_type;
5511
5512 class param_type
5513 {
5514 vector<double> __p_;
5515 public:
5516 typedef discrete_distribution distribution_type;
5517
5518 param_type() {}
5519 template<class _InputIterator>
5520 param_type(_InputIterator __f, _InputIterator __l)
5521 : __p_(__f, __l) {__init();}
5522 param_type(initializer_list<double> __wl)
5523 : __p_(__wl.begin(), __wl.end()) {__init();}
5524 template<class _UnaryOperation>
5525 param_type(size_t __nw, double __xmin, double __xmax,
5526 _UnaryOperation __fw);
5527
5528 vector<double> probabilities() const;
5529
5530 friend bool operator==(const param_type& __x, const param_type& __y)
5531 {return __x.__p_ == __y.__p_;}
5532 friend bool operator!=(const param_type& __x, const param_type& __y)
5533 {return !(__x == __y);}
5534
5535 private:
5536 void __init();
5537
5538 friend class discrete_distribution;
5539
5540 template <class _CharT, class _Traits, class _IT>
5541 friend
5542 basic_ostream<_CharT, _Traits>&
5543 operator<<(basic_ostream<_CharT, _Traits>& __os,
5544 const discrete_distribution<_IT>& __x);
5545
5546 template <class _CharT, class _Traits, class _IT>
5547 friend
5548 basic_istream<_CharT, _Traits>&
5549 operator>>(basic_istream<_CharT, _Traits>& __is,
5550 discrete_distribution<_IT>& __x);
5551 };
5552
5553private:
5554 param_type __p_;
5555
5556public:
5557 // constructor and reset functions
5558 discrete_distribution() {}
5559 template<class _InputIterator>
5560 discrete_distribution(_InputIterator __f, _InputIterator __l)
5561 : __p_(__f, __l) {}
5562 discrete_distribution(initializer_list<double> __wl)
5563 : __p_(__wl) {}
5564 template<class _UnaryOperation>
5565 discrete_distribution(size_t __nw, double __xmin, double __xmax,
5566 _UnaryOperation __fw)
5567 : __p_(__nw, __xmin, __xmax, __fw) {}
5568 explicit discrete_distribution(const param_type& __p)
5569 : __p_(__p) {}
5570 void reset() {}
5571
5572 // generating functions
5573 template<class _URNG> result_type operator()(_URNG& __g)
5574 {return (*this)(__g, __p_);}
5575 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5576
5577 // property functions
5578 vector<double> probabilities() const {return __p_.probabilities();}
5579
5580 param_type param() const {return __p_;}
5581 void param(const param_type& __p) {__p_ = __p;}
5582
5583 result_type min() const {return 0;}
5584 result_type max() const {return __p_.__p_.size();}
5585
5586 friend bool operator==(const discrete_distribution& __x,
5587 const discrete_distribution& __y)
5588 {return __x.__p_ == __y.__p_;}
5589 friend bool operator!=(const discrete_distribution& __x,
5590 const discrete_distribution& __y)
5591 {return !(__x == __y);}
5592
5593 template <class _CharT, class _Traits, class _IT>
5594 friend
5595 basic_ostream<_CharT, _Traits>&
5596 operator<<(basic_ostream<_CharT, _Traits>& __os,
5597 const discrete_distribution<_IT>& __x);
5598
5599 template <class _CharT, class _Traits, class _IT>
5600 friend
5601 basic_istream<_CharT, _Traits>&
5602 operator>>(basic_istream<_CharT, _Traits>& __is,
5603 discrete_distribution<_IT>& __x);
5604};
5605
5606template<class _IntType>
5607template<class _UnaryOperation>
5608discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5609 double __xmin,
5610 double __xmax,
5611 _UnaryOperation __fw)
5612{
5613 if (__nw > 1)
5614 {
5615 __p_.reserve(__nw - 1);
5616 double __d = (__xmax - __xmin) / __nw;
5617 double __d2 = __d / 2;
5618 for (size_t __k = 0; __k < __nw; ++__k)
5619 __p_.push_back(__fw(__xmin + __k * __d + __d2));
5620 __init();
5621 }
5622}
5623
5624template<class _IntType>
5625void
5626discrete_distribution<_IntType>::param_type::__init()
5627{
5628 if (!__p_.empty())
5629 {
5630 if (__p_.size() > 1)
5631 {
5632 double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0);
5633 for (_STD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
5634 __i < __e; ++__i)
5635 *__i /= __s;
5636 vector<double> __t(__p_.size() - 1);
5637 _STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
5638 swap(__p_, __t);
5639 }
5640 else
5641 {
5642 __p_.clear();
5643 __p_.shrink_to_fit();
5644 }
5645 }
5646}
5647
5648template<class _IntType>
5649vector<double>
5650discrete_distribution<_IntType>::param_type::probabilities() const
5651{
5652 size_t __n = __p_.size();
5653 _STD::vector<double> __p(__n+1);
5654 _STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
5655 if (__n > 0)
5656 __p[__n] = 1 - __p_[__n-1];
5657 else
5658 __p[0] = 1;
5659 return __p;
5660}
5661
5662template<class _IntType>
5663template<class _URNG>
5664_IntType
5665discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5666{
5667 uniform_real_distribution<double> __gen;
5668 return static_cast<_IntType>(
5669 _STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
5670 __p.__p_.begin());
5671}
5672
5673template <class _CharT, class _Traits, class _IT>
5674basic_ostream<_CharT, _Traits>&
5675operator<<(basic_ostream<_CharT, _Traits>& __os,
5676 const discrete_distribution<_IT>& __x)
5677{
5678 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005679 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5680 ios_base::scientific);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005681 _CharT __sp = __os.widen(' ');
5682 __os.fill(__sp);
5683 size_t __n = __x.__p_.__p_.size();
5684 __os << __n;
5685 for (size_t __i = 0; __i < __n; ++__i)
5686 __os << __sp << __x.__p_.__p_[__i];
5687 return __os;
5688}
5689
5690template <class _CharT, class _Traits, class _IT>
5691basic_istream<_CharT, _Traits>&
5692operator>>(basic_istream<_CharT, _Traits>& __is,
5693 discrete_distribution<_IT>& __x)
5694{
5695 typedef discrete_distribution<_IT> _Eng;
5696 typedef typename _Eng::result_type result_type;
5697 typedef typename _Eng::param_type param_type;
5698 __save_flags<_CharT, _Traits> _(__is);
5699 __is.flags(ios_base::dec | ios_base::skipws);
5700 size_t __n;
5701 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005702 vector<double> __p(__n);
Howard Hinnant551d8e42010-05-19 01:53:57 +00005703 for (size_t __i = 0; __i < __n; ++__i)
5704 __is >> __p[__i];
5705 if (!__is.fail())
5706 swap(__x.__p_.__p_, __p);
5707 return __is;
5708}
5709
Howard Hinnantd6d11712010-05-20 15:11:46 +00005710// piecewise_constant_distribution
5711
5712template<class _RealType = double>
5713class piecewise_constant_distribution
5714{
5715public:
5716 // types
5717 typedef _RealType result_type;
5718
5719 class param_type
5720 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005721 typedef typename common_type<double, result_type>::type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005722 vector<result_type> __b_;
Howard Hinnant2a592542010-05-24 00:35:40 +00005723 vector<double> __densities_;
5724 vector<__area_type> __areas_;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005725 public:
5726 typedef piecewise_constant_distribution distribution_type;
5727
5728 param_type();
5729 template<class _InputIteratorB, class _InputIteratorW>
5730 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5731 _InputIteratorW __fW);
5732 template<class _UnaryOperation>
5733 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5734 template<class _UnaryOperation>
5735 param_type(size_t __nw, result_type __xmin, result_type __xmax,
5736 _UnaryOperation __fw);
5737
5738 vector<result_type> intervals() const {return __b_;}
Howard Hinnant2a592542010-05-24 00:35:40 +00005739 vector<double> densities() const {return __densities_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005740
5741 friend bool operator==(const param_type& __x, const param_type& __y)
Howard Hinnant2a592542010-05-24 00:35:40 +00005742 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
Howard Hinnantd6d11712010-05-20 15:11:46 +00005743 friend bool operator!=(const param_type& __x, const param_type& __y)
5744 {return !(__x == __y);}
5745
5746 private:
5747 void __init();
5748
5749 friend class piecewise_constant_distribution;
5750
5751 template <class _CharT, class _Traits, class _RT>
5752 friend
5753 basic_ostream<_CharT, _Traits>&
5754 operator<<(basic_ostream<_CharT, _Traits>& __os,
5755 const piecewise_constant_distribution<_RT>& __x);
5756
5757 template <class _CharT, class _Traits, class _RT>
5758 friend
5759 basic_istream<_CharT, _Traits>&
5760 operator>>(basic_istream<_CharT, _Traits>& __is,
5761 piecewise_constant_distribution<_RT>& __x);
5762 };
5763
5764private:
5765 param_type __p_;
5766
5767public:
5768 // constructor and reset functions
5769 piecewise_constant_distribution() {}
5770 template<class _InputIteratorB, class _InputIteratorW>
5771 piecewise_constant_distribution(_InputIteratorB __fB,
5772 _InputIteratorB __lB,
5773 _InputIteratorW __fW)
5774 : __p_(__fB, __lB, __fW) {}
5775
5776 template<class _UnaryOperation>
5777 piecewise_constant_distribution(initializer_list<result_type> __bl,
5778 _UnaryOperation __fw)
5779 : __p_(__bl, __fw) {}
5780
5781 template<class _UnaryOperation>
5782 piecewise_constant_distribution(size_t __nw, result_type __xmin,
5783 result_type __xmax, _UnaryOperation __fw)
5784 : __p_(__nw, __xmin, __xmax, __fw) {}
5785
5786 explicit piecewise_constant_distribution(const param_type& __p)
5787 : __p_(__p) {}
5788
5789 void reset() {}
5790
5791 // generating functions
5792 template<class _URNG> result_type operator()(_URNG& __g)
5793 {return (*this)(__g, __p_);}
5794 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5795
5796 // property functions
5797 vector<result_type> intervals() const {return __p_.intervals();}
5798 vector<double> densities() const {return __p_.densities();}
5799
5800 param_type param() const {return __p_;}
5801 void param(const param_type& __p) {__p_ = __p;}
5802
5803 result_type min() const {return __p_.__b_.front();}
5804 result_type max() const {return __p_.__b_.back();}
5805
5806 friend bool operator==(const piecewise_constant_distribution& __x,
5807 const piecewise_constant_distribution& __y)
5808 {return __x.__p_ == __y.__p_;}
5809 friend bool operator!=(const piecewise_constant_distribution& __x,
5810 const piecewise_constant_distribution& __y)
5811 {return !(__x == __y);}
5812
5813 template <class _CharT, class _Traits, class _RT>
5814 friend
5815 basic_ostream<_CharT, _Traits>&
5816 operator<<(basic_ostream<_CharT, _Traits>& __os,
5817 const piecewise_constant_distribution<_RT>& __x);
5818
5819 template <class _CharT, class _Traits, class _RT>
5820 friend
5821 basic_istream<_CharT, _Traits>&
5822 operator>>(basic_istream<_CharT, _Traits>& __is,
5823 piecewise_constant_distribution<_RT>& __x);
5824};
5825
5826template<class _RealType>
5827void
5828piecewise_constant_distribution<_RealType>::param_type::__init()
5829{
Howard Hinnant2a592542010-05-24 00:35:40 +00005830 // __densities_ contains non-normalized areas
5831 __area_type __total_area = _STD::accumulate(__densities_.begin(),
5832 __densities_.end(),
5833 __area_type());
5834 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5835 __densities_[__i] /= __total_area;
5836 // __densities_ contains normalized areas
5837 __areas_.assign(__densities_.size(), __area_type());
5838 _STD::partial_sum(__densities_.begin(), __densities_.end() - 1,
5839 __areas_.begin() + 1);
5840 // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
5841 __densities_.back() = 1 - __areas_.back(); // correct round off error
5842 for (size_t __i = 0; __i < __densities_.size(); ++__i)
5843 __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
5844 // __densities_ now contains __densities_
Howard Hinnantd6d11712010-05-20 15:11:46 +00005845}
5846
5847template<class _RealType>
5848piecewise_constant_distribution<_RealType>::param_type::param_type()
Howard Hinnant2a592542010-05-24 00:35:40 +00005849 : __b_(2),
Howard Hinnant54305402010-05-25 00:27:34 +00005850 __densities_(1, 1.0),
5851 __areas_(1, 0.0)
Howard Hinnantd6d11712010-05-20 15:11:46 +00005852{
5853 __b_[1] = 1;
5854}
5855
5856template<class _RealType>
5857template<class _InputIteratorB, class _InputIteratorW>
5858piecewise_constant_distribution<_RealType>::param_type::param_type(
5859 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
5860 : __b_(__fB, __lB)
5861{
5862 if (__b_.size() < 2)
5863 {
5864 __b_.resize(2);
5865 __b_[0] = 0;
5866 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005867 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00005868 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005869 }
5870 else
5871 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005872 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005873 for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
Howard Hinnant2a592542010-05-24 00:35:40 +00005874 __densities_.push_back(*__fW);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005875 __init();
5876 }
5877}
5878
5879template<class _RealType>
5880template<class _UnaryOperation>
5881piecewise_constant_distribution<_RealType>::param_type::param_type(
5882 initializer_list<result_type> __bl, _UnaryOperation __fw)
5883 : __b_(__bl.begin(), __bl.end())
5884{
5885 if (__b_.size() < 2)
5886 {
5887 __b_.resize(2);
5888 __b_[0] = 0;
5889 __b_[1] = 1;
Howard Hinnant2a592542010-05-24 00:35:40 +00005890 __densities_.assign(1, 1.0);
Howard Hinnant54305402010-05-25 00:27:34 +00005891 __areas_.assign(1, 0.0);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005892 }
5893 else
5894 {
Howard Hinnant2a592542010-05-24 00:35:40 +00005895 __densities_.reserve(__b_.size() - 1);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005896 for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005897 __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005898 __init();
5899 }
5900}
5901
5902template<class _RealType>
5903template<class _UnaryOperation>
5904piecewise_constant_distribution<_RealType>::param_type::param_type(
5905 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
5906 : __b_(__nw == 0 ? 2 : __nw + 1)
5907{
5908 size_t __n = __b_.size() - 1;
5909 result_type __d = (__xmax - __xmin) / __n;
Howard Hinnant2a592542010-05-24 00:35:40 +00005910 __densities_.reserve(__n);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005911 for (size_t __i = 0; __i < __n; ++__i)
5912 {
5913 __b_[__i] = __xmin + __i * __d;
Howard Hinnant2a592542010-05-24 00:35:40 +00005914 __densities_.push_back(__fw(__b_[__i] + __d*.5));
Howard Hinnantd6d11712010-05-20 15:11:46 +00005915 }
5916 __b_[__n] = __xmax;
5917 __init();
5918}
5919
5920template<class _RealType>
Howard Hinnantd6d11712010-05-20 15:11:46 +00005921template<class _URNG>
5922_RealType
5923piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5924{
5925 typedef uniform_real_distribution<result_type> _Gen;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005926 result_type __u = _Gen()(__g);
Howard Hinnant2a592542010-05-24 00:35:40 +00005927 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
5928 static_cast<double>(__u)) - __p.__areas_.begin() - 1;
5929 return static_cast<result_type>((__u - __p.__areas_[__k]) / __p.__densities_[__k]
5930 + __p.__b_[__k]);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005931}
5932
5933template <class _CharT, class _Traits, class _RT>
5934basic_ostream<_CharT, _Traits>&
5935operator<<(basic_ostream<_CharT, _Traits>& __os,
5936 const piecewise_constant_distribution<_RT>& __x)
5937{
5938 __save_flags<_CharT, _Traits> _(__os);
Howard Hinnant2a592542010-05-24 00:35:40 +00005939 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5940 ios_base::scientific);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005941 _CharT __sp = __os.widen(' ');
5942 __os.fill(__sp);
Howard Hinnant2a592542010-05-24 00:35:40 +00005943 size_t __n = __x.__p_.__b_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005944 __os << __n;
5945 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005946 __os << __sp << __x.__p_.__b_[__i];
5947 __n = __x.__p_.__densities_.size();
Howard Hinnantd6d11712010-05-20 15:11:46 +00005948 __os << __sp << __n;
5949 for (size_t __i = 0; __i < __n; ++__i)
Howard Hinnant2a592542010-05-24 00:35:40 +00005950 __os << __sp << __x.__p_.__densities_[__i];
5951 __n = __x.__p_.__areas_.size();
5952 __os << __sp << __n;
5953 for (size_t __i = 0; __i < __n; ++__i)
5954 __os << __sp << __x.__p_.__areas_[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005955 return __os;
5956}
5957
5958template <class _CharT, class _Traits, class _RT>
5959basic_istream<_CharT, _Traits>&
5960operator>>(basic_istream<_CharT, _Traits>& __is,
5961 piecewise_constant_distribution<_RT>& __x)
5962{
5963 typedef piecewise_constant_distribution<_RT> _Eng;
5964 typedef typename _Eng::result_type result_type;
5965 typedef typename _Eng::param_type param_type;
Howard Hinnant2a592542010-05-24 00:35:40 +00005966 typedef typename param_type::__area_type __area_type;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005967 __save_flags<_CharT, _Traits> _(__is);
5968 __is.flags(ios_base::dec | ios_base::skipws);
5969 size_t __n;
5970 __is >> __n;
Howard Hinnantd6d11712010-05-20 15:11:46 +00005971 vector<result_type> __b(__n);
5972 for (size_t __i = 0; __i < __n; ++__i)
5973 __is >> __b[__i];
Howard Hinnant2a592542010-05-24 00:35:40 +00005974 __is >> __n;
5975 vector<double> __densities(__n);
5976 for (size_t __i = 0; __i < __n; ++__i)
5977 __is >> __densities[__i];
5978 __is >> __n;
5979 vector<__area_type> __areas(__n);
5980 for (size_t __i = 0; __i < __n; ++__i)
5981 __is >> __areas[__i];
Howard Hinnantd6d11712010-05-20 15:11:46 +00005982 if (!__is.fail())
5983 {
Howard Hinnantd6d11712010-05-20 15:11:46 +00005984 swap(__x.__p_.__b_, __b);
Howard Hinnant2a592542010-05-24 00:35:40 +00005985 swap(__x.__p_.__densities_, __densities);
5986 swap(__x.__p_.__areas_, __areas);
Howard Hinnantd6d11712010-05-20 15:11:46 +00005987 }
5988 return __is;
5989}
5990
Howard Hinnant54305402010-05-25 00:27:34 +00005991// piecewise_linear_distribution
5992
5993template<class _RealType = double>
5994class piecewise_linear_distribution
5995{
5996public:
5997 // types
5998 typedef _RealType result_type;
5999
6000 class param_type
6001 {
6002 typedef typename common_type<double, result_type>::type __area_type;
6003 vector<result_type> __b_;
6004 vector<double> __densities_;
6005 vector<__area_type> __areas_;
6006 public:
6007 typedef piecewise_linear_distribution distribution_type;
6008
6009 param_type();
6010 template<class _InputIteratorB, class _InputIteratorW>
6011 param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6012 _InputIteratorW __fW);
6013 template<class _UnaryOperation>
6014 param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
6015 template<class _UnaryOperation>
6016 param_type(size_t __nw, result_type __xmin, result_type __xmax,
6017 _UnaryOperation __fw);
6018
6019 vector<result_type> intervals() const {return __b_;}
6020 vector<double> densities() const {return __densities_;}
6021
6022 friend bool operator==(const param_type& __x, const param_type& __y)
6023 {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
6024 friend bool operator!=(const param_type& __x, const param_type& __y)
6025 {return !(__x == __y);}
6026
6027 private:
6028 void __init();
6029
6030 friend class piecewise_linear_distribution;
6031
6032 template <class _CharT, class _Traits, class _RT>
6033 friend
6034 basic_ostream<_CharT, _Traits>&
6035 operator<<(basic_ostream<_CharT, _Traits>& __os,
6036 const piecewise_linear_distribution<_RT>& __x);
6037
6038 template <class _CharT, class _Traits, class _RT>
6039 friend
6040 basic_istream<_CharT, _Traits>&
6041 operator>>(basic_istream<_CharT, _Traits>& __is,
6042 piecewise_linear_distribution<_RT>& __x);
6043 };
6044
6045private:
6046 param_type __p_;
6047
6048public:
6049 // constructor and reset functions
6050 piecewise_linear_distribution() {}
6051 template<class _InputIteratorB, class _InputIteratorW>
6052 piecewise_linear_distribution(_InputIteratorB __fB,
6053 _InputIteratorB __lB,
6054 _InputIteratorW __fW)
6055 : __p_(__fB, __lB, __fW) {}
6056
6057 template<class _UnaryOperation>
6058 piecewise_linear_distribution(initializer_list<result_type> __bl,
6059 _UnaryOperation __fw)
6060 : __p_(__bl, __fw) {}
6061
6062 template<class _UnaryOperation>
6063 piecewise_linear_distribution(size_t __nw, result_type __xmin,
6064 result_type __xmax, _UnaryOperation __fw)
6065 : __p_(__nw, __xmin, __xmax, __fw) {}
6066
6067 explicit piecewise_linear_distribution(const param_type& __p)
6068 : __p_(__p) {}
6069
6070 void reset() {}
6071
6072 // generating functions
6073 template<class _URNG> result_type operator()(_URNG& __g)
6074 {return (*this)(__g, __p_);}
6075 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6076
6077 // property functions
6078 vector<result_type> intervals() const {return __p_.intervals();}
6079 vector<double> densities() const {return __p_.densities();}
6080
6081 param_type param() const {return __p_;}
6082 void param(const param_type& __p) {__p_ = __p;}
6083
6084 result_type min() const {return __p_.__b_.front();}
6085 result_type max() const {return __p_.__b_.back();}
6086
6087 friend bool operator==(const piecewise_linear_distribution& __x,
6088 const piecewise_linear_distribution& __y)
6089 {return __x.__p_ == __y.__p_;}
6090 friend bool operator!=(const piecewise_linear_distribution& __x,
6091 const piecewise_linear_distribution& __y)
6092 {return !(__x == __y);}
6093
6094 template <class _CharT, class _Traits, class _RT>
6095 friend
6096 basic_ostream<_CharT, _Traits>&
6097 operator<<(basic_ostream<_CharT, _Traits>& __os,
6098 const piecewise_linear_distribution<_RT>& __x);
6099
6100 template <class _CharT, class _Traits, class _RT>
6101 friend
6102 basic_istream<_CharT, _Traits>&
6103 operator>>(basic_istream<_CharT, _Traits>& __is,
6104 piecewise_linear_distribution<_RT>& __x);
6105};
6106
6107template<class _RealType>
6108void
6109piecewise_linear_distribution<_RealType>::param_type::__init()
6110{
6111 __areas_.assign(__densities_.size() - 1, __area_type());
6112 __area_type _S = 0;
6113 for (size_t __i = 0; __i < __areas_.size(); ++__i)
6114 {
6115 __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6116 (__b_[__i+1] - __b_[__i]) * .5;
6117 _S += __areas_[__i];
6118 }
6119 for (size_t __i = __areas_.size(); __i > 1;)
6120 {
6121 --__i;
6122 __areas_[__i] = __areas_[__i-1] / _S;
6123 }
6124 __areas_[0] = 0;
6125 for (size_t __i = 1; __i < __areas_.size(); ++__i)
6126 __areas_[__i] += __areas_[__i-1];
6127 for (size_t __i = 0; __i < __densities_.size(); ++__i)
6128 __densities_[__i] /= _S;
6129}
6130
6131template<class _RealType>
6132piecewise_linear_distribution<_RealType>::param_type::param_type()
6133 : __b_(2),
6134 __densities_(2, 1.0),
6135 __areas_(1, 0.0)
6136{
6137 __b_[1] = 1;
6138}
6139
6140template<class _RealType>
6141template<class _InputIteratorB, class _InputIteratorW>
6142piecewise_linear_distribution<_RealType>::param_type::param_type(
6143 _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6144 : __b_(__fB, __lB)
6145{
6146 if (__b_.size() < 2)
6147 {
6148 __b_.resize(2);
6149 __b_[0] = 0;
6150 __b_[1] = 1;
6151 __densities_.assign(2, 1.0);
6152 __areas_.assign(1, 0.0);
6153 }
6154 else
6155 {
6156 __densities_.reserve(__b_.size());
6157 for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6158 __densities_.push_back(*__fW);
6159 __init();
6160 }
6161}
6162
6163template<class _RealType>
6164template<class _UnaryOperation>
6165piecewise_linear_distribution<_RealType>::param_type::param_type(
6166 initializer_list<result_type> __bl, _UnaryOperation __fw)
6167 : __b_(__bl.begin(), __bl.end())
6168{
6169 if (__b_.size() < 2)
6170 {
6171 __b_.resize(2);
6172 __b_[0] = 0;
6173 __b_[1] = 1;
6174 __densities_.assign(2, 1.0);
6175 __areas_.assign(1, 0.0);
6176 }
6177 else
6178 {
6179 __densities_.reserve(__b_.size());
6180 for (size_t __i = 0; __i < __b_.size(); ++__i)
6181 __densities_.push_back(__fw(__b_[__i]));
6182 __init();
6183 }
6184}
6185
6186template<class _RealType>
6187template<class _UnaryOperation>
6188piecewise_linear_distribution<_RealType>::param_type::param_type(
6189 size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6190 : __b_(__nw == 0 ? 2 : __nw + 1)
6191{
6192 size_t __n = __b_.size() - 1;
6193 result_type __d = (__xmax - __xmin) / __n;
6194 __densities_.reserve(__b_.size());
6195 for (size_t __i = 0; __i < __n; ++__i)
6196 {
6197 __b_[__i] = __xmin + __i * __d;
6198 __densities_.push_back(__fw(__b_[__i]));
6199 }
6200 __b_[__n] = __xmax;
6201 __densities_.push_back(__fw(__b_[__n]));
6202 __init();
6203}
6204
6205template<class _RealType>
6206template<class _URNG>
6207_RealType
6208piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6209{
6210 typedef uniform_real_distribution<result_type> _Gen;
6211 result_type __u = _Gen()(__g);
6212 ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
6213 static_cast<double>(__u)) - __p.__areas_.begin() - 1;
6214 __u -= __p.__areas_[__k];
6215 const double __dk = __p.__densities_[__k];
6216 const double __dk1 = __p.__densities_[__k+1];
6217 const double __deltad = __dk1 - __dk;
6218 const result_type __bk = __p.__b_[__k];
6219 if (__deltad == 0)
6220 return static_cast<result_type>(__u / __dk + __bk);
6221 const result_type __bk1 = __p.__b_[__k+1];
6222 const result_type __deltab = __bk1 - __bk;
6223 return static_cast<result_type>((__bk * __dk1 - __bk1 * __dk +
6224 _STD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
6225 __deltad);
6226}
6227
6228template <class _CharT, class _Traits, class _RT>
6229basic_ostream<_CharT, _Traits>&
6230operator<<(basic_ostream<_CharT, _Traits>& __os,
6231 const piecewise_linear_distribution<_RT>& __x)
6232{
6233 __save_flags<_CharT, _Traits> _(__os);
6234 __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6235 ios_base::scientific);
6236 _CharT __sp = __os.widen(' ');
6237 __os.fill(__sp);
6238 size_t __n = __x.__p_.__b_.size();
6239 __os << __n;
6240 for (size_t __i = 0; __i < __n; ++__i)
6241 __os << __sp << __x.__p_.__b_[__i];
6242 __n = __x.__p_.__densities_.size();
6243 __os << __sp << __n;
6244 for (size_t __i = 0; __i < __n; ++__i)
6245 __os << __sp << __x.__p_.__densities_[__i];
6246 __n = __x.__p_.__areas_.size();
6247 __os << __sp << __n;
6248 for (size_t __i = 0; __i < __n; ++__i)
6249 __os << __sp << __x.__p_.__areas_[__i];
6250 return __os;
6251}
6252
6253template <class _CharT, class _Traits, class _RT>
6254basic_istream<_CharT, _Traits>&
6255operator>>(basic_istream<_CharT, _Traits>& __is,
6256 piecewise_linear_distribution<_RT>& __x)
6257{
6258 typedef piecewise_linear_distribution<_RT> _Eng;
6259 typedef typename _Eng::result_type result_type;
6260 typedef typename _Eng::param_type param_type;
6261 typedef typename param_type::__area_type __area_type;
6262 __save_flags<_CharT, _Traits> _(__is);
6263 __is.flags(ios_base::dec | ios_base::skipws);
6264 size_t __n;
6265 __is >> __n;
6266 vector<result_type> __b(__n);
6267 for (size_t __i = 0; __i < __n; ++__i)
6268 __is >> __b[__i];
6269 __is >> __n;
6270 vector<double> __densities(__n);
6271 for (size_t __i = 0; __i < __n; ++__i)
6272 __is >> __densities[__i];
6273 __is >> __n;
6274 vector<__area_type> __areas(__n);
6275 for (size_t __i = 0; __i < __n; ++__i)
6276 __is >> __areas[__i];
6277 if (!__is.fail())
6278 {
6279 swap(__x.__p_.__b_, __b);
6280 swap(__x.__p_.__densities_, __densities);
6281 swap(__x.__p_.__areas_, __areas);
6282 }
6283 return __is;
6284}
6285
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00006286_LIBCPP_END_NAMESPACE_STD
6287
6288#endif // _LIBCPP_RANDOM