Saving extra values during DifferentialEquations solution only at specific times

I have a problem where I want to solve some extra parameters produced while solving a differential equation. For example, the following saves some random number everytime the ODE is evaluated:

f = (u, p, t) -> (a = rand(); push!(p, a); a * u)
u0 = 1 / 2
p = Float64[]
prob = ODEProblem(f, u0, (0.0, 1.0), p)
solve(prob, Tsit5(); saveat = 0.1)

This obviously fails since the ODE is evaluated many more times than simply every 0.1 units of time. How can I change this so that I only push into p when the solution is saved? Is it possible to get the exact time, or at least a closest time?

I think what you are looking for is a callback. If you look at the callback library there is a saving callback which should work for you.

1 Like