#include <iostream> #include <cstdlib> #include <vector> #include <iterator> using namespace std; int main() { int runs = 100000000; int WIDTH = 20; srand(8); std::vector<int> hits(WIDTH); for(int i = 0; i < runs; ++i){ int r = (rand() % ((RAND_MAX / WIDTH)*WIDTH))/(RAND_MAX / WIDTH); if(r >= WIDTH){ std::cout<<r<<" i="<<i<<std::endl; return 1; } hits[r]++; } std::copy(hits.begin(), hits.end(), std::ostream_iterator<int>(std::cout,", ")); return 0; }
Standard input is empty
5001100, 4999414, 4995275, 5001323, 5000493, 4997878, 5006182, 4999710, 5001731, 4997415, 4999753, 5002246, 5001067, 5006148, 4994913, 4996067, 5002092, 5001399, 4995437, 5000357,