Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sample with wide integer types #873

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StatsBase"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
authors = ["JuliaStats"]
version = "0.34.0"
version = "0.34.1"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
8 changes: 4 additions & 4 deletions src/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ Draw a pair of distinct integers between 1 and `n` without replacement.
Optionally specify a random number generator `rng` as the first argument
(defaults to `Random.GLOBAL_RNG`).
"""
function samplepair(rng::AbstractRNG, n::Int)
i1 = rand(rng, 1:n)
i2 = rand(rng, 1:n-1)
function samplepair(rng::AbstractRNG, n::Integer)
i1 = rand(rng, Base.OneTo(n))
i2 = rand(rng, Base.OneTo(n - one(n)))
ararslan marked this conversation as resolved.
Show resolved Hide resolved
return (i1, ifelse(i2 == i1, n, i2))
end
samplepair(n::Int) = samplepair(Random.GLOBAL_RNG, n)
samplepair(n::Integer) = samplepair(Random.GLOBAL_RNG, n)

"""
samplepair([rng], a)
Expand Down
15 changes: 14 additions & 1 deletion test/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ test_rng_use(sample, 1:10, 10)

@test samplepair(rng, [3, 4, 2, 6, 8]) === (3, 8)
@test samplepair(rng, [1, 2]) === (1, 2)

@test extrema(samplepair(rng, UInt128(2))) == UInt128.((1, 2))
ararslan marked this conversation as resolved.
Show resolved Hide resolved
end

test_rng_use(samplepair, 1000)
Expand Down Expand Up @@ -265,4 +267,15 @@ test_same(replace=false, ordered=false)
# This corner case should succeed
f(view(x, 2:4), view(x, 5:6))
end
end
end

@testset "issue #872" begin
for T in [Int8, Int16, Int32, Int64, Int128, BigInt], f in [identity, unsigned]
T == BigInt && f == unsigned && continue
T = f(T)
ararslan marked this conversation as resolved.
Show resolved Hide resolved
samp = sample(T(1):T(10), T(2); replace=false, ordered=false)
@test all(x -> x isa T, samp)
@test all(x -> T(1) <= x <= T(10), samp)
@test length(samp) == 2
end
end