Skip to content

Commit

Permalink
Replace MersenneTwister rng with StableRNG
Browse files Browse the repository at this point in the history
This was suggested in a discourse post where another package had
random numbers change between Julia versions.
Apparently this is expected behavior.
https://discourse.julialang.org/t/julia-1-11-1-gives-different-results-from-1-10-5/123433/53
  • Loading branch information
nathanrboyer committed Dec 6, 2024
1 parent 19e910b commit dba4dcf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
15 changes: 14 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,21 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
ShiftedArrays = "1277b4bf-5013-50f5-be3d-901d8477a67a"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["CategoricalArrays", "Combinatorics", "DataValues", "Dates", "Logging", "OffsetArrays", "Test", "Unitful", "ShiftedArrays", "SparseArrays"]
test = [
"CategoricalArrays",
"Combinatorics",
"DataValues",
"Dates",
"Logging",
"OffsetArrays",
"ShiftedArrays",
"SparseArrays",
"StableRNGs",
"Test",
"Unitful",
]
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DataFramesMeta = "1313f7d8-7da2-5740-9ea0-a2ca25f37964"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
Query = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
TidierData = "fe2206b3-d496-4ee9-a338-6a095c4ece80"
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Documenter
using DataFrames
using CategoricalArrays
using StableRNGs

DocMeta.setdocmeta!(DataFrames, :DocTestSetup, :(using DataFrames); recursive=true)

Expand Down
28 changes: 14 additions & 14 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2547,20 +2547,20 @@ $METADATA_FIXED
# Examples
```jldoctest
julia> using Random
julia> using Random, StableRNGs
julia> rng = MersenneTwister(1234);
julia> rng = StableRNG(1234);
julia> shuffle(rng, DataFrame(a=1:5, b=1:5))
5×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 3 3
2 │ 4 4
3 │ 1 1
4 │ 2 2
5 │ 5 5
1 │ 2 2
2 │ 1 1
3 │ 3 3
4 │ 5 5
5 │ 4 4
```
"""
Random.shuffle(df::AbstractDataFrame) =
Expand All @@ -2585,20 +2585,20 @@ Metadata having other styles is dropped (from parent data frame when `df` is a `
# Examples
```jldoctest
julia> using Random
julia> using Random, StableRNGs
julia> rng = MersenneTwister(1234);
julia> rng = StableRNG(1234);
julia> shuffle!(rng, DataFrame(a=1:5, b=1:5))
5×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 3 3
2 │ 4 4
3 │ 1 1
4 │ 2 2
5 │ 5 5
1 │ 2 2
2 │ 1 1
3 │ 3 3
4 │ 5 5
5 │ 4 4
```
"""
Random.shuffle!(df::AbstractDataFrame) =
Expand Down
14 changes: 7 additions & 7 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestDataFrame

using Dates, DataFrames, Statistics, Random, Test, Logging, DataStructures,
CategoricalArrays
CategoricalArrays, StableRNGs
using DataFrames: _columns, index
using OffsetArrays: OffsetArray
const = isequal
Expand Down Expand Up @@ -2136,17 +2136,17 @@ end
refdf = DataFrame(a=1:5, b=11:15)
refdf.c = refdf.a
for df in (refdf, view(refdf, 2:5, [2, 1]))
x = randperm(MersenneTwister(1234), nrow(df))
mt = MersenneTwister(1234)
x = randperm(StableRNG(1234), nrow(df))
mt = StableRNG(1234)
@test shuffle(mt, df) == df[x, :]
Random.seed!(1234)
x = randperm(nrow(df))
Random.seed!(1234)
@test shuffle(df) == df[x, :]
end
df = copy(refdf)
x = randperm(MersenneTwister(1234), nrow(df))
mt = MersenneTwister(1234)
x = randperm(StableRNG(1234), nrow(df))
mt = StableRNG(1234)
@test shuffle!(mt, df) === df
@test df == refdf[x, :]
df = copy(refdf)
Expand All @@ -2158,8 +2158,8 @@ end

df = copy(refdf)
dfv = view(df, 2:4, [3, 1])
x = randperm(MersenneTwister(1234), nrow(dfv))
mt = MersenneTwister(1234)
x = randperm(StableRNG(1234), nrow(dfv))
mt = StableRNG(1234)
@test shuffle!(mt, dfv) === dfv
@test dfv == view(refdf, 2:4, [3, 1])[x, :]
@test df[1, :] == refdf[1, :]
Expand Down

0 comments on commit dba4dcf

Please sign in to comment.