@@ -176,11 +176,6 @@ julia> randsubseq(Xoshiro(123), 1:8, 0.3)
176
176
randsubseq (A:: AbstractArray , p:: Real ) = randsubseq (default_rng (), A, p)
177
177
178
178
179
- # # rand Less Than Masked 52 bits (helper function)
180
-
181
- " Return a sampler generating a random `Int` (masked with `mask`) in ``[0, n)``, when `n <= 2^52`."
182
- ltm52 (n:: Int , mask:: Int = nextpow (2 , n)- 1 ) = LessThan (n- 1 , Masked (mask, UInt52Raw (Int)))
183
-
184
179
# # shuffle & shuffle!
185
180
186
181
"""
@@ -191,31 +186,22 @@ optionally supplying the random-number generator `rng`.
191
186
192
187
# Examples
193
188
```jldoctest
194
- julia> shuffle!(Xoshiro(123), Vector(1:10))
195
- 10-element Vector{Int64}:
196
- 5
197
- 4
198
- 2
199
- 3
200
- 6
201
- 10
202
- 8
203
- 1
204
- 9
205
- 7
189
+ julia> shuffle!(Xoshiro(0), Vector(1:6))
190
+ 6-element Vector{Int64}:
191
+ 5
192
+ 1
193
+ 2
194
+ 6
195
+ 3
196
+ 4
206
197
```
207
198
"""
208
- function shuffle! (r :: AbstractRNG , a:: AbstractArray )
199
+ function shuffle! (rng :: AbstractRNG , a:: AbstractArray )
209
200
# keep it consistent with `randperm!` and `randcycle!` if possible
210
201
require_one_based_indexing (a)
211
- n = length (a)
212
- @assert n <= Int64 (2 )^ 52
213
- n == 0 && return a
214
- mask = 3
215
- @inbounds for i = 2 : n
216
- j = 1 + rand (r, ltm52 (i, mask))
202
+ @inbounds for i = 2 : length (a)
203
+ j = rand (rng, 1 : i)
217
204
a[i], a[j] = a[j], a[i]
218
- i == 1 + mask && (mask = 2 * mask + 1 )
219
205
end
220
206
return a
221
207
end
@@ -247,18 +233,14 @@ indices, see [`randperm`](@ref).
247
233
248
234
# Examples
249
235
```jldoctest
250
- julia> shuffle(Xoshiro(123), Vector(1:10))
251
- 10-element Vector{Int64}:
252
- 5
253
- 4
254
- 2
255
- 3
256
- 6
257
- 10
258
- 8
259
- 1
260
- 9
261
- 7
236
+ julia> shuffle(Xoshiro(0), 1:6)
237
+ 6-element Vector{Int64}:
238
+ 5
239
+ 1
240
+ 2
241
+ 6
242
+ 3
243
+ 4
262
244
```
263
245
"""
264
246
shuffle (r:: AbstractRNG , a:: AbstractArray ) = shuffle! (r, copymutable (a))
@@ -285,12 +267,14 @@ To randomly permute an arbitrary vector, see [`shuffle`](@ref) or
285
267
286
268
# Examples
287
269
```jldoctest
288
- julia> randperm(Xoshiro(123), 4)
289
- 4-element Vector{Int64}:
270
+ julia> randperm(Xoshiro(0), 6)
271
+ 6-element Vector{Int64}:
272
+ 5
290
273
1
291
- 4
292
274
2
275
+ 6
293
276
3
277
+ 4
294
278
```
295
279
"""
296
280
randperm (r:: AbstractRNG , n:: T ) where {T <: Integer } = randperm! (r, Vector {T} (undef, n))
@@ -306,28 +290,27 @@ optional `rng` argument specifies a random number generator (see
306
290
307
291
# Examples
308
292
```jldoctest
309
- julia> randperm!(Xoshiro(123), Vector{Int}(undef, 4))
310
- 4-element Vector{Int64}:
293
+ julia> randperm!(Xoshiro(0), Vector{Int}(undef, 6))
294
+ 6-element Vector{Int64}:
295
+ 5
311
296
1
312
- 4
313
297
2
298
+ 6
314
299
3
300
+ 4
315
301
```
316
302
"""
317
- function randperm! (r :: AbstractRNG , a:: Array{<:Integer} )
303
+ function randperm! (rng :: AbstractRNG , a:: Array{<:Integer} )
318
304
# keep it consistent with `shuffle!` and `randcycle!` if possible
319
305
n = length (a)
320
- @assert n <= Int64 (2 )^ 52
321
306
n == 0 && return a
322
307
a[1 ] = 1
323
- mask = 3
324
308
@inbounds for i = 2 : n
325
- j = 1 + rand (r, ltm52 (i, mask) )
309
+ j = rand (rng, 1 : i )
326
310
if i != j # a[i] is undef (and could be #undef)
327
311
a[i] = a[j]
328
312
end
329
313
a[j] = i
330
- i == 1 + mask && (mask = 2 * mask + 1 )
331
314
end
332
315
return a
333
316
end
@@ -356,10 +339,13 @@ which are sampled uniformly. If `n == 0`, `randcycle` returns an empty vector.
356
339
357
340
# Examples
358
341
```jldoctest
359
- julia> randcycle(Xoshiro(123 ), 6)
342
+ julia> randcycle(Xoshiro(0 ), 6)
360
343
6-element Vector{Int64}:
361
344
5
345
+ 1
362
346
4
347
+ 6
348
+ 3
363
349
2
364
350
6
365
351
3
@@ -384,29 +370,29 @@ which are sampled uniformly. If `A` is empty, `randcycle!` leaves it unchanged.
384
370
385
371
# Examples
386
372
```jldoctest
387
- julia> randcycle!(Xoshiro(123 ), Vector{Int}(undef, 6))
373
+ julia> randcycle!(Xoshiro(0 ), Vector{Int}(undef, 6))
388
374
6-element Vector{Int64}:
389
375
5
376
+ 1
390
377
4
378
+ 6
379
+ 3
391
380
2
392
381
6
393
382
3
394
383
1
395
384
```
396
385
"""
397
- function randcycle! (r :: AbstractRNG , a:: Array{<:Integer} )
386
+ function randcycle! (rng :: AbstractRNG , a:: Array{<:Integer} )
398
387
# keep it consistent with `shuffle!` and `randperm!` if possible
399
388
n = length (a)
400
- @assert n <= Int64 (2 )^ 52
401
389
n == 0 && return a
402
390
a[1 ] = 1
403
- mask = 3
404
391
# Sattolo's algorithm:
405
392
@inbounds for i = 2 : n
406
- j = 1 + rand (r, ltm52 ( i- 1 , mask) )
393
+ j = rand (rng, 1 : i- 1 )
407
394
a[i] = a[j]
408
395
a[j] = i
409
- i == 1 + mask && (mask = 2 * mask + 1 )
410
396
end
411
397
return a
412
398
end
0 commit comments