Skip to content

Commit

Permalink
make hash(::Xoshiro) compatible with == (#51376)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet authored Sep 22, 2023
1 parent c476d84 commit bf03753
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
2 changes: 2 additions & 0 deletions stdlib/Random/src/Xoshiro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ rng_native_52(::TaskLocalRNG) = UInt64
copy(rng::Union{TaskLocalRNG, Xoshiro}) = Xoshiro(getstate(rng)...)
copy!(dst::Union{TaskLocalRNG, Xoshiro}, src::Union{TaskLocalRNG, Xoshiro}) = setstate!(dst, getstate(src))
==(x::Union{TaskLocalRNG, Xoshiro}, y::Union{TaskLocalRNG, Xoshiro}) = getstate(x) == getstate(y)
# use a magic (random) number to scramble `h` so that `hash(x)` is distinct from `hash(getstate(x))`
hash(x::Union{TaskLocalRNG, Xoshiro}, h::UInt) = hash(getstate(x), h + 0x49a62c2dda6fa9be % UInt)

function seed!(rng::Union{TaskLocalRNG, Xoshiro})
# as we get good randomness from RandomDevice, we can skip hashing
Expand Down
51 changes: 34 additions & 17 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -594,24 +594,41 @@ guardseed() do
Random.seed!(typemax(UInt128))
end

# copy, == and hash
let seed = rand(UInt32, 10)
r = MersenneTwister(seed)
@test r == MersenneTwister(seed) # r.vals should be all zeros
@test hash(r) == hash(MersenneTwister(seed))
s = copy(r)
@test s == r && s !== r
@test hash(s) == hash(r)
skip, len = rand(0:2000, 2)
for j=1:skip
rand(r)
rand(s)
@testset "copy, == and hash" begin
for RNG = (MersenneTwister, Xoshiro)
seed = rand(UInt32, 10)
r = RNG(seed)
t = RNG(seed)
@test r == t
@test hash(r) == hash(t)
s = copy(r)
@test s == r == t && s !== r
@test hash(s) == hash(r)
skip, len = rand(0:2000, 2)
for j=1:skip
rand(r)
@test r != s
@test hash(r) != hash(s)
rand(s)
end
@test rand(r, len) == rand(s, len)
@test s == r
@test hash(s) == hash(r)
h = rand(UInt)
@test hash(s, h) == hash(r, h)
if RNG == Xoshiro
t = copy(TaskLocalRNG())
@test hash(t) == hash(TaskLocalRNG())
@test hash(t, h) == hash(TaskLocalRNG(), h)
x = rand()
@test hash(t) != hash(TaskLocalRNG())
@test rand(t) == x
@test hash(t) == hash(TaskLocalRNG())
copy!(TaskLocalRNG(), r)
@test hash(TaskLocalRNG()) == hash(r)
@test TaskLocalRNG() == r
end
end
@test rand(r, len) == rand(s, len)
@test s == r
@test hash(s) == hash(r)
h = rand(UInt)
@test hash(s, h) == hash(r, h)
end

# MersenneTwister initialization with invalid values
Expand Down

0 comments on commit bf03753

Please sign in to comment.