# How to ensure reproducibility when using CUDA.rand()?

**URL:** https://discourse.julialang.org/t/how-to-ensure-reproducibility-when-using-cuda-rand/135987
**Category:** GPU
**Tags:** rng
**Created:** [March 3, 2026, 8:36am UTC](https://discourse.julialang.org/t/how-to-ensure-reproducibility-when-using-cuda-rand/135987 "2026-03-03T08:36:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![WG-ZHENG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wg-zheng/32/221054_2.png) [@WG-ZHENG](https://discourse.julialang.org/u/WG-ZHENG)
#### Post date: [March 3, 2026, 8:36am UTC](https://discourse.julialang.org/t/how-to-ensure-reproducibility-when-using-cuda-rand/135987/1 "2026-03-03T08:36:09Z")

</div>

It seems that it is not supported to pass a specified rng as argument to rand in CUDA.jl.  
The codes below raise an error:

```julia-auto
using CUDA
rng = CUDA.RNG(42)
a = CUDA.rand(rng, 1)

```

```julia-auto
ERROR: MethodError: no method matching rand(::CUDA.RNG, ::Int64)
You may have intended to import Base.rand
The function `rand` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  rand(::Integer, ::Integer...)
   @ CUDA E:\Softwares\Julia\.julia\packages\CUDA\x8d2s\src\random.jl:340
  rand(::Union{Type{Float32}, Type{Float64}, Type{UInt32}}, ::Integer, Integer...)
   @ CUDA E:\Softwares\Julia\.julia\packages\CUDA\x8d2s\src\random.jl:306
  rand(::Type, ::Integer, Integer...)
   @ CUDA E:\Softwares\Julia\.julia\packages\CUDA\x8d2s\src\random.jl:330

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [March 3, 2026, 8:44am UTC](https://discourse.julialang.org/t/how-to-ensure-reproducibility-when-using-cuda-rand/135987/2 "2026-03-03T08:44:43Z")

</div>

See

> **[Random numbers - Kernel programming · CUDA.jl](https://cuda.juliagpu.org/stable/development/kernel/#Random-numbers)**
>
> When arrays operations are not flexible enough, you can write your own GPU kernels in Julia. CUDA.jl aims to expose the full power of the CUDA programming model, i.e., at the same level of abstraction as CUDA C/C++, albeit with some Julia-specific...

Summary: it’s [`Random.seed!()`](https://docs.julialang.org/en/v1/stdlib/Random/#Random.seed), not `CUDA.RNG` (where did you find that?)

---

<div class="post-metadata">

### Author: ![WG-ZHENG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wg-zheng/32/221054_2.png) [@WG-ZHENG](https://discourse.julialang.org/u/WG-ZHENG)
#### Post date: [March 3, 2026, 9:06am UTC](https://discourse.julialang.org/t/how-to-ensure-reproducibility-when-using-cuda-rand/135987/3 "2026-03-03T09:06:28Z")

</div>

Thanks for the link.  
So the rng cannot be specified as an argument of CUDA.rand?  
I tried the codes below:

```julia-auto
Random.seed!(42)
a = CUDA.rand(1) # 0.24492861
Random.seed!(42)
b = CUDA.rand(1) # 0.19734985

```

It is still not reproducible…

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [March 3, 2026, 9:19am UTC](https://discourse.julialang.org/t/how-to-ensure-reproducibility-when-using-cuda-rand/135987/4 "2026-03-03T09:19:32Z")

</div>

Uhm, I think it’s `CUDA.seed!`, instead of `Random.seed!`

```julia-auto
julia> using CUDA

julia> CUDA.seed!(42)

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

julia> CUDA.seed!(42)

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

```

Alternatively:

```julia-auto
julia> rng = CUDA.RNG(2)
CUDA.RNG(0x00000002, 0x00000000)

julia> rand(rng, Float32, 1)
1-element CuArray{Float32, 1, CUDA.DeviceMemory}:
 0.59588563

julia> rng = CUDA.RNG(2)
CUDA.RNG(0x00000002, 0x00000000)

julia> rand(rng, Float32, 1)
1-element CuArray{Float32, 1, CUDA.DeviceMemory}:
 0.59588563

```
