Random number generator on GPU

Practical and theoretical implementation discussion.
shiqiu1105
Posts: 138
Joined: Sun May 27, 2012 4:42 pm

Random number generator on GPU

I am currenlty writing my own GPU tracer.

And I now need random number, or quasi random number for sampling.

But how to get random number with CUDA? What do people usually do?

Generate a bunch of random numbers on the CPU and pass it to kernel? Or use some library to generate numbers in the kernel directly?
Or implement their own RNG in the kernel??

keldor314
Posts: 10
Joined: Tue Jan 10, 2012 6:56 pm

Re: Random number generator on GPU

Just write your own. This is the one I use:

Code: Select all

__constant__ unsigned int shift1[4] = {6, 2, 13, 3};
__constant__ unsigned int shift2[4] = {13, 27, 21, 12};
__constant__ unsigned int shift3[4] = {18, 2, 7, 13};
__constant__ unsigned int offset[4] = {4294967294, 4294967288, 4294967280, 4294967168};
__shared__ unsigned int randStates[32];

__device__ unsigned int TausStep(unsigned int &z)
{
int index = threadIdx.x&3;
unsigned int b = (((z << shift1[index]) ^ z) >> shift2[index]);
return z = (((z & offset[index]) << shift3[index]) ^ b);
}

__device__ unsigned int randInt()
{
}