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