Home | History | Annotate | Download | only in rand.dist.samp.discrete
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // REQUIRES: long_tests
     11 
     12 // <random>
     13 
     14 // template<class IntType = int>
     15 // class discrete_distribution
     16 
     17 // template<class _URNG> result_type operator()(_URNG& g);
     18 
     19 #include <random>
     20 #include <vector>
     21 #include <cassert>
     22 
     23 int main()
     24 {
     25     {
     26         typedef std::discrete_distribution<> D;
     27         typedef std::minstd_rand G;
     28         G g;
     29         D d;
     30         const int N = 100;
     31         std::vector<D::result_type> u(d.max()+1);
     32         for (int i = 0; i < N; ++i)
     33         {
     34             D::result_type v = d(g);
     35             assert(d.min() <= v && v <= d.max());
     36             u[v]++;
     37         }
     38         std::vector<double> prob = d.probabilities();
     39         for (int i = 0; i <= d.max(); ++i)
     40             assert((double)u[i]/N == prob[i]);
     41     }
     42     {
     43         typedef std::discrete_distribution<> D;
     44         typedef std::minstd_rand G;
     45         G g;
     46         double p0[] = {.3};
     47         D d(p0, p0+1);
     48         const int N = 100;
     49         std::vector<D::result_type> u(d.max()+1);
     50         for (int i = 0; i < N; ++i)
     51         {
     52             D::result_type v = d(g);
     53             assert(d.min() <= v && v <= d.max());
     54             u[v]++;
     55         }
     56         std::vector<double> prob = d.probabilities();
     57         for (int i = 0; i <= d.max(); ++i)
     58             assert((double)u[i]/N == prob[i]);
     59     }
     60     {
     61         typedef std::discrete_distribution<> D;
     62         typedef std::minstd_rand G;
     63         G g;
     64         double p0[] = {.75, .25};
     65         D d(p0, p0+2);
     66         const int N = 1000000;
     67         std::vector<D::result_type> u(d.max()+1);
     68         for (int i = 0; i < N; ++i)
     69         {
     70             D::result_type v = d(g);
     71             assert(d.min() <= v && v <= d.max());
     72             u[v]++;
     73         }
     74         std::vector<double> prob = d.probabilities();
     75         for (int i = 0; i <= d.max(); ++i)
     76             assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
     77     }
     78     {
     79         typedef std::discrete_distribution<> D;
     80         typedef std::minstd_rand G;
     81         G g;
     82         double p0[] = {0, 1};
     83         D d(p0, p0+2);
     84         const int N = 1000000;
     85         std::vector<D::result_type> u(d.max()+1);
     86         for (int i = 0; i < N; ++i)
     87         {
     88             D::result_type v = d(g);
     89             assert(d.min() <= v && v <= d.max());
     90             u[v]++;
     91         }
     92         std::vector<double> prob = d.probabilities();
     93         assert((double)u[0]/N == prob[0]);
     94         assert((double)u[1]/N == prob[1]);
     95     }
     96     {
     97         typedef std::discrete_distribution<> D;
     98         typedef std::minstd_rand G;
     99         G g;
    100         double p0[] = {1, 0};
    101         D d(p0, p0+2);
    102         const int N = 1000000;
    103         std::vector<D::result_type> u(d.max()+1);
    104         for (int i = 0; i < N; ++i)
    105         {
    106             D::result_type v = d(g);
    107             assert(d.min() <= v && v <= d.max());
    108             u[v]++;
    109         }
    110         std::vector<double> prob = d.probabilities();
    111         assert((double)u[0]/N == prob[0]);
    112         assert((double)u[1]/N == prob[1]);
    113     }
    114     {
    115         typedef std::discrete_distribution<> D;
    116         typedef std::minstd_rand G;
    117         G g;
    118         double p0[] = {.3, .1, .6};
    119         D d(p0, p0+3);
    120         const int N = 10000000;
    121         std::vector<D::result_type> u(d.max()+1);
    122         for (int i = 0; i < N; ++i)
    123         {
    124             D::result_type v = d(g);
    125             assert(d.min() <= v && v <= d.max());
    126             u[v]++;
    127         }
    128         std::vector<double> prob = d.probabilities();
    129         for (int i = 0; i <= d.max(); ++i)
    130             assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    131     }
    132     {
    133         typedef std::discrete_distribution<> D;
    134         typedef std::minstd_rand G;
    135         G g;
    136         double p0[] = {0, 25, 75};
    137         D d(p0, p0+3);
    138         const int N = 1000000;
    139         std::vector<D::result_type> u(d.max()+1);
    140         for (int i = 0; i < N; ++i)
    141         {
    142             D::result_type v = d(g);
    143             assert(d.min() <= v && v <= d.max());
    144             u[v]++;
    145         }
    146         std::vector<double> prob = d.probabilities();
    147         for (int i = 0; i <= d.max(); ++i)
    148             if (prob[i] != 0)
    149                 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    150             else
    151                 assert(u[i] == 0);
    152     }
    153     {
    154         typedef std::discrete_distribution<> D;
    155         typedef std::minstd_rand G;
    156         G g;
    157         double p0[] = {25, 0, 75};
    158         D d(p0, p0+3);
    159         const int N = 1000000;
    160         std::vector<D::result_type> u(d.max()+1);
    161         for (int i = 0; i < N; ++i)
    162         {
    163             D::result_type v = d(g);
    164             assert(d.min() <= v && v <= d.max());
    165             u[v]++;
    166         }
    167         std::vector<double> prob = d.probabilities();
    168         for (int i = 0; i <= d.max(); ++i)
    169             if (prob[i] != 0)
    170                 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    171             else
    172                 assert(u[i] == 0);
    173     }
    174     {
    175         typedef std::discrete_distribution<> D;
    176         typedef std::minstd_rand G;
    177         G g;
    178         double p0[] = {25, 75, 0};
    179         D d(p0, p0+3);
    180         const int N = 1000000;
    181         std::vector<D::result_type> u(d.max()+1);
    182         for (int i = 0; i < N; ++i)
    183         {
    184             D::result_type v = d(g);
    185             assert(d.min() <= v && v <= d.max());
    186             u[v]++;
    187         }
    188         std::vector<double> prob = d.probabilities();
    189         for (int i = 0; i <= d.max(); ++i)
    190             if (prob[i] != 0)
    191                 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    192             else
    193                 assert(u[i] == 0);
    194     }
    195     {
    196         typedef std::discrete_distribution<> D;
    197         typedef std::minstd_rand G;
    198         G g;
    199         double p0[] = {0, 0, 1};
    200         D d(p0, p0+3);
    201         const int N = 100;
    202         std::vector<D::result_type> u(d.max()+1);
    203         for (int i = 0; i < N; ++i)
    204         {
    205             D::result_type v = d(g);
    206             assert(d.min() <= v && v <= d.max());
    207             u[v]++;
    208         }
    209         std::vector<double> prob = d.probabilities();
    210         for (int i = 0; i <= d.max(); ++i)
    211             if (prob[i] != 0)
    212                 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    213             else
    214                 assert(u[i] == 0);
    215     }
    216     {
    217         typedef std::discrete_distribution<> D;
    218         typedef std::minstd_rand G;
    219         G g;
    220         double p0[] = {0, 1, 0};
    221         D d(p0, p0+3);
    222         const int N = 100;
    223         std::vector<D::result_type> u(d.max()+1);
    224         for (int i = 0; i < N; ++i)
    225         {
    226             D::result_type v = d(g);
    227             assert(d.min() <= v && v <= d.max());
    228             u[v]++;
    229         }
    230         std::vector<double> prob = d.probabilities();
    231         for (int i = 0; i <= d.max(); ++i)
    232             if (prob[i] != 0)
    233                 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    234             else
    235                 assert(u[i] == 0);
    236     }
    237     {
    238         typedef std::discrete_distribution<> D;
    239         typedef std::minstd_rand G;
    240         G g;
    241         double p0[] = {1, 0, 0};
    242         D d(p0, p0+3);
    243         const int N = 100;
    244         std::vector<D::result_type> u(d.max()+1);
    245         for (int i = 0; i < N; ++i)
    246         {
    247             D::result_type v = d(g);
    248             assert(d.min() <= v && v <= d.max());
    249             u[v]++;
    250         }
    251         std::vector<double> prob = d.probabilities();
    252         for (int i = 0; i <= d.max(); ++i)
    253             if (prob[i] != 0)
    254                 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    255             else
    256                 assert(u[i] == 0);
    257     }
    258     {
    259         typedef std::discrete_distribution<> D;
    260         typedef std::minstd_rand G;
    261         G g;
    262         double p0[] = {33, 0, 0, 67};
    263         D d(p0, p0+3);
    264         const int N = 1000000;
    265         std::vector<D::result_type> u(d.max()+1);
    266         for (int i = 0; i < N; ++i)
    267         {
    268             D::result_type v = d(g);
    269             assert(d.min() <= v && v <= d.max());
    270             u[v]++;
    271         }
    272         std::vector<double> prob = d.probabilities();
    273         for (int i = 0; i <= d.max(); ++i)
    274             if (prob[i] != 0)
    275                 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
    276             else
    277                 assert(u[i] == 0);
    278     }
    279 }
    280