# Get to know the seed for the next CUDA random number

Hi,
I’m struggling to debug a code that does random sampling, and for that I use CUDA random numbers. In order to be able to reproduce the same numbers in a second run of the code, I set the random number generator using

``````CuArrays.seed!(number)
``````

and that seems to work well. But in some cases my sampling goes mad and I need to be able to know what the numbers used were. The simplest way to do that is to stop before the tragedy happens and to get the current seed value, so that if I start a second run from that seed, the sequence of generated numbers would be the same as if I had not stopped the simulation?

Maybe an example:

``````Currand.seed!(0)
Cuarrays(rand)
0.5467546
Cuarrays(rand)
0.434536
Cuarrays(rand)
0.564756
Cuarrays(rand)
0.956235
Cuarrays(rand)
0.354453
``````

Now if I stopped at the fourth step (getting 0.956235), is there a way to get a seed value
s such that
``
Currand.seed!(s)
Cuarrays(rand)

``
yields 0.354453 ?

thanks a lot,

Ferran.

CURAND splits the seed in an initial seed and the offset. There’s no way to recover any of those, but it’s easier to figure out the offset, like such:

``````julia> Random.seed!(CURAND.generator(), seed)

julia> CUDA.rand(Float32, 1)
1-element CuArray{Float32,1,Nothing}:
0.8481736

julia> CUDA.rand(Float32, 1)
1-element CuArray{Float32,1,Nothing}:
0.7659195

julia> Random.seed!(CURAND.generator(), seed)

julia> CUDA.rand(Float32, 1)
1-element CuArray{Float32,1,Nothing}:
0.8481736

julia> CUDA.rand(Float32, 1)
1-element CuArray{Float32,1,Nothing}:
0.7659195

julia> Random.seed!(CURAND.generator(), seed, 1)

julia> CUDA.rand(Float32, 1)
1-element CuArray{Float32,1,Nothing}:
0.7659195
``````

Then the only possible way to re-start from a given point is to keep a counter that updates every time rand() is called?
Thanks,
Ferran

Something like that, yes. Then pass that offset to `seed!`. Not really practical, but the library doesn’t have any other way to do this.