title: Generating random number in cuda kernel between 0 and 1
body: <p>I have a cuda application where I want to generate random numbers between 0 and 1. I have written a dummy code where a matrix of size 8x256 would be filled up by random numbers generated by kernel. My original matrix would be something like 8XBIG_NUMBER. But probably I am missing something in my code because of which I am not able to produce the desired result.I am posting my code below.</p>
  
  <pre><code>void main(int argc,char* argv[])    
  {
      float *test_var,*dev_test;
      curandState *state;
  
      test_var = (float *)malloc(8*256*sizeof(float));
      memset(test_var,0,8*256*sizeof(float));
  
      cudaMalloc((void **)&amp;dev_test,8*256*sizeof(float));
      cudaMemcpy(dev_test,test_var,8*256*sizeof(float),cudaMemcpyHostToDevice);
      dim3 gridDim(1,256/32,1);
      dim3 blockDim(8,32,1);
      cudaMalloc((void **)&amp;state,8*256*sizeof(curandState));
      setup_kernel&lt;&lt;&lt;gridDim,blockDim&gt;&gt;&gt;(state,unsigned(time(NULL)));
      test_kernel&lt;&lt;&lt;gridDim,blockDim&gt;&gt;&gt;(state,dev_test);
      cudaMemcpy(test_var,dev_test,8*256*sizeof(float),cudaMemcpyDeviceToHost);
      system("PAUSE");
  
      for (int i=0;i&lt;256;i++)
      {   for (int j=0;j&lt;8;j++)
          { printf("%f\t",test_var[i*8+j]);
          }
          printf("\n");
      }
  
      cudaFree(dev_test);
      cudaFree(state);
      free(test_var);
      exit(0);
  }
  
  __global__ void setup_kernel(curandState *state,unsigned long seed)
  {
      int id_col  = threadIdx.x + blockDim.x*blockIdx.x;
      int id_row = threadIdx.y+blockDim.y*blockIdx.y;
  
      curand_init(seed,(id_row*8+id_col),0,&amp;state[id_row*8+id_col]);
  }
  
  __global__ void test_kernel(curandState *state,float *dev_test)
  {
      int id_col  = threadIdx.x + blockDim.x*blockIdx.x;
      int id_row = threadIdx.y+blockDim.y*blockIdx.y;
  
       curandState local_state = state[id_row*8+id_col];
       dev_test[id_row*8+id_col] = curand(&amp;local_state);   
       state[id_row*8+id_col] = local_state;
  }
  </code></pre>
  
  <p>I want to generate a random number between 0 and 1 for each of those cells in the matrix. I would really appreciate of anyone's assistance. Thank you</p>