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

Last change on this file since 1036 was 1036, checked in by raasch, 9 years ago

code has been put under the GNU General Public License (v3)

  • Property svn:keywords set to Id
File size: 2.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-2012  Leibniz University Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: random_function.f90 1036 2012-10-22 13:43:42Z raasch $
27! RCS Log replace by Id keyword, revision history cleaned up
28!
29! Revision 1.3  2003/10/29 09:06:57  raasch
30! Former function changed to a module.
31!
32! Revision 1.1  1998/02/04 16:09:45  raasch
33! Initial revision
34!
35!
36! Description:
37! ------------
38! Random number generator, produces numbers equally distributed in interval [0,1]
39! This routine is taken from the "numerical recipies"
40!------------------------------------------------------------------------------!
41
42    IMPLICIT NONE
43
44    PRIVATE
45
46    PUBLIC random_function, random_function_ini
47
48    INTEGER, PUBLIC, SAVE ::  random_iv(32), random_iy
49
50    INTERFACE random_function_ini
51       MODULE PROCEDURE random_function_ini
52    END INTERFACE random_function_ini
53
54    INTERFACE random_function
55       MODULE PROCEDURE random_function
56    END INTERFACE random_function
57
58 CONTAINS
59
60    SUBROUTINE random_function_ini
61
62       IMPLICIT NONE
63
64       random_iv = 0
65       random_iy = 0
66
67    END SUBROUTINE random_function_ini
68
69    FUNCTION random_function( idum )
70
71
72       IMPLICIT NONE
73
74       INTEGER ::  ia, idum, im, iq, ir, ndiv, ntab
75       REAL    ::  am, eps, random_function, ranf, rnmx
76
77       PARAMETER ( ia=16807, im=2147483647, am=1.0/im, iq=127773, ir=2836, &
78                   ntab=32, ndiv=1+(im-1)/ntab, eps=1.2e-7, rnmx=1.0-eps )
79
80       INTEGER ::  j, k
81
82
83       IF ( idum .le. 0  .or.  random_iy .eq. 0 )  THEN
84          idum = max (-idum,1)
85          DO  j = ntab+8,1,-1
86             k    = idum / iq
87             idum = ia * ( idum - k * iq ) - ir * k
88             IF ( idum .lt. 0 )  idum = idum + im
89             IF ( j .le. ntab )  random_iv(j) = idum
90          ENDDO
91          random_iy = random_iv(1)
92       ENDIF
93
94       k    = idum / iq
95       idum = ia * ( idum - k * iq ) - ir * k
96       IF ( idum .lt. 0 )  idum = idum + im
97       j            = 1 + random_iy / ndiv
98       random_iy    = random_iv(j)
99       random_iv(j) = idum
100       random_function  = min ( am * random_iy , rnmx )
101
102    END FUNCTION random_function
103
104 END MODULE random_function_mod
Note: See TracBrowser for help on using the repository browser.