Saturday, February 14, 2015

MonteHall.java in a pgm



MonteHall.java

public class MonteHall
{
 public static void main(String[] args)
{
      int N = Integer.parseInt(args[0]); 
      int wins = 0;                     

       for (int i = 0; i < N; i++)
      {
                    int prize  = (int) (3 * Math.random());
                    int choice = (int) (3 * Math.random());
                    int reveal;
         do
        {
            reveal = (int) (3 * Math.random());
         } while ((reveal == choice) || (reveal == prize));
         int other = 0 + 1 + 2 - reveal - choice;
                     if (other == prize) wins++;
      }

      System.out.println("Fraction of games won = " + 1.0 * wins / N);
   }

}

No comments:

Post a Comment