Advertisement
Transigence

Monte Hall

Nov 15th, 2014
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MontyHall
  4. {
  5.     class Puzzle
  6.     {
  7.         private bool[] doors;
  8.  
  9.         public int[] unchosen (int choice)
  10.         {
  11.             int[] u = new int[2];
  12.             for (int i = 0, iu = 0; i < 3; ++i) {
  13.                 if (choice != i)
  14.                     u [iu++] = i;
  15.             }
  16.             //Console.WriteLine ("\t <<unchosen returning {0} and {1}>>", u [0], u [1]);
  17.             return u;
  18.         }
  19.  
  20.         public int unchosen_loser(Random r, int choice)
  21.         {
  22.             int[] unch = unchosen (choice);
  23.             int rand = r.Next (2);
  24.             if (doors [unch [rand]] != true)
  25.                 return unch [rand];
  26.             else
  27.             {
  28.                 if (rand == 0)
  29.                     return unch[1];
  30.                 else
  31.                     return unch[0];
  32.             }
  33.         }
  34.  
  35.         public bool reveal(int choice)
  36.         {
  37.             return doors [choice];
  38.         }
  39.  
  40.         public Puzzle(Random r)
  41.         {
  42.             doors = new bool[3];
  43.             doors [r.Next (3)] = true;
  44.         }
  45.  
  46.     }
  47.  
  48.     class MainClass
  49.     {
  50.    
  51.         public static void Main (string[] args)
  52.         {
  53.             int iterations = 10000000;
  54.             int choice;
  55.             int successes = 0;
  56.             int failures = 0;
  57.  
  58.             Random r = new Random();
  59.  
  60.             if (args.Length != 0)
  61.                 int.TryParse (args [0], out iterations);
  62.  
  63.             Console.WriteLine ("Testing Monte Hall problem {0} times...", iterations);
  64.  
  65.             r = new Random ();
  66.  
  67.             for (int i = 0; i < iterations; ++i)
  68.             {
  69.                 Puzzle p = new Puzzle (r);
  70.                 choice = r.Next (3);
  71.                 int[] unch = p.unchosen (choice);
  72.                 int loser = p.unchosen_loser (r, choice);
  73.                 //int newchoice;
  74.  
  75.                 //Console.WriteLine ("New puzzle!" + Environment.NewLine + "Choosing: {0}", choice + 1);
  76.                 //Console.WriteLine ("\t << Unchosen doors are {0} and {1}. >>", unch [0] + 1, unch [1] + 1);
  77.                 //Console.WriteLine ("Door {0} has been opened!", loser + 1);
  78.  
  79.                 //Change this below to alter strategy
  80.  
  81.                 if (unch [0] == loser)
  82.                     choice = unch [1];
  83.                 else
  84.                     choice = unch [0];
  85.  
  86.                 //Console.WriteLine ("Changing choice to {0}.", choice + 1);
  87.  
  88.                 if (p.reveal (choice) == true) {
  89.                     //Console.WriteLine ("You win!");
  90.                     ++successes;
  91.                 } else {
  92.                     //Console.WriteLine ("You lose.");
  93.                     ++failures;
  94.                 }
  95.                 for (int iw = 0; iw < 3; ++iw) {
  96.                     if (p.reveal (iw)) {
  97.                         //Console.WriteLine ("<< Winning door is {0}. >> ", iw + 1);
  98.                     }
  99.                 }
  100.                 //Console.WriteLine ();
  101.             }
  102.  
  103.             Console.WriteLine ("Out of {0} games, you have won {1} ({2}%).", iterations, successes, ((double)successes / (double)iterations) * 100);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement