source: palm/trunk/SOURCE/random_function.f90 @ 1433

Last change on this file since 1433 was 1343, checked in by kanani, 11 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1 MODULE random_function_mod
2
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-2014 Leibniz Universitaet Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: random_function.f90 1343 2014-03-26 17:07:58Z keck $
27!
28! 1342 2014-03-26 17:04:47Z kanani
29! REAL constants defined as wp-kind
30!
31! 1320 2014-03-20 08:40:49Z raasch
32! ONLY-attribute added to USE-statements,
33! kind-parameters added to all INTEGER and REAL declaration statements,
34! kinds are defined in new module kinds,
35! old module precision_kind is removed,
36! revision history before 2012 removed,
37! comment fields (!:) to be used for variable explanations added to
38! all variable declaration statements
39!
40! 1092 2013-02-02 11:24:22Z raasch
41! unused variables removed
42!
43! 1036 2012-10-22 13:43:42Z raasch
44! code put under GPL (PALM 3.9)
45!
46! RCS Log replace by Id keyword, revision history cleaned up
47!
48! Revision 1.1  1998/02/04 16:09:45  raasch
49! Initial revision
50!
51!
52! Description:
53! ------------
54! Random number generator, produces numbers equally distributed in interval [0,1]
55! This routine is taken from the "numerical recipies"
56!------------------------------------------------------------------------------!
57
58    USE kinds
59
60    IMPLICIT NONE
61
62    PRIVATE
63
64    PUBLIC random_function, random_function_ini
65
66    INTEGER(iwp), PUBLIC, SAVE ::  random_iv(32)  !:
67    INTEGER(iwp), PUBLIC, SAVE ::  random_iy      !:
68
69    INTERFACE random_function_ini
70       MODULE PROCEDURE random_function_ini
71    END INTERFACE random_function_ini
72
73    INTERFACE random_function
74       MODULE PROCEDURE random_function
75    END INTERFACE random_function
76
77 CONTAINS
78
79    SUBROUTINE random_function_ini
80
81       IMPLICIT NONE
82
83       random_iv = 0
84       random_iy = 0
85
86    END SUBROUTINE random_function_ini
87
88    FUNCTION random_function( idum )
89
90
91       IMPLICIT NONE
92
93       INTEGER(iwp) ::  ia               !:
94       INTEGER(iwp) ::  idum             !:
95       INTEGER(iwp) ::  im               !:
96       INTEGER(iwp) ::  iq               !:
97       INTEGER(iwp) ::  ir               !:
98       INTEGER(iwp) ::  ndiv             !:
99       INTEGER(iwp) ::  ntab             !:
100
101       INTEGER(iwp) ::  j                !:
102       INTEGER(iwp) ::  k                !:
103
104       REAL(wp)     ::  am               !:
105       REAL(wp)     ::  eps              !:
106       REAL(wp)     ::  random_function  !:
107       REAL(wp)     ::  rnmx             !:
108
109       PARAMETER ( ia=16807, im=2147483647, am=1.0_wp/im, iq=127773, ir=2836, &
110                   ntab=32, ndiv=1+(im-1)/ntab, eps=1.2e-7_wp, rnmx=1.0_wp-eps )
111
112       IF ( idum .le. 0  .or.  random_iy .eq. 0 )  THEN
113          idum = max (-idum,1)
114          DO  j = ntab+8,1,-1
115             k    = idum / iq
116             idum = ia * ( idum - k * iq ) - ir * k
117             IF ( idum .lt. 0 )  idum = idum + im
118             IF ( j .le. ntab )  random_iv(j) = idum
119          ENDDO
120          random_iy = random_iv(1)
121       ENDIF
122
123       k    = idum / iq
124       idum = ia * ( idum - k * iq ) - ir * k
125       IF ( idum .lt. 0 )  idum = idum + im
126       j            = 1 + random_iy / ndiv
127       random_iy    = random_iv(j)
128       random_iv(j) = idum
129       random_function  = min ( am * random_iy , rnmx )
130
131    END FUNCTION random_function
132
133 END MODULE random_function_mod
Note: See TracBrowser for help on using the repository browser.