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.
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
-
$\begingroup$ 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 thatRandomReal
generates on a closed interval, but you have no proof. $\endgroup$– rm -rf ♦Oct 19, 2012 at 17:56 -
$\begingroup$ @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. $\endgroup$ Oct 19, 2012 at 17:58
-
$\begingroup$ @rm-rf Now I think you're just being stubborn. Clearly I have shown that the
1 - $MachineEpsilon
and1
produce different results when used as the upper bound. Exactly what are you looking for? $\endgroup$ Oct 19, 2012 at 18:07 -
6$\begingroup$ 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. $\endgroup$– celtschkOct 19, 2012 at 19:43 -
1$\begingroup$ I love Mma, but this is an example where inadequacy of documentation combined with lack of access to the source leaves us guessing about something where there should be no guessing. $\endgroup$– AlanJan 19, 2016 at 17:36
1.0
is possible, it is still a rare event, you could just write a function which tests for1.0
and otherwise tries again, sayrightOpenRandomReal[] := Module[{rr = RandomReal[]}, If[rr == 1.0, rightOpenRandomReal[], rr]]
$\endgroup$"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$. $\endgroup$SeedRandom[Method -> "MersenneTwister"]
will generate1.
's if the range is compressed, e.g.Count[RandomReal[{1 - 100 $MachineEpsilon, 1}, 1000], 1.]
$\endgroup$