rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWTRandBinomial< Generator > Class Template Reference
[Statistics]

Used to generate random numbers from a binomial distribution. More...

#include <rw/rand.h>

Inheritance diagram for RWTRandBinomial< Generator >:
RWTRand< Generator > RWRandInterface

List of all members.

Public Member Functions

 RWTRandBinomial (size_t n=1, double p=0.5)
 RWTRandBinomial (const RWTRandBinomial< Generator > &g)
 RWTRandBinomial (const Generator &g, size_t n=1, double p=0.5)
virtual double operator() ()
virtual size_t operator() (double *out, size_t n)
int n () const
double p () const
void setN (size_t n)
void setP (double p)

Detailed Description

template<class Generator>
class RWTRandBinomial< Generator >

Class RWTRandBinomial<Generator> generates random numbers from a binomial distribution:

\[ \begin{array}{l l l} f(x) & = \binom{n}{x} p^x(1-p)^{n-x}, & \text{x = 0, 1, ..., n} \\ & = \text{0} \hfill, & \text{elsewhere} \end{array} \]

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.

Synopsis

 #include <rw/rand.h>
 RWTRandBinomial<Generator> gen;

Examples

 #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;
 }

Constructor & Destructor Documentation

template<class Generator>
RWTRandBinomial< Generator >::RWTRandBinomial ( size_t  n = 1,
double  p = 0.5 
)

Constructs a generator with the number of trials = n and the probability of success = p.

template<class Generator>
RWTRandBinomial< Generator >::RWTRandBinomial ( const RWTRandBinomial< Generator > &  g  ) 

Constructs self as a copy of g.

template<class Generator>
RWTRandBinomial< Generator >::RWTRandBinomial ( const Generator &  g,
size_t  n = 1,
double  p = 0.5 
)

Constructs a generator with underlying uniform [0, 1] generator g, number of trials = n, and probability of success = p.


Member Function Documentation

template<class Generator>
int RWTRandBinomial< Generator >::n (  )  const [inline]

Returns the number of trials for the distribution.

template<class Generator>
virtual size_t RWTRandBinomial< Generator >::operator() ( double *  out,
size_t  n 
) [virtual]

Assigns random numbers to n elements in out. Returns the number of random numbers generated.

Implements RWRandInterface.

template<class Generator>
virtual double RWTRandBinomial< Generator >::operator() (  )  [virtual]

Returns the next random number in the sequence.

Implements RWRandInterface.

template<class Generator>
double RWTRandBinomial< Generator >::p (  )  const [inline]

Returns the probability of success for the distribution.

template<class Generator>
void RWTRandBinomial< Generator >::setN ( size_t  n  ) 

Sets the number of trials for the distribution to n.

template<class Generator>
void RWTRandBinomial< Generator >::setP ( double  p  ) 

Sets the probability of success for the distribution to p.

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© 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.