1 |
jmc |
1.6 |
C $Header: /u/gcmpack/MITgcm/model/src/port_rand.F,v 1.5 2004/09/07 15:16:58 edhill Exp $ |
2 |
cnh |
1.3 |
C $Name: $ |
3 |
adcroft |
1.2 |
|
4 |
|
|
C Portable random number generator |
5 |
|
|
|
6 |
|
|
#undef _USE_INTEGERS |
7 |
|
|
|
8 |
edhill |
1.5 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
9 |
cnh |
1.3 |
CBOP |
10 |
|
|
C !ROUTINE: port_rand |
11 |
|
|
C !INTERFACE: |
12 |
molod |
1.4 |
real*8 function port_rand(seed) |
13 |
cnh |
1.3 |
|
14 |
edhill |
1.5 |
C !DESCRIPTION: |
15 |
cnh |
1.3 |
C Portable random number generator |
16 |
jmc |
1.6 |
C seed >=0 :: initialise using this seed ; and return 0 |
17 |
|
|
C seed < 0 :: if first call then initialise using the default seed (=mseed) |
18 |
|
|
C and always return a random number |
19 |
edhill |
1.5 |
|
20 |
|
|
C !USES: |
21 |
|
|
implicit none |
22 |
jmc |
1.6 |
C !INPUT PARAMETERS: |
23 |
|
|
#ifdef _USE_INTEGERS |
24 |
|
|
integer seed |
25 |
|
|
#else |
26 |
|
|
real*8 seed |
27 |
|
|
#endif |
28 |
edhill |
1.5 |
CEOP |
29 |
cnh |
1.3 |
|
30 |
|
|
C !LOCAL VARIABLES: |
31 |
jmc |
1.6 |
integer nff,idum |
32 |
adcroft |
1.2 |
parameter(nff=55) |
33 |
jmc |
1.6 |
parameter(idum=-2) |
34 |
adcroft |
1.2 |
real*8 fac |
35 |
|
|
#ifdef _USE_INTEGERS |
36 |
jmc |
1.6 |
integer mbig,mseed,mZ |
37 |
|
|
parameter (mbig=1000000000,mz=0,fac=1.d0/mbig) |
38 |
|
|
integer mj,mk,ma(nff) |
39 |
molod |
1.4 |
data mseed/161803398/ |
40 |
adcroft |
1.2 |
#else |
41 |
jmc |
1.6 |
real*8 mbig,mseed,mz |
42 |
|
|
parameter (mbig=4000000.,mz=0.,fac=1.d0/mbig) |
43 |
|
|
real*8 mj,mk,ma(nff) |
44 |
molod |
1.4 |
data mseed/1618033./ |
45 |
adcroft |
1.2 |
#endif |
46 |
|
|
logical firstCall |
47 |
jmc |
1.6 |
integer i,ii,inext,inextp,k |
48 |
|
|
data firstCall /.true./ |
49 |
adcroft |
1.2 |
save firstCall,inext,inextp,ma |
50 |
edhill |
1.5 |
|
51 |
jmc |
1.6 |
C- Initialise the random number generator |
52 |
|
|
if(firstCall .OR. seed.GE.mz)then |
53 |
|
|
if (seed.GE.mz) mseed = seed |
54 |
adcroft |
1.2 |
firstCall=.false. |
55 |
|
|
mj=mseed-iabs(idum) |
56 |
|
|
mj=mod(mj,mbig) |
57 |
|
|
ma(nff)=mj |
58 |
|
|
mk=1 |
59 |
|
|
do i=1,nff-1 |
60 |
|
|
ii=mod(21*i,nff) |
61 |
|
|
ma(ii)=mk |
62 |
|
|
mk=mj-mk |
63 |
|
|
if(mk.lt.mz)mk=mk+mbig |
64 |
|
|
mj=ma(ii) |
65 |
|
|
enddo |
66 |
|
|
do k=1,4 |
67 |
|
|
do i=1,nff |
68 |
|
|
ma(i)=ma(i)-ma(1+mod(i+30,nff)) |
69 |
jmc |
1.6 |
if(ma(i).lt.mz)ma(i)=ma(i)+mbig |
70 |
adcroft |
1.2 |
enddo |
71 |
|
|
enddo |
72 |
|
|
inext=0 |
73 |
|
|
inextp=31 |
74 |
|
|
endif |
75 |
jmc |
1.6 |
|
76 |
|
|
C- Compute a random number (only if seed < 0) |
77 |
|
|
if(seed.GE.mz)then |
78 |
|
|
port_rand=0.d0 |
79 |
|
|
else |
80 |
|
|
inext=mod(inext,nff)+1 |
81 |
|
|
inextp=mod(inextp,nff)+1 |
82 |
|
|
mj=ma(inext)-ma(inextp) |
83 |
|
|
if(mj.lt.mz)mj=mj+mbig |
84 |
|
|
ma(inext)=mj |
85 |
|
|
port_rand=mj*fac |
86 |
|
|
endif |
87 |
|
|
|
88 |
adcroft |
1.2 |
return |
89 |
|
|
end |
90 |
|
|
|
91 |
edhill |
1.5 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
92 |
|
|
|
93 |
adcroft |
1.2 |
subroutine port_ranarr(n,arr) |
94 |
|
|
implicit none |
95 |
|
|
integer n,i |
96 |
|
|
real arr(n) |
97 |
jmc |
1.6 |
real*8 port_rand |
98 |
|
|
#ifdef _USE_INTEGERS |
99 |
|
|
integer seed |
100 |
|
|
seed=-1 |
101 |
|
|
#else |
102 |
|
|
real*8 seed |
103 |
|
|
seed=-1.d0 |
104 |
|
|
#endif |
105 |
|
|
c seed=1618033.0d0 |
106 |
adcroft |
1.2 |
do i=1,n |
107 |
molod |
1.4 |
arr(i)=port_rand(seed) |
108 |
adcroft |
1.2 |
enddo |
109 |
|
|
|
110 |
|
|
return |
111 |
|
|
end |
112 |
edhill |
1.5 |
|
113 |
|
|
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
114 |
|
|
CBOP |
115 |
|
|
C !ROUTINE: port_rand_norm |
116 |
|
|
C !INTERFACE: |
117 |
|
|
real*8 function port_rand_norm() |
118 |
|
|
|
119 |
|
|
C !DESCRIPTION: |
120 |
|
|
C This function generates a normally distributed random number with |
121 |
|
|
C the so called polar algorithm. The algorithm actually generates 2 |
122 |
|
|
C numbers, but only 1 is returned for maximum compatibility with old |
123 |
|
|
C code. The most obvious way to improve this function would be to |
124 |
|
|
C make sure that the second number is not wasted. |
125 |
|
|
|
126 |
|
|
C Changed: 2004.09.06 antti.westerlund@fimr.fi |
127 |
|
|
|
128 |
|
|
C !USES: |
129 |
|
|
implicit none |
130 |
|
|
CEOP |
131 |
|
|
|
132 |
|
|
C !LOCAL VARIABLES: |
133 |
jmc |
1.6 |
real*8 port_rand |
134 |
edhill |
1.5 |
real*8 x1, x2, xs, t |
135 |
|
|
integer i |
136 |
|
|
|
137 |
jmc |
1.6 |
#ifdef _USE_INTEGERS |
138 |
|
|
integer seed |
139 |
|
|
seed=-1 |
140 |
|
|
#else |
141 |
|
|
real*8 seed |
142 |
|
|
seed=-1.d0 |
143 |
|
|
#endif |
144 |
|
|
c seed=1618033.0d0 |
145 |
|
|
|
146 |
edhill |
1.5 |
C first generate 2 equally distributed random numbers (-1,1) |
147 |
|
|
DO WHILE (1 .eq. 1) |
148 |
|
|
x1=2.0*port_rand(seed)-1.0 |
149 |
|
|
x2=2.0*port_rand(seed)-1.0 |
150 |
|
|
xs=x1**2+x2**2 |
151 |
|
|
if(xs .lt. 1.0 .and. xs .ne. 0.0) then |
152 |
|
|
goto 100 |
153 |
|
|
end if |
154 |
|
|
END DO |
155 |
|
|
100 continue |
156 |
|
|
|
157 |
|
|
t = sqrt(-2.0*log(xs)/xs) |
158 |
|
|
port_rand_norm = t*x1 |
159 |
|
|
C |
160 |
|
|
C also t*x2 would be a gaussian random number and could be returned |
161 |
|
|
|
162 |
|
|
return |
163 |
|
|
end |