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