Skip to content

Commit 1154fd9

Browse files
committed
Random: make randperm! and randcycle! accept AbstractArray
Relax the dispatch from `Array` to `AbstractArray`, while requiring one-based indexing.
1 parent d3759ba commit 1154fd9

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Standard library changes
6161

6262
#### Profile
6363

64+
#### Random
65+
66+
* `randperm!` and `randcycle!` now support non-`Array` `AbstractArray` inputs, assuming they are mutable and their indices are one-based ([#58596]).
67+
6468
#### REPL
6569

6670
* The display of `AbstractChar`s in the main REPL mode now includes LaTeX input information like what is shown in help mode ([#58181]).

stdlib/Random/src/misc.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,16 @@ randperm(r::AbstractRNG, n::T) where {T <: Integer} = randperm!(r, Vector{T}(und
297297
randperm(n::Integer) = randperm(default_rng(), n)
298298

299299
"""
300-
randperm!([rng=default_rng(),] A::Array{<:Integer})
300+
randperm!([rng=default_rng(),] A::AbstractArray{<:Integer})
301301
302302
Construct in `A` a random permutation of length `length(A)`. The
303303
optional `rng` argument specifies a random number generator (see
304304
[Random Numbers](@ref)). To randomly permute an arbitrary vector, see
305305
[`shuffle`](@ref) or [`shuffle!`](@ref).
306306
307+
!!! compat "Julia 1.13"
308+
`A isa Array` was required prior to Julia v1.13.
309+
307310
# Examples
308311
```jldoctest
309312
julia> randperm!(Xoshiro(123), Vector{Int}(undef, 4))
@@ -314,8 +317,9 @@ julia> randperm!(Xoshiro(123), Vector{Int}(undef, 4))
314317
3
315318
```
316319
"""
317-
function randperm!(r::AbstractRNG, a::Array{<:Integer})
320+
function randperm!(r::AbstractRNG, a::AbstractArray{<:Integer})
318321
# keep it consistent with `shuffle!` and `randcycle!` if possible
322+
Base.require_one_based_indexing(a)
319323
n = length(a)
320324
@assert n <= Int64(2)^52
321325
n == 0 && return a
@@ -332,7 +336,7 @@ function randperm!(r::AbstractRNG, a::Array{<:Integer})
332336
return a
333337
end
334338

335-
randperm!(a::Array{<:Integer}) = randperm!(default_rng(), a)
339+
randperm!(a::AbstractArray{<:Integer}) = randperm!(default_rng(), a)
336340

337341

338342
## randcycle & randcycle!
@@ -370,7 +374,7 @@ randcycle(r::AbstractRNG, n::T) where {T <: Integer} = randcycle!(r, Vector{T}(u
370374
randcycle(n::Integer) = randcycle(default_rng(), n)
371375

372376
"""
373-
randcycle!([rng=default_rng(),] A::Array{<:Integer})
377+
randcycle!([rng=default_rng(),] A::AbstractArray{<:Integer})
374378
375379
Construct in `A` a random cyclic permutation of length `n = length(A)`.
376380
The optional `rng` argument specifies a random number generator, see
@@ -382,6 +386,9 @@ which are sampled uniformly. If `A` is empty, `randcycle!` leaves it unchanged.
382386
383387
[`randcycle`](@ref) is a variant of this function that allocates a new vector.
384388
389+
!!! compat "Julia 1.13"
390+
`A isa Array` was required prior to Julia v1.13.
391+
385392
# Examples
386393
```jldoctest
387394
julia> randcycle!(Xoshiro(123), Vector{Int}(undef, 6))
@@ -394,8 +401,9 @@ julia> randcycle!(Xoshiro(123), Vector{Int}(undef, 6))
394401
1
395402
```
396403
"""
397-
function randcycle!(r::AbstractRNG, a::Array{<:Integer})
404+
function randcycle!(r::AbstractRNG, a::AbstractArray{<:Integer})
398405
# keep it consistent with `shuffle!` and `randperm!` if possible
406+
Base.require_one_based_indexing(a)
399407
n = length(a)
400408
@assert n <= Int64(2)^52
401409
n == 0 && return a
@@ -411,4 +419,4 @@ function randcycle!(r::AbstractRNG, a::Array{<:Integer})
411419
return a
412420
end
413421

414-
randcycle!(a::Array{<:Integer}) = randcycle!(default_rng(), a)
422+
randcycle!(a::AbstractArray{<:Integer}) = randcycle!(default_rng(), a)

0 commit comments

Comments
 (0)