fork download
  1. #include <vector>
  2. #include <random>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. struct Die
  7. {
  8. Die(unsigned faces = 6) : _faces(faces), _die(std::random_device()()) {}
  9. Die(const Die& d) : _faces(d._faces), _die(std::random_device()()) {}
  10.  
  11. unsigned operator()() { return std::uniform_int_distribution<unsigned>(1, _faces)(_die); }
  12.  
  13. private:
  14. unsigned _faces;
  15. std::mt19937 _die;
  16. };
  17.  
  18. int main()
  19. {
  20. const unsigned trials = 100000;
  21. const unsigned faces = 6;
  22. const unsigned num_dice = 6;
  23.  
  24. std::vector<Die> dice(num_dice, Die(faces));
  25. std::vector<unsigned> counts(faces * num_dice + 1);
  26.  
  27. for (unsigned i = 0; i < trials; ++i)
  28. {
  29. unsigned rolls = 0;
  30. for (auto& die : dice)
  31. rolls += die();
  32.  
  33. ++counts[rolls];
  34. }
  35.  
  36. std::cout << "Counts\n";
  37.  
  38. for (unsigned i = num_dice; i < counts.size(); ++i)
  39. {
  40. std::cout << std::setw(2) << i << ": ";
  41. std::cout << std::setw(6) << counts[i] << " - ";
  42. std::cout << std::setw(5) << std::fixed << std::setprecision(2);
  43. std::cout << (counts[i] / (double) trials)*100.0 << "%\n";
  44. }
  45. }
Success #stdin #stdout 0.04s 3476KB
stdin
Standard input is empty
stdout
Counts
 6:      1 -  0.00%
 7:     11 -  0.01%
 8:     34 -  0.03%
 9:     93 -  0.09%
10:    299 -  0.30%
11:    519 -  0.52%
12:    977 -  0.98%
13:   1661 -  1.66%
14:   2467 -  2.47%
15:   3594 -  3.59%
16:   4718 -  4.72%
17:   6210 -  6.21%
18:   7518 -  7.52%
19:   8345 -  8.35%
20:   8870 -  8.87%
21:   9399 -  9.40%
22:   9072 -  9.07%
23:   8350 -  8.35%
24:   7303 -  7.30%
25:   6188 -  6.19%
26:   4912 -  4.91%
27:   3458 -  3.46%
28:   2463 -  2.46%
29:   1582 -  1.58%
30:    966 -  0.97%
31:    527 -  0.53%
32:    282 -  0.28%
33:    120 -  0.12%
34:     43 -  0.04%
35:     14 -  0.01%
36:      4 -  0.00%