# Random numbers in CUDA

**URL:** <https://discourse.julialang.org/t/random-numbers-in-cuda/22239>\
**Category:** GPU\
**Created:** [March 23, 2019, 10:05am UTC](https://discourse.julialang.org/t/random-numbers-in-cuda/22239 "2019-03-23T10:05:46Z")\
**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:** [March 23, 2019, 10:05am UTC](https://discourse.julialang.org/t/random-numbers-in-cuda/22239/1 "2019-03-23T10:05:46Z")

</div>

Hi again,  
just in an attempt to build a very simple Monte Carlo calculation that uses the GPU, I need to generate random numbers. What is the current best way to get a bunch of random numbers with the GPU? I tried things like

```julia
rand(CuArray,Float32,10)

```

but got funny results

```julia
MethodError: no method matching rand(::Type{CuArray}, ::Type{Float32}, ::Int64)
Closest candidates are:
  rand(!Matched::Random.AbstractRNG, ::Type{X}, ::Integer, !Matched::Integer...) where X at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Random/src/Random.jl:257
  rand(!Matched::Random.AbstractRNG, ::Any, ::Integer, !Matched::Integer...) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Random/src/Random.jl:248
  rand(::Type{X}, !Matched::Integer, ::Integer...) where X at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/Random/src/Random.jl:258
  ...

Stacktrace:
 [1] top-level scope at In[18]:1

```

Thx,

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:** [March 23, 2019, 10:47am UTC](https://discourse.julialang.org/t/random-numbers-in-cuda/22239/2 "2019-03-23T10:47:07Z")

</div>

Why did you expect that to work? Doesn’t work with Base.Array either:

```julia
julia> using Random

julia> rand(Array, Float32, 10)
ERROR: MethodError: no method matching rand(::Type{Array}, ::Type{Float32}, ::Int64)

```

Either you can use the specific `curand` function (awaiting a constructor-based syntax):

```julia
julia> using CuArrays.CURAND

julia> curand(Float32, 10)
10-element CuArray{Float32,1}:

```

Or you can use the more idiomatic `rand!`:

```julia
julia> using Random

julia> a = CuArray{Float32}(undef, 10);

julia> rand!(a)
10-element CuArray{Float32,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:** [March 23, 2019, 10:56am UTC](https://discourse.julialang.org/t/random-numbers-in-cuda/22239/3 "2019-03-23T10:56:09Z")

</div>

Sorry, I expected that to work simply because it took it from elsewhere in the internet. But I also tried the

```julia
curand(Float32, 10)

```

version first (which actually seems the most obvious one), but failed 😕 Now I see that I was missing the

```julia
using CuArrays.CURAND

```

statement, while I explicitly had

```julia
using CuArrays

```

in my script. I somehow understood that, by issuing the latter, the former was implicitly loaded… but I was wrong.  
I’m sorry to bother with all these basic questions, I just wanted to avoid all that when I asked about documentation in a previous post (and please do not misunderstand me, I mean no complain 🙂 ).

Best and thanks again,

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:** [March 23, 2019, 11:08am UTC](https://discourse.julialang.org/t/random-numbers-in-cuda/22239/4 "2019-03-23T11:08:00Z")

</div>

> [@Ferran\_Mazzanti](#):
>
> Sorry, I expected that to work simply because it took it from elsewhere in the internet.

It used to be that way with GPUArrays, but it’s an API that non of the other array types support so we got rid of it. I asked about it because some of the docstrings might still mention the old syntax.

> [@Ferran\_Mazzanti](#):
>
> I somehow understood that, by issuing the latter, the former was implicitly loaded… but I was wrong.

We should fix that, since `rand` is always available: [Mimic Base exports · Issue #224 · JuliaGPU/CuArrays.jl · GitHub](https://github.com/JuliaGPU/CuArrays.jl/issues/224)
