fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. enum things {
  6. Goat = 0,
  7. Car = 1
  8. };
  9.  
  10. #define IGNORANT_MONTY 1
  11.  
  12. int rand(int max) {
  13. float RM = RAND_MAX;
  14. return rand() / RM * max;
  15. }
  16.  
  17. int main() {
  18. srand(time(0));
  19. int num = 10000;
  20.  
  21. int change = 0;
  22. int stay = 0;
  23. int discarded = 0;
  24.  
  25. while(stay + change < num) {
  26. things doors[3];
  27. for(auto & t : doors) {
  28. t = Goat;
  29. }
  30. doors[rand(3)] = Car;
  31.  
  32. int you = rand(3);
  33. int monty;
  34.  
  35. #if IGNORANT_MONTY
  36. monty = (rand(2) + you + 1) % 3;
  37.  
  38. if(doors[monty] == Car)
  39. discarded++;
  40. else
  41. #else
  42. for(int i = 0; i < 3; ++i) {
  43. if( you == i )
  44. continue;
  45. if( doors[i] == Goat )
  46. continue;
  47. monty = i;
  48. }
  49. #endif
  50.  
  51. if(doors[you] == Car)
  52. stay++;
  53. else if(doors[you] == Goat)
  54. change++;
  55. }
  56.  
  57. cout << "stay: " << stay << endl << "change: " << change << endl << "discarded: " << discarded << endl;
  58. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
stay: 5004
change: 4996
discarded: 4911