GPU based random number generator

We make use of a GPU with OpenCL support in order to generate random numbers.

It works as “Global memory is consistent across work-items in a single work-group at a work-group barrier, but there are no guarantees of memory consistency between different work-groups executing a kernel.”, also different work-groups don’t necessarily execute instructions at precisely the same time.

The kernel which is executed on multiple processing elements:

__kernel void rng(__global int* data) {
    int z =0;
    for(z = 0; z<10091; z++)
        data[0] = data[0] ^ 1;

All kernel instances try to read and write to the same memory element. We simply toggle the value
of the same element in a global array, by making use of xor.

If you play with this, I strongly suggest you play around with the following values (along with choosing the number
of iterations in the above for loop):

const size_t N = 1024*20;
const size_t groupsize = 4;

The output from ‘ent’ which provides information on how random
the data in a file is:

Entropy = 7.988515 bits per byte.

Optimum compression would reduce the size
of this 32632 byte file by 0 percent.

Chi square distribution for 32632 samples is 522.98, and randomly
would exceed this value less than 0.01 percent of the times.

Arithmetic mean value of data bytes is 127.8626 (127.5 = random).
Monte Carlo value for Pi is 3.168076499 (error 0.84 percent).
Serial correlation coefficient is -0.012086 (totally uncorrelated = 0.0).

Interesting notes:

The following generates more randomness:

const size_t N = ARRAY_SIZE*200;
const size_t groupsize = ARRAY_SIZE*10;


const size_t N = ARRAY_SIZE*10;
const size_t groupsize = ARRAY_SIZE*200;

This is because N roughly relates to the amount of compute units which are used. We’ve found the greater the value
of N, the more random the output is. N should be much greater than groupsize.

Get the code

(Please don’t use this RNG for anything important)

I’m currently looking into making it more efficient, to generate more bits / second, and am then
planning to subject it to the ‘dieharder’ program’s statistical tests

Leave Comment

five + 1 =

Error Please check your entries!