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
sample
throws error when used on UInt64
#872
Comments
I have no idea why it works for julia> for T in [Int8, Int16, Int32, Int64, Int128, BigInt], f in [identity, unsigned]
T == BigInt && f == unsigned && continue
T = f(T)
try
sample(T(1):T(10), T(2); replace=false, ordered=false)
@info "$T works"
catch ex
if ex isa MethodError && ex.f == samplepair
@info "$T does not work :("
else
rethrow()
end
end
end
[ Info: Int8 works
[ Info: UInt8 works
[ Info: Int16 works
[ Info: UInt16 works
[ Info: Int32 works
[ Info: UInt32 works
[ Info: Int64 works
[ Info: UInt64 does not work :(
[ Info: Int128 does not work :(
[ Info: UInt128 does not work :(
[ Info: BigInt does not work :( At any rate, this fixes it: --- a/src/sampling.jl
+++ b/src/sampling.jl
@@ -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)))
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) |
Thank you for a quick reply. Looking forward to newer versions of Julia, where this is fixed. Best regards |
Do you mean you tried with a source build of Julia |
Your code below
Those are probably the changes you made to fix the problem. I haven't tried anything new. I always used Julia master. Off-topic: It would be nice if StatsBase.jl were one day merged with Statistics.jl, as the former is not part of Base, but the latter is, so the naming is confusing. |
It will be fixed in the next release of the StatsBase package; it is not tied to Julia itself. Once the release is available, updating the package environment(s) where you're using StatsBase should automatically give you the new version that contains the fix. |
Thank you. Hopefully your edits won't have any performance degrading effects ( |
There should be no adverse effect on performance with those changes. |
In Julia 1.9.1 with
StatsBase
v0.34.0, the following coderuns fine, but the code
StatsBase.sample(T(1):T(10), T(2), replace=false,ordered=false)
returns
However, the code below runs fine:
The text was updated successfully, but these errors were encountered: