Skip to content

Instantly share code, notes, and snippets.

@zackster
Created June 17, 2018 15:42
Show Gist options
  • Save zackster/cf7a1e6b70d41b8fcb4876f72ab3ae35 to your computer and use it in GitHub Desktop.
Save zackster/cf7a1e6b70d41b8fcb4876f72ab3ae35 to your computer and use it in GitHub Desktop.
class Array
def sum
inject(0.0) { |result, el| result + el }
end
def mean
sum / size
end
end
def flip
if (rand * 2).ceil == 1
'heads'
else
'tails'
end
end
def simulate_alice
alice_tries = []
alice_not_found = true
while alice_not_found
alice_tries << flip
if alice_tries.count >= 2 && alice_tries[-2] == 'heads' && alice_tries[-1] == 'tails'
alice_not_found = false
end
end
alice_tries.count
end
def simulate_bob
bob_tries = []
bob_not_found = true
while bob_not_found
bob_tries << flip
if bob_tries.count >= 2 && bob_tries[-2] == 'heads' && bob_tries[-1] == 'heads'
bob_not_found = false
end
end
bob_tries.count
end
puts 10000.times.map { simulate_alice }.mean
puts 10000.times.map { simulate_bob }.mean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment