Monday, August 07, 2006

Scotch and Geeks

Fridays at my company generally end with myself and some of the developers sitting around one office shooting the shit and drinking a glass of scotch. Usually pretty harmless. This past Friday, the topic went to the discussion of Monty Hall probability. Check out the title link.

In any case, that got disturbing for a while when the arguments got a touch religious. Fortunately it died down and turned to politics and ended with my telling one of the developers with Bush Dementia Syndrome that he was an idiot. Not unusual when he makes unsupportable statements that allow me to put the boot in.

But back to Monty Hall. I got in this AM and one of the primary antagonists had gone and emailed this to those in the discussion.
#include

int
main(int argc, const char *argv[])
{
int rr; /* 0-2; identifies door that hides the Rolls Royce */
int choice; /* 0-2; identifies the player's initial choice */
int goat; /* 0-2; identifies the door opened by the host to reveal a goat */
int other; /* 0-2; identifies the other door that the player can select if he switches */

int no_switch_strategy = 0; /* number of Rolls Royces obtained by not switching */
int switch_strategy = 0; /* number of Rolls Royces obtained by switching */
int winners[3] = { 0 }; /* number of times that there are 0, 1 and 2 winning strategies */

int trial;

for (trial = 0; trial <>

/* place the Rolls Royce at random */

rr = rand() % 3;

/* player chooses at random */

choice = rand() % 3;

/* the host reveals a goat */

if (rr == choice) {

/* in this case the player has selected the Rolls Royce and we need to choose
randomly from the two goats */

goat = (rr + 1 + (rand() % 2)) % 3; /* think about it */

} else {

/* in this case the player has selected a goat and we reveal the other goat */

goat = 3 - choice - rr; /* sum of doors == 3 */

}

/* now set other to the door which is not the player's choice and not the revealed goat door */

other = 3 - choice - goat; /* sum of doors == 3 */

{
int wins = 0;

/* score both strategies */

if (choice == rr) {
no_switch_strategy++;
wins++;
}

if (other == rr) {
switch_strategy++;
wins++;
}

winners[wins]++;
}
}

printf("trials %d\n", trial);
printf("no switch strategy %d\n", no_switch_strategy);
printf("switch strategy %d\n", switch_strategy);
{
int i;
for (i = 0; i <>
printf("winners[%d] = %d\n", i, winners[i]);
}
}
}
With the results:
trials 1000000
no switch strategy 333409
switch strategy 666591
winners[0] = 0
winners[1] = 1000000
winners[2] = 0
Sometimes Geeks really baffle me.


No comments: