-
Notifications
You must be signed in to change notification settings - Fork 3
/
aunif.f
50 lines (41 loc) · 1.98 KB
/
aunif.f
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
real function aunif (x1) result (unif)
!! ~ ~ ~ PURPOSE ~ ~ ~
!! This function generates random numbers ranging from 0.0 to 1.0.
!! In the process of calculating the random number, the seed (x1) is
!! set to a new value.
!! This function implements the prime-modulus generator
!! xi = 16807 xi Mod(2**(31) - 1)
!! using code which ensures that no intermediate result uses more than
!! 31 bits
!! the theory behind the code is summarized in
!! Bratley, P., B.L. Fox and L.E. Schrage. 1983. A Guide to Simulation.
!! Springer-Verlag, New York. (pages 199-202)
!! ~ ~ ~ INCOMING VARIABLES ~ ~ ~
!! name |units |definition
!! ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!! x1 |none |random number generator seed (integer)
!! |where 0 < x1 < 2147483647
!! ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!! ~ ~ ~ OUTGOING VARIABLES ~ ~ ~
!! name |units |definition
!! ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!! unif |none |random number between 0.0 and 1.0
!! x1 |none |random number generator seed (integer)
!! |set to new value where 0 < x1 < 2147483647
!! ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!! ~ ~ ~ LOCAL DEFINIT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