15
$\begingroup$

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.

$\endgroup$
11
  • $\begingroup$ Well, which algorithm are you using, first of all? $\endgroup$ Oct 19, 2012 at 14:38
  • 3
    $\begingroup$ 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]] $\endgroup$
    – celtschk
    Oct 19, 2012 at 14:40
  • 1
    $\begingroup$ ... 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$. $\endgroup$ Oct 19, 2012 at 14:56
  • 1
    $\begingroup$ @J.M.: How do you know that reals generated with the Mersenne Twister won't take the value 1? $\endgroup$
    – celtschk
    Oct 19, 2012 at 17:38
  • 1
    $\begingroup$ @celtschk FWIW SeedRandom[Method -> "MersenneTwister"] will generate 1.'s if the range is compressed, e.g. Count[RandomReal[{1 - 100 $MachineEpsilon, 1}, 1000], 1.] $\endgroup$
    – Mr.Wizard
    Oct 19, 2012 at 17:40

1 Answer 1

12
$\begingroup$

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
$\endgroup$
7
  • $\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 that RandomReal 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$
    – Mr.Wizard
    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 and 1 produce different results when used as the upper bound. Exactly what are you looking for? $\endgroup$
    – Mr.Wizard
    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$
    – celtschk
    Oct 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$
    – Alan
    Jan 19, 2016 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.