Guest User

Untitled

a guest
Jun 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Probability {
  4.  
  5.     /**
  6.      * @param args the command line arguments
  7.      */
  8.     public static void main(String[] args) {
  9.  
  10.         int coin;
  11.         double rounds = 1000;
  12.         double countFlip = 0;
  13.         double successRate = 0;
  14.        
  15.         // first game with 3 heads required to win a round
  16.         // start by setting the wins to zero to avoid improper display of win percentage
  17.         System.out.println("-------- GAME ONE (3 heads to win round) ----------");
  18.         int playerOneWin = 0;
  19.         int playerTwoWin = 0;
  20.         for (int i = 0; i < rounds; i++) {
  21.             int playerOne = 0;
  22.             int playerTwo = 0;
  23.             countFlip = 0; // don't forget to reset the flip count per round!
  24.             while (playerOne < 3 || playerTwo < 3) {
  25.                 Random headTails = new Random();
  26.                 coin = headTails.nextInt(2) + 1;
  27.                 countFlip++;
  28.                 //System.out.println("The toss was: " + coin);
  29.                 if (coin == 1) { // we only care about heads, so check to see if the flipped coin was a head (1 = head, 2 = tails)
  30.                     // use modulus to check what player flipped.
  31.                     // (1/2 R 1 must be player 1, 2/2 R 0 must be player 2)
  32.                     if (countFlip % 2 == 1) {
  33.                         //System.out.println("Player 1 wins the coin toss!");
  34.                         playerOne++;
  35.                     } else if (countFlip % 2 == 0) {
  36.                         //System.out.println("Player 2 wins the coin toss!");
  37.                         playerTwo++;
  38.                     }
  39.                 }
  40.                 // the check to see who won first. break out of the loop on win.
  41.                 // player 2 is ignored if player 1 won first.
  42.                 if (playerOne == 3) {
  43.                     playerOneWin++;
  44.                     break;
  45.                 } else if (playerTwo == 3) {
  46.                     playerTwoWin++;
  47.                     break;
  48.  
  49.                 }
  50.             }
  51.             //System.out.println("Round over! 3 heads flipped!");
  52.             //System.out.println("coins flipped: " + countFlip);
  53.             //System.out.println("Current standing is: " + playerOneWin + " - " + playerTwoWin);
  54.         }
  55.         System.out.println(playerOneWin);
  56.         System.out.println(playerTwoWin);
  57.         successRate = (playerOneWin / rounds) * 100;
  58.         System.out.println(successRate + "%");
  59.  
  60.        
  61.         // second game with 50 heads required to win a round
  62.         System.out.println("");
  63.         System.out.println("-------- GAME TWO (50 heads to win round) ----------");
  64.         playerOneWin = 0;
  65.         playerTwoWin = 0;
  66.         for (int i = 0; i < rounds; i++) {
  67.             int playerOne = 0;
  68.             int playerTwo = 0;
  69.             countFlip = 0;
  70.             while (playerOne < 50 || playerTwo < 50) {
  71.                 Random headTails = new Random();
  72.                 coin = headTails.nextInt(2) + 1;
  73.                 countFlip++;
  74.                 //System.out.println("The toss was: " + coin);
  75.                 if (coin == 1) {
  76.                     if (countFlip % 2 == 1) {
  77.                         //System.out.println("Player 1 wins the coin toss!");
  78.                         playerOne++;
  79.                     } else if (countFlip % 2 == 0) {
  80.                        //System.out.println("Player 2 wins the coin toss!");
  81.                         playerTwo++;
  82.                     }
  83.                 }
  84.                 if (playerOne == 50) {
  85.                     playerOneWin++;
  86.                     break;
  87.                 } else if (playerTwo == 50) {
  88.                     playerTwoWin++;
  89.                     break;
  90.  
  91.                 }
  92.             }
  93.             //System.out.println("Round over! 50 heads flipped!");
  94.             //System.out.println("coins flipped: " + countFlip);
  95.             //System.out.println("Current standing is: " + playerOneWin + " - " + playerTwoWin);
  96.         }
  97.         System.out.println(playerOneWin);
  98.         System.out.println(playerTwoWin);
  99.         successRate = (playerOneWin / rounds) * 100;
  100.         System.out.println(successRate + "%");
  101.     }
  102. }
Add Comment
Please, Sign In to add comment