fork download
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <random>
  4. using namespace std;
  5.  
  6. std::mt19937 rnd;
  7.  
  8. double rndf() {
  9. return (rnd() - rnd.min() + 0.5) / (rnd.max() + 1.0 - rnd.min());
  10. }
  11.  
  12. /* Box Muller transform */
  13. double rnorm() {
  14. double u1 = rndf(), u2 = rndf();
  15. return sqrt(-2 * log(u1)) * cos(2 * M_PI * u2);
  16. }
  17.  
  18. bool win(double v) {
  19. int potions = 10;
  20. bool ok = true;
  21. for(int a=0; a<2000; a++) {
  22. while(rndf() < 0.4) potions++;
  23. double x = 2 * rnorm() + 4;
  24. if(x < v && potions) { potions--; continue; }
  25. double y = x + rnorm();
  26. if(y < 0) return false;
  27. }
  28. return true;
  29. }
  30.  
  31. int main() {
  32. printf("cutoff;wins\n");
  33. int ngames = 100;
  34. for(double v=1; v<6; v+=0.1) {
  35. int good = 0;
  36. for(int a=0; a<ngames; a++) if(win(v)) good++;
  37. printf("%6.2f;%f\n", v, good * 1. / ngames);
  38. }
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.77s 5384KB
stdin
Standard input is empty
stdout
cutoff;wins
  1.00;0.000000
  1.10;0.000000
  1.20;0.000000
  1.30;0.000000
  1.40;0.000000
  1.50;0.000000
  1.60;0.020000
  1.70;0.000000
  1.80;0.010000
  1.90;0.080000
  2.00;0.090000
  2.10;0.110000
  2.20;0.210000
  2.30;0.310000
  2.40;0.490000
  2.50;0.550000
  2.60;0.520000
  2.70;0.680000
  2.80;0.710000
  2.90;0.840000
  3.00;0.910000
  3.10;0.880000
  3.20;0.930000
  3.30;0.950000
  3.40;0.940000
  3.50;0.970000
  3.60;0.970000
  3.70;1.000000
  3.80;0.990000
  3.90;0.990000
  4.00;1.000000
  4.10;0.980000
  4.20;0.940000
  4.30;0.980000
  4.40;0.940000
  4.50;0.870000
  4.60;0.880000
  4.70;0.610000
  4.80;0.540000
  4.90;0.330000
  5.00;0.160000
  5.10;0.040000
  5.20;0.030000
  5.30;0.020000
  5.40;0.000000
  5.50;0.000000
  5.60;0.000000
  5.70;0.000000
  5.80;0.000000
  5.90;0.000000
  6.00;0.000000