Random number generation (RNG) is the process by which a sequence of random numbers may be drawn. When using a computer to draw the random numbers, the numbers are not completely random. The notion of “completely random” is nonsensical because of the infinitude of numbers. Random numbers must be drawn from some probability distribution. Furthermore, in most computer applications, the random numbers are actually pseudorandom. They depend entirely on an input seed and are then generated by a deterministic algorithm from that seed.
So what do (pseudo)random number generators do? RNGs are capable of (approximately) drawing integers from a Discrete Uniform distribution. For example, NumPy’s default built-in generator, the PCG64 generator, generates 128 bit numbers, allowing for \(2^{128}\), or about \(10^{38}\), possible integers. Importantly, each draw of of a random integer is (approximately) independent of all others.
In practice, the drawn integers are converted to floating-point numbers (since a double floating-point number has far less than 128 bits) on the interval [0, 1) by dividing a generated random integer by \(2^{138}\). Effectively, then, the random number generators provide draws out of a Uniform distribution on the interval [0, 1).
To convert from random numbers on a Uniform distribution to random numbers from a nonuniform distribution, we need a transform. For many named distributions convenient transforms exist. For example, the Box-Muller transform is often used to get random draws from a Normal distribution. In the absence of a clever transform, we can use a distribution’s quantile function, also known as a percent-point function, which is the inverse CDF, \(F^{-1}(y)\). For example, the quantile function for the Exponential distribution is
where \(p\) is the value of the CDF, ranging from zero to one. We first draw \(p\) out of a Uniform distribution on [0, 1), and then compute \(F^{-1}(p)\) to get a draw from an Exponential distribution. A graphical illustration of using a quantile function to draw 50 random numbers out of Gamma(5, 2) is shown below.
Each draw from a Uniform distribution, marked by an × on the vertical axis, is converted to a draw from a Gamma distribution, marked by ○ on the horizontal axis, by computing the inverse CDF. Because the draws of the random integers from which the draws from a Uniform distribution on \([0, 1)\) are independent, we get independent draws from the Gamma distribution.