Things I don’t like seeing in R code 23

This code is a personal bugbear.

x <- -log(1 - runif(n))

Why is this bad? First off, it’s exactly equivalent to:

x <- -log(runif(n))

because if 𝑋∼𝑈(0,1), 1−𝑋∼𝑈(0,1) — and, yes I know about the very unlikely corner case.

But even better, it’s also equivalent to:

x <- rexp(n)

because as any fule kno, this is exactly the invert-the-cdf method for generating standard exponentials. Learn your distributions, folks.