Advertisement
tejasd

Monty Hall Problem

Oct 7th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  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;
  8.  
  9. int main()
  10. {
  11.     srand (time(NULL)); //random number seed will be the current time
  12.     unsigned long long int prize, player_choice, open_door;
  13.  
  14.  
  15.     for (unsigned long long int i = 0; i < 1000000000; ++i)
  16.     {
  17.         prize = rand()%3 + 1;
  18.         player_choice = rand()%3 + 1;
  19.         if (prize==player_choice)
  20.         {
  21.             ++win_unchanged;
  22.         }
  23.         for (;;)
  24.         {
  25.             open_door = rand()%3 + 1;
  26.             if (open_door != prize)
  27.             {
  28.                 break;
  29.             }
  30.         }
  31.         switch (player_choice) //to change the door
  32.         {
  33.             case 1: if (open_door == 2)
  34.             {
  35.                 player_choice = 3;
  36.             }
  37.             else
  38.             {
  39.                 player_choice = 2;
  40.             }
  41.             break;
  42.  
  43.             case 2: if (open_door == 1)
  44.             {
  45.                 player_choice = 3;
  46.             }
  47.             else
  48.             {
  49.                 player_choice = 1;
  50.             }
  51.             break;
  52.  
  53.             case 3: if (open_door == 2)
  54.             {
  55.                 player_choice = 1;
  56.             }
  57.             else
  58.             {
  59.                 player_choice = 2;
  60.             }
  61.             break;
  62.         }
  63.         if (prize == player_choice)
  64.         {
  65.             ++win_changed;
  66.         }
  67.  
  68.  
  69.     }
  70.     cout << "Wins when changed = " << win_changed << endl;
  71.     cout << "Wins when not changed = " << win_unchanged << endl;
  72.     return 0;
  73.  
  74. }
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement