Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Probability {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- int coin;
- double rounds = 1000;
- double countFlip = 0;
- double successRate = 0;
- // first game with 3 heads required to win a round
- // start by setting the wins to zero to avoid improper display of win percentage
- System.out.println("-------- GAME ONE (3 heads to win round) ----------");
- int playerOneWin = 0;
- int playerTwoWin = 0;
- for (int i = 0; i < rounds; i++) {
- int playerOne = 0;
- int playerTwo = 0;
- countFlip = 0; // don't forget to reset the flip count per round!
- while (playerOne < 3 || playerTwo < 3) {
- Random headTails = new Random();
- coin = headTails.nextInt(2) + 1;
- countFlip++;
- //System.out.println("The toss was: " + coin);
- if (coin == 1) { // we only care about heads, so check to see if the flipped coin was a head (1 = head, 2 = tails)
- // use modulus to check what player flipped.
- // (1/2 R 1 must be player 1, 2/2 R 0 must be player 2)
- if (countFlip % 2 == 1) {
- //System.out.println("Player 1 wins the coin toss!");
- playerOne++;
- } else if (countFlip % 2 == 0) {
- //System.out.println("Player 2 wins the coin toss!");
- playerTwo++;
- }
- }
- // the check to see who won first. break out of the loop on win.
- // player 2 is ignored if player 1 won first.
- if (playerOne == 3) {
- playerOneWin++;
- break;
- } else if (playerTwo == 3) {
- playerTwoWin++;
- break;
- }
- }
- //System.out.println("Round over! 3 heads flipped!");
- //System.out.println("coins flipped: " + countFlip);
- //System.out.println("Current standing is: " + playerOneWin + " - " + playerTwoWin);
- }
- System.out.println(playerOneWin);
- System.out.println(playerTwoWin);
- successRate = (playerOneWin / rounds) * 100;
- System.out.println(successRate + "%");
- // second game with 50 heads required to win a round
- System.out.println("");
- System.out.println("-------- GAME TWO (50 heads to win round) ----------");
- playerOneWin = 0;
- playerTwoWin = 0;
- for (int i = 0; i < rounds; i++) {
- int playerOne = 0;
- int playerTwo = 0;
- countFlip = 0;
- while (playerOne < 50 || playerTwo < 50) {
- Random headTails = new Random();
- coin = headTails.nextInt(2) + 1;
- countFlip++;
- //System.out.println("The toss was: " + coin);
- if (coin == 1) {
- if (countFlip % 2 == 1) {
- //System.out.println("Player 1 wins the coin toss!");
- playerOne++;
- } else if (countFlip % 2 == 0) {
- //System.out.println("Player 2 wins the coin toss!");
- playerTwo++;
- }
- }
- if (playerOne == 50) {
- playerOneWin++;
- break;
- } else if (playerTwo == 50) {
- playerTwoWin++;
- break;
- }
- }
- //System.out.println("Round over! 50 heads flipped!");
- //System.out.println("coins flipped: " + countFlip);
- //System.out.println("Current standing is: " + playerOneWin + " - " + playerTwoWin);
- }
- System.out.println(playerOneWin);
- System.out.println(playerTwoWin);
- successRate = (playerOneWin / rounds) * 100;
- System.out.println(successRate + "%");
- }
- }
Add Comment
Please, Sign In to add comment