Advertisement
AndryS1

Untitled

Feb 24th, 2025 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <random>
  2. #include <iostream>
  3. constexpr int BASE = 100000;
  4. constexpr double PLANE_HIT = 0.2;
  5. constexpr double TARGET_HIT = 0.3;
  6.  
  7. int gen_value()
  8. {
  9.     static std::random_device rd;
  10.     static std::uniform_int_distribution<int> dist(1, 1 * BASE);
  11.     static std::mt19937 engine(rd());
  12.     return dist(engine);
  13. }
  14.  
  15. bool check(double prob)
  16. {
  17.     return gen_value() < prob * BASE;
  18. }
  19.  
  20. bool test()
  21. {
  22.     int value = gen_value();
  23.     int container_idx = 0;
  24.     if (value > 1.0/3 * BASE && value < 2.0/3 * BASE)
  25.     {
  26.         container_idx = 1;
  27.     }
  28.     else if (value > 2.0/3 * BASE)
  29.     {
  30.         container_idx = 2;
  31.     }
  32.     //std::cout << "Container id: " << container_idx << '\n';
  33.     int planes_left = 3;
  34.     while (planes_left--) {
  35.         //std::cout << "Plane #" << 3 - planes_left << " is attacking\n";
  36.         if (check(PLANE_HIT))
  37.         {
  38.             // container destroyed
  39.             //std::cout << "Plane #" << 3 - planes_left << " was destroyed\n";
  40.             if (container_idx-- == 0) {
  41.                 //std::cout << "Container was destroyed\n";
  42.                 return false;
  43.             }
  44.         }
  45.         else
  46.         {
  47.             // if target destroyed
  48.             if (check(TARGET_HIT)) {
  49.                 //std::cout << "Plane #" << 3 - planes_left << " has destroyed the target\n";
  50.                 return true;
  51.             }
  52.         }
  53.     }
  54.     return false;
  55. }
  56.  
  57. int main()
  58. {
  59.     const int probes = 1000000;
  60.     int i = probes;
  61.     int times = 0;
  62.     while (i--) {
  63.         if (test()) {
  64.             times++;
  65.         }
  66.     }
  67.     int return_value = times / (double)probes * 100;
  68.     std::cout << return_value;
  69.     return return_value;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement