Skip to content

Instantly share code, notes, and snippets.

@cpfiffer
Created February 24, 2018 20:11
Show Gist options
  • Save cpfiffer/c5635ccb4497ec91046124b00f1283b3 to your computer and use it in GitHub Desktop.
Save cpfiffer/c5635ccb4497ec91046124b00f1283b3 to your computer and use it in GitHub Desktop.
For the Riddler.
# From Keith Wynroe, a battle for the tokens:
#
# You have one token, and I have two tokens. Naturally, we both crave more tokens, so we play a game of skill that unfolds over a number of rounds in which the winner of each round gets to steal one token from the loser. The game itself ends when one of us is out of tokens — that person loses. Suppose that you’re better than me at this game and that you win each round two-thirds of the time and lose one-third of the time.
#
# What is your probability of winning the game?
@everywhere function play_game()
p1 = 2
p2 = 1
play = true
while play
prob = rand()
if prob >= 2/3
p2 = p2 + 1
p1 = p1 -1
else
p2 = p2 - 1
p1 = p1 +1
end
if p1 == 0 || p2 == 0
play = false
end
end
if p1 == 0
return 0
elseif p2 == 0
return 1
end
end
function play_games(times = 10000)
num = 0
num = @parallel (+) for i = 1:times
play_game()
end
p1_ratio = num/times
p2_ratio = 1-p1_ratio
println("Player 2 won $p2_ratio times out of $times rounds.")
end
play_games(10000000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment