# simple rand() a simple rand function on gpu exist?

**URL:** <https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727>\
**Category:** CUDA Programming and Performance\
**Created:** [May 26, 2009, 8:43pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727 "2009-05-26T20:43:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![helmoz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@helmoz](https://forums.developer.nvidia.com/u/helmoz)\
**Post date:** [May 26, 2009, 8:43pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/1 "2009-05-26T20:43:18Z")

</div>

Hi all,

I’m implementing a simple genetic algorithm on GPU and I need to use a random function. Because it’s not for big scientific purpose, I use the rand() function in my C version of the algorithm.

Is there a way to make a thread generate a random number? :unsure:

I read other post on the forum and it seem complicated. Somebody a hint?

Thanks

---

<div class="post-metadata">

**Author:** ![\_Tom](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_Tom](https://forums.developer.nvidia.com/u/_Tom)\
**Post date:** [May 26, 2009, 9:48pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/2 "2009-05-26T21:48:18Z")

</div>

Hint:

make a global memory buffer full of rand values, exactly matching the number of threads.

Then you supply to each thread this buffer + a single random offset evaluated on the cpu, via kernel parameters.

So you can have a random value for each thread using something like

```auto
uint i = threadIdx.x + blockIdx.x * blockDim.x;

uint randomIdx = (i + randomOffset)%totalLenght;

float random = randomLookup[randomIdxt];

```

I never tried it, but on large number of threads it should return good values.

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [May 27, 2009, 1:52am UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/3 "2009-05-27T01:52:26Z")

</div>

Check mersenne-twister SDK sample for generating random numbers

---

<div class="post-metadata">

**Author:** ![helmoz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@helmoz](https://forums.developer.nvidia.com/u/helmoz)\
**Post date:** [May 27, 2009, 1:13pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/4 "2009-05-27T13:13:05Z")

</div>

Thanks for the answer guys.

I was thinking doing what Tom suggested but I don’t know if it will be efficient. I mean doing copy to the global memory has a cost in time. Do you hink using the mesenne-twister is faster?

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [May 27, 2009, 1:16pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/5 "2009-05-27T13:16:31Z")

</div>

MT SDK sample - looks to be having some problem. I cant vouch. But there is a recent discussion thread from some “King…dude…”. Check that thread out before using that

---

<div class="post-metadata">

**Author:** ![jph4599](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jph4599](https://forums.developer.nvidia.com/u/jph4599)\
**Post date:** [May 27, 2009, 1:27pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/6 "2009-05-27T13:27:14Z")

</div>

Check out: [url=“[http://forums.nvidia.com/index.php?showtopic=97445](http://forums.nvidia.com/index.php?showtopic=97445)”][http://forums.nvidia.com/index.php?showtopic=97445[/url]](http://forums.nvidia.com/index.php?showtopic=97445%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![\_Tom](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_Tom](https://forums.developer.nvidia.com/u/_Tom)\
**Post date:** [May 27, 2009, 6:14pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/7 "2009-05-27T18:14:05Z")

</div>

> [@](#):
>
> Thanks for the answer guys.
> 
> I was thinking doing what Tom suggested but I don’t know if it will be efficient. I mean doing copy to the global memory has a cost in time. Do you hink using the mesenne-twister is faster?

You should do a copy only once, like to perform a “seeding”… then you have to upload a single unsigned int to perform the random offset.

Anyway, provided that the GPU can coalesce the accesses, it should be fast enough?

---

<div class="post-metadata">

**Author:** ![helmoz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@helmoz](https://forums.developer.nvidia.com/u/helmoz)\
**Post date:** [May 29, 2009, 8:32pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/8 "2009-05-29T20:32:34Z")

</div>

Finally I tried the _Tom_ method. But I think I’ll encounter a problem. Indeed, my genetic algorithm require 27 millions of randoms numbers. I don’t think to read 27 millions of value from the global memory is effective. Maybe I should try the mesenne-twister?

---

<div class="post-metadata">

**Author:** ![\_Tom](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_Tom](https://forums.developer.nvidia.com/u/_Tom)\
**Post date:** [May 30, 2009, 7:52am UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/9 "2009-05-30T07:52:08Z")

</div>

> [@](#):
>
> Finally I tried the _Tom_ method. But I think I’ll encounter a problem. Indeed, my genetic algorithm require 27 millions of randoms numbers. I don’t think to read 27 millions of value from the global memory is effective. Maybe I should try the mesenne-twister?

No in fact it is not effective at all :rolleyes:

I thought you had to read it at most once for thread… you could also try with constant memory, but anyway my method was just an idea…

---

<div class="post-metadata">

**Author:** ![helmoz](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@helmoz](https://forums.developer.nvidia.com/u/helmoz)\
**Post date:** [June 1, 2009, 8:48pm UTC](https://forums.developer.nvidia.com/t/simple-rand-a-simple-rand-function-on-gpu-exist/9727/10 "2009-06-01T20:48:15Z")

</div>

I read other thread about generating random value in CUDA kernel before posting there [External Image](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/blarg.gif "Image hosted on another site. Click to open in a new tab.") .

Anyway, I found the rand48 function very easy to use and seem to run fast. Here the code for people who wanna get it, just replace the .txt by .cu.

You can find it on this thread too :  
[url=“[http://forums.nvidia.com/index.php?showtopic=64545&st=0&p=438439&#entry438439](http://forums.nvidia.com/index.php?showtopic=64545&st=0&p=438439&#entry438439)”][http://forums.nvidia.com/index.php?showtop...mp;#entry438439[/url]](http://forums.nvidia.com/index.php?showtop...mp;#entry438439%5B/url%5D)  
[rand48.txt](https://forums.developer.nvidia.com/uploads/short-url/dna5QUOdyIuSEDBDFD5VMzqT4bc.txt) (4.49 KB)
