# Probability Distribution Transformations

Random number generators generate random numbers that tend to follow some kind of distribution. Uniform number generators generate a random number between 0 and 1 with equal likely hood everywhere in the range, Gaussian generators generate numbers that have higher probability to be near zero, etc. These distributions are given functions ($\inline&space;p(x)=1$ for uniform or $\inline&space;p(x)=e^{-x^2/2}$ for Gaussian). In order to calculate the probability that any number generated will fall in the range $\inline&space;[a,b]$, one would take the integral $\inline&space;\int_{a}^{b}p(x)\textup{d}x$. The probability to get a specific number $\inline&space;x$ would be $\inline&space;p(x)\textup{d}x$ which is just 0.

However, most programs and methods only allow for generation of certain probability distributions like the one mentioned above. There exists few random number generators with the distribution, for example, $\inline&space;p(x)=x^2$. Numbers with this distribution can be generated by using transformations on generated numbers. This transformation $\inline&space;y(x)$ can be determined in the following way.

Assume one wishes to transform a number $\inline&space;x$ given from probability distribution $\inline&space;p_1(x)$ to $\inline&space;y(x)$ which has distribution over many generations of, then that means that the probability of obtaining $\inline&space;x$ should equal the probability of obtaining $\inline&space;y(x)$. Written using equations, this equivalence can be stated as $\inline&space;p_1(x)\textup{d}x=p_2(y)\textup{d}y$. Integrating both sides yields $\inline&space;P_1(x)+C=P_2(y)$. Taking the inverse function of the integral of the second distribution on both sides yields the required transformation as shown below.

$y(x)=P_2^{-1}(P_1(x)+C)$

The constant $\inline&space;C$ merely serves as a way to shift the bounds of the distribution so it could yield numbers in any range because any probability distribution needs either bounds set or a normalization factor that causes the area under the curve to be 1. This means any probability distribution can be obtained using just a uniform generator. Transforming to or from the Gaussian actually becomes complicated due to the error function (integral of Gaussian) being non-elementary (if you want to know a good method for transformation to Gaussian, look at Box-Fuller Transform). The following shows an example transformation from uniform to $\inline&space;p(x)=x^2$.

$P_1(x)=x$

$P_2(y)=\frac{y^3}{3},&space;P_2^{-1}(y)=\sqrt[3]{3y}$

$y(x)=\sqrt[3]{3x}$