# Should I ignore $0$ when do inverse transform sampling?

Generic method

1. Generate $U \sim \mathrm{Uniform}(0,1)$.
2. Return $F^{-1}(U)$.

So, in step 1, $U$ has domain/support as $[0,1]$, so it is possible that $U=0$ or $U=1$, but $F^{-1}(0)=-\infty$. Should I reject the value $U=0$ and $U=1$ before applying step 2?

For example, discrete distribution sampling: $X$ takes on values $x_1, x_2, x_3$ with probability $p_1,p_2,p_3$

1. Generate $U \sim \mathrm{Uniform}(0,1)$.
2. Find the smallest $k$ such that $F(x_k)\geq U$ ($F$ is the CDF).

However, if $U=0$, and $p_1=0$, $k$ would be $1$. It could generate $x_1$ though its probability $p_1=0$. Is it acceptable?

-
If $U$ is generated as a sample of a random variable uniformly distributed on $(0,1)$, then $0$ and $1$ can't be values returned, can they? As a practical matter, congruential random number generators don't return a value of $0$ at all. I don't recall off the top of my head if it is possible for them to return $1$, but I suspect the answer is No there too. – Dilip Sarwate Mar 2 '12 at 23:13
@DilipSarwate: All linear congruential PRNGs I have seen are actually affine congruential and will generate $0$ just fine. Commonly, generators of "equidistributed between $0$ and $1$" random numbers actually produce something like $a\cdot 2^{-53}$ where $a$ is an integer equidistributed in $0\le a<2^{53}$, so they can return $0$ but $1$ exactly. – Henning Makholm Mar 2 '12 at 23:24
The results $0$ and $1$ occur with negligible probability. So as a practical fact, it doesn't matter what you choose to do. – André Nicolas Mar 2 '12 at 23:25
@André Nicolas: however, in a program, when Random.next() generates values [0,1), if it happens to output 0... the whole program I build upon the sampling algorithm would crash. – colinfang Mar 2 '12 at 23:27
@By "it doesn't matter" I meant that there is negligible potential error in the simulation. But there may be very good programming reasons not to allow $0$. The only potential issue is that a numerical procedure that crashes at $0$ may be unreliable near $0$, which is a much more serious matter. – André Nicolas Mar 2 '12 at 23:33

In theory, it doesn't matter: the event $U=0$ occurs with probability $0$, and can thus be ignored. (In probabilistic jargon, it almost never happens.)
In practice, it's possible that your PRNG may return a value that is exactly $0$. For a reasonably good PRNG, this is unlikely, but it may not be quite unlikely enough for you to bet that it never happens. Thus, if your program would crash on $U=0$, a prudent programmer should check for that event and generate a new random number if it occurs.
(Note that many PRNG routines are defined to return values in the range $0 \le U < 1$: for example, the Java default PRNG is defined to return values of the form $m\cdot2^{-53}$ for $m \in \{0,1,\dotsc,2^{53}-1\}$. If you're using such a PRNG, a cheap but effective way to avoid the case $U=0$ is to use the number $1-U$ instead of $U$.)