@@ -117,65 +117,21 @@ end
117117
118118#  Shared implementation between Xoshiro and TaskLocalRNG -- seeding
119119
120- function  seed! (x :: Union{TaskLocalRNG,Xoshiro} )
120+ function  seed! (rng :: Union{TaskLocalRNG,Xoshiro} )
121121    #  as we get good randomness from RandomDevice, we can skip hashing
122-     parent =  RandomDevice ()
123-     #  Constants have nothing up their sleeve, see task.c
124-     #  0x02011ce34bce797f == hash(UInt(1))|0x01
125-     #  0x5a94851fb48a6e05 == hash(UInt(2))|0x01
126-     #  0x3688cf5d48899fa7 == hash(UInt(3))|0x01
127-     #  0x867b4bb4c42e5661 == hash(UInt(4))|0x01
128-     setstate! (x,
129-               0x02011ce34bce797f  *  rand (parent, UInt64),
130-               0x5a94851fb48a6e05  *  rand (parent, UInt64),
131-               0x3688cf5d48899fa7  *  rand (parent, UInt64),
132-               0x867b4bb4c42e5661  *  rand (parent, UInt64))
122+     rd =  RandomDevice ()
123+     setstate! (rng, rand (rd, UInt64), rand (rd, UInt64), rand (rd, UInt64), rand (rd, UInt64))
133124end 
134125
135- function  seed! (rng:: Union{TaskLocalRNG,Xoshiro} , seed:: NTuple{4,UInt64} )
136-     #  TODO : Consider a less ad-hoc construction
137-     #  We can afford burning a handful of cycles here, and we don't want any
138-     #  surprises with respect to bad seeds / bad interactions.
139- 
140-     s0 =  s  =  Base. hash_64_64 (seed[1 ])
141-     s1 =  s +=  Base. hash_64_64 (seed[2 ])
142-     s2 =  s +=  Base. hash_64_64 (seed[3 ])
143-     s3 =  s +=  Base. hash_64_64 (seed[4 ])
144- 
126+ function  seed! (rng:: Union{TaskLocalRNG,Xoshiro} , seed:: Union{Vector{UInt32}, Vector{UInt64}} )
127+     c =  SHA. SHA2_256_CTX ()
128+     SHA. update! (c, reinterpret (UInt8, seed))
129+     s0, s1, s2, s3 =  reinterpret (UInt64, SHA. digest! (c))
145130    setstate! (rng, s0, s1, s2, s3)
146- 
147-     rand (rng, UInt64)
148-     rand (rng, UInt64)
149-     rand (rng, UInt64)
150-     rand (rng, UInt64)
151-     rng
152131end 
153132
154- function  seed! (rng:: Union{TaskLocalRNG, Xoshiro} , seed:: UInt128 )
155-     seed0 =  seed %  UInt64
156-     seed1 =  (seed>>> 64 ) %  UInt64
157-     seed! (rng, (seed0, seed1, zero (UInt64), zero (UInt64)))
158- end 
159- seed! (rng:: Union{TaskLocalRNG, Xoshiro} , seed:: Integer ) =  seed! (rng, UInt128 (seed))
160- 
161- function  seed! (rng:: Union{TaskLocalRNG, Xoshiro} , seed:: AbstractVector{UInt64} )
162-     if  length (seed) >  4 
163-         throw (ArgumentError (" seed should have no more than 256 bits" 
164-     end 
165-     seed0 =  length (seed)> 0  ?  seed[1 ] :  UInt64 (0 )
166-     seed1 =  length (seed)> 1  ?  seed[2 ] :  UInt64 (0 )
167-     seed2 =  length (seed)> 2  ?  seed[3 ] :  UInt64 (0 )
168-     seed3 =  length (seed)> 3  ?  seed[4 ] :  UInt64 (0 )
169-     seed! (rng, (seed0, seed1, seed2, seed3))
170- end 
133+ seed! (rng:: Union{TaskLocalRNG, Xoshiro} , seed:: Integer ) =  seed! (rng, make_seed (seed))
171134
172- function  seed! (rng:: Union{TaskLocalRNG, Xoshiro} , seed:: AbstractVector{UInt32} )
173-     if  iseven (length (seed))
174-         seed! (rng, reinterpret (UInt64, seed))
175-     else 
176-         seed! (rng, UInt64[reinterpret (UInt64, @view (seed[begin : end - 1 ])); seed[end ] %  UInt64])
177-     end 
178- end 
179135
180136@inline  function  rand (rng:: Union{TaskLocalRNG, Xoshiro} , :: SamplerType{UInt128} )
181137    first =  rand (rng, UInt64)
0 commit comments