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