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

**URL:** https://discourse.julialang.org/t/get-to-know-the-seed-for-the-next-cuda-random-number/42363
**Category:** GPU
**Created:** [July 1, 2020, 12:45pm UTC](https://discourse.julialang.org/t/get-to-know-the-seed-for-the-next-cuda-random-number/42363 "2020-07-01T12:45:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)
#### Post date: [July 1, 2020, 12:45pm UTC](https://discourse.julialang.org/t/get-to-know-the-seed-for-the-next-cuda-random-number/42363/1 "2020-07-01T12:45:22Z")

</div>

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

```julia
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:

```julia
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.

---

<div class="post-metadata">

### Author: ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)
#### Post date: [July 1, 2020, 2:04pm UTC](https://discourse.julialang.org/t/get-to-know-the-seed-for-the-next-cuda-random-number/42363/2 "2020-07-01T14:04:32Z")

</div>

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
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

```

---

<div class="post-metadata">

### Author: ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)
#### Post date: [July 2, 2020, 12:29pm UTC](https://discourse.julialang.org/t/get-to-know-the-seed-for-the-next-cuda-random-number/42363/3 "2020-07-02T12:29:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)
#### Post date: [July 3, 2020, 2:19pm UTC](https://discourse.julialang.org/t/get-to-know-the-seed-for-the-next-cuda-random-number/42363/4 "2020-07-03T14:19:57Z")

</div>

> [@Ferran\_Mazzanti](#):
>
> Then the only possible way to re-start from a given point is to keep a counter that updates _every_ time rand() is called?

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.
