Reputation: 7066
So I want to generate a random float in Ruby which lies in the interval (0.0, 1.0)
. Obviously, if we were talking about a closed interval, this would be totally straightforward:
Random.rand
...but we are talking about the open interval, i.e. the number must never be exactly 0.0 or 1.0 because this would crash the program. Just to give a little background, the random float serves as an input to a statistical function which throws an error if the input is not in (0.0, 1.0)
.
I can think of obvious solutions, such as "rolling again" if the result is 0.0 or 1.0, but I would like to know if there is a more elegant way to do this.
Upvotes: 1
Views: 83
Reputation: 168091
Probably this is the best that can be done
Random.rand(Float::MIN...1.0)
Upvotes: 3