# Gaussian random variable and CLT To generate two independent Gaussian random variables $x$ and $y$ we use these equation \begin{equation} x = \sigma \sqrt{-2ln\xi_1}\cos(2\pi \xi_2) \quad,\quad y = \sigma \sqrt{-2ln\xi_1}\sin(2\pi \xi_2) \end{equation} This figure show total number $N=10^5$ Gaussian random numbers. ![](https://i.imgur.com/OFfEbQZ.png) We can plot the semi-log to again verified the result. ![](https://i.imgur.com/ry4Uxc2.png) For generating two independent Gaussian variables $x_1$ and $x_2$ to form a distribution $y=x_1+x_2$, we use same approach above.I then use KDE (kernel density estimation) to fit two Gaussian distribution. ![](https://i.imgur.com/ej8scWr.png) Interesting happened when I generate $n$independent uniform random numbers $\xi_1$, $\xi_2$, ..., $\xi_n$, then I measure the probability of the random variable $y=\sigma_{k=1}^{n} \xi_k$. As you can see when $n=1$ it is just a uniform random distribution, however when $n=2$ or even larger number, the distribution looks gradually like Gaussian distribution. At $n=20$, it looks like a pretty decent Gaussian distribution which I use theoretical result which states that \begin{equation} \mu = \dfrac{1}{2} \quad \sigma_n^2 = \dfrac{1}{12n} \end{equation} ![](https://i.imgur.com/e2c0jLf.png) ![](https://i.imgur.com/j58gVj2.png) ![](https://i.imgur.com/tnYQ0rh.png) ![](https://i.imgur.com/WF0rRnI.png) ![](https://i.imgur.com/NHE81mu.png) ![](https://i.imgur.com/7s6CEci.png) I then use totally different random distribution consist of one-third of exponential distribution, another one-third of uniform distribution and one-third of Gaussian distribution. The parameter are given below. \begin{equation} P_1(x)=0.2 e^{-0.2x} \quad P_2(x)=1 \quad P_3(x)=\dfrac{1}{\sqrt{2\pi\times 0.2}}e^{-\dfrac{(x-\mu)^2}{2\times 0.2}} \end{equation} As you can see it become Gaussian again when n is larger (roughly at $n=10$). ![](https://i.imgur.com/ZkzekQZ.png) ![](https://i.imgur.com/iDRRF2B.png) ![](https://i.imgur.com/K4lVSTA.png) ![](https://i.imgur.com/GiOUz0x.png) ![](https://i.imgur.com/qJxjoKk.png) ## Code ### Gaurand ```fortran= program main implicit none double precision, external :: gaurand integer, parameter :: n=100000 double precision :: x(n), sig=dsqrt(.2d0) integer :: i do i = 1, n x(i) = gaurand(sig) end do open(66, file='a.dat', status='unknown') do i = 1, n write(66, *) x(i) end do end program main function gaurand(sig) implicit none double precision, parameter :: tpi=8.d0*atan(1.d0) double precision :: a, b, gaurand, sig call random_seed() call random_number(a) call random_seed() call random_number(b) gaurand = sig * dsqrt(-2.d0 * log(a)) * dcos(tpi * b) end function ``` ### CLT ```fortran= program clt implicit none integer, parameter :: n=1000, m=50 double precision :: x(n, m), y(n) integer :: i, j open(66, file='c50.dat', status='unknown') call random_seed() do i = 1, n call random_number(x) end do y = 0.d0 do j = 1, m y(:) = y(:) + x(:, j) end do y = y / m do i = 1, n write(66, *) y(i) end do end program clt ``` ```fortran= program clt implicit none double precision, external :: gaurand integer, parameter :: n=10000, m=3 double precision :: x1(n, m), x2(n, m), x3(n, m), y(n) integer :: i, j double precision :: a = .2d0 open(66, file='d3.dat', status='unknown') call random_seed() call random_number(x1) x1 = - 1.d0 / a * dlog(x1) call random_number(x2) do j = 1, m do i = 1, n x3(i, j) = gaurand(dsqrt(.2d0)) end do end do y = 0.d0 do j = 1, m y(:) = y(:) + x1(:, j) + x2(:, j)+ x3(:, j) end do y = y / m do i = 1, n write(66, *) y(i) end do end program clt function gaurand(sig) implicit none double precision, parameter :: tpi=8.d0*atan(1.d0) double precision :: a, b, gaurand, sig call random_seed() call random_number(a) call random_seed() call random_number(b) gaurand = sig * dsqrt(-2.d0 * log(a)) * dcos(tpi * b) end function ```