simple rand() a simple rand function on gpu exist?

Hi all,

I’m implementing a simple genetic algorithm on GPU and I need to use a random function. Because it’s not for big scientific purpose, I use the rand() function in my C version of the algorithm.

Is there a way to make a thread generate a random number? :unsure:

I read other post on the forum and it seem complicated. Somebody a hint?

Thanks

Hint:

make a global memory buffer full of rand values, exactly matching the number of threads.

Then you supply to each thread this buffer + a single random offset evaluated on the cpu, via kernel parameters.

So you can have a random value for each thread using something like

uint i = threadIdx.x + blockIdx.x * blockDim.x;

uint randomIdx = (i + randomOffset)%totalLenght;

float random = randomLookup[randomIdxt ];

I never tried it, but on large number of threads it should return good values.

Check mersenne-twister SDK sample for generating random numbers

Thanks for the answer guys.

I was thinking doing what Tom suggested but I don’t know if it will be efficient. I mean doing copy to the global memory has a cost in time. Do you hink using the mesenne-twister is faster?

MT SDK sample - looks to be having some problem. I cant vouch. But there is a recent discussion thread from some “King…dude…”. Check that thread out before using that

Check out: [url=“http://forums.nvidia.com/index.php?showtopic=97445”]http://forums.nvidia.com/index.php?showtopic=97445[/url]

You should do a copy only once, like to perform a “seeding”… then you have to upload a single unsigned int to perform the random offset.

Anyway, provided that the GPU can coalesce the accesses, it should be fast enough?

Finally I tried the Tom method. But I think I’ll encounter a problem. Indeed, my genetic algorithm require 27 millions of randoms numbers. I don’t think to read 27 millions of value from the global memory is effective. Maybe I should try the mesenne-twister?

No in fact it is not effective at all :rolleyes:

I thought you had to read it at most once for thread… you could also try with constant memory, but anyway my method was just an idea…

I read other thread about generating random value in CUDA kernel before posting there External Image .

Anyway, I found the rand48 function very easy to use and seem to run fast. Here the code for people who wanna get it, just replace the .txt by .cu.

You can find it on this thread too :
[url=“http://forums.nvidia.com/index.php?showtopic=64545&st=0&p=438439&#entry438439”]http://forums.nvidia.com/index.php?showtop...mp;#entry438439[/url]
rand48.txt (4.49 KB)