fork download
  1. #include <map>
  2. #include <sstream>
  3. #include <vector>
  4. #include <random>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <numeric>
  8.  
  9. struct Die
  10. {
  11. Die(unsigned faces = 6) : _faces(faces), _die(std::random_device()()) {}
  12. Die(const Die& d) : _faces(d._faces), _die(std::random_device()()) {}
  13.  
  14. unsigned operator()()
  15. {
  16. return std::uniform_int_distribution<unsigned>(1, _faces)(_die);
  17. }
  18.  
  19. private:
  20. unsigned _faces;
  21. std::mt19937 _die;
  22. };
  23.  
  24. int main()
  25. {
  26. const unsigned trials = 100000;
  27. const unsigned faces = 6;
  28. const unsigned num_dice = 2;
  29.  
  30. std::vector<Die> dice(num_dice, Die(faces));
  31.  
  32. using seq_map = std::map<std::vector<unsigned>, unsigned>;
  33. seq_map sequence_frequency ;
  34.  
  35. // generate and store roll sequences.
  36. for (unsigned i = 0; i < trials; ++i)
  37. {
  38. std::vector<unsigned> rolls;
  39. for (auto& die : dice)
  40. rolls.push_back(die());
  41.  
  42. ++sequence_frequency[rolls];
  43. }
  44.  
  45. // collate roll sequences which have the same total.
  46. std::map<unsigned, std::vector<seq_map::const_iterator>> by_total ;
  47. for (auto it = sequence_frequency.cbegin(); it != sequence_frequency.cend(); ++it)
  48. {
  49. unsigned total = std::accumulate(it->first.cbegin(), it->first.cend(), 0u);
  50. by_total[total].push_back(it);
  51. }
  52.  
  53. // display:
  54. for (const auto& seq : by_total)
  55. {
  56. std::cout << std::setw(3) << seq.first << ": ";
  57.  
  58. std::ostringstream rolls_string;
  59.  
  60. unsigned freq = 0;
  61. for (const auto& roll : seq.second)
  62. {
  63. for (const auto& value : roll->first)
  64. rolls_string << '[' << value << ']';
  65. rolls_string << " (" << roll->second << ") ";
  66. freq += roll->second;
  67. }
  68.  
  69. std::cout << std::setw(6) << freq << " - ";
  70. std::cout << std::setprecision(2) << std::fixed << std::setw(5);
  71. std::cout << (freq / (double) trials*100.0) << "%\n";
  72.  
  73. std::cout << '\t' << rolls_string.str() << '\n';
  74. }
  75. }
  76.  
Success #stdin #stdout 0.04s 3440KB
stdin
Standard input is empty
stdout
  2:   2789 -  2.79%
	[1][1] (2789)  
  3:   5465 -  5.46%
	[1][2] (2731)  [2][1] (2734)  
  4:   8114 -  8.11%
	[1][3] (2640)  [2][2] (2735)  [3][1] (2739)  
  5:  11144 - 11.14%
	[1][4] (2707)  [2][3] (2879)  [3][2] (2836)  [4][1] (2722)  
  6:  13824 - 13.82%
	[1][5] (2792)  [2][4] (2737)  [3][3] (2818)  [4][2] (2721)  [5][1] (2756)  
  7:  16795 - 16.80%
	[1][6] (2771)  [2][5] (2855)  [3][4] (2891)  [4][3] (2787)  [5][2] (2822)  [6][1] (2669)  
  8:  13882 - 13.88%
	[2][6] (2724)  [3][5] (2880)  [4][4] (2783)  [5][3] (2754)  [6][2] (2741)  
  9:  11187 - 11.19%
	[3][6] (2856)  [4][5] (2750)  [5][4] (2851)  [6][3] (2730)  
 10:   8347 -  8.35%
	[4][6] (2850)  [5][5] (2714)  [6][4] (2783)  
 11:   5632 -  5.63%
	[5][6] (2817)  [6][5] (2815)  
 12:   2821 -  2.82%
	[6][6] (2821)