Skip to content

Commit 057c58f

Browse files
committed
shuffle: allow multidimensional arrays
1 parent 9f61fda commit 057c58f

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

base/random.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -1480,12 +1480,12 @@ randsubseq(A::AbstractArray, p::Real) = randsubseq(GLOBAL_RNG, A, p)
14801480
end
14811481

14821482
"""
1483-
shuffle!([rng=GLOBAL_RNG,] v)
1483+
shuffle!([rng=GLOBAL_RNG,] v::AbstractArray)
14841484
1485-
In-place version of [`shuffle`](@ref): randomly permute the array `v` in-place,
1485+
In-place version of [`shuffle`](@ref): randomly permute `v` in-place,
14861486
optionally supplying the random-number generator `rng`.
14871487
"""
1488-
function shuffle!(r::AbstractRNG, a::AbstractVector)
1488+
function shuffle!(r::AbstractRNG, a::AbstractArray)
14891489
n = length(a)
14901490
@assert n <= Int64(2)^52
14911491
mask = nextpow2(n) - 1
@@ -1497,18 +1497,18 @@ function shuffle!(r::AbstractRNG, a::AbstractVector)
14971497
return a
14981498
end
14991499

1500-
shuffle!(a::AbstractVector) = shuffle!(GLOBAL_RNG, a)
1500+
shuffle!(a::AbstractArray) = shuffle!(GLOBAL_RNG, a)
15011501

15021502
"""
1503-
shuffle([rng=GLOBAL_RNG,] v)
1503+
shuffle([rng=GLOBAL_RNG,] v::AbstractArray)
15041504
15051505
Return a randomly permuted copy of `v`. The optional `rng` argument specifies a random
15061506
number generator (see [Random Numbers](@ref)).
1507-
To permute `v` in-place, see [`shuffle!`](@ref). To obtain randomly permuted
1507+
To permute `v` in-place, see [`shuffle!`](@ref). To obtain randomly permuted
15081508
indices, see [`randperm`](@ref).
15091509
"""
1510-
shuffle(r::AbstractRNG, a::AbstractVector) = shuffle!(r, copymutable(a))
1511-
shuffle(a::AbstractVector) = shuffle(GLOBAL_RNG, a)
1510+
shuffle(r::AbstractRNG, a::AbstractArray) = shuffle!(r, copymutable(a))
1511+
shuffle(a::AbstractArray) = shuffle(GLOBAL_RNG, a)
15121512

15131513
"""
15141514
randperm([rng=GLOBAL_RNG,] n::Integer)

test/random.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ let mta = MersenneTwister(42), mtb = MersenneTwister(42)
431431
@test shuffle(mta,collect(1:10)) == shuffle(mtb,collect(1:10))
432432
@test shuffle!(mta,collect(1:10)) == shuffle!(mtb,collect(1:10))
433433
@test shuffle(mta,collect(2:11)) == shuffle(mtb,2:11)
434-
434+
@test shuffle!(mta, rand(mta, 2, 3)) == shuffle!(mtb, rand(mtb, 2, 3))
435+
@test shuffle(mta, rand(mta, 2, 3)) == shuffle(mtb, rand(mtb, 2, 3))
435436
@test randperm(mta,10) == randperm(mtb,10)
436437
@test sort!(randperm(10)) == sort!(shuffle(1:10)) == collect(1:10)
437438
@test randperm(mta,big(10)) == randperm(mtb,big(10)) # cf. #16376

0 commit comments

Comments
 (0)