fork(2) download
  1. #include <algorithm>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <numeric>
  5. #include <vector>
  6. #include <random>
  7. int main()
  8. {
  9. using namespace std;
  10.  
  11. time_t t = time(NULL);
  12. //srand(static_cast<unsigned int>(t));
  13. std::mt19937 randgen(t);
  14. int simulationSize = 10000; // 1 million //lowered to 10k to run on ideone
  15. std::uniform_int_distribution<int> dist4(0,3);
  16. std::uniform_int_distribution<int> dist6(0,5);
  17. // simulation 1, 4 faces on dice
  18. cout << "Simulation 1( 4 faced dice ): " << endl;
  19. int fourSideDicePrizes[] { 1, 1, -1, -1 };
  20.  
  21. for (int i = 0; i < 10; i++)
  22. {
  23. int total = 0;
  24. for (int j = 0; j < simulationSize*100; ++j)
  25. {
  26. int roll = dist4(randgen);
  27.  
  28. total += fourSideDicePrizes[roll];
  29. }
  30. cout << total << endl;
  31. }
  32. cout << " -------------------------------------------------------\n\n" << endl;
  33.  
  34. // simulation 2, 6 dice
  35. cout << "Simulation 2( 6 faced dice ): " << endl;
  36. int sixSidedDicePrizes[] { 1, 1, -1, -1, -1, 1 };
  37.  
  38. for (int i = 0; i < 10; i++)
  39. {
  40. int total = 0;
  41. for (int j = 0; j < simulationSize * 100; ++j)
  42. {
  43. int roll = dist6(randgen);
  44.  
  45. total += sixSidedDicePrizes[roll];
  46. }
  47. cout << total << endl;
  48. }
  49. cout << " -------------------------------------------------------\n\n" << endl;
  50. cin.get();
  51. return 0;
  52. }
Success #stdin #stdout 1.33s 3104KB
stdin
Standard input is empty
stdout
Simulation 1( 4 faced dice ): 
1742
156
466
442
-1140
-1118
-1868
-212
1080
-750
 -------------------------------------------------------


Simulation 2( 6 faced dice ): 
-2
202
-750
-196
1298
2270
110
-1728
-1024
-164
 -------------------------------------------------------