Home | History | Annotate | Download | only in rand.dist.norm.cauchy
      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 cauchy_distribution
     14 
     15 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
     16 
     17 #include <random>
     18 #include <cassert>
     19 #include <vector>
     20 #include <algorithm>
     21 
     22 double
     23 f(double x, double a, double b)
     24 {
     25     return 1/3.1415926535897932 * std::atan((x - a)/b) + .5;
     26 }
     27 
     28 int main()
     29 {
     30     {
     31         typedef std::cauchy_distribution<> D;
     32         typedef D::param_type P;
     33         typedef std::mt19937 G;
     34         G g;
     35         const double a = 10;
     36         const double b = .5;
     37         D d;
     38         P p(a, b);
     39         const int N = 1000000;
     40         std::vector<D::result_type> u;
     41         for (int i = 0; i < N; ++i)
     42             u.push_back(d(g, p));
     43         std::sort(u.begin(), u.end());
     44         for (int i = 0; i < N; ++i)
     45             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
     46     }
     47     {
     48         typedef std::cauchy_distribution<> D;
     49         typedef D::param_type P;
     50         typedef std::mt19937 G;
     51         G g;
     52         const double a = -1.5;
     53         const double b = 1;
     54         D d;
     55         P p(a, b);
     56         const int N = 1000000;
     57         std::vector<D::result_type> u;
     58         for (int i = 0; i < N; ++i)
     59             u.push_back(d(g, p));
     60         std::sort(u.begin(), u.end());
     61         for (int i = 0; i < N; ++i)
     62             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
     63     }
     64     {
     65         typedef std::cauchy_distribution<> D;
     66         typedef D::param_type P;
     67         typedef std::mt19937 G;
     68         G g;
     69         const double a = .5;
     70         const double b = 2;
     71         D d;
     72         P p(a, b);
     73         const int N = 1000000;
     74         std::vector<D::result_type> u;
     75         for (int i = 0; i < N; ++i)
     76             u.push_back(d(g, p));
     77         std::sort(u.begin(), u.end());
     78         for (int i = 0; i < N; ++i)
     79             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
     80     }
     81 }
     82