Created
September 18, 2018 20:26
-
-
Save dvisockas/f524859ea2529f2656f54e8e66f22782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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