|
|
#include <task.h>class histogram { public: int l, r; int binsize; int nbin; int* h; long sum; long sqsum; histogram(int nb =16, int left =0, int right =16); void add(int bin); void print(); };
class randint { public: randint(long seed =0); int draw(); float fdraw(); void seed(long); };
class urand : public randint { public: int low, high; urand(int lo, int hi); int draw(); };
class erand : public randint { public: int mean; erand(int m); int draw(); };
The public member functions supplied in the task system classes
histogram
, randint
, urand
, and erand
are listed and described in the next two sections.
The following symbols are used:
histogram
object
randint
object
urand
object
erand
object
int
s
long int
float
histogram
provides simple facilities to generate
histograms.
Class histogram
has one form of constructor:
histogram
h(
nb,
left,
right);
histogram
object, h.
A histogram consists of nbin
bins,
h[0], ...
h[nbin-1]
,
covering a range l to r of integers.
The optional arguments to the histogram
constructor correspond
to the number of bins (nbin
s), and the left (l) and right
(r) ends of the range, respectively.
By default, nb is 16, left is 0, and right is 16,
in other words, there are 16 bins covering a range from 0 to 16.
.add(
i)
sqsum
.
If i is outside the range l-r, the range is extended by
either decreasing l or increasing r.
The number of bins however, remains constant,
so the size of the range covered by a bin is doubled
each time the size of the range is doubled.
.print()
randint
, urand
, and erand
provide basic facilities for generating random numbers,
and can serve as a paradigm for other, application-specific
generators.
Each object of class randint
provides an independent
sequence of random numbers.
Class randint
has one form of constructor:
randint
ri(
l);
randint
object, ri.
The argument is optional, and defaults to 0.
If l is given, it is used to seed ri.
=
ri.draw()
int
in the range from 0 to
largest_positive_integer
.
Integers returned by
randint::draw()
are uniformly distributed in that range.
=
ri.fdraw()
float
s that are uniformly distributed in the interval
0 to 1.
.seed(
l)
Classes urand
and erand
are both derived
from class randint
.
urand
ur(lo, hi)urand
object, ur.
lo and hi define the range from low
to high
for the distribution of numbers generated by this object.
=
ur.draw()
int
in the range low
to high
.
Integers returned from urand::draw()
will be uniformly distributed in the range.
erand
er(
i)
erand
object, er, with i as the
mean
for the distribution of random numbers generated.
=
er.draw()
int
.
Integers returned from erand::draw()
will be exponentially distributed around the mean
.
erand::draw()
uses log()
from the C math library,
so programs using it must be loaded with -lm.
``A set of C++ classes for co-routine style programming,'' by Stroustrup, B. and Shopiro, J. E., in Chapter 2 of the C++ Library Manual.