diff options
Diffstat (limited to 'lib/interval.rb')
-rw-r--r-- | lib/interval.rb | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/interval.rb b/lib/interval.rb index 0aa787f..3ba7e09 100644 --- a/lib/interval.rb +++ b/lib/interval.rb @@ -1,7 +1,7 @@ class Interval def initialize(min = Float::INFINITY, max = -Float::INFINITY) - @min = min - @max = max + @min = min.to_f + @max = max.to_f end attr_reader :min, :max @@ -17,4 +17,14 @@ class Interval def surround?(x) min < x && x < max end + + def sample + rand(@min...@max) + end + + def clamp(x) + return @min if x < @min + return @max if x > @max + x + end end |