#include #include #include #include /************************************************************** * This program generates 100 random variables following * a Poisson distribution with a parameter LAMBDA (that is, * LAMBDA events per unit time). * * To approximate this, we use an exponential distribution * with parameter LAMBDA to simulate the time intervals * between events. The first event is scheduled to start * at time t = 0. * * Remember to compile with the -lm option. **************************************************************/ #define LAMBDA 0.7 #define N 100 int main() { double event[N]; int i; // Seed the random number generator by using the current // time. If you want to recreate the random sequence, // use a fixed number other than 0. srand48( time( NULL ) ); event[0] = 0.0; for ( i = 1; i < N; ++i ) { double x; // drand48() returns a random variable x in [0, 1) // To avoid taking the logarithm of 0 (NaN), we // calculate 1 - x which gives a random variable // in (0, 1]. x = drand48(); event[i] = event[i - 1] - log( 1 - x )/LAMBDA; } // This prints the values in the format // a, b, c, d, ..., x, y, z printf( "%f", event[0] ); for ( i = 1; i < N; ++i ) { printf( ", %f", event[i] ); } printf( "\n" ); return 0; }