TASKSIM(3C++)
NAME
tasksim − histogram and random numbers for the task library
SYNOPSIS
#include <task.h>
struct histogram {
// exported data members
intl, r;// Left and right boundaries of the histogram
intbinsize;// Size of each bin
intnbin;// Number of bins
int∗h;// Pointer to storage
longsum;// Sum of all entries
longsqsum;// Sum of squares of all entries
// exported constructor
histogram(int bins=16, int left=0, int right=16);
// exported functions
voidadd(int val);
voidprint();
};
class randint {
public:
// exported constructor
randint(long seed=0);
// exported functions
intdraw();
floatfdraw();
doubleddraw();
voidseed(long);
};
class urand: public randint {
public:
// exported data members
intlow, high;
// exported constructor
urand(int lo, int hi);
// exported function
intdraw();
};
class erand : public randint {
public:
// exported data members
intmean;
// exported constructor
erand(int m);
// exported function
intdraw();
};
DESCRIPTION
The task library provides classes for gathering data and generating random numbers. These are particularly useful in simulations.
Histograms
histogram hist(bins, left, right);
Constructs an empty histogram object hist. It contains bins number of bins, from hist.h[0] through hist.h[bins-1]. Arguments left and right, which are ints, are assigned to data members l and r, respectively, and are the left and right endpoints of the histogram. The default values are 16 bins covering the range 0 through 16.
hist.add(val)
Adds one to the bin corresponding to value val of the histogram, where val is an int. Updates data members sum and sqsum. If val is outside the range l to r, the range is automatically extended by successive doubling to include it. The number of bins remains the same, however, so each bin will be widened proportionally and the counts adjusted to reflect the extended range.
hist.print()
Prints on stdout (not cout) the count of entries in each non-zero bin of histogram hist.
Random Numbers
The task library provides three classes which generate pseudo-random numbers, with uniform or exponential distribution. Each class object provides an independent series of pseudo-random values.
Class randint provides uniformly-distributed random numbers in the range 0 through INT_MAX.
randint ri(s);
Creates a randint object ri. The long argument is optional, and sets the starting seed of the generator for this object.
int i = ri.draw();
Returns the next uniformly-distributed pseudo-random value in the sequence as an int in the closed interval [0 .. INT_MAX].
float f = ri.fdraw();
Returns the next uniformly-distributed pseudo-random value in the sequence as a float in the half-open interval [0.0 .. 1.0).
double d = ri.ddraw();
Returns the next uniformly-distributed pseudo-random value in the sequence as a double in the half-open interval [0.0 .. 1.0).
ri.seed(s)
Re-initializes the generator using the supplied long value as the seed.
Class urand is derived from randint, and provides uniformly-distributed random numbers in a specified range.
urand ur(lo, hi);
Creates a urand object ur. The long arguments represent the low and high values of the range.
int i = ur.draw();
Returns the next uniformly-distributed pseudo-random value in the sequence as an int in the closed interval [lo .. hi].
Class erand is derived from randint, and provides exponentially-distributed random numbers about a specified mean.
erand er(m);
Creates an erand object er. The int argument is the mean of the distribution.
int i = er.draw();
Returns the next exponentially-distributed pseudo-random value in the sequence as an int about the mean. This function uses the log function from the C math library, so programs must be linked using −lm.
DIAGNOSTICS
See task(3C++).
SEE ALSO
TASK.INTRO(3C++), interrupt(3C++), queue(3C++), task(3C++)
Task Library Tutorial
SunOS WorkShop_4.2 — Last change: 26 October 1992