Last active
September 14, 2019 23:23
-
-
Save GeoffCrittenden/af3c525c0de1539759df60991a082bc1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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