FUNCTION random_gamma(s, first) RESULT(fn_val)
! Adapted from Fortran 77 code from the book:
! Dagpunar, J. 'Principles of random variate generation'
! Clarendon Press, Oxford, 1988. ISBN 0-19-852202-9
! FUNCTION GENERATES A RANDOM GAMMA VARIATE.
! CALLS EITHER random_gamma1 (S > 1.0)
! OR random_exponential (S = 1.0)
! OR random_gamma2 (S < 1.0).
! S = SHAPE PARAMETER OF DISTRIBUTION (0 < REAL).
REAL, INTENT(IN) :: s
LOGICAL, INTENT(IN) :: first
REAL :: fn_val
IF (s <= zero) THEN
WRITE(*, *) 'SHAPE PARAMETER VALUE MUST BE POSITIVE'
STOP
END IF
IF (s > one) THEN
fn_val = random_gamma1(s, first)
ELSE IF (s < one) THEN
fn_val = random_gamma2(s, first)
ELSE
fn_val = random_exponential()
END IF
RETURN
END FUNCTION random_gamma