simple cuda rng

A cheap and dirty method for generating uniform random numbers on your Nvidia GPU. Of course they are neither truly uniform nor random, but one rather limited example of a race condition that works for you rather than against 🙂

__global__ void rng_cuda(float * out){

        int t_idx = blockIdx.x * blockDim.x + threadIdx.x;

        __shared__ float values[512];

        values[threadIdx.x] = 0;
        //values[threadIdx.x] += 1;
        values[threadIdx.x] += clock();
        for (int i=0; i<16; ++i) values[threadIdx.x] += values[(threadIdx.x+32+i)%512];
        for (int i=0; i<16; ++i) values[(threadIdx.x+32+i)%512] += values[threadIdx.x];

        out[t_idx] = (cos(values[threadIdx.x])+1.0) / 2.0;



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s