Skip to content

Commit b280674

Browse files
committed
try to make rngs deterministic
1 parent 0614f1b commit b280674

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/SSA_stepper.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ function DiffEqBase.__init(jump_prob::JumpProblem,
184184
end
185185
else
186186
cb = deepcopy(jump_prob.jump_callback.discrete_callbacks[end])
187+
rng = cb.condition.rng
187188
if seed === nothing
188-
Random.seed!(cb.condition.rng, rand(UInt64))
189+
Random.seed!(rng, rand(UInt64))
189190
else
190-
Random.seed!(cb.condition.rng, seed)
191+
Random.seed!(rng, seed)
191192
end
192193
end
193194
opts = (callback = CallbackSet(callback),)

src/problem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ then be passed within a single [`JumpSet`](@ref) or as subsequent sequential arg
4444
$(FIELDS)
4545
4646
## Keyword Arguments
47-
- `rng`, the random number generator to use. On 1.7 and up defaults to Julia's built-in
48-
generator, below 1.7 uses RandomNumbers.jl's `Xorshifts.Xoroshiro128Star(rand(UInt64))`.
47+
- `rng`, the random number generator to use. Defaults to Julia's built-in
48+
generator.
4949
- `save_positions=(true,true)`, specifies whether to save the system's state (before, after)
5050
the jump occurs.
5151
- `spatial_system`, for spatial problems the underlying spatial structure.

src/solve.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ end
4747

4848
function resetted_jump_problem(_jump_prob, seed)
4949
jump_prob = deepcopy(_jump_prob)
50+
rng = jump_prob.jump_callback.discrete_callbacks[1].condition.rng
5051
if !isempty(jump_prob.jump_callback.discrete_callbacks)
5152
if seed === nothing
52-
Random.seed!(jump_prob.jump_callback.discrete_callbacks[1].condition.rng,
53-
rand(UInt64))
53+
Random.seed!(rng, rand(UInt64))
5454
else
55-
Random.seed!(jump_prob.jump_callback.discrete_callbacks[1].condition.rng, seed)
55+
Random.seed!(rng, seed)
5656
end
5757
end
5858

test/variable_rate.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,20 @@ end
275275
# https://github.com/SciML/JumpProcesses.jl/issues/320
276276
# note that even with the seeded StableRNG this test is not
277277
# deterministic for some reason.
278-
function getmean(Nsims, prob, alg, dt, tsave)
278+
function getmean(Nsims, prob, alg, dt, tsave, seed)
279279
umean = zeros(length(tsave))
280280
for i in 1:Nsims
281-
sol = solve(prob, alg; saveat = dt)
281+
sol = solve(prob, alg; saveat = dt, seed)
282282
umean .+= Array(sol(tsave; idxs = 1))
283+
seed += 1
283284
end
284285
umean ./= Nsims
285286
return umean
286287
end
287288

288289
let
289-
rng = StableRNG(12345)
290+
seed = 12345
291+
rng = StableRNG(seed)
290292
b = 2.0
291293
d = 1.0
292294
n0 = 1
@@ -320,7 +322,8 @@ let
320322
dt = 0.1
321323
tsave = range(tspan[1], tspan[2]; step = dt)
322324
for alg in (Tsit5(), Rodas5P(linsolve = QRFactorization()))
323-
umean = getmean(Nsims, sjm_prob, alg, dt, tsave)
325+
umean = getmean(Nsims, sjm_prob, alg, dt, tsave, seed)
324326
@test all(abs.(umean .- n.(tsave)) .< 0.05 * n.(tsave))
327+
seed += Nsims
325328
end
326329
end

0 commit comments

Comments
 (0)