Monty Hall problem: Difference between revisions

Added the code for C
(Vedit macro language added)
(Added the code for C)
Line 192:
<pre>Switching wins 21805 times.
Staying wins 10963 times.</pre>
 
 
=={{header|C}}==
 
<c>#include <stdio.h>
#include <stdlib.h>
 
#define TRIALS 10000
 
int brand(int n)
{
return ( n * ((double)rand()/((double)RAND_MAX + (double)1.0 ) ) );
}
 
#define PERCENT(X) ( 100.0*((float)(X))/((float)TRIALS) )
 
int main()
{
int prize, choice, show, notshown, newchoice;
int staywinning=0, changewinning=0, randomwinning=0;
int odoor[2];
int i,j,k;
for(i=0; i < TRIALS; i++)
{
/* put the prize somewhere */
prize = brand(3);
/* let the user choose a door */
choice = brand(3);
/* let us take a list of unchoosen doors */
for (j=0, k=0; j<3; j++)
{
if (j!=choice) odoor[k++] = j;
}
/* Monty opens one... */
if ( choice == prize )
{ /* staying the user will win... Monty opens a random
port*/
show = odoor[ brand(2) ];
notshown = odoor[ (show+1)%2 ];
} else { /* no random, Monty can open just one door... */
if ( odoor[0] == prize )
{
show = odoor[1];
notshown = odoor[0];
} else {
show = odoor[0];
notshown = odoor[1];
}
}
/* the user randomly choose one of the two closed doors
(one is his/her previous choice, the second is the
one notshown */
odoor[0] = choice;
odoor[1] = notshown;
newchoice = odoor[ brand(2) ];
/* now let us count if it takes it or not */
if ( choice == prize ) staywinning++;
if ( notshown == prize ) changewinning++;
if ( newchoice == prize ) randomwinning++;
}
printf("Staying: %.2f\n", PERCENT(staywinning) );
printf("Changing: %.2f\n", PERCENT(changewinning) );
printf("New random choice: %.2f\n", PERCENT(randomwinning) );
}
</c>
 
Output of one run:
 
<pre>
Staying: 33.41
Changing: 66.59
New random choice: 49.67
</pre>
 
 
=={{header|Fortran}}==
Anonymous user