SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
RWTRandUniform< Generator > Class Template Reference

Used to generate random numbers from a uniform distribution in an interval [a, b]. More...

#include <rw/rand.h>

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

Public Member Functions

 RWTRandUniform ()
 
 RWTRandUniform (const Generator &g, double a=0, double b=1)
 
 RWTRandUniform (const RWTRandUniform< Generator > &g)
 
 RWTRandUniform (double a, double b)
 
double highBound () const
 
double lowBound () const
 
virtual double operator() ()
 
virtual size_t operator() (double *out, size_t n)
 
void setRange (double a, double b)
 
- Public Member Functions inherited from RWTRand< Generator >
 RWTRand ()
 
 RWTRand (const Generator &g)
 
 RWTRand (const RWTRand< Generator > &x)
 
Generator & generator ()
 
const Generator & generator () const
 
void setGenerator (const Generator &x)
 
void setGenerator (const RWTRand< Generator > &x)
 

Detailed Description

template<class Generator>
class RWTRandUniform< Generator >

Class RWTRandUniform generates random numbers from a uniform distribution in an interval [a, b]. 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 uniform 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 may be used.

Synopsis
#include <rw/rand.h>
Used to generate random numbers from a uniform distribution in an interval [a, b].
Definition rand.h:213
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 uniform generator on [-1, 1] using the above
// generator.
RWTRandUniform<RWRandGenerator> uniform(uniform, -1.0, 1.0);
// Create a uniform generator on [0 100] using class
// RWRandGenerator with a random seed.
RWTRandUniform<RWRandGenerator> uniformOne(0.0, 100.0);
// Create a uniform generator on [3.14, 345.67] using a
// class MyDoubleRand to generate the uniform [0, 1] deviates.
RWTRandUniform<MyDoubleRand> myUniform(3.14, 345.67);
// Print a few values.
for (int j = 0; j < 10; j++) {
cout << "uniform [-1, 1] = " << uniform();
cout << "\nuniform [0, 100] = " << uniformOne();
cout << "\nuniform [3.14, 345.67] = " << myUniform() << endl;
}
// Restart one of the generators with another seed value.
(uniformOne.generator()).restart(654321L);
return 0;
}
Generates random numbers uniformly distributed over the interval [0, 1].
Definition randgen.h:61

Constructor & Destructor Documentation

◆ RWTRandUniform() [1/4]

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

Constructs a generator with default range [0, 1].

◆ RWTRandUniform() [2/4]

template<class Generator >
RWTRandUniform< Generator >::RWTRandUniform ( double a,
double b )

Constructs a generator with range [a, b].

◆ RWTRandUniform() [3/4]

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

Constructs self as a copy of g.

◆ RWTRandUniform() [4/4]

template<class Generator >
RWTRandUniform< Generator >::RWTRandUniform ( const Generator & g,
double a = 0,
double b = 1 )

Constructs a generator with underlying uniform [0, 1] generator g and range [a, b]

Member Function Documentation

◆ highBound()

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

Returns the upper range value.

◆ lowBound()

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

Returns the lower range value.

◆ operator()() [1/2]

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

Returns the next random number in the sequence.

Implements RWRandInterface.

◆ operator()() [2/2]

template<class Generator >
virtual size_t RWTRandUniform< 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.

◆ setRange()

template<class Generator >
void RWTRandUniform< Generator >::setRange ( double a,
double b )

Sets the range to [a, b].

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