public static void main(String[] args) { int stayWin = 0; int switchWin = 0; int stayFail = 0; int swtichFail = 0; int stay = 0; int sswitch = 0; boolean printEachResult = false; int total = 1000; for (int j = 0; j < total; j++) { int doorWithPrize = getRandomNumberInRange(1, 3); int choice = getRandomNumberInRange(1, 3); List<String> otherDoors = new ArrayList<>(); for (int i = 1; i < 4; i++) { if (i != choice) { otherDoors.add(String.valueOf(i)); } } if (otherDoors.contains(String.valueOf(doorWithPrize))) { // remove by object otherDoors.remove(String.valueOf(doorWithPrize)); } else { // remove by index otherDoors.remove(getRandomNumberInRange(0, 1)); } int doorToRemove = Integer.parseInt(otherDoors.get(0)); // print each result if (printEachResult) { System.out.println("Prize : " + doorWithPrize); System.out.println("Choice : " + choice); System.out.println("Remove : " + doorToRemove); if (choice == doorWithPrize) { System.out.println("Win"); } else { System.out.println("Fail"); } } List<String> newDoors = new ArrayList<>(); for (int i = 1; i < 4; i++) { if (i != doorToRemove) { newDoors.add(String.valueOf(i)); } } int newChoiceIndex = getRandomNumberInRange(0, 1); int newChoice = Integer.parseInt(newDoors.get(newChoiceIndex)); if (newChoice == choice) { // stay stay++; if (doorWithPrize == choice) { stayWin++; } else { stayFail++; } } else { // switch sswitch++; if (doorWithPrize == newChoice) { switchWin++; } else { swtichFail++; } } } float stayWinPercent = (float) stayWin / (float) stay * 100.f; float stayFailPercent = (float) stayFail / (float) stay * 100.f; float switchWinPercent = (float) switchWin / (float) sswitch * 100.f; float switchFailPercent = (float) swtichFail / (float) sswitch * 100.f; System.out.println("Stay => " + stay); System.out.println("Win : " + stayWinPercent); System.out.println("Fail : " + stayFailPercent); System.out.println("Switch => " + sswitch); System.out.println("Win : " + switchWinPercent); System.out.println("Fail : " + switchFailPercent); } private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; }
Result
Stay => 499868 Win : 33.314995 Fail : 66.685005 Switch => 500132 Win : 66.766174 Fail : 33.233826