Skip to content

Commit 750a854

Browse files
rfourquetnalimilan
authored andcommitted
make hash(::Xoshiro) compatible with == (#51376)
1 parent 624404d commit 750a854

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

stdlib/Random/src/Xoshiro.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ function ==(a::Xoshiro, b::Xoshiro)
8181
a.s0 == b.s0 && a.s1 == b.s1 && a.s2 == b.s2 && a.s3 == b.s3 && a.s4 == b.s4
8282
end
8383

84+
hash(x::Xoshiro, h::UInt) = hash((x.s0, x.s1, x.s2, x.s3, x.s4), h + 0x49a62c2dda6fa9be % UInt)
85+
8486
rng_native_52(::Xoshiro) = UInt64
8587

8688
@inline function rand(rng::Xoshiro, ::SamplerType{UInt64})
@@ -221,6 +223,12 @@ end
221223

222224
==(a::TaskLocalRNG, b::Xoshiro) = b == a
223225

226+
function hash(x::TaskLocalRNG, h::UInt)
227+
t = current_task()
228+
hash((t.rngState0, t.rngState1, t.rngState2, t.rngState3, t.rngState4), h + 0x49a62c2dda6fa9be % UInt)
229+
end
230+
231+
224232
# for partial words, use upper bits from Xoshiro
225233

226234
rand(r::Union{TaskLocalRNG, Xoshiro}, ::SamplerTrivial{UInt52Raw{UInt64}}) = rand(r, UInt64) >>> 12

stdlib/Random/test/runtests.jl

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -594,24 +594,41 @@ guardseed() do
594594
Random.seed!(typemax(UInt128))
595595
end
596596

597-
# copy, == and hash
598-
let seed = rand(UInt32, 10)
599-
r = MersenneTwister(seed)
600-
@test r == MersenneTwister(seed) # r.vals should be all zeros
601-
@test hash(r) == hash(MersenneTwister(seed))
602-
s = copy(r)
603-
@test s == r && s !== r
604-
@test hash(s) == hash(r)
605-
skip, len = rand(0:2000, 2)
606-
for j=1:skip
607-
rand(r)
608-
rand(s)
597+
@testset "copy, == and hash" begin
598+
for RNG = (MersenneTwister, Xoshiro)
599+
seed = rand(UInt32, 10)
600+
r = RNG(seed)
601+
t = RNG(seed)
602+
@test r == t
603+
@test hash(r) == hash(t)
604+
s = copy(r)
605+
@test s == r == t && s !== r
606+
@test hash(s) == hash(r)
607+
skip, len = rand(0:2000, 2)
608+
for j=1:skip
609+
rand(r)
610+
@test r != s
611+
@test hash(r) != hash(s)
612+
rand(s)
613+
end
614+
@test rand(r, len) == rand(s, len)
615+
@test s == r
616+
@test hash(s) == hash(r)
617+
h = rand(UInt)
618+
@test hash(s, h) == hash(r, h)
619+
if RNG == Xoshiro
620+
t = copy(TaskLocalRNG())
621+
@test hash(t) == hash(TaskLocalRNG())
622+
@test hash(t, h) == hash(TaskLocalRNG(), h)
623+
x = rand()
624+
@test hash(t) != hash(TaskLocalRNG())
625+
@test rand(t) == x
626+
@test hash(t) == hash(TaskLocalRNG())
627+
copy!(TaskLocalRNG(), r)
628+
@test hash(TaskLocalRNG()) == hash(r)
629+
@test TaskLocalRNG() == r
630+
end
609631
end
610-
@test rand(r, len) == rand(s, len)
611-
@test s == r
612-
@test hash(s) == hash(r)
613-
h = rand(UInt)
614-
@test hash(s, h) == hash(r, h)
615632
end
616633

617634
# MersenneTwister initialization with invalid values

0 commit comments

Comments
 (0)