Hi,

Random#rand and Kernel#rand are exclusive i.e. they return a value >= 0.0
but <max if max is a Float. What if one wants a rand that is inclusive of
the max? Would any of the below approaches guarantee to give an inclusive
range?

class Random
  # Return a random number between 0 (inclusive) and max (inclusive).
  def rand_incl(max = 1.0)
    if Float === max
      rand(max+Float::EPSILON)
    elsif Integer === max
      rand(max+1)
    end
  end

  # Same but with other strategy
  def rand_incl2(max = 1.0)
    if Float === max
      num_floats = (max - min)/Float::EPSILON
      min + rand(1+num_floats.to_i) * Float::EPSILON
    elsif Integer === max
      min + rand(max+1-min)
    end
  end
end

Thanks in advance,

Robert Feldt