# curand - generating discrete random variables from a histogram

Hi,

I need to generate discrete random variables from a numerically specified distribution function (histogram) on the device. I can see two options:

1. On the device: have an array which holds cumulative probabilities, draw a uniform variable from curand, do a first occurrence search for this uniform variable in the array (potentially slow as the search will likely create divergence problems)

2. Utilise undocumented API in curand: Create a curandDiscreteDistribution_t histogram on the host (how?), from the device use the api curand_discrete to generate a random variable. Clean up with curandDestroyDistribution

Clearly 2) is the superior approach and Nvidia have undoubtedly put lots of thought into making curand_discrete efficient.

The relevant help documentation is below:

``````curandStatus_t
curandCreatePoissonDistribution(
double lambda,
curandDiscreteDistribution_t *discrete_distribution)
The curandCreatePoissonDistribution() function is used to create a histogram for the
Poisson distribution with the given lambda.

__device__ unsigned int
curand_discrete (
curandState_t *state,
curandDiscreteDistribution_t discrete_distribution)
This function returns a single discrete distributed unsigned int based on a distribution for
the given discrete distribution histogram.

curandStatus_t
curandDestroyDistribution(
curandDiscreteDistribution_t discrete_distribution)
``````

Is the source code for curandCreatePoissonDistribution available somewhere? This would help me understand how to create my own curandDiscreteDistribution_t *discrete_distribution for my own histogram.

Thanks!

hmm wont a histogram force a potentially infinite distribution into a finite one?
Numerical Recipes in C gives ways of converting uniform random distributions
to a variety of distributions. I am sure it includes Gaussian and so perhaps
also has Poisson.
I have a CUDA implementation of Park-Miller’s minimum PRNG
http://www.cs.ucl.ac.uk/staff/ucacbbl/ftp/gp-code/random-numbers/

Bill

No, I have to explicitly implement a custom discrete probability mass function here, it cannot be approximated.

It looks like someone had a go at this before https://devtalk.nvidia.com/default/topic/838082/generating-sample-from-a-non-uniform-discrete-distribution-/ but did not manage to create their own histogram using the api.

Anyone from Nvidia on the forum who can shed some light on how to create a curandDiscreteDistribution_t ?

If you want to see something documented, I suggest filing a RFE.

Thanks, will do.