# My Weblog

## 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);
}
```