Fixed an issue with the random Maxwellian subroutines

This commit is contained in:
Jorge Gonzalez 2026-02-04 13:52:24 +01:00
commit 23bba31005
2 changed files with 15 additions and 22 deletions

View file

@ -48,24 +48,23 @@ MODULE moduleRandom
END FUNCTION randomIntAB
!Returns a random number in a Maxwellian distribution of mean 0 and width 1 with the Box-Muller Method
FUNCTION randomMaxwellian() RESULT(rnd)
USE moduleConstParam, ONLY: PI
IMPLICIT NONE
function randomMaxwellian() result(rnd)
USE moduleConstParam, only: pi
implicit none
REAL(8):: rnd
REAL(8):: v1, v2, Rsquare
real(8):: rnd
real(8):: v1, v2, Rsquare
Rsquare = 1.D0
do while (Rsquare >= 1.D0 .and. Rsquare > 0.D0)
v1 = 2.D0 * random() - 1.D0
v2 = 2.D0 * random() - 1.D0
Rsquare = v1**2 + v2**2
v1 = 0.d0
do while (v1 <= 0.d0)
v1 = random()
end do
v2 = random()
rnd = v2 * sqrt(-2.D0 * log(Rsquare) / Rsquare)
rnd = sqrt(-2.d0*log(v1))*cos(2*pi*v2)
END FUNCTION randomMaxwellian
end function randomMaxwellian
!Returns a random number in a Maxwellian distribution of mean 0 and width 1
FUNCTION randomHalfMaxwellian() RESULT(rnd)