Jump to content

Monty Hall problem: Difference between revisions

m
Line 575:
 
=={{header|ColdFusion}}==
<lang cfm><cfscript>
function runmontyhall(num_tests) {
<cfscript>
// number of wins when player switches after original selection
function runmontyhall(num_tests) {
switch_wins = 0;
// number of wins when player switches after original selection
// number of wins when players "sticks" with original selection
switch_wins = 0;
stick_wins = 0;
// number of wins when players "sticks" with original selection
// run all the tests
stick_wins = 0;
for(i=1;i<=num_tests;i++) {
// run all the tests
// unconditioned potential for selection of each door
for(i=1;i<=num_tests;i++) {
doors = [0,0,0];
// unconditioned potential for selection of each door
// winning door is randomly assigned...
doors = [0,0,0];
winner = randrange(1,3);
// winning door is randomly assigned...
// ...and actualized in the array of real doors
winner = randrange(1,3);
doors[winner] = 1;
// ...and actualized in the array of real doors
// player chooses one of three doors
doors[winner] = 1;
choice = randrange(1,3);
// player chooses one of three doors
do {
choice = randrange(1,3);
// monty randomly reveals a door...
do {
shown = randrange(1,3);
// monty randomly reveals a door...
}
shown = randrange(1,3);
// ...but monty only reveals empty doors;
}
// he will not reveal the door that the player has choosen
// ...but monty only reveals empty doors;
// henor will nothe reveal the winning door that the player has choosen
while(shown==choice || doors[shown]==1);
// nor will he reveal the winning door
// when the door the player originally selected is the winner, the "stick" option gains a point
while(shown==choice || doors[shown]==1);
stick_wins += doors[choice];
// when the door the player originally selected is the winner, the "stick" option gains a point
// to calculate the number of times the player would have won with a "switch", subtract the
stick_wins += doors[choice];
// to calculate the number of times the player would have won with a "switch", subtract the// "value" of the chosen, "stuck-to" door from 1, the possible number of wins if the player
// if the player // chose and stuck with the winning door (1), the player would not have won by switching, so the value is 1-1=0
// the value is 1-1=0 if the player chose and stuck with a losing door (0), the player would have won by switching, so the value is 1-0=1
// have won by switching, so the value is 1-0=1
switch_wins += 1-doors[choice];
}
// finally, simply run the percentages for each outcome
stick_percentage = (stick_wins/num_tests)*100;
switch_percentage = (switch_wins/num_tests)*100;
writeoutput('Number of Tests: ' & num_tests);
writeoutput('<br />Stick Wins: ' & stick_wins & ' ['& stick_percentage &'%]');
writeoutput('<br />Switch Wins: ' & switch_wins & ' ['& switch_percentage &'%]');
}
// finally, simply run the percentages for each outcome
runmontyhall(10000);
stick_percentage = (stick_wins/num_tests)*100;
</cfscript>
switch_percentage = (switch_wins/num_tests)*100;
</lang>
writeoutput('Number of Tests: ' & num_tests);
writeoutput('<br />Stick Wins: ' & stick_wins & ' ['& stick_percentage &'%]');
writeoutput('<br />Switch Wins: ' & switch_wins & ' ['& switch_percentage &'%]');
}
runmontyhall(10000);
</cfscript></lang>
Output:
<pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.