julia icon indicating copy to clipboard operation
julia copied to clipboard

Bad error message in rand([])

Open LilithHafner opened this issue 1 year ago • 2 comments

julia> rand([])
ERROR: ArgumentError: range must be non-empty
Stacktrace:
 [1] SamplerRangeNDL
   @ ~/.julia/juliaup/julia-1.8.0-rc3+0.x64/share/julia/stdlib/v1.8/Random/src/generation.jl:333 [inlined]
 [2] Sampler
   @ ~/.julia/juliaup/julia-1.8.0-rc3+0.x64/share/julia/stdlib/v1.8/Random/src/generation.jl:189 [inlined]
 [3] Sampler
   @ ~/.julia/juliaup/julia-1.8.0-rc3+0.x64/share/julia/stdlib/v1.8/Random/src/generation.jl:418 [inlined]
 [4] Sampler
   @ ~/.julia/juliaup/julia-1.8.0-rc3+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:140 [inlined]
 [5] rand
   @ ~/.julia/juliaup/julia-1.8.0-rc3+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:254 [inlined]
 [6] rand(X::Vector{Any})
   @ Random ~/.julia/juliaup/julia-1.8.0-rc3+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:259
 [7] top-level scope
   @ REPL[273]:1

[] is not a range.

LilithHafner avatar Aug 07 '22 18:08 LilithHafner

function SamplerRangeNDL(r::AbstractUnitRange{T}) where {T}

does receive an AbstractUnitRange, and complains if it's empty. The conversion from Array to Range happens here at

Sampler(::Type{RNG}, r::AbstractArray, n::Repetition) where {RNG<:AbstractRNG} =
    SamplerSimple(r, Sampler(RNG, firstindex(r):lastindex(r), n))

mgkuhn avatar Aug 08 '22 18:08 mgkuhn

We could either throw a different message in SamplerRangeNDL (e.g. "collection must be non-empty", a less specific but still true message) or add another check earlier in the pipeline.

LilithHafner avatar Aug 08 '22 18:08 LilithHafner