# HG changeset patch
# User Alpar Juttner <alpar@cs.elte.hu>
# Date 1205781687 0
# Node ID f857981306ea15db2c67a5e1264ae326a6fab840
# Parent  e28fc773f3c0e08fe5a51c8af7e01b8b70aaa957# Parent  5d4decd1b8707fd93e0243a1d5b703219592815e
Merge

diff -r e28fc773f3c0 -r f857981306ea lemon/random.h
--- a/lemon/random.h	Mon Mar 17 18:31:52 2008 +0000
+++ b/lemon/random.h	Mon Mar 17 19:21:27 2008 +0000
@@ -803,6 +803,29 @@
       return exponential(gamma(k,1.0/x_min));
     }  
       
+    /// Poisson distribution
+
+    /// This function generates a Poisson distribution random number with
+    /// parameter \c lambda.
+    /// 
+    /// The probability mass function of this distribusion is
+    /// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
+    /// \note The algorithm is taken from the book of Donald E. Knuth titled
+    /// ''Seminumerical Algorithms'' (1969). Its running time is linear in the
+    /// return value.
+    
+    int poisson(double lambda)
+    {
+      const double l = std::exp(-lambda);
+      int k=0;
+      double p = 1.0;
+      do {
+	k++;
+	p*=real<double>();
+      } while (p>=l);
+      return k-1;
+    }  
+      
     ///@}
     
     ///\name Two dimensional distributions
diff -r e28fc773f3c0 -r f857981306ea test/random_test.cc
--- a/test/random_test.cc	Mon Mar 17 18:31:52 2008 +0000
+++ b/test/random_test.cc	Mon Mar 17 19:21:27 2008 +0000
@@ -33,4 +33,5 @@
   a=lemon::rnd.gamma(4);
   //Does gamma work with integer k?
   a=lemon::rnd.gamma(4.0,0);
+  a=lemon::rnd.poisson(.5);
 }