# How to set the seed properly in CURAND

**URL:** https://discourse.julialang.org/t/how-to-set-the-seed-properly-in-curand/24180
**Category:** GPU
**Created:** [May 13, 2019, 4:20pm UTC](https://discourse.julialang.org/t/how-to-set-the-seed-properly-in-curand/24180 "2019-05-13T16:20:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [May 13, 2019, 4:20pm UTC](https://discourse.julialang.org/t/how-to-set-the-seed-properly-in-curand/24180/1 "2019-05-13T16:20:12Z")

</div>

I just found it’s a little tricky for me to set the seed in `CuArrays.CURAND`

```julia
julia> using CuArrays, CuArrays.CURAND

julia> s = CuArrays.CURAND.create_generator()
CuArrays.CURAND.RNG(Ptr{Nothing} @0x00000000054faec0, 100)

julia> rand(s, Float32, 3)
3-element CuArray{Float32,1}:
 0.74021935
 0.9209938 
 0.03902049

julia> rand(s, Float32, 3)
3-element CuArray{Float32,1}:
 0.9689629 
 0.92514056
 0.4463501 

```

Obviously, setting the same seed `s` doesn’t work.

After reading the source code, I found there’s a function named `set_pseudo_random_generator_seed`:

```julia
julia> CuArrays.CURAND.set_pseudo_random_generator_seed(s, 123)
0x00000000

julia> rand(s, Float32, 3)
3-element CuArray{Float32,1}:
 0.048817795
 0.57627815 
 0.8071622  

julia> CuArrays.CURAND.set_pseudo_random_generator_seed(s, 123)
0x00000000

julia> rand(s, Float32, 3)
3-element CuArray{Float32,1}:
 0.036559016
 0.5210836  
 0.61308384 

```

Still not working.

After googling I found this answer:

> <https://stackoverflow.com/questions/12212384/curand-generating-different-random-numbers-with-same-seed>

And yes, it seems works!

```julia
julia> CuArrays.CURAND.set_generator_offset(s, 0)
0x00000000

julia> rand(s, Float32, 3)
3-element CuArray{Float32,1}:
 0.48431787
 0.40131402
 0.79087174

julia> CuArrays.CURAND.set_generator_offset(s, 0)
0x00000000

julia> rand(s, Float32, 3)
3-element CuArray{Float32,1}:
 0.48431787
 0.40131402
 0.79087174

```

I’m not very familiar with CURAND. Can anyone help to point out why I need to call `set_generator_offset` here (is it the right way to do so)? And what’s the meaning of `set_pseudo_random_generator_seed`?

Found another resource. Hope it is useful for someone else.

> **[Creating Randomness and Acummulating Change](https://richiesams.blogspot.com/2015/03/creating-randomness-and-acummulating.html)**
>
> This is the second post in a series documenting my adventures in creating a GPU path tracer from scratch. If you missed it, the first post i...

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [May 14, 2019, 9:04am UTC](https://discourse.julialang.org/t/how-to-set-the-seed-properly-in-curand/24180/2 "2019-05-14T09:04:11Z")

</div>

The curand documentation explains the offset briefly:

[https://docs.nvidia.com/cuda/curand/host-api-overview.html#generator-options](https://docs.nvidia.com/cuda/curand/host-api-overview.html#generator-options)

To get the same random numbers you’d need the same seed and the same offset, as you’ve found. The seed defines the sequence, and the offset allows you to skip to a point in the sequence.

It does seem a bit surprising that setting the seed doesn’t also reset the offset to zero.

---

<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: [November 15, 2019, 8:53am UTC](https://discourse.julialang.org/t/how-to-set-the-seed-properly-in-curand/24180/3 "2019-11-15T08:53:10Z")

</div>

Hi,

is this still working for you? In my case, the very first line

```julia
using CuArrays, CuArrays.CURAND
s = CuArrays.CURAND.create_generator()

```

spits

```julia
UndefVarError: create_generator not defined

Stacktrace:
 [1] getproperty(::Module, ::Symbol) at ./Base.jl:13
 [2] top-level scope at In[14]:2

```

so I can not initialize my random numboos ☹  
But I’m 100% sure I’m missing something, so any help will be much appreciated.  
BTW my CuArrays etc things are working well on my Julia 1.2.0 boxes… pretty well I must say! More than 100 times faster that usual Julia, which I start to seriously believe is not that fast as people claims…

Thanks in advance,

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: [November 15, 2019, 8:55am UTC](https://discourse.julialang.org/t/how-to-set-the-seed-properly-in-curand/24180/4 "2019-11-15T08:55:41Z")

</div>

@findmyway made a PR that puts these calls in `seed!`, so there’s no need to call internal methods anymore: [https://github.com/JuliaGPU/CuArrays.jl/pull/344](https://github.com/JuliaGPU/CuArrays.jl/pull/344).

---

<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: [November 15, 2019, 4:04pm UTC](https://discourse.julialang.org/t/how-to-set-the-seed-properly-in-curand/24180/5 "2019-11-15T16:04:49Z")

</div>

Oh, thanks, that now works as expected.  
May I tell you that you guys are doing a great work with this CUDA ports to Julia? It’s been the first time that I can do real and useful CUDA programming without feeling it it a big mess 🙂
