OTOH, without looking up exactly what Epsilon is (the smallest possible
absolute interval represented by a float?), wouldn't your technique fail if
max is big, because {big float} + {small float} = {big float} ?  I'd
suggest something clever with Rationals, personally, along the lines of
`(max*rand(range+1))/range"

Sent from my phone, so excuse the typos.
On Mar 7, 2013 5:56 PM, "Robert Feldt" <robert.feldt / gmail.com> wrote:

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