fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <random>
  5.  
  6. int main()
  7. {
  8. // no hardware access on ideone
  9. // std::random_device rd;
  10. // std::mt19937 gen(rd());
  11. std::mt19937 gen(time(NULL));
  12.  
  13. std::vector<double> p = {0, 1.0/15, 2.0/15, 3.0/15, 4.0/15, 5.0/15};
  14. std::discrete_distribution<> d(p.begin(), p.end());
  15. std::map<int, int> m;
  16. for(int n=0; n<10000; ++n) {
  17. ++m[d(gen)];
  18. }
  19. for(auto i = m.begin(); i!=m.end(); ++i) {
  20. std::cout << i->first << " generated " << i->second << " times\n";
  21. }
  22. }
  23.  
Success #stdin #stdout 0s 3024KB
stdin
Standard input is empty
stdout
1 generated 685 times
2 generated 1323 times
3 generated 2011 times
4 generated 2628 times
5 generated 3353 times