Solution (source code)

= Solution

For rate $\lambda>0$, return
$$
\boxed{X=-\frac1\lambda\log U_1.}
$$
For $x\geq0$, $\mathbb P(X>x)=\mathbb P(U_1<e^{-\lambda x})=e^{-\lambda x}$, so $X$ has the <exponential distribution> of rate $\lambda$. This is <inverse transform sampling>; using $-\log(1-U_1)/\lambda$ is equivalent. Uniform draws at exact endpoints can be excluded, since those events have <probability> zero under the ideal continuous law.