# Gpu\_rand inside @cuda

**URL:** https://discourse.julialang.org/t/gpu-rand-inside-cuda/50322
**Category:** GPU
**Created:** [November 17, 2020, 7:39pm UTC](https://discourse.julialang.org/t/gpu-rand-inside-cuda/50322 "2020-11-17T19:39:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dgatey](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dgatey](https://discourse.julialang.org/u/dgatey)
#### Post date: [November 17, 2020, 7:39pm UTC](https://discourse.julialang.org/t/gpu-rand-inside-cuda/50322/1 "2020-11-17T19:39:31Z")

</div>

Hey Everyone,  
I’ve managed to use gpu\_rand via gpu\_call but was wondering if it is possible to use gpu\_rand via @cuda as I’m currently using a 2D block.  
The @cuda function call only accepts bit types so I’m struggling to determine how to pick up the KernelContext and randstate required for gpu\_rand.  
Best

---

<div class="post-metadata">

### Author: ![dgatey](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@dgatey](https://discourse.julialang.org/u/dgatey)
#### Post date: [November 18, 2020, 9:24am UTC](https://discourse.julialang.org/t/gpu-rand-inside-cuda/50322/2 "2020-11-18T09:24:31Z")

</div>

Basically I’ve been working on replicating this - [sampling a Categorical on the GPU](https://tianjun.me/essays/Categorical_Sampling_on_GPU_with_Julia)  
Note: global\_rng → default\_rng now

The implementation is slightly different as I have a 2D array where each element is the number of samples required and each row in the array has a different weight function. To implement this I’m using a 2D block. For the time being I’ve implemented one of the previously posted `xorshift` impementations but given it requires two random numbers per sample being able to work with RNG is beneficial for incrementing the seed and ultimately repeatability.

---

<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, 10:41am UTC](https://discourse.julialang.org/t/gpu-rand-inside-cuda/50322/3 "2020-11-18T10:41:14Z")

</div>

Have you find a method to generate a random number in GPU kernel function? I met the same problem with you. All `rand` like function will returns a CuArray. But memory allocation is not allowed in kernel function.

So, how to get a pure function which is not wrapped in a CuArray?
