Draxion

3 Door Paradox Solution

May 28th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. public class HelloWorld{
  2.  
  3.      public static void main(String []args){
  4.         int i = 0;
  5.         int wins = 0;
  6.         for (i = 0; i < 1000000; i++) {
  7.             int winner = (int)Math.ceil(Math.random()*3.0); //Winning choice
  8.             int choice = (int)Math.ceil(Math.random()*3); //Initial Pick
  9.             int open = (int)Math.ceil(Math.random()*3); //Door to open
  10.             while (choice == open || winner == open) { //Making sure the open door is not the winner and the picked door
  11.                 open = (int)Math.ceil(Math.random()*3);
  12.             }
  13.             int swap = 1;
  14.             while (choice == swap || swap == open) { //Picks the remaining door`
  15.                 swap++;
  16.             }
  17.             if (swap == winner)
  18.                 wins++;
  19.            
  20.         }
  21.        
  22.         System.out.println(wins + " swaps won out of " + i + " trials for a win percentage of " + (double)wins/(double)i);
  23.      }
  24. }
Add Comment
Please, Sign In to add comment