-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
unify shuffle
and randperm
#50318
unify shuffle
and randperm
#50318
Conversation
It makes sense not to me not to have |
The difference can be traced back to the initial implementation of 13 years ago by @ViralBShah: Lines 151 to 169 in 18b72d1
@rfourquet made an improvement in #14772 while preserving the original style. I don't see any reason why they are different, and since julia/stdlib/Random/src/misc.jl Line 267 in e4600c5
julia> n = 4
julia> shuffle(MersenneTwister(1), collect(Base.OneTo(n)))
4-element Vector{Int64}:
1
2
4
3
julia> shuffle(MersenneTwister(1), Base.OneTo(n))
4-element Vector{Int64}:
1
3
4
2
julia> collect(Base.OneTo(n)) == Base.OneTo(n)
true
julia> shuffle(MersenneTwister(1), collect(Base.OneTo(n))) == shuffle(MersenneTwister(1), Base.OneTo(n))
false |
Why is it advantageous to guarantee that these properties hold? It's all randomness either way - I don't think guaranteeing a particular result is desirable, as that will likely constrain any performance or other improvements to these algorithms in the future. Not to mention that |
It's not a major advantage, but if it's easy to do it seems worthwhile |
Also, it makes the source code less readable/maintainable if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
If we want to guarantee these properties, this PR also should check that that they hold in a test. |
It can prevent some further improvements, although this is unlikely to happen. I am happy to add some tests, but don't insist on it. These properties can also be documented, but don't have to be, I think. |
Thanks @ctarn ! |
unify the algorithms of
shuffle
andrandperm
without performance loss, so that:shuffle(rng, 1:n) == randperm(rng, n)
shuffle(rng, eachindex(1:n)) == shuffle(rng, 1:n)
see also:
shuffle
works differently onBase.OneTo
than others #50304correctness:
performance: