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