## Random numbers

### Normal distribution

In order to generate random numbers that follow a normal distribution, one first needs to create two uniform random numbers $u_{1}$ and $u_{2}$, in the range of 0 to 1. The Box–Muller transform can then be used to turn these into two Gaussian random numbers via

\begin{equation*} g_{1} = \sqrt{-2 \ln(u_{1})} \cos(2 \pi u_{2}) \end{equation*}

and

\begin{equation*} g_{2} = \sqrt{-2 \ln(u_{1})} \sin(2 \pi u_{2}) \end{equation*}

Note that $g_{1}$ and $g_{2}$ are independent, and two independent uniform random numbers will always be required for the algorithm to work, even if only one Gaussian random number is needed and calculation of the other one is omitted. The resulting normal distribution will have a standard deviation of $\sigma = 1$. In order to generate a distribution with a different standard deviation, $g_{1}$ and $g_{2}$ can simply be multiplied by the desired $\sigma$.

### Power law

Random numbers following a power law, $P(x) \propto x^{n}$, can be generated from a single uniform random number, $u$, in the range of 0 to 1 through the following transform:

\begin{equation*} p = \left[ \left( x_{1}^{n + 1} - x_{0}^{n + 1} \right) u + x_{0}^{n + 1} \right]^{1 / (n + 1)} \end{equation*}

Here, $x_{0}$ and $x_{1}$ are the lower and upper limit of the range across which the distribution is generated, and $n$ is the power.

### Random 3D vector

In order to generate vectors that point in random directions, one can use an equal-area projection of the sphere onto a cylinder. First, one needs to generate two uniform random deviates in the range of $0 \leq \vartheta < 2 \pi$ and $-1 \leq z \leq 1$. The resulting random unit vector can then be written as

\begin{equation*} \vec{v} = \left( {\begin{array}{c} \sqrt{1 - z^{2}} \cos(\vartheta) \\ \sqrt{1 - z^{2}} \sin(\vartheta) \\ z \end{array}} \right) \end{equation*}

The resulting vectors will point in random directions and will be uniformly distributed on the unit sphere.

An alternative method is to generate three random Cartesian vector components, $x$, $y$, and $z$, each in the range of $-1$ to $1$, and discard all those vectors whose absolute value is $\sqrt{x^{2} + y^{2} + z^{2}} > 1$. The advantage of this method is that it avoids the calculation of computationally expensive trigonometric and square root functions, but at the expense of having to generate (and discard) more random numbers.