Module: Essential Math Group: Statistics classes
RWTRandBinomial<Generator>RWTRand<Generator>
#include <rw/rand.h> RWTRandBinomial<Generator> gen;
Class RWTRandBinomial<Generator> generates random numbers from a binomial distribution:
where n is the number of trials, and p is the probability of success. The algorithm used to generate the random numbers begins with random numbers generated from a uniform distribution on the interval [0, 1]. Thus, the speed and statistical properties of the binomial random number generator may be changed by varying the Generator template parameter responsible for generating the uniformly distributed numbers.
The class Generator must be a function object whose function call operator returns a uniform random double between 0 and 1. The class RWRandGenerator supplied with the library may be used.
#include <rw/rand.h> #include <iostream.h> #include "myrand.h" int main() { // Create a uniform generator on [0, 1] with an // initial seed value. RWRandGenerator uniform(123456L); // Create a binomial generator with number of trials 25 and // probability of success = 0.34 using the above uniform // generator. RWTRandBinomial<RWRandGenerator> binomial(uniform, 25, 0.34); // Create a binomial generator with number of trials = 1 and // probability of success = .5 using class RWRandGenerator. RWTRandBinomial<RWRandGenerator> binomialOne; // Create a binomial generator with number of trials = 31 // and probability of success = .27 using a class // MyDoubleRand to generate the uniform [0, 1] deviates. RWTRandBinomial<MyDoubleRand> myBinomial(31, .27); // Print a few values for ( int j = 0; j < 10; t+j ) { cout << "binomial n=25, p=.34 = " << binomial(); cout << "\nbinomial n=1, p=0.5 = " << binomialOne(); cout << "\nbinomial n=31 and p=.27 = " << myBinomial() << endl; } // Restart one of the generators with another seed value // using the RWRandGenerator method restart() ( binomialOne.generator() ).restart( 654321L); return 0; }
RWTRandBinomial(unsigned n = 1, double p = 0.5);
Constructs a generator with the number of trials = n and the probability of success = p.
RWTRandBinomial(const Generator& g, unsigned n=1,
double p=.5);
Constructs a generator with underlying uniform [0, 1] generator g, number of trials = n, and probability of success = p.
RWTRandBinomial(const RWTRandBinomial<Generator>& g);
Constructs self as a copy of g.
double operator()()
Returns the next random number in the sequence.
size_t operator()(double* out, size_t n);
Assigns random numbers to n elements in out. Returns the number of random numbers generated.
double n() const;
Returns the number of trials for the distribution.
double p() const;
Returns the probability of success for the distribution.
void setN(unsigned n);
Sets the number of trials for the distribution to n.
void setP(double p);
Sets the probability of success for the distribution to p.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.