DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
C++ Task Library

tasksim(C++)


tasksim -- histograms and random numbers for simulations with C++ tasks

Syntax

#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(); };

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:

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 a 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(C++).

See also

task.intro(C++), task(C++), interrupt(C++), queue(C++)

``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.


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005