Skip to content

Commit 7e5c4a8

Browse files
authored
Fix streams of randn and randexp on Julia >= 1.11 (#22)
1 parent 9af20f9 commit 7e5c4a8

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

Project.toml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
name = "StableRNGs"
2-
uuid = "860ef19b-820b-49d6-a774-d7a799459cd3"
1+
name = "StableRNGs"
2+
uuid = "860ef19b-820b-49d6-a774-d7a799459cd3"
33
authors = ["Rafael Fourquet <[email protected]>"]
4-
version = "1.0.1"
4+
version = "1.0.2"
55

66
[deps]
77
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
8-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
98

109
[compat]
10+
Random = "<0.0.1, 1"
11+
Test = "<0.0.1, 1"
1112
julia = "1"
13+
14+
[extras]
15+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
16+
17+
[targets]
18+
test = ["Test"]

src/StableRNGs.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,29 @@ function Random.shuffle!(r::StableRNG, a::AbstractArray)
155155
return a
156156
end
157157

158+
# https://github.com/JuliaRandom/StableRNGs.jl/issues/20
159+
@noinline function Random.randn_unlikely(rng::StableRNG, idx, rabs, x)
160+
@inbounds if idx == 0
161+
while true
162+
xx = -Random.ziggurat_nor_inv_r*log(rand(rng))
163+
yy = -log(rand(rng))
164+
yy+yy > xx*xx &&
165+
return (rabs >> 8) % Bool ? -Random.ziggurat_nor_r-xx : Random.ziggurat_nor_r+xx
166+
end
167+
elseif (Random.fi[idx] - Random.fi[idx+1])*rand(rng) + Random.fi[idx+1] < exp(-0.5*x*x)
168+
return x # return from the triangular area
169+
else
170+
return randn(rng)
171+
end
172+
end
173+
@noinline function Random.randexp_unlikely(rng::StableRNG, idx, x)
174+
@inbounds if idx == 0
175+
return Random.ziggurat_exp_r - log(rand(rng))
176+
elseif (Random.fe[idx] - Random.fe[idx+1])*rand(rng) + Random.fe[idx+1] < exp(-x)
177+
return x # return from the triangular area
178+
else
179+
return Random.randexp(rng)
180+
end
181+
end
182+
158183
end # module

test/runtests.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,30 @@ end
140140
shuffle!(StableRNG(10), b)
141141
@test b == a_shuffled
142142
end
143+
144+
# https://github.com/JuliaRandom/StableRNGs.jl/issues/20
145+
@testset "`randn` stability" begin
146+
ref = [
147+
0.5745734638645761,
148+
0.9050768627399978,
149+
0.7998353512850861,
150+
3.8845391427592286,
151+
-0.9209167676765456,
152+
-0.8486914352114853,
153+
-1.187370886634173
154+
]
155+
@test randn(StableRNG(1_337), 10_000)[4214:4220] == ref
156+
end
157+
158+
@testset "`randexp` stability" begin
159+
ref = [
160+
1.7805339657229489,
161+
4.29332694381576,
162+
0.20777989530218552,
163+
8.196071589366719,
164+
7.551925528256079,
165+
1.3540162045313204,
166+
0.5239664874260928
167+
]
168+
@test randexp(StableRNG(1_337), 10_000)[1545:1551] == ref
169+
end

0 commit comments

Comments
 (0)