FUNCTION random_gauss( idum, upper_limit ) !--------------------------------------------------------------------------------! ! This file is part of PALM. ! ! PALM is free software: you can redistribute it and/or modify it under the terms ! of the GNU General Public License as published by the Free Software Foundation, ! either version 3 of the License, or (at your option) any later version. ! ! PALM is distributed in the hope that it will be useful, but WITHOUT ANY ! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ! A PARTICULAR PURPOSE. See the GNU General Public License for more details. ! ! You should have received a copy of the GNU General Public License along with ! PALM. If not, see . ! ! Copyright 1997-2012 Leibniz University Hannover !--------------------------------------------------------------------------------! ! ! Current revisions: ! ----------------- ! ! ! Former revisions: ! ----------------- ! $Id: random_gauss.f90 1037 2012-10-22 14:10:22Z franke $ ! ! 1036 2012-10-22 13:43:42Z raasch ! code put under GPL (PALM 3.9) ! ! RCS Log replace by Id keyword, revision history cleaned up ! ! Revision 1.4 2006/08/04 15:01:48 raasch ! Range of random number is limited by an upper limit (new second parameter) ! ! Revision 1.1 1998/03/25 20:09:47 raasch ! Initial revision ! ! ! Description: ! ------------ ! Generates a gaussian distributed random number (mean value 1, sigma = 1) ! This routine is taken from the "numerical recipies". !------------------------------------------------------------------------------! USE random_function_mod IMPLICIT NONE INTEGER :: idum, iset REAL :: fac, gset, random_gauss, rsq, upper_limit, v1, v2 SAVE iset, gset DATA iset /0/ ! !-- Random numbers are created as long as they do not fall below the given !-- upper limit DO IF ( iset == 0 ) THEN rsq = 0.0 DO WHILE ( rsq >= 1.0 .OR. rsq == 0.0 ) v1 = 2.0 * random_function( idum ) - 1.0 v2 = 2.0 * random_function( idum ) - 1.0 rsq = v1**2 + v2**2 ENDDO fac = SQRT( -2.0 * LOG( rsq ) / rsq ) gset = v1 * fac random_gauss = v2 * fac + 1.0 iset = 1 ELSE random_gauss = gset + 1.0 iset = 0 ENDIF IF ( ABS( random_gauss - 1.0 ) <= upper_limit ) EXIT ENDDO END FUNCTION random_gauss