Skip to content

Instantly share code, notes, and snippets.

@tzechienchu
Created December 7, 2022 08:54
Show Gist options
  • Save tzechienchu/26e3ebdfad9084ad33e4758432d6ed98 to your computer and use it in GitHub Desktop.
Save tzechienchu/26e3ebdfad9084ad33e4758432d6ed98 to your computer and use it in GitHub Desktop.
Generate Poisson
//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