# How to generate a random number in CUDA kernel function

**URL:** <https://discourse.julialang.org/t/how-to-generate-a-random-number-in-cuda-kernel-function/50364>\
**Category:** GPU\
**Tags:** question\
**Created:** [November 18, 2020, 9:14am UTC](https://discourse.julialang.org/t/how-to-generate-a-random-number-in-cuda-kernel-function/50364 "2020-11-18T09:14:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [November 18, 2020, 9:14am UTC](https://discourse.julialang.org/t/how-to-generate-a-random-number-in-cuda-kernel-function/50364/1 "2020-11-18T09:14:14Z")

</div>

All the rand like function in CUDA.jl will get a CuArray. But memory allocation is not allowed in a kernel function. Thus, a CuArray can not be generated. So, are there any method to get a pure random number instead of an Array.

---

<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 18, 2020, 1:04pm UTC](https://discourse.julialang.org/t/how-to-generate-a-random-number-in-cuda-kernel-function/50364/2 "2020-11-18T13:04:00Z")

</div>

Host `rand` calls don’t work on the GPU. We also don’t support the CURAND device API for now. See [Generating Random Number from inside Kernel](https://discourse.julialang.org/t/generating-random-number-from-inside-kernel/8071) for possible workarounds.

IIRC @tkf also had a solution, maybe he can elaborate (Slack has eaten the history).

---

<div class="post-metadata">

**Author:** ![Ellipse0934](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellipse0934/32/4012_2.png) [@Ellipse0934](https://discourse.julialang.org/u/Ellipse0934)\
**Post date:** [November 18, 2020, 6:52pm UTC](https://discourse.julialang.org/t/how-to-generate-a-random-number-in-cuda-kernel-function/50364/3 "2020-11-18T18:52:54Z")

</div>

tkf’s implementation here [Monte-Carlo π · FoldsCUDA](https://juliafolds.github.io/FoldsCUDA.jl/dev/examples/monte_carlo_pi/)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [November 18, 2020, 7:21pm UTC](https://discourse.julialang.org/t/how-to-generate-a-random-number-in-cuda-kernel-function/50364/4 "2020-11-18T19:21:56Z")

</div>

I suggested Julia would use this:  
[https://sunoru.github.io/RandomNumbers.jl/stable/man/xorshifts/](https://sunoru.github.io/RandomNumbers.jl/stable/man/xorshifts/)

> Xoroshiro128, Xoroshiro128Star and Xoroshiro128Plus: The successor to Xorshift128 series. They make use of a carefully handcrafted shift/rotate-based linear transformation, resulting in a significant improvement in speed and in statistical quality. Therefore, Xoroshiro128Plus is the current best suggestion for replacing other low-quality generators.

I’ve not looked into issues with GPUs, maybe this works as is.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [November 20, 2020, 1:11am UTC](https://discourse.julialang.org/t/how-to-generate-a-random-number-in-cuda-kernel-function/50364/5 "2020-11-20T01:11:56Z")

</div>

FYI here’s a follow-up comment on the FoldsCUDA.jl link: [Advice for improving Monte-Carlo code - #25 by tkf](https://discourse.julialang.org/t/advice-for-improving-monte-carlo-code/49782/25)
