On Thu, Mar 7, 2013 at 8:55 AM, Robert Feldt <[email protected]> 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/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to