Created
December 7, 2022 08:54
-
-
Save tzechienchu/26e3ebdfad9084ad33e4758432d6ed98 to your computer and use it in GitHub Desktop.
Generate Poisson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Poisson function -- returns a single Poisson random variable | |
int funPoissonSingle(double lambda) | |
{ | |
double exp_lambda = exp(-lambda); //constant for terminating loop | |
double randUni; //uniform variable | |
double prodUni; //product of uniform variables | |
int randPoisson; //Poisson variable | |
//initialize variables | |
randPoisson = -1; | |
prodUni = 1; | |
do | |
{ | |
randUni = funUniformSingle(); //generate uniform variable | |
prodUni = prodUni * randUni; //update product | |
randPoisson++; //increase Poisson variable | |
} while (prodUni > exp_lambda); | |
return randPoisson; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment