Skip to content

Instantly share code, notes, and snippets.

@ahultgren
Last active December 19, 2015 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahultgren/6033661 to your computer and use it in GitHub Desktop.
Save ahultgren/6033661 to your computer and use it in GitHub Desktop.
Once upon a time there was a show where a guest could win The Grand Prize™.

Once upon a time there was a show where a guest could win The Grand Prize™. To do so he/she just had to pick one box of three. Of these three boxes, two were empty. Thus, only one very randomly chosen box contained the grand prize. When the guest had picked a box of his/her liking, however, there was a twist. The host of the show, who knew which box was the very randomly chosen one, would remove a box that was empty. Now the guest was faced with the option to keep the already picked box, or swap to the other one. What is the correct thing to do?

Well, according to common sense, it really shouldn't matter. Math disagrees though. According to math, since we from the beginning know that the chosen box had 1/3 chance of containing the grand prize, and the second box is removed, the last one has to have the rest of the 2/3s of chance. Since this is obviously ridiculous I decided to once and for all, for the greater sanity of the world, actually test it.

And, in short, to the dismay of my common sense, math prevailed.

// Disclaimer: this code is very crude, do not write real code like this
function test () {
// Save results
var first = [], second = [], third = [];
// Do the test a few times
for(var i = 100; i--;) {
// Create new set of boxes
var boxes = [0, 0, 0];
// Make one of the boxes truthy
var right = rand(2);
boxes[right] = 1;
// Pick one box at random
var pick1 = rand(2);
first.push(boxes[pick1]);
// Mark a falsy box
var wrong = rand(2);
while(wrong === right || wrong === pick1) {
wrong = rand(2);
}
// Pick the one that was not chosen first and not marked as wrong
var pick2 = rand(2);
while(pick2 === wrong || pick2 === pick1) {
pick2 = rand(2);
}
second.push(boxes[pick2]);
// Pick one at random of the boxes not marked as wrong
var pick3 = rand(2);
while(pick3 === wrong) {
pick3 = rand(2);
}
third.push(boxes[pick3]);
}
return [first, second, third];
}
// Do the test a few times
var result = [];
for(var l = 100; l--;) {
result.push(test());
}
// Log the average
var sum = result.reduce(function (memo, a, i) {
memo[0] = memo[0] + a[0].filter(truthy).length;
memo[1] = memo[1] + a[1].filter(truthy).length;
memo[2] = memo[2] + a[2].filter(truthy).length;
return memo;
}, [0, 0, 0]);
console.log(sum[0]/100, sum[1]/100, sum[2]/100);
// If math is correct it should log close to: 33.33 66.66 50.00
/* Helpers
============================================================================= */
function rand (max) {
return ~~(Math.random() * (max + 1));
}
function truthy (a) {
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment