Random Generator and CUDA

I have a question about random generators in CUDA. I use Curand to generate random numbers with the following code:

__device__ float priceValue(int threadid){ unsigned int seed = threadid ; curandState s; curand_init (seed , 0, 0, &s); float randomWalk = 2; while (abs(randomWalk)> 1) { randomWalk = curand_normal(&s); } return randomWalk; } 

I tried to repeat this code many times, I always have the same output. I could not find what was wrong in this code. Threads give the same identifiers, but curand_normal functions should change every time they start, right?

+4
source share
1 answer

You run init every time you request a random value. Instead, you should run curand_init() once, in a separate kernel at the beginning of your code. Then, when you want to get a new random value, just call curand_normal() . Then the values ​​will change every time you call the function of your device.

See my answer here for an example.

If you want to use time as a seed instead of a stream identifier, then pass the value returned by clock() , or something else your favorite time function:

 unsigned int seed = (unsigned int) clock64(); 
+3
source

All Articles