# include # include # include using namespace std; int main ( ); int duel_result ( double a_accuracy, double b_accuracy ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // duel_simulation() simulates a duel. // // Discussion: // // Player 1 fires at player 2, and hits with a probability of P(1). // If Player 2 misses, then Player 2 fires at Player 1, hitting with // a probability of P(2). // // The duel continues with alternating shots until only one player // survives. // // The simulation is intended to estimate the probabilities that a // player will survive, and the number of turns required. // // The exact probability that player 1 will survive is // // P(1) / ( P(1) + P(2) - P(1) * P(2) ) // // Player 2's chance is // // P(2) * ( 1 - P(1) ) / ( P(1) + P(2) - P(1) * P(2) ) // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 08 November 2009 // // Author: // // John Burkardt // // Reference: // // Paul Nahin, // Duelling Idiots and Other Probability Puzzlers, // Princeton University Press, 2000, // ISBN13: 978-0691009797, // LC: QA273.N29. // // Martin Shubik, // "Does the Fittest Necessarily Survive?", // in Readings in Game Theory and Political Behavior, // edited by Martin Shubik, // Doubleday, 1954, // LC: H61.S53. // // Input: // // double A_ACCURACY, B_ACCURACY, the probabilities that players A and // B will hit their opponent in a single shot. // // int DUEL_NUM, the number of duels to run. // // Output: // // double A_PROB, B_PROB, the estimated probablities that players // A and B will survive. // // double TURN_AVERAGE, the average number of turns required to // complete the duel. // { double a_accuracy; double a_prob; int a_wins; double b_accuracy; double b_prob; int b_wins; int duel; int duel_num; int seed; int winner; cout << "\n"; cout << "duel_simulation():\n"; cout << " C++ version\n"; srand48 ( time ( 0 ) ); cout << "Enter number of duels to run: "; cin >> duel_num; cout << "Enter player A's accuracy between 0.0 and 1.0: "; cin >> a_accuracy; cout << "Enter player B's accuracy between 0.0 and 1.0: "; cin >> b_accuracy; seed = time ( 0 ); srand ( seed ); a_wins = 0; b_wins = 0; for ( duel = 1; duel <= duel_num; duel++ ) { winner = duel_result ( a_accuracy, b_accuracy ); if ( winner == 1 ) { a_wins = a_wins + 1; } else { b_wins = b_wins + 1; } } a_prob = ( double ) a_wins / ( double ) duel_num; b_prob = ( double ) b_wins / ( double ) duel_num; cout << "\n"; cout << " Player A wins with probability " << a_prob << "\n"; cout << " Player B wins with probability " << b_prob << "\n"; cout << "\n"; cout << "duel_simulation():\n"; cout << " Normal end of execution.\n"; return 0; } //****************************************************************************80 int duel_result ( double a_accuracy, double b_accuracy ) //****************************************************************************80 // // Purpose: // // duel_result() returns the outcome of a single duel. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 17 September 2012 // // Author: // // John Burkardt // // Reference: // // Martin Shubik, // “Does the Fittest Necessarily Survive?”, // in Readings in Game Theory and Political Behavior, // edited by Martin Shubik, // Doubleday, 1954, // LC: H61.S53. // // Input: // // double A_ACCURACY, B_ACCURACY, the probabilities that player A // and B will hit their opponent in a single shot. // // Output: // // int DUEL_RESULT, the survivor of the duel. // { double r; int winner; while ( true ) { r = drand48 ( ); if ( r <= a_accuracy ) { winner = 1; break; } r = drand48 ( ); if ( r <= b_accuracy ) { winner = 2; break; } } return winner; }