RandomSample
Generates random numbers following a Poisson distribution.
Interface
#include <codecogs/statistics/distributions/discrete/poisson/randomsample.h>
using namespace Statistics::Distributions::Discrete::Poisson;
RandomSample(double mu, double seed = 0.8476) : Stats::Random::Mersenne(seed)~RandomSample()int genInt()void set_mu(double mu);double Density(double) const;void Build1(double);void Build2(double);void Destruct1()void Destruct2()void RandomSample::set_mu(double mu)void RandomSample::Build1(double mux)void RandomSample::Build2(double mux)int RandomSample::Gen1()double RandomSample::Density(double x) constint sample(double mu, double seed=0.123)
Overview
The Poisson distribution is used to model the number of events occurring within a given time interval. The formula for the Poisson probability mass function, which we also use with this random number generator is
where is the shape parameter indicating the average number of events in the given time interval.
Using this class, the diagram below is generated from two distinct sequences of 1000 random numbers. Each pair of numbers are plotted against each other, to illustrate the Poisson behaviour of this non-uniform random number generator.

The following example displays 40 random floating point numbers from a Poisson distribution. It uses two different generators to achieve this. The first generator uses a particular value to initialize the seed, while the second one is using the system timer. Notice that it was necessary to divide the timer by the <em> MERSENNEDIV </em> value in order to keep the seed in the interval from 0 to 1. Since the seed of the first generator is never changed, the first 20 numbers will always remain the same. However since the second generator is initialized via the system timer, the next 20 numbers will obviously vary with each execution of the program,
In the example output you will find 20 numbers corresponding to the output of the first generator.
Speed
The average running time for generating 100,000,000 random numbers using this class on a 750MHz microprocessor is 56 seconds.
Example 1
#include <iostream>
#include <time.h>
#include <codecogs/statistics/distributions/discrete/poisson/randomsample.h>
int main()
{
Stats::Dists::Discrete::Poisson::RandomSample A(53.29, 0.15);
Stats::Dists::Discrete::Poisson::RandomSample B(61.47, time(0) / MERSENNEDIV);
for (int i = 0; i < 20; ++i)
std::cout << A.genInt() << " ";
std::cout << std::endl << std::endl;
for (int i = 0; i < 20; ++i)
std::cout << B.genInt() << " ";
std::cout << std::endl;
return 0;
}Output:
51 60 51 68 50 50 45 61 58 49 55 38 48 58 52 60 52 48 48 48References
- NIST/SEMATECH e-Handbook of Statistical Methods, http://www.itl.nist.gov/div898/handbook/
- The Newran03 random number generator library of Robert Davies, http://www.robertnz.net/nr03doc.htm
Members of RandomSample
CLASS METHOD
RandomSample
Constructor that sets up the class variables and initializes the associated random number generator with the given seed.
Parameters
CLASS METHOD
RandomSample
CLASS METHOD
genInt
CLASS METHOD
set_mu
CLASS METHOD
Density
CLASS METHOD
Build1
CLASS METHOD
Build2
CLASS METHOD
Destruct1
CLASS METHOD
Destruct2
CLASS METHOD
set_mu
CLASS METHOD
Build1
CLASS METHOD
Build2
CLASS METHOD
Gen1
CLASS METHOD
Density
CLASS METHOD
sample
This function is a simple wrapper around the randomsample class provided in this module. It uses a static to keep a single instance of this class, so that each call to this function returns a new random number. As a result this function is not necessarily thread safe, in the sense that with identical initial seed, the sequence of random numbers may differ on a multitasking or multi threaded system.
The seed is only set on the first call to this function. Thereafter this parameter is ignored. If you do no want to set the seed, then we suggest you use the system clock the first time you call this function, i.e.
#include <time.h>
...
sample(mu, time(0) / MERSENNEDIV);If you require more advance behaviour, we strongly recommend using the underlying class randomsample (above).