SourcePro® API Reference Guide

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

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

#include <rw/rand.h>

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

Public Member Functions

 RWTRandNormal ()
 
 RWTRandNormal (double mean, double variance)
 
 RWTRandNormal (const RWTRandNormal< Generator > &g)
 
 RWTRandNormal (const Generator &g, double mean=0, double variance=1)
 
double mean () const
 
virtual double operator() ()
 
virtual size_t operator() (double *out, size_t n)
 
void setMean (double m)
 
void setVariance (double v)
 
double variance () const
 
- 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 RWTRandNormal< Generator >

Class RWTRandNormal generates random numbers from a normal distribution:

\[ f(x) = \frac{1}{\sigma \sqrt{2 \pi}}\text{exp} \begin{bmatrix} -\frac{(x - \mu )^2)}{2 \sigma ^2} \end{bmatrix} , - \infty < \text{x} < \infty \]

where \( \mu\) is the mean, and \( \sigma ^2\) is the variance. 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 normal random numbers may be varied by varying the Generator template parameter, which is 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 a normal generator with mean 75 and variance 100
// using the above uniform generator for generator.
RWTRandNormal<RWRandGenerator> normal(uniform, 75.0, 100.0);
// Create a normal generator with mean 0 and variance 1
// using class RWRandGenerator.
// Create a normal generator with mean -3.14 and variance
// 345.67 using a class MyDoubleRand to generate the
// uniform [0, 1] deviates.
RWTRandNormal<MyDoubleRand> myRandNormal( -3.14, 345.67 );
// Print a few values.
for ( int j = 0; j < 10; j++ )
{
cout << "normal(75,100) = " << normal();
cout << "\normal(0,1) = " << normalZeroOne();
cout << "normal(-3.14,345.67) = " << myRandNormal() << endl;
}
// Restart one of the generators with another seed value
// using the RWRandGenerator method restart().
( normalZeroOne.generator() ).restart( 654321L);
return 0;
}

Constructor & Destructor Documentation

template<class Generator>
RWTRandNormal< Generator >::RWTRandNormal ( )

Constructs a generator with mean 0 and variance 1.

template<class Generator>
RWTRandNormal< Generator >::RWTRandNormal ( double  mean,
double  variance 
)

Constructs a generator with the mean specified by the first parameter and variance specified by the second parameter.

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

Constructs self as a copy of g.

template<class Generator>
RWTRandNormal< Generator >::RWTRandNormal ( const Generator &  g,
double  mean = 0,
double  variance = 1 
)

Constructs a generator with underlying uniform [0, 1] generator g and the specified mean and variance.

Member Function Documentation

template<class Generator>
double RWTRandNormal< Generator >::mean ( ) const
inline

Returns the mean of the distribution.

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

Returns the next random number in the sequence.

Implements RWRandInterface.

template<class Generator>
virtual size_t RWTRandNormal< 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 RWTRandNormal< Generator >::setMean ( double  m)
inline

Sets the mean of the distribution to m.

template<class Generator>
void RWTRandNormal< Generator >::setVariance ( double  v)

Sets the variance of the distribution to v.

template<class Generator>
double RWTRandNormal< Generator >::variance ( ) const
inline

Returns the variance of the distribution.

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