RandomSample
Generates random numbers following a negative binomial distribution.
You're viewing an older version of this page (#228). View the current version.
Interface
#include <codecogs/statistics/distributions/discrete/negativebinomial/randomsample.h>
using namespace Statistics::Distributions::Discrete::Negativebinomial;
Overview
The negative binomial distribution, also known as the Pascal distribution or Pólya distribution, gives the probability of successes and
failures in
trials, and success on the
th trial. The probability density function is therefore given by
The distribution function is then given by :
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 negative binomial behaviour of this non-uniform random number generator.

Below you will find 20 numbers corresponding to the output of the first generator :
12 10 12 13 12 7 7 7 7 13 7 7 11 3 11 12 15 7 8 10
Speed:
The average running time for generating 100,000,000 random numbers using this class on a 750MHz microprocessor is 72 seconds.
References
- MathWorld, http://mathworld.wolfram.com/NegativeBinomialDistribution.html
- The Newran03 random number generator library of Robert Davies, http://www.robertnz.net/nr03doc.htm
Example 1
The following example displays 40 random floating point numbers from a negative binomial 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 with the MERSENNEDIV value in order to keep the seed in the (0, 1) interval. 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,
#include <iostream>
#include <time.h>
#include <codecogs/stats/dists/discrete/negativebinomial/randomsample.h>
using namespace std;
int main()
{
Stats::Dists::Discrete::NegativeBinomial::RandomSample A(100, 0.1, 0.275);
Stats::Dists::Discrete::NegativeBinomial::RandomSample B(114, 0.7, time(0) / MERSENNEDIV);
for (int i = 0; i < 20; ++i)
cout << A.genReal() << " ";
cout << endl;
for (int i = 0; i < 20; ++i)
cout << B.genReal() << " ";
cout << endl;
return 0;
}Members of RandomSample
CLASS METHOD
RandomSample
Class constructor.
CLASS METHOD
RandomSample
Class destructor.
CLASS METHOD
genReal
Generates a random deviate from the negative binomial distribution.
CLASS METHOD
RandomSample
Constructor that sets up the class variables and initializes the associated random number generator with the given seed.
CLASS METHOD