Negafilms Origins
Negafilms Origins

Reputation: 1

Monty Hall Problem C Monte Carlo Simulation

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;   
}

Upvotes: 0

Views: 772

Answers (1)

Boris Lipschitz
Boris Lipschitz

Reputation: 1641

I've been looking at your code way too long, unable to see the problem. What an idiot i've been, lol. There is no problem with the code (except for an accidental, and luckily benign, memory overrun). The problem is what are you trying to simulate.

You simulate 10000000 games, where in half of the cases the player decides to keep his door (his chance is 33.3% in this case) and half of the cases where the player decides to switch (his chance is 66.7% in this case). Of course you are getting 50%, cause thats what you are simulating.

Set suitch = 1, permanently, and you'll... get 66.7%

And yea... please make random 3 elements long or stop initing at the beginning to fix the memory overrun, and comment out the former debug for(i=0; i<3; i++), cause you are running the simulation ifs chain 3 times each iteration for no good reason. But thats unrelated :-)

Upvotes: 2

Related Questions