From: Robert Feldt Date: 2013-03-07T16:55:54+09:00 Subject: Random#rand_inclusive? --047d7beb95c01c1a9504d7510986 Content-Type: text/plain; charset=ISO-8859-1 Hi, Random#rand and Kernel#rand are exclusive i.e. they return a value >= 0.0 but
Random#rand and Kernel#rand are exclusive i.e. they = return a value >=3D 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 gua= rantee to give an inclusive range?

class Random
=A0 # Return a random numbe= r between 0 (inclusive) and max (inclusive).
=A0 def rand_incl(ma= x =3D 1.0)
=A0 =A0 if Float =3D=3D=3D max
=A0 =A0 =A0 r= and(max+Float::EPSILON)
=A0 =A0 elsif Integer =3D=3D=3D max
=A0 =A0 =A0 rand(max+1)<= /div>
=A0 =A0 end
=A0 end

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

Thanks in advance,

Robert Feldt

--047d7beb95c01c1a9504d7510986--