blob: a94ca2cc2113db0b486325766c047ef0f16d18aa [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_RANDOM
12#define _LIBCPP_RANDOM
13
14/*
15 random synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22// Engines
23
24template <class UIntType, UIntType a, UIntType c, UIntType m>
25class linear_congruential_engine
26{
27public:
28 // types
29 typedef UIntType result_type;
30
31 // engine characteristics
32 static constexpr result_type multiplier = a;
33 static constexpr result_type increment = c;
34 static constexpr result_type modulus = m;
35 static constexpr result_type min() { return c == 0u ? 1u: 0u;}
36 static constexpr result_type max() { return m - 1u;}
37 static constexpr result_type default_seed = 1u;
38
39 // constructors and seeding functions
40 explicit linear_congruential_engine(result_type s = default_seed);
41 template<class Sseq> explicit linear_congruential_engine(Sseq& q);
42 void seed(result_type s = default_seed);
43 template<class Sseq> void seed(Sseq& q);
44
45 // generating functions
46 result_type operator()();
47 void discard(unsigned long long z);
48};
49
50template <class UIntType, UIntType a, UIntType c, UIntType m>
51bool
52operator==(const linear_congruential_engine<UIntType, a, c, m>& x,
53 const linear_congruential_engine<UIntType, a, c, m>& y);
54
55template <class UIntType, UIntType a, UIntType c, UIntType m>
56bool
57operator!=(const linear_congruential_engine<UIntType, a, c, m>& x,
58 const linear_congruential_engine<UIntType, a, c, m>& y);
59
60template <class charT, class traits,
61 class UIntType, UIntType a, UIntType c, UIntType m>
62basic_ostream<charT, traits>&
63operator<<(basic_ostream<charT, traits>& os,
64 const linear_congruential_engine<UIntType, a, c, m>& x);
65
66template <class charT, class traits,
67 class UIntType, UIntType a, UIntType c, UIntType m>
68basic_istream<charT, traits>&
69operator>>(basic_istream<charT, traits>& is,
70 linear_congruential_engine<UIntType, a, c, m>& x);
71
72template <class UIntType, size_t w, size_t n, size_t m, size_t r,
73 UIntType a, size_t u, UIntType d, size_t s,
74 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
75class mersenne_twister_engine
76{
77public:
78 // types
79 typedef UIntType result_type;
80
81 // engine characteristics
82 static constexpr size_t word_size = w;
83 static constexpr size_t state_size = n;
84 static constexpr size_t shift_size = m;
85 static constexpr size_t mask_bits = r;
86 static constexpr result_type xor_mask = a;
87 static constexpr size_t tempering_u = u;
88 static constexpr result_type tempering_d = d;
89 static constexpr size_t tempering_s = s;
90 static constexpr result_type tempering_b = b;
91 static constexpr size_t tempering_t = t;
92 static constexpr result_type tempering_c = c;
93 static constexpr size_t tempering_l = l;
94 static constexpr result_type initialization_multiplier = f;
95 static constexpr result_type min () { return 0; }
96 static constexpr result_type max() { return 2^w - 1; }
97 static constexpr result_type default_seed = 5489u;
98
99 // constructors and seeding functions
100 explicit mersenne_twister_engine(result_type value = default_seed);
101 template<class Sseq> explicit mersenne_twister_engine(Sseq& q);
102 void seed(result_type value = default_seed);
103 template<class Sseq> void seed(Sseq& q);
104
105 // generating functions
106 result_type operator()();
107 void discard(unsigned long long z);
108};
109
110template <class UIntType, size_t w, size_t n, size_t m, size_t r,
111 UIntType a, size_t u, UIntType d, size_t s,
112 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
113bool
114operator==(
115 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
116 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
117
118template <class UIntType, size_t w, size_t n, size_t m, size_t r,
119 UIntType a, size_t u, UIntType d, size_t s,
120 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
121bool
122operator!=(
123 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x,
124 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& y);
125
126template <class charT, class traits,
127 class UIntType, size_t w, size_t n, size_t m, size_t r,
128 UIntType a, size_t u, UIntType d, size_t s,
129 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
130basic_ostream<charT, traits>&
131operator<<(basic_ostream<charT, traits>& os,
132 const mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
133
134template <class charT, class traits,
135 class UIntType, size_t w, size_t n, size_t m, size_t r,
136 UIntType a, size_t u, UIntType d, size_t s,
137 UIntType b, size_t t, UIntType c, size_t l, UIntType f>
138basic_istream<charT, traits>&
139operator>>(basic_istream<charT, traits>& is,
140 mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>& x);
141
142template<class UIntType, size_t w, size_t s, size_t r>
143class subtract_with_carry_engine
144{
145public:
146 // types
147 typedef UIntType result_type;
148
149 // engine characteristics
150 static constexpr size_t word_size = w;
151 static constexpr size_t short_lag = s;
152 static constexpr size_t long_lag = r;
153 static constexpr result_type min() { return 0; }
154 static constexpr result_type max() { return m-1; }
155 static constexpr result_type default_seed = 19780503u;
156
157 // constructors and seeding functions
158 explicit subtract_with_carry_engine(result_type value = default_seed);
159 template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
160 void seed(result_type value = default_seed);
161 template<class Sseq> void seed(Sseq& q);
162
163 // generating functions
164 result_type operator()();
165 void discard(unsigned long long z);
166};
167
168template<class UIntType, size_t w, size_t s, size_t r>
169bool
170operator==(
171 const subtract_with_carry_engine<UIntType, w, s, r>& x,
172 const subtract_with_carry_engine<UIntType, w, s, r>& y);
173
174template<class UIntType, size_t w, size_t s, size_t r>
175bool
176operator!=(
177 const subtract_with_carry_engine<UIntType, w, s, r>& x,
178 const subtract_with_carry_engine<UIntType, w, s, r>& y);
179
180template <class charT, class traits,
181 class UIntType, size_t w, size_t s, size_t r>
182basic_ostream<charT, traits>&
183operator<<(basic_ostream<charT, traits>& os,
184 const subtract_with_carry_engine<UIntType, w, s, r>& x);
185
186template <class charT, class traits,
187 class UIntType, size_t w, size_t s, size_t r>
188basic_istream<charT, traits>&
189operator>>(basic_istream<charT, traits>& is,
190 subtract_with_carry_engine<UIntType, w, s, r>& x);
191
192template<class Engine, size_t p, size_t r>
193class discard_block_engine
194{
195public:
196 // types
197 typedef typename Engine::result_type result_type;
198
199 // engine characteristics
200 static constexpr size_t block_size = p;
201 static constexpr size_t used_block = r;
202 static constexpr result_type min() { return Engine::min(); }
203 static constexpr result_type max() { return Engine::max(); }
204
205 // constructors and seeding functions
206 discard_block_engine();
207 explicit discard_block_engine(const Engine& e);
208 explicit discard_block_engine(Engine&& e);
209 explicit discard_block_engine(result_type s);
210 template<class Sseq> explicit discard_block_engine(Sseq& q);
211 void seed();
212 void seed(result_type s);
213 template<class Sseq> void seed(Sseq& q);
214
215 // generating functions
216 result_type operator()();
217 void discard(unsigned long long z);
218
219 // property functions
220 const Engine& base() const;
221};
222
223template<class Engine, size_t p, size_t r>
224bool
225operator==(
226 const discard_block_engine<Engine, p, r>& x,
227 const discard_block_engine<Engine, p, r>& y);
228
229template<class Engine, size_t p, size_t r>
230bool
231operator!=(
232 const discard_block_engine<Engine, p, r>& x,
233 const discard_block_engine<Engine, p, r>& y);
234
235template <class charT, class traits,
236 class Engine, size_t p, size_t r>
237basic_ostream<charT, traits>&
238operator<<(basic_ostream<charT, traits>& os,
239 const discard_block_engine<Engine, p, r>& x);
240
241template <class charT, class traits,
242 class Engine, size_t p, size_t r>
243basic_istream<charT, traits>&
244operator>>(basic_istream<charT, traits>& is,
245 discard_block_engine<Engine, p, r>& x);
246
247template<class Engine, size_t w, class UIntType>
248class independent_bits_engine
249{
250public:
251 // types
252 typedef UIntType result_type;
253
254 // engine characteristics
255 static constexpr result_type min() { return 0; }
256 static constexpr result_type max() { return 2^w - 1; }
257
258 // constructors and seeding functions
259 independent_bits_engine();
260 explicit independent_bits_engine(const Engine& e);
261 explicit independent_bits_engine(Engine&& e);
262 explicit independent_bits_engine(result_type s);
263 template<class Sseq> explicit independent_bits_engine(Sseq& q);
264 void seed();
265 void seed(result_type s);
266 template<class Sseq> void seed(Sseq& q);
267
268 // generating functions
269 result_type operator()(); void discard(unsigned long long z);
270
271 // property functions
272 const Engine& base() const;
273};
274
275template<class Engine, size_t w, class UIntType>
276bool
277operator==(
278 const independent_bits_engine<Engine, w, UIntType>& x,
279 const independent_bits_engine<Engine, w, UIntType>& y);
280
281template<class Engine, size_t w, class UIntType>
282bool
283operator!=(
284 const independent_bits_engine<Engine, w, UIntType>& x,
285 const independent_bits_engine<Engine, w, UIntType>& y);
286
287template <class charT, class traits,
288 class Engine, size_t w, class UIntType>
289basic_ostream<charT, traits>&
290operator<<(basic_ostream<charT, traits>& os,
291 const independent_bits_engine<Engine, w, UIntType>& x);
292
293template <class charT, class traits,
294 class Engine, size_t w, class UIntType>
295basic_istream<charT, traits>&
296operator>>(basic_istream<charT, traits>& is,
297 independent_bits_engine<Engine, w, UIntType>& x);
298
299template<class Engine, size_t k>
300class shuffle_order_engine
301{
302public:
303 // types
304 typedef typename Engine::result_type result_type;
305
306 // engine characteristics
307 static constexpr size_t table_size = k;
308 static constexpr result_type min() { return Engine::min; }
309 static constexpr result_type max() { return Engine::max; }
310
311 // constructors and seeding functions
312 shuffle_order_engine();
313 explicit shuffle_order_engine(const Engine& e);
314 explicit shuffle_order_engine(Engine&& e);
315 explicit shuffle_order_engine(result_type s);
316 template<class Sseq> explicit shuffle_order_engine(Sseq& q);
317 void seed();
318 void seed(result_type s);
319 template<class Sseq> void seed(Sseq& q);
320
321 // generating functions
322 result_type operator()();
323 void discard(unsigned long long z);
324
325 // property functions
326 const Engine& base() const;
327};
328
329template<class Engine, size_t k>
330bool
331operator==(
332 const shuffle_order_engine<Engine, k>& x,
333 const shuffle_order_engine<Engine, k>& y);
334
335template<class Engine, size_t k>
336bool
337operator!=(
338 const shuffle_order_engine<Engine, k>& x,
339 const shuffle_order_engine<Engine, k>& y);
340
341template <class charT, class traits,
342 class Engine, size_t k>
343basic_ostream<charT, traits>&
344operator<<(basic_ostream<charT, traits>& os,
345 const shuffle_order_engine<Engine, k>& x);
346
347template <class charT, class traits,
348 class Engine, size_t k>
349basic_istream<charT, traits>&
350operator>>(basic_istream<charT, traits>& is,
351 shuffle_order_engine<Engine, k>& x);
352
353typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
354 minstd_rand0;
355typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
356 minstd_rand;
357typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
358 0x9908b0df,
359 11, 0xffffffff,
360 7, 0x9d2c5680,
361 15, 0xefc60000,
362 18, 1812433253> mt19937;
363typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
364 0xb5026f5aa96619e9,
365 29, 0x5555555555555555,
366 17, 0x71d67fffeda60000,
367 37, 0xfff7eee000000000,
368 43, 6364136223846793005> mt19937_64;
369typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
370typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
371typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
372typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
373typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
374typedef minstd_rand0 default_random_engine;
375
376// Generators
377
378class random_device
379{
380public:
381 // types
382 typedef unsigned int result_type;
383
384 // generator characteristics
385 static constexpr result_type min() { return numeric_limits<result_type>::min(); }
386 static constexpr result_type max() { return numeric_limits<result_type>::max(); }
387
388 // constructors
389 explicit random_device(const string& token = "/dev/urandom");
390
391 // generating functions
392 result_type operator()();
393
394 // property functions
395 double entropy() const;
396
397 // no copy functions
398 random_device(const random_device& ) = delete;
399 void operator=(const random_device& ) = delete;
400};
401
402// Utilities
403
404class seed_seq
405{
406public:
407 // types
408 typedef uint_least32_t result_type;
409
410 // constructors
411 seed_seq();
412 template<class T>
413 seed_seq(initializer_list<T> il);
414 template<class InputIterator>
415 seed_seq(InputIterator begin, InputIterator end);
416
417 // generating functions
418 template<class RandomAccessIterator>
419 void generate(RandomAccessIterator begin, RandomAccessIterator end);
420
421 // property functions
422 size_t size() const;
423 template<class OutputIterator>
424 void param(OutputIterator dest) const;
425
426 // no copy functions
427 seed_seq(const seed_seq&) = delete;
428 void operator=(const seed_seq& ) = delete;
429};
430
431template<class RealType, size_t bits, class URNG>
432 RealType generate_canonical(URNG& g);
433
434// Distributions
435
436template<class IntType = int>
437class uniform_int_distribution
438{
439public:
440 // types
441 typedef IntType result_type;
442
443 class param_type
444 {
445 public:
446 typedef uniform_int_distribution distribution_type;
447
448 explicit param_type(IntType a = 0,
449 IntType b = numeric_limits<IntType>::max());
450
451 result_type a() const;
452 result_type b() const;
453
454 friend bool operator==(const param_type& x, const param_type& y);
455 friend bool operator!=(const param_type& x, const param_type& y);
456 };
457
458 // constructors and reset functions
459 explicit uniform_int_distribution(IntType a = 0,
460 IntType b = numeric_limits<IntType>::max());
461 explicit uniform_int_distribution(const param_type& parm);
462 void reset();
463
464 // generating functions
465 template<class URNG> result_type operator()(URNG& g);
466 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
467
468 // property functions
469 result_type a() const;
470 result_type b() const;
471
472 param_type param() const;
473 void param(const param_type& parm);
474
475 result_type min() const;
476 result_type max() const;
477
478 friend bool operator==(const uniform_int_distribution& x,
479 const uniform_int_distribution& y);
480 friend bool operator!=(const uniform_int_distribution& x,
481 const uniform_int_distribution& y);
482
483 template <class charT, class traits>
484 friend
485 basic_ostream<charT, traits>&
486 operator<<(basic_ostream<charT, traits>& os,
487 const uniform_int_distribution& x);
488
489 template <class charT, class traits>
490 friend
491 basic_istream<charT, traits>&
492 operator>>(basic_istream<charT, traits>& is,
493 uniform_int_distribution& x);
494};
495
496template<class RealType = double>
497class uniform_real_distribution
498{
499public:
500 // types
501 typedef RealType result_type;
502
503 class param_type
504 {
505 public:
506 typedef uniform_real_distribution distribution_type;
507
508 explicit param_type(RealType a = 0,
509 RealType b = 1);
510
511 result_type a() const;
512 result_type b() const;
513
514 friend bool operator==(const param_type& x, const param_type& y);
515 friend bool operator!=(const param_type& x, const param_type& y);
516 };
517
518 // constructors and reset functions
519 explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
520 explicit uniform_real_distribution(const param_type& parm);
521 void reset();
522
523 // generating functions
524 template<class URNG> result_type operator()(URNG& g);
525 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
526
527 // property functions
528 result_type a() const;
529 result_type b() const;
530
531 param_type param() const;
532 void param(const param_type& parm);
533
534 result_type min() const;
535 result_type max() const;
536
537 friend bool operator==(const uniform_real_distribution& x,
538 const uniform_real_distribution& y);
539 friend bool operator!=(const uniform_real_distribution& x,
540 const uniform_real_distribution& y);
541
542 template <class charT, class traits>
543 friend
544 basic_ostream<charT, traits>&
545 operator<<(basic_ostream<charT, traits>& os,
546 const uniform_real_distribution& x);
547
548 template <class charT, class traits>
549 friend
550 basic_istream<charT, traits>&
551 operator>>(basic_istream<charT, traits>& is,
552 uniform_real_distribution& x);
553};
554
555class bernoulli_distribution
556{
557public:
558 // types
559 typedef bool result_type;
560
561 class param_type
562 {
563 public:
564 typedef bernoulli_distribution distribution_type;
565
566 explicit param_type(double p = 0.5);
567
568 double p() const;
569
570 friend bool operator==(const param_type& x, const param_type& y);
571 friend bool operator!=(const param_type& x, const param_type& y);
572 };
573
574 // constructors and reset functions
575 explicit bernoulli_distribution(double p = 0.5);
576 explicit bernoulli_distribution(const param_type& parm);
577 void reset();
578
579 // generating functions
580 template<class URNG> result_type operator()(URNG& g);
581 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
582
583 // property functions
584 double p() const;
585
586 param_type param() const;
587 void param(const param_type& parm);
588
589 result_type min() const;
590 result_type max() const;
591
592 friend bool operator==(const bernoulli_distribution& x,
593 const bernoulli_distribution& y);
594 friend bool operator!=(const bernoulli_distribution& x,
595 const bernoulli_distribution& y);
596
597 template <class charT, class traits>
598 friend
599 basic_ostream<charT, traits>&
600 operator<<(basic_ostream<charT, traits>& os,
601 const bernoulli_distribution& x);
602
603 template <class charT, class traits>
604 friend
605 basic_istream<charT, traits>&
606 operator>>(basic_istream<charT, traits>& is,
607 bernoulli_distribution& x);
608};
609
610template<class IntType = int>
Howard Hinnant03aad812010-05-11 23:26:59 +0000611class binomial_distribution
612{
613public:
614 // types
615 typedef IntType result_type;
616
617 class param_type
618 {
619 public:
620 typedef binomial_distribution distribution_type;
621
622 explicit param_type(IntType t = 1, double p = 0.5);
623
624 IntType t() const;
625 double p() const;
626
627 friend bool operator==(const param_type& x, const param_type& y);
628 friend bool operator!=(const param_type& x, const param_type& y);
629 };
630
631 // constructors and reset functions
632 explicit binomial_distribution(IntType t = 1, double p = 0.5);
633 explicit binomial_distribution(const param_type& parm);
634 void reset();
635
636 // generating functions
637 template<class URNG> result_type operator()(URNG& g);
638 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
639
640 // property functions
641 IntType t() const;
642 double p() const;
643
644 param_type param() const;
645 void param(const param_type& parm);
646
647 result_type min() const;
648 result_type max() const;
649
650 friend bool operator==(const binomial_distribution& x,
651 const binomial_distribution& y);
652 friend bool operator!=(const binomial_distribution& x,
653 const binomial_distribution& y);
654
655 template <class charT, class traits>
656 friend
657 basic_ostream<charT, traits>&
658 operator<<(basic_ostream<charT, traits>& os,
659 const binomial_distribution& x);
660
661 template <class charT, class traits>
662 friend
663 basic_istream<charT, traits>&
664 operator>>(basic_istream<charT, traits>& is,
665 binomial_distribution& x);
666};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668template<class IntType = int>
669 class geometric_distribution;
670
671template<class IntType = int>
672 class negative_binomial_distribution;
673
674template<class IntType = int>
675 class poisson_distribution;
676
677template<class RealType = double>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000678class exponential_distribution
679{
680public:
681 // types
682 typedef RealType result_type;
683
684 class param_type
685 {
686 public:
687 typedef exponential_distribution distribution_type;
688
Howard Hinnanta64111c2010-05-12 21:02:31 +0000689 explicit param_type(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000690
Howard Hinnanta64111c2010-05-12 21:02:31 +0000691 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000692
693 friend bool operator==(const param_type& x, const param_type& y);
694 friend bool operator!=(const param_type& x, const param_type& y);
695 };
696
697 // constructors and reset functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000698 explicit exponential_distribution(result_type lambda = 1.0);
Howard Hinnant30a840f2010-05-12 17:08:57 +0000699 explicit exponential_distribution(const param_type& parm);
700 void reset();
701
702 // generating functions
703 template<class URNG> result_type operator()(URNG& g);
704 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
705
706 // property functions
Howard Hinnanta64111c2010-05-12 21:02:31 +0000707 result_type lambda() const;
Howard Hinnant30a840f2010-05-12 17:08:57 +0000708
709 param_type param() const;
710 void param(const param_type& parm);
711
712 result_type min() const;
713 result_type max() const;
714
715 friend bool operator==(const exponential_distribution& x,
716 const exponential_distribution& y);
717 friend bool operator!=(const exponential_distribution& x,
718 const exponential_distribution& y);
719
720 template <class charT, class traits>
721 friend
722 basic_ostream<charT, traits>&
723 operator<<(basic_ostream<charT, traits>& os,
724 const exponential_distribution& x);
725
726 template <class charT, class traits>
727 friend
728 basic_istream<charT, traits>&
729 operator>>(basic_istream<charT, traits>& is,
730 exponential_distribution& x);
731};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
733template<class RealType = double>
734 class gamma_distribution;
735
736template<class RealType = double>
737 class weibull_distribution;
738
739template<class RealType = double>
740 class extreme_value_distribution;
741
742template<class RealType = double>
Howard Hinnanta64111c2010-05-12 21:02:31 +0000743class normal_distribution
744{
745public:
746 // types
747 typedef RealType result_type;
748
749 class param_type
750 {
751 public:
752 typedef normal_distribution distribution_type;
753
754 explicit param_type(result_type mean = 0, result_type stddev = 1);
755
756 result_type mean() const;
757 result_type stddev() const;
758
759 friend bool operator==(const param_type& x, const param_type& y);
760 friend bool operator!=(const param_type& x, const param_type& y);
761 };
762
763 // constructors and reset functions
764 explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
765 explicit normal_distribution(const param_type& parm);
766 void reset();
767
768 // generating functions
769 template<class URNG> result_type operator()(URNG& g);
770 template<class URNG> result_type operator()(URNG& g, const param_type& parm);
771
772 // property functions
773 result_type mean() const;
774 result_type stddev() const;
775
776 param_type param() const;
777 void param(const param_type& parm);
778
779 result_type min() const;
780 result_type max() const;
781
782 friend bool operator==(const normal_distribution& x,
783 const normal_distribution& y);
784 friend bool operator!=(const normal_distribution& x,
785 const normal_distribution& y);
786
787 template <class charT, class traits>
788 friend
789 basic_ostream<charT, traits>&
790 operator<<(basic_ostream<charT, traits>& os,
791 const normal_distribution& x);
792
793 template <class charT, class traits>
794 friend
795 basic_istream<charT, traits>&
796 operator>>(basic_istream<charT, traits>& is,
797 normal_distribution& x);
798};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000799
800template<class RealType = double>
801 class lognormal_distribution;
802
803template<class RealType = double>
804 class chi_squared_distribution;
805
806template<class RealType = double>
807 class cauchy_distribution;
808
809template<class RealType = double>
810 class fisher_f_distribution;
811
812template<class RealType = double>
813 class student_t_distribution;
814
815template<class IntType = int>
816 class discrete_distribution;
817
818template<class RealType = double>
819 class piecewise_constant_distribution;
820
821template<class RealType = double>
822 class piecewise_linear_distribution;
823
824} // std
825*/
826
827#include <__config>
828#include <cstddef>
829#include <type_traits>
830#include <initializer_list>
831#include <cstdint>
832#include <limits>
833#include <algorithm>
834#include <vector>
835#include <string>
836#include <istream>
837#include <ostream>
Howard Hinnant30a840f2010-05-12 17:08:57 +0000838#include <cmath>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839
840#pragma GCC system_header
841
842_LIBCPP_BEGIN_NAMESPACE_STD
843
844// linear_congruential_engine
845
846template <unsigned long long __a, unsigned long long __c,
847 unsigned long long __m, unsigned long long _M,
848 bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
849struct __lce_ta;
850
851// 64
852
853template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
854struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
855{
856 typedef unsigned long long result_type;
857 static result_type next(result_type __x)
858 {
859 // Schrage's algorithm
860 const result_type __q = __m / __a;
861 const result_type __r = __m % __a;
862 const result_type __t0 = __a * (__x % __q);
863 const result_type __t1 = __r * (__x / __q);
864 __x = __t0 + (__t0 < __t1) * __m - __t1;
865 __x += __c - (__x >= __m - __c) * __m;
866 return __x;
867 }
868};
869
870template <unsigned long long __a, unsigned long long __m>
871struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
872{
873 typedef unsigned long long result_type;
874 static result_type next(result_type __x)
875 {
876 // Schrage's algorithm
877 const result_type __q = __m / __a;
878 const result_type __r = __m % __a;
879 const result_type __t0 = __a * (__x % __q);
880 const result_type __t1 = __r * (__x / __q);
881 __x = __t0 + (__t0 < __t1) * __m - __t1;
882 return __x;
883 }
884};
885
886template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
887struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
888{
889 typedef unsigned long long result_type;
890 static result_type next(result_type __x)
891 {
892 return (__a * __x + __c) % __m;
893 }
894};
895
896template <unsigned long long __a, unsigned long long __c>
897struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
898{
899 typedef unsigned long long result_type;
900 static result_type next(result_type __x)
901 {
902 return __a * __x + __c;
903 }
904};
905
906// 32
907
908template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
909struct __lce_ta<_A, _C, _M, unsigned(~0), true>
910{
911 typedef unsigned result_type;
912 static result_type next(result_type __x)
913 {
914 const result_type __a = static_cast<result_type>(_A);
915 const result_type __c = static_cast<result_type>(_C);
916 const result_type __m = static_cast<result_type>(_M);
917 // Schrage's algorithm
918 const result_type __q = __m / __a;
919 const result_type __r = __m % __a;
920 const result_type __t0 = __a * (__x % __q);
921 const result_type __t1 = __r * (__x / __q);
922 __x = __t0 + (__t0 < __t1) * __m - __t1;
923 __x += __c - (__x >= __m - __c) * __m;
924 return __x;
925 }
926};
927
928template <unsigned long long _A, unsigned long long _M>
929struct __lce_ta<_A, 0, _M, unsigned(~0), true>
930{
931 typedef unsigned result_type;
932 static result_type next(result_type __x)
933 {
934 const result_type __a = static_cast<result_type>(_A);
935 const result_type __m = static_cast<result_type>(_M);
936 // Schrage's algorithm
937 const result_type __q = __m / __a;
938 const result_type __r = __m % __a;
939 const result_type __t0 = __a * (__x % __q);
940 const result_type __t1 = __r * (__x / __q);
941 __x = __t0 + (__t0 < __t1) * __m - __t1;
942 return __x;
943 }
944};
945
946template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
947struct __lce_ta<_A, _C, _M, unsigned(~0), false>
948{
949 typedef unsigned result_type;
950 static result_type next(result_type __x)
951 {
952 const result_type __a = static_cast<result_type>(_A);
953 const result_type __c = static_cast<result_type>(_C);
954 const result_type __m = static_cast<result_type>(_M);
955 return (__a * __x + __c) % __m;
956 }
957};
958
959template <unsigned long long _A, unsigned long long _C>
960struct __lce_ta<_A, _C, 0, unsigned(~0), false>
961{
962 typedef unsigned result_type;
963 static result_type next(result_type __x)
964 {
965 const result_type __a = static_cast<result_type>(_A);
966 const result_type __c = static_cast<result_type>(_C);
967 return __a * __x + __c;
968 }
969};
970
971// 16
972
973template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
974struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
975{
976 typedef unsigned short result_type;
977 static result_type next(result_type __x)
978 {
979 return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
980 }
981};
982
983template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
984class linear_congruential_engine;
985
986template <class _CharT, class _Traits,
987 class _U, _U _A, _U _C, _U _N>
988basic_ostream<_CharT, _Traits>&
989operator<<(basic_ostream<_CharT, _Traits>& __os,
990 const linear_congruential_engine<_U, _A, _C, _N>&);
991
992template <class _CharT, class _Traits,
993 class _U, _U _A, _U _C, _U _N>
994basic_istream<_CharT, _Traits>&
995operator>>(basic_istream<_CharT, _Traits>& __is,
996 linear_congruential_engine<_U, _A, _C, _N>& __x);
997
998template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
999class linear_congruential_engine
1000{
1001public:
1002 // types
1003 typedef _UIntType result_type;
1004
1005private:
1006 result_type __x_;
1007
1008 static const result_type _M = result_type(~0);
1009
1010 static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1011 static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1012public:
1013 static const result_type _Min = __c == 0u ? 1u: 0u;
1014 static const result_type _Max = __m - 1u;
1015 static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
1016
1017 // engine characteristics
1018 static const/*expr*/ result_type multiplier = __a;
1019 static const/*expr*/ result_type increment = __c;
1020 static const/*expr*/ result_type modulus = __m;
1021 static const/*expr*/ result_type min() {return _Min;}
1022 static const/*expr*/ result_type max() {return _Max;}
1023 static const/*expr*/ result_type default_seed = 1u;
1024
1025 // constructors and seeding functions
1026 explicit linear_congruential_engine(result_type __s = default_seed)
1027 {seed(__s);}
1028 template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q)
1029 {seed(__q);}
1030 void seed(result_type __s = default_seed)
1031 {seed(integral_constant<bool, __m == 0>(),
1032 integral_constant<bool, __c == 0>(), __s);}
1033 template<class _Sseq>
1034 typename enable_if
1035 <
1036 !is_convertible<_Sseq, result_type>::value,
1037 void
1038 >::type
1039 seed(_Sseq& __q)
1040 {__seed(__q, integral_constant<unsigned,
1041 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1042 : (__m-1) / 0x100000000ull)>());}
1043
1044 // generating functions
1045 result_type operator()()
1046 {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1047 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1048
1049 friend bool operator==(const linear_congruential_engine& __x,
1050 const linear_congruential_engine& __y)
1051 {return __x.__x_ == __y.__x_;}
1052 friend bool operator!=(const linear_congruential_engine& __x,
1053 const linear_congruential_engine& __y)
1054 {return !(__x == __y);}
1055
1056private:
1057
1058 void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1059 void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1060 void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1061 1 : __s % __m;}
1062 void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1063
1064 template<class _Sseq>
1065 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1066 template<class _Sseq>
1067 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1068
1069 template <class _CharT, class _Traits,
1070 class _U, _U _A, _U _C, _U _N>
1071 friend
1072 basic_ostream<_CharT, _Traits>&
1073 operator<<(basic_ostream<_CharT, _Traits>& __os,
1074 const linear_congruential_engine<_U, _A, _C, _N>&);
1075
1076 template <class _CharT, class _Traits,
1077 class _U, _U _A, _U _C, _U _N>
1078 friend
1079 basic_istream<_CharT, _Traits>&
1080 operator>>(basic_istream<_CharT, _Traits>& __is,
1081 linear_congruential_engine<_U, _A, _C, _N>& __x);
1082};
1083
1084template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1085template<class _Sseq>
1086void
1087linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1088 integral_constant<unsigned, 1>)
1089{
1090 const unsigned __k = 1;
1091 uint32_t __ar[__k+3];
1092 __q.generate(__ar, __ar + __k + 3);
1093 result_type __s = static_cast<result_type>(__ar[3] % __m);
1094 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1095}
1096
1097template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1098template<class _Sseq>
1099void
1100linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1101 integral_constant<unsigned, 2>)
1102{
1103 const unsigned __k = 2;
1104 uint32_t __ar[__k+3];
1105 __q.generate(__ar, __ar + __k + 3);
1106 result_type __s = static_cast<result_type>((__ar[3] +
1107 (uint64_t)__ar[4] << 32) % __m);
1108 __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1109}
1110
1111template <class _CharT, class _Traits>
1112class __save_flags
1113{
1114 typedef basic_ios<_CharT, _Traits> __stream_type;
1115 typedef typename __stream_type::fmtflags fmtflags;
1116
1117 __stream_type& __stream_;
1118 fmtflags __fmtflags_;
1119 _CharT __fill_;
1120
1121 __save_flags(const __save_flags&);
1122 __save_flags& operator=(const __save_flags&);
1123public:
1124 explicit __save_flags(__stream_type& __stream)
1125 : __stream_(__stream),
1126 __fmtflags_(__stream.flags()),
1127 __fill_(__stream.fill())
1128 {}
1129 ~__save_flags()
1130 {
1131 __stream_.flags(__fmtflags_);
1132 __stream_.fill(__fill_);
1133 }
1134};
1135
1136template <class _CharT, class _Traits,
1137 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1138inline
1139basic_ostream<_CharT, _Traits>&
1140operator<<(basic_ostream<_CharT, _Traits>& __os,
1141 const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1142{
1143 __save_flags<_CharT, _Traits> _(__os);
1144 __os.flags(ios_base::dec | ios_base::left);
1145 __os.fill(__os.widen(' '));
1146 return __os << __x.__x_;
1147}
1148
1149template <class _CharT, class _Traits,
1150 class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1151basic_istream<_CharT, _Traits>&
1152operator>>(basic_istream<_CharT, _Traits>& __is,
1153 linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1154{
1155 __save_flags<_CharT, _Traits> _(__is);
1156 __is.flags(ios_base::dec | ios_base::skipws);
1157 _UIntType __t;
1158 __is >> __t;
1159 if (!__is.fail())
1160 __x.__x_ = __t;
1161 return __is;
1162}
1163
1164typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
1165 minstd_rand0;
1166typedef minstd_rand0 default_random_engine;
1167typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
1168 minstd_rand;
1169// mersenne_twister_engine
1170
1171template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1172 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1173 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1174class mersenne_twister_engine;
1175
1176template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1177 _UI _A, size_t _U, _UI _D, size_t _S,
1178 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1179bool
1180operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1181 _B, _T, _C, _L, _F>& __x,
1182 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1183 _B, _T, _C, _L, _F>& __y);
1184
1185template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1186 _UI _A, size_t _U, _UI _D, size_t _S,
1187 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1188bool
1189operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1190 _B, _T, _C, _L, _F>& __x,
1191 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1192 _B, _T, _C, _L, _F>& __y);
1193
1194template <class _CharT, class _Traits,
1195 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1196 _UI _A, size_t _U, _UI _D, size_t _S,
1197 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1198basic_ostream<_CharT, _Traits>&
1199operator<<(basic_ostream<_CharT, _Traits>& __os,
1200 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1201 _B, _T, _C, _L, _F>& __x);
1202
1203template <class _CharT, class _Traits,
1204 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1205 _UI _A, size_t _U, _UI _D, size_t _S,
1206 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1207basic_istream<_CharT, _Traits>&
1208operator>>(basic_istream<_CharT, _Traits>& __is,
1209 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1210 _B, _T, _C, _L, _F>& __x);
1211
1212template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1213 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1214 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1215class mersenne_twister_engine
1216{
1217public:
1218 // types
1219 typedef _UIntType result_type;
1220
1221private:
1222 result_type __x_[__n];
1223 size_t __i_;
1224
1225 static_assert( 0 < __m, "mersenne_twister_engine invalid parameters");
1226 static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
1227 static const result_type _Dt = numeric_limits<result_type>::digits;
1228 static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
1229 static_assert( 2 <= __w, "mersenne_twister_engine invalid parameters");
1230 static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
1231 static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
1232 static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
1233 static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
1234 static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
1235public:
1236 static const result_type _Min = 0;
1237 static const result_type _Max = __w == _Dt ? result_type(~0) :
1238 (result_type(1) << __w) - result_type(1);
1239 static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
1240 static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
1241 static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
1242 static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
1243 static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
1244 static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
1245
1246 // engine characteristics
1247 static const/*expr*/ size_t word_size = __w;
1248 static const/*expr*/ size_t state_size = __n;
1249 static const/*expr*/ size_t shift_size = __m;
1250 static const/*expr*/ size_t mask_bits = __r;
1251 static const/*expr*/ result_type xor_mask = __a;
1252 static const/*expr*/ size_t tempering_u = __u;
1253 static const/*expr*/ result_type tempering_d = __d;
1254 static const/*expr*/ size_t tempering_s = __s;
1255 static const/*expr*/ result_type tempering_b = __b;
1256 static const/*expr*/ size_t tempering_t = __t;
1257 static const/*expr*/ result_type tempering_c = __c;
1258 static const/*expr*/ size_t tempering_l = __l;
1259 static const/*expr*/ result_type initialization_multiplier = __f;
1260 static const/*expr*/ result_type min() { return _Min; }
1261 static const/*expr*/ result_type max() { return _Max; }
1262 static const/*expr*/ result_type default_seed = 5489u;
1263
1264 // constructors and seeding functions
1265 explicit mersenne_twister_engine(result_type __sd = default_seed)
1266 {seed(__sd);}
1267 template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q)
1268 {seed(__q);}
1269 void seed(result_type __sd = default_seed);
1270 template<class _Sseq>
1271 typename enable_if
1272 <
1273 !is_convertible<_Sseq, result_type>::value,
1274 void
1275 >::type
1276 seed(_Sseq& __q)
1277 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1278
1279 // generating functions
1280 result_type operator()();
1281 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1282
1283 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1284 _UI _A, size_t _U, _UI _D, size_t _S,
1285 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1286 friend
1287 bool
1288 operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1289 _B, _T, _C, _L, _F>& __x,
1290 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1291 _B, _T, _C, _L, _F>& __y);
1292
1293 template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1294 _UI _A, size_t _U, _UI _D, size_t _S,
1295 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1296 friend
1297 bool
1298 operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1299 _B, _T, _C, _L, _F>& __x,
1300 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1301 _B, _T, _C, _L, _F>& __y);
1302
1303 template <class _CharT, class _Traits,
1304 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1305 _UI _A, size_t _U, _UI _D, size_t _S,
1306 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1307 friend
1308 basic_ostream<_CharT, _Traits>&
1309 operator<<(basic_ostream<_CharT, _Traits>& __os,
1310 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1311 _B, _T, _C, _L, _F>& __x);
1312
1313 template <class _CharT, class _Traits,
1314 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1315 _UI _A, size_t _U, _UI _D, size_t _S,
1316 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1317 friend
1318 basic_istream<_CharT, _Traits>&
1319 operator>>(basic_istream<_CharT, _Traits>& __is,
1320 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1321 _B, _T, _C, _L, _F>& __x);
1322private:
1323
1324 template<class _Sseq>
1325 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1326 template<class _Sseq>
1327 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1328
1329 template <size_t __count>
1330 static
1331 typename enable_if
1332 <
1333 __count < __w,
1334 result_type
1335 >::type
1336 __lshift(result_type __x) {return (__x << __count) & _Max;}
1337
1338 template <size_t __count>
1339 static
1340 typename enable_if
1341 <
1342 (__count >= __w),
1343 result_type
1344 >::type
1345 __lshift(result_type __x) {return result_type(0);}
1346
1347 template <size_t __count>
1348 static
1349 typename enable_if
1350 <
1351 __count < _Dt,
1352 result_type
1353 >::type
1354 __rshift(result_type __x) {return __x >> __count;}
1355
1356 template <size_t __count>
1357 static
1358 typename enable_if
1359 <
1360 (__count >= _Dt),
1361 result_type
1362 >::type
1363 __rshift(result_type __x) {return result_type(0);}
1364};
1365
1366template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1367 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1368 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1369void
1370mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1371 __t, __c, __l, __f>::seed(result_type __sd)
1372{ // __w >= 2
1373 __x_[0] = __sd & _Max;
1374 for (size_t __i = 1; __i < __n; ++__i)
1375 __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
1376 __i_ = 0;
1377}
1378
1379template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1380 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1381 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1382template<class _Sseq>
1383void
1384mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1385 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
1386{
1387 const unsigned __k = 1;
1388 uint32_t __ar[__n * __k];
1389 __q.generate(__ar, __ar + __n * __k);
1390 for (size_t __i = 0; __i < __n; ++__i)
1391 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1392 const result_type __mask = __r == _Dt ? result_type(~0) :
1393 (result_type(1) << __r) - result_type(1);
1394 __i_ = 0;
1395 if ((__x_[0] & ~__mask) == 0)
1396 {
1397 for (size_t __i = 1; __i < __n; ++__i)
1398 if (__x_[__i] != 0)
1399 return;
1400 __x_[0] = _Max;
1401 }
1402}
1403
1404template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1405 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1406 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1407template<class _Sseq>
1408void
1409mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1410 __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
1411{
1412 const unsigned __k = 2;
1413 uint32_t __ar[__n * __k];
1414 __q.generate(__ar, __ar + __n * __k);
1415 for (size_t __i = 0; __i < __n; ++__i)
1416 __x_[__i] = static_cast<result_type>(
1417 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1418 const result_type __mask = __r == _Dt ? result_type(~0) :
1419 (result_type(1) << __r) - result_type(1);
1420 __i_ = 0;
1421 if ((__x_[0] & ~__mask) == 0)
1422 {
1423 for (size_t __i = 1; __i < __n; ++__i)
1424 if (__x_[__i] != 0)
1425 return;
1426 __x_[0] = _Max;
1427 }
1428}
1429
1430template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
1431 _UIntType __a, size_t __u, _UIntType __d, size_t __s,
1432 _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
1433_UIntType
1434mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
1435 __t, __c, __l, __f>::operator()()
1436{
1437 const size_t __j = (__i_ + 1) % __n;
1438 const result_type __mask = __r == _Dt ? result_type(~0) :
1439 (result_type(1) << __r) - result_type(1);
1440 const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
1441 const size_t __k = (__i_ + __m) % __n;
1442 __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
1443 result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
1444 __i_ = __j;
1445 __z ^= __lshift<__s>(__z) & __b;
1446 __z ^= __lshift<__t>(__z) & __c;
1447 return __z ^ __rshift<__l>(__z);
1448}
1449
1450template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1451 _UI _A, size_t _U, _UI _D, size_t _S,
1452 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1453bool
1454operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1455 _B, _T, _C, _L, _F>& __x,
1456 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1457 _B, _T, _C, _L, _F>& __y)
1458{
1459 if (__x.__i_ == __y.__i_)
1460 return _STD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
1461 if (__x.__i_ == 0 || __y.__i_ == 0)
1462 {
1463 size_t __j = _STD::min(_N - __x.__i_, _N - __y.__i_);
1464 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1465 __y.__x_ + __y.__i_))
1466 return false;
1467 if (__x.__i_ == 0)
1468 return _STD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
1469 return _STD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
1470 }
1471 if (__x.__i_ < __y.__i_)
1472 {
1473 size_t __j = _N - __y.__i_;
1474 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1475 __y.__x_ + __y.__i_))
1476 return false;
1477 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
1478 __y.__x_))
1479 return false;
1480 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1481 __y.__x_ + (_N - (__x.__i_ + __j)));
1482 }
1483 size_t __j = _N - __x.__i_;
1484 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1485 __x.__x_ + __x.__i_))
1486 return false;
1487 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
1488 __x.__x_))
1489 return false;
1490 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1491 __x.__x_ + (_N - (__y.__i_ + __j)));
1492}
1493
1494template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1495 _UI _A, size_t _U, _UI _D, size_t _S,
1496 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1497inline
1498bool
1499operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1500 _B, _T, _C, _L, _F>& __x,
1501 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1502 _B, _T, _C, _L, _F>& __y)
1503{
1504 return !(__x == __y);
1505}
1506
1507template <class _CharT, class _Traits,
1508 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1509 _UI _A, size_t _U, _UI _D, size_t _S,
1510 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1511basic_ostream<_CharT, _Traits>&
1512operator<<(basic_ostream<_CharT, _Traits>& __os,
1513 const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1514 _B, _T, _C, _L, _F>& __x)
1515{
1516 __save_flags<_CharT, _Traits> _(__os);
1517 __os.flags(ios_base::dec | ios_base::left);
1518 _CharT __sp = __os.widen(' ');
1519 __os.fill(__sp);
1520 __os << __x.__x_[__x.__i_];
1521 for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
1522 __os << __sp << __x.__x_[__j];
1523 for (size_t __j = 0; __j < __x.__i_; ++__j)
1524 __os << __sp << __x.__x_[__j];
1525 return __os;
1526}
1527
1528template <class _CharT, class _Traits,
1529 class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
1530 _UI _A, size_t _U, _UI _D, size_t _S,
1531 _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
1532basic_istream<_CharT, _Traits>&
1533operator>>(basic_istream<_CharT, _Traits>& __is,
1534 mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
1535 _B, _T, _C, _L, _F>& __x)
1536{
1537 __save_flags<_CharT, _Traits> _(__is);
1538 __is.flags(ios_base::dec | ios_base::skipws);
1539 _UI __t[_N];
1540 for (size_t __i = 0; __i < _N; ++__i)
1541 __is >> __t[__i];
1542 if (!__is.fail())
1543 {
1544 for (size_t __i = 0; __i < _N; ++__i)
1545 __x.__x_[__i] = __t[__i];
1546 __x.__i_ = 0;
1547 }
1548 return __is;
1549}
1550
1551typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
1552 0x9908b0df, 11, 0xffffffff,
1553 7, 0x9d2c5680,
1554 15, 0xefc60000,
1555 18, 1812433253> mt19937;
1556typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
1557 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
1558 17, 0x71d67fffeda60000ULL,
1559 37, 0xfff7eee000000000ULL,
1560 43, 6364136223846793005ULL> mt19937_64;
1561
1562// subtract_with_carry_engine
1563
1564template<class _UIntType, size_t __w, size_t __s, size_t __r>
1565class subtract_with_carry_engine;
1566
1567template<class _UI, size_t _W, size_t _S, size_t _R>
1568bool
1569operator==(
1570 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1571 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1572
1573template<class _UI, size_t _W, size_t _S, size_t _R>
1574bool
1575operator!=(
1576 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1577 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1578
1579template <class _CharT, class _Traits,
1580 class _UI, size_t _W, size_t _S, size_t _R>
1581basic_ostream<_CharT, _Traits>&
1582operator<<(basic_ostream<_CharT, _Traits>& __os,
1583 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1584
1585template <class _CharT, class _Traits,
1586 class _UI, size_t _W, size_t _S, size_t _R>
1587basic_istream<_CharT, _Traits>&
1588operator>>(basic_istream<_CharT, _Traits>& __is,
1589 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1590
1591template<class _UIntType, size_t __w, size_t __s, size_t __r>
1592class subtract_with_carry_engine
1593{
1594public:
1595 // types
1596 typedef _UIntType result_type;
1597
1598private:
1599 result_type __x_[__r];
1600 result_type __c_;
1601 size_t __i_;
1602
1603 static const result_type _Dt = numeric_limits<result_type>::digits;
1604 static_assert( 0 < __w, "subtract_with_carry_engine invalid parameters");
1605 static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
1606 static_assert( 0 < __s, "subtract_with_carry_engine invalid parameters");
1607 static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
1608public:
1609 static const result_type _Min = 0;
1610 static const result_type _Max = __w == _Dt ? result_type(~0) :
1611 (result_type(1) << __w) - result_type(1);
1612 static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
1613
1614 // engine characteristics
1615 static const/*expr*/ size_t word_size = __w;
1616 static const/*expr*/ size_t short_lag = __s;
1617 static const/*expr*/ size_t long_lag = __r;
1618 static const/*expr*/ result_type min() { return _Min; }
1619 static const/*expr*/ result_type max() { return _Max; }
1620 static const/*expr*/ result_type default_seed = 19780503u;
1621
1622 // constructors and seeding functions
1623 explicit subtract_with_carry_engine(result_type __sd = default_seed)
1624 {seed(__sd);}
1625 template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q)
1626 {seed(__q);}
1627 void seed(result_type __sd = default_seed)
1628 {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1629 template<class _Sseq>
1630 typename enable_if
1631 <
1632 !is_convertible<_Sseq, result_type>::value,
1633 void
1634 >::type
1635 seed(_Sseq& __q)
1636 {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
1637
1638 // generating functions
1639 result_type operator()();
1640 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1641
1642 template<class _UI, size_t _W, size_t _S, size_t _R>
1643 friend
1644 bool
1645 operator==(
1646 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1647 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1648
1649 template<class _UI, size_t _W, size_t _S, size_t _R>
1650 friend
1651 bool
1652 operator!=(
1653 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1654 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
1655
1656 template <class _CharT, class _Traits,
1657 class _UI, size_t _W, size_t _S, size_t _R>
1658 friend
1659 basic_ostream<_CharT, _Traits>&
1660 operator<<(basic_ostream<_CharT, _Traits>& __os,
1661 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1662
1663 template <class _CharT, class _Traits,
1664 class _UI, size_t _W, size_t _S, size_t _R>
1665 friend
1666 basic_istream<_CharT, _Traits>&
1667 operator>>(basic_istream<_CharT, _Traits>& __is,
1668 subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
1669
1670private:
1671
1672 void seed(result_type __sd, integral_constant<unsigned, 1>);
1673 void seed(result_type __sd, integral_constant<unsigned, 2>);
1674 template<class _Sseq>
1675 void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1676 template<class _Sseq>
1677 void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1678};
1679
1680template<class _UIntType, size_t __w, size_t __s, size_t __r>
1681void
1682subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1683 integral_constant<unsigned, 1>)
1684{
1685 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1686 __e(__sd == 0u ? default_seed : __sd);
1687 for (size_t __i = 0; __i < __r; ++__i)
1688 __x_[__i] = static_cast<result_type>(__e() & _Max);
1689 __c_ = __x_[__r-1] == 0;
1690 __i_ = 0;
1691}
1692
1693template<class _UIntType, size_t __w, size_t __s, size_t __r>
1694void
1695subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
1696 integral_constant<unsigned, 2>)
1697{
1698 linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
1699 __e(__sd == 0u ? default_seed : __sd);
1700 for (size_t __i = 0; __i < __r; ++__i)
1701 __x_[__i] = static_cast<result_type>(
1702 (__e() + ((uint64_t)__e() << 32)) & _Max);
1703 __c_ = __x_[__r-1] == 0;
1704 __i_ = 0;
1705}
1706
1707template<class _UIntType, size_t __w, size_t __s, size_t __r>
1708template<class _Sseq>
1709void
1710subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1711 integral_constant<unsigned, 1>)
1712{
1713 const unsigned __k = 1;
1714 uint32_t __ar[__r * __k];
1715 __q.generate(__ar, __ar + __r * __k);
1716 for (size_t __i = 0; __i < __r; ++__i)
1717 __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
1718 __c_ = __x_[__r-1] == 0;
1719 __i_ = 0;
1720}
1721
1722template<class _UIntType, size_t __w, size_t __s, size_t __r>
1723template<class _Sseq>
1724void
1725subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
1726 integral_constant<unsigned, 2>)
1727{
1728 const unsigned __k = 2;
1729 uint32_t __ar[__r * __k];
1730 __q.generate(__ar, __ar + __r * __k);
1731 for (size_t __i = 0; __i < __r; ++__i)
1732 __x_[__i] = static_cast<result_type>(
1733 (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
1734 __c_ = __x_[__r-1] == 0;
1735 __i_ = 0;
1736}
1737
1738template<class _UIntType, size_t __w, size_t __s, size_t __r>
1739_UIntType
1740subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
1741{
1742 const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
1743 result_type& __xr = __x_[__i_];
1744 result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
1745 __xr = (__xs - __xr - __c_) & _Max;
1746 __c_ = __new_c;
1747 __i_ = (__i_ + 1) % __r;
1748 return __xr;
1749}
1750
1751template<class _UI, size_t _W, size_t _S, size_t _R>
1752bool
1753operator==(
1754 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1755 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1756{
1757 if (__x.__c_ != __y.__c_)
1758 return false;
1759 if (__x.__i_ == __y.__i_)
1760 return _STD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
1761 if (__x.__i_ == 0 || __y.__i_ == 0)
1762 {
1763 size_t __j = _STD::min(_R - __x.__i_, _R - __y.__i_);
1764 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
1765 __y.__x_ + __y.__i_))
1766 return false;
1767 if (__x.__i_ == 0)
1768 return _STD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
1769 return _STD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
1770 }
1771 if (__x.__i_ < __y.__i_)
1772 {
1773 size_t __j = _R - __y.__i_;
1774 if (!_STD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
1775 __y.__x_ + __y.__i_))
1776 return false;
1777 if (!_STD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
1778 __y.__x_))
1779 return false;
1780 return _STD::equal(__x.__x_, __x.__x_ + __x.__i_,
1781 __y.__x_ + (_R - (__x.__i_ + __j)));
1782 }
1783 size_t __j = _R - __x.__i_;
1784 if (!_STD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
1785 __x.__x_ + __x.__i_))
1786 return false;
1787 if (!_STD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
1788 __x.__x_))
1789 return false;
1790 return _STD::equal(__y.__x_, __y.__x_ + __y.__i_,
1791 __x.__x_ + (_R - (__y.__i_ + __j)));
1792}
1793
1794template<class _UI, size_t _W, size_t _S, size_t _R>
1795inline
1796bool
1797operator!=(
1798 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
1799 const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
1800{
1801 return !(__x == __y);
1802}
1803
1804template <class _CharT, class _Traits,
1805 class _UI, size_t _W, size_t _S, size_t _R>
1806basic_ostream<_CharT, _Traits>&
1807operator<<(basic_ostream<_CharT, _Traits>& __os,
1808 const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1809{
1810 __save_flags<_CharT, _Traits> _(__os);
1811 __os.flags(ios_base::dec | ios_base::left);
1812 _CharT __sp = __os.widen(' ');
1813 __os.fill(__sp);
1814 __os << __x.__x_[__x.__i_];
1815 for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
1816 __os << __sp << __x.__x_[__j];
1817 for (size_t __j = 0; __j < __x.__i_; ++__j)
1818 __os << __sp << __x.__x_[__j];
1819 __os << __sp << __x.__c_;
1820 return __os;
1821}
1822
1823template <class _CharT, class _Traits,
1824 class _UI, size_t _W, size_t _S, size_t _R>
1825basic_istream<_CharT, _Traits>&
1826operator>>(basic_istream<_CharT, _Traits>& __is,
1827 subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
1828{
1829 __save_flags<_CharT, _Traits> _(__is);
1830 __is.flags(ios_base::dec | ios_base::skipws);
1831 _UI __t[_R+1];
1832 for (size_t __i = 0; __i < _R+1; ++__i)
1833 __is >> __t[__i];
1834 if (!__is.fail())
1835 {
1836 for (size_t __i = 0; __i < _R; ++__i)
1837 __x.__x_[__i] = __t[__i];
1838 __x.__c_ = __t[_R];
1839 __x.__i_ = 0;
1840 }
1841 return __is;
1842}
1843
1844typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24> ranlux24_base;
1845typedef subtract_with_carry_engine<uint_fast64_t, 48, 5, 12> ranlux48_base;
1846
1847// discard_block_engine
1848
1849template<class _Engine, size_t __p, size_t __r>
1850class discard_block_engine
1851{
1852 _Engine __e_;
1853 int __n_;
1854
1855 static_assert( 0 < __r, "discard_block_engine invalid parameters");
1856 static_assert(__r <= __p, "discard_block_engine invalid parameters");
1857public:
1858 // types
1859 typedef typename _Engine::result_type result_type;
1860
1861 // engine characteristics
1862 static const/*expr*/ size_t block_size = __p;
1863 static const/*expr*/ size_t used_block = __r;
1864
1865 // Temporary work around for lack of constexpr
1866 static const result_type _Min = _Engine::_Min;
1867 static const result_type _Max = _Engine::_Max;
1868
1869 static const/*expr*/ result_type min() { return _Engine::min(); }
1870 static const/*expr*/ result_type max() { return _Engine::max(); }
1871
1872 // constructors and seeding functions
1873 discard_block_engine() : __n_(0) {}
1874// explicit discard_block_engine(const _Engine& __e);
1875// explicit discard_block_engine(_Engine&& __e);
1876 explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
1877 template<class _Sseq> explicit discard_block_engine(_Sseq& __q)
1878 : __e_(__q), __n_(0) {}
1879 void seed() {__e_.seed(); __n_ = 0;}
1880 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
1881 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
1882
1883 // generating functions
1884 result_type operator()();
1885 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1886
1887 // property functions
1888 const _Engine& base() const {return __e_;}
1889
1890 template<class _Eng, size_t _P, size_t _R>
1891 friend
1892 bool
1893 operator==(
1894 const discard_block_engine<_Eng, _P, _R>& __x,
1895 const discard_block_engine<_Eng, _P, _R>& __y);
1896
1897 template<class _Eng, size_t _P, size_t _R>
1898 friend
1899 bool
1900 operator!=(
1901 const discard_block_engine<_Eng, _P, _R>& __x,
1902 const discard_block_engine<_Eng, _P, _R>& __y);
1903
1904 template <class _CharT, class _Traits,
1905 class _Eng, size_t _P, size_t _R>
1906 friend
1907 basic_ostream<_CharT, _Traits>&
1908 operator<<(basic_ostream<_CharT, _Traits>& __os,
1909 const discard_block_engine<_Eng, _P, _R>& __x);
1910
1911 template <class _CharT, class _Traits,
1912 class _Eng, size_t _P, size_t _R>
1913 friend
1914 basic_istream<_CharT, _Traits>&
1915 operator>>(basic_istream<_CharT, _Traits>& __is,
1916 discard_block_engine<_Eng, _P, _R>& __x);
1917};
1918
1919template<class _Engine, size_t __p, size_t __r>
1920typename discard_block_engine<_Engine, __p, __r>::result_type
1921discard_block_engine<_Engine, __p, __r>::operator()()
1922{
1923 if (__n_ >= __r)
1924 {
1925 __e_.discard(__p - __r);
1926 __n_ = 0;
1927 }
1928 ++__n_;
1929 return __e_();
1930}
1931
1932template<class _Eng, size_t _P, size_t _R>
1933inline
1934bool
1935operator==(const discard_block_engine<_Eng, _P, _R>& __x,
1936 const discard_block_engine<_Eng, _P, _R>& __y)
1937{
1938 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
1939}
1940
1941template<class _Eng, size_t _P, size_t _R>
1942inline
1943bool
1944operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
1945 const discard_block_engine<_Eng, _P, _R>& __y)
1946{
1947 return !(__x == __y);
1948}
1949
1950template <class _CharT, class _Traits,
1951 class _Eng, size_t _P, size_t _R>
1952basic_ostream<_CharT, _Traits>&
1953operator<<(basic_ostream<_CharT, _Traits>& __os,
1954 const discard_block_engine<_Eng, _P, _R>& __x)
1955{
1956 __save_flags<_CharT, _Traits> _(__os);
1957 __os.flags(ios_base::dec | ios_base::left);
1958 _CharT __sp = __os.widen(' ');
1959 __os.fill(__sp);
1960 return __os << __x.__e_ << __sp << __x.__n_;
1961}
1962
1963template <class _CharT, class _Traits,
1964 class _Eng, size_t _P, size_t _R>
1965basic_istream<_CharT, _Traits>&
1966operator>>(basic_istream<_CharT, _Traits>& __is,
1967 discard_block_engine<_Eng, _P, _R>& __x)
1968{
1969 __save_flags<_CharT, _Traits> _(__is);
1970 __is.flags(ios_base::dec | ios_base::skipws);
1971 _Eng __e;
1972 int __n;
1973 __is >> __e >> __n;
1974 if (!__is.fail())
1975 {
1976 __x.__e_ = __e;
1977 __x.__n_ = __n;
1978 }
1979 return __is;
1980}
1981
1982typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
1983typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
1984
1985// independent_bits_engine
1986
1987template <unsigned long long _X, size_t _R>
1988struct __log2_imp
1989{
1990 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
1991 : __log2_imp<_X, _R - 1>::value;
1992};
1993
1994template <unsigned long long _X>
1995struct __log2_imp<_X, 0>
1996{
1997 static const size_t value = 0;
1998};
1999
2000template <size_t _R>
2001struct __log2_imp<0, _R>
2002{
2003 static const size_t value = _R + 1;
2004};
2005
2006template <class _UI, _UI _X>
2007struct __log2
2008{
2009 static const size_t value = __log2_imp<_X,
2010 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
2011};
2012
2013template<class _Engine, size_t __w, class _UIntType>
2014class independent_bits_engine
2015{
2016 template <class _UI, _UI _R0, size_t _W, size_t _M>
2017 class __get_n
2018 {
2019 static const size_t _Dt = numeric_limits<_UI>::digits;
2020 static const size_t _N = _W / _M + (_W % _M != 0);
2021 static const size_t _W0 = _W / _N;
2022 static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2023 public:
2024 static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2025 };
2026public:
2027 // types
2028 typedef _UIntType result_type;
2029
2030private:
2031 _Engine __e_;
2032
2033 static const result_type _Dt = numeric_limits<result_type>::digits;
2034 static_assert( 0 < __w, "independent_bits_engine invalid parameters");
2035 static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2036
2037 typedef typename _Engine::result_type _Engine_result_type;
2038 typedef typename conditional
2039 <
2040 sizeof(_Engine_result_type) <= sizeof(result_type),
2041 result_type,
2042 _Engine_result_type
2043 >::type _Working_result_type;
2044 // Temporary work around for lack of constexpr
2045 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2046 + _Working_result_type(1);
2047 static const size_t __m = __log2<_Working_result_type, _R>::value;
2048 static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2049 static const size_t __w0 = __w / __n;
2050 static const size_t __n0 = __n - __w % __n;
2051 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2052 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2053 static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2054 (_R >> __w0) << __w0;
2055 static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2056 (_R >> (__w0+1)) << (__w0+1);
2057 static const _Engine_result_type __mask0 = __w0 > 0 ?
2058 _Engine_result_type(~0) >> (_EDt - __w0) :
2059 _Engine_result_type(0);
2060 static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2061 _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2062 _Engine_result_type(~0);
2063public:
2064 static const result_type _Min = 0;
2065 static const result_type _Max = __w == _Dt ? result_type(~0) :
2066 (result_type(1) << __w) - result_type(1);
2067 static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2068
2069 // engine characteristics
2070 static const/*expr*/ result_type min() { return _Min; }
2071 static const/*expr*/ result_type max() { return _Max; }
2072
2073 // constructors and seeding functions
2074 independent_bits_engine() {}
2075// explicit independent_bits_engine(const _Engine& __e);
2076// explicit independent_bits_engine(_Engine&& __e);
2077 explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2078 template<class _Sseq> explicit independent_bits_engine(_Sseq& __q)
2079 : __e_(__q) {}
2080 void seed() {__e_.seed();}
2081 void seed(result_type __sd) {__e_.seed(__sd);}
2082 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q);}
2083
2084 // generating functions
2085 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2086 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2087
2088 // property functions
2089 const _Engine& base() const {return __e_;}
2090
2091 template<class _Eng, size_t _W, class _UI>
2092 friend
2093 bool
2094 operator==(
2095 const independent_bits_engine<_Eng, _W, _UI>& __x,
2096 const independent_bits_engine<_Eng, _W, _UI>& __y);
2097
2098 template<class _Eng, size_t _W, class _UI>
2099 friend
2100 bool
2101 operator!=(
2102 const independent_bits_engine<_Eng, _W, _UI>& __x,
2103 const independent_bits_engine<_Eng, _W, _UI>& __y);
2104
2105 template <class _CharT, class _Traits,
2106 class _Eng, size_t _W, class _UI>
2107 friend
2108 basic_ostream<_CharT, _Traits>&
2109 operator<<(basic_ostream<_CharT, _Traits>& __os,
2110 const independent_bits_engine<_Eng, _W, _UI>& __x);
2111
2112 template <class _CharT, class _Traits,
2113 class _Eng, size_t _W, class _UI>
2114 friend
2115 basic_istream<_CharT, _Traits>&
2116 operator>>(basic_istream<_CharT, _Traits>& __is,
2117 independent_bits_engine<_Eng, _W, _UI>& __x);
2118
2119private:
2120 result_type __eval(false_type);
2121 result_type __eval(true_type);
2122
2123 template <size_t __count>
2124 static
2125 typename enable_if
2126 <
2127 __count < _Dt,
2128 result_type
2129 >::type
2130 __lshift(result_type __x) {return __x << __count;}
2131
2132 template <size_t __count>
2133 static
2134 typename enable_if
2135 <
2136 (__count >= _Dt),
2137 result_type
2138 >::type
2139 __lshift(result_type __x) {return result_type(0);}
2140};
2141
2142template<class _Engine, size_t __w, class _UIntType>
2143inline
2144_UIntType
2145independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
2146{
2147 return static_cast<result_type>(__e_() & __mask0);
2148}
2149
2150template<class _Engine, size_t __w, class _UIntType>
2151_UIntType
2152independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
2153{
2154 result_type _S = 0;
2155 for (size_t __k = 0; __k < __n0; ++__k)
2156 {
2157 _Engine_result_type __u;
2158 do
2159 {
2160 __u = __e_() - _Engine::min();
2161 } while (__u >= __y0);
2162 _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
2163 }
2164 for (size_t __k = __n0; __k < __n; ++__k)
2165 {
2166 _Engine_result_type __u;
2167 do
2168 {
2169 __u = __e_() - _Engine::min();
2170 } while (__u >= __y1);
2171 _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
2172 }
2173 return _S;
2174}
2175
2176template<class _Eng, size_t _W, class _UI>
2177inline
2178bool
2179operator==(
2180 const independent_bits_engine<_Eng, _W, _UI>& __x,
2181 const independent_bits_engine<_Eng, _W, _UI>& __y)
2182{
2183 return __x.base() == __y.base();
2184}
2185
2186template<class _Eng, size_t _W, class _UI>
2187inline
2188bool
2189operator!=(
2190 const independent_bits_engine<_Eng, _W, _UI>& __x,
2191 const independent_bits_engine<_Eng, _W, _UI>& __y)
2192{
2193 return !(__x == __y);
2194}
2195
2196template <class _CharT, class _Traits,
2197 class _Eng, size_t _W, class _UI>
2198basic_ostream<_CharT, _Traits>&
2199operator<<(basic_ostream<_CharT, _Traits>& __os,
2200 const independent_bits_engine<_Eng, _W, _UI>& __x)
2201{
2202 return __os << __x.base();
2203}
2204
2205template <class _CharT, class _Traits,
2206 class _Eng, size_t _W, class _UI>
2207basic_istream<_CharT, _Traits>&
2208operator>>(basic_istream<_CharT, _Traits>& __is,
2209 independent_bits_engine<_Eng, _W, _UI>& __x)
2210{
2211 _Eng __e;
2212 __is >> __e;
2213 if (!__is.fail())
2214 __x.__e_ = __e;
2215 return __is;
2216}
2217
2218// shuffle_order_engine
2219
2220template <uint64_t _Xp, uint64_t _Yp>
2221struct __ugcd
2222{
2223 static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
2224};
2225
2226template <uint64_t _Xp>
2227struct __ugcd<_Xp, 0>
2228{
2229 static const uint64_t value = _Xp;
2230};
2231
2232template <uint64_t _N, uint64_t _D>
2233class __uratio
2234{
2235 static_assert(_D != 0, "__uratio divide by 0");
2236 static const uint64_t __gcd = __ugcd<_N, _D>::value;
2237public:
2238 static const uint64_t num = _N / __gcd;
2239 static const uint64_t den = _D / __gcd;
2240
2241 typedef __uratio<num, den> type;
2242};
2243
2244template<class _Engine, size_t __k>
2245class shuffle_order_engine
2246{
2247 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
2248public:
2249 // types
2250 typedef typename _Engine::result_type result_type;
2251
2252private:
2253 _Engine __e_;
2254 result_type _V_[__k];
2255 result_type _Y_;
2256
2257public:
2258 // engine characteristics
2259 static const/*expr*/ size_t table_size = __k;
2260
2261 static const result_type _Min = _Engine::_Min;
2262 static const result_type _Max = _Engine::_Max;
2263 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
2264 static const/*expr*/ result_type min() { return _Min; }
2265 static const/*expr*/ result_type max() { return _Max; }
2266
2267 static const unsigned long long _R = _Max - _Min + 1ull;
2268
2269 // constructors and seeding functions
2270 shuffle_order_engine() {__init();}
2271// explicit shuffle_order_engine(const _Engine& __e);
2272// explicit shuffle_order_engine(_Engine&& e);
2273 explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
2274 template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q)
2275 : __e_(__q) {__init();}
2276 void seed() {__e_.seed(); __init();}
2277 void seed(result_type __sd) {__e_.seed(__sd); __init();}
2278 template<class _Sseq> void seed(_Sseq& __q) {__e_.seed(__q); __init();}
2279
2280 // generating functions
2281 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2282 void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2283
2284 // property functions
2285 const _Engine& base() const {return __e_;}
2286
2287private:
2288 template<class _Eng, size_t _K>
2289 friend
2290 bool
2291 operator==(
2292 const shuffle_order_engine<_Eng, _K>& __x,
2293 const shuffle_order_engine<_Eng, _K>& __y);
2294
2295 template<class _Eng, size_t _K>
2296 friend
2297 bool
2298 operator!=(
2299 const shuffle_order_engine<_Eng, _K>& __x,
2300 const shuffle_order_engine<_Eng, _K>& __y);
2301
2302 template <class _CharT, class _Traits,
2303 class _Eng, size_t _K>
2304 friend
2305 basic_ostream<_CharT, _Traits>&
2306 operator<<(basic_ostream<_CharT, _Traits>& __os,
2307 const shuffle_order_engine<_Eng, _K>& __x);
2308
2309 template <class _CharT, class _Traits,
2310 class _Eng, size_t _K>
2311 friend
2312 basic_istream<_CharT, _Traits>&
2313 operator>>(basic_istream<_CharT, _Traits>& __is,
2314 shuffle_order_engine<_Eng, _K>& __x);
2315
2316 void __init()
2317 {
2318 for (size_t __i = 0; __i < __k; ++__i)
2319 _V_[__i] = __e_();
2320 _Y_ = __e_();
2321 }
2322
2323 result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
2324 result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
2325
2326 result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
2327 result_type __eval2(true_type) {return __evalf<__k, 0>();}
2328
2329 template <uint64_t _N, uint64_t _D>
2330 typename enable_if
2331 <
2332 (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
2333 result_type
2334 >::type
2335 __eval(__uratio<_N, _D>)
2336 {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
2337
2338 template <uint64_t _N, uint64_t _D>
2339 typename enable_if
2340 <
2341 __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
2342 result_type
2343 >::type
2344 __eval(__uratio<_N, _D>)
2345 {
2346 const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
2347 / __uratio<_N, _D>::den);
2348 _Y_ = _V_[__j];
2349 _V_[__j] = __e_();
2350 return _Y_;
2351 }
2352
2353 template <uint64_t __n, uint64_t __d>
2354 result_type __evalf()
2355 {
2356 const double _F = __d == 0 ?
2357 __n / (2. * 0x8000000000000000ull) :
2358 __n / (double)__d;
2359 const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
2360 _Y_ = _V_[__j];
2361 _V_[__j] = __e_();
2362 return _Y_;
2363 }
2364};
2365
2366template<class _Eng, size_t _K>
2367bool
2368operator==(
2369 const shuffle_order_engine<_Eng, _K>& __x,
2370 const shuffle_order_engine<_Eng, _K>& __y)
2371{
2372 return __x._Y_ == __y._Y_ && _STD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
2373 __x.__e_ == __y.__e_;
2374}
2375
2376template<class _Eng, size_t _K>
2377inline
2378bool
2379operator!=(
2380 const shuffle_order_engine<_Eng, _K>& __x,
2381 const shuffle_order_engine<_Eng, _K>& __y)
2382{
2383 return !(__x == __y);
2384}
2385
2386template <class _CharT, class _Traits,
2387 class _Eng, size_t _K>
2388basic_ostream<_CharT, _Traits>&
2389operator<<(basic_ostream<_CharT, _Traits>& __os,
2390 const shuffle_order_engine<_Eng, _K>& __x)
2391{
2392 __save_flags<_CharT, _Traits> _(__os);
2393 __os.flags(ios_base::dec | ios_base::left);
2394 _CharT __sp = __os.widen(' ');
2395 __os.fill(__sp);
2396 __os << __x.__e_ << __sp << __x._V_[0];
2397 for (size_t __i = 1; __i < _K; ++__i)
2398 __os << __sp << __x._V_[__i];
2399 return __os << __sp << __x._Y_;
2400}
2401
2402template <class _CharT, class _Traits,
2403 class _Eng, size_t _K>
2404basic_istream<_CharT, _Traits>&
2405operator>>(basic_istream<_CharT, _Traits>& __is,
2406 shuffle_order_engine<_Eng, _K>& __x)
2407{
2408 typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
2409 __save_flags<_CharT, _Traits> _(__is);
2410 __is.flags(ios_base::dec | ios_base::skipws);
2411 _Eng __e;
2412 result_type _V[_K+1];
2413 __is >> __e;
2414 for (size_t __i = 0; __i < _K+1; ++__i)
2415 __is >> _V[__i];
2416 if (!__is.fail())
2417 {
2418 __x.__e_ = __e;
2419 for (size_t __i = 0; __i < _K; ++__i)
2420 __x._V_[__i] = _V[__i];
2421 __x._Y_ = _V[_K];
2422 }
2423 return __is;
2424}
2425
2426typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
2427
2428// random_device
2429
2430class random_device
2431{
2432 int __f_;
2433public:
2434 // types
2435 typedef unsigned result_type;
2436
2437 // generator characteristics
2438 static const result_type _Min = 0;
2439 static const result_type _Max = 0xFFFFFFFFu;
2440
2441 static const/*expr*/ result_type min() { return _Min;}
2442 static const/*expr*/ result_type max() { return _Max;}
2443
2444 // constructors
2445 explicit random_device(const string& __token = "/dev/urandom");
2446 ~random_device();
2447
2448 // generating functions
2449 result_type operator()();
2450
2451 // property functions
2452 double entropy() const;
2453
2454private:
2455 // no copy functions
2456 random_device(const random_device&); // = delete;
2457 random_device& operator=(const random_device&); // = delete;
2458};
2459
2460// seed_seq
2461
2462class seed_seq
2463{
2464public:
2465 // types
2466 typedef uint32_t result_type;
2467
2468private:
2469 vector<result_type> __v_;
2470
2471 template<class _InputIterator>
2472 void init(_InputIterator __first, _InputIterator __last);
2473public:
2474 // constructors
2475 seed_seq() {}
2476 template<class _Tp>
2477 seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
2478
2479 template<class _InputIterator>
2480 seed_seq(_InputIterator __first, _InputIterator __last)
2481 {init(__first, __last);}
2482
2483 // generating functions
2484 template<class _RandomAccessIterator>
2485 void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
2486
2487 // property functions
2488 size_t size() const {return __v_.size();}
2489 template<class _OutputIterator>
2490 void param(_OutputIterator __dest) const
2491 {_STD::copy(__v_.begin(), __v_.end(), __dest);}
2492
2493private:
2494 // no copy functions
2495 seed_seq(const seed_seq&); // = delete;
2496 void operator=(const seed_seq&); // = delete;
2497
2498 static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
2499};
2500
2501template<class _InputIterator>
2502void
2503seed_seq::init(_InputIterator __first, _InputIterator __last)
2504{
2505 for (_InputIterator __s = __first; __s != __last; ++__s)
2506 __v_.push_back(*__s & 0xFFFFFFFF);
2507}
2508
2509template<class _RandomAccessIterator>
2510void
2511seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
2512{
2513 if (__first != __last)
2514 {
2515 _STD::fill(__first, __last, 0x8b8b8b8b);
2516 const size_t __n = static_cast<size_t>(__last - __first);
2517 const size_t __s = __v_.size();
2518 const size_t __t = (__n >= 623) ? 11
2519 : (__n >= 68) ? 7
2520 : (__n >= 39) ? 5
2521 : (__n >= 7) ? 3
2522 : (__n - 1) / 2;
2523 const size_t __p = (__n - __t) / 2;
2524 const size_t __q = __p + __t;
2525 const size_t __m = _STD::max(__s + 1, __n);
2526 // __k = 0;
2527 {
2528 result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
2529 ^ __first[__n - 1]);
2530 __first[__p] += __r;
2531 __r += __s;
2532 __first[__q] += __r;
2533 __first[0] = __r;
2534 }
2535 for (size_t __k = 1; __k <= __s; ++__k)
2536 {
2537 const size_t __kmodn = __k % __n;
2538 const size_t __kpmodn = (__k + __p) % __n;
2539 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2540 ^ __first[(__k - 1) % __n]);
2541 __first[__kpmodn] += __r;
2542 __r += __kmodn + __v_[__k-1];
2543 __first[(__k + __q) % __n] += __r;
2544 __first[__kmodn] = __r;
2545 }
2546 for (size_t __k = __s + 1; __k < __m; ++__k)
2547 {
2548 const size_t __kmodn = __k % __n;
2549 const size_t __kpmodn = (__k + __p) % __n;
2550 result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
2551 ^ __first[(__k - 1) % __n]);
2552 __first[__kpmodn] += __r;
2553 __r += __kmodn;
2554 __first[(__k + __q) % __n] += __r;
2555 __first[__kmodn] = __r;
2556 }
2557 for (size_t __k = __m; __k < __m + __n; ++__k)
2558 {
2559 const size_t __kmodn = __k % __n;
2560 const size_t __kpmodn = (__k + __p) % __n;
2561 result_type __r = 1566083941 * _T(__first[__kmodn] +
2562 __first[__kpmodn] +
2563 __first[(__k - 1) % __n]);
2564 __first[__kpmodn] ^= __r;
2565 __r -= __kmodn;
2566 __first[(__k + __q) % __n] ^= __r;
2567 __first[__kmodn] = __r;
2568 }
2569 }
2570}
2571
Howard Hinnant30a840f2010-05-12 17:08:57 +00002572// generate_canonical
2573
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574template<class _RealType, size_t __bits, class _URNG>
2575_RealType
2576generate_canonical(_URNG& __g)
2577{
2578 const size_t _Dt = numeric_limits<_RealType>::digits;
2579 const size_t __b = _Dt < __bits ? _Dt : __bits;
2580 const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
2581 const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
2582 const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
2583 _RealType __base = _R;
2584 _RealType _S = __g() - _URNG::_Min;
2585 for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
2586 _S += (__g() - _URNG::_Min) * __base;
2587 return _S / __base;
2588}
2589
2590// __independent_bits_engine
2591
2592template<class _Engine, class _UIntType>
2593class __independent_bits_engine
2594{
2595public:
2596 // types
2597 typedef _UIntType result_type;
2598
2599private:
2600 typedef typename _Engine::result_type _Engine_result_type;
2601 typedef typename conditional
2602 <
2603 sizeof(_Engine_result_type) <= sizeof(result_type),
2604 result_type,
2605 _Engine_result_type
2606 >::type _Working_result_type;
2607
2608 _Engine& __e_;
2609 size_t __w_;
2610 size_t __w0_;
2611 size_t __n_;
2612 size_t __n0_;
2613 _Working_result_type __y0_;
2614 _Working_result_type __y1_;
2615 _Engine_result_type __mask0_;
2616 _Engine_result_type __mask1_;
2617
2618 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2619 + _Working_result_type(1);
2620 static const size_t __m = __log2<_Working_result_type, _R>::value;
2621 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2622 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2623
2624public:
2625 // constructors and seeding functions
2626 __independent_bits_engine(_Engine& __e, size_t __w);
2627
2628 // generating functions
2629 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2630
2631private:
2632 result_type __eval(false_type);
2633 result_type __eval(true_type);
2634};
2635
2636template<class _Engine, class _UIntType>
2637__independent_bits_engine<_Engine, _UIntType>
2638 ::__independent_bits_engine(_Engine& __e, size_t __w)
2639 : __e_(__e),
2640 __w_(__w)
2641{
2642 __n_ = __w_ / __m + (__w_ % __m != 0);
2643 __w0_ = __w_ / __n_;
2644 if (_R == 0)
2645 __y0_ = _R;
2646 else if (__w0_ < _WDt)
2647 __y0_ = (_R >> __w0_) << __w0_;
2648 else
2649 __y0_ = 0;
2650 if (_R - __y0_ > __y0_ / __n_)
2651 {
2652 ++__n_;
2653 __w0_ = __w_ / __n_;
2654 if (__w0_ < _WDt)
2655 __y0_ = (_R >> __w0_) << __w0_;
2656 else
2657 __y0_ = 0;
2658 }
2659 __n0_ = __n_ - __w_ % __n_;
2660 if (__w0_ < _WDt - 1)
2661 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2662 else
2663 __y1_ = 0;
2664 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2665 _Engine_result_type(0);
2666 __mask1_ = __w0_ < _EDt - 1 ?
2667 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2668 _Engine_result_type(~0);
2669}
2670
2671template<class _Engine, class _UIntType>
2672inline
2673_UIntType
2674__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
2675{
2676 return static_cast<result_type>(__e_() & __mask0_);
2677}
2678
2679template<class _Engine, class _UIntType>
2680_UIntType
2681__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
2682{
2683 result_type _S = 0;
2684 for (size_t __k = 0; __k < __n0_; ++__k)
2685 {
2686 _Engine_result_type __u;
2687 do
2688 {
2689 __u = __e_() - _Engine::min();
2690 } while (__u >= __y0_);
2691 if (__w0_ < _EDt)
2692 _S <<= __w0_;
2693 else
2694 _S = 0;
2695 _S += __u & __mask0_;
2696 }
2697 for (size_t __k = __n0_; __k < __n_; ++__k)
2698 {
2699 _Engine_result_type __u;
2700 do
2701 {
2702 __u = __e_() - _Engine::min();
2703 } while (__u >= __y1_);
2704 if (__w0_ < _EDt - 1)
2705 _S <<= __w0_ + 1;
2706 else
2707 _S = 0;
2708 _S += __u & __mask1_;
2709 }
2710 return _S;
2711}
2712
Howard Hinnant30a840f2010-05-12 17:08:57 +00002713// uniform_int_distribution
2714
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715template<class _IntType = int>
2716class uniform_int_distribution
2717{
2718public:
2719 // types
2720 typedef _IntType result_type;
2721
2722 class param_type
2723 {
2724 result_type __a_;
2725 result_type __b_;
2726 public:
2727 typedef uniform_int_distribution distribution_type;
2728
2729 explicit param_type(result_type __a = 0,
2730 result_type __b = numeric_limits<result_type>::max())
2731 : __a_(__a), __b_(__b) {}
2732
2733 result_type a() const {return __a_;}
2734 result_type b() const {return __b_;}
2735
2736 friend bool operator==(const param_type& __x, const param_type& __y)
2737 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2738 friend bool operator!=(const param_type& __x, const param_type& __y)
2739 {return !(__x == __y);}
2740 };
2741
2742private:
2743 param_type __p_;
2744
2745public:
2746 // constructors and reset functions
2747 explicit uniform_int_distribution(result_type __a = 0,
2748 result_type __b = numeric_limits<result_type>::max())
2749 : __p_(param_type(__a, __b)) {}
2750 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2751 void reset() {}
2752
2753 // generating functions
2754 template<class _URNG> result_type operator()(_URNG& __g)
2755 {return (*this)(__g, __p_);}
2756 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2757
2758 // property functions
2759 result_type a() const {return __p_.a();}
2760 result_type b() const {return __p_.b();}
2761
2762 param_type param() const {return __p_;}
2763 void param(const param_type& __p) {__p_ = __p;}
2764
2765 result_type min() const {return a();}
2766 result_type max() const {return b();}
2767
2768 friend bool operator==(const uniform_int_distribution& __x,
2769 const uniform_int_distribution& __y)
2770 {return __x.__p_ == __y.__p_;}
2771 friend bool operator!=(const uniform_int_distribution& __x,
2772 const uniform_int_distribution& __y)
2773 {return !(__x == __y);}
2774};
2775
2776template<class _IntType>
2777template<class _URNG>
2778typename uniform_int_distribution<_IntType>::result_type
2779uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2780{
2781 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2782 uint32_t, uint64_t>::type _UIntType;
2783 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2784 if (_R == 1)
2785 return __p.a();
2786 const size_t _Dt = numeric_limits<_UIntType>::digits;
2787 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2788 if (_R == 0)
2789 return static_cast<result_type>(_Eng(__g, _Dt)());
2790 size_t __w = _Dt - __clz(_R) - 1;
2791 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2792 ++__w;
2793 _Eng __e(__g, __w);
2794 _UIntType __u;
2795 do
2796 {
2797 __u = __e();
2798 } while (__u >= _R);
2799 return static_cast<result_type>(__u + __p.a());
2800}
2801
2802template <class _CharT, class _Traits, class _IT>
2803basic_ostream<_CharT, _Traits>&
2804operator<<(basic_ostream<_CharT, _Traits>& __os,
2805 const uniform_int_distribution<_IT>& __x)
2806{
2807 __save_flags<_CharT, _Traits> _(__os);
2808 __os.flags(ios_base::dec | ios_base::left);
2809 _CharT __sp = __os.widen(' ');
2810 __os.fill(__sp);
2811 return __os << __x.a() << __sp << __x.b();
2812}
2813
2814template <class _CharT, class _Traits, class _IT>
2815basic_istream<_CharT, _Traits>&
2816operator>>(basic_istream<_CharT, _Traits>& __is,
2817 uniform_int_distribution<_IT>& __x)
2818{
2819 typedef uniform_int_distribution<_IT> _Eng;
2820 typedef typename _Eng::result_type result_type;
2821 typedef typename _Eng::param_type param_type;
2822 __save_flags<_CharT, _Traits> _(__is);
2823 __is.flags(ios_base::dec | ios_base::skipws);
2824 result_type __a;
2825 result_type __b;
2826 __is >> __a >> __b;
2827 if (!__is.fail())
2828 __x.param(param_type(__a, __b));
2829 return __is;
2830}
2831
Howard Hinnant30a840f2010-05-12 17:08:57 +00002832// uniform_real_distribution
2833
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834template<class _RealType = double>
2835class uniform_real_distribution
2836{
2837public:
2838 // types
2839 typedef _RealType result_type;
2840
2841 class param_type
2842 {
2843 result_type __a_;
2844 result_type __b_;
2845 public:
2846 typedef uniform_real_distribution distribution_type;
2847
2848 explicit param_type(result_type __a = 0,
2849 result_type __b = 1)
2850 : __a_(__a), __b_(__b) {}
2851
2852 result_type a() const {return __a_;}
2853 result_type b() const {return __b_;}
2854
2855 friend bool operator==(const param_type& __x, const param_type& __y)
2856 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2857 friend bool operator!=(const param_type& __x, const param_type& __y)
2858 {return !(__x == __y);}
2859 };
2860
2861private:
2862 param_type __p_;
2863
2864public:
2865 // constructors and reset functions
2866 explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
2867 : __p_(param_type(__a, __b)) {}
2868 explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
2869 void reset() {}
2870
2871 // generating functions
2872 template<class _URNG> result_type operator()(_URNG& __g)
2873 {return (*this)(__g, __p_);}
2874 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2875
2876 // property functions
2877 result_type a() const {return __p_.a();}
2878 result_type b() const {return __p_.b();}
2879
2880 param_type param() const {return __p_;}
2881 void param(const param_type& __p) {__p_ = __p;}
2882
2883 result_type min() const {return a();}
2884 result_type max() const {return b();}
2885
2886 friend bool operator==(const uniform_real_distribution& __x,
2887 const uniform_real_distribution& __y)
2888 {return __x.__p_ == __y.__p_;}
2889 friend bool operator!=(const uniform_real_distribution& __x,
2890 const uniform_real_distribution& __y)
2891 {return !(__x == __y);}
2892};
2893
2894template<class _RealType>
2895template<class _URNG>
2896inline
2897typename uniform_real_distribution<_RealType>::result_type
2898uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
2899{
2900 return (__p.b() - __p.a())
2901 * _STD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
2902 + __p.a();
2903}
2904
2905template <class _CharT, class _Traits, class _RT>
2906basic_ostream<_CharT, _Traits>&
2907operator<<(basic_ostream<_CharT, _Traits>& __os,
2908 const uniform_real_distribution<_RT>& __x)
2909{
2910 __save_flags<_CharT, _Traits> _(__os);
2911 __os.flags(ios_base::dec | ios_base::left);
2912 _CharT __sp = __os.widen(' ');
2913 __os.fill(__sp);
2914 return __os << __x.a() << __sp << __x.b();
2915}
2916
2917template <class _CharT, class _Traits, class _RT>
2918basic_istream<_CharT, _Traits>&
2919operator>>(basic_istream<_CharT, _Traits>& __is,
2920 uniform_real_distribution<_RT>& __x)
2921{
2922 typedef uniform_real_distribution<_RT> _Eng;
2923 typedef typename _Eng::result_type result_type;
2924 typedef typename _Eng::param_type param_type;
2925 __save_flags<_CharT, _Traits> _(__is);
2926 __is.flags(ios_base::dec | ios_base::skipws);
2927 result_type __a;
2928 result_type __b;
2929 __is >> __a >> __b;
2930 if (!__is.fail())
2931 __x.param(param_type(__a, __b));
2932 return __is;
2933}
2934
Howard Hinnant30a840f2010-05-12 17:08:57 +00002935// bernoulli_distribution
2936
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002937class bernoulli_distribution
2938{
2939public:
2940 // types
2941 typedef bool result_type;
2942
2943 class param_type
2944 {
2945 double __p_;
2946 public:
2947 typedef bernoulli_distribution distribution_type;
2948
2949 explicit param_type(double __p = 0.5) : __p_(__p) {}
2950
2951 double p() const {return __p_;}
2952
2953 friend bool operator==(const param_type& __x, const param_type& __y)
2954 {return __x.__p_ == __y.__p_;}
2955 friend bool operator!=(const param_type& __x, const param_type& __y)
2956 {return !(__x == __y);}
2957 };
2958
2959private:
2960 param_type __p_;
2961
2962public:
2963 // constructors and reset functions
2964 explicit bernoulli_distribution(double __p = 0.5)
2965 : __p_(param_type(__p)) {}
2966 explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
2967 void reset() {}
2968
2969 // generating functions
2970 template<class _URNG> result_type operator()(_URNG& __g)
2971 {return (*this)(__g, __p_);}
Howard Hinnant03aad812010-05-11 23:26:59 +00002972 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002973
2974 // property functions
2975 double p() const {return __p_.p();}
2976
2977 param_type param() const {return __p_;}
2978 void param(const param_type& __p) {__p_ = __p;}
2979
2980 result_type min() const {return false;}
2981 result_type max() const {return true;}
2982
2983 friend bool operator==(const bernoulli_distribution& __x,
2984 const bernoulli_distribution& __y)
2985 {return __x.__p_ == __y.__p_;}
2986 friend bool operator!=(const bernoulli_distribution& __x,
2987 const bernoulli_distribution& __y)
2988 {return !(__x == __y);}
2989};
2990
Howard Hinnant03aad812010-05-11 23:26:59 +00002991template<class _URNG>
2992inline
2993bernoulli_distribution::result_type
2994bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
2995{
2996 return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.);
2997}
2998
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002999template <class _CharT, class _Traits>
3000basic_ostream<_CharT, _Traits>&
3001operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3002{
3003 __save_flags<_CharT, _Traits> _(__os);
3004 __os.flags(ios_base::dec | ios_base::left);
3005 _CharT __sp = __os.widen(' ');
3006 __os.fill(__sp);
3007 return __os << __x.p();
3008}
3009
3010template <class _CharT, class _Traits>
3011basic_istream<_CharT, _Traits>&
3012operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3013{
3014 typedef bernoulli_distribution _Eng;
3015 typedef typename _Eng::param_type param_type;
3016 __save_flags<_CharT, _Traits> _(__is);
3017 __is.flags(ios_base::dec | ios_base::skipws);
3018 double __p;
3019 __is >> __p;
3020 if (!__is.fail())
3021 __x.param(param_type(__p));
3022 return __is;
3023}
3024
Howard Hinnant30a840f2010-05-12 17:08:57 +00003025// binomial_distribution
3026
Howard Hinnant03aad812010-05-11 23:26:59 +00003027template<class _IntType = int>
3028class binomial_distribution
3029{
3030public:
3031 // types
3032 typedef _IntType result_type;
3033
3034 class param_type
3035 {
3036 result_type __t_;
3037 double __p_;
3038 public:
3039 typedef binomial_distribution distribution_type;
3040
3041 explicit param_type(result_type __t = 1, double __p = 0.5)
3042 : __t_(__t), __p_(__p) {}
3043
3044 result_type t() const {return __t_;}
3045 double p() const {return __p_;}
3046
3047 friend bool operator==(const param_type& __x, const param_type& __y)
3048 {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3049 friend bool operator!=(const param_type& __x, const param_type& __y)
3050 {return !(__x == __y);}
3051 };
3052
3053private:
3054 param_type __p_;
3055
3056public:
3057 // constructors and reset functions
3058 explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3059 : __p_(param_type(__t, __p)) {}
3060 explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3061 void reset() {}
3062
3063 // generating functions
3064 template<class _URNG> result_type operator()(_URNG& __g)
3065 {return (*this)(__g, __p_);}
3066 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3067
3068 // property functions
3069 result_type t() const {return __p_.t();}
3070 double p() const {return __p_.p();}
3071
3072 param_type param() const {return __p_;}
3073 void param(const param_type& __p) {__p_ = __p;}
3074
3075 result_type min() const {return 0;}
3076 result_type max() const {return t();}
3077
3078 friend bool operator==(const binomial_distribution& __x,
3079 const binomial_distribution& __y)
3080 {return __x.__p_ == __y.__p_;}
3081 friend bool operator!=(const binomial_distribution& __x,
3082 const binomial_distribution& __y)
3083 {return !(__x == __y);}
3084};
3085
3086template<class _IntType>
3087template<class _URNG>
3088_IntType
3089binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3090{
3091 bernoulli_distribution __bd(__p.p());
3092 _IntType __r = 0;
3093 _IntType __t = __p.t();
3094 for (_IntType __i = 0; __i < __t; ++__i)
3095 __r += __bd(__g);
3096 return __r;
3097}
3098
3099template <class _CharT, class _Traits, class _IntType>
3100basic_ostream<_CharT, _Traits>&
3101operator<<(basic_ostream<_CharT, _Traits>& __os,
3102 const binomial_distribution<_IntType>& __x)
3103{
3104 __save_flags<_CharT, _Traits> _(__os);
3105 __os.flags(ios_base::dec | ios_base::left);
3106 _CharT __sp = __os.widen(' ');
3107 __os.fill(__sp);
3108 return __os << __x.t() << __sp << __x.p();
3109}
3110
3111template <class _CharT, class _Traits, class _IntType>
3112basic_istream<_CharT, _Traits>&
3113operator>>(basic_istream<_CharT, _Traits>& __is,
3114 binomial_distribution<_IntType>& __x)
3115{
3116 typedef binomial_distribution<_IntType> _Eng;
3117 typedef typename _Eng::result_type result_type;
3118 typedef typename _Eng::param_type param_type;
3119 __save_flags<_CharT, _Traits> _(__is);
3120 __is.flags(ios_base::dec | ios_base::skipws);
3121 result_type __t;
3122 double __p;
3123 __is >> __t >> __p;
3124 if (!__is.fail())
3125 __x.param(param_type(__t, __p));
3126 return __is;
3127}
3128
Howard Hinnant30a840f2010-05-12 17:08:57 +00003129// exponential_distribution
3130
3131template<class _RealType = double>
3132class exponential_distribution
3133{
3134public:
3135 // types
3136 typedef _RealType result_type;
3137
3138 class param_type
3139 {
3140 result_type __lambda_;
3141 public:
3142 typedef exponential_distribution distribution_type;
3143
3144 explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3145
3146 result_type lambda() const {return __lambda_;}
3147
3148 friend bool operator==(const param_type& __x, const param_type& __y)
3149 {return __x.__lambda_ == __y.__lambda_;}
3150 friend bool operator!=(const param_type& __x, const param_type& __y)
3151 {return !(__x == __y);}
3152 };
3153
3154private:
3155 param_type __p_;
3156
3157public:
3158 // constructors and reset functions
3159 explicit exponential_distribution(result_type __lambda = 1)
3160 : __p_(param_type(__lambda)) {}
3161 explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3162 void reset() {}
3163
3164 // generating functions
3165 template<class _URNG> result_type operator()(_URNG& __g)
3166 {return (*this)(__g, __p_);}
3167 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3168
3169 // property functions
3170 result_type lambda() const {return __p_.lambda();}
3171
3172 param_type param() const {return __p_;}
3173 void param(const param_type& __p) {__p_ = __p;}
3174
3175 result_type min() const {return 0;}
3176 result_type max() const
3177 {return -std::log(1-std::nextafter(result_type(1), result_type(-1))) /
3178 __p_.lambda();}
3179
3180 friend bool operator==(const exponential_distribution& __x,
3181 const exponential_distribution& __y)
3182 {return __x.__p_ == __y.__p_;}
3183 friend bool operator!=(const exponential_distribution& __x,
3184 const exponential_distribution& __y)
3185 {return !(__x == __y);}
3186};
3187
3188template <class _RealType>
3189template<class _URNG>
3190_RealType
3191exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3192{
3193 return -_STD::log
3194 (
3195 result_type(1) -
3196 _STD::generate_canonical<result_type,
3197 numeric_limits<result_type>::digits>(__g)
3198 )
3199 / __p.lambda();
3200}
3201
3202template <class _CharT, class _Traits, class _RealType>
3203basic_ostream<_CharT, _Traits>&
3204operator<<(basic_ostream<_CharT, _Traits>& __os,
3205 const exponential_distribution<_RealType>& __x)
3206{
3207 __save_flags<_CharT, _Traits> _(__os);
3208 __os.flags(ios_base::dec | ios_base::left);
3209 return __os << __x.lambda();
3210}
3211
3212template <class _CharT, class _Traits, class _RealType>
3213basic_istream<_CharT, _Traits>&
3214operator>>(basic_istream<_CharT, _Traits>& __is,
3215 exponential_distribution<_RealType>& __x)
3216{
3217 typedef exponential_distribution<_RealType> _Eng;
3218 typedef typename _Eng::result_type result_type;
3219 typedef typename _Eng::param_type param_type;
3220 __save_flags<_CharT, _Traits> _(__is);
3221 __is.flags(ios_base::dec | ios_base::skipws);
3222 result_type __lambda;
3223 __is >> __lambda;
3224 if (!__is.fail())
3225 __x.param(param_type(__lambda));
3226 return __is;
3227}
3228
Howard Hinnanta64111c2010-05-12 21:02:31 +00003229// normal_distribution
3230
3231template<class _RealType = double>
3232class normal_distribution
3233{
3234public:
3235 // types
3236 typedef _RealType result_type;
3237
3238 class param_type
3239 {
3240 result_type __mean_;
3241 result_type __stddev_;
3242 public:
3243 typedef normal_distribution distribution_type;
3244
3245 explicit param_type(result_type __mean = 0, result_type __stddev = 1)
3246 : __mean_(__mean), __stddev_(__stddev) {}
3247
3248 result_type mean() const {return __mean_;}
3249 result_type stddev() const {return __stddev_;}
3250
3251 friend bool operator==(const param_type& __x, const param_type& __y)
3252 {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
3253 friend bool operator!=(const param_type& __x, const param_type& __y)
3254 {return !(__x == __y);}
3255 };
3256
3257private:
3258 param_type __p_;
3259 result_type _V_;
3260 bool _V_hot_;
3261
3262public:
3263 // constructors and reset functions
3264 explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
3265 : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
3266 explicit normal_distribution(const param_type& __p)
3267 : __p_(__p), _V_hot_(false) {}
3268 void reset() {_V_hot_ = false;}
3269
3270 // generating functions
3271 template<class _URNG> result_type operator()(_URNG& __g)
3272 {return (*this)(__g, __p_);}
3273 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3274
3275 // property functions
3276 result_type mean() const {return __p_.mean();}
3277 result_type stddev() const {return __p_.stddev();}
3278
3279 param_type param() const {return __p_;}
3280 void param(const param_type& __p) {__p_ = __p;}
3281
3282 result_type min() const {return -numeric_limits<result_type>::infinity();}
3283 result_type max() const {return numeric_limits<result_type>::infinity();}
3284
3285 friend bool operator==(const normal_distribution& __x,
3286 const normal_distribution& __y)
3287 {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
3288 (!__x._V_hot_ || __x._V_ == __y._V_);}
3289 friend bool operator!=(const normal_distribution& __x,
3290 const normal_distribution& __y)
3291 {return !(__x == __y);}
3292
3293 template <class _CharT, class _Traits, class _RT>
3294 friend
3295 basic_ostream<_CharT, _Traits>&
3296 operator<<(basic_ostream<_CharT, _Traits>& __os,
3297 const normal_distribution<_RT>& __x);
3298
3299 template <class _CharT, class _Traits, class _RT>
3300 friend
3301 basic_istream<_CharT, _Traits>&
3302 operator>>(basic_istream<_CharT, _Traits>& __is,
3303 normal_distribution<_RT>& __x);
3304};
3305
3306template <class _RealType>
3307template<class _URNG>
3308_RealType
3309normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3310{
3311 result_type _U;
3312 if (_V_hot_)
3313 {
3314 _V_hot_ = false;
3315 _U = _V_;
3316 }
3317 else
3318 {
3319 uniform_real_distribution<result_type> _Uni(-1, 1);
3320 result_type __u;
3321 result_type __v;
3322 result_type __s;
3323 do
3324 {
3325 __u = _Uni(__g);
3326 __v = _Uni(__g);
3327 __s = __u * __u + __v * __v;
3328 } while (__s > 1 || __s == 0);
3329 result_type _F = _STD::sqrt(-2 * _STD::log(__s) / __s);
3330 _V_ = __v * _F;
3331 _V_hot_ = true;
3332 _U = __u * _F;
3333 }
3334 return _U * __p.stddev() + __p.mean();
3335}
3336
3337template <class _CharT, class _Traits, class _RT>
3338basic_ostream<_CharT, _Traits>&
3339operator<<(basic_ostream<_CharT, _Traits>& __os,
3340 const normal_distribution<_RT>& __x)
3341{
3342 __save_flags<_CharT, _Traits> _(__os);
3343 __os.flags(ios_base::dec | ios_base::left);
3344 _CharT __sp = __os.widen(' ');
3345 __os.fill(__sp);
3346 __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
3347 if (__x._V_hot_)
3348 __os << __sp << __x._V_;
3349 return __os;
3350}
3351
3352template <class _CharT, class _Traits, class _RT>
3353basic_istream<_CharT, _Traits>&
3354operator>>(basic_istream<_CharT, _Traits>& __is,
3355 normal_distribution<_RT>& __x)
3356{
3357 typedef normal_distribution<_RT> _Eng;
3358 typedef typename _Eng::result_type result_type;
3359 typedef typename _Eng::param_type param_type;
3360 __save_flags<_CharT, _Traits> _(__is);
3361 __is.flags(ios_base::dec | ios_base::skipws);
3362 result_type __mean;
3363 result_type __stddev;
3364 result_type _V = 0;
3365 bool _V_hot = false;
3366 __is >> __mean >> __stddev >> _V_hot;
3367 if (_V_hot)
3368 __is >> _V;
3369 if (!__is.fail())
3370 {
3371 __x.param(param_type(__mean, __stddev));
3372 __x._V_hot_ = _V_hot;
3373 __x._V_ = _V;
3374 }
3375 return __is;
3376}
3377
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378_LIBCPP_END_NAMESPACE_STD
3379
3380#endif // _LIBCPP_RANDOM