the singularity of being and nothingness
Monty Hall Problem…in ColdFusion
If you saw my post about the Monty Hall Problem, you know that calculating the probabilities in this puzzle, while not terrifically complicated, are nonetheless a bit frustrating to our feelings of common sense. Therefore, I decided to whip up a quick script that would simulate the Monty Hall problem in code.
It turns out that this has been done many times before, and if you head on over to rosettacode.org, you can find scripts for nearly every language you could imagine…except ColdFusion.
Thinking this to be an incredible travesty, I have produced a CF version of the problem, and have posted it over at rosettacode.org.
If you’re too lazy to click a link, however, here is the script in its entirety:
<cfscript> function runmontyhall(num_tests) { // number of wins when player switches after original selection switch_wins = 0; // number of wins when players "sticks" with original selection stick_wins = 0; // run all the tests for(i=1;i<=num_tests;i++) { // unconditioned potential for selection of each door doors = [0,0,0]; // winning door is randomly assigned... winner = randrange(1,3); // ...and actualized in the array of real doors doors[winner] = 1; // player chooses one of three doors choice = randrange(1,3); do { // 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 // nor will he reveal the winning door while(shown==choice || doors[shown]==1); // when the door the player originally selected is the winner, the "stick" option gains a point 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 chose and stuck with the winning door (1), the player would not have won by switching, so 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 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 &'%]'); } runmontyhall(10000); </cfscript>
This produces:
Tests: 10,000 | Switching wins: 6655 [66.55%] | Sticking wins: 3345 [33.45%]
Print article | This entry was posted by existdissolve on June 6, 2010 at 8:56 am, and is filed under ColdFusion, Cool Stuff. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |