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