# Random numbers in \[0.f0,1.f0\]

**URL:** https://discourse.julialang.org/t/random-numbers-in-0-f0-1-f0/30926
**Category:** GPU
**Created:** [November 10, 2019, 5:31pm UTC](https://discourse.julialang.org/t/random-numbers-in-0-f0-1-f0/30926 "2019-11-10T17:31:00Z")
**Posts on this page:** 3
**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: [November 10, 2019, 5:31pm UTC](https://discourse.julialang.org/t/random-numbers-in-0-f0-1-f0/30926/1 "2019-11-10T17:31:00Z")

</div>

Hi again,  
is there a simple way to have the same as this

```julia
rand([0.f0,1.f0],10)

```

on the GPU? I need to generate random bits (in that case they’re Float32 but anyway), and trying the obvious thing refuses to work

```julia
CuArrays.rand([0.f0,1.f0],10)

MethodError: no method matching rand(::Array{Float32,1}, ::Int64)
You may have intended to import Base.rand
Closest candidates are:
  rand(!Matched::Union{Type{Float32}, Type{Float64}}, ::Integer, !Matched::Integer...) at /home/mazzanti/.julia/packages/CuArrays/wXQp8/src/rand/highlevel.jl:122
  rand(!Matched::Type, ::Integer, !Matched::Integer...) at /home/mazzanti/.julia/packages/CuArrays/wXQp8/src/rand/highlevel.jl:147
  rand(!Matched::Integer, ::Integer...) at /home/mazzanti/.julia/packages/CuArrays/wXQp8/src/rand/highlevel.jl:157

Stacktrace:
 [1] ##core#449() at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:297
 [2] ##sample#450(::BenchmarkTools.Parameters) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:303
 [3] #_run#16(::Bool, ::String, ::Base.Iterators.Pairs{Symbol,Integer,NTuple{4,Symbol},NamedTuple{(:samples, :evals, :gctrial, :gcsample),Tuple{Int64,Int64,Bool,Bool}}}, ::typeof(BenchmarkTools._run), ::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}, ::BenchmarkTools.Parameters) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:331
 [4] (::getfield(BenchmarkTools, Symbol("#kw##_run")))(::NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}, ::typeof(BenchmarkTools._run), ::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}, ::BenchmarkTools.Parameters) at ./none:0
 [5] (::getfield(Base, Symbol("#inner#2")){Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}},typeof(BenchmarkTools._run),Tuple{BenchmarkTools.Benchmark{Symbol("##benchmark#448")},BenchmarkTools.Parameters}})() at ./essentials.jl:793
 [6] #invokelatest#1 at ./essentials.jl:794 [inlined]
 [7] #invokelatest at ./none:0 [inlined]
 [8] #run_result#37 at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:32 [inlined]
 [9] #run_result at ./none:0 [inlined]
 [10] #run#39(::Base.Iterators.Pairs{Symbol,Integer,NTuple{5,Symbol},NamedTuple{(:verbose, :samples, :evals, :gctrial, :gcsample),Tuple{Bool,Int64,Int64,Bool,Bool}}}, ::typeof(run), ::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}, ::BenchmarkTools.Parameters) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:46
 [11] #run at ./none:0 [inlined] (repeats 2 times)
 [12] #warmup#42 at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:79 [inlined]
 [13] warmup(::BenchmarkTools.Benchmark{Symbol("##benchmark#448")}) at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:79
 [14] top-level scope at /home/mazzanti/.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:390
 [15] top-level scope at In[93]:1

```

I can do

```julia
CuArray(rand([0.f0,1.f0],10))

```

but that takes more time than generating the numbers on the GPU 😕

Thanks a lot,

Ferran.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [November 10, 2019, 5:51pm UTC](https://discourse.julialang.org/t/random-numbers-in-0-f0-1-f0/30926/2 "2019-11-10T17:51:41Z")

</div>

you can try to do something like this:

> [@GPU randn way slower than rand?](https://discourse.julialang.org/t/gpu-randn-way-slower-than-rand/18236/3):
>
> Hi Tim, Thanks for the reply. I understand that testing once in global is not accurate, it’s just that the difference is too large to be normal. Here the new result following your code ac = Array{Float64}(undef, 2^20) ag = cu(ac) @btime randn!(ac) @btime CuArrays.@sync randn!(ag) 6.788 ms (0 allocations: 0 bytes) 7.626 s (5242883 allocations: 288.00 MiB) So the allocations are clearly the problem, but I can’t really figure out the reason. Even with rand there are some extra allocations @…

but as you noted bits are bytes anyways, so maybe you can just use a `if < 0.5` or something.

---

<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 10, 2019, 9:04pm UTC](https://discourse.julialang.org/t/random-numbers-in-0-f0-1-f0/30926/3 "2019-11-10T21:04:11Z")

</div>

Not all methods of `rand` are implemented for the GPU in CuArrays. If you have a need for a specific method, you can always file an issue (or better yet, have a look at how to implement it or extend the current functionality 🙂).
