SourcePro® API Reference Guide

 
List of all members | Public Member Functions
RWTRandBinomial< Generator > Class Template Reference

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

#include <rw/rand.h>

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

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)
 
int n () const
 
virtual double operator() ()
 
virtual size_t operator() (double *out, size_t n)
 
double p () const
 
void setN (size_t n)
 
void setP (double p)
 
- Public Member Functions inherited from RWTRand< Generator >
 RWTRand ()
 
 RWTRand (const Generator &g)
 
 RWTRand (const RWTRand< Generator > &x)
 
const Generator & generator () const
 
Generator & generator ()
 
void setGenerator (const Generator &x)
 
void setGenerator (const RWTRand< Generator > &x)
 

Detailed Description

template<class Generator>
class RWTRandBinomial< Generator >

Class RWTRandBinomial 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>
Example
#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.
// 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 double RWTRandBinomial< Generator >::operator() ( )
virtual

Returns the next random number in the sequence.

Implements RWRandInterface.

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

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.