On Thu, Mar 7, 2013 at 8:55 AM, Robert Feldt <robert.feldt / gmail.com> wrote:

> 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?

I don't think there is any good way to achieve that for floats.  You
could try rounding but this would effectively mean to reduce the
number of significant decimals.

def rand_incl(float, decimals = 10)
  rand(float).round(decimals)
end

I would have to reason a bit longer about what that does to
statistical distribution of values.  It might still not be exactly
evenly distributed - especially since the edge values have fewer
values to round to (i.e. negatives are missing and values >= max).
Nah, forget it: does not look like a good idea.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/