# RandomReal closed on left & open on right?

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.

-
Well, which algorithm are you using, first of all? –  Guess who it is. Oct 19 '12 at 14:38
Given that if 1.0 is possible, it is still a rare event, you could just write a function which tests for 1.0 and otherwise tries again, say rightOpenRandomReal[] := Module[{rr = RandomReal[]}, If[rr == 1.0, rightOpenRandomReal[], rr]] –  celtschk Oct 19 '12 at 14:40
... I was asking about which PRNG generator are you using. If, e.g., you're using "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$. –  Guess who it is. Oct 19 '12 at 14:56
@J.M.: How do you know that reals generated with the Mersenne Twister won't take the value 1? –  celtschk Oct 19 '12 at 17:38
@celtschk FWIW SeedRandom[Method -> "MersenneTwister"] will generate 1.'s if the range is compressed, e.g. Count[RandomReal[{1 - 100 $MachineEpsilon, 1}, 1000], 1.] – Mr.Wizard Oct 19 '12 at 17:40 ## 1 Answer Perhaps I'm missing some complexity to this issue but why can you not simply use: RandomReal[1 -$MachineEpsilon, 10]


The limit certainly appears to work. For example:

Count[RandomReal[{1 - 100 $MachineEpsilon, 1}, 5*^6], 1.]  37447  Count[RandomReal[{1 - 100$MachineEpsilon, 1 - $MachineEpsilon}, 5*^6], 1.]  0  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: Count[RandomReal[{1 - 100$MachineEpsilon, 1}, 5*^6], 1 - 100 $MachineEpsilon]  12433  - I essentially asked this in a comment, before I edited it out and deleted it. The reason was because upon closer reading, I realized that the OP was asking for an authoritative reference on how RandomReal works, not an implementation — "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." Your answer assumes that RandomReal generates on a closed interval, but you have no proof. – The Toad Oct 19 '12 at 17:56 @rm-rf "Your answer assumes that RandomReal generates on a closed interval, but you have no proof." My answer shows plainly that at least when given a tightly restricted range it does. – Mr.Wizard Oct 19 '12 at 17:58 @rm-rf Now I think you're just being stubborn. Clearly I have shown that the 1 -$MachineEpsilon and 1 produce different results when used as the upper bound. Exactly what are you looking for? –  Mr.Wizard Oct 19 '12 at 18:07
@Mr.Wizard, I think, for practical purposes, your answer solves my problem. I will wait a little bit before marking it to see if some new insight appears. BTW, your use of Count and 1-100 \$MachineEpsilon is very useful and helpful. –  Reb.Cabin Oct 19 '12 at 18:41
Assuming that internal PRNGs follow the same rules which are imposed on self-defined PRNGs, the following quote from Random Number Generation should be at least a strong hint that the interval borders are included: "If random reals are supported, then gobj["GenerateReals"[n, {a, b}, prec]] is expected to return a list of n random reals with precision prec in the range [a,b]" — note the square brackets. –  celtschk Oct 19 '12 at 19:43