random revision 227825
1// -*- C++ -*-
2//===--------------------------- random -----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. 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_rand                                       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>
611class 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};
667
668template<class IntType = int>
669class geometric_distribution
670{
671public:
672    // types
673    typedef IntType result_type;
674
675    class param_type
676    {
677    public:
678        typedef geometric_distribution distribution_type;
679
680        explicit param_type(double p = 0.5);
681
682        double p() const;
683
684        friend bool operator==(const param_type& x, const param_type& y);
685        friend bool operator!=(const param_type& x, const param_type& y);
686    };
687
688    // constructors and reset functions
689    explicit geometric_distribution(double p = 0.5);
690    explicit geometric_distribution(const param_type& parm);
691    void reset();
692
693    // generating functions
694    template<class URNG> result_type operator()(URNG& g);
695    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
696
697    // property functions
698    double p() const;
699
700    param_type param() const;
701    void param(const param_type& parm);
702
703    result_type min() const;
704    result_type max() const;
705
706    friend bool operator==(const geometric_distribution& x,
707                           const geometric_distribution& y);
708    friend bool operator!=(const geometric_distribution& x,
709                           const geometric_distribution& y);
710
711    template <class charT, class traits>
712    friend
713    basic_ostream<charT, traits>&
714    operator<<(basic_ostream<charT, traits>& os,
715               const geometric_distribution& x);
716
717    template <class charT, class traits>
718    friend
719    basic_istream<charT, traits>&
720    operator>>(basic_istream<charT, traits>& is,
721               geometric_distribution& x);
722};
723
724template<class IntType = int>
725class negative_binomial_distribution
726{
727public:
728    // types
729    typedef IntType result_type;
730
731    class param_type
732    {
733    public:
734        typedef negative_binomial_distribution distribution_type;
735
736        explicit param_type(result_type k = 1, double p = 0.5);
737
738        result_type k() const;
739        double p() const;
740
741        friend bool operator==(const param_type& x, const param_type& y);
742        friend bool operator!=(const param_type& x, const param_type& y);
743    };
744
745    // constructor and reset functions
746    explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
747    explicit negative_binomial_distribution(const param_type& parm);
748    void reset();
749
750    // generating functions
751    template<class URNG> result_type operator()(URNG& g);
752    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
753
754    // property functions
755    result_type k() const;
756    double p() const;
757
758    param_type param() const;
759    void param(const param_type& parm);
760
761    result_type min() const;
762    result_type max() const;
763
764    friend bool operator==(const negative_binomial_distribution& x,
765                           const negative_binomial_distribution& y);
766    friend bool operator!=(const negative_binomial_distribution& x,
767                           const negative_binomial_distribution& y);
768
769    template <class charT, class traits>
770    friend
771    basic_ostream<charT, traits>&
772    operator<<(basic_ostream<charT, traits>& os,
773               const negative_binomial_distribution& x);
774
775    template <class charT, class traits>
776    friend
777    basic_istream<charT, traits>&
778    operator>>(basic_istream<charT, traits>& is,
779               negative_binomial_distribution& x);
780};
781
782template<class IntType = int>
783class poisson_distribution
784{
785public:
786    // types
787    typedef IntType result_type;
788
789    class param_type
790    {
791    public:
792        typedef poisson_distribution distribution_type;
793
794        explicit param_type(double mean = 1.0);
795
796        double mean() const;
797
798        friend bool operator==(const param_type& x, const param_type& y);
799        friend bool operator!=(const param_type& x, const param_type& y);
800    };
801
802    // constructors and reset functions
803    explicit poisson_distribution(double mean = 1.0);
804    explicit poisson_distribution(const param_type& parm);
805    void reset();
806
807    // generating functions
808    template<class URNG> result_type operator()(URNG& g);
809    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
810
811    // property functions
812    double mean() const;
813
814    param_type param() const;
815    void param(const param_type& parm);
816
817    result_type min() const;
818    result_type max() const;
819
820    friend bool operator==(const poisson_distribution& x,
821                           const poisson_distribution& y);
822    friend bool operator!=(const poisson_distribution& x,
823                           const poisson_distribution& y);
824
825    template <class charT, class traits>
826    friend
827    basic_ostream<charT, traits>&
828    operator<<(basic_ostream<charT, traits>& os,
829               const poisson_distribution& x);
830
831    template <class charT, class traits>
832    friend
833    basic_istream<charT, traits>&
834    operator>>(basic_istream<charT, traits>& is,
835               poisson_distribution& x);
836};
837
838template<class RealType = double>
839class exponential_distribution
840{
841public:
842    // types
843    typedef RealType result_type;
844
845    class param_type
846    {
847    public:
848        typedef exponential_distribution distribution_type;
849
850        explicit param_type(result_type lambda = 1.0);
851
852        result_type lambda() const;
853
854        friend bool operator==(const param_type& x, const param_type& y);
855        friend bool operator!=(const param_type& x, const param_type& y);
856    };
857
858    // constructors and reset functions
859    explicit exponential_distribution(result_type lambda = 1.0);
860    explicit exponential_distribution(const param_type& parm);
861    void reset();
862
863    // generating functions
864    template<class URNG> result_type operator()(URNG& g);
865    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
866
867    // property functions
868    result_type lambda() const;
869
870    param_type param() const;
871    void param(const param_type& parm);
872
873    result_type min() const;
874    result_type max() const;
875
876    friend bool operator==(const exponential_distribution& x,
877                           const exponential_distribution& y);
878    friend bool operator!=(const exponential_distribution& x,
879                           const exponential_distribution& y);
880
881    template <class charT, class traits>
882    friend
883    basic_ostream<charT, traits>&
884    operator<<(basic_ostream<charT, traits>& os,
885               const exponential_distribution& x);
886
887    template <class charT, class traits>
888    friend
889    basic_istream<charT, traits>&
890    operator>>(basic_istream<charT, traits>& is,
891               exponential_distribution& x);
892};
893
894template<class RealType = double>
895class gamma_distribution
896{
897public:
898    // types
899    typedef RealType result_type;
900
901    class param_type
902    {
903    public:
904        typedef gamma_distribution distribution_type;
905
906        explicit param_type(result_type alpha = 1, result_type beta = 1);
907
908        result_type alpha() const;
909        result_type beta() const;
910
911        friend bool operator==(const param_type& x, const param_type& y);
912        friend bool operator!=(const param_type& x, const param_type& y);
913    };
914
915    // constructors and reset functions
916    explicit gamma_distribution(result_type alpha = 1, result_type beta = 1);
917    explicit gamma_distribution(const param_type& parm);
918    void reset();
919
920    // generating functions
921    template<class URNG> result_type operator()(URNG& g);
922    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
923
924    // property functions
925    result_type alpha() const;
926    result_type beta() const;
927
928    param_type param() const;
929    void param(const param_type& parm);
930
931    result_type min() const;
932    result_type max() const;
933
934    friend bool operator==(const gamma_distribution& x,
935                           const gamma_distribution& y);
936    friend bool operator!=(const gamma_distribution& x,
937                           const gamma_distribution& y);
938
939    template <class charT, class traits>
940    friend
941    basic_ostream<charT, traits>&
942    operator<<(basic_ostream<charT, traits>& os,
943               const gamma_distribution& x);
944
945    template <class charT, class traits>
946    friend
947    basic_istream<charT, traits>&
948    operator>>(basic_istream<charT, traits>& is,
949               gamma_distribution& x);
950};
951
952template<class RealType = double>
953class weibull_distribution
954{
955public:
956    // types
957    typedef RealType result_type;
958
959    class param_type
960    {
961    public:
962        typedef weibull_distribution distribution_type;
963
964        explicit param_type(result_type alpha = 1, result_type beta = 1);
965
966        result_type a() const;
967        result_type b() const;
968
969        friend bool operator==(const param_type& x, const param_type& y);
970        friend bool operator!=(const param_type& x, const param_type& y);
971    };
972
973    // constructor and reset functions
974    explicit weibull_distribution(result_type a = 1, result_type b = 1);
975    explicit weibull_distribution(const param_type& parm);
976    void reset();
977
978    // generating functions
979    template<class URNG> result_type operator()(URNG& g);
980    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
981
982    // property functions
983    result_type a() const;
984    result_type b() const;
985
986    param_type param() const;
987    void param(const param_type& parm);
988
989    result_type min() const;
990    result_type max() const;
991
992    friend bool operator==(const weibull_distribution& x,
993                           const weibull_distribution& y);
994    friend bool operator!=(const weibull_distribution& x,
995                           const weibull_distribution& y);
996
997    template <class charT, class traits>
998    friend
999    basic_ostream<charT, traits>&
1000    operator<<(basic_ostream<charT, traits>& os,
1001               const weibull_distribution& x);
1002
1003    template <class charT, class traits>
1004    friend
1005    basic_istream<charT, traits>&
1006    operator>>(basic_istream<charT, traits>& is,
1007               weibull_distribution& x);
1008};
1009
1010template<class RealType = double>
1011class extreme_value_distribution
1012{
1013public:
1014    // types
1015    typedef RealType result_type;
1016
1017    class param_type
1018    {
1019    public:
1020        typedef extreme_value_distribution distribution_type;
1021
1022        explicit param_type(result_type a = 0, result_type b = 1);
1023
1024        result_type a() const;
1025        result_type b() const;
1026
1027        friend bool operator==(const param_type& x, const param_type& y);
1028        friend bool operator!=(const param_type& x, const param_type& y);
1029    };
1030
1031    // constructor and reset functions
1032    explicit extreme_value_distribution(result_type a = 0, result_type b = 1);
1033    explicit extreme_value_distribution(const param_type& parm);
1034    void reset();
1035
1036    // generating functions
1037    template<class URNG> result_type operator()(URNG& g);
1038    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1039
1040    // property functions
1041    result_type a() const;
1042    result_type b() const;
1043
1044    param_type param() const;
1045    void param(const param_type& parm);
1046
1047    result_type min() const;
1048    result_type max() const;
1049
1050    friend bool operator==(const extreme_value_distribution& x,
1051                           const extreme_value_distribution& y);
1052    friend bool operator!=(const extreme_value_distribution& x,
1053                           const extreme_value_distribution& y);
1054
1055    template <class charT, class traits>
1056    friend
1057    basic_ostream<charT, traits>&
1058    operator<<(basic_ostream<charT, traits>& os,
1059               const extreme_value_distribution& x);
1060
1061    template <class charT, class traits>
1062    friend
1063    basic_istream<charT, traits>&
1064    operator>>(basic_istream<charT, traits>& is,
1065               extreme_value_distribution& x);
1066};
1067
1068template<class RealType = double>
1069class normal_distribution
1070{
1071public:
1072    // types
1073    typedef RealType result_type;
1074
1075    class param_type
1076    {
1077    public:
1078        typedef normal_distribution distribution_type;
1079
1080        explicit param_type(result_type mean = 0, result_type stddev = 1);
1081
1082        result_type mean() const;
1083        result_type stddev() const;
1084
1085        friend bool operator==(const param_type& x, const param_type& y);
1086        friend bool operator!=(const param_type& x, const param_type& y);
1087    };
1088
1089    // constructors and reset functions
1090    explicit normal_distribution(result_type mean = 0, result_type stddev = 1);
1091    explicit normal_distribution(const param_type& parm);
1092    void reset();
1093
1094    // generating functions
1095    template<class URNG> result_type operator()(URNG& g);
1096    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1097
1098    // property functions
1099    result_type mean() const;
1100    result_type stddev() const;
1101
1102    param_type param() const;
1103    void param(const param_type& parm);
1104
1105    result_type min() const;
1106    result_type max() const;
1107
1108    friend bool operator==(const normal_distribution& x,
1109                           const normal_distribution& y);
1110    friend bool operator!=(const normal_distribution& x,
1111                           const normal_distribution& y);
1112
1113    template <class charT, class traits>
1114    friend
1115    basic_ostream<charT, traits>&
1116    operator<<(basic_ostream<charT, traits>& os,
1117               const normal_distribution& x);
1118
1119    template <class charT, class traits>
1120    friend
1121    basic_istream<charT, traits>&
1122    operator>>(basic_istream<charT, traits>& is,
1123               normal_distribution& x);
1124};
1125
1126template<class RealType = double>
1127class lognormal_distribution
1128{
1129public:
1130    // types
1131    typedef RealType result_type;
1132
1133    class param_type
1134    {
1135    public:
1136        typedef lognormal_distribution distribution_type;
1137
1138        explicit param_type(result_type m = 0, result_type s = 1);
1139
1140        result_type m() const;
1141        result_type s() const;
1142
1143        friend bool operator==(const param_type& x, const param_type& y);
1144        friend bool operator!=(const param_type& x, const param_type& y);
1145    };
1146
1147    // constructor and reset functions
1148    explicit lognormal_distribution(result_type m = 0, result_type s = 1);
1149    explicit lognormal_distribution(const param_type& parm);
1150    void reset();
1151
1152    // generating functions
1153    template<class URNG> result_type operator()(URNG& g);
1154    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1155
1156    // property functions
1157    result_type m() const;
1158    result_type s() const;
1159
1160    param_type param() const;
1161    void param(const param_type& parm);
1162
1163    result_type min() const;
1164    result_type max() const;
1165
1166    friend bool operator==(const lognormal_distribution& x,
1167                           const lognormal_distribution& y);
1168    friend bool operator!=(const lognormal_distribution& x,
1169                           const lognormal_distribution& y);
1170
1171    template <class charT, class traits>
1172    friend
1173    basic_ostream<charT, traits>&
1174    operator<<(basic_ostream<charT, traits>& os,
1175               const lognormal_distribution& x);
1176
1177    template <class charT, class traits>
1178    friend
1179    basic_istream<charT, traits>&
1180    operator>>(basic_istream<charT, traits>& is,
1181               lognormal_distribution& x);
1182};
1183
1184template<class RealType = double>
1185class chi_squared_distribution
1186{
1187public:
1188    // types
1189    typedef RealType result_type;
1190
1191    class param_type
1192    {
1193    public:
1194        typedef chi_squared_distribution distribution_type;
1195
1196        explicit param_type(result_type n = 1);
1197
1198        result_type n() const;
1199
1200        friend bool operator==(const param_type& x, const param_type& y);
1201        friend bool operator!=(const param_type& x, const param_type& y);
1202    };
1203
1204    // constructor and reset functions
1205    explicit chi_squared_distribution(result_type n = 1);
1206    explicit chi_squared_distribution(const param_type& parm);
1207    void reset();
1208
1209    // generating functions
1210    template<class URNG> result_type operator()(URNG& g);
1211    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1212
1213    // property functions
1214    result_type n() const;
1215
1216    param_type param() const;
1217    void param(const param_type& parm);
1218
1219    result_type min() const;
1220    result_type max() const;
1221
1222    friend bool operator==(const chi_squared_distribution& x,
1223                           const chi_squared_distribution& y);
1224    friend bool operator!=(const chi_squared_distribution& x,
1225                           const chi_squared_distribution& y);
1226
1227    template <class charT, class traits>
1228    friend
1229    basic_ostream<charT, traits>&
1230    operator<<(basic_ostream<charT, traits>& os,
1231               const chi_squared_distribution& x);
1232
1233    template <class charT, class traits>
1234    friend
1235    basic_istream<charT, traits>&
1236    operator>>(basic_istream<charT, traits>& is,
1237               chi_squared_distribution& x);
1238};
1239
1240template<class RealType = double>
1241class cauchy_distribution
1242{
1243public:
1244    // types
1245    typedef RealType result_type;
1246
1247    class param_type
1248    {
1249    public:
1250        typedef cauchy_distribution distribution_type;
1251
1252        explicit param_type(result_type a = 0, result_type b = 1);
1253
1254        result_type a() const;
1255        result_type b() const;
1256
1257        friend bool operator==(const param_type& x, const param_type& y);
1258        friend bool operator!=(const param_type& x, const param_type& y);
1259    };
1260
1261    // constructor and reset functions
1262    explicit cauchy_distribution(result_type a = 0, result_type b = 1);
1263    explicit cauchy_distribution(const param_type& parm);
1264    void reset();
1265
1266    // generating functions
1267    template<class URNG> result_type operator()(URNG& g);
1268    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1269
1270    // property functions
1271    result_type a() const;
1272    result_type b() const;
1273
1274    param_type param() const;
1275    void param(const param_type& parm);
1276
1277    result_type min() const;
1278    result_type max() const;
1279
1280    friend bool operator==(const cauchy_distribution& x,
1281                           const cauchy_distribution& y);
1282    friend bool operator!=(const cauchy_distribution& x,
1283                           const cauchy_distribution& y);
1284
1285    template <class charT, class traits>
1286    friend
1287    basic_ostream<charT, traits>&
1288    operator<<(basic_ostream<charT, traits>& os,
1289               const cauchy_distribution& x);
1290
1291    template <class charT, class traits>
1292    friend
1293    basic_istream<charT, traits>&
1294    operator>>(basic_istream<charT, traits>& is,
1295               cauchy_distribution& x);
1296};
1297
1298template<class RealType = double>
1299class fisher_f_distribution
1300{
1301public:
1302    // types
1303    typedef RealType result_type;
1304
1305    class param_type
1306    {
1307    public:
1308        typedef fisher_f_distribution distribution_type;
1309
1310        explicit param_type(result_type m = 1, result_type n = 1);
1311
1312        result_type m() const;
1313        result_type n() const;
1314
1315        friend bool operator==(const param_type& x, const param_type& y);
1316        friend bool operator!=(const param_type& x, const param_type& y);
1317    };
1318
1319    // constructor and reset functions
1320    explicit fisher_f_distribution(result_type m = 1, result_type n = 1);
1321    explicit fisher_f_distribution(const param_type& parm);
1322    void reset();
1323
1324    // generating functions
1325    template<class URNG> result_type operator()(URNG& g);
1326    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1327
1328    // property functions
1329    result_type m() const;
1330    result_type n() const;
1331
1332    param_type param() const;
1333    void param(const param_type& parm);
1334
1335    result_type min() const;
1336    result_type max() const;
1337
1338    friend bool operator==(const fisher_f_distribution& x,
1339                           const fisher_f_distribution& y);
1340    friend bool operator!=(const fisher_f_distribution& x,
1341                           const fisher_f_distribution& y);
1342
1343    template <class charT, class traits>
1344    friend
1345    basic_ostream<charT, traits>&
1346    operator<<(basic_ostream<charT, traits>& os,
1347               const fisher_f_distribution& x);
1348
1349    template <class charT, class traits>
1350    friend
1351    basic_istream<charT, traits>&
1352    operator>>(basic_istream<charT, traits>& is,
1353               fisher_f_distribution& x);
1354};
1355
1356template<class RealType = double>
1357class student_t_distribution
1358{
1359public:
1360    // types
1361    typedef RealType result_type;
1362
1363    class param_type
1364    {
1365    public:
1366        typedef student_t_distribution distribution_type;
1367
1368        explicit param_type(result_type n = 1);
1369
1370        result_type n() const;
1371
1372        friend bool operator==(const param_type& x, const param_type& y);
1373        friend bool operator!=(const param_type& x, const param_type& y);
1374    };
1375
1376    // constructor and reset functions
1377    explicit student_t_distribution(result_type n = 1);
1378    explicit student_t_distribution(const param_type& parm);
1379    void reset();
1380
1381    // generating functions
1382    template<class URNG> result_type operator()(URNG& g);
1383    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1384
1385    // property functions
1386    result_type n() const;
1387
1388    param_type param() const;
1389    void param(const param_type& parm);
1390
1391    result_type min() const;
1392    result_type max() const;
1393
1394    friend bool operator==(const student_t_distribution& x,
1395                           const student_t_distribution& y);
1396    friend bool operator!=(const student_t_distribution& x,
1397                           const student_t_distribution& y);
1398
1399    template <class charT, class traits>
1400    friend
1401    basic_ostream<charT, traits>&
1402    operator<<(basic_ostream<charT, traits>& os,
1403               const student_t_distribution& x);
1404
1405    template <class charT, class traits>
1406    friend
1407    basic_istream<charT, traits>&
1408    operator>>(basic_istream<charT, traits>& is,
1409               student_t_distribution& x);
1410};
1411
1412template<class IntType = int>
1413class discrete_distribution
1414{
1415public:
1416    // types
1417    typedef IntType result_type;
1418
1419    class param_type
1420    {
1421    public:
1422        typedef discrete_distribution distribution_type;
1423
1424        param_type();
1425        template<class InputIterator>
1426            param_type(InputIterator firstW, InputIterator lastW);
1427        param_type(initializer_list<double> wl);
1428        template<class UnaryOperation>
1429            param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
1430
1431        vector<double> probabilities() const;
1432
1433        friend bool operator==(const param_type& x, const param_type& y);
1434        friend bool operator!=(const param_type& x, const param_type& y);
1435    };
1436
1437    // constructor and reset functions
1438    discrete_distribution();
1439    template<class InputIterator>
1440        discrete_distribution(InputIterator firstW, InputIterator lastW);
1441    discrete_distribution(initializer_list<double> wl);
1442    template<class UnaryOperation>
1443        discrete_distribution(size_t nw, double xmin, double xmax,
1444                              UnaryOperation fw);
1445    explicit discrete_distribution(const param_type& parm);
1446    void reset();
1447
1448    // generating functions
1449    template<class URNG> result_type operator()(URNG& g);
1450    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1451
1452    // property functions
1453    vector<double> probabilities() const;
1454
1455    param_type param() const;
1456    void param(const param_type& parm);
1457
1458    result_type min() const;
1459    result_type max() const;
1460
1461    friend bool operator==(const discrete_distribution& x,
1462                           const discrete_distribution& y);
1463    friend bool operator!=(const discrete_distribution& x,
1464                           const discrete_distribution& y);
1465
1466    template <class charT, class traits>
1467    friend
1468    basic_ostream<charT, traits>&
1469    operator<<(basic_ostream<charT, traits>& os,
1470               const discrete_distribution& x);
1471
1472    template <class charT, class traits>
1473    friend
1474    basic_istream<charT, traits>&
1475    operator>>(basic_istream<charT, traits>& is,
1476               discrete_distribution& x);
1477};
1478
1479template<class RealType = double>
1480class piecewise_constant_distribution
1481{
1482    // types
1483    typedef RealType result_type;
1484
1485    class param_type
1486    {
1487    public:
1488        typedef piecewise_constant_distribution distribution_type;
1489
1490        param_type();
1491        template<class InputIteratorB, class InputIteratorW>
1492            param_type(InputIteratorB firstB, InputIteratorB lastB,
1493                       InputIteratorW firstW);
1494        template<class UnaryOperation>
1495            param_type(initializer_list<result_type> bl, UnaryOperation fw);
1496        template<class UnaryOperation>
1497            param_type(size_t nw, result_type xmin, result_type xmax,
1498                       UnaryOperation fw);
1499
1500        vector<result_type> intervals() const;
1501        vector<result_type> densities() const;
1502
1503        friend bool operator==(const param_type& x, const param_type& y);
1504        friend bool operator!=(const param_type& x, const param_type& y);
1505    };
1506
1507    // constructor and reset functions
1508    piecewise_constant_distribution();
1509    template<class InputIteratorB, class InputIteratorW>
1510        piecewise_constant_distribution(InputIteratorB firstB,
1511                                        InputIteratorB lastB,
1512                                        InputIteratorW firstW);
1513    template<class UnaryOperation>
1514        piecewise_constant_distribution(initializer_list<result_type> bl,
1515                                        UnaryOperation fw);
1516    template<class UnaryOperation>
1517        piecewise_constant_distribution(size_t nw, result_type xmin,
1518                                        result_type xmax, UnaryOperation fw);
1519    explicit piecewise_constant_distribution(const param_type& parm);
1520    void reset();
1521
1522    // generating functions
1523    template<class URNG> result_type operator()(URNG& g);
1524    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1525
1526    // property functions
1527    vector<result_type> intervals() const;
1528    vector<result_type> densities() const;
1529
1530    param_type param() const;
1531    void param(const param_type& parm);
1532
1533    result_type min() const;
1534    result_type max() const;
1535
1536    friend bool operator==(const piecewise_constant_distribution& x,
1537                           const piecewise_constant_distribution& y);
1538    friend bool operator!=(const piecewise_constant_distribution& x,
1539                           const piecewise_constant_distribution& y);
1540
1541    template <class charT, class traits>
1542    friend
1543    basic_ostream<charT, traits>&
1544    operator<<(basic_ostream<charT, traits>& os,
1545               const piecewise_constant_distribution& x);
1546
1547    template <class charT, class traits>
1548    friend
1549    basic_istream<charT, traits>&
1550    operator>>(basic_istream<charT, traits>& is,
1551               piecewise_constant_distribution& x);
1552};
1553
1554template<class RealType = double>
1555class piecewise_linear_distribution
1556{
1557    // types
1558    typedef RealType result_type;
1559
1560    class param_type
1561    {
1562    public:
1563        typedef piecewise_linear_distribution distribution_type;
1564
1565        param_type();
1566        template<class InputIteratorB, class InputIteratorW>
1567            param_type(InputIteratorB firstB, InputIteratorB lastB,
1568                       InputIteratorW firstW);
1569        template<class UnaryOperation>
1570            param_type(initializer_list<result_type> bl, UnaryOperation fw);
1571        template<class UnaryOperation>
1572            param_type(size_t nw, result_type xmin, result_type xmax,
1573                       UnaryOperation fw);
1574
1575        vector<result_type> intervals() const;
1576        vector<result_type> densities() const;
1577
1578        friend bool operator==(const param_type& x, const param_type& y);
1579        friend bool operator!=(const param_type& x, const param_type& y);
1580    };
1581
1582    // constructor and reset functions
1583    piecewise_linear_distribution();
1584    template<class InputIteratorB, class InputIteratorW>
1585        piecewise_linear_distribution(InputIteratorB firstB,
1586                                      InputIteratorB lastB,
1587                                      InputIteratorW firstW);
1588
1589    template<class UnaryOperation>
1590        piecewise_linear_distribution(initializer_list<result_type> bl,
1591                                      UnaryOperation fw);
1592
1593    template<class UnaryOperation>
1594        piecewise_linear_distribution(size_t nw, result_type xmin,
1595                                      result_type xmax, UnaryOperation fw);
1596
1597    explicit piecewise_linear_distribution(const param_type& parm);
1598    void reset();
1599
1600    // generating functions
1601    template<class URNG> result_type operator()(URNG& g);
1602    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
1603
1604    // property functions
1605    vector<result_type> intervals() const;
1606    vector<result_type> densities() const;
1607
1608    param_type param() const;
1609    void param(const param_type& parm);
1610
1611    result_type min() const;
1612    result_type max() const;
1613
1614    friend bool operator==(const piecewise_linear_distribution& x,
1615                           const piecewise_linear_distribution& y);
1616    friend bool operator!=(const piecewise_linear_distribution& x,
1617                           const piecewise_linear_distribution& y);
1618
1619    template <class charT, class traits>
1620    friend
1621    basic_ostream<charT, traits>&
1622    operator<<(basic_ostream<charT, traits>& os,
1623               const piecewise_linear_distribution& x);
1624
1625    template <class charT, class traits>
1626    friend
1627    basic_istream<charT, traits>&
1628    operator>>(basic_istream<charT, traits>& is,
1629               piecewise_linear_distribution& x);
1630};
1631
1632} // std
1633*/
1634
1635#include <__config>
1636#include <cstddef>
1637#include <type_traits>
1638#include <initializer_list>
1639#include <cstdint>
1640#include <limits>
1641#include <algorithm>
1642#include <numeric>
1643#include <vector>
1644#include <string>
1645#include <istream>
1646#include <ostream>
1647#include <cmath>
1648
1649#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1650#pragma GCC system_header
1651#endif
1652
1653_LIBCPP_BEGIN_NAMESPACE_STD
1654
1655// __is_seed_sequence
1656
1657template <class _Sseq, class _Engine>
1658struct __is_seed_sequence
1659{
1660    static const bool value =
1661              !is_convertible<_Sseq, typename _Engine::result_type>::value &&
1662              !is_same<typename remove_cv<_Sseq>::type, _Engine>::value;
1663};
1664
1665// linear_congruential_engine
1666
1667template <unsigned long long __a, unsigned long long __c,
1668          unsigned long long __m, unsigned long long _M,
1669          bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_M-__c)/__a)>
1670struct __lce_ta;
1671
1672// 64
1673
1674template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1675struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
1676{
1677    typedef unsigned long long result_type;
1678    _LIBCPP_INLINE_VISIBILITY
1679    static result_type next(result_type __x)
1680    {
1681        // Schrage's algorithm
1682        const result_type __q = __m / __a;
1683        const result_type __r = __m % __a;
1684        const result_type __t0 = __a * (__x % __q);
1685        const result_type __t1 = __r * (__x / __q);
1686        __x = __t0 + (__t0 < __t1) * __m - __t1;
1687        __x += __c - (__x >= __m - __c) * __m;
1688        return __x;
1689    }
1690};
1691
1692template <unsigned long long __a, unsigned long long __m>
1693struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
1694{
1695    typedef unsigned long long result_type;
1696    _LIBCPP_INLINE_VISIBILITY
1697    static result_type next(result_type __x)
1698    {
1699        // Schrage's algorithm
1700        const result_type __q = __m / __a;
1701        const result_type __r = __m % __a;
1702        const result_type __t0 = __a * (__x % __q);
1703        const result_type __t1 = __r * (__x / __q);
1704        __x = __t0 + (__t0 < __t1) * __m - __t1;
1705        return __x;
1706    }
1707};
1708
1709template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
1710struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
1711{
1712    typedef unsigned long long result_type;
1713    _LIBCPP_INLINE_VISIBILITY
1714    static result_type next(result_type __x)
1715    {
1716        return (__a * __x + __c) % __m;
1717    }
1718};
1719
1720template <unsigned long long __a, unsigned long long __c>
1721struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
1722{
1723    typedef unsigned long long result_type;
1724    _LIBCPP_INLINE_VISIBILITY
1725    static result_type next(result_type __x)
1726    {
1727        return __a * __x + __c;
1728    }
1729};
1730
1731// 32
1732
1733template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1734struct __lce_ta<_A, _C, _M, unsigned(~0), true>
1735{
1736    typedef unsigned result_type;
1737    _LIBCPP_INLINE_VISIBILITY
1738    static result_type next(result_type __x)
1739    {
1740        const result_type __a = static_cast<result_type>(_A);
1741        const result_type __c = static_cast<result_type>(_C);
1742        const result_type __m = static_cast<result_type>(_M);
1743        // Schrage's algorithm
1744        const result_type __q = __m / __a;
1745        const result_type __r = __m % __a;
1746        const result_type __t0 = __a * (__x % __q);
1747        const result_type __t1 = __r * (__x / __q);
1748        __x = __t0 + (__t0 < __t1) * __m - __t1;
1749        __x += __c - (__x >= __m - __c) * __m;
1750        return __x;
1751    }
1752};
1753
1754template <unsigned long long _A, unsigned long long _M>
1755struct __lce_ta<_A, 0, _M, unsigned(~0), true>
1756{
1757    typedef unsigned result_type;
1758    _LIBCPP_INLINE_VISIBILITY
1759    static result_type next(result_type __x)
1760    {
1761        const result_type __a = static_cast<result_type>(_A);
1762        const result_type __m = static_cast<result_type>(_M);
1763        // Schrage's algorithm
1764        const result_type __q = __m / __a;
1765        const result_type __r = __m % __a;
1766        const result_type __t0 = __a * (__x % __q);
1767        const result_type __t1 = __r * (__x / __q);
1768        __x = __t0 + (__t0 < __t1) * __m - __t1;
1769        return __x;
1770    }
1771};
1772
1773template <unsigned long long _A, unsigned long long _C, unsigned long long _M>
1774struct __lce_ta<_A, _C, _M, unsigned(~0), false>
1775{
1776    typedef unsigned result_type;
1777    _LIBCPP_INLINE_VISIBILITY
1778    static result_type next(result_type __x)
1779    {
1780        const result_type __a = static_cast<result_type>(_A);
1781        const result_type __c = static_cast<result_type>(_C);
1782        const result_type __m = static_cast<result_type>(_M);
1783        return (__a * __x + __c) % __m;
1784    }
1785};
1786
1787template <unsigned long long _A, unsigned long long _C>
1788struct __lce_ta<_A, _C, 0, unsigned(~0), false>
1789{
1790    typedef unsigned result_type;
1791    _LIBCPP_INLINE_VISIBILITY
1792    static result_type next(result_type __x)
1793    {
1794        const result_type __a = static_cast<result_type>(_A);
1795        const result_type __c = static_cast<result_type>(_C);
1796        return __a * __x + __c;
1797    }
1798};
1799
1800// 16
1801
1802template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
1803struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
1804{
1805    typedef unsigned short result_type;
1806    _LIBCPP_INLINE_VISIBILITY
1807    static result_type next(result_type __x)
1808    {
1809        return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
1810    }
1811};
1812
1813template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1814class linear_congruential_engine;
1815
1816template <class _CharT, class _Traits,
1817          class _U, _U _A, _U _C, _U _N>
1818basic_ostream<_CharT, _Traits>&
1819operator<<(basic_ostream<_CharT, _Traits>& __os,
1820           const linear_congruential_engine<_U, _A, _C, _N>&);
1821
1822template <class _CharT, class _Traits,
1823          class _U, _U _A, _U _C, _U _N>
1824basic_istream<_CharT, _Traits>&
1825operator>>(basic_istream<_CharT, _Traits>& __is,
1826           linear_congruential_engine<_U, _A, _C, _N>& __x);
1827
1828template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1829class _LIBCPP_VISIBLE linear_congruential_engine
1830{
1831public:
1832    // types
1833    typedef _UIntType result_type;
1834
1835private:
1836    result_type __x_;
1837
1838    static const result_type _M = result_type(~0);
1839
1840    static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
1841    static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
1842public:
1843    static const result_type _Min = __c == 0u ? 1u: 0u;
1844    static const result_type _Max = __m - 1u;
1845    static_assert(_Min < _Max,           "linear_congruential_engine invalid parameters");
1846
1847    // engine characteristics
1848    static const/*expr*/ result_type multiplier = __a;
1849    static const/*expr*/ result_type increment = __c;
1850    static const/*expr*/ result_type modulus = __m;
1851    _LIBCPP_INLINE_VISIBILITY
1852    static const/*expr*/ result_type min() {return _Min;}
1853    _LIBCPP_INLINE_VISIBILITY
1854    static const/*expr*/ result_type max() {return _Max;}
1855    static const/*expr*/ result_type default_seed = 1u;
1856
1857    // constructors and seeding functions
1858    _LIBCPP_INLINE_VISIBILITY
1859    explicit linear_congruential_engine(result_type __s = default_seed)
1860        {seed(__s);}
1861    template<class _Sseq> explicit linear_congruential_engine(_Sseq& __q,
1862        _LIBCPP_INLINE_VISIBILITY
1863        typename enable_if<__is_seed_sequence<_Sseq, linear_congruential_engine>::value>::type* = 0)
1864        {seed(__q);}
1865    _LIBCPP_INLINE_VISIBILITY
1866    void seed(result_type __s = default_seed)
1867        {seed(integral_constant<bool, __m == 0>(),
1868              integral_constant<bool, __c == 0>(), __s);}
1869    template<class _Sseq>
1870        _LIBCPP_INLINE_VISIBILITY
1871        typename enable_if
1872        <
1873            __is_seed_sequence<_Sseq, linear_congruential_engine>::value,
1874            void
1875        >::type
1876        seed(_Sseq& __q)
1877            {__seed(__q, integral_constant<unsigned,
1878                1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
1879                             :  (__m-1) / 0x100000000ull)>());}
1880
1881    // generating functions
1882    _LIBCPP_INLINE_VISIBILITY
1883    result_type operator()()
1884        {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _M>::next(__x_));}
1885    _LIBCPP_INLINE_VISIBILITY
1886    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
1887
1888    friend _LIBCPP_INLINE_VISIBILITY
1889    bool operator==(const linear_congruential_engine& __x,
1890                    const linear_congruential_engine& __y)
1891        {return __x.__x_ == __y.__x_;}
1892    friend _LIBCPP_INLINE_VISIBILITY
1893    bool operator!=(const linear_congruential_engine& __x,
1894                    const linear_congruential_engine& __y)
1895        {return !(__x == __y);}
1896
1897private:
1898
1899    _LIBCPP_INLINE_VISIBILITY
1900    void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
1901    _LIBCPP_INLINE_VISIBILITY
1902    void seed(true_type, false_type, result_type __s) {__x_ = __s;}
1903    _LIBCPP_INLINE_VISIBILITY
1904    void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
1905                                                                 1 : __s % __m;}
1906    _LIBCPP_INLINE_VISIBILITY
1907    void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
1908
1909    template<class _Sseq>
1910        void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
1911    template<class _Sseq>
1912        void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
1913
1914    template <class _CharT, class _Traits,
1915              class _U, _U _A, _U _C, _U _N>
1916    friend
1917    basic_ostream<_CharT, _Traits>&
1918    operator<<(basic_ostream<_CharT, _Traits>& __os,
1919               const linear_congruential_engine<_U, _A, _C, _N>&);
1920
1921    template <class _CharT, class _Traits,
1922              class _U, _U _A, _U _C, _U _N>
1923    friend
1924    basic_istream<_CharT, _Traits>&
1925    operator>>(basic_istream<_CharT, _Traits>& __is,
1926               linear_congruential_engine<_U, _A, _C, _N>& __x);
1927};
1928
1929template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1930template<class _Sseq>
1931void
1932linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1933                                                 integral_constant<unsigned, 1>)
1934{
1935    const unsigned __k = 1;
1936    uint32_t __ar[__k+3];
1937    __q.generate(__ar, __ar + __k + 3);
1938    result_type __s = static_cast<result_type>(__ar[3] % __m);
1939    __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1940}
1941
1942template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1943template<class _Sseq>
1944void
1945linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
1946                                                 integral_constant<unsigned, 2>)
1947{
1948    const unsigned __k = 2;
1949    uint32_t __ar[__k+3];
1950    __q.generate(__ar, __ar + __k + 3);
1951    result_type __s = static_cast<result_type>((__ar[3] +
1952                                                (uint64_t)__ar[4] << 32) % __m);
1953    __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
1954}
1955
1956template <class _CharT, class _Traits>
1957class __save_flags
1958{
1959    typedef basic_ios<_CharT, _Traits> __stream_type;
1960    typedef typename __stream_type::fmtflags fmtflags;
1961
1962    __stream_type& __stream_;
1963    fmtflags       __fmtflags_;
1964    _CharT         __fill_;
1965
1966    __save_flags(const __save_flags&);
1967    __save_flags& operator=(const __save_flags&);
1968public:
1969    _LIBCPP_INLINE_VISIBILITY
1970    explicit __save_flags(__stream_type& __stream)
1971        : __stream_(__stream),
1972          __fmtflags_(__stream.flags()),
1973          __fill_(__stream.fill())
1974        {}
1975    _LIBCPP_INLINE_VISIBILITY
1976    ~__save_flags()
1977    {
1978        __stream_.flags(__fmtflags_);
1979        __stream_.fill(__fill_);
1980    }
1981};
1982
1983template <class _CharT, class _Traits,
1984          class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1985inline _LIBCPP_INLINE_VISIBILITY
1986basic_ostream<_CharT, _Traits>&
1987operator<<(basic_ostream<_CharT, _Traits>& __os,
1988           const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
1989{
1990    __save_flags<_CharT, _Traits> _(__os);
1991    __os.flags(ios_base::dec | ios_base::left);
1992    __os.fill(__os.widen(' '));
1993    return __os << __x.__x_;
1994}
1995
1996template <class _CharT, class _Traits,
1997          class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
1998basic_istream<_CharT, _Traits>&
1999operator>>(basic_istream<_CharT, _Traits>& __is,
2000           linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
2001{
2002    __save_flags<_CharT, _Traits> _(__is);
2003    __is.flags(ios_base::dec | ios_base::skipws);
2004    _UIntType __t;
2005    __is >> __t;
2006    if (!__is.fail())
2007        __x.__x_ = __t;
2008    return __is;
2009}
2010
2011typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
2012                                                                   minstd_rand0;
2013typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
2014                                                                    minstd_rand;
2015typedef minstd_rand                                       default_random_engine;
2016// mersenne_twister_engine
2017
2018template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2019          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2020          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2021class mersenne_twister_engine;
2022
2023template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2024          _UI _A, size_t _U, _UI _D, size_t _S,
2025          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2026bool
2027operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2028                                         _B, _T, _C, _L, _F>& __x,
2029           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2030                                         _B, _T, _C, _L, _F>& __y);
2031
2032template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2033          _UI _A, size_t _U, _UI _D, size_t _S,
2034          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2035bool
2036operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2037                                         _B, _T, _C, _L, _F>& __x,
2038           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2039                                         _B, _T, _C, _L, _F>& __y);
2040
2041template <class _CharT, class _Traits,
2042          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2043          _UI _A, size_t _U, _UI _D, size_t _S,
2044          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2045basic_ostream<_CharT, _Traits>&
2046operator<<(basic_ostream<_CharT, _Traits>& __os,
2047           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2048                                         _B, _T, _C, _L, _F>& __x);
2049
2050template <class _CharT, class _Traits,
2051          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2052          _UI _A, size_t _U, _UI _D, size_t _S,
2053          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2054basic_istream<_CharT, _Traits>&
2055operator>>(basic_istream<_CharT, _Traits>& __is,
2056           mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2057                                   _B, _T, _C, _L, _F>& __x);
2058
2059template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2060          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2061          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2062class _LIBCPP_VISIBLE mersenne_twister_engine
2063{
2064public:
2065    // types
2066    typedef _UIntType result_type;
2067
2068private:
2069    result_type __x_[__n];
2070    size_t      __i_;
2071
2072    static_assert(  0 <  __m, "mersenne_twister_engine invalid parameters");
2073    static_assert(__m <= __n, "mersenne_twister_engine invalid parameters");
2074    static const result_type _Dt = numeric_limits<result_type>::digits;
2075    static_assert(__w <= _Dt, "mersenne_twister_engine invalid parameters");
2076    static_assert(  2 <= __w, "mersenne_twister_engine invalid parameters");
2077    static_assert(__r <= __w, "mersenne_twister_engine invalid parameters");
2078    static_assert(__u <= __w, "mersenne_twister_engine invalid parameters");
2079    static_assert(__s <= __w, "mersenne_twister_engine invalid parameters");
2080    static_assert(__t <= __w, "mersenne_twister_engine invalid parameters");
2081    static_assert(__l <= __w, "mersenne_twister_engine invalid parameters");
2082public:
2083    static const result_type _Min = 0;
2084    static const result_type _Max = __w == _Dt ? result_type(~0) :
2085                                   (result_type(1) << __w) - result_type(1);
2086    static_assert(_Min < _Max, "mersenne_twister_engine invalid parameters");
2087    static_assert(__a <= _Max, "mersenne_twister_engine invalid parameters");
2088    static_assert(__b <= _Max, "mersenne_twister_engine invalid parameters");
2089    static_assert(__c <= _Max, "mersenne_twister_engine invalid parameters");
2090    static_assert(__d <= _Max, "mersenne_twister_engine invalid parameters");
2091    static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
2092
2093    // engine characteristics
2094    static const/*expr*/ size_t word_size = __w;
2095    static const/*expr*/ size_t state_size = __n;
2096    static const/*expr*/ size_t shift_size = __m;
2097    static const/*expr*/ size_t mask_bits = __r;
2098    static const/*expr*/ result_type xor_mask = __a;
2099    static const/*expr*/ size_t tempering_u = __u;
2100    static const/*expr*/ result_type tempering_d = __d;
2101    static const/*expr*/ size_t tempering_s = __s;
2102    static const/*expr*/ result_type tempering_b = __b;
2103    static const/*expr*/ size_t tempering_t = __t;
2104    static const/*expr*/ result_type tempering_c = __c;
2105    static const/*expr*/ size_t tempering_l = __l;
2106    static const/*expr*/ result_type initialization_multiplier = __f;
2107    _LIBCPP_INLINE_VISIBILITY
2108    static const/*expr*/ result_type min() { return _Min; }
2109    _LIBCPP_INLINE_VISIBILITY
2110    static const/*expr*/ result_type max() { return _Max; }
2111    static const/*expr*/ result_type default_seed = 5489u;
2112
2113    // constructors and seeding functions
2114    _LIBCPP_INLINE_VISIBILITY
2115    explicit mersenne_twister_engine(result_type __sd = default_seed)
2116        {seed(__sd);}
2117    template<class _Sseq> explicit mersenne_twister_engine(_Sseq& __q,
2118        _LIBCPP_INLINE_VISIBILITY
2119        typename enable_if<__is_seed_sequence<_Sseq, mersenne_twister_engine>::value>::type* = 0)
2120        {seed(__q);}
2121    void seed(result_type __sd = default_seed);
2122    template<class _Sseq>
2123        _LIBCPP_INLINE_VISIBILITY
2124        typename enable_if
2125        <
2126            __is_seed_sequence<_Sseq, mersenne_twister_engine>::value,
2127            void
2128        >::type
2129        seed(_Sseq& __q)
2130            {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2131
2132    // generating functions
2133    result_type operator()();
2134    _LIBCPP_INLINE_VISIBILITY
2135    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2136
2137    template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2138              _UI _A, size_t _U, _UI _D, size_t _S,
2139              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2140    friend
2141    bool
2142    operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2143                                             _B, _T, _C, _L, _F>& __x,
2144               const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2145                                             _B, _T, _C, _L, _F>& __y);
2146
2147    template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2148              _UI _A, size_t _U, _UI _D, size_t _S,
2149              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2150    friend
2151    bool
2152    operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2153                                             _B, _T, _C, _L, _F>& __x,
2154               const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2155                                             _B, _T, _C, _L, _F>& __y);
2156
2157    template <class _CharT, class _Traits,
2158              class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2159              _UI _A, size_t _U, _UI _D, size_t _S,
2160              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2161    friend
2162    basic_ostream<_CharT, _Traits>&
2163    operator<<(basic_ostream<_CharT, _Traits>& __os,
2164               const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2165                                             _B, _T, _C, _L, _F>& __x);
2166
2167    template <class _CharT, class _Traits,
2168              class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2169              _UI _A, size_t _U, _UI _D, size_t _S,
2170              _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2171    friend
2172    basic_istream<_CharT, _Traits>&
2173    operator>>(basic_istream<_CharT, _Traits>& __is,
2174               mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2175                                       _B, _T, _C, _L, _F>& __x);
2176private:
2177
2178    template<class _Sseq>
2179        void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2180    template<class _Sseq>
2181        void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2182
2183    template <size_t __count>
2184        _LIBCPP_INLINE_VISIBILITY
2185        static
2186        typename enable_if
2187        <
2188            __count < __w,
2189            result_type
2190        >::type
2191        __lshift(result_type __x) {return (__x << __count) & _Max;}
2192
2193    template <size_t __count>
2194        _LIBCPP_INLINE_VISIBILITY
2195        static
2196        typename enable_if
2197        <
2198            (__count >= __w),
2199            result_type
2200        >::type
2201        __lshift(result_type __x) {return result_type(0);}
2202
2203    template <size_t __count>
2204        _LIBCPP_INLINE_VISIBILITY
2205        static
2206        typename enable_if
2207        <
2208            __count < _Dt,
2209            result_type
2210        >::type
2211        __rshift(result_type __x) {return __x >> __count;}
2212
2213    template <size_t __count>
2214        _LIBCPP_INLINE_VISIBILITY
2215        static
2216        typename enable_if
2217        <
2218            (__count >= _Dt),
2219            result_type
2220        >::type
2221        __rshift(result_type __x) {return result_type(0);}
2222};
2223
2224template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2225          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2226          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2227void
2228mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2229    __t, __c, __l, __f>::seed(result_type __sd)
2230{   // __w >= 2
2231    __x_[0] = __sd & _Max;
2232    for (size_t __i = 1; __i < __n; ++__i)
2233        __x_[__i] = (__f * (__x_[__i-1] ^ __rshift<__w - 2>(__x_[__i-1])) + __i) & _Max;
2234    __i_ = 0;
2235}
2236
2237template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2238          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2239          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2240template<class _Sseq>
2241void
2242mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2243    __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 1>)
2244{
2245    const unsigned __k = 1;
2246    uint32_t __ar[__n * __k];
2247    __q.generate(__ar, __ar + __n * __k);
2248    for (size_t __i = 0; __i < __n; ++__i)
2249        __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2250    const result_type __mask = __r == _Dt ? result_type(~0) :
2251                                       (result_type(1) << __r) - result_type(1);
2252    __i_ = 0;
2253    if ((__x_[0] & ~__mask) == 0)
2254    {
2255        for (size_t __i = 1; __i < __n; ++__i)
2256            if (__x_[__i] != 0)
2257                return;
2258        __x_[0] = _Max;
2259    }
2260}
2261
2262template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2263          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2264          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2265template<class _Sseq>
2266void
2267mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2268    __t, __c, __l, __f>::__seed(_Sseq& __q, integral_constant<unsigned, 2>)
2269{
2270    const unsigned __k = 2;
2271    uint32_t __ar[__n * __k];
2272    __q.generate(__ar, __ar + __n * __k);
2273    for (size_t __i = 0; __i < __n; ++__i)
2274        __x_[__i] = static_cast<result_type>(
2275            (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2276    const result_type __mask = __r == _Dt ? result_type(~0) :
2277                                       (result_type(1) << __r) - result_type(1);
2278    __i_ = 0;
2279    if ((__x_[0] & ~__mask) == 0)
2280    {
2281        for (size_t __i = 1; __i < __n; ++__i)
2282            if (__x_[__i] != 0)
2283                return;
2284        __x_[0] = _Max;
2285    }
2286}
2287
2288template <class _UIntType, size_t __w, size_t __n, size_t __m, size_t __r,
2289          _UIntType __a, size_t __u, _UIntType __d, size_t __s,
2290          _UIntType __b, size_t __t, _UIntType __c, size_t __l, _UIntType __f>
2291_UIntType
2292mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b,
2293    __t, __c, __l, __f>::operator()()
2294{
2295    const size_t __j = (__i_ + 1) % __n;
2296    const result_type __mask = __r == _Dt ? result_type(~0) :
2297                                       (result_type(1) << __r) - result_type(1);
2298    const result_type _Y = (__x_[__i_] & ~__mask) | (__x_[__j] & __mask);
2299    const size_t __k = (__i_ + __m) % __n;
2300    __x_[__i_] = __x_[__k] ^ __rshift<1>(_Y) ^ (__a * (_Y & 1));
2301    result_type __z = __x_[__i_] ^ (__rshift<__u>(__x_[__i_]) & __d);
2302    __i_ = __j;
2303    __z ^= __lshift<__s>(__z) & __b;
2304    __z ^= __lshift<__t>(__z) & __c;
2305    return __z ^ __rshift<__l>(__z);
2306}
2307
2308template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2309          _UI _A, size_t _U, _UI _D, size_t _S,
2310          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2311bool
2312operator==(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2313                                         _B, _T, _C, _L, _F>& __x,
2314           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2315                                         _B, _T, _C, _L, _F>& __y)
2316{
2317    if (__x.__i_ == __y.__i_)
2318        return _VSTD::equal(__x.__x_, __x.__x_ + _N, __y.__x_);
2319    if (__x.__i_ == 0 || __y.__i_ == 0)
2320    {
2321        size_t __j = _VSTD::min(_N - __x.__i_, _N - __y.__i_);
2322        if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2323                         __y.__x_ + __y.__i_))
2324            return false;
2325        if (__x.__i_ == 0)
2326            return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _N, __y.__x_);
2327        return _VSTD::equal(__x.__x_, __x.__x_ + (_N - __j), __y.__x_ + __j);
2328    }
2329    if (__x.__i_ < __y.__i_)
2330    {
2331        size_t __j = _N - __y.__i_;
2332        if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2333                         __y.__x_ + __y.__i_))
2334            return false;
2335        if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _N,
2336                         __y.__x_))
2337            return false;
2338        return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
2339                           __y.__x_ + (_N - (__x.__i_ + __j)));
2340    }
2341    size_t __j = _N - __x.__i_;
2342    if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2343                     __x.__x_ + __x.__i_))
2344        return false;
2345    if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _N,
2346                     __x.__x_))
2347        return false;
2348    return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
2349                       __x.__x_ + (_N - (__y.__i_ + __j)));
2350}
2351
2352template <class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2353          _UI _A, size_t _U, _UI _D, size_t _S,
2354          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2355inline _LIBCPP_INLINE_VISIBILITY
2356bool
2357operator!=(const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2358                                         _B, _T, _C, _L, _F>& __x,
2359           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2360                                         _B, _T, _C, _L, _F>& __y)
2361{
2362    return !(__x == __y);
2363}
2364
2365template <class _CharT, class _Traits,
2366          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2367          _UI _A, size_t _U, _UI _D, size_t _S,
2368          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2369basic_ostream<_CharT, _Traits>&
2370operator<<(basic_ostream<_CharT, _Traits>& __os,
2371           const mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2372                                         _B, _T, _C, _L, _F>& __x)
2373{
2374    __save_flags<_CharT, _Traits> _(__os);
2375    __os.flags(ios_base::dec | ios_base::left);
2376    _CharT __sp = __os.widen(' ');
2377    __os.fill(__sp);
2378    __os << __x.__x_[__x.__i_];
2379    for (size_t __j = __x.__i_ + 1; __j < _N; ++__j)
2380        __os << __sp << __x.__x_[__j];
2381    for (size_t __j = 0; __j < __x.__i_; ++__j)
2382        __os << __sp << __x.__x_[__j];
2383    return __os;
2384}
2385
2386template <class _CharT, class _Traits,
2387          class _UI, size_t _W, size_t _N, size_t _M, size_t _R,
2388          _UI _A, size_t _U, _UI _D, size_t _S,
2389          _UI _B, size_t _T, _UI _C, size_t _L, _UI _F>
2390basic_istream<_CharT, _Traits>&
2391operator>>(basic_istream<_CharT, _Traits>& __is,
2392           mersenne_twister_engine<_UI, _W, _N, _M, _R, _A, _U, _D, _S,
2393                                   _B, _T, _C, _L, _F>& __x)
2394{
2395    __save_flags<_CharT, _Traits> _(__is);
2396    __is.flags(ios_base::dec | ios_base::skipws);
2397    _UI __t[_N];
2398    for (size_t __i = 0; __i < _N; ++__i)
2399        __is >> __t[__i];
2400    if (!__is.fail())
2401    {
2402        for (size_t __i = 0; __i < _N; ++__i)
2403            __x.__x_[__i] = __t[__i];
2404        __x.__i_ = 0;
2405    }
2406    return __is;
2407}
2408
2409typedef mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,
2410                                0x9908b0df, 11, 0xffffffff,
2411                                7,  0x9d2c5680,
2412                                15, 0xefc60000,
2413                                18, 1812433253>                         mt19937;
2414typedef mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,
2415                                0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL,
2416                                17, 0x71d67fffeda60000ULL,
2417                                37, 0xfff7eee000000000ULL,
2418                                43, 6364136223846793005ULL>          mt19937_64;
2419
2420// subtract_with_carry_engine
2421
2422template<class _UIntType, size_t __w, size_t __s, size_t __r>
2423class subtract_with_carry_engine;
2424
2425template<class _UI, size_t _W, size_t _S, size_t _R>
2426bool
2427operator==(
2428    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2429    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2430
2431template<class _UI, size_t _W, size_t _S, size_t _R>
2432bool
2433operator!=(
2434    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2435    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2436
2437template <class _CharT, class _Traits,
2438          class _UI, size_t _W, size_t _S, size_t _R>
2439basic_ostream<_CharT, _Traits>&
2440operator<<(basic_ostream<_CharT, _Traits>& __os,
2441           const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2442
2443template <class _CharT, class _Traits,
2444          class _UI, size_t _W, size_t _S, size_t _R>
2445basic_istream<_CharT, _Traits>&
2446operator>>(basic_istream<_CharT, _Traits>& __is,
2447           subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2448
2449template<class _UIntType, size_t __w, size_t __s, size_t __r>
2450class _LIBCPP_VISIBLE subtract_with_carry_engine
2451{
2452public:
2453    // types
2454    typedef _UIntType result_type;
2455
2456private:
2457    result_type __x_[__r];
2458    result_type  __c_;
2459    size_t      __i_;
2460
2461    static const result_type _Dt = numeric_limits<result_type>::digits;
2462    static_assert(  0 <  __w, "subtract_with_carry_engine invalid parameters");
2463    static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
2464    static_assert(  0 <  __s, "subtract_with_carry_engine invalid parameters");
2465    static_assert(__s <  __r, "subtract_with_carry_engine invalid parameters");
2466public:
2467    static const result_type _Min = 0;
2468    static const result_type _Max = __w == _Dt ? result_type(~0) :
2469                                   (result_type(1) << __w) - result_type(1);
2470    static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
2471
2472    // engine characteristics
2473    static const/*expr*/ size_t word_size = __w;
2474    static const/*expr*/ size_t short_lag = __s;
2475    static const/*expr*/ size_t long_lag = __r;
2476    _LIBCPP_INLINE_VISIBILITY
2477    static const/*expr*/ result_type min() { return _Min; }
2478    _LIBCPP_INLINE_VISIBILITY
2479    static const/*expr*/ result_type max() { return _Max; }
2480    static const/*expr*/ result_type default_seed = 19780503u;
2481
2482    // constructors and seeding functions
2483    _LIBCPP_INLINE_VISIBILITY
2484    explicit subtract_with_carry_engine(result_type __sd = default_seed)
2485        {seed(__sd);}
2486    template<class _Sseq> explicit subtract_with_carry_engine(_Sseq& __q,
2487        _LIBCPP_INLINE_VISIBILITY
2488        typename enable_if<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value>::type* = 0)
2489        {seed(__q);}
2490    _LIBCPP_INLINE_VISIBILITY
2491    void seed(result_type __sd = default_seed)
2492        {seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2493    template<class _Sseq>
2494        _LIBCPP_INLINE_VISIBILITY
2495        typename enable_if
2496        <
2497            __is_seed_sequence<_Sseq, subtract_with_carry_engine>::value,
2498            void
2499        >::type
2500        seed(_Sseq& __q)
2501            {__seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());}
2502
2503    // generating functions
2504    result_type operator()();
2505    _LIBCPP_INLINE_VISIBILITY
2506    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2507
2508    template<class _UI, size_t _W, size_t _S, size_t _R>
2509    friend
2510    bool
2511    operator==(
2512        const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2513        const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2514
2515    template<class _UI, size_t _W, size_t _S, size_t _R>
2516    friend
2517    bool
2518    operator!=(
2519        const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2520        const subtract_with_carry_engine<_UI, _W, _S, _R>& __y);
2521
2522    template <class _CharT, class _Traits,
2523              class _UI, size_t _W, size_t _S, size_t _R>
2524    friend
2525    basic_ostream<_CharT, _Traits>&
2526    operator<<(basic_ostream<_CharT, _Traits>& __os,
2527               const subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2528
2529    template <class _CharT, class _Traits,
2530              class _UI, size_t _W, size_t _S, size_t _R>
2531    friend
2532    basic_istream<_CharT, _Traits>&
2533    operator>>(basic_istream<_CharT, _Traits>& __is,
2534               subtract_with_carry_engine<_UI, _W, _S, _R>& __x);
2535
2536private:
2537
2538    void seed(result_type __sd, integral_constant<unsigned, 1>);
2539    void seed(result_type __sd, integral_constant<unsigned, 2>);
2540    template<class _Sseq>
2541        void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
2542    template<class _Sseq>
2543        void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
2544};
2545
2546template<class _UIntType, size_t __w, size_t __s, size_t __r>
2547void
2548subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2549        integral_constant<unsigned, 1>)
2550{
2551    linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2552        __e(__sd == 0u ? default_seed : __sd);
2553    for (size_t __i = 0; __i < __r; ++__i)
2554        __x_[__i] = static_cast<result_type>(__e() & _Max);
2555    __c_ = __x_[__r-1] == 0;
2556    __i_ = 0;
2557}
2558
2559template<class _UIntType, size_t __w, size_t __s, size_t __r>
2560void
2561subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd,
2562        integral_constant<unsigned, 2>)
2563{
2564    linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
2565        __e(__sd == 0u ? default_seed : __sd);
2566    for (size_t __i = 0; __i < __r; ++__i)
2567    {
2568        result_type __e0 = __e();
2569        __x_[__i] = static_cast<result_type>(
2570                                    (__e0 + ((uint64_t)__e() << 32)) & _Max);
2571    }
2572    __c_ = __x_[__r-1] == 0;
2573    __i_ = 0;
2574}
2575
2576template<class _UIntType, size_t __w, size_t __s, size_t __r>
2577template<class _Sseq>
2578void
2579subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2580        integral_constant<unsigned, 1>)
2581{
2582    const unsigned __k = 1;
2583    uint32_t __ar[__r * __k];
2584    __q.generate(__ar, __ar + __r * __k);
2585    for (size_t __i = 0; __i < __r; ++__i)
2586        __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
2587    __c_ = __x_[__r-1] == 0;
2588    __i_ = 0;
2589}
2590
2591template<class _UIntType, size_t __w, size_t __s, size_t __r>
2592template<class _Sseq>
2593void
2594subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q,
2595        integral_constant<unsigned, 2>)
2596{
2597    const unsigned __k = 2;
2598    uint32_t __ar[__r * __k];
2599    __q.generate(__ar, __ar + __r * __k);
2600    for (size_t __i = 0; __i < __r; ++__i)
2601        __x_[__i] = static_cast<result_type>(
2602                  (__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
2603    __c_ = __x_[__r-1] == 0;
2604    __i_ = 0;
2605}
2606
2607template<class _UIntType, size_t __w, size_t __s, size_t __r>
2608_UIntType
2609subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()()
2610{
2611    const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
2612    result_type& __xr = __x_[__i_];
2613    result_type __new_c = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
2614    __xr = (__xs - __xr - __c_) & _Max;
2615    __c_ = __new_c;
2616    __i_ = (__i_ + 1) % __r;
2617    return __xr;
2618}
2619
2620template<class _UI, size_t _W, size_t _S, size_t _R>
2621bool
2622operator==(
2623    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2624    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2625{
2626    if (__x.__c_ != __y.__c_)
2627        return false;
2628    if (__x.__i_ == __y.__i_)
2629        return _VSTD::equal(__x.__x_, __x.__x_ + _R, __y.__x_);
2630    if (__x.__i_ == 0 || __y.__i_ == 0)
2631    {
2632        size_t __j = _VSTD::min(_R - __x.__i_, _R - __y.__i_);
2633        if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j,
2634                         __y.__x_ + __y.__i_))
2635            return false;
2636        if (__x.__i_ == 0)
2637            return _VSTD::equal(__x.__x_ + __j, __x.__x_ + _R, __y.__x_);
2638        return _VSTD::equal(__x.__x_, __x.__x_ + (_R - __j), __y.__x_ + __j);
2639    }
2640    if (__x.__i_ < __y.__i_)
2641    {
2642        size_t __j = _R - __y.__i_;
2643        if (!_VSTD::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j),
2644                         __y.__x_ + __y.__i_))
2645            return false;
2646        if (!_VSTD::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _R,
2647                         __y.__x_))
2648            return false;
2649        return _VSTD::equal(__x.__x_, __x.__x_ + __x.__i_,
2650                           __y.__x_ + (_R - (__x.__i_ + __j)));
2651    }
2652    size_t __j = _R - __x.__i_;
2653    if (!_VSTD::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j),
2654                     __x.__x_ + __x.__i_))
2655        return false;
2656    if (!_VSTD::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _R,
2657                     __x.__x_))
2658        return false;
2659    return _VSTD::equal(__y.__x_, __y.__x_ + __y.__i_,
2660                       __x.__x_ + (_R - (__y.__i_ + __j)));
2661}
2662
2663template<class _UI, size_t _W, size_t _S, size_t _R>
2664inline _LIBCPP_INLINE_VISIBILITY
2665bool
2666operator!=(
2667    const subtract_with_carry_engine<_UI, _W, _S, _R>& __x,
2668    const subtract_with_carry_engine<_UI, _W, _S, _R>& __y)
2669{
2670    return !(__x == __y);
2671}
2672
2673template <class _CharT, class _Traits,
2674          class _UI, size_t _W, size_t _S, size_t _R>
2675basic_ostream<_CharT, _Traits>&
2676operator<<(basic_ostream<_CharT, _Traits>& __os,
2677           const subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2678{
2679    __save_flags<_CharT, _Traits> _(__os);
2680    __os.flags(ios_base::dec | ios_base::left);
2681    _CharT __sp = __os.widen(' ');
2682    __os.fill(__sp);
2683    __os << __x.__x_[__x.__i_];
2684    for (size_t __j = __x.__i_ + 1; __j < _R; ++__j)
2685        __os << __sp << __x.__x_[__j];
2686    for (size_t __j = 0; __j < __x.__i_; ++__j)
2687        __os << __sp << __x.__x_[__j];
2688    __os << __sp << __x.__c_;
2689    return __os;
2690}
2691
2692template <class _CharT, class _Traits,
2693          class _UI, size_t _W, size_t _S, size_t _R>
2694basic_istream<_CharT, _Traits>&
2695operator>>(basic_istream<_CharT, _Traits>& __is,
2696           subtract_with_carry_engine<_UI, _W, _S, _R>& __x)
2697{
2698    __save_flags<_CharT, _Traits> _(__is);
2699    __is.flags(ios_base::dec | ios_base::skipws);
2700    _UI __t[_R+1];
2701    for (size_t __i = 0; __i < _R+1; ++__i)
2702        __is >> __t[__i];
2703    if (!__is.fail())
2704    {
2705        for (size_t __i = 0; __i < _R; ++__i)
2706            __x.__x_[__i] = __t[__i];
2707        __x.__c_ = __t[_R];
2708        __x.__i_ = 0;
2709    }
2710    return __is;
2711}
2712
2713typedef subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>     ranlux24_base;
2714typedef subtract_with_carry_engine<uint_fast64_t, 48,  5, 12>     ranlux48_base;
2715
2716// discard_block_engine
2717
2718template<class _Engine, size_t __p, size_t __r>
2719class _LIBCPP_VISIBLE discard_block_engine
2720{
2721    _Engine __e_;
2722    int     __n_;
2723
2724    static_assert(  0 <  __r, "discard_block_engine invalid parameters");
2725    static_assert(__r <= __p, "discard_block_engine invalid parameters");
2726public:
2727    // types
2728    typedef typename _Engine::result_type result_type;
2729
2730    // engine characteristics
2731    static const/*expr*/ size_t block_size = __p;
2732    static const/*expr*/ size_t used_block = __r;
2733
2734    // Temporary work around for lack of constexpr
2735    static const result_type _Min = _Engine::_Min;
2736    static const result_type _Max = _Engine::_Max;
2737
2738    _LIBCPP_INLINE_VISIBILITY
2739    static const/*expr*/ result_type min() { return _Engine::min(); }
2740    _LIBCPP_INLINE_VISIBILITY
2741    static const/*expr*/ result_type max() { return _Engine::max(); }
2742
2743    // constructors and seeding functions
2744    _LIBCPP_INLINE_VISIBILITY
2745    discard_block_engine() : __n_(0) {}
2746    _LIBCPP_INLINE_VISIBILITY
2747    explicit discard_block_engine(const _Engine& __e)
2748        : __e_(__e), __n_(0) {}
2749#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2750    _LIBCPP_INLINE_VISIBILITY
2751    explicit discard_block_engine(_Engine&& __e)
2752        : __e_(_VSTD::move(__e)), __n_(0) {}
2753#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2754    _LIBCPP_INLINE_VISIBILITY
2755    explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
2756    template<class _Sseq>
2757        _LIBCPP_INLINE_VISIBILITY
2758        explicit discard_block_engine(_Sseq& __q,
2759        typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
2760                           !is_convertible<_Sseq, _Engine>::value>::type* = 0)
2761        : __e_(__q), __n_(0) {}
2762    _LIBCPP_INLINE_VISIBILITY
2763    void seed() {__e_.seed(); __n_ = 0;}
2764    _LIBCPP_INLINE_VISIBILITY
2765    void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
2766    template<class _Sseq>
2767        _LIBCPP_INLINE_VISIBILITY
2768        typename enable_if
2769        <
2770            __is_seed_sequence<_Sseq, discard_block_engine>::value,
2771            void
2772        >::type
2773        seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
2774
2775    // generating functions
2776    result_type operator()();
2777    _LIBCPP_INLINE_VISIBILITY
2778    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2779
2780    // property functions
2781    _LIBCPP_INLINE_VISIBILITY
2782    const _Engine& base() const {return __e_;}
2783
2784    template<class _Eng, size_t _P, size_t _R>
2785    friend
2786    bool
2787    operator==(
2788        const discard_block_engine<_Eng, _P, _R>& __x,
2789        const discard_block_engine<_Eng, _P, _R>& __y);
2790
2791    template<class _Eng, size_t _P, size_t _R>
2792    friend
2793    bool
2794    operator!=(
2795        const discard_block_engine<_Eng, _P, _R>& __x,
2796        const discard_block_engine<_Eng, _P, _R>& __y);
2797
2798    template <class _CharT, class _Traits,
2799              class _Eng, size_t _P, size_t _R>
2800    friend
2801    basic_ostream<_CharT, _Traits>&
2802    operator<<(basic_ostream<_CharT, _Traits>& __os,
2803               const discard_block_engine<_Eng, _P, _R>& __x);
2804
2805    template <class _CharT, class _Traits,
2806              class _Eng, size_t _P, size_t _R>
2807    friend
2808    basic_istream<_CharT, _Traits>&
2809    operator>>(basic_istream<_CharT, _Traits>& __is,
2810               discard_block_engine<_Eng, _P, _R>& __x);
2811};
2812
2813template<class _Engine, size_t __p, size_t __r>
2814typename discard_block_engine<_Engine, __p, __r>::result_type
2815discard_block_engine<_Engine, __p, __r>::operator()()
2816{
2817    if (__n_ >= __r)
2818    {
2819        __e_.discard(__p - __r);
2820        __n_ = 0;
2821    }
2822    ++__n_;
2823    return __e_();
2824}
2825
2826template<class _Eng, size_t _P, size_t _R>
2827inline _LIBCPP_INLINE_VISIBILITY
2828bool
2829operator==(const discard_block_engine<_Eng, _P, _R>& __x,
2830           const discard_block_engine<_Eng, _P, _R>& __y)
2831{
2832    return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
2833}
2834
2835template<class _Eng, size_t _P, size_t _R>
2836inline _LIBCPP_INLINE_VISIBILITY
2837bool
2838operator!=(const discard_block_engine<_Eng, _P, _R>& __x,
2839           const discard_block_engine<_Eng, _P, _R>& __y)
2840{
2841    return !(__x == __y);
2842}
2843
2844template <class _CharT, class _Traits,
2845          class _Eng, size_t _P, size_t _R>
2846basic_ostream<_CharT, _Traits>&
2847operator<<(basic_ostream<_CharT, _Traits>& __os,
2848           const discard_block_engine<_Eng, _P, _R>& __x)
2849{
2850    __save_flags<_CharT, _Traits> _(__os);
2851    __os.flags(ios_base::dec | ios_base::left);
2852    _CharT __sp = __os.widen(' ');
2853    __os.fill(__sp);
2854    return __os << __x.__e_ << __sp << __x.__n_;
2855}
2856
2857template <class _CharT, class _Traits,
2858          class _Eng, size_t _P, size_t _R>
2859basic_istream<_CharT, _Traits>&
2860operator>>(basic_istream<_CharT, _Traits>& __is,
2861           discard_block_engine<_Eng, _P, _R>& __x)
2862{
2863    __save_flags<_CharT, _Traits> _(__is);
2864    __is.flags(ios_base::dec | ios_base::skipws);
2865    _Eng __e;
2866    int __n;
2867    __is >> __e >> __n;
2868    if (!__is.fail())
2869    {
2870        __x.__e_ = __e;
2871        __x.__n_ = __n;
2872    }
2873    return __is;
2874}
2875
2876typedef discard_block_engine<ranlux24_base, 223, 23> ranlux24;
2877typedef discard_block_engine<ranlux48_base, 389, 11> ranlux48;
2878
2879// independent_bits_engine
2880
2881template<class _Engine, size_t __w, class _UIntType>
2882class _LIBCPP_VISIBLE independent_bits_engine
2883{
2884    template <class _UI, _UI _R0, size_t _W, size_t _M>
2885    class __get_n
2886    {
2887        static const size_t _Dt = numeric_limits<_UI>::digits;
2888        static const size_t _N = _W / _M + (_W % _M != 0);
2889        static const size_t _W0 = _W / _N;
2890        static const _UI _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
2891    public:
2892        static const size_t value = _R0 - _Y0 > _Y0 / _N ? _N + 1 : _N;
2893    };
2894public:
2895    // types
2896    typedef _UIntType result_type;
2897
2898private:
2899    _Engine __e_;
2900
2901    static const result_type _Dt = numeric_limits<result_type>::digits;
2902    static_assert(  0 <  __w, "independent_bits_engine invalid parameters");
2903    static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
2904
2905    typedef typename _Engine::result_type _Engine_result_type;
2906    typedef typename conditional
2907        <
2908            sizeof(_Engine_result_type) <= sizeof(result_type),
2909                result_type,
2910                _Engine_result_type
2911        >::type _Working_result_type;
2912    // Temporary work around for lack of constexpr
2913    static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2914                                                         + _Working_result_type(1);
2915    static const size_t __m = __log2<_Working_result_type, _R>::value;
2916    static const size_t __n = __get_n<_Working_result_type, _R, __w, __m>::value;
2917    static const size_t __w0 = __w / __n;
2918    static const size_t __n0 = __n - __w % __n;
2919    static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2920    static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2921    static const _Working_result_type __y0 = __w0 >= _WDt ? 0 :
2922                                                   (_R >> __w0) << __w0;
2923    static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 :
2924                                                   (_R >> (__w0+1)) << (__w0+1);
2925    static const _Engine_result_type __mask0 = __w0 > 0 ?
2926                                _Engine_result_type(~0) >> (_EDt - __w0) :
2927                                _Engine_result_type(0);
2928    static const _Engine_result_type __mask1 = __w0 < _EDt - 1 ?
2929                                _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) :
2930                                _Engine_result_type(~0);
2931public:
2932    static const result_type _Min = 0;
2933    static const result_type _Max = __w == _Dt ? result_type(~0) :
2934                                   (result_type(1) << __w) - result_type(1);
2935    static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
2936
2937    // engine characteristics
2938    _LIBCPP_INLINE_VISIBILITY
2939    static const/*expr*/ result_type min() { return _Min; }
2940    _LIBCPP_INLINE_VISIBILITY
2941    static const/*expr*/ result_type max() { return _Max; }
2942
2943    // constructors and seeding functions
2944    _LIBCPP_INLINE_VISIBILITY
2945    independent_bits_engine() {}
2946    _LIBCPP_INLINE_VISIBILITY
2947    explicit independent_bits_engine(const _Engine& __e)
2948        : __e_(__e) {}
2949#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2950    _LIBCPP_INLINE_VISIBILITY
2951    explicit independent_bits_engine(_Engine&& __e)
2952        : __e_(_VSTD::move(__e)) {}
2953#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2954    _LIBCPP_INLINE_VISIBILITY
2955    explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
2956    template<class _Sseq> explicit independent_bits_engine(_Sseq& __q,
2957        _LIBCPP_INLINE_VISIBILITY
2958        typename enable_if<__is_seed_sequence<_Sseq, independent_bits_engine>::value &&
2959                           !is_convertible<_Sseq, _Engine>::value>::type* = 0)
2960         : __e_(__q) {}
2961    _LIBCPP_INLINE_VISIBILITY
2962    void seed() {__e_.seed();}
2963    _LIBCPP_INLINE_VISIBILITY
2964    void seed(result_type __sd) {__e_.seed(__sd);}
2965    template<class _Sseq>
2966        _LIBCPP_INLINE_VISIBILITY
2967        typename enable_if
2968        <
2969            __is_seed_sequence<_Sseq, independent_bits_engine>::value,
2970            void
2971        >::type
2972        seed(_Sseq& __q) {__e_.seed(__q);}
2973
2974    // generating functions
2975    _LIBCPP_INLINE_VISIBILITY
2976    result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2977    _LIBCPP_INLINE_VISIBILITY
2978    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
2979
2980    // property functions
2981    _LIBCPP_INLINE_VISIBILITY
2982    const _Engine& base() const {return __e_;}
2983
2984    template<class _Eng, size_t _W, class _UI>
2985    friend
2986    bool
2987    operator==(
2988        const independent_bits_engine<_Eng, _W, _UI>& __x,
2989        const independent_bits_engine<_Eng, _W, _UI>& __y);
2990
2991    template<class _Eng, size_t _W, class _UI>
2992    friend
2993    bool
2994    operator!=(
2995        const independent_bits_engine<_Eng, _W, _UI>& __x,
2996        const independent_bits_engine<_Eng, _W, _UI>& __y);
2997
2998    template <class _CharT, class _Traits,
2999              class _Eng, size_t _W, class _UI>
3000    friend
3001    basic_ostream<_CharT, _Traits>&
3002    operator<<(basic_ostream<_CharT, _Traits>& __os,
3003               const independent_bits_engine<_Eng, _W, _UI>& __x);
3004
3005    template <class _CharT, class _Traits,
3006              class _Eng, size_t _W, class _UI>
3007    friend
3008    basic_istream<_CharT, _Traits>&
3009    operator>>(basic_istream<_CharT, _Traits>& __is,
3010               independent_bits_engine<_Eng, _W, _UI>& __x);
3011
3012private:
3013    result_type __eval(false_type);
3014    result_type __eval(true_type);
3015
3016    template <size_t __count>
3017        _LIBCPP_INLINE_VISIBILITY
3018        static
3019        typename enable_if
3020        <
3021            __count < _Dt,
3022            result_type
3023        >::type
3024        __lshift(result_type __x) {return __x << __count;}
3025
3026    template <size_t __count>
3027        _LIBCPP_INLINE_VISIBILITY
3028        static
3029        typename enable_if
3030        <
3031            (__count >= _Dt),
3032            result_type
3033        >::type
3034        __lshift(result_type __x) {return result_type(0);}
3035};
3036
3037template<class _Engine, size_t __w, class _UIntType>
3038inline _LIBCPP_INLINE_VISIBILITY
3039_UIntType
3040independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type)
3041{
3042    return static_cast<result_type>(__e_() & __mask0);
3043}
3044
3045template<class _Engine, size_t __w, class _UIntType>
3046_UIntType
3047independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type)
3048{
3049    result_type _S = 0;
3050    for (size_t __k = 0; __k < __n0; ++__k)
3051    {
3052        _Engine_result_type __u;
3053        do
3054        {
3055            __u = __e_() - _Engine::min();
3056        } while (__u >= __y0);
3057        _S = static_cast<result_type>(__lshift<__w0>(_S) + (__u & __mask0));
3058    }
3059    for (size_t __k = __n0; __k < __n; ++__k)
3060    {
3061        _Engine_result_type __u;
3062        do
3063        {
3064            __u = __e_() - _Engine::min();
3065        } while (__u >= __y1);
3066        _S = static_cast<result_type>(__lshift<__w0+1>(_S) + (__u & __mask1));
3067    }
3068    return _S;
3069}
3070
3071template<class _Eng, size_t _W, class _UI>
3072inline _LIBCPP_INLINE_VISIBILITY
3073bool
3074operator==(
3075    const independent_bits_engine<_Eng, _W, _UI>& __x,
3076    const independent_bits_engine<_Eng, _W, _UI>& __y)
3077{
3078    return __x.base() == __y.base();
3079}
3080
3081template<class _Eng, size_t _W, class _UI>
3082inline _LIBCPP_INLINE_VISIBILITY
3083bool
3084operator!=(
3085    const independent_bits_engine<_Eng, _W, _UI>& __x,
3086    const independent_bits_engine<_Eng, _W, _UI>& __y)
3087{
3088    return !(__x == __y);
3089}
3090
3091template <class _CharT, class _Traits,
3092          class _Eng, size_t _W, class _UI>
3093basic_ostream<_CharT, _Traits>&
3094operator<<(basic_ostream<_CharT, _Traits>& __os,
3095           const independent_bits_engine<_Eng, _W, _UI>& __x)
3096{
3097    return __os << __x.base();
3098}
3099
3100template <class _CharT, class _Traits,
3101          class _Eng, size_t _W, class _UI>
3102basic_istream<_CharT, _Traits>&
3103operator>>(basic_istream<_CharT, _Traits>& __is,
3104           independent_bits_engine<_Eng, _W, _UI>& __x)
3105{
3106    _Eng __e;
3107    __is >> __e;
3108    if (!__is.fail())
3109        __x.__e_ = __e;
3110    return __is;
3111}
3112
3113// shuffle_order_engine
3114
3115template <uint64_t _Xp, uint64_t _Yp>
3116struct __ugcd
3117{
3118    static const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
3119};
3120
3121template <uint64_t _Xp>
3122struct __ugcd<_Xp, 0>
3123{
3124    static const uint64_t value = _Xp;
3125};
3126
3127template <uint64_t _N, uint64_t _D>
3128class __uratio
3129{
3130    static_assert(_D != 0, "__uratio divide by 0");
3131    static const uint64_t __gcd = __ugcd<_N, _D>::value;
3132public:
3133    static const uint64_t num = _N / __gcd;
3134    static const uint64_t den = _D / __gcd;
3135
3136    typedef __uratio<num, den> type;
3137};
3138
3139template<class _Engine, size_t __k>
3140class _LIBCPP_VISIBLE shuffle_order_engine
3141{
3142    static_assert(0 < __k, "shuffle_order_engine invalid parameters");
3143public:
3144    // types
3145    typedef typename _Engine::result_type result_type;
3146
3147private:
3148    _Engine __e_;
3149    result_type _V_[__k];
3150    result_type _Y_;
3151
3152public:
3153    // engine characteristics
3154    static const/*expr*/ size_t table_size = __k;
3155
3156    static const result_type _Min = _Engine::_Min;
3157    static const result_type _Max = _Engine::_Max;
3158    static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
3159    _LIBCPP_INLINE_VISIBILITY
3160    static const/*expr*/ result_type min() { return _Min; }
3161    _LIBCPP_INLINE_VISIBILITY
3162    static const/*expr*/ result_type max() { return _Max; }
3163
3164    static const unsigned long long _R = _Max - _Min + 1ull;
3165
3166    // constructors and seeding functions
3167    _LIBCPP_INLINE_VISIBILITY
3168    shuffle_order_engine() {__init();}
3169    _LIBCPP_INLINE_VISIBILITY
3170    explicit shuffle_order_engine(const _Engine& __e)
3171        : __e_(__e) {__init();}
3172#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3173    _LIBCPP_INLINE_VISIBILITY
3174    explicit shuffle_order_engine(_Engine&& __e)
3175        : __e_(_VSTD::move(__e)) {__init();}
3176#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3177    _LIBCPP_INLINE_VISIBILITY
3178    explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
3179    template<class _Sseq> explicit shuffle_order_engine(_Sseq& __q,
3180        _LIBCPP_INLINE_VISIBILITY
3181        typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
3182                           !is_convertible<_Sseq, _Engine>::value>::type* = 0)
3183         : __e_(__q) {__init();}
3184    _LIBCPP_INLINE_VISIBILITY
3185    void seed() {__e_.seed(); __init();}
3186    _LIBCPP_INLINE_VISIBILITY
3187    void seed(result_type __sd) {__e_.seed(__sd); __init();}
3188    template<class _Sseq>
3189        _LIBCPP_INLINE_VISIBILITY
3190        typename enable_if
3191        <
3192            __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
3193            void
3194        >::type
3195        seed(_Sseq& __q) {__e_.seed(__q); __init();}
3196
3197    // generating functions
3198    _LIBCPP_INLINE_VISIBILITY
3199    result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
3200    _LIBCPP_INLINE_VISIBILITY
3201    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
3202
3203    // property functions
3204    _LIBCPP_INLINE_VISIBILITY
3205    const _Engine& base() const {return __e_;}
3206
3207private:
3208    template<class _Eng, size_t _K>
3209    friend
3210    bool
3211    operator==(
3212        const shuffle_order_engine<_Eng, _K>& __x,
3213        const shuffle_order_engine<_Eng, _K>& __y);
3214
3215    template<class _Eng, size_t _K>
3216    friend
3217    bool
3218    operator!=(
3219        const shuffle_order_engine<_Eng, _K>& __x,
3220        const shuffle_order_engine<_Eng, _K>& __y);
3221
3222    template <class _CharT, class _Traits,
3223              class _Eng, size_t _K>
3224    friend
3225    basic_ostream<_CharT, _Traits>&
3226    operator<<(basic_ostream<_CharT, _Traits>& __os,
3227               const shuffle_order_engine<_Eng, _K>& __x);
3228
3229    template <class _CharT, class _Traits,
3230              class _Eng, size_t _K>
3231    friend
3232    basic_istream<_CharT, _Traits>&
3233    operator>>(basic_istream<_CharT, _Traits>& __is,
3234               shuffle_order_engine<_Eng, _K>& __x);
3235
3236    _LIBCPP_INLINE_VISIBILITY
3237    void __init()
3238    {
3239        for (size_t __i = 0; __i < __k; ++__i)
3240            _V_[__i] = __e_();
3241        _Y_ = __e_();
3242    }
3243
3244    _LIBCPP_INLINE_VISIBILITY
3245    result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
3246    _LIBCPP_INLINE_VISIBILITY
3247    result_type __eval(true_type) {return __eval(__uratio<__k, _R>());}
3248
3249    _LIBCPP_INLINE_VISIBILITY
3250    result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
3251    _LIBCPP_INLINE_VISIBILITY
3252    result_type __eval2(true_type) {return __evalf<__k, 0>();}
3253
3254    template <uint64_t _N, uint64_t _D>
3255        _LIBCPP_INLINE_VISIBILITY
3256        typename enable_if
3257        <
3258            (__uratio<_N, _D>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
3259            result_type
3260        >::type
3261        __eval(__uratio<_N, _D>)
3262            {return __evalf<__uratio<_N, _D>::num, __uratio<_N, _D>::den>();}
3263
3264    template <uint64_t _N, uint64_t _D>
3265        _LIBCPP_INLINE_VISIBILITY
3266        typename enable_if
3267        <
3268            __uratio<_N, _D>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
3269            result_type
3270        >::type
3271        __eval(__uratio<_N, _D>)
3272        {
3273            const size_t __j = static_cast<size_t>(__uratio<_N, _D>::num * (_Y_ - _Min)
3274                                                   / __uratio<_N, _D>::den);
3275            _Y_ = _V_[__j];
3276            _V_[__j] = __e_();
3277            return _Y_;
3278        }
3279
3280    template <uint64_t __n, uint64_t __d>
3281        _LIBCPP_INLINE_VISIBILITY
3282        result_type __evalf()
3283        {
3284            const double _F = __d == 0 ?
3285                __n / (2. * 0x8000000000000000ull) :
3286                __n / (double)__d;
3287            const size_t __j = static_cast<size_t>(_F * (_Y_ - _Min));
3288            _Y_ = _V_[__j];
3289            _V_[__j] = __e_();
3290            return _Y_;
3291        }
3292};
3293
3294template<class _Eng, size_t _K>
3295bool
3296operator==(
3297    const shuffle_order_engine<_Eng, _K>& __x,
3298    const shuffle_order_engine<_Eng, _K>& __y)
3299{
3300    return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _K, __y._V_) &&
3301           __x.__e_ == __y.__e_;
3302}
3303
3304template<class _Eng, size_t _K>
3305inline _LIBCPP_INLINE_VISIBILITY
3306bool
3307operator!=(
3308    const shuffle_order_engine<_Eng, _K>& __x,
3309    const shuffle_order_engine<_Eng, _K>& __y)
3310{
3311    return !(__x == __y);
3312}
3313
3314template <class _CharT, class _Traits,
3315          class _Eng, size_t _K>
3316basic_ostream<_CharT, _Traits>&
3317operator<<(basic_ostream<_CharT, _Traits>& __os,
3318           const shuffle_order_engine<_Eng, _K>& __x)
3319{
3320    __save_flags<_CharT, _Traits> _(__os);
3321    __os.flags(ios_base::dec | ios_base::left);
3322    _CharT __sp = __os.widen(' ');
3323    __os.fill(__sp);
3324    __os << __x.__e_ << __sp << __x._V_[0];
3325    for (size_t __i = 1; __i < _K; ++__i)
3326        __os << __sp << __x._V_[__i];
3327    return __os << __sp << __x._Y_;
3328}
3329
3330template <class _CharT, class _Traits,
3331          class _Eng, size_t _K>
3332basic_istream<_CharT, _Traits>&
3333operator>>(basic_istream<_CharT, _Traits>& __is,
3334           shuffle_order_engine<_Eng, _K>& __x)
3335{
3336    typedef typename shuffle_order_engine<_Eng, _K>::result_type result_type;
3337    __save_flags<_CharT, _Traits> _(__is);
3338    __is.flags(ios_base::dec | ios_base::skipws);
3339    _Eng __e;
3340    result_type _V[_K+1];
3341    __is >> __e;
3342    for (size_t __i = 0; __i < _K+1; ++__i)
3343        __is >> _V[__i];
3344    if (!__is.fail())
3345    {
3346        __x.__e_ = __e;
3347        for (size_t __i = 0; __i < _K; ++__i)
3348            __x._V_[__i] = _V[__i];
3349        __x._Y_ = _V[_K];
3350    }
3351    return __is;
3352}
3353
3354typedef shuffle_order_engine<minstd_rand0, 256>                         knuth_b;
3355
3356// random_device
3357
3358class _LIBCPP_VISIBLE random_device
3359{
3360    int __f_;
3361public:
3362    // types
3363    typedef unsigned result_type;
3364
3365    // generator characteristics
3366    static const result_type _Min = 0;
3367    static const result_type _Max = 0xFFFFFFFFu;
3368
3369    _LIBCPP_INLINE_VISIBILITY
3370    static const/*expr*/ result_type min() { return _Min;}
3371    _LIBCPP_INLINE_VISIBILITY
3372    static const/*expr*/ result_type max() { return _Max;}
3373
3374    // constructors
3375    explicit random_device(const string& __token = "/dev/urandom");
3376    ~random_device();
3377
3378    // generating functions
3379    result_type operator()();
3380
3381    // property functions
3382    double entropy() const;
3383
3384private:
3385    // no copy functions
3386    random_device(const random_device&); // = delete;
3387    random_device& operator=(const random_device&); // = delete;
3388};
3389
3390// seed_seq
3391
3392class _LIBCPP_VISIBLE seed_seq
3393{
3394public:
3395    // types
3396    typedef uint32_t result_type;
3397
3398private:
3399    vector<result_type> __v_;
3400
3401    template<class _InputIterator>
3402        void init(_InputIterator __first, _InputIterator __last);
3403public:
3404    // constructors
3405    _LIBCPP_INLINE_VISIBILITY
3406    seed_seq() {}
3407#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
3408    template<class _Tp>
3409        _LIBCPP_INLINE_VISIBILITY
3410        seed_seq(initializer_list<_Tp> __il) {init(__il.begin(), __il.end());}
3411#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
3412
3413    template<class _InputIterator>
3414        _LIBCPP_INLINE_VISIBILITY
3415        seed_seq(_InputIterator __first, _InputIterator __last)
3416             {init(__first, __last);}
3417
3418    // generating functions
3419    template<class _RandomAccessIterator>
3420        void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
3421
3422    // property functions
3423    _LIBCPP_INLINE_VISIBILITY
3424    size_t size() const {return __v_.size();}
3425    template<class _OutputIterator>
3426        _LIBCPP_INLINE_VISIBILITY
3427        void param(_OutputIterator __dest) const
3428            {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
3429
3430private:
3431    // no copy functions
3432    seed_seq(const seed_seq&); // = delete;
3433    void operator=(const seed_seq&); // = delete;
3434
3435    _LIBCPP_INLINE_VISIBILITY
3436    static result_type _T(result_type __x) {return __x ^ (__x >> 27);}
3437};
3438
3439template<class _InputIterator>
3440void
3441seed_seq::init(_InputIterator __first, _InputIterator __last)
3442{
3443    for (_InputIterator __s = __first; __s != __last; ++__s)
3444        __v_.push_back(*__s & 0xFFFFFFFF);
3445}
3446
3447template<class _RandomAccessIterator>
3448void
3449seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
3450{
3451    if (__first != __last)
3452    {
3453        _VSTD::fill(__first, __last, 0x8b8b8b8b);
3454        const size_t __n = static_cast<size_t>(__last - __first);
3455        const size_t __s = __v_.size();
3456        const size_t __t = (__n >= 623) ? 11
3457                         : (__n >= 68) ? 7
3458                         : (__n >= 39) ? 5
3459                         : (__n >= 7)  ? 3
3460                         : (__n - 1) / 2;
3461        const size_t __p = (__n - __t) / 2;
3462        const size_t __q = __p + __t;
3463        const size_t __m = _VSTD::max(__s + 1, __n);
3464        // __k = 0;
3465        {
3466            result_type __r = 1664525 * _T(__first[0] ^ __first[__p]
3467                                                      ^  __first[__n - 1]);
3468            __first[__p] += __r;
3469            __r += __s;
3470            __first[__q] += __r;
3471            __first[0] = __r;
3472        }
3473        for (size_t __k = 1; __k <= __s; ++__k)
3474        {
3475            const size_t __kmodn = __k % __n;
3476            const size_t __kpmodn = (__k + __p) % __n;
3477            result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3478                                           ^ __first[(__k - 1) % __n]);
3479            __first[__kpmodn] += __r;
3480            __r +=  __kmodn + __v_[__k-1];
3481            __first[(__k + __q) % __n] += __r;
3482            __first[__kmodn] = __r;
3483        }
3484        for (size_t __k = __s + 1; __k < __m; ++__k)
3485        {
3486            const size_t __kmodn = __k % __n;
3487            const size_t __kpmodn = (__k + __p) % __n;
3488            result_type __r = 1664525 * _T(__first[__kmodn] ^ __first[__kpmodn]
3489                                           ^ __first[(__k - 1) % __n]);
3490            __first[__kpmodn] += __r;
3491            __r +=  __kmodn;
3492            __first[(__k + __q) % __n] += __r;
3493            __first[__kmodn] = __r;
3494        }
3495        for (size_t __k = __m; __k < __m + __n; ++__k)
3496        {
3497            const size_t __kmodn = __k % __n;
3498            const size_t __kpmodn = (__k + __p) % __n;
3499            result_type __r = 1566083941 * _T(__first[__kmodn] +
3500                                              __first[__kpmodn] +
3501                                              __first[(__k - 1) % __n]);
3502            __first[__kpmodn] ^= __r;
3503            __r -= __kmodn;
3504            __first[(__k + __q) % __n] ^= __r;
3505            __first[__kmodn] = __r;
3506        }
3507    }
3508}
3509
3510// generate_canonical
3511
3512template<class _RealType, size_t __bits, class _URNG>
3513_RealType
3514generate_canonical(_URNG& __g)
3515{
3516    const size_t _Dt = numeric_limits<_RealType>::digits;
3517    const size_t __b = _Dt < __bits ? _Dt : __bits;
3518    const size_t __logR = __log2<uint64_t, _URNG::_Max - _URNG::_Min + uint64_t(1)>::value;
3519    const size_t __k = __b / __logR + (__b % __logR != 0) + (__b == 0);
3520    const _RealType _R = _URNG::_Max - _URNG::_Min + _RealType(1);
3521    _RealType __base = _R;
3522    _RealType _S = __g() - _URNG::_Min;
3523    for (size_t __i = 1; __i < __k; ++__i, __base *= _R)
3524        _S += (__g() - _URNG::_Min) * __base;
3525    return _S / __base;
3526}
3527
3528// uniform_int_distribution
3529
3530// in <algorithm>
3531
3532template <class _CharT, class _Traits, class _IT>
3533basic_ostream<_CharT, _Traits>&
3534operator<<(basic_ostream<_CharT, _Traits>& __os,
3535           const uniform_int_distribution<_IT>& __x)
3536{
3537    __save_flags<_CharT, _Traits> _(__os);
3538    __os.flags(ios_base::dec | ios_base::left);
3539    _CharT __sp = __os.widen(' ');
3540    __os.fill(__sp);
3541    return __os << __x.a() << __sp << __x.b();
3542}
3543
3544template <class _CharT, class _Traits, class _IT>
3545basic_istream<_CharT, _Traits>&
3546operator>>(basic_istream<_CharT, _Traits>& __is,
3547           uniform_int_distribution<_IT>& __x)
3548{
3549    typedef uniform_int_distribution<_IT> _Eng;
3550    typedef typename _Eng::result_type result_type;
3551    typedef typename _Eng::param_type param_type;
3552    __save_flags<_CharT, _Traits> _(__is);
3553    __is.flags(ios_base::dec | ios_base::skipws);
3554    result_type __a;
3555    result_type __b;
3556    __is >> __a >> __b;
3557    if (!__is.fail())
3558        __x.param(param_type(__a, __b));
3559    return __is;
3560}
3561
3562// uniform_real_distribution
3563
3564template<class _RealType = double>
3565class _LIBCPP_VISIBLE uniform_real_distribution
3566{
3567public:
3568    // types
3569    typedef _RealType result_type;
3570
3571    class _LIBCPP_VISIBLE param_type
3572    {
3573        result_type __a_;
3574        result_type __b_;
3575    public:
3576        typedef uniform_real_distribution distribution_type;
3577
3578        _LIBCPP_INLINE_VISIBILITY
3579        explicit param_type(result_type __a = 0,
3580                            result_type __b = 1)
3581            : __a_(__a), __b_(__b) {}
3582
3583        _LIBCPP_INLINE_VISIBILITY
3584        result_type a() const {return __a_;}
3585        _LIBCPP_INLINE_VISIBILITY
3586        result_type b() const {return __b_;}
3587
3588        friend _LIBCPP_INLINE_VISIBILITY
3589        bool operator==(const param_type& __x, const param_type& __y)
3590            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3591        friend _LIBCPP_INLINE_VISIBILITY
3592        bool operator!=(const param_type& __x, const param_type& __y)
3593            {return !(__x == __y);}
3594    };
3595
3596private:
3597    param_type __p_;
3598
3599public:
3600    // constructors and reset functions
3601    _LIBCPP_INLINE_VISIBILITY
3602    explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
3603        : __p_(param_type(__a, __b)) {}
3604    _LIBCPP_INLINE_VISIBILITY
3605    explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
3606    _LIBCPP_INLINE_VISIBILITY
3607    void reset() {}
3608
3609    // generating functions
3610    template<class _URNG>
3611        _LIBCPP_INLINE_VISIBILITY
3612        result_type operator()(_URNG& __g)
3613        {return (*this)(__g, __p_);}
3614    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3615
3616    // property functions
3617    _LIBCPP_INLINE_VISIBILITY
3618    result_type a() const {return __p_.a();}
3619    _LIBCPP_INLINE_VISIBILITY
3620    result_type b() const {return __p_.b();}
3621
3622    _LIBCPP_INLINE_VISIBILITY
3623    param_type param() const {return __p_;}
3624    _LIBCPP_INLINE_VISIBILITY
3625    void param(const param_type& __p) {__p_ = __p;}
3626
3627    _LIBCPP_INLINE_VISIBILITY
3628    result_type min() const {return a();}
3629    _LIBCPP_INLINE_VISIBILITY
3630    result_type max() const {return b();}
3631
3632    friend _LIBCPP_INLINE_VISIBILITY
3633        bool operator==(const uniform_real_distribution& __x,
3634                        const uniform_real_distribution& __y)
3635        {return __x.__p_ == __y.__p_;}
3636    friend _LIBCPP_INLINE_VISIBILITY
3637        bool operator!=(const uniform_real_distribution& __x,
3638                        const uniform_real_distribution& __y)
3639        {return !(__x == __y);}
3640};
3641
3642template<class _RealType>
3643template<class _URNG>
3644inline _LIBCPP_INLINE_VISIBILITY
3645typename uniform_real_distribution<_RealType>::result_type
3646uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
3647{
3648    return (__p.b() - __p.a())
3649        * _VSTD::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g)
3650        + __p.a();
3651}
3652
3653template <class _CharT, class _Traits, class _RT>
3654basic_ostream<_CharT, _Traits>&
3655operator<<(basic_ostream<_CharT, _Traits>& __os,
3656           const uniform_real_distribution<_RT>& __x)
3657{
3658    __save_flags<_CharT, _Traits> _(__os);
3659    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3660               ios_base::scientific);
3661    _CharT __sp = __os.widen(' ');
3662    __os.fill(__sp);
3663    return __os << __x.a() << __sp << __x.b();
3664}
3665
3666template <class _CharT, class _Traits, class _RT>
3667basic_istream<_CharT, _Traits>&
3668operator>>(basic_istream<_CharT, _Traits>& __is,
3669           uniform_real_distribution<_RT>& __x)
3670{
3671    typedef uniform_real_distribution<_RT> _Eng;
3672    typedef typename _Eng::result_type result_type;
3673    typedef typename _Eng::param_type param_type;
3674    __save_flags<_CharT, _Traits> _(__is);
3675    __is.flags(ios_base::dec | ios_base::skipws);
3676    result_type __a;
3677    result_type __b;
3678    __is >> __a >> __b;
3679    if (!__is.fail())
3680        __x.param(param_type(__a, __b));
3681    return __is;
3682}
3683
3684// bernoulli_distribution
3685
3686class _LIBCPP_VISIBLE bernoulli_distribution
3687{
3688public:
3689    // types
3690    typedef bool result_type;
3691
3692    class _LIBCPP_VISIBLE param_type
3693    {
3694        double __p_;
3695    public:
3696        typedef bernoulli_distribution distribution_type;
3697
3698        _LIBCPP_INLINE_VISIBILITY
3699        explicit param_type(double __p = 0.5) : __p_(__p) {}
3700
3701        _LIBCPP_INLINE_VISIBILITY
3702        double p() const {return __p_;}
3703
3704        friend _LIBCPP_INLINE_VISIBILITY
3705            bool operator==(const param_type& __x, const param_type& __y)
3706            {return __x.__p_ == __y.__p_;}
3707        friend _LIBCPP_INLINE_VISIBILITY
3708            bool operator!=(const param_type& __x, const param_type& __y)
3709            {return !(__x == __y);}
3710    };
3711
3712private:
3713    param_type __p_;
3714
3715public:
3716    // constructors and reset functions
3717    _LIBCPP_INLINE_VISIBILITY
3718    explicit bernoulli_distribution(double __p = 0.5)
3719        : __p_(param_type(__p)) {}
3720    _LIBCPP_INLINE_VISIBILITY
3721    explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
3722    _LIBCPP_INLINE_VISIBILITY
3723    void reset() {}
3724
3725    // generating functions
3726    template<class _URNG>
3727        _LIBCPP_INLINE_VISIBILITY
3728        result_type operator()(_URNG& __g)
3729        {return (*this)(__g, __p_);}
3730    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3731
3732    // property functions
3733    _LIBCPP_INLINE_VISIBILITY
3734    double p() const {return __p_.p();}
3735
3736    _LIBCPP_INLINE_VISIBILITY
3737    param_type param() const {return __p_;}
3738    _LIBCPP_INLINE_VISIBILITY
3739    void param(const param_type& __p) {__p_ = __p;}
3740
3741    _LIBCPP_INLINE_VISIBILITY
3742    result_type min() const {return false;}
3743    _LIBCPP_INLINE_VISIBILITY
3744    result_type max() const {return true;}
3745
3746    friend _LIBCPP_INLINE_VISIBILITY
3747        bool operator==(const bernoulli_distribution& __x,
3748                        const bernoulli_distribution& __y)
3749        {return __x.__p_ == __y.__p_;}
3750    friend _LIBCPP_INLINE_VISIBILITY
3751        bool operator!=(const bernoulli_distribution& __x,
3752                        const bernoulli_distribution& __y)
3753        {return !(__x == __y);}
3754};
3755
3756template<class _URNG>
3757inline _LIBCPP_INLINE_VISIBILITY
3758bernoulli_distribution::result_type
3759bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
3760{
3761    uniform_real_distribution<double> __gen;
3762    return __gen(__g) < __p.p();
3763}
3764
3765template <class _CharT, class _Traits>
3766basic_ostream<_CharT, _Traits>&
3767operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
3768{
3769    __save_flags<_CharT, _Traits> _(__os);
3770    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3771               ios_base::scientific);
3772    _CharT __sp = __os.widen(' ');
3773    __os.fill(__sp);
3774    return __os << __x.p();
3775}
3776
3777template <class _CharT, class _Traits>
3778basic_istream<_CharT, _Traits>&
3779operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x)
3780{
3781    typedef bernoulli_distribution _Eng;
3782    typedef typename _Eng::param_type param_type;
3783    __save_flags<_CharT, _Traits> _(__is);
3784    __is.flags(ios_base::dec | ios_base::skipws);
3785    double __p;
3786    __is >> __p;
3787    if (!__is.fail())
3788        __x.param(param_type(__p));
3789    return __is;
3790}
3791
3792// binomial_distribution
3793
3794template<class _IntType = int>
3795class _LIBCPP_VISIBLE binomial_distribution
3796{
3797public:
3798    // types
3799    typedef _IntType result_type;
3800
3801    class _LIBCPP_VISIBLE param_type
3802    {
3803        result_type __t_;
3804        double __p_;
3805        double __pr_;
3806        double __odds_ratio_;
3807        result_type __r0_;
3808    public:
3809        typedef binomial_distribution distribution_type;
3810
3811        explicit param_type(result_type __t = 1, double __p = 0.5);
3812
3813        _LIBCPP_INLINE_VISIBILITY
3814        result_type t() const {return __t_;}
3815        _LIBCPP_INLINE_VISIBILITY
3816        double p() const {return __p_;}
3817
3818        friend _LIBCPP_INLINE_VISIBILITY
3819            bool operator==(const param_type& __x, const param_type& __y)
3820            {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
3821        friend _LIBCPP_INLINE_VISIBILITY
3822            bool operator!=(const param_type& __x, const param_type& __y)
3823            {return !(__x == __y);}
3824
3825        friend class binomial_distribution;
3826    };
3827
3828private:
3829    param_type __p_;
3830
3831public:
3832    // constructors and reset functions
3833    _LIBCPP_INLINE_VISIBILITY
3834    explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
3835        : __p_(param_type(__t, __p)) {}
3836    _LIBCPP_INLINE_VISIBILITY
3837    explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
3838    _LIBCPP_INLINE_VISIBILITY
3839    void reset() {}
3840
3841    // generating functions
3842    template<class _URNG>
3843        _LIBCPP_INLINE_VISIBILITY
3844        result_type operator()(_URNG& __g)
3845        {return (*this)(__g, __p_);}
3846    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3847
3848    // property functions
3849    _LIBCPP_INLINE_VISIBILITY
3850    result_type t() const {return __p_.t();}
3851    _LIBCPP_INLINE_VISIBILITY
3852    double p() const {return __p_.p();}
3853
3854    _LIBCPP_INLINE_VISIBILITY
3855    param_type param() const {return __p_;}
3856    _LIBCPP_INLINE_VISIBILITY
3857    void param(const param_type& __p) {__p_ = __p;}
3858
3859    _LIBCPP_INLINE_VISIBILITY
3860    result_type min() const {return 0;}
3861    _LIBCPP_INLINE_VISIBILITY
3862    result_type max() const {return t();}
3863
3864    friend _LIBCPP_INLINE_VISIBILITY
3865        bool operator==(const binomial_distribution& __x,
3866                        const binomial_distribution& __y)
3867        {return __x.__p_ == __y.__p_;}
3868    friend _LIBCPP_INLINE_VISIBILITY
3869        bool operator!=(const binomial_distribution& __x,
3870                        const binomial_distribution& __y)
3871        {return !(__x == __y);}
3872};
3873
3874template<class _IntType>
3875binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p)
3876    : __t_(__t), __p_(__p)
3877{
3878    if (0 < __p_ && __p_ < 1)
3879    {
3880        __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
3881        __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) -
3882                          _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) +
3883                          (__t_ - __r0_) * _VSTD::log(1 - __p_));
3884        __odds_ratio_ = __p_ / (1 - __p_);
3885    }
3886}
3887
3888template<class _IntType>
3889template<class _URNG>
3890_IntType
3891binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr)
3892{
3893    if (__pr.__t_ == 0 || __pr.__p_ == 0)
3894        return 0;
3895    if (__pr.__p_ == 1)
3896        return __pr.__t_;
3897    uniform_real_distribution<double> __gen;
3898    double __u = __gen(__g) - __pr.__pr_;
3899    if (__u < 0)
3900        return __pr.__r0_;
3901    double __pu = __pr.__pr_;
3902    double __pd = __pu;
3903    result_type __ru = __pr.__r0_;
3904    result_type __rd = __ru;
3905    while (true)
3906    {
3907        if (__rd >= 1)
3908        {
3909            __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
3910            __u -= __pd;
3911            if (__u < 0)
3912                return __rd - 1;
3913        }
3914        --__rd;
3915        ++__ru;
3916        if (__ru <= __pr.__t_)
3917        {
3918            __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
3919            __u -= __pu;
3920            if (__u < 0)
3921                return __ru;
3922        }
3923    }
3924}
3925
3926template <class _CharT, class _Traits, class _IntType>
3927basic_ostream<_CharT, _Traits>&
3928operator<<(basic_ostream<_CharT, _Traits>& __os,
3929           const binomial_distribution<_IntType>& __x)
3930{
3931    __save_flags<_CharT, _Traits> _(__os);
3932    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
3933               ios_base::scientific);
3934    _CharT __sp = __os.widen(' ');
3935    __os.fill(__sp);
3936    return __os << __x.t() << __sp << __x.p();
3937}
3938
3939template <class _CharT, class _Traits, class _IntType>
3940basic_istream<_CharT, _Traits>&
3941operator>>(basic_istream<_CharT, _Traits>& __is,
3942           binomial_distribution<_IntType>& __x)
3943{
3944    typedef binomial_distribution<_IntType> _Eng;
3945    typedef typename _Eng::result_type result_type;
3946    typedef typename _Eng::param_type param_type;
3947    __save_flags<_CharT, _Traits> _(__is);
3948    __is.flags(ios_base::dec | ios_base::skipws);
3949    result_type __t;
3950    double __p;
3951    __is >> __t >> __p;
3952    if (!__is.fail())
3953        __x.param(param_type(__t, __p));
3954    return __is;
3955}
3956
3957// exponential_distribution
3958
3959template<class _RealType = double>
3960class _LIBCPP_VISIBLE exponential_distribution
3961{
3962public:
3963    // types
3964    typedef _RealType result_type;
3965
3966    class _LIBCPP_VISIBLE param_type
3967    {
3968        result_type __lambda_;
3969    public:
3970        typedef exponential_distribution distribution_type;
3971
3972        _LIBCPP_INLINE_VISIBILITY
3973        explicit param_type(result_type __lambda = 1) : __lambda_(__lambda) {}
3974
3975        _LIBCPP_INLINE_VISIBILITY
3976        result_type lambda() const {return __lambda_;}
3977
3978        friend _LIBCPP_INLINE_VISIBILITY
3979            bool operator==(const param_type& __x, const param_type& __y)
3980            {return __x.__lambda_ == __y.__lambda_;}
3981        friend _LIBCPP_INLINE_VISIBILITY
3982            bool operator!=(const param_type& __x, const param_type& __y)
3983            {return !(__x == __y);}
3984    };
3985
3986private:
3987    param_type __p_;
3988
3989public:
3990    // constructors and reset functions
3991    _LIBCPP_INLINE_VISIBILITY
3992    explicit exponential_distribution(result_type __lambda = 1)
3993        : __p_(param_type(__lambda)) {}
3994    _LIBCPP_INLINE_VISIBILITY
3995    explicit exponential_distribution(const param_type& __p) : __p_(__p) {}
3996    _LIBCPP_INLINE_VISIBILITY
3997    void reset() {}
3998
3999    // generating functions
4000    template<class _URNG>
4001        _LIBCPP_INLINE_VISIBILITY
4002        result_type operator()(_URNG& __g)
4003        {return (*this)(__g, __p_);}
4004    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4005
4006    // property functions
4007    _LIBCPP_INLINE_VISIBILITY
4008    result_type lambda() const {return __p_.lambda();}
4009
4010    _LIBCPP_INLINE_VISIBILITY
4011    param_type param() const {return __p_;}
4012    _LIBCPP_INLINE_VISIBILITY
4013    void param(const param_type& __p) {__p_ = __p;}
4014
4015    _LIBCPP_INLINE_VISIBILITY
4016    result_type min() const {return 0;}
4017    _LIBCPP_INLINE_VISIBILITY
4018    result_type max() const {return numeric_limits<result_type>::infinity();}
4019
4020    friend _LIBCPP_INLINE_VISIBILITY
4021        bool operator==(const exponential_distribution& __x,
4022                        const exponential_distribution& __y)
4023        {return __x.__p_ == __y.__p_;}
4024    friend _LIBCPP_INLINE_VISIBILITY
4025        bool operator!=(const exponential_distribution& __x,
4026                        const exponential_distribution& __y)
4027        {return !(__x == __y);}
4028};
4029
4030template <class _RealType>
4031template<class _URNG>
4032_RealType
4033exponential_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4034{
4035    return -_VSTD::log
4036                  (
4037                      result_type(1) -
4038                      _VSTD::generate_canonical<result_type,
4039                                       numeric_limits<result_type>::digits>(__g)
4040                  )
4041                  / __p.lambda();
4042}
4043
4044template <class _CharT, class _Traits, class _RealType>
4045basic_ostream<_CharT, _Traits>&
4046operator<<(basic_ostream<_CharT, _Traits>& __os,
4047           const exponential_distribution<_RealType>& __x)
4048{
4049    __save_flags<_CharT, _Traits> _(__os);
4050    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4051               ios_base::scientific);
4052    return __os << __x.lambda();
4053}
4054
4055template <class _CharT, class _Traits, class _RealType>
4056basic_istream<_CharT, _Traits>&
4057operator>>(basic_istream<_CharT, _Traits>& __is,
4058           exponential_distribution<_RealType>& __x)
4059{
4060    typedef exponential_distribution<_RealType> _Eng;
4061    typedef typename _Eng::result_type result_type;
4062    typedef typename _Eng::param_type param_type;
4063    __save_flags<_CharT, _Traits> _(__is);
4064    __is.flags(ios_base::dec | ios_base::skipws);
4065    result_type __lambda;
4066    __is >> __lambda;
4067    if (!__is.fail())
4068        __x.param(param_type(__lambda));
4069    return __is;
4070}
4071
4072// normal_distribution
4073
4074template<class _RealType = double>
4075class _LIBCPP_VISIBLE normal_distribution
4076{
4077public:
4078    // types
4079    typedef _RealType result_type;
4080
4081    class _LIBCPP_VISIBLE param_type
4082    {
4083        result_type __mean_;
4084        result_type __stddev_;
4085    public:
4086        typedef normal_distribution distribution_type;
4087
4088        _LIBCPP_INLINE_VISIBILITY
4089        explicit param_type(result_type __mean = 0, result_type __stddev = 1)
4090            : __mean_(__mean), __stddev_(__stddev) {}
4091
4092        _LIBCPP_INLINE_VISIBILITY
4093        result_type mean() const {return __mean_;}
4094        _LIBCPP_INLINE_VISIBILITY
4095        result_type stddev() const {return __stddev_;}
4096
4097        friend _LIBCPP_INLINE_VISIBILITY
4098            bool operator==(const param_type& __x, const param_type& __y)
4099            {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
4100        friend _LIBCPP_INLINE_VISIBILITY
4101            bool operator!=(const param_type& __x, const param_type& __y)
4102            {return !(__x == __y);}
4103    };
4104
4105private:
4106    param_type __p_;
4107    result_type _V_;
4108    bool _V_hot_;
4109
4110public:
4111    // constructors and reset functions
4112    _LIBCPP_INLINE_VISIBILITY
4113    explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
4114        : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
4115    _LIBCPP_INLINE_VISIBILITY
4116    explicit normal_distribution(const param_type& __p)
4117        : __p_(__p), _V_hot_(false) {}
4118    _LIBCPP_INLINE_VISIBILITY
4119    void reset() {_V_hot_ = false;}
4120
4121    // generating functions
4122    template<class _URNG>
4123        _LIBCPP_INLINE_VISIBILITY
4124        result_type operator()(_URNG& __g)
4125        {return (*this)(__g, __p_);}
4126    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4127
4128    // property functions
4129    _LIBCPP_INLINE_VISIBILITY
4130    result_type mean() const {return __p_.mean();}
4131    _LIBCPP_INLINE_VISIBILITY
4132    result_type stddev() const {return __p_.stddev();}
4133
4134    _LIBCPP_INLINE_VISIBILITY
4135    param_type param() const {return __p_;}
4136    _LIBCPP_INLINE_VISIBILITY
4137    void param(const param_type& __p) {__p_ = __p;}
4138
4139    _LIBCPP_INLINE_VISIBILITY
4140    result_type min() const {return -numeric_limits<result_type>::infinity();}
4141    _LIBCPP_INLINE_VISIBILITY
4142    result_type max() const {return numeric_limits<result_type>::infinity();}
4143
4144    friend _LIBCPP_INLINE_VISIBILITY
4145        bool operator==(const normal_distribution& __x,
4146                        const normal_distribution& __y)
4147        {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
4148                (!__x._V_hot_ || __x._V_ == __y._V_);}
4149    friend _LIBCPP_INLINE_VISIBILITY
4150        bool operator!=(const normal_distribution& __x,
4151                        const normal_distribution& __y)
4152        {return !(__x == __y);}
4153
4154    template <class _CharT, class _Traits, class _RT>
4155    friend
4156    basic_ostream<_CharT, _Traits>&
4157    operator<<(basic_ostream<_CharT, _Traits>& __os,
4158               const normal_distribution<_RT>& __x);
4159
4160    template <class _CharT, class _Traits, class _RT>
4161    friend
4162    basic_istream<_CharT, _Traits>&
4163    operator>>(basic_istream<_CharT, _Traits>& __is,
4164               normal_distribution<_RT>& __x);
4165};
4166
4167template <class _RealType>
4168template<class _URNG>
4169_RealType
4170normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4171{
4172    result_type _U;
4173    if (_V_hot_)
4174    {
4175        _V_hot_ = false;
4176        _U = _V_;
4177    }
4178    else
4179    {
4180        uniform_real_distribution<result_type> _Uni(-1, 1);
4181        result_type __u;
4182        result_type __v;
4183        result_type __s;
4184        do
4185        {
4186            __u = _Uni(__g);
4187            __v = _Uni(__g);
4188            __s = __u * __u + __v * __v;
4189        } while (__s > 1 || __s == 0);
4190        result_type _F = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
4191        _V_ = __v * _F;
4192        _V_hot_ = true;
4193        _U = __u * _F;
4194    }
4195    return _U * __p.stddev() + __p.mean();
4196}
4197
4198template <class _CharT, class _Traits, class _RT>
4199basic_ostream<_CharT, _Traits>&
4200operator<<(basic_ostream<_CharT, _Traits>& __os,
4201           const normal_distribution<_RT>& __x)
4202{
4203    __save_flags<_CharT, _Traits> _(__os);
4204    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4205               ios_base::scientific);
4206    _CharT __sp = __os.widen(' ');
4207    __os.fill(__sp);
4208    __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
4209    if (__x._V_hot_)
4210        __os << __sp << __x._V_;
4211    return __os;
4212}
4213
4214template <class _CharT, class _Traits, class _RT>
4215basic_istream<_CharT, _Traits>&
4216operator>>(basic_istream<_CharT, _Traits>& __is,
4217           normal_distribution<_RT>& __x)
4218{
4219    typedef normal_distribution<_RT> _Eng;
4220    typedef typename _Eng::result_type result_type;
4221    typedef typename _Eng::param_type param_type;
4222    __save_flags<_CharT, _Traits> _(__is);
4223    __is.flags(ios_base::dec | ios_base::skipws);
4224    result_type __mean;
4225    result_type __stddev;
4226    result_type _V = 0;
4227    bool _V_hot = false;
4228    __is >> __mean >> __stddev >> _V_hot;
4229    if (_V_hot)
4230        __is >> _V;
4231    if (!__is.fail())
4232    {
4233        __x.param(param_type(__mean, __stddev));
4234        __x._V_hot_ = _V_hot;
4235        __x._V_ = _V;
4236    }
4237    return __is;
4238}
4239
4240// lognormal_distribution
4241
4242template<class _RealType = double>
4243class _LIBCPP_VISIBLE lognormal_distribution
4244{
4245public:
4246    // types
4247    typedef _RealType result_type;
4248
4249    class _LIBCPP_VISIBLE param_type
4250    {
4251        normal_distribution<result_type> __nd_;
4252    public:
4253        typedef lognormal_distribution distribution_type;
4254
4255        _LIBCPP_INLINE_VISIBILITY
4256        explicit param_type(result_type __m = 0, result_type __s = 1)
4257            : __nd_(__m, __s) {}
4258
4259        _LIBCPP_INLINE_VISIBILITY
4260        result_type m() const {return __nd_.mean();}
4261        _LIBCPP_INLINE_VISIBILITY
4262        result_type s() const {return __nd_.stddev();}
4263
4264        friend _LIBCPP_INLINE_VISIBILITY
4265            bool operator==(const param_type& __x, const param_type& __y)
4266            {return __x.__nd_ == __y.__nd_;}
4267        friend _LIBCPP_INLINE_VISIBILITY
4268            bool operator!=(const param_type& __x, const param_type& __y)
4269            {return !(__x == __y);}
4270        friend class lognormal_distribution;
4271
4272        template <class _CharT, class _Traits, class _RT>
4273        friend
4274        basic_ostream<_CharT, _Traits>&
4275        operator<<(basic_ostream<_CharT, _Traits>& __os,
4276                   const lognormal_distribution<_RT>& __x);
4277
4278        template <class _CharT, class _Traits, class _RT>
4279        friend
4280        basic_istream<_CharT, _Traits>&
4281        operator>>(basic_istream<_CharT, _Traits>& __is,
4282                   lognormal_distribution<_RT>& __x);
4283    };
4284
4285private:
4286    param_type __p_;
4287
4288public:
4289    // constructor and reset functions
4290    _LIBCPP_INLINE_VISIBILITY
4291    explicit lognormal_distribution(result_type __m = 0, result_type __s = 1)
4292        : __p_(param_type(__m, __s)) {}
4293    _LIBCPP_INLINE_VISIBILITY
4294    explicit lognormal_distribution(const param_type& __p)
4295        : __p_(__p) {}
4296    _LIBCPP_INLINE_VISIBILITY
4297    void reset() {__p_.__nd_.reset();}
4298
4299    // generating functions
4300    template<class _URNG>
4301        _LIBCPP_INLINE_VISIBILITY
4302        result_type operator()(_URNG& __g)
4303        {return (*this)(__g, __p_);}
4304    template<class _URNG>
4305        _LIBCPP_INLINE_VISIBILITY
4306        result_type operator()(_URNG& __g, const param_type& __p)
4307        {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
4308
4309    // property functions
4310    _LIBCPP_INLINE_VISIBILITY
4311    result_type m() const {return __p_.m();}
4312    _LIBCPP_INLINE_VISIBILITY
4313    result_type s() const {return __p_.s();}
4314
4315    _LIBCPP_INLINE_VISIBILITY
4316    param_type param() const {return __p_;}
4317    _LIBCPP_INLINE_VISIBILITY
4318    void param(const param_type& __p) {__p_ = __p;}
4319
4320    _LIBCPP_INLINE_VISIBILITY
4321    result_type min() const {return 0;}
4322    _LIBCPP_INLINE_VISIBILITY
4323    result_type max() const {return numeric_limits<result_type>::infinity();}
4324
4325    friend _LIBCPP_INLINE_VISIBILITY
4326        bool operator==(const lognormal_distribution& __x,
4327                        const lognormal_distribution& __y)
4328        {return __x.__p_ == __y.__p_;}
4329    friend _LIBCPP_INLINE_VISIBILITY
4330        bool operator!=(const lognormal_distribution& __x,
4331                        const lognormal_distribution& __y)
4332        {return !(__x == __y);}
4333
4334    template <class _CharT, class _Traits, class _RT>
4335    friend
4336    basic_ostream<_CharT, _Traits>&
4337    operator<<(basic_ostream<_CharT, _Traits>& __os,
4338               const lognormal_distribution<_RT>& __x);
4339
4340    template <class _CharT, class _Traits, class _RT>
4341    friend
4342    basic_istream<_CharT, _Traits>&
4343    operator>>(basic_istream<_CharT, _Traits>& __is,
4344               lognormal_distribution<_RT>& __x);
4345};
4346
4347template <class _CharT, class _Traits, class _RT>
4348inline _LIBCPP_INLINE_VISIBILITY
4349basic_ostream<_CharT, _Traits>&
4350operator<<(basic_ostream<_CharT, _Traits>& __os,
4351           const lognormal_distribution<_RT>& __x)
4352{
4353    return __os << __x.__p_.__nd_;
4354}
4355
4356template <class _CharT, class _Traits, class _RT>
4357inline _LIBCPP_INLINE_VISIBILITY
4358basic_istream<_CharT, _Traits>&
4359operator>>(basic_istream<_CharT, _Traits>& __is,
4360           lognormal_distribution<_RT>& __x)
4361{
4362    return __is >> __x.__p_.__nd_;
4363}
4364
4365// poisson_distribution
4366
4367template<class _IntType = int>
4368class _LIBCPP_VISIBLE poisson_distribution
4369{
4370public:
4371    // types
4372    typedef _IntType result_type;
4373
4374    class _LIBCPP_VISIBLE param_type
4375    {
4376        double __mean_;
4377        double __s_;
4378        double __d_;
4379        double __l_;
4380        double __omega_;
4381        double __c0_;
4382        double __c1_;
4383        double __c2_;
4384        double __c3_;
4385        double __c_;
4386
4387    public:
4388        typedef poisson_distribution distribution_type;
4389
4390        explicit param_type(double __mean = 1.0);
4391
4392        _LIBCPP_INLINE_VISIBILITY
4393        double mean() const {return __mean_;}
4394
4395        friend _LIBCPP_INLINE_VISIBILITY
4396            bool operator==(const param_type& __x, const param_type& __y)
4397            {return __x.__mean_ == __y.__mean_;}
4398        friend _LIBCPP_INLINE_VISIBILITY
4399            bool operator!=(const param_type& __x, const param_type& __y)
4400            {return !(__x == __y);}
4401
4402        friend class poisson_distribution;
4403    };
4404
4405private:
4406    param_type __p_;
4407
4408public:
4409    // constructors and reset functions
4410    _LIBCPP_INLINE_VISIBILITY
4411    explicit poisson_distribution(double __mean = 1.0) : __p_(__mean) {}
4412    _LIBCPP_INLINE_VISIBILITY
4413    explicit poisson_distribution(const param_type& __p) : __p_(__p) {}
4414    _LIBCPP_INLINE_VISIBILITY
4415    void reset() {}
4416
4417    // generating functions
4418    template<class _URNG>
4419        _LIBCPP_INLINE_VISIBILITY
4420        result_type operator()(_URNG& __g)
4421        {return (*this)(__g, __p_);}
4422    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4423
4424    // property functions
4425    _LIBCPP_INLINE_VISIBILITY
4426    double mean() const {return __p_.mean();}
4427
4428    _LIBCPP_INLINE_VISIBILITY
4429    param_type param() const {return __p_;}
4430    _LIBCPP_INLINE_VISIBILITY
4431    void param(const param_type& __p) {__p_ = __p;}
4432
4433    _LIBCPP_INLINE_VISIBILITY
4434    result_type min() const {return 0;}
4435    _LIBCPP_INLINE_VISIBILITY
4436    result_type max() const {return numeric_limits<result_type>::max();}
4437
4438    friend _LIBCPP_INLINE_VISIBILITY
4439        bool operator==(const poisson_distribution& __x,
4440                        const poisson_distribution& __y)
4441        {return __x.__p_ == __y.__p_;}
4442    friend _LIBCPP_INLINE_VISIBILITY
4443        bool operator!=(const poisson_distribution& __x,
4444                        const poisson_distribution& __y)
4445        {return !(__x == __y);}
4446};
4447
4448template<class _IntType>
4449poisson_distribution<_IntType>::param_type::param_type(double __mean)
4450    : __mean_(__mean)
4451{
4452    if (__mean_ < 10)
4453    {
4454        __s_ = 0;
4455        __d_ = 0;
4456        __l_ = _VSTD::exp(-__mean_);
4457        __omega_ = 0;
4458        __c3_ = 0;
4459        __c2_ = 0;
4460        __c1_ = 0;
4461        __c0_ = 0;
4462        __c_ = 0;
4463    }
4464    else
4465    {
4466        __s_ = _VSTD::sqrt(__mean_);
4467        __d_ = 6 * __mean_ * __mean_;
4468        __l_ = static_cast<result_type>(__mean_ - 1.1484);
4469        __omega_ = .3989423 / __s_;
4470        double __b1_ = .4166667E-1 / __mean_;
4471        double __b2_ = .3 * __b1_ * __b1_;
4472        __c3_ = .1428571 * __b1_ * __b2_;
4473        __c2_ = __b2_ - 15. * __c3_;
4474        __c1_ = __b1_ - 6. * __b2_ + 45. * __c3_;
4475        __c0_ = 1. - __b1_ + 3. * __b2_ - 15. * __c3_;
4476        __c_ = .1069 / __mean_;
4477    }
4478}
4479
4480template <class _IntType>
4481template<class _URNG>
4482_IntType
4483poisson_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
4484{
4485    result_type __x;
4486    uniform_real_distribution<double> __urd;
4487    if (__pr.__mean_ < 10)
4488    {
4489         __x = 0;
4490        for (double __p = __urd(__urng); __p > __pr.__l_; ++__x)
4491            __p *= __urd(__urng);
4492    }
4493    else
4494    {
4495        double __difmuk;
4496        double __g = __pr.__mean_ + __pr.__s_ * normal_distribution<double>()(__urng);
4497        double __u;
4498        if (__g > 0)
4499        {
4500            __x = static_cast<result_type>(__g);
4501            if (__x >= __pr.__l_)
4502                return __x;
4503            __difmuk = __pr.__mean_ - __x;
4504            __u = __urd(__urng);
4505            if (__pr.__d_ * __u >= __difmuk * __difmuk * __difmuk)
4506                return __x;
4507        }
4508        exponential_distribution<double> __edist;
4509        for (bool __using_exp_dist = false; true; __using_exp_dist = true)
4510        {
4511            double __e;
4512            if (__using_exp_dist || __g < 0)
4513            {
4514                double __t;
4515                do
4516                {
4517                    __e = __edist(__urng);
4518                    __u = __urd(__urng);
4519                    __u += __u - 1;
4520                    __t = 1.8 + (__u < 0 ? -__e : __e);
4521                } while (__t <= -.6744);
4522                __x = __pr.__mean_ + __pr.__s_ * __t;
4523                __difmuk = __pr.__mean_ - __x;
4524                __using_exp_dist = true;
4525            }
4526            double __px;
4527            double __py;
4528            if (__x < 10)
4529            {
4530                const result_type __fac[] = {1, 1, 2, 6, 24, 120, 720, 5040,
4531                                             40320, 362880};
4532                __px = -__pr.__mean_;
4533                __py = _VSTD::pow(__pr.__mean_, (double)__x) / __fac[__x];
4534            }
4535            else
4536            {
4537                double __del = .8333333E-1 / __x;
4538                __del -= 4.8 * __del * __del * __del;
4539                double __v = __difmuk / __x;
4540                if (_VSTD::abs(__v) > 0.25)
4541                    __px = __x * _VSTD::log(1 + __v) - __difmuk - __del;
4542                else
4543                    __px = __x * __v * __v * (((((((.1250060 * __v + -.1384794) *
4544                           __v + .1421878) * __v + -.1661269) * __v + .2000118) *
4545                           __v + -.2500068) * __v + .3333333) * __v + -.5) - __del;
4546                __py = .3989423 / _VSTD::sqrt(__x);
4547            }
4548            double __r = (0.5 - __difmuk) / __pr.__s_;
4549            double __r2 = __r * __r;
4550            double __fx = -0.5 * __r2;
4551            double __fy = __pr.__omega_ * (((__pr.__c3_ * __r2 + __pr.__c2_) *
4552                                        __r2 + __pr.__c1_) * __r2 + __pr.__c0_);
4553            if (__using_exp_dist)
4554            {
4555                if (__pr.__c_ * _VSTD::abs(__u) <= __py * _VSTD::exp(__px + __e) -
4556                                                   __fy * _VSTD::exp(__fx + __e))
4557                    break;
4558            }
4559            else
4560            {
4561                if (__fy - __u * __fy <= __py * _VSTD::exp(__px - __fx))
4562                    break;
4563            }
4564        }
4565    }
4566    return __x;
4567}
4568
4569template <class _CharT, class _Traits, class _IntType>
4570basic_ostream<_CharT, _Traits>&
4571operator<<(basic_ostream<_CharT, _Traits>& __os,
4572           const poisson_distribution<_IntType>& __x)
4573{
4574    __save_flags<_CharT, _Traits> _(__os);
4575    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4576               ios_base::scientific);
4577    return __os << __x.mean();
4578}
4579
4580template <class _CharT, class _Traits, class _IntType>
4581basic_istream<_CharT, _Traits>&
4582operator>>(basic_istream<_CharT, _Traits>& __is,
4583           poisson_distribution<_IntType>& __x)
4584{
4585    typedef poisson_distribution<_IntType> _Eng;
4586    typedef typename _Eng::param_type param_type;
4587    __save_flags<_CharT, _Traits> _(__is);
4588    __is.flags(ios_base::dec | ios_base::skipws);
4589    double __mean;
4590    __is >> __mean;
4591    if (!__is.fail())
4592        __x.param(param_type(__mean));
4593    return __is;
4594}
4595
4596// weibull_distribution
4597
4598template<class _RealType = double>
4599class _LIBCPP_VISIBLE weibull_distribution
4600{
4601public:
4602    // types
4603    typedef _RealType result_type;
4604
4605    class _LIBCPP_VISIBLE param_type
4606    {
4607        result_type __a_;
4608        result_type __b_;
4609    public:
4610        typedef weibull_distribution distribution_type;
4611
4612        _LIBCPP_INLINE_VISIBILITY
4613        explicit param_type(result_type __a = 1, result_type __b = 1)
4614            : __a_(__a), __b_(__b) {}
4615
4616        _LIBCPP_INLINE_VISIBILITY
4617        result_type a() const {return __a_;}
4618        _LIBCPP_INLINE_VISIBILITY
4619        result_type b() const {return __b_;}
4620
4621        friend _LIBCPP_INLINE_VISIBILITY
4622            bool operator==(const param_type& __x, const param_type& __y)
4623            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4624        friend _LIBCPP_INLINE_VISIBILITY
4625            bool operator!=(const param_type& __x, const param_type& __y)
4626            {return !(__x == __y);}
4627    };
4628
4629private:
4630    param_type __p_;
4631
4632public:
4633    // constructor and reset functions
4634    _LIBCPP_INLINE_VISIBILITY
4635    explicit weibull_distribution(result_type __a = 1, result_type __b = 1)
4636        : __p_(param_type(__a, __b)) {}
4637    _LIBCPP_INLINE_VISIBILITY
4638    explicit weibull_distribution(const param_type& __p)
4639        : __p_(__p) {}
4640    _LIBCPP_INLINE_VISIBILITY
4641    void reset() {}
4642
4643    // generating functions
4644    template<class _URNG>
4645        _LIBCPP_INLINE_VISIBILITY
4646        result_type operator()(_URNG& __g)
4647        {return (*this)(__g, __p_);}
4648    template<class _URNG>
4649        _LIBCPP_INLINE_VISIBILITY
4650        result_type operator()(_URNG& __g, const param_type& __p)
4651        {return __p.b() *
4652            _VSTD::pow(exponential_distribution<result_type>()(__g), 1/__p.a());}
4653
4654    // property functions
4655    _LIBCPP_INLINE_VISIBILITY
4656    result_type a() const {return __p_.a();}
4657    _LIBCPP_INLINE_VISIBILITY
4658    result_type b() const {return __p_.b();}
4659
4660    _LIBCPP_INLINE_VISIBILITY
4661    param_type param() const {return __p_;}
4662    _LIBCPP_INLINE_VISIBILITY
4663    void param(const param_type& __p) {__p_ = __p;}
4664
4665    _LIBCPP_INLINE_VISIBILITY
4666    result_type min() const {return 0;}
4667    _LIBCPP_INLINE_VISIBILITY
4668    result_type max() const {return numeric_limits<result_type>::infinity();}
4669
4670    friend _LIBCPP_INLINE_VISIBILITY
4671        bool operator==(const weibull_distribution& __x,
4672                        const weibull_distribution& __y)
4673        {return __x.__p_ == __y.__p_;}
4674    friend _LIBCPP_INLINE_VISIBILITY
4675        bool operator!=(const weibull_distribution& __x,
4676                        const weibull_distribution& __y)
4677        {return !(__x == __y);}
4678};
4679
4680template <class _CharT, class _Traits, class _RT>
4681basic_ostream<_CharT, _Traits>&
4682operator<<(basic_ostream<_CharT, _Traits>& __os,
4683           const weibull_distribution<_RT>& __x)
4684{
4685    __save_flags<_CharT, _Traits> _(__os);
4686    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4687               ios_base::scientific);
4688    _CharT __sp = __os.widen(' ');
4689    __os.fill(__sp);
4690    __os << __x.a() << __sp << __x.b();
4691    return __os;
4692}
4693
4694template <class _CharT, class _Traits, class _RT>
4695basic_istream<_CharT, _Traits>&
4696operator>>(basic_istream<_CharT, _Traits>& __is,
4697           weibull_distribution<_RT>& __x)
4698{
4699    typedef weibull_distribution<_RT> _Eng;
4700    typedef typename _Eng::result_type result_type;
4701    typedef typename _Eng::param_type param_type;
4702    __save_flags<_CharT, _Traits> _(__is);
4703    __is.flags(ios_base::dec | ios_base::skipws);
4704    result_type __a;
4705    result_type __b;
4706    __is >> __a >> __b;
4707    if (!__is.fail())
4708        __x.param(param_type(__a, __b));
4709    return __is;
4710}
4711
4712template<class _RealType = double>
4713class _LIBCPP_VISIBLE extreme_value_distribution
4714{
4715public:
4716    // types
4717    typedef _RealType result_type;
4718
4719    class _LIBCPP_VISIBLE param_type
4720    {
4721        result_type __a_;
4722        result_type __b_;
4723    public:
4724        typedef extreme_value_distribution distribution_type;
4725
4726        _LIBCPP_INLINE_VISIBILITY
4727        explicit param_type(result_type __a = 0, result_type __b = 1)
4728            : __a_(__a), __b_(__b) {}
4729
4730        _LIBCPP_INLINE_VISIBILITY
4731        result_type a() const {return __a_;}
4732        _LIBCPP_INLINE_VISIBILITY
4733        result_type b() const {return __b_;}
4734
4735        friend _LIBCPP_INLINE_VISIBILITY
4736            bool operator==(const param_type& __x, const param_type& __y)
4737            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
4738        friend _LIBCPP_INLINE_VISIBILITY
4739            bool operator!=(const param_type& __x, const param_type& __y)
4740            {return !(__x == __y);}
4741    };
4742
4743private:
4744    param_type __p_;
4745
4746public:
4747    // constructor and reset functions
4748    _LIBCPP_INLINE_VISIBILITY
4749    explicit extreme_value_distribution(result_type __a = 0, result_type __b = 1)
4750        : __p_(param_type(__a, __b)) {}
4751    _LIBCPP_INLINE_VISIBILITY
4752    explicit extreme_value_distribution(const param_type& __p)
4753        : __p_(__p) {}
4754    _LIBCPP_INLINE_VISIBILITY
4755    void reset() {}
4756
4757    // generating functions
4758    template<class _URNG>
4759        _LIBCPP_INLINE_VISIBILITY
4760        result_type operator()(_URNG& __g)
4761        {return (*this)(__g, __p_);}
4762    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4763
4764    // property functions
4765    _LIBCPP_INLINE_VISIBILITY
4766    result_type a() const {return __p_.a();}
4767    _LIBCPP_INLINE_VISIBILITY
4768    result_type b() const {return __p_.b();}
4769
4770    _LIBCPP_INLINE_VISIBILITY
4771    param_type param() const {return __p_;}
4772    _LIBCPP_INLINE_VISIBILITY
4773    void param(const param_type& __p) {__p_ = __p;}
4774
4775    _LIBCPP_INLINE_VISIBILITY
4776    result_type min() const {return -numeric_limits<result_type>::infinity();}
4777    _LIBCPP_INLINE_VISIBILITY
4778    result_type max() const {return numeric_limits<result_type>::infinity();}
4779
4780    friend _LIBCPP_INLINE_VISIBILITY
4781        bool operator==(const extreme_value_distribution& __x,
4782                        const extreme_value_distribution& __y)
4783        {return __x.__p_ == __y.__p_;}
4784    friend _LIBCPP_INLINE_VISIBILITY
4785        bool operator!=(const extreme_value_distribution& __x,
4786                        const extreme_value_distribution& __y)
4787        {return !(__x == __y);}
4788};
4789
4790template<class _RealType>
4791template<class _URNG>
4792_RealType
4793extreme_value_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4794{
4795    return __p.a() - __p.b() *
4796         _VSTD::log(-_VSTD::log(1-uniform_real_distribution<result_type>()(__g)));
4797}
4798
4799template <class _CharT, class _Traits, class _RT>
4800basic_ostream<_CharT, _Traits>&
4801operator<<(basic_ostream<_CharT, _Traits>& __os,
4802           const extreme_value_distribution<_RT>& __x)
4803{
4804    __save_flags<_CharT, _Traits> _(__os);
4805    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4806               ios_base::scientific);
4807    _CharT __sp = __os.widen(' ');
4808    __os.fill(__sp);
4809    __os << __x.a() << __sp << __x.b();
4810    return __os;
4811}
4812
4813template <class _CharT, class _Traits, class _RT>
4814basic_istream<_CharT, _Traits>&
4815operator>>(basic_istream<_CharT, _Traits>& __is,
4816           extreme_value_distribution<_RT>& __x)
4817{
4818    typedef extreme_value_distribution<_RT> _Eng;
4819    typedef typename _Eng::result_type result_type;
4820    typedef typename _Eng::param_type param_type;
4821    __save_flags<_CharT, _Traits> _(__is);
4822    __is.flags(ios_base::dec | ios_base::skipws);
4823    result_type __a;
4824    result_type __b;
4825    __is >> __a >> __b;
4826    if (!__is.fail())
4827        __x.param(param_type(__a, __b));
4828    return __is;
4829}
4830
4831// gamma_distribution
4832
4833template<class _RealType = double>
4834class _LIBCPP_VISIBLE gamma_distribution
4835{
4836public:
4837    // types
4838    typedef _RealType result_type;
4839
4840    class _LIBCPP_VISIBLE param_type
4841    {
4842        result_type __alpha_;
4843        result_type __beta_;
4844    public:
4845        typedef gamma_distribution distribution_type;
4846
4847        _LIBCPP_INLINE_VISIBILITY
4848        explicit param_type(result_type __alpha = 1, result_type __beta = 1)
4849            : __alpha_(__alpha), __beta_(__beta) {}
4850
4851        _LIBCPP_INLINE_VISIBILITY
4852        result_type alpha() const {return __alpha_;}
4853        _LIBCPP_INLINE_VISIBILITY
4854        result_type beta() const {return __beta_;}
4855
4856        friend _LIBCPP_INLINE_VISIBILITY
4857            bool operator==(const param_type& __x, const param_type& __y)
4858            {return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;}
4859        friend _LIBCPP_INLINE_VISIBILITY
4860            bool operator!=(const param_type& __x, const param_type& __y)
4861            {return !(__x == __y);}
4862    };
4863
4864private:
4865    param_type __p_;
4866
4867public:
4868    // constructors and reset functions
4869    _LIBCPP_INLINE_VISIBILITY
4870    explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
4871        : __p_(param_type(__alpha, __beta)) {}
4872    _LIBCPP_INLINE_VISIBILITY
4873    explicit gamma_distribution(const param_type& __p)
4874        : __p_(__p) {}
4875    _LIBCPP_INLINE_VISIBILITY
4876    void reset() {}
4877
4878    // generating functions
4879    template<class _URNG>
4880        _LIBCPP_INLINE_VISIBILITY
4881        result_type operator()(_URNG& __g)
4882        {return (*this)(__g, __p_);}
4883    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
4884
4885    // property functions
4886    _LIBCPP_INLINE_VISIBILITY
4887    result_type alpha() const {return __p_.alpha();}
4888    _LIBCPP_INLINE_VISIBILITY
4889    result_type beta() const {return __p_.beta();}
4890
4891    _LIBCPP_INLINE_VISIBILITY
4892    param_type param() const {return __p_;}
4893    _LIBCPP_INLINE_VISIBILITY
4894    void param(const param_type& __p) {__p_ = __p;}
4895
4896    _LIBCPP_INLINE_VISIBILITY
4897    result_type min() const {return 0;}
4898    _LIBCPP_INLINE_VISIBILITY
4899    result_type max() const {return numeric_limits<result_type>::infinity();}
4900
4901    friend _LIBCPP_INLINE_VISIBILITY
4902        bool operator==(const gamma_distribution& __x,
4903                        const gamma_distribution& __y)
4904        {return __x.__p_ == __y.__p_;}
4905    friend _LIBCPP_INLINE_VISIBILITY
4906        bool operator!=(const gamma_distribution& __x,
4907                        const gamma_distribution& __y)
4908        {return !(__x == __y);}
4909};
4910
4911template <class _RealType>
4912template<class _URNG>
4913_RealType
4914gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
4915{
4916    result_type __a = __p.alpha();
4917    uniform_real_distribution<result_type> __gen(0, 1);
4918    exponential_distribution<result_type> __egen;
4919    result_type __x;
4920    if (__a == 1)
4921        __x = __egen(__g);
4922    else if (__a > 1)
4923    {
4924        const result_type __b = __a - 1;
4925        const result_type __c = 3 * __a - result_type(0.75);
4926        while (true)
4927        {
4928            const result_type __u = __gen(__g);
4929            const result_type __v = __gen(__g);
4930            const result_type __w = __u * (1 - __u);
4931            if (__w != 0)
4932            {
4933                const result_type __y = _VSTD::sqrt(__c / __w) *
4934                                        (__u - result_type(0.5));
4935                __x = __b + __y;
4936                if (__x >= 0)
4937                {
4938                    const result_type __z = 64 * __w * __w * __w * __v * __v;
4939                    if (__z <= 1 - 2 * __y * __y / __x)
4940                        break;
4941                    if (_VSTD::log(__z) <= 2 * (__b * _VSTD::log(__x / __b) - __y))
4942                        break;
4943                }
4944            }
4945        }
4946    }
4947    else  // __a < 1
4948    {
4949        while (true)
4950        {
4951            const result_type __u = __gen(__g);
4952            const result_type __es = __egen(__g);
4953            if (__u <= 1 - __a)
4954            {
4955                __x = _VSTD::pow(__u, 1 / __a);
4956                if (__x <= __es)
4957                    break;
4958            }
4959            else
4960            {
4961                const result_type __e = -_VSTD::log((1-__u)/__a);
4962                __x = _VSTD::pow(1 - __a + __a * __e, 1 / __a);
4963                if (__x <= __e + __es)
4964                    break;
4965            }
4966        }
4967    }
4968    return __x * __p.beta();
4969}
4970
4971template <class _CharT, class _Traits, class _RT>
4972basic_ostream<_CharT, _Traits>&
4973operator<<(basic_ostream<_CharT, _Traits>& __os,
4974           const gamma_distribution<_RT>& __x)
4975{
4976    __save_flags<_CharT, _Traits> _(__os);
4977    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
4978               ios_base::scientific);
4979    _CharT __sp = __os.widen(' ');
4980    __os.fill(__sp);
4981    __os << __x.alpha() << __sp << __x.beta();
4982    return __os;
4983}
4984
4985template <class _CharT, class _Traits, class _RT>
4986basic_istream<_CharT, _Traits>&
4987operator>>(basic_istream<_CharT, _Traits>& __is,
4988           gamma_distribution<_RT>& __x)
4989{
4990    typedef gamma_distribution<_RT> _Eng;
4991    typedef typename _Eng::result_type result_type;
4992    typedef typename _Eng::param_type param_type;
4993    __save_flags<_CharT, _Traits> _(__is);
4994    __is.flags(ios_base::dec | ios_base::skipws);
4995    result_type __alpha;
4996    result_type __beta;
4997    __is >> __alpha >> __beta;
4998    if (!__is.fail())
4999        __x.param(param_type(__alpha, __beta));
5000    return __is;
5001}
5002
5003// negative_binomial_distribution
5004
5005template<class _IntType = int>
5006class _LIBCPP_VISIBLE negative_binomial_distribution
5007{
5008public:
5009    // types
5010    typedef _IntType result_type;
5011
5012    class _LIBCPP_VISIBLE param_type
5013    {
5014        result_type __k_;
5015        double __p_;
5016    public:
5017        typedef negative_binomial_distribution distribution_type;
5018
5019        _LIBCPP_INLINE_VISIBILITY
5020        explicit param_type(result_type __k = 1, double __p = 0.5)
5021            : __k_(__k), __p_(__p) {}
5022
5023        _LIBCPP_INLINE_VISIBILITY
5024        result_type k() const {return __k_;}
5025        _LIBCPP_INLINE_VISIBILITY
5026        double p() const {return __p_;}
5027
5028        friend _LIBCPP_INLINE_VISIBILITY
5029            bool operator==(const param_type& __x, const param_type& __y)
5030            {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
5031        friend _LIBCPP_INLINE_VISIBILITY
5032            bool operator!=(const param_type& __x, const param_type& __y)
5033            {return !(__x == __y);}
5034    };
5035
5036private:
5037    param_type __p_;
5038
5039public:
5040    // constructor and reset functions
5041    _LIBCPP_INLINE_VISIBILITY
5042    explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
5043        : __p_(__k, __p) {}
5044    _LIBCPP_INLINE_VISIBILITY
5045    explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
5046    _LIBCPP_INLINE_VISIBILITY
5047    void reset() {}
5048
5049    // generating functions
5050    template<class _URNG>
5051        _LIBCPP_INLINE_VISIBILITY
5052        result_type operator()(_URNG& __g)
5053        {return (*this)(__g, __p_);}
5054    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5055
5056    // property functions
5057    _LIBCPP_INLINE_VISIBILITY
5058    result_type k() const {return __p_.k();}
5059    _LIBCPP_INLINE_VISIBILITY
5060    double p() const {return __p_.p();}
5061
5062    _LIBCPP_INLINE_VISIBILITY
5063    param_type param() const {return __p_;}
5064    _LIBCPP_INLINE_VISIBILITY
5065    void param(const param_type& __p) {__p_ = __p;}
5066
5067    _LIBCPP_INLINE_VISIBILITY
5068    result_type min() const {return 0;}
5069    _LIBCPP_INLINE_VISIBILITY
5070    result_type max() const {return numeric_limits<result_type>::max();}
5071
5072    friend _LIBCPP_INLINE_VISIBILITY
5073        bool operator==(const negative_binomial_distribution& __x,
5074                        const negative_binomial_distribution& __y)
5075        {return __x.__p_ == __y.__p_;}
5076    friend _LIBCPP_INLINE_VISIBILITY
5077        bool operator!=(const negative_binomial_distribution& __x,
5078                        const negative_binomial_distribution& __y)
5079        {return !(__x == __y);}
5080};
5081
5082template <class _IntType>
5083template<class _URNG>
5084_IntType
5085negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
5086{
5087    result_type __k = __pr.k();
5088    double __p = __pr.p();
5089    if (__k <= 21 * __p)
5090    {
5091        bernoulli_distribution __gen(__p);
5092        result_type __f = 0;
5093        result_type __s = 0;
5094        while (__s < __k)
5095        {
5096            if (__gen(__urng))
5097                ++__s;
5098            else
5099                ++__f;
5100        }
5101        return __f;
5102    }
5103    return poisson_distribution<result_type>(gamma_distribution<double>
5104                                            (__k, (1-__p)/__p)(__urng))(__urng);
5105}
5106
5107template <class _CharT, class _Traits, class _IntType>
5108basic_ostream<_CharT, _Traits>&
5109operator<<(basic_ostream<_CharT, _Traits>& __os,
5110           const negative_binomial_distribution<_IntType>& __x)
5111{
5112    __save_flags<_CharT, _Traits> _(__os);
5113    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5114               ios_base::scientific);
5115    _CharT __sp = __os.widen(' ');
5116    __os.fill(__sp);
5117    return __os << __x.k() << __sp << __x.p();
5118}
5119
5120template <class _CharT, class _Traits, class _IntType>
5121basic_istream<_CharT, _Traits>&
5122operator>>(basic_istream<_CharT, _Traits>& __is,
5123           negative_binomial_distribution<_IntType>& __x)
5124{
5125    typedef negative_binomial_distribution<_IntType> _Eng;
5126    typedef typename _Eng::result_type result_type;
5127    typedef typename _Eng::param_type param_type;
5128    __save_flags<_CharT, _Traits> _(__is);
5129    __is.flags(ios_base::dec | ios_base::skipws);
5130    result_type __k;
5131    double __p;
5132    __is >> __k >> __p;
5133    if (!__is.fail())
5134        __x.param(param_type(__k, __p));
5135    return __is;
5136}
5137
5138// geometric_distribution
5139
5140template<class _IntType = int>
5141class _LIBCPP_VISIBLE geometric_distribution
5142{
5143public:
5144    // types
5145    typedef _IntType result_type;
5146
5147    class _LIBCPP_VISIBLE param_type
5148    {
5149        double __p_;
5150    public:
5151        typedef geometric_distribution distribution_type;
5152
5153        _LIBCPP_INLINE_VISIBILITY
5154        explicit param_type(double __p = 0.5) : __p_(__p) {}
5155
5156        _LIBCPP_INLINE_VISIBILITY
5157        double p() const {return __p_;}
5158
5159        friend _LIBCPP_INLINE_VISIBILITY
5160            bool operator==(const param_type& __x, const param_type& __y)
5161            {return __x.__p_ == __y.__p_;}
5162        friend _LIBCPP_INLINE_VISIBILITY
5163            bool operator!=(const param_type& __x, const param_type& __y)
5164            {return !(__x == __y);}
5165    };
5166
5167private:
5168    param_type __p_;
5169
5170public:
5171    // constructors and reset functions
5172    _LIBCPP_INLINE_VISIBILITY
5173    explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
5174    _LIBCPP_INLINE_VISIBILITY
5175    explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
5176    _LIBCPP_INLINE_VISIBILITY
5177    void reset() {}
5178
5179    // generating functions
5180    template<class _URNG>
5181        _LIBCPP_INLINE_VISIBILITY
5182        result_type operator()(_URNG& __g)
5183        {return (*this)(__g, __p_);}
5184    template<class _URNG>
5185        _LIBCPP_INLINE_VISIBILITY
5186        result_type operator()(_URNG& __g, const param_type& __p)
5187        {return negative_binomial_distribution<result_type>(1, __p.p())(__g);}
5188
5189    // property functions
5190    _LIBCPP_INLINE_VISIBILITY
5191    double p() const {return __p_.p();}
5192
5193    _LIBCPP_INLINE_VISIBILITY
5194    param_type param() const {return __p_;}
5195    _LIBCPP_INLINE_VISIBILITY
5196    void param(const param_type& __p) {__p_ = __p;}
5197
5198    _LIBCPP_INLINE_VISIBILITY
5199    result_type min() const {return 0;}
5200    _LIBCPP_INLINE_VISIBILITY
5201    result_type max() const {return numeric_limits<result_type>::max();}
5202
5203    friend _LIBCPP_INLINE_VISIBILITY
5204        bool operator==(const geometric_distribution& __x,
5205                        const geometric_distribution& __y)
5206        {return __x.__p_ == __y.__p_;}
5207    friend _LIBCPP_INLINE_VISIBILITY
5208        bool operator!=(const geometric_distribution& __x,
5209                        const geometric_distribution& __y)
5210        {return !(__x == __y);}
5211};
5212
5213template <class _CharT, class _Traits, class _IntType>
5214basic_ostream<_CharT, _Traits>&
5215operator<<(basic_ostream<_CharT, _Traits>& __os,
5216           const geometric_distribution<_IntType>& __x)
5217{
5218    __save_flags<_CharT, _Traits> _(__os);
5219    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5220               ios_base::scientific);
5221    return __os << __x.p();
5222}
5223
5224template <class _CharT, class _Traits, class _IntType>
5225basic_istream<_CharT, _Traits>&
5226operator>>(basic_istream<_CharT, _Traits>& __is,
5227           geometric_distribution<_IntType>& __x)
5228{
5229    typedef geometric_distribution<_IntType> _Eng;
5230    typedef typename _Eng::param_type param_type;
5231    __save_flags<_CharT, _Traits> _(__is);
5232    __is.flags(ios_base::dec | ios_base::skipws);
5233    double __p;
5234    __is >> __p;
5235    if (!__is.fail())
5236        __x.param(param_type(__p));
5237    return __is;
5238}
5239
5240// chi_squared_distribution
5241
5242template<class _RealType = double>
5243class _LIBCPP_VISIBLE chi_squared_distribution
5244{
5245public:
5246    // types
5247    typedef _RealType result_type;
5248
5249    class _LIBCPP_VISIBLE param_type
5250    {
5251        result_type __n_;
5252    public:
5253        typedef chi_squared_distribution distribution_type;
5254
5255        _LIBCPP_INLINE_VISIBILITY
5256        explicit param_type(result_type __n = 1) : __n_(__n) {}
5257
5258        _LIBCPP_INLINE_VISIBILITY
5259        result_type n() const {return __n_;}
5260
5261        friend _LIBCPP_INLINE_VISIBILITY
5262            bool operator==(const param_type& __x, const param_type& __y)
5263            {return __x.__n_ == __y.__n_;}
5264        friend _LIBCPP_INLINE_VISIBILITY
5265            bool operator!=(const param_type& __x, const param_type& __y)
5266            {return !(__x == __y);}
5267    };
5268
5269private:
5270    param_type __p_;
5271
5272public:
5273    // constructor and reset functions
5274    _LIBCPP_INLINE_VISIBILITY
5275    explicit chi_squared_distribution(result_type __n = 1)
5276        : __p_(param_type(__n)) {}
5277    _LIBCPP_INLINE_VISIBILITY
5278    explicit chi_squared_distribution(const param_type& __p)
5279        : __p_(__p) {}
5280    _LIBCPP_INLINE_VISIBILITY
5281    void reset() {}
5282
5283    // generating functions
5284    template<class _URNG>
5285        _LIBCPP_INLINE_VISIBILITY
5286        result_type operator()(_URNG& __g)
5287        {return (*this)(__g, __p_);}
5288    template<class _URNG>
5289        _LIBCPP_INLINE_VISIBILITY
5290        result_type operator()(_URNG& __g, const param_type& __p)
5291        {return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);}
5292
5293    // property functions
5294    _LIBCPP_INLINE_VISIBILITY
5295    result_type n() const {return __p_.n();}
5296
5297    _LIBCPP_INLINE_VISIBILITY
5298    param_type param() const {return __p_;}
5299    _LIBCPP_INLINE_VISIBILITY
5300    void param(const param_type& __p) {__p_ = __p;}
5301
5302    _LIBCPP_INLINE_VISIBILITY
5303    result_type min() const {return 0;}
5304    _LIBCPP_INLINE_VISIBILITY
5305    result_type max() const {return numeric_limits<result_type>::infinity();}
5306
5307    friend _LIBCPP_INLINE_VISIBILITY
5308        bool operator==(const chi_squared_distribution& __x,
5309                        const chi_squared_distribution& __y)
5310        {return __x.__p_ == __y.__p_;}
5311    friend _LIBCPP_INLINE_VISIBILITY
5312        bool operator!=(const chi_squared_distribution& __x,
5313                        const chi_squared_distribution& __y)
5314        {return !(__x == __y);}
5315};
5316
5317template <class _CharT, class _Traits, class _RT>
5318basic_ostream<_CharT, _Traits>&
5319operator<<(basic_ostream<_CharT, _Traits>& __os,
5320           const chi_squared_distribution<_RT>& __x)
5321{
5322    __save_flags<_CharT, _Traits> _(__os);
5323    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5324               ios_base::scientific);
5325    __os << __x.n();
5326    return __os;
5327}
5328
5329template <class _CharT, class _Traits, class _RT>
5330basic_istream<_CharT, _Traits>&
5331operator>>(basic_istream<_CharT, _Traits>& __is,
5332           chi_squared_distribution<_RT>& __x)
5333{
5334    typedef chi_squared_distribution<_RT> _Eng;
5335    typedef typename _Eng::result_type result_type;
5336    typedef typename _Eng::param_type param_type;
5337    __save_flags<_CharT, _Traits> _(__is);
5338    __is.flags(ios_base::dec | ios_base::skipws);
5339    result_type __n;
5340    __is >> __n;
5341    if (!__is.fail())
5342        __x.param(param_type(__n));
5343    return __is;
5344}
5345
5346// cauchy_distribution
5347
5348template<class _RealType = double>
5349class _LIBCPP_VISIBLE cauchy_distribution
5350{
5351public:
5352    // types
5353    typedef _RealType result_type;
5354
5355    class _LIBCPP_VISIBLE param_type
5356    {
5357        result_type __a_;
5358        result_type __b_;
5359    public:
5360        typedef cauchy_distribution distribution_type;
5361
5362        _LIBCPP_INLINE_VISIBILITY
5363        explicit param_type(result_type __a = 0, result_type __b = 1)
5364            : __a_(__a), __b_(__b) {}
5365
5366        _LIBCPP_INLINE_VISIBILITY
5367        result_type a() const {return __a_;}
5368        _LIBCPP_INLINE_VISIBILITY
5369        result_type b() const {return __b_;}
5370
5371        friend _LIBCPP_INLINE_VISIBILITY
5372            bool operator==(const param_type& __x, const param_type& __y)
5373            {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
5374        friend _LIBCPP_INLINE_VISIBILITY
5375            bool operator!=(const param_type& __x, const param_type& __y)
5376            {return !(__x == __y);}
5377    };
5378
5379private:
5380    param_type __p_;
5381
5382public:
5383    // constructor and reset functions
5384    _LIBCPP_INLINE_VISIBILITY
5385    explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
5386        : __p_(param_type(__a, __b)) {}
5387    _LIBCPP_INLINE_VISIBILITY
5388    explicit cauchy_distribution(const param_type& __p)
5389        : __p_(__p) {}
5390    _LIBCPP_INLINE_VISIBILITY
5391    void reset() {}
5392
5393    // generating functions
5394    template<class _URNG>
5395        _LIBCPP_INLINE_VISIBILITY
5396        result_type operator()(_URNG& __g)
5397        {return (*this)(__g, __p_);}
5398    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5399
5400    // property functions
5401    _LIBCPP_INLINE_VISIBILITY
5402    result_type a() const {return __p_.a();}
5403    _LIBCPP_INLINE_VISIBILITY
5404    result_type b() const {return __p_.b();}
5405
5406    _LIBCPP_INLINE_VISIBILITY
5407    param_type param() const {return __p_;}
5408    _LIBCPP_INLINE_VISIBILITY
5409    void param(const param_type& __p) {__p_ = __p;}
5410
5411    _LIBCPP_INLINE_VISIBILITY
5412    result_type min() const {return -numeric_limits<result_type>::infinity();}
5413    _LIBCPP_INLINE_VISIBILITY
5414    result_type max() const {return numeric_limits<result_type>::infinity();}
5415
5416    friend _LIBCPP_INLINE_VISIBILITY
5417        bool operator==(const cauchy_distribution& __x,
5418                        const cauchy_distribution& __y)
5419        {return __x.__p_ == __y.__p_;}
5420    friend _LIBCPP_INLINE_VISIBILITY
5421        bool operator!=(const cauchy_distribution& __x,
5422                        const cauchy_distribution& __y)
5423        {return !(__x == __y);}
5424};
5425
5426template <class _RealType>
5427template<class _URNG>
5428inline _LIBCPP_INLINE_VISIBILITY
5429_RealType
5430cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5431{
5432    uniform_real_distribution<result_type> __gen;
5433    // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
5434    return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
5435}
5436
5437template <class _CharT, class _Traits, class _RT>
5438basic_ostream<_CharT, _Traits>&
5439operator<<(basic_ostream<_CharT, _Traits>& __os,
5440           const cauchy_distribution<_RT>& __x)
5441{
5442    __save_flags<_CharT, _Traits> _(__os);
5443    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5444               ios_base::scientific);
5445    _CharT __sp = __os.widen(' ');
5446    __os.fill(__sp);
5447    __os << __x.a() << __sp << __x.b();
5448    return __os;
5449}
5450
5451template <class _CharT, class _Traits, class _RT>
5452basic_istream<_CharT, _Traits>&
5453operator>>(basic_istream<_CharT, _Traits>& __is,
5454           cauchy_distribution<_RT>& __x)
5455{
5456    typedef cauchy_distribution<_RT> _Eng;
5457    typedef typename _Eng::result_type result_type;
5458    typedef typename _Eng::param_type param_type;
5459    __save_flags<_CharT, _Traits> _(__is);
5460    __is.flags(ios_base::dec | ios_base::skipws);
5461    result_type __a;
5462    result_type __b;
5463    __is >> __a >> __b;
5464    if (!__is.fail())
5465        __x.param(param_type(__a, __b));
5466    return __is;
5467}
5468
5469// fisher_f_distribution
5470
5471template<class _RealType = double>
5472class _LIBCPP_VISIBLE fisher_f_distribution
5473{
5474public:
5475    // types
5476    typedef _RealType result_type;
5477
5478    class _LIBCPP_VISIBLE param_type
5479    {
5480        result_type __m_;
5481        result_type __n_;
5482    public:
5483        typedef fisher_f_distribution distribution_type;
5484
5485        _LIBCPP_INLINE_VISIBILITY
5486        explicit param_type(result_type __m = 1, result_type __n = 1)
5487            : __m_(__m), __n_(__n) {}
5488
5489        _LIBCPP_INLINE_VISIBILITY
5490        result_type m() const {return __m_;}
5491        _LIBCPP_INLINE_VISIBILITY
5492        result_type n() const {return __n_;}
5493
5494        friend _LIBCPP_INLINE_VISIBILITY
5495            bool operator==(const param_type& __x, const param_type& __y)
5496            {return __x.__m_ == __y.__m_ && __x.__n_ == __y.__n_;}
5497        friend _LIBCPP_INLINE_VISIBILITY
5498            bool operator!=(const param_type& __x, const param_type& __y)
5499            {return !(__x == __y);}
5500    };
5501
5502private:
5503    param_type __p_;
5504
5505public:
5506    // constructor and reset functions
5507    _LIBCPP_INLINE_VISIBILITY
5508    explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
5509        : __p_(param_type(__m, __n)) {}
5510    _LIBCPP_INLINE_VISIBILITY
5511    explicit fisher_f_distribution(const param_type& __p)
5512        : __p_(__p) {}
5513    _LIBCPP_INLINE_VISIBILITY
5514    void reset() {}
5515
5516    // generating functions
5517    template<class _URNG>
5518        _LIBCPP_INLINE_VISIBILITY
5519        result_type operator()(_URNG& __g)
5520        {return (*this)(__g, __p_);}
5521    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5522
5523    // property functions
5524    _LIBCPP_INLINE_VISIBILITY
5525    result_type m() const {return __p_.m();}
5526    _LIBCPP_INLINE_VISIBILITY
5527    result_type n() const {return __p_.n();}
5528
5529    _LIBCPP_INLINE_VISIBILITY
5530    param_type param() const {return __p_;}
5531    _LIBCPP_INLINE_VISIBILITY
5532    void param(const param_type& __p) {__p_ = __p;}
5533
5534    _LIBCPP_INLINE_VISIBILITY
5535    result_type min() const {return 0;}
5536    _LIBCPP_INLINE_VISIBILITY
5537    result_type max() const {return numeric_limits<result_type>::infinity();}
5538
5539    friend _LIBCPP_INLINE_VISIBILITY
5540        bool operator==(const fisher_f_distribution& __x,
5541                        const fisher_f_distribution& __y)
5542        {return __x.__p_ == __y.__p_;}
5543    friend _LIBCPP_INLINE_VISIBILITY
5544        bool operator!=(const fisher_f_distribution& __x,
5545                        const fisher_f_distribution& __y)
5546        {return !(__x == __y);}
5547};
5548
5549template <class _RealType>
5550template<class _URNG>
5551_RealType
5552fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5553{
5554    gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
5555    gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
5556    return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
5557}
5558
5559template <class _CharT, class _Traits, class _RT>
5560basic_ostream<_CharT, _Traits>&
5561operator<<(basic_ostream<_CharT, _Traits>& __os,
5562           const fisher_f_distribution<_RT>& __x)
5563{
5564    __save_flags<_CharT, _Traits> _(__os);
5565    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5566               ios_base::scientific);
5567    _CharT __sp = __os.widen(' ');
5568    __os.fill(__sp);
5569    __os << __x.m() << __sp << __x.n();
5570    return __os;
5571}
5572
5573template <class _CharT, class _Traits, class _RT>
5574basic_istream<_CharT, _Traits>&
5575operator>>(basic_istream<_CharT, _Traits>& __is,
5576           fisher_f_distribution<_RT>& __x)
5577{
5578    typedef fisher_f_distribution<_RT> _Eng;
5579    typedef typename _Eng::result_type result_type;
5580    typedef typename _Eng::param_type param_type;
5581    __save_flags<_CharT, _Traits> _(__is);
5582    __is.flags(ios_base::dec | ios_base::skipws);
5583    result_type __m;
5584    result_type __n;
5585    __is >> __m >> __n;
5586    if (!__is.fail())
5587        __x.param(param_type(__m, __n));
5588    return __is;
5589}
5590
5591// student_t_distribution
5592
5593template<class _RealType = double>
5594class _LIBCPP_VISIBLE student_t_distribution
5595{
5596public:
5597    // types
5598    typedef _RealType result_type;
5599
5600    class _LIBCPP_VISIBLE param_type
5601    {
5602        result_type __n_;
5603    public:
5604        typedef student_t_distribution distribution_type;
5605
5606        _LIBCPP_INLINE_VISIBILITY
5607        explicit param_type(result_type __n = 1) : __n_(__n) {}
5608
5609        _LIBCPP_INLINE_VISIBILITY
5610        result_type n() const {return __n_;}
5611
5612        friend _LIBCPP_INLINE_VISIBILITY
5613            bool operator==(const param_type& __x, const param_type& __y)
5614            {return __x.__n_ == __y.__n_;}
5615        friend _LIBCPP_INLINE_VISIBILITY
5616            bool operator!=(const param_type& __x, const param_type& __y)
5617            {return !(__x == __y);}
5618    };
5619
5620private:
5621    param_type __p_;
5622    normal_distribution<result_type> __nd_;
5623
5624public:
5625    // constructor and reset functions
5626    _LIBCPP_INLINE_VISIBILITY
5627    explicit student_t_distribution(result_type __n = 1)
5628        : __p_(param_type(__n)) {}
5629    _LIBCPP_INLINE_VISIBILITY
5630    explicit student_t_distribution(const param_type& __p)
5631        : __p_(__p) {}
5632    _LIBCPP_INLINE_VISIBILITY
5633    void reset() {__nd_.reset();}
5634
5635    // generating functions
5636    template<class _URNG>
5637        _LIBCPP_INLINE_VISIBILITY
5638        result_type operator()(_URNG& __g)
5639        {return (*this)(__g, __p_);}
5640    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5641
5642    // property functions
5643    _LIBCPP_INLINE_VISIBILITY
5644    result_type n() const {return __p_.n();}
5645
5646    _LIBCPP_INLINE_VISIBILITY
5647    param_type param() const {return __p_;}
5648    _LIBCPP_INLINE_VISIBILITY
5649    void param(const param_type& __p) {__p_ = __p;}
5650
5651    _LIBCPP_INLINE_VISIBILITY
5652    result_type min() const {return -numeric_limits<result_type>::infinity();}
5653    _LIBCPP_INLINE_VISIBILITY
5654    result_type max() const {return numeric_limits<result_type>::infinity();}
5655
5656    friend _LIBCPP_INLINE_VISIBILITY
5657        bool operator==(const student_t_distribution& __x,
5658                        const student_t_distribution& __y)
5659        {return __x.__p_ == __y.__p_;}
5660    friend _LIBCPP_INLINE_VISIBILITY
5661        bool operator!=(const student_t_distribution& __x,
5662                        const student_t_distribution& __y)
5663        {return !(__x == __y);}
5664};
5665
5666template <class _RealType>
5667template<class _URNG>
5668_RealType
5669student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
5670{
5671    gamma_distribution<result_type> __gd(__p.n() * .5, 2);
5672    return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
5673}
5674
5675template <class _CharT, class _Traits, class _RT>
5676basic_ostream<_CharT, _Traits>&
5677operator<<(basic_ostream<_CharT, _Traits>& __os,
5678           const student_t_distribution<_RT>& __x)
5679{
5680    __save_flags<_CharT, _Traits> _(__os);
5681    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5682               ios_base::scientific);
5683    __os << __x.n();
5684    return __os;
5685}
5686
5687template <class _CharT, class _Traits, class _RT>
5688basic_istream<_CharT, _Traits>&
5689operator>>(basic_istream<_CharT, _Traits>& __is,
5690           student_t_distribution<_RT>& __x)
5691{
5692    typedef student_t_distribution<_RT> _Eng;
5693    typedef typename _Eng::result_type result_type;
5694    typedef typename _Eng::param_type param_type;
5695    __save_flags<_CharT, _Traits> _(__is);
5696    __is.flags(ios_base::dec | ios_base::skipws);
5697    result_type __n;
5698    __is >> __n;
5699    if (!__is.fail())
5700        __x.param(param_type(__n));
5701    return __is;
5702}
5703
5704// discrete_distribution
5705
5706template<class _IntType = int>
5707class _LIBCPP_VISIBLE discrete_distribution
5708{
5709public:
5710    // types
5711    typedef _IntType result_type;
5712
5713    class _LIBCPP_VISIBLE param_type
5714    {
5715        vector<double> __p_;
5716    public:
5717        typedef discrete_distribution distribution_type;
5718
5719        _LIBCPP_INLINE_VISIBILITY
5720        param_type() {}
5721        template<class _InputIterator>
5722            _LIBCPP_INLINE_VISIBILITY
5723            param_type(_InputIterator __f, _InputIterator __l)
5724            : __p_(__f, __l) {__init();}
5725#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
5726        _LIBCPP_INLINE_VISIBILITY
5727        param_type(initializer_list<double> __wl)
5728            : __p_(__wl.begin(), __wl.end()) {__init();}
5729#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
5730        template<class _UnaryOperation>
5731            param_type(size_t __nw, double __xmin, double __xmax,
5732                       _UnaryOperation __fw);
5733
5734        vector<double> probabilities() const;
5735
5736        friend _LIBCPP_INLINE_VISIBILITY
5737            bool operator==(const param_type& __x, const param_type& __y)
5738            {return __x.__p_ == __y.__p_;}
5739        friend _LIBCPP_INLINE_VISIBILITY
5740            bool operator!=(const param_type& __x, const param_type& __y)
5741            {return !(__x == __y);}
5742
5743    private:
5744        void __init();
5745
5746        friend class discrete_distribution;
5747
5748        template <class _CharT, class _Traits, class _IT>
5749        friend
5750        basic_ostream<_CharT, _Traits>&
5751        operator<<(basic_ostream<_CharT, _Traits>& __os,
5752                   const discrete_distribution<_IT>& __x);
5753
5754        template <class _CharT, class _Traits, class _IT>
5755        friend
5756        basic_istream<_CharT, _Traits>&
5757        operator>>(basic_istream<_CharT, _Traits>& __is,
5758                   discrete_distribution<_IT>& __x);
5759    };
5760
5761private:
5762    param_type __p_;
5763
5764public:
5765    // constructor and reset functions
5766    _LIBCPP_INLINE_VISIBILITY
5767    discrete_distribution() {}
5768    template<class _InputIterator>
5769        _LIBCPP_INLINE_VISIBILITY
5770        discrete_distribution(_InputIterator __f, _InputIterator __l)
5771            : __p_(__f, __l) {}
5772#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
5773    _LIBCPP_INLINE_VISIBILITY
5774    discrete_distribution(initializer_list<double> __wl)
5775        : __p_(__wl) {}
5776#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
5777    template<class _UnaryOperation>
5778        _LIBCPP_INLINE_VISIBILITY
5779        discrete_distribution(size_t __nw, double __xmin, double __xmax,
5780                              _UnaryOperation __fw)
5781        : __p_(__nw, __xmin, __xmax, __fw) {}
5782    explicit discrete_distribution(const param_type& __p)
5783    _LIBCPP_INLINE_VISIBILITY
5784        : __p_(__p) {}
5785    _LIBCPP_INLINE_VISIBILITY
5786    void reset() {}
5787
5788    // generating functions
5789    template<class _URNG>
5790        _LIBCPP_INLINE_VISIBILITY
5791        result_type operator()(_URNG& __g)
5792        {return (*this)(__g, __p_);}
5793    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
5794
5795    // property functions
5796    _LIBCPP_INLINE_VISIBILITY
5797    vector<double> probabilities() const {return __p_.probabilities();}
5798
5799    _LIBCPP_INLINE_VISIBILITY
5800    param_type param() const {return __p_;}
5801    _LIBCPP_INLINE_VISIBILITY
5802    void param(const param_type& __p) {__p_ = __p;}
5803
5804    _LIBCPP_INLINE_VISIBILITY
5805    result_type min() const {return 0;}
5806    _LIBCPP_INLINE_VISIBILITY
5807    result_type max() const {return __p_.__p_.size();}
5808
5809    friend _LIBCPP_INLINE_VISIBILITY
5810        bool operator==(const discrete_distribution& __x,
5811                        const discrete_distribution& __y)
5812        {return __x.__p_ == __y.__p_;}
5813    friend _LIBCPP_INLINE_VISIBILITY
5814        bool operator!=(const discrete_distribution& __x,
5815                        const discrete_distribution& __y)
5816        {return !(__x == __y);}
5817
5818    template <class _CharT, class _Traits, class _IT>
5819    friend
5820    basic_ostream<_CharT, _Traits>&
5821    operator<<(basic_ostream<_CharT, _Traits>& __os,
5822               const discrete_distribution<_IT>& __x);
5823
5824    template <class _CharT, class _Traits, class _IT>
5825    friend
5826    basic_istream<_CharT, _Traits>&
5827    operator>>(basic_istream<_CharT, _Traits>& __is,
5828               discrete_distribution<_IT>& __x);
5829};
5830
5831template<class _IntType>
5832template<class _UnaryOperation>
5833discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
5834                                                        double __xmin,
5835                                                        double __xmax,
5836                                                        _UnaryOperation __fw)
5837{
5838    if (__nw > 1)
5839    {
5840        __p_.reserve(__nw - 1);
5841        double __d = (__xmax - __xmin) / __nw;
5842        double __d2 = __d / 2;
5843        for (size_t __k = 0; __k < __nw; ++__k)
5844            __p_.push_back(__fw(__xmin + __k * __d + __d2));
5845        __init();
5846    }
5847}
5848
5849template<class _IntType>
5850void
5851discrete_distribution<_IntType>::param_type::__init()
5852{
5853    if (!__p_.empty())
5854    {
5855        if (__p_.size() > 1)
5856        {
5857            double __s = _VSTD::accumulate(__p_.begin(), __p_.end(), 0.0);
5858            for (_VSTD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
5859                                                                       __i < __e; ++__i)
5860                *__i /= __s;
5861            vector<double> __t(__p_.size() - 1);
5862            _VSTD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
5863            swap(__p_, __t);
5864        }
5865        else
5866        {
5867            __p_.clear();
5868            __p_.shrink_to_fit();
5869        }
5870    }
5871}
5872
5873template<class _IntType>
5874vector<double>
5875discrete_distribution<_IntType>::param_type::probabilities() const
5876{
5877    size_t __n = __p_.size();
5878    _VSTD::vector<double> __p(__n+1);
5879    _VSTD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
5880    if (__n > 0)
5881        __p[__n] = 1 - __p_[__n-1];
5882    else
5883        __p[0] = 1;
5884    return __p;
5885}
5886
5887template<class _IntType>
5888template<class _URNG>
5889_IntType
5890discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
5891{
5892    uniform_real_distribution<double> __gen;
5893    return static_cast<_IntType>(
5894           _VSTD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
5895                                                              __p.__p_.begin());
5896}
5897
5898template <class _CharT, class _Traits, class _IT>
5899basic_ostream<_CharT, _Traits>&
5900operator<<(basic_ostream<_CharT, _Traits>& __os,
5901           const discrete_distribution<_IT>& __x)
5902{
5903    __save_flags<_CharT, _Traits> _(__os);
5904    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
5905               ios_base::scientific);
5906    _CharT __sp = __os.widen(' ');
5907    __os.fill(__sp);
5908    size_t __n = __x.__p_.__p_.size();
5909    __os << __n;
5910    for (size_t __i = 0; __i < __n; ++__i)
5911        __os << __sp << __x.__p_.__p_[__i];
5912    return __os;
5913}
5914
5915template <class _CharT, class _Traits, class _IT>
5916basic_istream<_CharT, _Traits>&
5917operator>>(basic_istream<_CharT, _Traits>& __is,
5918           discrete_distribution<_IT>& __x)
5919{
5920    typedef discrete_distribution<_IT> _Eng;
5921    typedef typename _Eng::result_type result_type;
5922    typedef typename _Eng::param_type param_type;
5923    __save_flags<_CharT, _Traits> _(__is);
5924    __is.flags(ios_base::dec | ios_base::skipws);
5925    size_t __n;
5926    __is >> __n;
5927    vector<double> __p(__n);
5928    for (size_t __i = 0; __i < __n; ++__i)
5929        __is >> __p[__i];
5930    if (!__is.fail())
5931        swap(__x.__p_.__p_, __p);
5932    return __is;
5933}
5934
5935// piecewise_constant_distribution
5936
5937template<class _RealType = double>
5938class _LIBCPP_VISIBLE piecewise_constant_distribution
5939{
5940public:
5941    // types
5942    typedef _RealType result_type;
5943
5944    class _LIBCPP_VISIBLE param_type
5945    {
5946        vector<result_type> __b_;
5947        vector<result_type> __densities_;
5948        vector<result_type> __areas_;
5949    public:
5950        typedef piecewise_constant_distribution distribution_type;
5951
5952        param_type();
5953        template<class _InputIteratorB, class _InputIteratorW>
5954            param_type(_InputIteratorB __fB, _InputIteratorB __lB,
5955                       _InputIteratorW __fW);
5956#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
5957        template<class _UnaryOperation>
5958            param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
5959#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
5960        template<class _UnaryOperation>
5961            param_type(size_t __nw, result_type __xmin, result_type __xmax,
5962                       _UnaryOperation __fw);
5963        param_type & operator=(const param_type& __rhs);
5964
5965        _LIBCPP_INLINE_VISIBILITY
5966        vector<result_type> intervals() const {return __b_;}
5967        _LIBCPP_INLINE_VISIBILITY
5968        vector<result_type> densities() const {return __densities_;}
5969
5970        friend _LIBCPP_INLINE_VISIBILITY
5971            bool operator==(const param_type& __x, const param_type& __y)
5972            {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
5973        friend _LIBCPP_INLINE_VISIBILITY
5974            bool operator!=(const param_type& __x, const param_type& __y)
5975            {return !(__x == __y);}
5976
5977    private:
5978        void __init();
5979
5980        friend class piecewise_constant_distribution;
5981
5982        template <class _CharT, class _Traits, class _RT>
5983        friend
5984        basic_ostream<_CharT, _Traits>&
5985        operator<<(basic_ostream<_CharT, _Traits>& __os,
5986                   const piecewise_constant_distribution<_RT>& __x);
5987
5988        template <class _CharT, class _Traits, class _RT>
5989        friend
5990        basic_istream<_CharT, _Traits>&
5991        operator>>(basic_istream<_CharT, _Traits>& __is,
5992                   piecewise_constant_distribution<_RT>& __x);
5993    };
5994
5995private:
5996    param_type __p_;
5997
5998public:
5999    // constructor and reset functions
6000    _LIBCPP_INLINE_VISIBILITY
6001    piecewise_constant_distribution() {}
6002    template<class _InputIteratorB, class _InputIteratorW>
6003        _LIBCPP_INLINE_VISIBILITY
6004        piecewise_constant_distribution(_InputIteratorB __fB,
6005                                        _InputIteratorB __lB,
6006                                        _InputIteratorW __fW)
6007        : __p_(__fB, __lB, __fW) {}
6008
6009#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6010    template<class _UnaryOperation>
6011        _LIBCPP_INLINE_VISIBILITY
6012        piecewise_constant_distribution(initializer_list<result_type> __bl,
6013                                        _UnaryOperation __fw)
6014        : __p_(__bl, __fw) {}
6015#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6016
6017    template<class _UnaryOperation>
6018        _LIBCPP_INLINE_VISIBILITY
6019        piecewise_constant_distribution(size_t __nw, result_type __xmin,
6020                                        result_type __xmax, _UnaryOperation __fw)
6021        : __p_(__nw, __xmin, __xmax, __fw) {}
6022
6023    _LIBCPP_INLINE_VISIBILITY
6024    explicit piecewise_constant_distribution(const param_type& __p)
6025        : __p_(__p) {}
6026
6027    _LIBCPP_INLINE_VISIBILITY
6028    void reset() {}
6029
6030    // generating functions
6031    template<class _URNG>
6032        _LIBCPP_INLINE_VISIBILITY
6033        result_type operator()(_URNG& __g)
6034        {return (*this)(__g, __p_);}
6035    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6036
6037    // property functions
6038    _LIBCPP_INLINE_VISIBILITY
6039    vector<result_type> intervals() const {return __p_.intervals();}
6040    _LIBCPP_INLINE_VISIBILITY
6041    vector<result_type> densities() const {return __p_.densities();}
6042
6043    _LIBCPP_INLINE_VISIBILITY
6044    param_type param() const {return __p_;}
6045    _LIBCPP_INLINE_VISIBILITY
6046    void param(const param_type& __p) {__p_ = __p;}
6047
6048    _LIBCPP_INLINE_VISIBILITY
6049    result_type min() const {return __p_.__b_.front();}
6050    _LIBCPP_INLINE_VISIBILITY
6051    result_type max() const {return __p_.__b_.back();}
6052
6053    friend _LIBCPP_INLINE_VISIBILITY
6054        bool operator==(const piecewise_constant_distribution& __x,
6055                        const piecewise_constant_distribution& __y)
6056        {return __x.__p_ == __y.__p_;}
6057    friend _LIBCPP_INLINE_VISIBILITY
6058        bool operator!=(const piecewise_constant_distribution& __x,
6059                           const piecewise_constant_distribution& __y)
6060        {return !(__x == __y);}
6061
6062    template <class _CharT, class _Traits, class _RT>
6063    friend
6064    basic_ostream<_CharT, _Traits>&
6065    operator<<(basic_ostream<_CharT, _Traits>& __os,
6066               const piecewise_constant_distribution<_RT>& __x);
6067
6068    template <class _CharT, class _Traits, class _RT>
6069    friend
6070    basic_istream<_CharT, _Traits>&
6071    operator>>(basic_istream<_CharT, _Traits>& __is,
6072               piecewise_constant_distribution<_RT>& __x);
6073};
6074
6075template<class _RealType>
6076typename piecewise_constant_distribution<_RealType>::param_type &
6077piecewise_constant_distribution<_RealType>::param_type::operator=
6078                                                       (const param_type& __rhs)
6079{
6080//  These can throw
6081    __b_.reserve        (__rhs.__b_.size ());
6082    __densities_.reserve(__rhs.__densities_.size());
6083    __areas_.reserve    (__rhs.__areas_.size());
6084
6085//  These can not throw
6086    __b_         = __rhs.__b_;
6087    __densities_ = __rhs.__densities_;
6088    __areas_     =  __rhs.__areas_;
6089    return *this;
6090}
6091
6092template<class _RealType>
6093void
6094piecewise_constant_distribution<_RealType>::param_type::__init()
6095{
6096    // __densities_ contains non-normalized areas
6097    result_type __total_area = _VSTD::accumulate(__densities_.begin(),
6098                                                __densities_.end(),
6099                                                result_type());
6100    for (size_t __i = 0; __i < __densities_.size(); ++__i)
6101        __densities_[__i] /= __total_area;
6102    // __densities_ contains normalized areas
6103    __areas_.assign(__densities_.size(), result_type());
6104    _VSTD::partial_sum(__densities_.begin(), __densities_.end() - 1,
6105                                                          __areas_.begin() + 1);
6106    // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
6107    __densities_.back() = 1 - __areas_.back();  // correct round off error
6108    for (size_t __i = 0; __i < __densities_.size(); ++__i)
6109        __densities_[__i] /= (__b_[__i+1] - __b_[__i]);
6110    // __densities_ now contains __densities_
6111}
6112
6113template<class _RealType>
6114piecewise_constant_distribution<_RealType>::param_type::param_type()
6115    : __b_(2),
6116      __densities_(1, 1.0),
6117      __areas_(1, 0.0)
6118{
6119    __b_[1] = 1;
6120}
6121
6122template<class _RealType>
6123template<class _InputIteratorB, class _InputIteratorW>
6124piecewise_constant_distribution<_RealType>::param_type::param_type(
6125        _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6126    : __b_(__fB, __lB)
6127{
6128    if (__b_.size() < 2)
6129    {
6130        __b_.resize(2);
6131        __b_[0] = 0;
6132        __b_[1] = 1;
6133        __densities_.assign(1, 1.0);
6134        __areas_.assign(1, 0.0);
6135    }
6136    else
6137    {
6138        __densities_.reserve(__b_.size() - 1);
6139        for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__fW)
6140            __densities_.push_back(*__fW);
6141        __init();
6142    }
6143}
6144
6145#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6146
6147template<class _RealType>
6148template<class _UnaryOperation>
6149piecewise_constant_distribution<_RealType>::param_type::param_type(
6150        initializer_list<result_type> __bl, _UnaryOperation __fw)
6151    : __b_(__bl.begin(), __bl.end())
6152{
6153    if (__b_.size() < 2)
6154    {
6155        __b_.resize(2);
6156        __b_[0] = 0;
6157        __b_[1] = 1;
6158        __densities_.assign(1, 1.0);
6159        __areas_.assign(1, 0.0);
6160    }
6161    else
6162    {
6163        __densities_.reserve(__b_.size() - 1);
6164        for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
6165            __densities_.push_back(__fw((__b_[__i+1] + __b_[__i])*.5));
6166        __init();
6167    }
6168}
6169
6170#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6171
6172template<class _RealType>
6173template<class _UnaryOperation>
6174piecewise_constant_distribution<_RealType>::param_type::param_type(
6175        size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6176    : __b_(__nw == 0 ? 2 : __nw + 1)
6177{
6178    size_t __n = __b_.size() - 1;
6179    result_type __d = (__xmax - __xmin) / __n;
6180    __densities_.reserve(__n);
6181    for (size_t __i = 0; __i < __n; ++__i)
6182    {
6183        __b_[__i] = __xmin + __i * __d;
6184        __densities_.push_back(__fw(__b_[__i] + __d*.5));
6185    }
6186    __b_[__n] = __xmax;
6187    __init();
6188}
6189
6190template<class _RealType>
6191template<class _URNG>
6192_RealType
6193piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6194{
6195    typedef uniform_real_distribution<result_type> _Gen;
6196    result_type __u = _Gen()(__g);
6197    ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
6198                                      __u) - __p.__areas_.begin() - 1;
6199    return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
6200}
6201
6202template <class _CharT, class _Traits, class _RT>
6203basic_ostream<_CharT, _Traits>&
6204operator<<(basic_ostream<_CharT, _Traits>& __os,
6205           const piecewise_constant_distribution<_RT>& __x)
6206{
6207    __save_flags<_CharT, _Traits> _(__os);
6208    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6209               ios_base::scientific);
6210    _CharT __sp = __os.widen(' ');
6211    __os.fill(__sp);
6212    size_t __n = __x.__p_.__b_.size();
6213    __os << __n;
6214    for (size_t __i = 0; __i < __n; ++__i)
6215        __os << __sp << __x.__p_.__b_[__i];
6216    __n = __x.__p_.__densities_.size();
6217    __os << __sp << __n;
6218    for (size_t __i = 0; __i < __n; ++__i)
6219        __os << __sp << __x.__p_.__densities_[__i];
6220    __n = __x.__p_.__areas_.size();
6221    __os << __sp << __n;
6222    for (size_t __i = 0; __i < __n; ++__i)
6223        __os << __sp << __x.__p_.__areas_[__i];
6224    return __os;
6225}
6226
6227template <class _CharT, class _Traits, class _RT>
6228basic_istream<_CharT, _Traits>&
6229operator>>(basic_istream<_CharT, _Traits>& __is,
6230           piecewise_constant_distribution<_RT>& __x)
6231{
6232    typedef piecewise_constant_distribution<_RT> _Eng;
6233    typedef typename _Eng::result_type result_type;
6234    typedef typename _Eng::param_type param_type;
6235    __save_flags<_CharT, _Traits> _(__is);
6236    __is.flags(ios_base::dec | ios_base::skipws);
6237    size_t __n;
6238    __is >> __n;
6239    vector<result_type> __b(__n);
6240    for (size_t __i = 0; __i < __n; ++__i)
6241        __is >> __b[__i];
6242    __is >> __n;
6243    vector<result_type> __densities(__n);
6244    for (size_t __i = 0; __i < __n; ++__i)
6245        __is >> __densities[__i];
6246    __is >> __n;
6247    vector<result_type> __areas(__n);
6248    for (size_t __i = 0; __i < __n; ++__i)
6249        __is >> __areas[__i];
6250    if (!__is.fail())
6251    {
6252        swap(__x.__p_.__b_, __b);
6253        swap(__x.__p_.__densities_, __densities);
6254        swap(__x.__p_.__areas_, __areas);
6255    }
6256    return __is;
6257}
6258
6259// piecewise_linear_distribution
6260
6261template<class _RealType = double>
6262class _LIBCPP_VISIBLE piecewise_linear_distribution
6263{
6264public:
6265    // types
6266    typedef _RealType result_type;
6267
6268    class _LIBCPP_VISIBLE param_type
6269    {
6270        vector<result_type> __b_;
6271        vector<result_type> __densities_;
6272        vector<result_type> __areas_;
6273    public:
6274        typedef piecewise_linear_distribution distribution_type;
6275
6276        param_type();
6277        template<class _InputIteratorB, class _InputIteratorW>
6278            param_type(_InputIteratorB __fB, _InputIteratorB __lB,
6279                       _InputIteratorW __fW);
6280#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6281        template<class _UnaryOperation>
6282            param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
6283#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6284        template<class _UnaryOperation>
6285            param_type(size_t __nw, result_type __xmin, result_type __xmax,
6286                       _UnaryOperation __fw);
6287        param_type & operator=(const param_type& __rhs);
6288        
6289        _LIBCPP_INLINE_VISIBILITY
6290        vector<result_type> intervals() const {return __b_;}
6291        _LIBCPP_INLINE_VISIBILITY
6292        vector<result_type> densities() const {return __densities_;}
6293
6294        friend _LIBCPP_INLINE_VISIBILITY
6295            bool operator==(const param_type& __x, const param_type& __y)
6296            {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;}
6297        friend _LIBCPP_INLINE_VISIBILITY
6298            bool operator!=(const param_type& __x, const param_type& __y)
6299            {return !(__x == __y);}
6300
6301    private:
6302        void __init();
6303
6304        friend class piecewise_linear_distribution;
6305
6306        template <class _CharT, class _Traits, class _RT>
6307        friend
6308        basic_ostream<_CharT, _Traits>&
6309        operator<<(basic_ostream<_CharT, _Traits>& __os,
6310                   const piecewise_linear_distribution<_RT>& __x);
6311
6312        template <class _CharT, class _Traits, class _RT>
6313        friend
6314        basic_istream<_CharT, _Traits>&
6315        operator>>(basic_istream<_CharT, _Traits>& __is,
6316                   piecewise_linear_distribution<_RT>& __x);
6317    };
6318
6319private:
6320    param_type __p_;
6321
6322public:
6323    // constructor and reset functions
6324    _LIBCPP_INLINE_VISIBILITY
6325    piecewise_linear_distribution() {}
6326    template<class _InputIteratorB, class _InputIteratorW>
6327        _LIBCPP_INLINE_VISIBILITY
6328        piecewise_linear_distribution(_InputIteratorB __fB,
6329                                      _InputIteratorB __lB,
6330                                      _InputIteratorW __fW)
6331        : __p_(__fB, __lB, __fW) {}
6332
6333#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6334    template<class _UnaryOperation>
6335        _LIBCPP_INLINE_VISIBILITY
6336        piecewise_linear_distribution(initializer_list<result_type> __bl,
6337                                      _UnaryOperation __fw)
6338        : __p_(__bl, __fw) {}
6339#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6340
6341    template<class _UnaryOperation>
6342        _LIBCPP_INLINE_VISIBILITY
6343        piecewise_linear_distribution(size_t __nw, result_type __xmin,
6344                                      result_type __xmax, _UnaryOperation __fw)
6345        : __p_(__nw, __xmin, __xmax, __fw) {}
6346
6347    _LIBCPP_INLINE_VISIBILITY
6348    explicit piecewise_linear_distribution(const param_type& __p)
6349        : __p_(__p) {}
6350
6351    _LIBCPP_INLINE_VISIBILITY
6352    void reset() {}
6353
6354    // generating functions
6355    template<class _URNG>
6356        _LIBCPP_INLINE_VISIBILITY
6357        result_type operator()(_URNG& __g)
6358        {return (*this)(__g, __p_);}
6359    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
6360
6361    // property functions
6362    _LIBCPP_INLINE_VISIBILITY
6363    vector<result_type> intervals() const {return __p_.intervals();}
6364    _LIBCPP_INLINE_VISIBILITY
6365    vector<result_type> densities() const {return __p_.densities();}
6366
6367    _LIBCPP_INLINE_VISIBILITY
6368    param_type param() const {return __p_;}
6369    _LIBCPP_INLINE_VISIBILITY
6370    void param(const param_type& __p) {__p_ = __p;}
6371
6372    _LIBCPP_INLINE_VISIBILITY
6373    result_type min() const {return __p_.__b_.front();}
6374    _LIBCPP_INLINE_VISIBILITY
6375    result_type max() const {return __p_.__b_.back();}
6376
6377    friend _LIBCPP_INLINE_VISIBILITY
6378        bool operator==(const piecewise_linear_distribution& __x,
6379                        const piecewise_linear_distribution& __y)
6380        {return __x.__p_ == __y.__p_;}
6381    friend _LIBCPP_INLINE_VISIBILITY
6382        bool operator!=(const piecewise_linear_distribution& __x,
6383                        const piecewise_linear_distribution& __y)
6384        {return !(__x == __y);}
6385
6386    template <class _CharT, class _Traits, class _RT>
6387    friend
6388    basic_ostream<_CharT, _Traits>&
6389    operator<<(basic_ostream<_CharT, _Traits>& __os,
6390               const piecewise_linear_distribution<_RT>& __x);
6391
6392    template <class _CharT, class _Traits, class _RT>
6393    friend
6394    basic_istream<_CharT, _Traits>&
6395    operator>>(basic_istream<_CharT, _Traits>& __is,
6396               piecewise_linear_distribution<_RT>& __x);
6397};
6398
6399template<class _RealType>
6400typename piecewise_linear_distribution<_RealType>::param_type &
6401piecewise_linear_distribution<_RealType>::param_type::operator=
6402                                                       (const param_type& __rhs)
6403{
6404//  These can throw
6405    __b_.reserve        (__rhs.__b_.size ());
6406    __densities_.reserve(__rhs.__densities_.size());
6407    __areas_.reserve    (__rhs.__areas_.size());
6408
6409//  These can not throw
6410    __b_         = __rhs.__b_;
6411    __densities_ = __rhs.__densities_;
6412    __areas_     =  __rhs.__areas_;
6413    return *this;
6414}
6415
6416
6417template<class _RealType>
6418void
6419piecewise_linear_distribution<_RealType>::param_type::__init()
6420{
6421    __areas_.assign(__densities_.size() - 1, result_type());
6422    result_type _S = 0;
6423    for (size_t __i = 0; __i < __areas_.size(); ++__i)
6424    {
6425        __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) *
6426                        (__b_[__i+1] - __b_[__i]) * .5;
6427        _S += __areas_[__i];
6428    }
6429    for (size_t __i = __areas_.size(); __i > 1;)
6430    {
6431        --__i;
6432        __areas_[__i] = __areas_[__i-1] / _S;
6433    }
6434    __areas_[0] = 0;
6435    for (size_t __i = 1; __i < __areas_.size(); ++__i)
6436        __areas_[__i] += __areas_[__i-1];
6437    for (size_t __i = 0; __i < __densities_.size(); ++__i)
6438        __densities_[__i] /= _S;
6439}
6440
6441template<class _RealType>
6442piecewise_linear_distribution<_RealType>::param_type::param_type()
6443    : __b_(2),
6444      __densities_(2, 1.0),
6445      __areas_(1, 0.0)
6446{
6447    __b_[1] = 1;
6448}
6449
6450template<class _RealType>
6451template<class _InputIteratorB, class _InputIteratorW>
6452piecewise_linear_distribution<_RealType>::param_type::param_type(
6453        _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW)
6454    : __b_(__fB, __lB)
6455{
6456    if (__b_.size() < 2)
6457    {
6458        __b_.resize(2);
6459        __b_[0] = 0;
6460        __b_[1] = 1;
6461        __densities_.assign(2, 1.0);
6462        __areas_.assign(1, 0.0);
6463    }
6464    else
6465    {
6466        __densities_.reserve(__b_.size());
6467        for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW)
6468            __densities_.push_back(*__fW);
6469        __init();
6470    }
6471}
6472
6473#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6474
6475template<class _RealType>
6476template<class _UnaryOperation>
6477piecewise_linear_distribution<_RealType>::param_type::param_type(
6478        initializer_list<result_type> __bl, _UnaryOperation __fw)
6479    : __b_(__bl.begin(), __bl.end())
6480{
6481    if (__b_.size() < 2)
6482    {
6483        __b_.resize(2);
6484        __b_[0] = 0;
6485        __b_[1] = 1;
6486        __densities_.assign(2, 1.0);
6487        __areas_.assign(1, 0.0);
6488    }
6489    else
6490    {
6491        __densities_.reserve(__b_.size());
6492        for (size_t __i = 0; __i < __b_.size(); ++__i)
6493            __densities_.push_back(__fw(__b_[__i]));
6494        __init();
6495    }
6496}
6497
6498#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
6499
6500template<class _RealType>
6501template<class _UnaryOperation>
6502piecewise_linear_distribution<_RealType>::param_type::param_type(
6503        size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
6504    : __b_(__nw == 0 ? 2 : __nw + 1)
6505{
6506    size_t __n = __b_.size() - 1;
6507    result_type __d = (__xmax - __xmin) / __n;
6508    __densities_.reserve(__b_.size());
6509    for (size_t __i = 0; __i < __n; ++__i)
6510    {
6511        __b_[__i] = __xmin + __i * __d;
6512        __densities_.push_back(__fw(__b_[__i]));
6513    }
6514    __b_[__n] = __xmax;
6515    __densities_.push_back(__fw(__b_[__n]));
6516    __init();
6517}
6518
6519template<class _RealType>
6520template<class _URNG>
6521_RealType
6522piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
6523{
6524    typedef uniform_real_distribution<result_type> _Gen;
6525    result_type __u = _Gen()(__g);
6526    ptrdiff_t __k = _VSTD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(),
6527                                      __u) - __p.__areas_.begin() - 1;
6528    __u -= __p.__areas_[__k];
6529    const result_type __dk = __p.__densities_[__k];
6530    const result_type __dk1 = __p.__densities_[__k+1];
6531    const result_type __deltad = __dk1 - __dk;
6532    const result_type __bk = __p.__b_[__k];
6533    if (__deltad == 0)
6534        return __u / __dk + __bk;
6535    const result_type __bk1 = __p.__b_[__k+1];
6536    const result_type __deltab = __bk1 - __bk;
6537    return (__bk * __dk1 - __bk1 * __dk +
6538        _VSTD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) /
6539        __deltad;
6540}
6541
6542template <class _CharT, class _Traits, class _RT>
6543basic_ostream<_CharT, _Traits>&
6544operator<<(basic_ostream<_CharT, _Traits>& __os,
6545           const piecewise_linear_distribution<_RT>& __x)
6546{
6547    __save_flags<_CharT, _Traits> _(__os);
6548    __os.flags(ios_base::dec | ios_base::left | ios_base::fixed |
6549               ios_base::scientific);
6550    _CharT __sp = __os.widen(' ');
6551    __os.fill(__sp);
6552    size_t __n = __x.__p_.__b_.size();
6553    __os << __n;
6554    for (size_t __i = 0; __i < __n; ++__i)
6555        __os << __sp << __x.__p_.__b_[__i];
6556    __n = __x.__p_.__densities_.size();
6557    __os << __sp << __n;
6558    for (size_t __i = 0; __i < __n; ++__i)
6559        __os << __sp << __x.__p_.__densities_[__i];
6560    __n = __x.__p_.__areas_.size();
6561    __os << __sp << __n;
6562    for (size_t __i = 0; __i < __n; ++__i)
6563        __os << __sp << __x.__p_.__areas_[__i];
6564    return __os;
6565}
6566
6567template <class _CharT, class _Traits, class _RT>
6568basic_istream<_CharT, _Traits>&
6569operator>>(basic_istream<_CharT, _Traits>& __is,
6570           piecewise_linear_distribution<_RT>& __x)
6571{
6572    typedef piecewise_linear_distribution<_RT> _Eng;
6573    typedef typename _Eng::result_type result_type;
6574    typedef typename _Eng::param_type param_type;
6575    __save_flags<_CharT, _Traits> _(__is);
6576    __is.flags(ios_base::dec | ios_base::skipws);
6577    size_t __n;
6578    __is >> __n;
6579    vector<result_type> __b(__n);
6580    for (size_t __i = 0; __i < __n; ++__i)
6581        __is >> __b[__i];
6582    __is >> __n;
6583    vector<result_type> __densities(__n);
6584    for (size_t __i = 0; __i < __n; ++__i)
6585        __is >> __densities[__i];
6586    __is >> __n;
6587    vector<result_type> __areas(__n);
6588    for (size_t __i = 0; __i < __n; ++__i)
6589        __is >> __areas[__i];
6590    if (!__is.fail())
6591    {
6592        swap(__x.__p_.__b_, __b);
6593        swap(__x.__p_.__densities_, __densities);
6594        swap(__x.__p_.__areas_, __areas);
6595    }
6596    return __is;
6597}
6598
6599_LIBCPP_END_NAMESPACE_STD
6600
6601#endif  // _LIBCPP_RANDOM
6602