fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. int main() {
  8. int runs = 100000000;
  9. int WIDTH = 20;
  10. srand(8);
  11.  
  12. std::vector<int> hits(WIDTH);
  13.  
  14. for(int i = 0; i < runs; ++i){
  15. int r = (rand() % ((RAND_MAX / WIDTH)*WIDTH))/(RAND_MAX / WIDTH);
  16. if(r >= WIDTH){
  17. std::cout<<r<<" i="<<i<<std::endl;
  18. return 1;
  19. }
  20. hits[r]++;
  21. }
  22.  
  23. std::copy(hits.begin(), hits.end(), std::ostream_iterator<int>(std::cout,", "));
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 2.85s 3228KB
stdin
Standard input is empty
stdout
5001100, 4999414, 4995275, 5001323, 5000493, 4997878, 5006182, 4999710, 5001731, 4997415, 4999753, 5002246, 5001067, 5006148, 4994913, 4996067, 5002092, 5001399, 4995437, 5000357,