-
Notifications
You must be signed in to change notification settings - Fork 2
/
Random.jl
68 lines (60 loc) · 1.25 KB
/
Random.jl
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
abstract type RNG end
mutable struct LFG <: RNG # Lagged Fibonacci generator
p
q
m
s
op
end
LFG() = LFG(24, 55, 2^32, [rand(UInt) % 2^32 for i = 1:55], +)
LFG(p, q, m) = LPG(p, q, m, [rand(UInt) % m for i = 1:q], +)
function Base.rand(rng::LFG)
new = rng.op(rng.s[rng.p], rng.s[rng.q]) % rng.m
popfirst!(rng.s)
push!(rng.s, new)
new
end
function Base.rand(rng::LFG, n::Int)
rnd = []
for j = 1:n
new = rng.op(rng.s[rng.p], rng.s[rng.q]) % rng.m
popfirst!(rng.s)
push!(rng.s, new)
push!(rnd, new)
end
rnd
end
mutable struct RANMAR <: RNG # RANMAR
r::LFG
t
s
end
function ranseq(t) # helper function for RANMAR
if t - 7654321 >= 0
t = t - 7654321
else
t = t - 7654321 + 2^24 - 3
end
return t
end
function RANMAR()
r = LFG(97, 33, 2^24, [rand(UInt) % 2^32 for i = 1:97], -)
t = [rand(UInt) % 2^24]
for i = 2:97
push!(t, ranseq(t[i - 1]))
end
s = []
for i = 1:97
push!(s, (r.s[i] - t[i]) % 2^24)
end
t = t[97]
RANMAR(r, t, s)
end
function Base.rand(rng::RANMAR)
rnew = rand(rng.r)
tnew = ranseq(rng.t)
rng.t = tnew
snew = (rnew - tnew) % 2^24
push!(rng.s, snew)
snew
end