fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <random>
  4. #include <unordered_map>
  5. #include <functional>
  6. using namespace std;
  7. const double E = 1e-6;
  8.  
  9. void increaseCounters(unordered_map<int, int>& counters)
  10. {
  11. for (auto& counter : counters)
  12. ++counter.second;
  13. }
  14.  
  15. void resetCounters(unordered_map<int, int>& counters)
  16. {
  17. for (auto& counter : counters)
  18. counter.second = 0;
  19. }
  20.  
  21. double countInRS(double rate, unordered_map<int, int>& gacha)
  22. {
  23. const int N = 1000000;
  24. int totalNum = 0;
  25. unordered_map<int, int> counters;
  26. for (auto& p : gacha)counters[p.first] = 0;
  27. std::random_device rd;
  28. std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
  29. std::uniform_real_distribution<> dis(0.0, 1.0);
  30. for (int i = 0; i < N; ++i)
  31. {
  32. bool hit = false;
  33. //check every counter to see if we hit one of them
  34. for (auto& p : counters)
  35. {
  36. if (!hit && p.second + 1 == p.first)
  37. {
  38. int r = rand() % 100;
  39. if (r < gacha[p.first])
  40. {
  41. ++totalNum;
  42. hit = true;
  43. break;
  44. }
  45. }
  46. }
  47. if (hit)
  48. {
  49. resetCounters(counters);
  50. continue;
  51. }
  52. double r = dis(gen);
  53. if (rate - r > E)
  54. {
  55. ++totalNum;
  56. resetCounters(counters);
  57. }
  58. else
  59. increaseCounters(counters);
  60. }
  61. return static_cast<double>(totalNum) / static_cast<double>(N);
  62. }
  63.  
  64. double getActualRate(double targetRate, unordered_map<int, int>& gacha)
  65. {
  66. //actual rate
  67. double lo = 0, hi = targetRate;
  68. while (hi - lo > E)
  69. {
  70. double mid = (lo + hi) / 2;
  71. double actualRate = countInRS(mid, gacha);
  72. if (actualRate - targetRate > E)hi = mid;
  73. else lo = mid;
  74. cout << "\t" << to_string(mid) << endl;
  75. }
  76. return lo;
  77. }
  78.  
  79. int main()
  80. {
  81. int N = 10;
  82. double total = 0;
  83. //every pair <n, k> represent you have k/100 chance to get this card
  84. //when you have rolled n - 1 times without getting this card
  85. unordered_map<int, int> gacha = { {10, 10}, {100, 100} };
  86. //Run N times, calculate average
  87. for (int i = 0; i < N; ++i)
  88. {
  89. cout << "Iteration " << to_string(i) << " : " << endl;
  90. double acutalRate = getActualRate(0.015, gacha);
  91. total += acutalRate;
  92. cout << "Iteration " << to_string(i) << " end, " << "actual rate : " << to_string(acutalRate) << endl;
  93. }
  94. cout << "Average result: " + to_string(total / N) << endl;
  95. return 0;
  96. }
  97.  
  98.  
  99.  
Success #stdin #stdout 1.88s 4516KB
stdin
Standard input is empty
stdout
Iteration 0 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006914
	0.006855
	0.006826
	0.006812
	0.006819
	0.006815
	0.006813
	0.006814
Iteration 0 end, actual rate : 0.006813
Iteration 1 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006914
	0.006855
	0.006826
	0.006812
	0.006804
	0.006808
	0.006806
	0.006805
Iteration 1 end, actual rate : 0.006804
Iteration 2 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006738
	0.006709
	0.006694
	0.006702
	0.006705
	0.006707
	0.006706
Iteration 2 end, actual rate : 0.006705
Iteration 3 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006621
	0.006650
	0.006665
	0.006672
	0.006669
	0.006667
	0.006668
Iteration 3 end, actual rate : 0.006668
Iteration 4 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006738
	0.006709
	0.006724
	0.006731
	0.006735
	0.006736
	0.006736
Iteration 4 end, actual rate : 0.006735
Iteration 5 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006621
	0.006592
	0.006606
	0.006614
	0.006610
	0.006612
	0.006613
Iteration 5 end, actual rate : 0.006613
Iteration 6 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006738
	0.006768
	0.006753
	0.006760
	0.006757
	0.006758
	0.006759
Iteration 6 end, actual rate : 0.006759
Iteration 7 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006738
	0.006768
	0.006753
	0.006746
	0.006742
	0.006740
	0.006739
Iteration 7 end, actual rate : 0.006738
Iteration 8 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006621
	0.006650
	0.006665
	0.006672
	0.006669
	0.006671
	0.006671
Iteration 8 end, actual rate : 0.006671
Iteration 9 : 
	0.007500
	0.003750
	0.005625
	0.006562
	0.007031
	0.006797
	0.006680
	0.006621
	0.006650
	0.006665
	0.006672
	0.006669
	0.006671
	0.006671
Iteration 9 end, actual rate : 0.006671
Average result: 0.006718