Advertisement
Guest User

Monty Hall problem simulator v2

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