-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.f90
318 lines (291 loc) · 9.14 KB
/
random.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
!------------------------------------------------------------------------------!
! NDSPMHD: A Smoothed Particle (Magneto)Hydrodynamics code for (astrophysical) !
! fluid dynamics simulations in 1, 2 and 3 spatial dimensions. !
! !
! (c) 2002-2015 Daniel Price !
! !
! http://users.monash.edu.au/~dprice/ndspmhd !
! daniel.price@monash.edu -or- dprice@cantab.net (forwards to current address) !
! !
! NDSPMHD comes with ABSOLUTELY NO WARRANTY. !
! This is free software; and you are welcome to redistribute !
! it under the terms of the GNU General Public License !
! (see LICENSE file for details) and the provision that !
! this notice remains intact. If you modify this file, please !
! note section 2a) of the GPLv2 states that: !
! !
! a) You must cause the modified files to carry prominent notices !
! stating that you changed the files and the date of any change. !
! !
! ChangeLog: !
!------------------------------------------------------------------------------!!--------------------------------------------------------------------------!
!+
! MODULE: random
!
! DESCRIPTION:
! this module contains a motley collection of random number
! generator routines, used in various particle setups
!
! REFERENCES: None
!
! OWNER: Daniel Price
!
! $Id: ed760ff76b1b46d0fb7cbf1342aa4c1de9d25ba3 $
!
! DEPENDENCIES: None
!+
!--------------------------------------------------------------------------
module random
implicit none
public :: ran1,ran2,rayleigh_deviate
public :: sobseq
public :: ranset,dran
private
contains
!!------------------------------------------------------------------------!!
!!
!! Random number generator using the minimal standard generator of
!! Park & Miller (1988) + shuffling (see Press et al, Numerical Recipes)
!!
!! Period is about 10**8
!!
!! Returns a uniform random deviate between 0.0 and 1.0 (exclusive of
!! endpoints). Call with iseed < 0 to initialise, thereafter do not
!! alter iseed between calls.
!!
!!------------------------------------------------------------------------!!
real function ran1(iseed)
integer, intent(inout) :: iseed
integer, parameter :: ia = 16807, im=2147483647, iq = 127773, ir = 2836
integer, parameter :: ntab = 32, ndiv = 1+(im-1)/ntab
integer :: iv(ntab)
integer :: j,k,iy
real, parameter :: am = 1./im, eps = 1.2e-7, floatmax = 1.-eps
save iv,iy
data iv /ntab*0/, iy /0/
!
!--initialise
!
if (iseed <= 0 .or. iy==0) then
iseed = max(-iseed,1) ! do not allow iseed = 0
do j = ntab+8,1,-1
k = iseed/iq
iseed = ia*(iseed-k*iq) - ir*k
if (iseed < 0) iseed = iseed + im
if (j <= ntab) iv(j) = iseed
enddo
iy = iv(1)
endif
!
!--generate random number
!
k = iseed/iq
iseed = ia*(iseed-k*iq) - ir*k
if (iseed < 0) iseed = iseed + im
j = 1 + iy/ndiv
iy = iv(j)
iv(j) = iseed
ran1 = min(am*iy,floatmax)
return
end function ran1
!!------------------------------------------------------------------------!!
!!
!! Long period random number generator (see Press et al, Numerical Recipes)
!!
!! Period is about 2 x 10**18
!!
!! Returns a uniform random deviate between 0.0 and 1.0 (exclusive of
!! endpoints). Call with iseed < 0 to initialise, thereafter do not
!! alter iseed between calls.
!!
!!------------------------------------------------------------------------!!
real function ran2(iseed)
integer, parameter :: im1=2147483563, im2=2147483399, &
imm1=im1-1, ia1=40014, ia2=40692, iq1=53668, iq2=52774, ir1=12211, &
ir2=3791, ntab=32,ndiv=1+imm1/ntab
real, parameter :: am=1./im1, eps=1.2e-7, rnmx=1.-eps
integer :: iseed
integer :: iseed2,j,k,iv(ntab),iy
save iv,iy,iseed2
data iseed2/123456789/, iv/ntab*0/, iy/0/
!
!--initialise random sequence
!
if (iseed <= 0) then
iseed = max(-iseed,1) ! iseed not zero
iseed2 = iseed
do j=ntab+8,1,-1
k = iseed/iq1
iseed = ia1*(iseed-k*iq1) - k*ir1
if (iseed < 0) iseed = iseed + im1
if (j <= ntab) iv(j) = iseed
enddo
iy = iv(1)
endif
k = iseed/iq1
iseed = ia1*(iseed-k*iq1) - k*ir1
if (iseed < 0) iseed = iseed + im1
k = iseed2/iq2
iseed2 = ia2*(iseed2-k*iq2) - k*iq2
if (iseed2 < 0) iseed2 = iseed2 + im2
j = 1 + iy/ndiv
iy = iv(j) - iseed2
iv(j) = iseed
if (iy < 1) iy = iy + imm1
ran2 = min(am*iy,rnmx)
return
end function ran2
!!-------------------------------------------------------------------------
!!
!! Function returns a random number drawn from a Rayleigh distribution
!! P(r) = r*e^(-r^2/(2*s^2))/s^2
!!
!! Useful for drawing amplitudes from a Gaussian distribution,
!! since the modulus is distributed according to a Rayleigh distribution.
!!
!!-------------------------------------------------------------------------
real function rayleigh_deviate(iseed)
integer :: iseed
rayleigh_deviate = sqrt(-log(ran1(iseed)))
end function rayleigh_deviate
!!-------------------------------------------------------------------------
!!
!! Quasi Random sobol sequence from Numerical Recipes
!!
!!-------------------------------------------------------------------------
subroutine sobseq(n,x)
integer, intent(in) :: n
real :: x(*)
integer, parameter :: maxbit=30, maxdim=6
integer i,im,in,ipp,j,k,l,ip(maxdim),iu(maxdim,maxbit)
integer iv(maxbit*maxdim),ix(maxdim),mdeg(maxdim)
real fac
save ip,mdeg,ix,iv,in,fac
equivalence (iv,iu)
data ip /0,1,1,2,1,4/, mdeg /1,2,3,3,4,4/, ix /6*0/
data iv /6*1,3,1,3,3,1,1,5,7,7,3,3,5,15,11,5,15,13,9,156*0/
if (n < 0) then
do k=1,maxdim
do j=1,mdeg(k)
iu(k,j)=iu(k,j)*2**(maxbit-j)
enddo
do j=mdeg(k)+1,maxbit
ipp=ip(k)
i=iu(k,j-mdeg(k))
i=ieor(i,i/2**mdeg(k))
do l=mdeg(k)-1,1,-1
if(iand(ipp,1) /= 0)i=ieor(i,iu(k,j-l))
ipp=ipp/2
enddo
iu(k,j)=i
enddo
enddo
fac=1./2.**maxbit
in=0
else
im=in
do j=1,maxbit
if(iand(im,1)==0)goto 1
im=im/2
enddo
stop 'maxbit too small in sobseq'
1 im=(j-1)*maxdim
do k=1,min(n,maxdim)
ix(k)=ieor(ix(k),iv(im+k))
x(k)=ix(k)*fac
enddo
in=in+1
endif
return
end subroutine sobseq
!--------------------------------------------------
! Black box random number generator routines follow
!--------------------------------------------------
!--------------------------------
subroutine ranset(ir,iseed)
!
! This routine is used to initialize all of the
! random number generators in RANPAK.
!
! The seeding is performed as determined by ir
! and iseed as follows:
!
! ir <= 0, initialize all seeds
! ir > 0, initialize only seed ir
! iseed < 1, the default seed is used
! iseed >= 1, use the value passed in iseed
!
! After initialization, the value of the jth
! random number seed should be accessed with
! the RANGET routine.
!
! CODE DEPENDENCIES: (internal RANPAK COMMON block)
!
! DATE: DEC. 1, 1994
! AUTHOR: R.D. STEWART
!
integer ir,iseed,iset,i
integer nrg
parameter (nrg=100)
integer isd(nrg)
common /ranpak/ isd
if (iseed > 0) then
iset = iseed
else
! use the "demonic" default seed
iset = 666
endif
if (ir < 1) then
! initialize all random number generator seeds
! to iseed
do i=1,nrg
isd(i) = iset
enddo
elseif (ir <= nrg) then
isd(ir) = iset
endif
ir = 1
return
end subroutine ranset
subroutine dran(ir,rand_no)
!
! PREFERRED DOUBLE PRECISION RANDOM NUMBER GENERATOR
!
! The random number seed is set initially by a call to the
! RANSET routine. ir is a pointer to an internal random
! number seed and NOT a random number seed.
!
! COMMENTS: This routine will work correctly on any computer
! with a maximum integer greater than or equal
! to 2**31 - 1. NOTE: The proper function of
! the routine can verified by checking to see
! that the random number seed after the generation
! of 10000 random numbers is 1,043,618,065
!
! For more information refer to the classic paper
!
! Park, S.K. and Miller, K.W., "Random number generators:
! good ones are hard to find." Communications of the
! ACM, Vol 31, No 10 (Oct. 1988).
!
! CODE DEPENDENCIES: (internal RANPAK COMMON block)
!
! DATE: DEC. 1, 1994
! AUTHOR: R.D. STEWART
!
integer nrg,ir
parameter (nrg=100)
integer isd(nrg)
common /ranpak/ isd
double precision tmp,a,seed,rand_no
double precision m,minv
parameter (a=16807.0d+00,m=2147483647.0d+00)
parameter (minv=1.0d+00/m)
seed = isd(ir)
tmp = a*seed
seed = tmp - m*dint(tmp*minv)
rand_no = seed*minv
isd(ir) = int(seed)
return
end subroutine dran
end module random