Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random generator & CUDA

Tags:

random

cuda

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?

like image 625
ALFRAM Avatar asked Oct 21 '22 15:10

ALFRAM


1 Answers

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();
like image 65
Robert Crovella Avatar answered Nov 03 '22 09:11

Robert Crovella