vendredi 4 septembre 2015

Generate a random number within range that follows Poisson Distribution in C

I am currently using the code below to generate a random number in C that follows the poisson distribution. While this code works, I want to be able to setup a range? For example, I want these values to be between 65 and 95. Specifying a mean of 80 kind of works but there is great possibility of getting number outside my range. The number has to follow the poisson distribution.

int RandPoisson(double mean) {
    double limit = exp(-mean);
    double product = ((double)rand()/INT_MAX);

    int count=0;
    for (; product > limit; count++)
        product *= ((double)rand()/INT_MAX);
    return count;
}

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire