TASKSIM(3C++)
NAME
tasksim − histograms and random numbers for simulations with C++ tasks
SYNOPSIS
#include <task.h>
class histogram {
public:
intl, r;
intbinsize;
intnbin;
int∗h;
longsum;
longsqsum;
histogram(int nb =16, int left =0, int right =16);
voidadd(int bin);
voidprint();
};
class randint {
public:
randint(long seed =0);
intdraw();
floatfdraw();
voidseed(long);
};
class urand : public randint {
public:
intlow, high;
urand(int lo, int hi);
intdraw();
};
class erand : public randint {
public:
intmean;
erand(int m);
intdraw();
};
DESCRIPTION
The C++ task library can be used to program simulations. To support such applications, the library supplies classes to ease data gathering and random number generation.
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:
h a histogram object
ri a randint object
ur a urand object
er a erand object
i, nb, left, right, lo, hi, m
ints
l a long int
f a float
Histograms
Class histogram provides simple facilities to generate histograms.
Class histogram has one form of constructor:
histogram h( nb, left, right );
Constructs a 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 (nbins), 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.
h.add( i )
Adds one to the ith bin. The sum of the integers added is maintained in sum, and the sum of their squares is maintained in 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.
h.print()
Prints the numbers of entries for each non-empty bin in h.
Random Number Generation
Classes 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 );
Constructs a randint object, ri. The argument is optional, and defaults to 0. If l is given, it is used to seed ri.
i = ri.draw()
Returns an random int in the range from 0 to largest_positive_integer. Integers returned by randint::draw() are uniformly distributed in that range.
f = ri.fdraw()
Returns floats that are uniformly distributed in the interval 0 to 1.
ri.seed( l )
Reinitializes a generator with the seed l.
Classes urand and erand are both derived from class randint.
urand ur( lo, hi );
Constructs a urand object, ur. lo and hi define the range from low to high for the distribution of numbers generated by this object.
i = ur.draw()
Returns a random int in the range low to high. Integers returned from urand::draw() will be uniformly distributed in the range.
erand er( i )
Constructs an erand object, er, with i as the mean for the distribution of random numbers generated.
i = er.draw()
Returns a random 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.
DIAGNOSTICS
See task(3C++).
SEE ALSO
TASK.INTRO(3C++), task(3C++), interrupt(3C++), queue(3C++)
Stroustrup, B. and Shopiro, J. E., "A Set of C++ Classes for Co-routine Style Programming," in AT&T C++ Language System Release 2.0 Library Manual.
SunOS Lucid/C++_3.0beta — Last change: C++ Task Library