[1] | 1 | FUNCTION random_gauss( idum, upper_limit ) |
---|
| 2 | |
---|
[1036] | 3 | !--------------------------------------------------------------------------------! |
---|
| 4 | ! This file is part of PALM. |
---|
| 5 | ! |
---|
| 6 | ! PALM is free software: you can redistribute it and/or modify it under the terms |
---|
| 7 | ! of the GNU General Public License as published by the Free Software Foundation, |
---|
| 8 | ! either version 3 of the License, or (at your option) any later version. |
---|
| 9 | ! |
---|
| 10 | ! PALM is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
| 11 | ! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
---|
| 12 | ! A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
| 13 | ! |
---|
| 14 | ! You should have received a copy of the GNU General Public License along with |
---|
| 15 | ! PALM. If not, see <http://www.gnu.org/licenses/>. |
---|
| 16 | ! |
---|
| 17 | ! Copyright 1997-2012 Leibniz University Hannover |
---|
| 18 | !--------------------------------------------------------------------------------! |
---|
| 19 | ! |
---|
[484] | 20 | ! Current revisions: |
---|
[1] | 21 | ! ----------------- |
---|
| 22 | ! |
---|
| 23 | ! |
---|
| 24 | ! Former revisions: |
---|
| 25 | ! ----------------- |
---|
[3] | 26 | ! $Id: random_gauss.f90 1037 2012-10-22 14:10:22Z hoffmann $ |
---|
[1037] | 27 | ! |
---|
| 28 | ! 1036 2012-10-22 13:43:42Z raasch |
---|
| 29 | ! code put under GPL (PALM 3.9) |
---|
| 30 | ! |
---|
[3] | 31 | ! RCS Log replace by Id keyword, revision history cleaned up |
---|
| 32 | ! |
---|
[1] | 33 | ! Revision 1.4 2006/08/04 15:01:48 raasch |
---|
| 34 | ! Range of random number is limited by an upper limit (new second parameter) |
---|
| 35 | ! |
---|
| 36 | ! Revision 1.1 1998/03/25 20:09:47 raasch |
---|
| 37 | ! Initial revision |
---|
| 38 | ! |
---|
| 39 | ! |
---|
| 40 | ! Description: |
---|
| 41 | ! ------------ |
---|
| 42 | ! Generates a gaussian distributed random number (mean value 1, sigma = 1) |
---|
| 43 | ! This routine is taken from the "numerical recipies". |
---|
| 44 | !------------------------------------------------------------------------------! |
---|
| 45 | |
---|
| 46 | USE random_function_mod |
---|
| 47 | |
---|
| 48 | IMPLICIT NONE |
---|
| 49 | |
---|
| 50 | INTEGER :: idum, iset |
---|
| 51 | REAL :: fac, gset, random_gauss, rsq, upper_limit, v1, v2 |
---|
| 52 | |
---|
| 53 | SAVE iset, gset |
---|
| 54 | |
---|
| 55 | DATA iset /0/ |
---|
| 56 | |
---|
| 57 | ! |
---|
| 58 | !-- Random numbers are created as long as they do not fall below the given |
---|
| 59 | !-- upper limit |
---|
| 60 | DO |
---|
| 61 | |
---|
| 62 | IF ( iset == 0 ) THEN |
---|
| 63 | rsq = 0.0 |
---|
| 64 | DO WHILE ( rsq >= 1.0 .OR. rsq == 0.0 ) |
---|
| 65 | v1 = 2.0 * random_function( idum ) - 1.0 |
---|
| 66 | v2 = 2.0 * random_function( idum ) - 1.0 |
---|
| 67 | rsq = v1**2 + v2**2 |
---|
| 68 | ENDDO |
---|
| 69 | fac = SQRT( -2.0 * LOG( rsq ) / rsq ) |
---|
| 70 | gset = v1 * fac |
---|
| 71 | random_gauss = v2 * fac + 1.0 |
---|
| 72 | iset = 1 |
---|
| 73 | ELSE |
---|
| 74 | random_gauss = gset + 1.0 |
---|
| 75 | iset = 0 |
---|
| 76 | ENDIF |
---|
| 77 | |
---|
| 78 | IF ( ABS( random_gauss - 1.0 ) <= upper_limit ) EXIT |
---|
| 79 | |
---|
| 80 | ENDDO |
---|
| 81 | |
---|
| 82 | END FUNCTION random_gauss |
---|