View Single Post
  #18  
Old 10-14-2013, 01:50 AM
r00t r00t is offline
Sarnak


Join Date: Jun 2013
Posts: 330
Default

I wrote a program after reading a lot of mathematicians were only convinced after seeing computer simulations. In 9000 trials, switching makes you win ~6000 (2/3), not switching only ~3000 (1/3)

Code:
#include <stdio.h>
#include <stdlib.h>

#define MONTY_TRIALS 9000

bool monty_hall_problem(bool switch_doors)
{
    bool doors[3] = { false, false, false };
    doors[rand()%3] = true;

    unsigned int choice = rand() % 3;
    return (switch_doors) ? !doors[choice] : doors[choice];
}

int main(int argc, char* argv[])
{
    int switch_wins = 0, no_switch_wins = 0;

    for (int i = 0; i < MONTY_TRIALS; ++i)
    {
        if (monty_hall_problem(true)) ++switch_wins;
        if (monty_hall_problem(false)) ++no_switch_wins;
    }

    printf("Switch: %d\tNo Switch: %d", switch_wins, no_switch_wins);

    return 0;
}