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