Skip to content

Commit d662500

Browse files
committed
make hash(::Xoshiro) compatible with ==
1 parent c476d84 commit d662500

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

stdlib/Random/src/Xoshiro.jl

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ rng_native_52(::TaskLocalRNG) = UInt64
122122
copy(rng::Union{TaskLocalRNG, Xoshiro}) = Xoshiro(getstate(rng)...)
123123
copy!(dst::Union{TaskLocalRNG, Xoshiro}, src::Union{TaskLocalRNG, Xoshiro}) = setstate!(dst, getstate(src))
124124
==(x::Union{TaskLocalRNG, Xoshiro}, y::Union{TaskLocalRNG, Xoshiro}) = getstate(x) == getstate(y)
125+
# use a magic (random) number to scramble `h` so that `hash(x)` is distinct from `hash(getstate(x))`
126+
hash(x::Union{TaskLocalRNG, Xoshiro}, h::UInt) = hash(getstate(x), h + 0x49a62c2dda6fa9be % UInt)
125127

126128
function seed!(rng::Union{TaskLocalRNG, Xoshiro})
127129
# as we get good randomness from RandomDevice, we can skip hashing

stdlib/Random/test/runtests.jl

+34-17
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)