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, const param_type& parm);
     18 
     19 #include <random>
     20 #include <vector>
     21 #include <cassert>
     22 
     23 int main()
     24 {
     25     {
     26         typedef std::discrete_distribution<> D;
     27         typedef D::param_type P;
     28         typedef std::minstd_rand G;
     29         G g;
     30         D d;
     31         double p0[] = {.3, .1, .6};
     32         P p(p0, p0+3);
     33         const int N = 10000000;
     34         std::vector<D::result_type> u(3);
     35         for (int i = 0; i < N; ++i)
     36         {
     37             D::result_type v = d(g, p);
     38             assert(0 <= v && v <= 2);
     39             u[v]++;
     40         }
     41         std::vector<double> prob = p.probabilities();
     42         for (int i = 0; i <= 2; ++i)
     43             assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
     44     }
     45 }
     46