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

Last change on this file since 1321 was 1321, checked in by raasch, 8 years ago

last commit documented

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