Advertisement
Guest User

Monty Hall problem simulator

a guest
Oct 12th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. void PrintDoors(unsigned int car, unsigned int chosenDoor, bool*opened);
  8.  
  9. int main()
  10. {
  11.   srand((unsigned)time(NULL));
  12.  
  13.   unsigned int wins=0, winsStick=0, winsSwitch=0;
  14.   for(unsigned int game=1;; ++game) {
  15.     unsigned int car = rand()%3;
  16.     bool opened[3] = {0,0,0};
  17.     unsigned int chosenDoor=10, switchDoor, chooseSwitch=10;
  18.  
  19.     cout << "Game " << game << endl << "---------" << endl;
  20.    
  21.     //Contestant chooses initial door
  22.     PrintDoors(car, chosenDoor, opened);
  23.  
  24.     while(chosenDoor<1 || chosenDoor>3) {
  25.       cout << "Choose a door (1,2,3): ";
  26.       cin >> chosenDoor ;
  27.     }
  28.     --chosenDoor;
  29.  
  30.     //Monty opens door
  31.     if(chosenDoor==car)
  32.       opened[ (car + rand()%2 + 1) % 3 ] = 1;
  33.     else if((chosenDoor+1)%3 != car)
  34.       opened[ (chosenDoor+1)%3 ] = 1;
  35.     else if((car+1)%3 != chosenDoor)
  36.       opened[ (car+1)%3 ] = 1;
  37.  
  38.     cout << endl;
  39.     PrintDoors(car, chosenDoor, opened);
  40.  
  41.     //Record result for switching
  42.     switchDoor = (chosenDoor+1)%3;  
  43.     if(opened[switchDoor])
  44.       switchDoor = (switchDoor+1)%3;
  45.    
  46.     if(switchDoor==car)
  47.       ++winsSwitch;
  48.    
  49.     //Record result for sticking
  50.     if(chosenDoor==car)
  51.       ++winsStick;
  52.  
  53.     //Contestant chooses to switch or stick
  54.     while(chooseSwitch<0 || chooseSwitch>1) {
  55.       cout << "Stick (0) or switch (1)?: ";
  56.       cin >> chooseSwitch;
  57.     }
  58.  
  59.     if(chooseSwitch==1)
  60.       chosenDoor = switchDoor;
  61.  
  62.     //Find game result
  63.     opened[0] = opened[1] = opened[2] = 1;
  64.     cout << endl;
  65.     PrintDoors(car, chosenDoor, opened);
  66.     cout << endl;
  67.  
  68.     if(chosenDoor==car) {
  69.       cout << "*** You win the car! ***" << endl << endl;
  70.       ++wins;
  71.     } else
  72.       cout << "*** You win a goat. ***" << endl << endl;
  73.  
  74.     cout << "Won " << wins << " cars in " << game << " games, p=" << static_cast<float>(wins)/game << "" << endl;
  75.     cout << "By sticking every time, you would have won " << winsStick<< " cars in " << game << " games, p=" << static_cast<float>(winsStick)/game << endl;
  76.     cout << "By switching every time, you would have won " << winsSwitch << " cars in " << game << " games, p=" << static_cast<float>(winsSwitch)/game << endl << endl;
  77.   }
  78.  
  79.   return 0;
  80. }
  81.  
  82. void PrintDoors(unsigned int car, unsigned int chosenDoor, bool*opened)
  83. {
  84.   for(int door=0; door<3; ++door) {
  85.     cout << door+1;
  86.    
  87.     if(door == chosenDoor)
  88.       cout << "*";
  89.     else
  90.       cout << " ";
  91.    
  92.     if(!opened[door])
  93.       cout << "[x]";
  94.     else if(car!=door)
  95.       cout << "[Goat]";
  96.     else
  97.       cout << "[Car]";
  98.    
  99.     cout << "     ";
  100.   }
  101.  
  102.   cout << endl;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement