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

Last change on this file since 1320 was 1320, checked in by raasch, 10 years ago

ONLY-attribute added to USE-statements,
kind-parameters added to all INTEGER and REAL declaration statements,
kinds are defined in new module kinds,
old module precision_kind is removed,
revision history before 2012 removed,
comment fields (!:) to be used for variable explanations added to all variable declaration statements

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