Gaussian random number generator.
This function returns a deviate of a Gaussian distribution
with mean , and standard deviation .
We use the Inverse Transform Sampling Method for sampling . With this method we get , where and are uniform random numbers in the interval .
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=rp), | intent(in) | :: | mu | Mean value of the Gaussian distribution. |
||
real(kind=rp), | intent(in) | :: | sigma | Standard deviation of the Gaussian distribution. |
Sampled number from the Gaussian distribution .
FUNCTION random_norm(mu,sigma)
!! @note Gaussian random number generator. @endnote
!! This function returns a deviate of a Gaussian distribution
!! $$f_G(x;\mu,\sigma) =
!! \frac{1}{\sigma\sqrt{2\pi}} \exp{\left( -(x-\mu)^2/2\sigma^2 \right)},$$
!!
!! with mean \(\mu\), and standard deviation \(\sigma\).
!!
!! We use the Inverse Transform Sampling Method for sampling \(x\).
!! With this method we get \(x = \sqrt{-2\log{(1-y)}}\cos(2\pi z)\),
!! where \(y\) and \(z\) are uniform random numbers in the interval \([0,1]\).
REAL(rp), INTENT(IN) :: mu
!! Mean value \(\mu\) of the Gaussian distribution.
REAL(rp), INTENT(IN) :: sigma
!! Standard deviation \(\sigma\) of the Gaussian distribution.
REAL(rp) :: random_norm
!! Sampled number \(x\) from the Gaussian distribution \(f_G(x;\mu,\sigma)\).
REAL(rp) :: rand1
!! Uniform random number in the interval \([0,1]\).
REAL(rp) :: rand2
!! Uniform random number in the interval \([0,1]\).
call RANDOM_NUMBER(rand1)
call RANDOM_NUMBER(rand2)
random_norm = SQRT(-2.0_rp*LOG(1.0_rp-rand1))*COS(2.0_rp*C_PI*rand2);
END FUNCTION random_norm