Skip to content

Commit

Permalink
add shuffle implementation to preserve stability (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson authored Dec 11, 2023
1 parent f52c066 commit 4d02c43
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ jobs:
matrix:
version:
- "1.0" # earliest supported release
- "1.1"
- "1.2"
- "1.3"
- "1.4"
- "1.5"
- "1.6"
- "1.7"
- "1.8"
- "1" # latest release
- "nightly"
os:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StableRNGs"
uuid = "860ef19b-820b-49d6-a774-d7a799459cd3"
authors = ["Rafael Fourquet <[email protected]>"]
version = "1.0.0"
version = "1.0.1"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
22 changes: 22 additions & 0 deletions src/StableRNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ using Random: Random, AbstractRNG, Sampler, SamplerType

import Random: rand, seed!

# Compat
if VERSION < v"1.2.0-"
using Base: has_offset_axes
require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))
else
using Base: require_one_based_indexing
end

# implementation of LehmerRNG based on the constants found at the
# MIT licensed code by Melissa E. O'Neill at
Expand Down Expand Up @@ -132,5 +139,20 @@ for T in Base.BitInteger_types
SamplerRangeFast(r)
end

# https://github.com/JuliaRandom/StableRNGs.jl/issues/10
Random.shuffle(r::StableRNG, a::AbstractArray) = Random.shuffle!(r, Base.copymutable(a))
function Random.shuffle!(r::StableRNG, a::AbstractArray)
require_one_based_indexing(a)
n = length(a)
n <= 1 && return a # nextpow below won't work with n == 0
@assert n <= Int64(2)^52
mask = nextpow(2, n) - 1
for i = n:-1:2
(mask >> 1) == i && (mask >>= 1)
j = 1 + rand(r, Random.ltm52(i, mask))
a[i], a[j] = a[j], a[i]
end
return a
end

end # module
8 changes: 2 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,11 @@ end
end
end

# Reference test
@testset "`shuffle` stability" begin
a = 1:10
a_shuffled = [4, 5, 6, 1, 3, 7, 10, 2, 8, 9]

# If these tests fail due to changes in the algorithm used in `Random.shuffle`,
# we should vendor the old implementation here to keep it stable.
# See <https://github.com/JuliaRandom/StableRNGs.jl/issues/10>.
@test shuffle(StableRNG(10), a) == a_shuffled

@test shuffle(StableRNG(10), a) == a_shuffled
b = collect(a)
shuffle!(StableRNG(10), b)
@test b == a_shuffled
Expand Down

2 comments on commit 4d02c43

@rfourquet
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/96886

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v1.0.1 -m "<description of version>" 4d02c437e7f634655419dfdb65933b93c370da38
git push origin v1.0.1

Please sign in to comment.