SourcePro® API Reference Guide

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

Used to generate random numbers from an exponential distribution. More...

#include <rw/rand.h>

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

Public Member Functions

 RWTRandExponential (double lam=1)
 
 RWTRandExponential (const RWTRandExponential< Generator > &g)
 
 RWTRandExponential (const Generator &g, double lam=1)
 
double highBound () const
 
double lambda () const
 
double lowBound () const
 
virtual double operator() ()
 
virtual size_t operator() (double *out, size_t n)
 
void setLambda (double l)
 
- 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 RWTRandExponential< Generator >

Class RWTRandExponential generates random numbers from an exponential distribution:

\[ g(w) = \left\{\begin{matrix} \lambda e^{-\lambda \omega } , & 0 < w \\ 0 \hfill, & w \leq 0 \end{matrix}\right. \]

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 exponentially distributed random numbers may be changed by varying the Generator template parameter responsible for generating these 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 an exponential generator with lambda = 4.5
// using the above uniform generator.
// Create an exponential generator with lambda = 1 using class
// RWRandGenerator.
// Create an exponential generator with lambda 3.14 using
// a class MyDoubleRand to generate the uniform [0, 1] deviates.
// Print a few values.
for ( int j = 0; j < 10; j++ )
{
cout << "exponential lambda = 4.5 = " << exp();
cout << "\nexponential lambda = 1 = " << expOne();
cout << "\nexponential lambda = 3.14 = " << myExp() << endl;
}
// Restart one of the generators with another seed value
// using the RWRandGenerator method restart().
( expOne.generator() ).restart( 654321L);
return 0;
}

Constructor & Destructor Documentation

template<class Generator>
RWTRandExponential< Generator >::RWTRandExponential ( double  lam = 1)

Constructs a generator with lambda = lam.

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

Constructs self as a copy of g.

template<class Generator>
RWTRandExponential< Generator >::RWTRandExponential ( const Generator &  g,
double  lam = 1 
)

Constructs a generator with underlying uniform [0, 1] generator g lambda=lam.

Member Function Documentation

template<class Generator>
double RWTRandExponential< Generator >::highBound ( ) const

Returns the upper bound of 99% of the distribution.

template<class Generator>
double RWTRandExponential< Generator >::lambda ( ) const
inline

Returns the value of lambda for the distribution.

template<class Generator>
double RWTRandExponential< Generator >::lowBound ( ) const

Returns the lower bound of 99% of the distribution.

template<class Generator>
virtual double RWTRandExponential< Generator >::operator() ( )
virtual

Returns the next random number in the sequence.

Implements RWRandInterface.

template<class Generator>
virtual size_t RWTRandExponential< 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>
void RWTRandExponential< Generator >::setLambda ( double  l)
inline

Sets the value of lambda for the distribution to l.

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