Advertisement
tejasd

Monty Hall Problem

Dec 12th, 2011
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int win_changed = 0,win_unchanged = 0, open_door_choice = 0;
  8. unsigned long long int trials;
  9.  
  10. int main()
  11. {
  12.     cout << "Enter the number of simulations you want to make : ";
  13.     cin >> trials;
  14.     if (trials == 0)
  15.     {
  16.         trials = 10000000000;
  17.     }
  18.     srand (time(NULL)); //random number seed will be the current time
  19.     unsigned long long int prize, player_choice, open_door;
  20.  
  21.  
  22.     for (unsigned long long int i = 0; i < trials; ++i)
  23.     {
  24.         prize = rand()%3 + 1;
  25.         player_choice = rand()%3 + 1;
  26.         if (prize==player_choice)
  27.         {
  28.             ++win_unchanged;
  29.         }
  30.         //to open a door
  31.         if (prize != player_choice)
  32.         {
  33.             for (int i = 1; i < 4; ++i)
  34.             {
  35.                 if (i != prize && i != player_choice)
  36.                 {
  37.                     open_door = i;
  38.                     break;
  39.  
  40.                 }
  41.             }
  42.         }
  43.         else
  44.         {
  45.             if (open_door_choice == 0)
  46.             {
  47.                 for (int i = 1; i < 4; ++i)
  48.                 {
  49.                     if (i != prize)
  50.                     {
  51.                         open_door = i;
  52.                         open_door_choice = 1;
  53.                         break;
  54.                     }
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 for (int i = 3; i <= 1; ++i)
  60.                 {
  61.                     if (i != prize)
  62.                     {
  63.                         open_door = i;
  64.                         open_door_choice = 0;
  65.                         break;
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         switch (player_choice) //to change the door
  72.         {
  73.             case 1: if (open_door == 2)
  74.             {
  75.                 player_choice = 3;
  76.             }
  77.             else
  78.             {
  79.                 player_choice = 2;
  80.             }
  81.             break;
  82.  
  83.             case 2: if (open_door == 1)
  84.             {
  85.                 player_choice = 3;
  86.             }
  87.             else
  88.             {
  89.                 player_choice = 1;
  90.             }
  91.             break;
  92.  
  93.             case 3: if (open_door == 2)
  94.             {
  95.                 player_choice = 1;
  96.             }
  97.             else
  98.             {
  99.                 player_choice = 2;
  100.             }
  101.             break;
  102.         }
  103.         if (prize == player_choice)
  104.         {
  105.             ++win_changed;
  106.         }
  107.  
  108.  
  109.     }
  110.     cout << "Wins when changed = " << win_changed << endl;
  111.     cout << "Wins when not changed = " << win_unchanged << endl;
  112.     return 0;
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement