-
Notifications
You must be signed in to change notification settings - Fork 59
/
randlc_prb.f90
337 lines (280 loc) · 7.46 KB
/
randlc_prb.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
program main
!*****************************************************************************80
!
!! RANDLC_PRB calls sample problems for the RANDLC library.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 11 March 2010
!
! Author:
!
! John Burkardt
!
implicit none
call timestamp ( )
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'RANDLC_PRB'
write ( *, '(a)' ) ' FORTRAN90 version:'
write ( *, '(a)' ) ' Test the RANDLC library.'
call test01 ( )
call test02 ( )
call test03 ( )
call test04 ( )
!
! Terminate.
!
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'RANDLC_PRB'
write ( *, '(a)' ) ' Normal end of execution.'
write ( *, '(a)' ) ' '
call timestamp ( )
return
end
subroutine test01 ( )
!*****************************************************************************80
!
!! TEST01 tests RANDLC.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 08 March 2010
!
! Author:
!
! John Burkardt
!
implicit none
integer ( kind = 4 ) i
real ( kind = 8 ) randlc
real ( kind = 8 ) seed
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'TEST01'
write ( *, '(a)' ) ' RANDLC computes pseudorandom values '
write ( *, '(a)' ) ' in the interval [0,1].'
seed = 123456789.0D+00
write ( *, '(a)' ) ' '
write ( *, '(a,f15.0)' ) ' The initial seed is ', seed
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' I RANDLC'
write ( *, '(a)' ) ' '
do i = 1, 10
write ( *, '(2x,i8,2x,f14.6)' ) i, randlc ( seed )
end do
return
end
subroutine test02 ( )
!*****************************************************************************80
!
!! TEST02 tests RANDLC.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 08 March 2010
!
! Author:
!
! John Burkardt
!
implicit none
integer ( kind = 4 ), parameter :: n = 1000
integer ( kind = 4 ) i
real ( kind = 8 ) randlc
real ( kind = 8 ) seed
real ( kind = 8 ) seed_in
real ( kind = 8 ) seed_out
real ( kind = 8 ) u(n)
real ( kind = 8 ) u_avg
real ( kind = 8 ) u_var
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'TEST02'
write ( *, '(a)' ) ' RANDLC computes a sequence of uniformly'
write ( *, '(a)' ) ' distributed pseudorandom numbers.'
seed = 123456789.0D+00
write ( *, '(a)' ) ' '
write ( *, '(a,f15.0)' ) ' Initial SEED = ', seed
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' First 10 values:'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' I Input Output RANDLC'
write ( *, '(a)' ) ' SEED SEED'
write ( *, '(a)' ) ' '
do i = 1, 10
seed_in = seed
u(i) = randlc ( seed )
seed_out = seed
write ( *, '(2x,i6,2x,f15.0,2x,f15.0,2x,f10.6)' ) &
i, seed_in, seed_out, u(i)
end do
write ( *, '(a)' ) ' '
write ( *, '(a,i6,a)' ) ' Now call RANDLC ', n, ' times.'
u_avg = 0.0D+00
do i = 1, n
u(i) = randlc ( seed )
u_avg = u_avg + u(i)
end do
u_avg = u_avg / real ( n, kind = 8 )
u_var = 0.0D+00
do i = 1, n
u_var = u_var + ( u(i) - u_avg )**2
end do
u_var = u_var / real ( n - 1, kind = 8 )
write ( *, '(a)' ) ' '
write ( *, '(a,g14.6)' ) ' Average value = ', u_avg
write ( *, '(a,g14.6)' ) ' Expecting ', 0.5D+00
write ( *, '(a)' ) ' '
write ( *, '(a,g14.6)' ) ' Variance = ', u_var
write ( *, '(a,g14.6)' ) ' Expecting ', 1.0D+00 / 12.0D+00
return
end
subroutine test03 ( )
!*****************************************************************************80
!
!! TEST03 tests RANDLC.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 08 March 2010
!
! Author:
!
! John Burkardt
!
implicit none
integer ( kind = 4 ) i
real ( kind = 8 ) randlc
real ( kind = 8 ) seed
real ( kind = 8 ) seed_in
real ( kind = 8 ) seed_out
real ( kind = 8 ) seed_save
real ( kind = 8 ) x
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'TEST03'
write ( *, '(a)' ) ' RANDLC computes a sequence of pseudorandom numbers'
write ( *, '(a)' ) ' but all computations depend on the seed value.'
write ( *, '(a)' ) ' In this test, we show how a sequence of "random"'
write ( *, '(a)' ) ' values can be manipulated by accessing the seed.'
seed = 1066.0D+00
write ( *, '(a)' ) ' '
write ( *, '(a,f15.0)' ) ' Set SEED to ', seed
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' Now call RANDLC 10 times, and watch SEED.'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' I Input Output RANDLC'
write ( *, '(a)' ) ' SEED SEED'
write ( *, '(a)' ) ' '
do i = 1, 10
seed_in = seed
if ( i == 5 ) then
seed_save = seed
end if
x = randlc ( seed )
seed_out = seed
write ( *, '(2x,i6,2x,f15.0,2x,f15.0,2x,f10.6)' ) i, seed_in, seed_out, x
end do
seed = seed_save
write ( *, '(a)' ) ' '
write ( *, '(a,f15.0)' ) ' Reset SEED to its value at step 5, = ', seed
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' Now call RANDLC 10 times, and watch how SEED'
write ( *, '(a)' ) ' and RANDLC restart themselves.'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' I Input Output RANDLC'
write ( *, '(a)' ) ' SEED SEED'
write ( *, '(a)' ) ' '
do i = 1, 10
seed_in = seed
x = randlc ( seed )
seed_out = seed
write ( *, '(2x,i6,2x,f15.0,2x,f15.0,2x,f10.6)' ) i, seed_in, seed_out, x
end do
seed = 0.0D+00
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' What happens with an initial zero SEED?'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' I Input Output RANDLC'
write ( *, '(a)' ) ' SEED SEED'
write ( *, '(a)' ) ' '
do i = 1, 10
seed_in = seed
x = randlc ( seed )
seed_out = seed
write ( *, '(2x,i6,2x,f15.0,2x,f15.0,2x,f10.6)' ) i, seed_in, seed_out, x
end do
seed = -123456789.0D+00
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' What happens with an initial negative SEED?'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' I Input Output RANDLC'
write ( *, '(a)' ) ' SEED SEED'
write ( *, '(a)' ) ' '
do i = 1, 10
seed_in = seed
x = randlc ( seed )
seed_out = seed
write ( *, '(2x,i6,2x,f15.0,2x,f15.0,2x,f10.6)' ) i, seed_in, seed_out, x
end do
return
end
subroutine test04 ( )
!*****************************************************************************80
!
!! RANDLC_TEST04 tests RANDLC_JUMP.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 11 March 2010
!
! Author:
!
! John Burkardt
!
implicit none
integer ( kind = 4 ) i
integer ( kind = 4 ) k
integer ( kind = 4 ) klog
real ( kind = 8 ) randlc
real ( kind = 8 ) randlc_jump
real ( kind = 8 ) seed
real ( kind = 8 ) x1
real ( kind = 8 ) x2
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'RANDLC_TEST04'
write ( *, '(a)' ) ' RANDLC_JUMP jumps directly to the K-th value'
write ( *, '(a)' ) ' returned by RANDLC.'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' K X(hard way) X(jump)'
write ( *, '(a)' ) ' '
k = 1
do klog = 1, 10
seed = 123456789.0D+00
do i = 1, k
x1 = randlc ( seed )
end do
seed = 123456789.0D+00
x2 = randlc_jump ( seed, k )
write ( *, '(2x,i8,2x,f10.6,2x,f10.6)' ) k, x1, x2
k = k * 2
end do
return
end