Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <random>
- #include <iostream>
- constexpr int BASE = 100000;
- constexpr double PLANE_HIT = 0.2;
- constexpr double TARGET_HIT = 0.3;
- int gen_value()
- {
- static std::random_device rd;
- static std::uniform_int_distribution<int> dist(1, 1 * BASE);
- static std::mt19937 engine(rd());
- return dist(engine);
- }
- bool check(double prob)
- {
- return gen_value() < prob * BASE;
- }
- bool test()
- {
- int value = gen_value();
- int container_idx = 0;
- if (value > 1.0/3 * BASE && value < 2.0/3 * BASE)
- {
- container_idx = 1;
- }
- else if (value > 2.0/3 * BASE)
- {
- container_idx = 2;
- }
- //std::cout << "Container id: " << container_idx << '\n';
- int planes_left = 3;
- while (planes_left--) {
- //std::cout << "Plane #" << 3 - planes_left << " is attacking\n";
- if (check(PLANE_HIT))
- {
- // container destroyed
- //std::cout << "Plane #" << 3 - planes_left << " was destroyed\n";
- if (container_idx-- == 0) {
- //std::cout << "Container was destroyed\n";
- return false;
- }
- }
- else
- {
- // if target destroyed
- if (check(TARGET_HIT)) {
- //std::cout << "Plane #" << 3 - planes_left << " has destroyed the target\n";
- return true;
- }
- }
- }
- return false;
- }
- int main()
- {
- const int probes = 1000000;
- int i = probes;
- int times = 0;
- while (i--) {
- if (test()) {
- times++;
- }
- }
- int return_value = times / (double)probes * 100;
- std::cout << return_value;
- return return_value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement