RNG in jocl

Hi! :slight_smile:

Is there a way to perform RNG in pure opencl. Most programs like smallptGPU that perform monte-carlo simulation call c++ RNG function.

The closest I can get from the web is this http://www.doc.ic.ac.uk/~dt10/research/rngs-gpu-mwc64x.html - Marco, I think this should be a good add-on for your jocl examples.:wink:

I haven’t tried it but it seems not suitable for monte-carlo simulation which needs random seeds to jump start the random number generator to become purely random.

Thank you.

Hello

One approach could be to have a look at the NVIDIA Mersenne Twister example, and possibly port it to JOCL. (I tried that for the CUDA/JCuda MersenneTwister - it’s not available at the website, but I don’t remember whether I ran into problems or simply did not have enough time).

I can’t access the source code from the website, so I don’t know whether it would be easy to port this. In any case, it could be a nice example (but I don’t know when I find the time for this…)

I’m not sure in how far the initial random seeds could be passed in from Java - of course, these are not “high-quality random” (in a cryptographic sense) but might be suitable for a Raytracer…

bye
Marco

Thanks for the link to the Nvidia site. I’ll check on both the MT and the QuasiRandom. For the source code in the link also doesn’t work for me but the examples given are enough to get started anyway.

Thank you.

I borrowed a trick I learned developing for AVR microcontrollers when I needed a simple RNG for a kernel, perhaps someone will find it useful:

// define masks for shift register for convenience
// 32 bits
#define bit30 0x40000000
#define bit27 0x08000000

// needs a seed for at least the first execution
// after the first execution we should use the previous value of the accumulator (not shown here)
uint accumulator = seed;

// implement the shift register
accumulator = accumulator << 1 ;
// & with bit 30 for 'linear feedback shift register
int bit0 = (accumulator & bit27)==0?0:1;
// & with bit 27
int bit1 = (accumulator & bit30)==0?0:1;
accumulator = accumulator + (bit0 ^ bit1) ;
//convert to float with range 0.0 to 0.999…
float rdm = as_float((accumulator & 0x7FFFFF) | 0x3F800000) - 1.0f;

Though it may not seem like it would be at first, the spectrum of this RNG is very white. I would not use this approach where strong random numbers are needed.