# How do I generate Maxwell-Boltzmann variates using a uniform distribution random number generator?

I am doing a molecular dynamics simulation. I need to assign initial velocities to the atoms. I want to assign the initial velocities which follow the Maxwell-Boltzmann distribution. How do I calculate such initial velocities using a uniform random number generator with range [0,1)?

## 1 Answer

The initial velocities are drawn from a Gaussian distribution with variance $$\sigma_i^2=\frac{k_{\textrm{B}}T}{m_i},$$ where $k_{\textrm{B}}$ denotes Boltzmann's constant, $T$ is the temperature and $m_i$ is the mass of the $i^{\textrm{th}}$ particle.

Thus, the problem boils down to generate random numbers from a gaussian distribution using uniformly distributed random numbers. This is fortunately quite simple: the Wikipedia article https://en.wikipedia.org/wiki/Box–Muller_transform shows some very common algorithms how to transform uniform random numbers into gaussian random numbers.

Let's put everything together: every component of the velocity of the $i^{\textrm{th}}$ particle is computed via $$v_{i,\alpha}=\sqrt{\frac{k_{\textrm{B}}T}{m_i}}\,\mathcal{N}(0,1)\,,\quad\alpha\in\{x,y,z\}\,,$$ where $\mathcal{N}(0,1)$ is a gaussian random number with variance 1 and mean 0.

With this definition, each velocity component follows a gaussian distribution $$\pi(v_\alpha)\textrm{d}v_\alpha\propto\exp\left(-\frac{v_\alpha^2}{2\sigma^2}\right)\textrm{d}v_\alpha\,,$$ but when you write the distribution of the velocity vector in spherical coordinates and integrate the angular components, you obtain $$\pi(v)\textrm{d}v \propto v^2\exp\left(-\frac{v^2}{2\sigma^2}\right)\textrm{d}v\,,$$ which is the desired Maxwell-Boltzmann distribution.

• If masses of all particles are same, these velocities are from scaled gaussian distribution, not maxwell-boltzmann distribution. Or, am I missing something? – Yogesh Yadav Jun 18 '15 at 14:30
• If all masses are the same, you draw all velocity components for all particles from the same scaled gaussian distribution. The distribution of particle velocities that you generate this way follows the Maxwell-Boltzmann distribution. – Christoph Wehmeyer Jun 18 '15 at 14:37
• I added a short explanation why a gaussian distribution of the velocity vector components yields a Maxwell-Boltzmann distribution for the particle velocity, i.e., the length of the velocity vector. – Christoph Wehmeyer Jun 18 '15 at 16:21