JuliaRandom / StableRNGs.jl

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

StableRNG is not stable #4

Closed ablaom closed 4 years ago

ablaom commented 4 years ago

set up

using Random
using StableRNGs

gen = Random.RangeGenerator(1:2)

in julia 1.4.2

rng = StableRNGs.StableRNG(600)
julia> [rand(rng, gen) for i in 1:3]
3-element Array{Int64,1}:
 2
 2
 1

in julia 1.5.0

rng = StableRNGs.StableRNG(600)
julia> [rand(rng, gen) for i in 1:3]
3-element Array{Int64,1}:
 2
 1
 1

This affects rand for Distributions.Categorical and, presumably, many other functions.

version info

Julia Version 1.4.2
Commit 44fa15b150* (2020-05-23 18:35 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, skylake)
Environment:
  JULIA_PATH = /Applications/Julia-1.4.app/Contents/Resources/julia/bin/julia
  JULIA_NUM_THREADS = 5
julia> versioninfo()
Julia Version 1.5.0
Commit 96786e22cc (2020-08-01 23:44 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:
  JULIA_PATH = /Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia
  JULIA_NUM_THREADS = 5
ablaom commented 4 years ago

Of course the other possibility is a bug in Random's implementation of rand for the objects constructed with RangeGenerator, which I have not investigated.

rfourquet commented 4 years ago

Thanks for the report. This is expected behavior, RangeGenerator dates back from pre-Julia-1.0 era and was kept only for helping the transition, it's deprecated. The current API would be to write gen = Random.Sampler(rng, 1:n). Cf. e.g. https://github.com/JuliaStats/StatsBase.jl/pull/576 which fixed a similar use of RangeGenerator in StatsBase.

ablaom commented 4 years ago

Thanks for explaining!