Guest User

Monty Hall problem, for once and always solved!

a guest
Sep 7th, 2024
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | Source Code | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define CHOICE_KEEP 0
  7. #define CHOICE_SWITCH 1
  8.  
  9. int main() {
  10.     srand(time(NULL));
  11.     unsigned int total_keep_wins = 0;
  12.     unsigned int total_switch_wins = 0;
  13.     for (int j = 0; j < 100000; j++) {
  14.         unsigned int choices_made[2] = {0};
  15.         for (int k = 0; k < 1000; k++) {
  16.             // Choice by showman
  17.             unsigned int door_showman_choice = rand() % 3;
  18.             unsigned int door_prize;
  19.             // Looking for door to put prize behind randomly
  20.             while (true) {
  21.                 door_prize = rand() % 3;
  22.                 if (door_prize == door_showman_choice) {
  23.                     // Invalid choice, try again
  24.                     continue;
  25.                 }
  26.                 // Found valid door to put prize behind
  27.                 break;
  28.             }
  29.             // Your first door choice
  30.             unsigned int door_first_choice = 0;
  31.             while (true) {
  32.                 door_first_choice = rand() % 3;
  33.                 if (door_first_choice != door_showman_choice) {
  34.                     // Made a valid choice, not the showman door
  35.                     break;
  36.                 }
  37.                
  38.             }
  39.             // Decide if you'll switch
  40.             bool do_switch = rand() % 2;
  41.             // You've won if the switch value doesn't equal your choice
  42.             bool won = do_switch != (door_first_choice == door_prize);
  43.             choices_made[do_switch] += won;
  44.         }
  45.         if(choices_made[CHOICE_SWITCH] == choices_made[CHOICE_KEEP]){
  46.             continue;
  47.         }
  48.         printf("Win switch: %d, win keep: %d: %s\n",
  49.                choices_made[CHOICE_SWITCH], choices_made[CHOICE_KEEP],
  50.                choices_made[CHOICE_SWITCH] > choices_made[CHOICE_KEEP]
  51.                    ? "switch won"
  52.                    : "keep won");
  53.                    
  54.         if (choices_made[CHOICE_SWITCH] > choices_made[CHOICE_KEEP]) {
  55.             total_switch_wins++;
  56.         } else {
  57.             total_keep_wins++;
  58.         }
  59.     }
  60.     printf("Switch wins :%d Keep wins: %d\n",total_switch_wins, total_keep_wins);
  61.     printf("Conclusion: %s by a margin of %d/%d times.\n", total_switch_wins > total_keep_wins
  62.                                    ? "switching is best choice"
  63.                                    : "keeping is best choice",
  64.                                 total_switch_wins > total_keep_wins  ? total_switch_wins - total_keep_wins : total_keep_wins - total_switch_wins,
  65.                                 total_switch_wins + total_keep_wins);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment