So after seeing another video for the Monty Hall Problem and since I learned about Monte Carlo simulation methods, I thought I would try to find the percentage 66,66% of winning the game if you switch doors. The problem is that I get 50%, and one thing that worried when thinking up the algorithm is if my model was correct. I had 2 random guesses implemented, one for choosing door 1 to 3 with 1 in 3 chances and one for choosing to switch doors with 1 in 2 chances. The if statements were for assigning the doors with the prizes and for the different possibilities for each of those guesses. I don't know if I can reduce that part, but it works for now (I think). Where was my thinking incorrect? And can you suggest a fix to my algorithm? Thank you very much!
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int seed=time(NULL);
srand(seed);
double u, random[2], suitch=0.0, total=0.0;
int nall=10000000, nright=0, i, door[3], k, j;
for(j=0; j<nall; j++)
{
for(i=0; i<3; i++)
random[i]=0.0, door[i]=0;
for(i=0; i<2; i++)
{
u=(1.*rand())/RAND_MAX;
random[i]=3.*u;
//printf("%lf\t%lf\n",u,random[i]);
}
suitch=2.*u;
//printf("%lf\n",suitch);
if(floor(random[0])==0)
door[0]=1, door[1]=0, door[2]=0;
else if(floor(random[0])==1)
door[0]=0, door[1]=1, door[2]=0;
else if(floor(random[0])==2)
door[0]=0, door[1]=0, door[2]=1;
for(i=0; i<3; i++)
//printf("%d\t",door[i]);
if((floor(random[1])==0)&&(floor(suitch)==0))
k=door[0];
else if((floor(random[1])==1)&&(floor(suitch)==0))
k=door[1];
else if((floor(random[1])==2)&&(floor(suitch)==0))
k=door[2];
else if((floor(random[1])==0)&&(floor(suitch)==1))
{
if(door[1]==1)
k=door[1];
else if(door[1]==0)
k=door[2];
}
else if((floor(random[1])==1)&&(floor(suitch)==1))
{
if(door[0]==1)
k=door[0];
else if(door[0]==0)
k=door[2];
}
else if((floor(random[1])==2)&&(floor(suitch)==1))
{
if(door[0]==1)
k=door[0];
else if(door[0]==0)
k=door[1];
}
if(k==1)
nright++;
}
total=1.*nright/nall;
printf("%d\t%d\t%lf\t", k, nright, total);
return 0;
}