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