random number generation on each thread? random number generation rng

in my code

i need random number generation function (e.g ranf()) on each thread
for independent sampling on each thread
and i can’t know how many times the code call RNG.

i mean…
i need simple rng function that works on following code

              a=drand() ;
              if(condition1)  b[thread.x]=a;
          }while (condition2)

make a many random number and copy to each thread is impossible in my system.
but many RNG code is just random number generation using gpu.
not a function that works on thread!

no simple rng function in Cuda??
plz help… T.T

you should carefully consider if the approach you described is the only one you can do. the do-while will most probably give you diverging threads, so will the if-clause. also the array b should at least be in shared memory (with the scope of a being exclusive to a single thread); otherwise you’ll get single memory transactions for every single thread.

however: you can easily write your own prng yourself on the gpu if you don’t need extremely long periods. for example you can implement a LCG which will give you good random numbers for little cost.

I’m finishing up a PRNG library for both CPU and GPU computation. I had hoped to have it out in time for Nvision but it’ll probably be in a week or two… documentation takes a while!

The PRNG’s goal is high quality, low register use, and easy threading support.

I’ll post when on the CUDA forums when it’s available.

Here’s what I’ve been doing:

__global__ void buf_init(uint *mem, uint len, uint seed)


        uint idx = (blockIdx.x * blockDim.x) + threadIdx.x;

        uint inc = gridDim.x * blockDim.x;

        uint val;

       val = ((idx + 1) * seed);

        for(; idx < len; idx += inc)


                mem[idx] = val;

                val = (val * seed);




Simple and meets the needs of my projects.