/** * Author: Arvind Pai * Email: arvindpai1 AT gmail DOT com * Description: Simulation of the Monty Hall Problem * Date: 22 Dec 2009 */#include <stdio.h>#include <stdlib.h>#include<time.h>int main() { int sim; // Number of simulations int prize; // Prize moor int choice; // Player's first choice int open; // Door Monty Hall opens int swap; // Player's choice after swapping int win = 0; // Number of times player wins by swapping puts("How many simulations do you want to run?"); scanf("%d", &sim); srand(time(0)); for(int i = 0; i < sim; ++i) { prize = rand() % 3; choice = rand() % 3; if(choice == prize) { // Player's first choice is the prize door // Monty Hall has to open one of remaining two doors open = ((rand() % 2) + choice + 1) % 3; } else { // Player's first choice is a junk door // Monty Hall is forced to open the other junk door open = 3 - (choice + prize); } swap = 3 - (choice + open); // Player's choice after swapping if(swap == prize) { // Player wins prize by swapping ++win; } } printf("\nWins = %d\nTotal = %d\nRatio = %f\n", win, sim, (float)win/sim); return 0;}