Skip to content

Commit 9a3d4b4

Browse files
authored
Merge pull request #22226 from JuliaLang/rf/shuffle-array
shuffle: allow multidimensional arrays
2 parents 5aa15ff + 1946a1f commit 9a3d4b4

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

base/random.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -1482,12 +1482,12 @@ randsubseq(A::AbstractArray, p::Real) = randsubseq(GLOBAL_RNG, A, p)
14821482
end
14831483

14841484
"""
1485-
shuffle!([rng=GLOBAL_RNG,] v)
1485+
shuffle!([rng=GLOBAL_RNG,] v::AbstractArray)
14861486
1487-
In-place version of [`shuffle`](@ref): randomly permute the array `v` in-place,
1487+
In-place version of [`shuffle`](@ref): randomly permute `v` in-place,
14881488
optionally supplying the random-number generator `rng`.
14891489
"""
1490-
function shuffle!(r::AbstractRNG, a::AbstractVector)
1490+
function shuffle!(r::AbstractRNG, a::AbstractArray)
14911491
n = length(a)
14921492
@assert n <= Int64(2)^52
14931493
mask = nextpow2(n) - 1
@@ -1499,18 +1499,18 @@ function shuffle!(r::AbstractRNG, a::AbstractVector)
14991499
return a
15001500
end
15011501

1502-
shuffle!(a::AbstractVector) = shuffle!(GLOBAL_RNG, a)
1502+
shuffle!(a::AbstractArray) = shuffle!(GLOBAL_RNG, a)
15031503

15041504
"""
1505-
shuffle([rng=GLOBAL_RNG,] v)
1505+
shuffle([rng=GLOBAL_RNG,] v::AbstractArray)
15061506
15071507
Return a randomly permuted copy of `v`. The optional `rng` argument specifies a random
15081508
number generator (see [Random Numbers](@ref)).
1509-
To permute `v` in-place, see [`shuffle!`](@ref). To obtain randomly permuted
1509+
To permute `v` in-place, see [`shuffle!`](@ref). To obtain randomly permuted
15101510
indices, see [`randperm`](@ref).
15111511
"""
1512-
shuffle(r::AbstractRNG, a::AbstractVector) = shuffle!(r, copymutable(a))
1513-
shuffle(a::AbstractVector) = shuffle(GLOBAL_RNG, a)
1512+
shuffle(r::AbstractRNG, a::AbstractArray) = shuffle!(r, copymutable(a))
1513+
shuffle(a::AbstractArray) = shuffle(GLOBAL_RNG, a)
15141514

15151515
"""
15161516
randperm([rng=GLOBAL_RNG,] n::Integer)

test/random.jl

+2
Original file line numberDiff line numberDiff line change
@@ -431,6 +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+
@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))
434436

435437
@test randperm(mta,10) == randperm(mtb,10)
436438
@test sort!(randperm(10)) == sort!(shuffle(1:10)) == collect(1:10)

0 commit comments

Comments
 (0)