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 uniform_real_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 <numeric> 21 #include <cstddef> 22 23 template <class T> 24 inline 25 T sqr(T x)26sqr(T x) 27 { 28 return x * x; 29 } 30 main()31int main() 32 { 33 { 34 typedef std::uniform_real_distribution<> D; 35 typedef std::minstd_rand G; 36 typedef D::param_type P; 37 G g; 38 D d(5.5, 25); 39 P p(-10, 20); 40 const int N = 100000; 41 std::vector<D::result_type> u; 42 for (int i = 0; i < N; ++i) 43 { 44 D::result_type v = d(g, p); 45 assert(p.a() <= v && v < p.b()); 46 u.push_back(v); 47 } 48 D::result_type mean = std::accumulate(u.begin(), u.end(), 49 D::result_type(0)) / u.size(); 50 D::result_type var = 0; 51 D::result_type skew = 0; 52 D::result_type kurtosis = 0; 53 for (std::size_t i = 0; i < u.size(); ++i) 54 { 55 D::result_type dbl = (u[i] - mean); 56 D::result_type d2 = sqr(dbl); 57 var += d2; 58 skew += dbl * d2; 59 kurtosis += d2 * d2; 60 } 61 var /= u.size(); 62 D::result_type dev = std::sqrt(var); 63 skew /= u.size() * dev * var; 64 kurtosis /= u.size() * var * var; 65 kurtosis -= 3; 66 D::result_type x_mean = (p.a() + p.b()) / 2; 67 D::result_type x_var = sqr(p.b() - p.a()) / 12; 68 D::result_type x_skew = 0; 69 D::result_type x_kurtosis = -6./5; 70 assert(std::abs((mean - x_mean) / x_mean) < 0.01); 71 assert(std::abs((var - x_var) / x_var) < 0.01); 72 assert(std::abs(skew - x_skew) < 0.01); 73 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01); 74 } 75 } 76