Skip to content

Instantly share code, notes, and snippets.

@GeoffCrittenden
Last active September 14, 2019 23:23
Show Gist options
  • Save GeoffCrittenden/af3c525c0de1539759df60991a082bc1 to your computer and use it in GitHub Desktop.
Save GeoffCrittenden/af3c525c0de1539759df60991a082bc1 to your computer and use it in GitHub Desktop.
# inspired by Matt Parker's Frog Problem video
# https://www.youtube.com/watch?v=ZLTyX4zL2Fc
module Froggie
class << self
def calculate_average_jumps(n_frogs = 10_000, n_spots = 10)
total_jumps = 0.0
n_frogs.times { total_jumps += Frog.new(n_spots).jump! }
total_jumps / n_frogs
end
end
class Frog
def initialize(n_spots = 10)
@jumps_taken = 0
@remaining_spots = n_spots
end
def jump!
return @jumps_taken if @remaining_spots.zero?
@jumps_taken += 1
@remaining_spots -= (rand(@remaining_spots) + 1)
jump!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment