Skip to content

Instantly share code, notes, and snippets.

@dvisockas
Created September 18, 2018 20:26
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 dvisockas/f524859ea2529f2656f54e8e66f22782 to your computer and use it in GitHub Desktop.
Save dvisockas/f524859ea2529f2656f54e8e66f22782 to your computer and use it in GitHub Desktop.
'''
Implementation of a simulation of a St. Petersburg paradox
For more info, visit:
https://en.wikipedia.org/wiki/St._Petersburg_paradox
'''
class Toss
attr_reader :n
HEADS = 0
TAILS = 1
STAKE = 2
def initialize(n = 1)
@n = n
end
def winnings
action = [HEADS, TAILS].sample
if action == HEADS
Toss.new(n + 1).winnings
else
pot
end
end
def pot
2**n
end
end
n_steps = 100_000_000
wins = []
(1..n_steps).each do |time|
toss = Toss.new
wins << toss.winnings
end
puts(wins.sum / wins.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment