Not Monty Hall Simulation

There's also MontyHallSimulation, but I've done this in C because a goto statement actually makes the difference between TheMontyHallProblem? and NotTheMontyHallProblem clearer:

The only difference is in the goto line. In TheMontyHallProblem?, the host always picks an empty box. In NotTheMontyHallProblem, he picks one at random, but we skip over the cases when that box has the prize.

-- AndrewMcGuinness


  #include <stdio.h>

extern int random();

int rand(int n) { return (random()/4)%n; }

int montyHallProblem() { int youPick,hostPicks,prize,youSwitchTo,i;

start:

prize=rand(3);

youPick=rand(3);

host:

hostPicks=(rand(2)+youPick+1)%3; if ( hostPicks == prize ) goto host;

/* now you switch */ for ( i = 0 ; i < 3 ; ++i ) { if (( i!=youPick ) && ( i!=hostPicks )) { youSwitchTo = i; break; } }

return ( youSwitchTo == prize ); }

int notMontyHallProblem() { int youPick,hostPicks,prize,youSwitchTo,i;

start:

prize=rand(3);

youPick=rand(3);

host:

hostPicks=rand(3); if ( hostPicks == prize ) goto start;

/* now you switch */ for ( i = 0 ; i < 3 ; ++i ) { if (( i!=youPick ) && ( i!=hostPicks )) { youSwitchTo = i; break; } }

return ( youSwitchTo == prize ); }

main() { int loop,wins,tries; tries=100000;

for ( wins = loop = 0 ; loop < tries ; ++loop ) if ( montyHallProblem() ) ++wins;

printf( "MontyHallProblem: %d out of %d\n", wins, loop );

for ( wins = loop = 0 ; loop < tries ; ++loop ) if ( notMontyHallProblem() ) ++wins; printf( "NotMontyHallProblem?: %d out of %d\n", wins, loop ); }


EditText of this page (last edited October 3, 2002) or FindPage with title or text search