1

I have been looking online for a simple online OpenCL number generator. I can't find anything easy to use.

I want something like the following:

int generateRandomNumber(int fromNumber, int toNumber)
{
    int num = functionOfRandomness();
    return num;
}

If anyone knows an example of how I can get a random number between two values it would be very appreciated. It does not have to be a complicated random system just something that will work simply and quickly

Thanks

bubblebath
  • 939
  • 4
  • 18
  • 45
  • 1
    Answers in http://stackoverflow.com/questions/9912143/how-to-get-a-random-number-in-opencl not good enough for you? – JayC Apr 17 '13 at 15:49
  • I would like something that is already implemented. The Park-Miller one described is Pseudo – bubblebath Apr 17 '13 at 16:38
  • All algorithms to generate random number sequences are pseudorandom. Any *method* to generate cryptographically strong random number sequences generally requires input from already "random" sources. – JayC Apr 17 '13 at 18:58
  • Of course, the quality of a pseudorandom number generator can always be in question, see: http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator – JayC Apr 17 '13 at 19:03
  • 1
    Also related: http://stackoverflow.com/questions/1167253/implementation-of-rand – JayC Apr 17 '13 at 19:08

2 Answers2

3

I ripped this following code from the Real-Time OpenCL Path Tracer at geeks3d. The kernel is in plain text. It's they way I first learned how to use OpenCL.

http://www.geeks3d.com/20120502/laguna-real-time-opencl-path-tracer/

float4 rand(uint2 *state)
{
    const float4 invMaxInt = (float4) (1.0f/4294967296.0f, 1.0f/4294967296.0f, 1.0f/4294967296.0f, 0);
    uint x = (*state).x * 17 + (*state).y * 13123;
    (*state).x = (x<<13) ^ x;
    (*state).y ^= (x<<7);

    uint4 tmp = (uint4)
    ( (x * (x * x * 15731 + 74323) + 871483),
      (x * (x * x * 13734 + 37828) + 234234),
      (x * (x * x * 11687 + 26461) + 137589), 0 );

    return convert_float4(tmp) * invMaxInt;
}
-3

The standard rand() function in C will produce random floating point numbers.

You just need to call this from within your own function and adjust the output by the internal RAND_MAX variable. The following example should produce a random floating-point number within a desired range. You could easily cast the result as an int if that's what you need.

#include<stdlib.h>

float myrand( float setmin, float setmax )
{

    float range = setmax - setmin ; 

    if(RAND_MAX>=range) 
       return ( setmin + rand()/(RAND_MAX/range) ) 

    else
        return ( setmin + rand()/(range/RAND_MAX) ) 

}
JWDN
  • 382
  • 3
  • 13
  • warning: implicit declaration of function 'rand' is invalid in C99 – bubblebath Apr 17 '13 at 18:59
  • You cannot use standard C library functions within a OpenCl method. The code above could be useful for other reasons, though. – JayC Apr 17 '13 at 19:38
  • If not rand() is not an option, one of George Marsaglia's algorithms (e.g. [multiply-with-carry](http://en.wikipedia.org/wiki/Multiply-with-carry)) should suffice, and the simple algorithm above will give you output in the desired range. Of course you need to establish what "RAND_MAX" would be for whichever method you use. – JWDN Apr 18 '13 at 08:32