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 IntType = int> 13 // class discrete_distribution 14 15 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm); 16 17 #include <random> 18 #include <vector> 19 #include <cassert> 20 21 int main() 22 { 23 { 24 typedef std::discrete_distribution<> D; 25 typedef D::param_type P; 26 typedef std::minstd_rand G; 27 G g; 28 D d; 29 double p0[] = {.3, .1, .6}; 30 P p(p0, p0+3); 31 const int N = 10000000; 32 std::vector<D::result_type> u(3); 33 for (int i = 0; i < N; ++i) 34 { 35 D::result_type v = d(g, p); 36 assert(0 <= v && v <= 2); 37 u[v]++; 38 } 39 std::vector<double> prob = p.probabilities(); 40 for (int i = 0; i <= 2; ++i) 41 assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); 42 } 43 } 44