1

I am attempting to build a particle system utilizing CUDA to do the heavy lifting. I want to randomize some of the particles' initial values like velocity and life span. The random numbers don't have to be super random since it's just for visual effect. I found this post that addresses the same subject:

Random Number Generation in CUDA

That suggests a linear congruential is the way to go. It seems like it should be simple to implement, but I am having trouble getting anything useful of my implementation. Can anyone provide some code that will run in the device?

I am using CUDA with VC++ on Windows 7 64bit.

Community
  • 1
  • 1
Mr Bell
  • 9,228
  • 18
  • 84
  • 134
  • And what exactly is the problem? Linear Congruential Generator code is Q = (Q*a)+b, Q being the random number in question. The constants a and b vary between implementations - you can find some popular combinations at http://en.wikipedia.org/wiki/Linear_congruential_generator – mingos Feb 04 '10 at 19:33
  • It's considered blatantly offensive to ask for source. A nicer trick is to post something (even the most rudimentary, if you will) you've whipped up yourselves to get people interested! And, always, explain, why, where and which part you are having trouble implementing. – dirkgently Feb 04 '10 at 19:33
  • Dirkgently: Why is asking for code (especially code that probably amounts to a dozen lines are less) blatantly offensive? – Mr Bell Feb 04 '10 at 19:44
  • Mingos: The problem is I need random numbers generated in device and I dont know how to generate them. Are linear congruential's the way to go? Is there something better? If linear congruential is the way, how is implemented in CUDA? – Mr Bell Feb 04 '10 at 19:45
  • Oh, so the requirement is that it's calculated in the GPU? OK, my bad. LCG are the fastest, but low quality random numbers. If at all possible, I'd use another fast algorithm, but with a higher period and random number quality, like Complementary Multiply With Carry, Generalised Feedback Shift Register or perhaps Mersenne Twister. I don't know any implementations for CUDA though, sorry... – mingos Feb 04 '10 at 20:19

2 Answers2

4

CUDA pseudo random number generators are

  1. included in the NVidia SDK eg C/src/MersenneTwister/ and C/src/quasirandomGenerator

  2. available as separate papers and source:

    2.a Langdon's paper and Langdon's source code

    2.b Mersenne Twister on GPU

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
3

Depending on your requirements there are a number of open source options. There are also several commercial options such as NAG who have implemented l'Ecuyer's MRG32k3a. Be wary of using an LCG if you need to ensure that your streams are not correlated - you can use leapfrog/splitting but you'll need a very long period!

If you want to go open source then you should definitely consider using thrust for it's simplicity. There are also some RNGs in the NVIDIA SDK, including the Mersenne Twister PRNG sample (MT607, MT19937 is on the forums) and the Sobol and Niederreiter QRNGs.

Finally, CUDPP also has a random number generator.

Tom
  • 20,852
  • 4
  • 42
  • 54