Thread: newbie - poisson numbers

  1. #1
    Registered User
    Join Date
    Oct 2008

    newbie - poisson numbers

    Hi everyone,

    Ive been trying to create a function to generate random poisson numbers. Here is the Knuth algorithm from wikipedia

    algorithm poisson random number (Knuth):
    Let L ← e−λ, k ← 0 and p ← 1.
    k ← k + 1.
    Generate uniform random number u in [0,1] and let p ← p × u.
    while p ≥ L.
    return k − 1.

    I coded this like this:

     unsigned long PoissonRandomNumber(double landa){
    double l=exp(-landa);
    unsigned long k=0;
    double p=1;
    while (p>=l)
    double u= (double)rand() / RAND_MAX;
    return k-1;

    But i only get 0s from using this. Any help would be greatly greatly appreciated! Actually, any help on any code to generate poisson random numbers would be huge help to me.

    EDIT: I also tried it like this code I found on the web:

    double p=exp(-landa);
    double g=p;
    double u= (double)rand() / RAND_MAX;
    unsigned long k=0;
    while (u>g)
    return k;
    This just gives me the same number everytime when i put in a particular landa.
    Last edited by fireflym; 10-27-2008 at 10:14 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Aha master5001... so much for your latex poll.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Maybe ((double)rand()) -- make sure the rand gets casted before the division, otherwise u is always 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting numbers in arrays
    By rachael033 in forum C++ Programming
    Replies: 10
    Last Post: 05-29-2007, 02:56 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM