@@ -48,28 +48,35 @@ mutable struct Xoshiro <: AbstractRNG
48
48
s1:: UInt64
49
49
s2:: UInt64
50
50
s3:: UInt64
51
+ s4:: UInt64 # internal splitmix state
51
52
52
- Xoshiro (s0:: Integer , s1:: Integer , s2:: Integer , s3:: Integer ) = new (s0, s1, s2, s3)
53
+ Xoshiro (s0:: Integer , s1:: Integer , s2:: Integer , s3:: Integer , s4:: Integer ) = new (s0, s1, s2, s3, s4)
54
+ Xoshiro (s0:: Integer , s1:: Integer , s2:: Integer , s3:: Integer ) = new (s0, s1, s2, s3, UInt64 (0 ))
53
55
Xoshiro (seed= nothing ) = seed! (new (), seed)
54
56
end
55
57
56
- function setstate! (x:: Xoshiro , s0:: UInt64 , s1:: UInt64 , s2:: UInt64 , s3:: UInt64 )
58
+ function setstate! (
59
+ x:: Xoshiro ,
60
+ s0:: UInt64 , s1:: UInt64 , s2:: UInt64 , s3:: UInt64 , # xoshiro256 state
61
+ s4:: UInt64 , # internal splitmix state
62
+ )
57
63
x. s0 = s0
58
64
x. s1 = s1
59
65
x. s2 = s2
60
66
x. s3 = s3
67
+ x. s4 = s4
61
68
x
62
69
end
63
70
64
- copy (rng:: Xoshiro ) = Xoshiro (rng. s0, rng. s1, rng. s2, rng. s3)
71
+ copy (rng:: Xoshiro ) = Xoshiro (rng. s0, rng. s1, rng. s2, rng. s3, rng . s4 )
65
72
66
73
function copy! (dst:: Xoshiro , src:: Xoshiro )
67
- dst. s0, dst. s1, dst. s2, dst. s3 = src. s0, src. s1, src. s2, src. s3
74
+ dst. s0, dst. s1, dst. s2, dst. s3, dst . s4 = src. s0, src. s1, src. s2, src. s3, src . s4
68
75
dst
69
76
end
70
77
71
78
function == (a:: Xoshiro , b:: Xoshiro )
72
- a. s0 == b. s0 && a. s1 == b. s1 && a. s2 == b. s2 && a. s3 == b. s3
79
+ a. s0 == b. s0 && a. s1 == b. s1 && a. s2 == b. s2 && a. s3 == b. s3 && a . s4 == b . s4
73
80
end
74
81
75
82
rng_native_52 (:: Xoshiro ) = UInt64
@@ -116,7 +123,7 @@ rng_native_52(::TaskLocalRNG) = UInt64
116
123
function setstate! (
117
124
x:: TaskLocalRNG ,
118
125
s0:: UInt64 , s1:: UInt64 , s2:: UInt64 , s3:: UInt64 , # xoshiro256 state
119
- s4:: UInt64 = 1 s0 + 3 s1 + 5 s2 + 7 s3 , # internal splitmix state
126
+ s4:: UInt64 , # internal splitmix state
120
127
)
121
128
t = current_task ()
122
129
t. rngState0 = s0
@@ -148,14 +155,20 @@ end
148
155
function seed! (rng:: Union{TaskLocalRNG,Xoshiro} )
149
156
# as we get good randomness from RandomDevice, we can skip hashing
150
157
rd = RandomDevice ()
151
- setstate! (rng, rand (rd, UInt64), rand (rd, UInt64), rand (rd, UInt64), rand (rd, UInt64))
158
+ s0 = rand (rd, UInt64)
159
+ s1 = rand (rd, UInt64)
160
+ s2 = rand (rd, UInt64)
161
+ s3 = rand (rd, UInt64)
162
+ s4 = 1 s0 + 3 s1 + 5 s2 + 7 s3
163
+ setstate! (rng, s0, s1, s2, s3, s4)
152
164
end
153
165
154
166
function seed! (rng:: Union{TaskLocalRNG,Xoshiro} , seed:: Union{Vector{UInt32}, Vector{UInt64}} )
155
167
c = SHA. SHA2_256_CTX ()
156
168
SHA. update! (c, reinterpret (UInt8, seed))
157
169
s0, s1, s2, s3 = reinterpret (UInt64, SHA. digest! (c))
158
- setstate! (rng, s0, s1, s2, s3)
170
+ s4 = 1 s0 + 3 s1 + 5 s2 + 7 s3
171
+ setstate! (rng, s0, s1, s2, s3, s4)
159
172
end
160
173
161
174
seed! (rng:: Union{TaskLocalRNG, Xoshiro} , seed:: Integer ) = seed! (rng, make_seed (seed))
@@ -178,24 +191,30 @@ end
178
191
179
192
function copy (rng:: TaskLocalRNG )
180
193
t = current_task ()
181
- Xoshiro (t. rngState0, t. rngState1, t. rngState2, t. rngState3)
194
+ Xoshiro (t. rngState0, t. rngState1, t. rngState2, t. rngState3, t . rngState4 )
182
195
end
183
196
184
197
function copy! (dst:: TaskLocalRNG , src:: Xoshiro )
185
198
t = current_task ()
186
- setstate! (dst, src. s0, src. s1, src. s2, src. s3)
199
+ setstate! (dst, src. s0, src. s1, src. s2, src. s3, src . s4 )
187
200
return dst
188
201
end
189
202
190
203
function copy! (dst:: Xoshiro , src:: TaskLocalRNG )
191
204
t = current_task ()
192
- setstate! (dst, t. rngState0, t. rngState1, t. rngState2, t. rngState3)
205
+ setstate! (dst, t. rngState0, t. rngState1, t. rngState2, t. rngState3, t . rngState4 )
193
206
return dst
194
207
end
195
208
196
209
function == (a:: Xoshiro , b:: TaskLocalRNG )
197
210
t = current_task ()
198
- a. s0 == t. rngState0 && a. s1 == t. rngState1 && a. s2 == t. rngState2 && a. s3 == t. rngState3
211
+ (
212
+ a. s0 == t. rngState0 &&
213
+ a. s1 == t. rngState1 &&
214
+ a. s2 == t. rngState2 &&
215
+ a. s3 == t. rngState3 &&
216
+ a. s4 == t. rngState4
217
+ )
199
218
end
200
219
201
220
== (a:: TaskLocalRNG , b:: Xoshiro ) = b == a
0 commit comments