Home | History | Annotate | Download | only in rand.dist.samp.plinear
      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 RealType = double>
     15 // class piecewise_linear_distribution
     16 
     17 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
     18 
     19 #include <random>
     20 #include <vector>
     21 #include <iterator>
     22 #include <numeric>
     23 #include <cassert>
     24 
     25 template <class T>
     26 inline
     27 T
     28 sqr(T x)
     29 {
     30     return x*x;
     31 }
     32 
     33 double
     34 f(double x, double a, double m, double b, double c)
     35 {
     36     return a + m*(sqr(x) - sqr(b))/2 + c*(x-b);
     37 }
     38 
     39 int main()
     40 {
     41     {
     42         typedef std::piecewise_linear_distribution<> D;
     43         typedef D::param_type P;
     44         typedef std::mt19937_64 G;
     45         G g;
     46         double b[] = {10, 14, 16, 17};
     47         double p[] = {25, 62.5, 12.5, 0};
     48         const size_t Np = sizeof(p) / sizeof(p[0]) - 1;
     49         D d;
     50         P pa(b, b+Np+1, p);
     51         const int N = 1000000;
     52         std::vector<D::result_type> u;
     53         for (int i = 0; i < N; ++i)
     54         {
     55             D::result_type v = d(g, pa);
     56             assert(10 <= v && v < 17);
     57             u.push_back(v);
     58         }
     59         std::sort(u.begin(), u.end());
     60         int kp = -1;
     61         double a;
     62         double m;
     63         double bk;
     64         double c;
     65         std::vector<double> areas(Np);
     66         double S = 0;
     67         for (int i = 0; i < areas.size(); ++i)
     68         {
     69             areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2;
     70             S += areas[i];
     71         }
     72         for (int i = 0; i < areas.size(); ++i)
     73             areas[i] /= S;
     74         for (int i = 0; i < Np+1; ++i)
     75             p[i] /= S;
     76         for (int i = 0; i < N; ++i)
     77         {
     78             int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1;
     79             if (k != kp)
     80             {
     81                 a = 0;
     82                 for (int j = 0; j < k; ++j)
     83                     a += areas[j];
     84                 m = (p[k+1] - p[k]) / (b[k+1] - b[k]);
     85                 bk = b[k];
     86                 c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]);
     87                 kp = k;
     88             }
     89             assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001);
     90         }
     91     }
     92 }
     93