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