1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // REQUIRES: long_tests
10 
11 // <random>
12 
13 // template<class RealType = double>
14 // class cauchy_distribution
15 
16 // template<class _URNG> result_type operator()(_URNG& g);
17 
18 #include <random>
19 #include <cassert>
20 #include <vector>
21 #include <algorithm>
22 
23 #include "test_macros.h"
24 
25 double
f(double x,double a,double b)26 f(double x, double a, double b)
27 {
28     return 1/3.1415926535897932 * std::atan((x - a)/b) + .5;
29 }
30 
main(int,char **)31 int main(int, char**)
32 {
33     {
34         typedef std::cauchy_distribution<> D;
35         typedef std::mt19937 G;
36         G g;
37         const double a = 10;
38         const double b = .5;
39         D d(a, b);
40         const int N = 1000000;
41         std::vector<D::result_type> u;
42         for (int i = 0; i < N; ++i)
43             u.push_back(d(g));
44         std::sort(u.begin(), u.end());
45         for (int i = 0; i < N; ++i)
46             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
47     }
48     {
49         typedef std::cauchy_distribution<> D;
50         typedef std::mt19937 G;
51         G g;
52         const double a = -1.5;
53         const double b = 1;
54         D d(a, b);
55         const int N = 1000000;
56         std::vector<D::result_type> u;
57         for (int i = 0; i < N; ++i)
58             u.push_back(d(g));
59         std::sort(u.begin(), u.end());
60         for (int i = 0; i < N; ++i)
61             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
62     }
63     {
64         typedef std::cauchy_distribution<> D;
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   return 0;
80 }
81