title: Generate random number within a function with cuRAND without preallocation
body: <p>Is it possible to generate random numbers within a device function without preallocate all the states? I would like to generate and use them in "realtime". I need to use them for Monte Carlo simulations what are the most suitable for this purpose? The number generated below are single precision is it possible to have them in double precision?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "cuda_runtime.h"
  #include "device_launch_parameters.h"
  #include &lt;curand_kernel.h&gt;
  
  __global__ void cudaRand(float *d_out, unsigned long seed)
  {
      int i = blockDim.x * blockIdx.x + threadIdx.x;
      curandState state;
      curand_init(seed, i, 0, &amp;state);
      d_out[i] = curand_uniform(&amp;state);
  }
  
  int main(int argc, char** argv)
  {
      size_t N = 1 &lt;&lt; 4;
      float *v = new float[N];
  
      float *d_out;
      cudaMalloc((void**)&amp;d_out, N * sizeof(float));
  
      // generate random numbers
      cudaRand &lt;&lt; &lt; 1, N &gt;&gt; &gt; (d_out, time(NULL));
  
      cudaMemcpy(v, d_out, N * sizeof(float), cudaMemcpyDeviceToHost);
  
      for (size_t i = 0; i &lt; N; i++)
      {
          printf("out: %f\n", v[i]);
      }
  
      cudaFree(d_out);
      delete[] v;
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>UPDATE</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "cuda_runtime.h"
  #include "device_launch_parameters.h"
  #include &lt;curand_kernel.h&gt;
  #include &lt;ctime&gt;
  
  __global__ void cudaRand(double *d_out)
  {
      int i = blockDim.x * blockIdx.x + threadIdx.x;
      curandState state;
      curand_init((unsigned long long)clock() + i, 0, 0, &amp;state);
  
      d_out[i] = curand_uniform_double(&amp;state);
  }
  
  int main(int argc, char** argv)
  {
      size_t N = 1 &lt;&lt; 4;
      double *h_v = new double[N];
  
      double *d_out;
      cudaMalloc((void**)&amp;d_out, N * sizeof(double));
  
      // generate random numbers
      cudaRand &lt;&lt; &lt; 1, N &gt;&gt; &gt; (d_out);
  
      cudaMemcpy(h_v, d_out, N * sizeof(double), cudaMemcpyDeviceToHost);
  
      for (size_t i = 0; i &lt; N; i++)
          printf("out: %f\n", h_v[i]);
  
      cudaFree(d_out);
      delete[] h_v;
  
      return 0;
  }
  </code></pre>