Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
Created January 9, 2014 23:49
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 chris-taylor/8344336 to your computer and use it in GitHub Desktop.
Save chris-taylor/8344336 to your computer and use it in GitHub Desktop.
You roll a dice up to three times. After each of the first two rolls you can choose to accept the number showing on the dice in dollars, or roll again. What is your expected winnings if you play optimally?
import Control.Probability
expected p = expectation (runProb p)
die = uniform [1..6] :: Bayes Double Integer
game n
| n == 0 = die
| otherwise = do
x <- die
if fromInteger x > expected next
then return x
else next
where
next = game (n-1)
{--------------------
>> printProb $ game 2
1 5.56%
2 5.56%
3 5.56%
4 16.67%
5 33.33%
6 33.33%
>> expected $ game 2
4.66666666666667
---------------------}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment