fork download
  1. #include <array>
  2. #include <iostream>
  3. #include <vector>
  4. #include <map>
  5. #include <random>
  6.  
  7. int main()
  8. {
  9. // no hardware access on ideone
  10. // std::random_device rd;
  11. // std::mt19937 gen(rd());
  12. std::mt19937 gen(time(NULL));
  13.  
  14. const std::array<double, 6> p = {10, 10, 20, 30, 20, 10};
  15. std::discrete_distribution<> d(p.begin(), p.end());
  16. std::map<int, int> m;
  17. for(int n=0; n<10000; ++n) {
  18. ++m[d(gen)];
  19. }
  20. for(auto i = m.begin(); i!=m.end(); ++i) {
  21. std::cout << i->first << " generated " << i->second << " times\n";
  22. }
  23. }
  24.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
0 generated 998 times
1 generated 987 times
2 generated 2035 times
3 generated 2960 times
4 generated 2001 times
5 generated 1019 times