# What's a good random number generator?

**URL:** <https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322>\
**Category:** CUDA Programming and Performance\
**Created:** [April 11, 2008, 7:38pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322 "2008-04-11T19:38:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![molecube](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@molecube](https://forums.developer.nvidia.com/u/molecube)\
**Post date:** [April 11, 2008, 7:38pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/1 "2008-04-11T19:38:34Z")

</div>

Since rand() cannot be used in CUDA, what is a good random number generator that provides uniform distribution over a certain range of numbers that I could use?

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [April 11, 2008, 7:50pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/2 "2008-04-11T19:50:22Z")

</div>

There’s a Mersenne Twister example in the SDK. That’s a good quality generator, though I’m not sure if the example shows how to use the results in device code.

---

<div class="post-metadata">

**Author:** ![jordyvaneijk](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jordyvaneijk](https://forums.developer.nvidia.com/u/jordyvaneijk)\
**Post date:** [April 14, 2008, 1:48pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/3 "2008-04-14T13:48:04Z")

</div>

> [@](#):
>
> Since rand() cannot be used in CUDA, what is a good random number generator that provides uniform distribution over a certain range of numbers that I could use?
> 
> [snapback]360671[/snapback]

I think that if you do a search on the forum you can find a lot of them. there has been a discussion about it a while ago. Can’t find it that quick but there is something…

Check it out.

---

<div class="post-metadata">

**Author:** ![x248](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@x248](https://forums.developer.nvidia.com/u/x248)\
**Post date:** [April 14, 2008, 2:15pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/4 "2008-04-14T14:15:54Z")

</div>

> [@](#):
>
> I think that if you do a search on the forum you can find a lot of them. there has been a discussion about it a while ago. Can’t find it that quick but there is something…
> 
> Check it out.
> 
> [snapback]362083[/snapback]

The mersenne twister19937 is the best, but if you want something simple

and possible without double:

take the Wichmann/Hill (the period is 10^12).

There are 3 componants (integer) in the “diffusion”.

x(0)=100,y(0)=20,z(0)=2500

x(i)=171\*x(i-1) mod 30269

y(i)=172\*y(i-1) mod 30307

z(i)=170\*z(i-1) mod 30323

and u(i)=((x(i)/30269) + (y(i)/30307) + (z(i)/30323)) mod 1

u(i) is an uniform in [0,1]

---

<div class="post-metadata">

**Author:** ![molecube](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@molecube](https://forums.developer.nvidia.com/u/molecube)\
**Post date:** [April 15, 2008, 11:09pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/5 "2008-04-15T23:09:02Z")

</div>

> [@](#):
>
> The mersenne twister19937 is the best, but if you want something simple
> 
> and possible without double:
> 
> take the Wichmann/Hill (the period is 10^12).
> 
> There are 3 componants (integer) in the “diffusion”.
> 
> x(0)=100,y(0)=20,z(0)=2500
> 
> x(i)=171\*x(i-1) mod 30269
> 
> y(i)=172\*y(i-1) mod 30307
> 
> z(i)=170\*z(i-1) mod 30323
> 
> and u(i)=((x(i)/30269)&nbsp; + (y(i)/30307)&nbsp; + (z(i)/30323))&nbsp; mod 1
> 
> u(i) is an uniform in [0,1]
> 
> [snapback]362089[/snapback]

Thanks, I suppose the above could be put into a kernel function that can be run and used within device code? And would I basically increment ‘i’ every time I want a random number?

---

<div class="post-metadata">

**Author:** ![DenisR](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@DenisR](https://forums.developer.nvidia.com/u/DenisR)\
**Post date:** [April 16, 2008, 5:26am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/6 "2008-04-16T05:26:26Z")

</div>

> [@](#):
>
> Thanks, I suppose the above could be put into a kernel function that can be run and used within device code? And would I basically increment ‘i’ every time I want a random number?
> 
> [snapback]362860[/snapback]

You would not have any i in your code I think, I would think it looks something like this:

```auto
unsigned int x=100,y=20,z=2500;

repeat this part for every uniform randnr you want:

x *=171;

x%=30269;

y*=172;

y%=30307;

z*=170;

z%= 30323;

float u=((((float)x)/30269.0)  + (((float)y)/30307.0)  + (((float)z)/30323.0)) % 1.0;

```

But you have to make sure each thread gets its own set of initial x,y,z values, otherwise all threads

---

<div class="post-metadata">

**Author:** ![molecube](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@molecube](https://forums.developer.nvidia.com/u/molecube)\
**Post date:** [April 16, 2008, 6:36am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/7 "2008-04-16T06:36:53Z")

</div>

> [@](#):
>
> You would not have any i in your code I think, I would think it looks something like this:
> 
> ```auto
> unsigned int x=100,y=20,z=2500;
> 
> repeat this part for every uniform randnr you want:
> 
> x *=171;
> 
> x%=30269;
> 
> y*=172;
> 
> y%=30307;
> 
> z*=170;
> 
> z%= 30323;
> 
> float u=((((float)x)/30269.0)  + (((float)y)/30307.0)  + (((float)z)/30323.0)) % 1.0;
> 
> ```
> 
> But you have to make sure each thread gets its own set of initial x,y,z values, otherwise all threads
> 
> [snapback]362994[/snapback]

Thanks for the clarification. So each thread would have the same initial values (int x=100,y=20,z=2500)? Otherwise…what?

---

<div class="post-metadata">

**Author:** ![jlehtone](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jlehtone](https://forums.developer.nvidia.com/u/jlehtone)\
**Post date:** [April 16, 2008, 12:27pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/8 "2008-04-16T12:27:53Z")

</div>

> [@](#):
>
> So each thread would have the same initial values (int x=100,y=20,z=2500)? Otherwise…what?

I think DenisR said: _each thread must have different initial values_.

If each thread starts with the same (x,y,z), then they all use the same serie of numbers. Not very random.

If the (x,y,z) is in global memory and the update of (x,y,z) is atomic in each call to “device\_rand()” from threads, then threads get different values as they should. But atomic update of three values forces serial behaviour.

With “local rand”, if the period is 10^12 and you have N threads, then logically – to remain unique – each thread would start with local (x,y,z) that is 10^12/N steps forward from the start of the previous thread.

---

<div class="post-metadata">

**Author:** ![zakim](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zakim](https://forums.developer.nvidia.com/u/zakim)\
**Post date:** [September 8, 2008, 6:58am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/9 "2008-09-08T06:58:31Z")

</div>

Thanks for the good info.

I’d just like to add that Wichmann has a new 4-sequence algorithm that is better than the 3-sequence mentioned here and the paper mentions parallelism.

The paper’s title is Generating good pseudo-random numbers 2006.  
[url=“[http://portal.acm.org/citation.cfm?id=1219278](http://portal.acm.org/citation.cfm?id=1219278)”][http://portal.acm.org/citation.cfm?id=1219278[/url]](http://portal.acm.org/citation.cfm?id=1219278%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [September 8, 2008, 4:18pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/10 "2008-09-08T16:18:54Z")

</div>

> [@](#):
>
> Thanks for the good info.
> 
> I’d just like to add that Wichmann has a new 4-sequence algorithm that is better than the 3-sequence mentioned here and the paper mentions parallelism.
> 
> The paper’s title is Generating good pseudo-random numbers 2006.
> 
> http://portal.acm.org/citation.cfm?id=1219278
> 
> [snapback]436952[/snapback]

Can you please post a code snippet? I can’t access the paper.

Also, does anyone know a simple generator that only needs one variable? I’d like to put it into smem. I have half a mind to use randu(), but there must exist something as simple but a bit better. I honestly don’t need something good.

---

<div class="post-metadata">

**Author:** ![zakim](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zakim](https://forums.developer.nvidia.com/u/zakim)\
**Post date:** [September 8, 2008, 10:00pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/11 "2008-09-08T22:00:26Z")

</div>

> [@](#):
>
> Can you please post a code snippet? I can’t access the paper.
> 
> Also, does anyone know a simple generator that only needs one variable? I’d like to put it into smem. I have half a mind to use randu(), but there must exist something as simple but a bit better. I honestly don’t need something good.
> 
> [snapback]437094[/snapback]

I consider this to be a simple generator with good properties that uses far less state variables as compared to Mersenne Twister.

Here is the 32-bit version

```auto
     // Assuming the width of integers are 32-bits

      ix = 11600 * ( ix % 185127 ) - 10379 * ( ix / 185127 );

      iy = 47003 * ( iy % 45688 ) - 10479 * ( iy / 45688 );

      iz = 23000 * ( iz % 93368 ) - 19423 * ( iz / 93368 );

      it = 33000 * ( it %  65075 ) - 8123 *  ( it / 65075 );

      

      if ( ix < 0 ) ix += 2147483579;

      if ( iy < 0 ) iy += 2147483543;

      if ( iz < 0 ) iz += 2147483423;

      if ( it < 0 )  it  += 2147483123;

      

      float W = ( ((float) ix)/2147483579.0 + ((float) iy)/ 2147483543.0

                              + ((float) iz)/2147483423.0 + ((float) it)/2147483123.0 );

       

      return fmod(W, 1.0);

```

---

<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:** [September 9, 2008, 8:23am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/12 "2008-09-09T08:23:02Z")

</div>

> [@](#):
>
> There’s a Mersenne Twister example in the SDK.&nbsp; That’s a good quality generator, though I’m not sure if the example shows how to use the results in device code.
> 
> [snapback]360679[/snapback]

Monte-carlo simulation example makes use of the MT generator - i believe (not yet checked… but dats what I used for my MC)

You can always launch a kernel to generate the random numbers in global memory required for your simulation (could be quite huge)

and

then launch your kernel that uses the random numbers (you can index them sequentialy…)

---

<div class="post-metadata">

**Author:** ![Reimar](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Reimar](https://forums.developer.nvidia.com/u/Reimar)\
**Post date:** [September 9, 2008, 8:37am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/13 "2008-09-09T08:37:46Z")

</div>

> [@](#):
>
> Since rand() cannot be used in CUDA, what is a good random number generator that provides uniform distribution over a certain range of numbers that I could use?
> 
> [snapback]360671[/snapback]

If you originally wanted to use rand() why do you insist on having a good one to replace it?

Or to say it differently: If rand() is good enough for you, any of the previous ones are sever overkill.

See http://en.wikipedia.org/wiki/Linear\_congruential\_generator for the trivial ones that you get when using rand().

“man rand” also lists the one suggested by POSIX.

Either way, don’t forget to check that the random number generator you use is actually suitable (and also that it is not overkill) for what you do.

---

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [September 9, 2008, 10:23pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/14 "2008-09-09T22:23:24Z")

</div>

> [@](#):
>
> If you originally wanted to use rand() why do you insist on having a good one to replace it?
> 
> Or to say it differently: If rand() is good enough for you, any of the previous ones are sever overkill.
> 
> See http://en.wikipedia.org/wiki/Linear\_congruential\_generator for the trivial ones that you get when using rand().
> 
> “man rand” also lists the one suggested by POSIX.
> 
> Either way, don’t forget to check that the random number generator you use is actually suitable (and also that it is not overkill) for what you do.
> 
> [snapback]437372[/snapback]

Thanks. A linear congruence generator is exactly what I needed.

---

<div class="post-metadata">

**Author:** ![kbarros](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kbarros](https://forums.developer.nvidia.com/u/kbarros)\
**Post date:** [September 11, 2008, 3:01pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/15 "2008-09-11T15:01:36Z")

</div>

Linear congruential generators are already implemented in CUDA

See for example  
[url=“[http://www.amolf.nl/~vanmeel/mdgpu/download.html](http://www.amolf.nl/~vanmeel/mdgpu/download.html)”][http://www.amolf.nl/~vanmeel/mdgpu/download.html[/url]](http://www.amolf.nl/~vanmeel/mdgpu/download.html%5B/url%5D)

Note that the generated random sequence is exactly that of the rand48 algorithm provided by the C standard library, but the implementation has been parallelized in a clever way.

I’ve modified this implementation so that random numbers can be generated, one at a time, within a CUDA device function. The code is attached.

---

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [September 11, 2008, 5:55pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/16 "2008-09-11T17:55:16Z")

</div>

> [@](#):
>
> Linear congruential generators are already implemented in CUDA
> 
> See for example
> 
> http://www.amolf.nl/~vanmeel/mdgpu/download.html
> 
> Note that the generated random sequence is exactly that of the rand48 algorithm provided by the C standard library, but the implementation has been parallelized in a clever way.
> 
> I’ve modified this implementation so that random numbers can be generated, one at a time, within a CUDA device function. The code is attached.
> 
> [snapback]438439[/snapback]

Thanks. The defintion of ‘uint2’ is missing. P.S. is it ok to call cudaFree() right after a kernel call? Will cudaFree block until the kernel’s finished? Just wondering (I know you’d normally have a memcpy anyway).

---

<div class="post-metadata">

**Author:** ![kbarros](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kbarros](https://forums.developer.nvidia.com/u/kbarros)\
**Post date:** [September 11, 2008, 7:51pm UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/17 "2008-09-11T19:51:32Z")

</div>

> [@](#):
>
> Thanks. The defintion of ‘uint2’ is missing. P.S. is it ok to call cudaFree() right after a kernel call? Will cudaFree block until the kernel’s finished? Just wondering (I know you’d normally have a memcpy anyway).
> 
> [snapback]438502[/snapback]

The intention is that the file “rand48.cu” is included into your file “MyCudaCode.cu”, so that the functions it defines are accessible. The nvcc compiler has uint2 as a built in type.

The CUDA documentation does not list cudaFree as asynchronous (section 4.5.1.5 in my copy), so I expect that it blocks.

Kip

---

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [September 12, 2008, 2:21am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/18 "2008-09-12T02:21:55Z")

</div>

> [@](#):
>
> The intention is that the file “rand48.cu” is included into your file “MyCudaCode.cu”, so that the functions it defines are accessible. The nvcc compiler has uint2 as a built in type.
> 
> The CUDA documentation does not list cudaFree as asynchronous (section 4.5.1.5 in my copy), so I expect that it blocks.
> 
> Kip
> 
> [snapback]438559[/snapback]

oh ok, thanks

---

<div class="post-metadata">

**Author:** ![zakim](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zakim](https://forums.developer.nvidia.com/u/zakim)\
**Post date:** [September 12, 2008, 6:29am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/19 "2008-09-12T06:29:19Z")

</div>

The one I posted is an excellence combination of 4 Linear congruential generators. The great thing is that it is still accurate after having each thread having its own seed.

And the best thing is that I doesn’t need to write to global mem, just registers.

MT’s algorithm is complicated.

And yes, I was using it for a CUDAfied poisson generator that has a different mean parameter for every 3D point in the data

---

<div class="post-metadata">

**Author:** ![Simon\_Green](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Simon\_Green](https://forums.developer.nvidia.com/u/Simon_Green)\
**Post date:** [November 23, 2008, 12:17am UTC](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322/20 "2008-11-23T00:17:10Z")

</div>

This code is very useful, thanks for posting.

There’s a small typo at the end:

**device** inline int rand48\_nextFloat(Rand48 &r);

should be:

**device** inline float rand48\_nextFloat(Rand48 &r);

[Next page](https://forums.developer.nvidia.com/t/whats-a-good-random-number-generator/3322.md?page=2)
