clProbDist
An OpenCL library for probability distributions
DocsTutorial/example2.c

Complete code for Example 2: non-uniform variate generation

/*
* Authors:
*
* David Munger <mungerd@iro.umontreal.ca> (2015)
* Pierre L'Ecuyer <lecuyer@iro.umontreal.ca> (2015)
*
*/
#include <clRNG/mrg31k3p.h>
cl_double simulateOneRun(cl_double lambda, clrngMrg31k3pStream* stream)
{
cl_double output = 0.0;
for (int i = 0; i < 100; i++) {
cl_double u = clrngMrg31k3pRandomU01(stream);
cl_int poisson_variate = clprobdistPoissonInverseCDF(lambda, u, NULL);
// modify output using poisson_variate
output += poisson_variate / (100.0 * clprobdistPoissonMean(lambda, NULL));
}
return output;
}
int main()
{
// prepare
int replications = 1024;
cl_double lambda = 50.0;
clrngMrg31k3pStream* stream = clrngMrg31k3pCreateStreams(NULL, 1, NULL, NULL);
// simulate
cl_double sum = 0.0;
for (int i = 0; i < replications; i++)
sum += simulateOneRun(lambda, stream);
printf("The average output is %.3f\n", sum / replications);
// clean up
clrngMrg31k3pDestroyStreams(stream);
return 0;
}