Skip to content

Commit

Permalink
Make randperm and randcycle use the argument's type. (#29670)
Browse files Browse the repository at this point in the history
Remove tests for eltype being `Int`. This is potentially controversial
as it may break things, but I did not see it documented anywhere as
part of the API.
  • Loading branch information
tpapp authored and JeffBezanson committed Nov 29, 2018
1 parent 89a2c08 commit caea73d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 9 additions & 5 deletions stdlib/Random/src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,12 @@ shuffle(a::AbstractArray) = shuffle(GLOBAL_RNG, a)
randperm([rng=GLOBAL_RNG,] n::Integer)
Construct a random permutation of length `n`. The optional `rng`
argument specifies a random number generator (see [Random Numbers](@ref)).
To randomly permute an arbitrary vector, see [`shuffle`](@ref)
or [`shuffle!`](@ref).
argument specifies a random number generator (see [Random
Numbers](@ref)). The element type of the result is the same as the type
of `n`.
To randomly permute an arbitrary vector, see [`shuffle`](@ref) or
[`shuffle!`](@ref).
# Examples
```jldoctest
Expand All @@ -269,7 +272,7 @@ julia> randperm(MersenneTwister(1234), 4)
3
```
"""
randperm(r::AbstractRNG, n::Integer) = randperm!(r, Vector{Int}(undef, n))
randperm(r::AbstractRNG, n::T) where {T <: Integer} = randperm!(r, Vector{T}(undef, n))
randperm(n::Integer) = randperm(GLOBAL_RNG, n)

"""
Expand Down Expand Up @@ -317,6 +320,7 @@ randperm!(a::Array{<:Integer}) = randperm!(GLOBAL_RNG, a)
Construct a random cyclic permutation of length `n`. The optional `rng`
argument specifies a random number generator, see [Random Numbers](@ref).
The element type of the result is the same as the type of `n`.
# Examples
```jldoctest
Expand All @@ -330,7 +334,7 @@ julia> randcycle(MersenneTwister(1234), 6)
2
```
"""
randcycle(r::AbstractRNG, n::Integer) = randcycle!(r, Vector{Int}(undef, n))
randcycle(r::AbstractRNG, n::T) where {T <: Integer} = randcycle!(r, Vector{T}(undef, n))
randcycle(n::Integer) = randcycle(GLOBAL_RNG, n)

"""
Expand Down
12 changes: 10 additions & 2 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,26 @@ let mta = MersenneTwister(42), mtb = MersenneTwister(42)
@test sort!(randperm(10)) == sort!(shuffle(1:10)) == 1:10
@test randperm(mta,big(10)) == randperm(mtb,big(10)) # cf. #16376
@test randperm(0) == []
@test eltype(randperm(UInt(1))) === Int
@test_throws ErrorException randperm(-1)

let p = randperm(UInt16(12))
@test typeof(p) Vector{UInt16}
@test sort!(p) == 1:12
end

A, B = Vector{Int}(undef, 10), Vector{Int}(undef, 10)
@test randperm!(mta, A) == randperm!(mtb, B)
@test randperm!(A) === A

@test randcycle(mta,10) == randcycle(mtb,10)
@test eltype(randcycle(UInt(1))) === Int
@test randcycle!(mta, A) == randcycle!(mtb, B)
@test randcycle!(A) === A

let p = randcycle(UInt16(10))
@test typeof(p) Vector{UInt16}
@test sort!(p) == 1:10
end

@test sprand(mta,1,1,0.9) == sprand(mtb,1,1,0.9)
@test sprand(mta,10,10,0.3) == sprand(mtb,10,10,0.3)
end
Expand Down

0 comments on commit caea73d

Please sign in to comment.