I have a question regarding the random generators in CUDA . I am using 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 have tried to relaunch this code many times, I have always the same output. I could not find what’s wrong in this code. The threads give the same Ids but the curand_normal functions should change at each launching, right?
You're running init each time you ask for a random value. Instead you should run curand_init()
once, in a separate kernel at the start of your code. Then when you want a new random value, just call curand_normal()
. Then the values will change each time you call your device function.
For an example see my answer here.
If you want to use time as a seed instead of thread ID, then pass the value returned by clock()
or whatever is your favorite time function:
unsigned int seed = (unsigned int) clock64();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With