public
Last active

An inclusive rand() for ruby 1.9

  • Download Gist
rand_incl.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class << Random
def rand_incl max=1.0
raise ArgumentError "maximum not greater than 0" if max <= 0.0
if @incl_lim.nil?
@incl_lim = (1 / Float::EPSILON).to_i
@incl_lim1 = @incl_lim + 1
end
if Float === max
max*rand(@incl_lim1) / @incl_lim
else
rand(max+1)
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.