Taken from the wikipedia article which credits Knuth (and notes that there are implementations with better time performance) this code is a complete program implementing a minimal test scaffold. The code that does the actual work is 169 characters still formatted:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <string.h>
/* For serious work you need a high quality random number generator
*
* And these macros may need to be adjusted for your system and choice
* of random number engine.
*/
#define GOODRAND random()
#define GOODRANDMAX INT_MAX
#define RANDTYPE long
/* poisson.c
*
* Implementation straight from
* http://en.wikipedia.org/wiki/Poisson_distribution#Generating_Poisson-distributed_random_variables
* which credits Knuth.
*
* Time complexity is O(lambda), which is not optimal.
*/
RANDTYPE poisson(double lambda){
RANDTYPE k=0;
double L=exp(-lambda), p=1;
do {
++k;
p *= GOODRAND/(double)GOODRANDMAX;
} while (p > L);
return --k;
}
/* minimal testing scaffold
*
* Note that with no initialzation the sequece will repeat on every test.
*/
int main(int argc, char**argv){
while (argc > 1) {
RANDTYPE in = atoi(argv[--argc]);
printf("lambda=%d: %d\n",in,poisson(in));
}
}