Wednesday, 20 April 2011

What if Alice used Java?

There was a cool puzzle posted on xkcd blag not so long ago. Well, it was actually more than a year ago... Anyway, the puzzle goes like this:
Alice secretly picks two different real numbers by an unknown process and puts them in two (abstract) envelopes. Bob chooses one of the two envelopes randomly (with a fair coin toss), and shows you the number in that envelope. You must now guess whether the number in the other, closed envelope is larger or smaller than the one you’ve seen.

Is there a strategy which gives you a better than 50% chance of guessing correctly, no matter what procedure Alice used to pick her numbers?
I decided to check what would happen if the unknown and mysterious process of Alice was in fact Java pseudo-random generator.

As suggested in the first comment of the blag the method for decision making was to use a logistic function. This function approaches 0 at negative infinity and 1 at positive infinity. Once we saw a number form the envelope, we calculate its logistic value p(x). Then we guess lower with probability p(x) or higher with p(x)'.

Logistic (sigmoid) function

For the purpose of this task I also used Java to get our proability of choosing one or another. To be honest, I expected the accuracy of the guessing to be a little bit over 50%.

Well, here is what I got (after one billion runs):
Score: 750,013,026
Accuracy: 75.0013026%
It is surprisingly close to 75% as though it converged at that point.

The other thing worth noticing is the slope of sigmoid function - it is practically as saying: "Guess lower if my number is greater than 7, or higher if it is less than -7 (roughly)". I will later change it to have a simple threshold function: "Guess lower/higher if greater/less than 0". Just to see what impact does it have.

And here is the code I used to simulate it:

No comments:

Post a Comment