Skip to content

Commit

Permalink
Fix sample with wide integer types (#873)
Browse files Browse the repository at this point in the history
As observed in issue 872, an unordered sample without replacement with a
type 64 bits or wider (notably excluding `Int64`, at least on a 64-bit
system) hits a `MethodError` in `samplepair`, which had been restricted
to `Int` inputs. Relaxing this restriction and adjusting the
`samplepair` implementation accordingly allows these cases to succeed.
  • Loading branch information
ararslan committed Jul 5, 2023
1 parent 82190d1 commit e85e008
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
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, one(n):n)
i2 = rand(rng, one(n):(n - one(n)))
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
29 changes: 28 additions & 1 deletion test/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ test_rng_use(sample, 1:10, 10)

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

onetwo = samplepair(rng, UInt128(2))
@test extrema(onetwo) == (1, 2)
@test eltype(onetwo) === UInt128
end

test_rng_use(samplepair, 1000)
Expand Down Expand Up @@ -265,4 +269,27 @@ 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)
# The type of the second argument should not affect the return type
let 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
let samp = sample(T(1):T(10), 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
let samp = sample(1:10, T(2); replace=false, ordered=false)
@test all(x -> x isa Int, samp)
@test all(x -> 1 <= x <= 10, samp)
@test length(samp) == 2
end
end
end

2 comments on commit e85e008

@devmotion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92211

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.34.1 -m "<description of version>" e85e008b017490382a5ad0b0c7fbe90d53e2a6dc
git push origin v0.34.1

Please sign in to comment.