TITLE: prove the way to generate geometrically distributed random numbers
QUESTION [8 upvotes]: The way to generate geometrically distributed random numbers is the following
$$\lfloor{\ln(u)/\ln(1-p)}\rfloor$$
where $u$ is uniformly distributed in $[0,1]$ and $p$ is the parameter in the geometric distribution. 
But can anybody help provide a rigorous proof? I only see that $\ln(u)/\ln(1-p)$ is exponentially distributed, but how to get the geometric distribution?

REPLY [7 votes]: Let us, as is customary, use capital letters for random variables, and consider 
$$
N=\lfloor{\ln(U)/\ln(1-p)}\rfloor,
$$
where $U$ is uniform on $(0,1)$. Then $N$ is a random nonnegative integer and, for every $n\geqslant0$, the event $A_n=\{N=n\}$ is
$$
A_n=\{(n+1)\ln(1-p)\lt\ln U\leqslant n\ln(1-p)\}.
$$
(Note that $\ln(1-p)$ is negative.) Thus,
$$
A_n=\{(1-p)^{n+1}\lt U\leqslant(1-p)^n\}.
$$
For every $u\lt v$ in $(0,1)$, $P[u\lt U\leqslant v]=v-u$ hence 
$$
P[N=n]=P[A_n]=(1-p)^n-(1-p)^{n+1}=p(1-p)^n.
$$
This proves that the distribution of $N$ is geometric (with parameter $p$ or $1-p$, depending on the convention used).