Package org.uncommons.maths.random
Class BinomialGenerator
- java.lang.Object
-
- org.uncommons.maths.random.BinomialGenerator
-
- All Implemented Interfaces:
NumberGenerator<java.lang.Integer>
public class BinomialGenerator extends java.lang.Object implements NumberGenerator<java.lang.Integer>
Discrete random sequence that follows a binomial distribution.
-
-
Field Summary
Fields Modifier and Type Field Description private double
lastP
private NumberGenerator<java.lang.Integer>
n
private NumberGenerator<java.lang.Double>
p
private BitString
pBits
private java.util.Random
rng
-
Constructor Summary
Constructors Constructor Description BinomialGenerator(int n, double p, java.util.Random rng)
Creates a generator of binomially-distributed values from a distribution with the specified parameters.BinomialGenerator(NumberGenerator<java.lang.Integer> n, NumberGenerator<java.lang.Double> p, java.util.Random rng)
Creates a generator of binomially-distributed values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
binomialWithEvenProbability(int n)
Generating binomial values when p = 0.5 is straightforward.java.lang.Integer
nextValue()
Generate the next binomial value from the current values of n and p.
-
-
-
Field Detail
-
rng
private final java.util.Random rng
-
n
private final NumberGenerator<java.lang.Integer> n
-
p
private final NumberGenerator<java.lang.Double> p
-
pBits
private transient BitString pBits
-
lastP
private transient double lastP
-
-
Constructor Detail
-
BinomialGenerator
public BinomialGenerator(NumberGenerator<java.lang.Integer> n, NumberGenerator<java.lang.Double> p, java.util.Random rng)
Creates a generator of binomially-distributed values. The number of trials (n) and the probability of success in each trial (p) are determined by the provided
NumberGenerator
s. This means that the statistical parameters of this generator may change over time. One example of where this is useful is if the n and p generators are attached to GUI controls that allow a user to tweak the parameters while a program is running.To create a Binomial generator with a constant n and p, use the
BinomialGenerator(int, double, Random)
constructor instead.- Parameters:
n
- ANumberGenerator
that provides the number of trials for the Binomial distribution used for the next generated value. This generator must produce only positive values.p
- ANumberGenerator
that provides the probability of succes in a single trial for the Binomial distribution used for the next generated value. This generator must produce only values in the range 0 - 1.rng
- The source of randomness.
-
BinomialGenerator
public BinomialGenerator(int n, double p, java.util.Random rng)
Creates a generator of binomially-distributed values from a distribution with the specified parameters.- Parameters:
n
- The number of trials (and therefore the maximum possible value returned by this sequence).p
- The probability (between 0 and 1) of success in any one trial.rng
- The source of randomness used to generate the binomial values.
-
-
Method Detail
-
nextValue
public java.lang.Integer nextValue()
Generate the next binomial value from the current values of n and p. The algorithm used is from The Art of Computer Programming Volume 2 (Seminumerical Algorithms) by Donald Knuth (page 589 in the Third Edition) where it is credited to J.H. Ahrens.- Specified by:
nextValue
in interfaceNumberGenerator<java.lang.Integer>
- Returns:
- The next value from the generator.
-
binomialWithEvenProbability
private int binomialWithEvenProbability(int n)
Generating binomial values when p = 0.5 is straightforward. It simply a case of generating n random bits and counting how many are 1s.- Parameters:
n
- The number of trials.- Returns:
- The number of successful outcomes from n trials.
-
-