Skip to content

Commit 2efcb75

Browse files
authored
Merge pull request #198 from dhanak/dev
Use seed! to put every copy of rng into a unique state
2 parents f22e0a6 + 10843eb commit 2efcb75

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

src/classification/main.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,10 @@ function build_forest(
370370
loss = (ns, n) -> util.entropy(ns, n, entropy_terms)
371371

372372
if rng isa Random.AbstractRNG
373+
shared_seed = rand(rng, UInt)
373374
Threads.@threads for i in 1:n_trees
374375
# The Mersenne Twister (Julia's default) is not thread-safe.
375-
_rng = copy(rng)
376-
# Take some elements from the ring to have different states for each tree. This
377-
# is the only way given that only a `copy` can be expected to exist for RNGs.
378-
rand(_rng, i)
376+
_rng = Random.seed!(copy(rng), shared_seed + i)
379377
inds = rand(_rng, 1:t_samples, n_samples)
380378
forest[i] = build_tree(
381379
labels[inds],

src/regression/main.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ function build_forest(
9595
forest = impurity_importance ? Vector{Root{S, T}}(undef, n_trees) : Vector{LeafOrNode{S, T}}(undef, n_trees)
9696

9797
if rng isa Random.AbstractRNG
98+
shared_seed = rand(rng, UInt)
9899
Threads.@threads for i in 1:n_trees
99100
# The Mersenne Twister (Julia's default) is not thread-safe.
100-
_rng = copy(rng)
101-
# Take some elements from the ring to have different states for each tree.
102-
# This is the only way given that only a `copy` can be expected to exist for RNGs.
103-
rand(_rng, i)
101+
_rng = Random.seed!(copy(rng), shared_seed + i)
104102
inds = rand(_rng, 1:t_samples, n_samples)
105103
forest[i] = build_tree(
106104
labels[inds],

0 commit comments

Comments
 (0)