I have a number of algorithms that depend on uniform random reals in half-open intervals such as $[0,1)$. In particular, I need a (pseudo) random-number generator that produces machine-precision numbers in the range $0.0$ to $1-\epsilon$. It can return $0.999...$, but will never return exactly $1.0$. I haven't found in the Mathematica documentation whether Mathematica's RandomReal satisfies this requirement. The documentation does state that RandomInteger[{xMin,xMax}] produces values in the double-closed interval $[x_{min}, x_{max}]$ inclusive of both ends, but I haven't found an equally clear statement about the real-number generators. The documentation that I've read just says "between 0 and 1." I could read this as double-open, but it really isn't precise enough for me. I would be grateful for an authoritative answer.
Tell me more
×
Mathematica Stack Exchange is a question and answer site for
users of Mathematica. It's 100% free, no registration required.
|
|
|||
| show 4 more comments |
|
Perhaps I'm missing some complexity to this issue but why can you not simply use:
The limit certainly appears to work. For example:
The first line shows that at least when using this restricted range the upper bound is closed. This shows that lower bound is closed as well:
|
|||||||||||||
|

1.0is possible, it is still a rare event, you could just write a function which tests for1.0and otherwise tries again, sayrightOpenRandomReal[] := Module[{rr = RandomReal[]}, If[rr == 1.0, rightOpenRandomReal[], rr]]– celtschk Oct 19 '12 at 14:40"Congruential", then yes, you will hit $0$ and/or $1$, depending on your choice of multiplier and modulus."MersenneTwister"might hit $0$, but it won't hit $1$. – J. M.♦ Oct 19 '12 at 14:56SeedRandom[Method -> "MersenneTwister"]will generate1.'s if the range is compressed, e.g.Count[RandomReal[{1 - 100 $MachineEpsilon, 1}, 1000], 1.]– Mr.Wizard♦ Oct 19 '12 at 17:40