My Weblog

Blog about programming and math

Monty Hall Problem

Today i was watching some probability videos on youtube to solve project euler problem 307 and fortunately found a video about Monty Hall Problem. After reading wiki article to get feel of problem , i tried to simulate the situation and to my complete surprise the simulation result was almost same as theoretical [ I know there is nothing to surprise but considering my probability skills , it was kind of surprise to me :)]. Source code for simulation.

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

int main()
	{
		int M[3]={0,1,1};//Car, Goat , Goat 
		int opendoor,closedoor,win=0,swin=0,k;
		for(int i=0;i<100000000;i++)
		{
			k=rand()%3;//contestant chose a door random
			if(k==1) opendoor=2;//contestant has chosen the first door so presenter will open second
			else if(k==2) opendoor=1;//contestant has chosen the second door so presenter will open first  
			else opendoor=rand()%2+1;//contest has chosen door 0 which contains CAR so presenter will open any of first or second
			for(int i=0;i<3;i++) 
			{
				if(i==k || i==opendoor) continue;
				closedoor=i;
			}	
			//winning probability withour swapping
			if(k==0) win++;
			swap(k,closedoor);
			//winning probability with swapping
			if(k==0)swin++;
		}

		printf("probability without swapping %lf\nprobability with swapping %lf\n",win*1.0/100000000.0,swin*1.0/100000000.0);
	}
Advertisements

October 28, 2010 - Posted by | Programming

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: