@@ -11,8 +11,9 @@ using Random
11
11
using Random. DSFMT
12
12
13
13
using 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
15
15
16
+ import SHA
16
17
import Future # randjump
17
18
18
19
function test_uniform (xs:: AbstractArray{T} ) where {T<: AbstractFloat }
@@ -297,7 +298,7 @@ for f in (:<, :<=, :>, :>=, :(==), :(!=))
297
298
end
298
299
299
300
# test all rand APIs
300
- for rng in ([], [MersenneTwister (0 )], [RandomDevice ()], [Xoshiro ()])
301
+ for rng in ([], [MersenneTwister (0 )], [RandomDevice ()], [Xoshiro (0 )], [ SeedHasher ( 0 )])
301
302
realrng = rng == [] ? default_rng () : only (rng)
302
303
ftypes = [Float16, Float32, Float64, FakeFloat64, BigFloat]
303
304
cftypes = [ComplexF16, ComplexF32, ComplexF64, ftypes... ]
@@ -453,7 +454,8 @@ function hist(X, n)
453
454
end
454
455
455
456
@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)],
457
459
T in [Float16, Float32, Float64, BigFloat],
458
460
prec in (T == BigFloat ? [3 , 53 , 64 , 100 , 256 , 1000 ] : [256 ])
459
461
480
482
# but also for 3 linear combinations of positions (for the array version)
481
483
lcs = unique! .([rand (1 : n, 2 ), rand (1 : n, 3 ), rand (1 : n, 5 )])
482
484
aslcs = zeros (Int, 3 )
483
- for rng = (MersenneTwister (), RandomDevice (), Xoshiro ())
485
+ seed = rand (UInt128)
486
+ for rng = (MersenneTwister (seed), RandomDevice (), Xoshiro (seed), SeedHasher (seed))
484
487
for scalar = [false , true ]
485
488
fill! (a, 0 )
486
489
fill! (as, 0 )
662
665
@testset " Random.seed!(rng, ...) returns rng" begin
663
666
# issue #21248
664
667
seed = rand (UInt)
665
- for m = ([MersenneTwister (seed)], [Xoshiro (seed)], [])
668
+ for m = ([MersenneTwister (seed)], [Xoshiro (seed)], [SeedHasher (seed)], [ ])
666
669
m2 = m == [] ? default_rng () : m[1 ]
667
670
@test Random. seed! (m... ) === m2
668
671
@test Random. seed! (m... , rand (UInt)) === m2
708
711
# this shouldn't crash (#22403)
709
712
@test_throws MethodError rand! (Union{UInt,Int}[1 , 2 , 3 ])
710
713
711
- @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 )
712
715
m = RNG ()
713
716
a = rand (m, Int)
714
717
m = RNG ()
729
732
@test rand (m, Int) ∉ (a, b, c, d)
730
733
end
731
734
732
- @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 )
733
736
seeds = Any[0 , 1 , 2 , 10000 , 10001 , rand (UInt32, 8 ), randstring (), randstring (), rand (UInt128, 3 )... ]
734
737
if RNG == Xoshiro
735
738
push! (seeds, rand (UInt64, rand (1 : 4 )))
@@ -769,7 +772,10 @@ struct RandomStruct23964 end
769
772
@test_throws MethodError rand (RandomStruct23964 ())
770
773
end
771
774
772
- @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))),
773
779
T ∈ (Bool, Int8, Int16, Int32, UInt32, Int64, Int128, UInt128)
774
780
if T === Bool
775
781
@test rand (RNG, false : true ) ∈ (false , true )
912
918
@test rand (rng) == rand (GLOBAL_RNG)
913
919
end
914
920
915
- @testset " RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice)
916
- @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
917
926
end
918
927
919
928
@testset " generated scalar integers do not overlap" begin
@@ -1211,7 +1220,14 @@ end
1211
1220
end
1212
1221
end
1213
1222
1223
+
1214
1224
@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
+
1215
1231
# Test that:
1216
1232
# 1) if n == m, then hash_seed(n) == hash_seed(m)
1217
1233
# 2) if n != m, then hash_seed(n) != hash_seed(m)
@@ -1224,12 +1240,12 @@ end
1224
1240
T <: Signed && push! (seeds, T (0 ), T (1 ), T (2 ), T (- 1 ), T (- 2 ))
1225
1241
end
1226
1242
1227
- vseeds = Dict {Vector{UInt8} , BigInt} ()
1243
+ vseeds = Dict {String , BigInt} ()
1228
1244
for seed = seeds
1229
1245
bigseed = big (seed)
1230
- vseed = Random . hash_seed (bigseed)
1246
+ vseed = hash_seed (bigseed)
1231
1247
# test property 1) above
1232
- @test Random . hash_seed (seed) == vseed
1248
+ @test hash_seed (seed) == vseed
1233
1249
# test property 2) above
1234
1250
@test bigseed == get! (vseeds, vseed, bigseed)
1235
1251
# test that the property 1) is actually inherited by `seed!`
@@ -1241,16 +1257,16 @@ end
1241
1257
end
1242
1258
1243
1259
seed32 = rand (UInt32, rand (1 : 9 ))
1244
- hash32 = Random . hash_seed (seed32)
1245
- @test Random . hash_seed (map (UInt64, seed32)) == hash32
1260
+ hash32 = hash_seed (seed32)
1261
+ @test hash_seed (map (UInt64, seed32)) == hash32
1246
1262
@test hash32 ∉ keys (vseeds)
1247
1263
1248
1264
seed_str = randstring ()
1249
1265
seed_gstr = GenericString (seed_str)
1250
- @test Random . hash_seed (seed_str) == Random . hash_seed (seed_gstr)
1251
- string_seeds = Set {Vector{UInt8} } ()
1266
+ @test hash_seed (seed_str) == hash_seed (seed_gstr)
1267
+ string_seeds = Set {String } ()
1252
1268
for ch = ' A' :' z'
1253
- vseed = Random . hash_seed (string (ch))
1269
+ vseed = hash_seed (string (ch))
1254
1270
@test vseed ∉ keys (vseeds)
1255
1271
@test vseed ∉ string_seeds
1256
1272
push! (string_seeds, vseed)
0 commit comments