JuliaRandom / StableRNGs.jl

A Julia RNG with stable streams
MIT License
57 stars 6 forks source link

Inconsistent results for StatsBase `wsample` #3

Closed Arkoniak closed 4 years ago

Arkoniak commented 4 years ago

I've found this strange behavior, not sure where the problem is.

Julia 1.4.1

using StableRNGs
using StatsBase

rng = StableRNG(2020)
ws = rand(rng, 150)

# Reset here rng, since `ws` are not so important
rng = StableRNG(2020)
wsample(rng, 1:150, ws, 100)[1:5]

5-element Array{Int64,1}:
  19
 117
  57
  42
 106

Julia nightly (Version 1.5.0-DEV.873 (2020-05-07), Commit d4d930399c (0 days old master))

using StableRNGs
using StatsBase

rng = StableRNG(2020)
ws = rand(rng, 150)

# Reset here rng, since `ws` are not so important
rng = StableRNG(2020)
wsample(rng, 1:150, ws, 100)[1:5]

5-element Array{Int64,1}:
  36
 147
  68
  44
  57

In both Julia versions

Pkg.status("StableRNGs")
# StableRNGs v0.1.1
Pkg.status("StatsBase")
# StatsBase v0.33.0
rfourquet commented 4 years ago

Thanks for the report! I get an error on the call to sample:

julia> sample(rng, 1:150, ws, 100)[1:5]
ERROR: MethodError: no method matching sample(::LehmerRNG, ::UnitRange{Int64}, ::Array{Float64,1}, ::Int64)
Closest candidates are:
  sample(::Random.AbstractRNG, ::AbstractArray{T,N} where N, ::AbstractWeights, ::Integer; replace, ordered) where T
[...]

Can you reproduce the bug in a fresh Julia session without loading your startup.jl file? (julia --startup-file=no)

Arkoniak commented 4 years ago

Sorry, it was a typo, it should have been wsample. Fixed description.

rfourquet commented 4 years ago

Ok, I found the problem: StatsBase uses RangeGenerator in alias_sample!, which is probably a legacy from the pre-Julia-1.0 era. RangeGenerator in turn has changed its result in version 1.5. I will make a PR to StatsBase fixing this. Thanks again!

rfourquet commented 4 years ago

This should now be fixed in StatsBase (on master).