• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
16 
17 #include <random>
18 #include <cassert>
19 #include <vector>
20 #include <algorithm>
21 
22 double
f(double x,double a,double b)23 f(double x, double a, double b)
24 {
25     return 1/3.1415926535897932 * std::atan((x - a)/b) + .5;
26 }
27 
main()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(a, b);
38         const int N = 1000000;
39         std::vector<D::result_type> u;
40         for (int i = 0; i < N; ++i)
41             u.push_back(d(g));
42         std::sort(u.begin(), u.end());
43         for (int i = 0; i < N; ++i)
44             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
45     }
46     {
47         typedef std::cauchy_distribution<> D;
48         typedef D::param_type P;
49         typedef std::mt19937 G;
50         G g;
51         const double a = -1.5;
52         const double b = 1;
53         D d(a, b);
54         const int N = 1000000;
55         std::vector<D::result_type> u;
56         for (int i = 0; i < N; ++i)
57             u.push_back(d(g));
58         std::sort(u.begin(), u.end());
59         for (int i = 0; i < N; ++i)
60             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
61     }
62     {
63         typedef std::cauchy_distribution<> D;
64         typedef D::param_type P;
65         typedef std::mt19937 G;
66         G g;
67         const double a = .5;
68         const double b = 2;
69         D d(a, b);
70         const int N = 1000000;
71         std::vector<D::result_type> u;
72         for (int i = 0; i < N; ++i)
73             u.push_back(d(g));
74         std::sort(u.begin(), u.end());
75         for (int i = 0; i < N; ++i)
76             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
77     }
78 }
79