Fixes to random variables

After reading some works and reviewing what I had, I've done some
corrections to how the randomb velicities in Maxwellian distributions
are calculated. These should be correct now.
This commit is contained in:
Jorge Gonzalez 2025-08-02 13:25:48 +02:00
commit 4b040c35c3
2 changed files with 12 additions and 11 deletions

View file

@ -47,22 +47,23 @@ MODULE moduleRandom
END FUNCTION randomIntAB
!Returns a random number in a Maxwellian distribution of mean 0 and width 1
!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
REAL(8):: rnd
REAL(8):: x, y
REAL(8):: v1, v2, Rsquare
rnd = 0.D0
x = 0.D0
DO WHILE (x == 0.D0)
CALL RANDOM_NUMBER(x)
END DO
CALL RANDOM_NUMBER(y)
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
rnd = DSQRT(-2.D0*DLOG(x))*DCOS(2.D0*PI*y)
end do
rnd = v2 * sqrt(-2.D0 * log(Rsquare) / Rsquare)
END FUNCTION randomMaxwellian