fork download
  1. #include <random>
  2. #include <array>
  3. #include <iostream>
  4.  
  5. int RollADice1(const std::array<int, 6>& chances) {
  6. static std::random_device rd; //Will be used to obtain a seed for the random number engine
  7. static std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
  8. static std::discrete_distribution<int> distrib(chances.cbegin(), chances.cend());
  9.  
  10. return distrib(gen);
  11. }
  12.  
  13. int main() {
  14. constexpr std::array<int, 6> chances {
  15. 100,
  16. 100,
  17. 200,
  18. 100,
  19. 100,
  20. 600
  21. };
  22.  
  23. std::array<int, 6> arr{};
  24.  
  25. for(int i=0; i < 120'000; ++i) {
  26. ++arr.at(RollADice1(chances));
  27. }
  28.  
  29. for(int i=0; i < arr.size(); ++i) {
  30. std::cout << i << ' ' << arr[i] << '\n';
  31. }
  32.  
  33. std::cout << "Sum: " << std::accumulate(arr.begin(), arr.end(), 0) << '\n';
  34. }
Success #stdin #stdout 0.01s 5400KB
stdin
Standard input is empty
stdout
0 10202
1 9856
2 19853
3 9867
4 9962
5 60260
Sum: 120000