@@ -182,11 +182,6 @@ julia> randsubseq(rng, 1:8, 0.3)
182
182
randsubseq (A:: AbstractArray , p:: Real ) = randsubseq (default_rng (), A, p)
183
183
184
184
185
- # # rand Less Than Masked 52 bits (helper function)
186
-
187
- " Return a sampler generating a random `Int` (masked with `mask`) in ``[0, n)``, when `n <= 2^52`."
188
- ltm52 (n:: Int , mask:: Int = nextpow (2 , n)- 1 ) = LessThan (n- 1 , Masked (mask, UInt52Raw (Int)))
189
-
190
185
# # shuffle & shuffle!
191
186
192
187
"""
@@ -197,39 +192,22 @@ optionally supplying the random-number generator `rng`.
197
192
198
193
# Examples
199
194
```jldoctest
200
- julia> rng = MersenneTwister(1234);
201
-
202
- julia> shuffle!(rng, Vector(1:16))
203
- 16-element Vector{Int64}:
204
- 16
205
- 1
206
- 14
207
- 12
208
- 5
209
- 10
210
- 4
211
- 15
212
- 13
213
- 3
214
- 7
215
- 9
216
- 6
217
- 11
218
- 8
219
- 2
195
+ julia> shuffle!(Xoshiro(0), Vector(1:6))
196
+ 6-element Vector{Int64}:
197
+ 5
198
+ 1
199
+ 2
200
+ 6
201
+ 3
202
+ 4
220
203
```
221
204
"""
222
- function shuffle! (r :: AbstractRNG , a:: AbstractArray )
205
+ function shuffle! (rng :: AbstractRNG , a:: AbstractArray )
223
206
# keep it consistent with `randperm!` and `randcycle!` if possible
224
207
require_one_based_indexing (a)
225
- n = length (a)
226
- @assert n <= Int64 (2 )^ 52
227
- n == 0 && return a
228
- mask = 3
229
- @inbounds for i = 2 : n
230
- j = 1 + rand (r, ltm52 (i, mask))
208
+ @inbounds for i = 2 : length (a)
209
+ j = rand (rng, 1 : i)
231
210
a[i], a[j] = a[j], a[i]
232
- i == 1 + mask && (mask = 2 * mask + 1 )
233
211
end
234
212
return a
235
213
end
@@ -246,20 +224,14 @@ indices, see [`randperm`](@ref).
246
224
247
225
# Examples
248
226
```jldoctest
249
- julia> rng = MersenneTwister(1234);
250
-
251
- julia> shuffle(rng, Vector(1:10))
252
- 10-element Vector{Int64}:
253
- 2
254
- 1
255
- 7
256
- 9
257
- 5
258
- 10
259
- 4
260
- 8
261
- 6
262
- 3
227
+ julia> shuffle(Xoshiro(0), 1:6)
228
+ 6-element Vector{Int64}:
229
+ 5
230
+ 1
231
+ 2
232
+ 6
233
+ 3
234
+ 4
263
235
```
264
236
"""
265
237
shuffle (r:: AbstractRNG , a:: AbstractArray ) = shuffle! (r, copymutable (a))
@@ -286,12 +258,14 @@ To randomly permute an arbitrary vector, see [`shuffle`](@ref) or
286
258
287
259
# Examples
288
260
```jldoctest
289
- julia> randperm(MersenneTwister(1234 ), 4 )
290
- 4 -element Vector{Int64}:
291
- 2
261
+ julia> randperm(Xoshiro(0 ), 6 )
262
+ 6 -element Vector{Int64}:
263
+ 5
292
264
1
293
- 4
265
+ 2
266
+ 6
294
267
3
268
+ 4
295
269
```
296
270
"""
297
271
randperm (r:: AbstractRNG , n:: T ) where {T <: Integer } = randperm! (r, Vector {T} (undef, n))
@@ -307,28 +281,27 @@ optional `rng` argument specifies a random number generator (see
307
281
308
282
# Examples
309
283
```jldoctest
310
- julia> randperm!(MersenneTwister(1234 ), Vector{Int}(undef, 4 ))
311
- 4 -element Vector{Int64}:
312
- 2
284
+ julia> randperm!(Xoshiro(0 ), Vector{Int}(undef, 6 ))
285
+ 6 -element Vector{Int64}:
286
+ 5
313
287
1
314
- 4
288
+ 2
289
+ 6
315
290
3
291
+ 4
316
292
```
317
293
"""
318
- function randperm! (r :: AbstractRNG , a:: Array{<:Integer} )
294
+ function randperm! (rng :: AbstractRNG , a:: Array{<:Integer} )
319
295
# keep it consistent with `shuffle!` and `randcycle!` if possible
320
296
n = length (a)
321
- @assert n <= Int64 (2 )^ 52
322
297
n == 0 && return a
323
298
a[1 ] = 1
324
- mask = 3
325
299
@inbounds for i = 2 : n
326
- j = 1 + rand (r, ltm52 (i, mask) )
300
+ j = rand (rng, 1 : i )
327
301
if i != j # a[i] is undef (and could be #undef)
328
302
a[i] = a[j]
329
303
end
330
304
a[j] = i
331
- i == 1 + mask && (mask = 2 * mask + 1 )
332
305
end
333
306
return a
334
307
end
@@ -351,13 +324,13 @@ The element type of the result is the same as the type of `n`.
351
324
352
325
# Examples
353
326
```jldoctest
354
- julia> randcycle(MersenneTwister(1234 ), 6)
327
+ julia> randcycle(Xoshiro(0 ), 6)
355
328
6-element Vector{Int64}:
356
- 3
357
329
5
330
+ 1
358
331
4
359
332
6
360
- 1
333
+ 3
361
334
2
362
335
```
363
336
"""
@@ -373,28 +346,25 @@ The optional `rng` argument specifies a random number generator, see
373
346
374
347
# Examples
375
348
```jldoctest
376
- julia> randcycle!(MersenneTwister(1234 ), Vector{Int}(undef, 6))
349
+ julia> randcycle!(Xoshiro(0 ), Vector{Int}(undef, 6))
377
350
6-element Vector{Int64}:
378
- 3
379
351
5
352
+ 1
380
353
4
381
354
6
382
- 1
355
+ 3
383
356
2
384
357
```
385
358
"""
386
- function randcycle! (r :: AbstractRNG , a:: Array{<:Integer} )
359
+ function randcycle! (rng :: AbstractRNG , a:: Array{<:Integer} )
387
360
# keep it consistent with `shuffle!` and `randperm!` if possible
388
361
n = length (a)
389
- @assert n <= Int64 (2 )^ 52
390
362
n == 0 && return a
391
363
a[1 ] = 1
392
- mask = 3
393
364
@inbounds for i = 2 : n
394
- j = 1 + rand (r, ltm52 ( i- 1 , mask) )
365
+ j = rand (rng, 1 : i- 1 )
395
366
a[i] = a[j]
396
367
a[j] = i
397
- i == 1 + mask && (mask = 2 * mask + 1 )
398
368
end
399
369
return a
400
370
end
0 commit comments