@@ -11,8 +11,9 @@ using Random
1111using Random. DSFMT
1212
1313using Random: default_rng, Sampler, SamplerRangeFast, SamplerRangeInt, SamplerRangeNDL, MT_CACHE_F, MT_CACHE_I
14- using Random: jump_128, jump_192, jump_128!, jump_192!
14+ using Random: jump_128, jump_192, jump_128!, jump_192!, SeedHasher
1515
16+ import SHA
1617import Future # randjump
1718
1819function test_uniform (xs:: AbstractArray{T} ) where {T<: AbstractFloat }
@@ -297,7 +298,7 @@ for f in (:<, :<=, :>, :>=, :(==), :(!=))
297298end
298299
299300# test all rand APIs
300- for rng in ([], [MersenneTwister (0 )], [RandomDevice ()], [Xoshiro ()])
301+ for rng in ([], [MersenneTwister (0 )], [RandomDevice ()], [Xoshiro (0 )], [ SeedHasher ( 0 )])
301302 realrng = rng == [] ? default_rng () : only (rng)
302303 ftypes = [Float16, Float32, Float64, FakeFloat64, BigFloat]
303304 cftypes = [ComplexF16, ComplexF32, ComplexF64, ftypes... ]
@@ -453,7 +454,8 @@ function hist(X, n)
453454end
454455
455456@testset " uniform distribution of floats" begin
456- for rng in [MersenneTwister (), RandomDevice (), Xoshiro ()],
457+ seed = rand (UInt128)
458+ for rng in [MersenneTwister (seed), RandomDevice (), Xoshiro (seed), SeedHasher (seed)],
457459 T in [Float16, Float32, Float64, BigFloat],
458460 prec in (T == BigFloat ? [3 , 53 , 64 , 100 , 256 , 1000 ] : [256 ])
459461
480482 # but also for 3 linear combinations of positions (for the array version)
481483 lcs = unique! .([rand (1 : n, 2 ), rand (1 : n, 3 ), rand (1 : n, 5 )])
482484 aslcs = zeros (Int, 3 )
483- for rng = (MersenneTwister (), RandomDevice (), Xoshiro ())
485+ seed = rand (UInt128)
486+ for rng = (MersenneTwister (seed), RandomDevice (), Xoshiro (seed), SeedHasher (seed))
484487 for scalar = [false , true ]
485488 fill! (a, 0 )
486489 fill! (as, 0 )
662665@testset " Random.seed!(rng, ...) returns rng" begin
663666 # issue #21248
664667 seed = rand (UInt)
665- for m = ([MersenneTwister (seed)], [Xoshiro (seed)], [])
668+ for m = ([MersenneTwister (seed)], [Xoshiro (seed)], [SeedHasher (seed)], [ ])
666669 m2 = m == [] ? default_rng () : m[1 ]
667670 @test Random. seed! (m... ) === m2
668671 @test Random. seed! (m... , rand (UInt)) === m2
675678 @test Random. seed! (m... , typemax (UInt)) === m2
676679 @test Random. seed! (m... , typemax (UInt128)) === m2
677680 @test Random. seed! (m... , " a random seed" ) === m2
681+ @test Random. seed! (m... , Random. default_rng ()) === m2
678682 end
679683end
680684
707711# this shouldn't crash (#22403)
708712@test_throws MethodError rand! (Union{UInt,Int}[1 , 2 , 3 ])
709713
710- @testset " $RNG () & Random.seed!(rng::$RNG ) initializes randomly" for RNG in (MersenneTwister, RandomDevice, Xoshiro)
714+ @testset " $RNG () & Random.seed!(rng::$RNG ) initializes randomly" for RNG in (MersenneTwister, RandomDevice, Xoshiro, SeedHasher )
711715 m = RNG ()
712716 a = rand (m, Int)
713717 m = RNG ()
728732 @test rand (m, Int) ∉ (a, b, c, d)
729733end
730734
731- @testset " $RNG (seed) & Random.seed!(m::$RNG , seed) produce the same stream" for RNG= (MersenneTwister,Xoshiro)
735+ @testset " $RNG (seed) & Random.seed!(m::$RNG , seed) produce the same stream" for RNG= (MersenneTwister, Xoshiro, SeedHasher )
732736 seeds = Any[0 , 1 , 2 , 10000 , 10001 , rand (UInt32, 8 ), randstring (), randstring (), rand (UInt128, 3 )... ]
733737 if RNG == Xoshiro
734738 push! (seeds, rand (UInt64, rand (1 : 4 )))
739743 Random. seed! (m, seed)
740744 @test a == [rand (m) for _= 1 : 100 ]
741745 end
746+ # rng as a seed
747+ m = RNG (Xoshiro (0 ))
748+ a = [rand (m) for _= 1 : 100 ]
749+ Random. seed! (m, Xoshiro (0 ))
750+ @test a == [rand (m) for _= 1 : 100 ]
742751end
743752
744753@testset " Random.seed!(seed) sets Random.GLOBAL_SEED" begin
@@ -763,7 +772,10 @@ struct RandomStruct23964 end
763772 @test_throws MethodError rand (RandomStruct23964 ())
764773end
765774
766- @testset " rand(::$(typeof (RNG)) , ::UnitRange{$T }" for RNG ∈ (MersenneTwister (rand (UInt128)), RandomDevice (), Xoshiro ()),
775+ @testset " rand(::$(typeof (RNG)) , ::UnitRange{$T }" for RNG ∈ (MersenneTwister (rand (UInt128)),
776+ RandomDevice (),
777+ Xoshiro (rand (UInt128)),
778+ SeedHasher (rand (UInt128))),
767779 T ∈ (Bool, Int8, Int16, Int32, UInt32, Int64, Int128, UInt128)
768780 if T === Bool
769781 @test rand (RNG, false : true ) ∈ (false , true )
906918 @test rand (rng) == rand (GLOBAL_RNG)
907919end
908920
909- @testset " RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice)
910- @test length .(rand .(T (), 1 : 3 )) == 1 : 3
921+ @testset " RNGs broadcast as scalars: $(typeof (RNG)) " for RNG in (MersenneTwister (0 ),
922+ RandomDevice (),
923+ Xoshiro (0 ),
924+ SeedHasher (0 ))
925+ @test length .(rand .(RNG, 1 : 3 )) == 1 : 3
911926end
912927
913928@testset " generated scalar integers do not overlap" begin
@@ -1205,7 +1220,14 @@ end
12051220 end
12061221end
12071222
1223+
12081224@testset " seed! and hash_seed" begin
1225+ function hash_seed (seed)
1226+ ctx = SHA. SHA2_256_CTX ()
1227+ Random. hash_seed (seed, ctx)
1228+ bytes2hex (SHA. digest! (ctx))
1229+ end
1230+
12091231 # Test that:
12101232 # 1) if n == m, then hash_seed(n) == hash_seed(m)
12111233 # 2) if n != m, then hash_seed(n) != hash_seed(m)
@@ -1218,12 +1240,12 @@ end
12181240 T <: Signed && push! (seeds, T (0 ), T (1 ), T (2 ), T (- 1 ), T (- 2 ))
12191241 end
12201242
1221- vseeds = Dict {Vector{UInt8} , BigInt} ()
1243+ vseeds = Dict {String , BigInt} ()
12221244 for seed = seeds
12231245 bigseed = big (seed)
1224- vseed = Random . hash_seed (bigseed)
1246+ vseed = hash_seed (bigseed)
12251247 # test property 1) above
1226- @test Random . hash_seed (seed) == vseed
1248+ @test hash_seed (seed) == vseed
12271249 # test property 2) above
12281250 @test bigseed == get! (vseeds, vseed, bigseed)
12291251 # test that the property 1) is actually inherited by `seed!`
@@ -1235,16 +1257,16 @@ end
12351257 end
12361258
12371259 seed32 = rand (UInt32, rand (1 : 9 ))
1238- hash32 = Random . hash_seed (seed32)
1239- @test Random . hash_seed (map (UInt64, seed32)) == hash32
1260+ hash32 = hash_seed (seed32)
1261+ @test hash_seed (map (UInt64, seed32)) == hash32
12401262 @test hash32 ∉ keys (vseeds)
12411263
12421264 seed_str = randstring ()
12431265 seed_gstr = GenericString (seed_str)
1244- @test Random . hash_seed (seed_str) == Random . hash_seed (seed_gstr)
1245- string_seeds = Set {Vector{UInt8} } ()
1266+ @test hash_seed (seed_str) == hash_seed (seed_gstr)
1267+ string_seeds = Set {String } ()
12461268 for ch = ' A' :' z'
1247- vseed = Random . hash_seed (string (ch))
1269+ vseed = hash_seed (string (ch))
12481270 @test vseed ∉ keys (vseeds)
12491271 @test vseed ∉ string_seeds
12501272 push! (string_seeds, vseed)
0 commit comments