NAME
tasksim - histogram and random numbers for the task library
SYNOPSIS
#include <task.h>
struct histogram {
// exported data members
int l, r; // Left and right boundaries of the histogram
int binsize; // Size of each bin
int nbin; // Number of bins
int* h; // Pointer to storage
long sum; // Sum of all entries
long sqsum; // Sum of squares of all entries
// exported constructor
histogram(int bins=16, int left=0, int right=16);
// exported functions
void add(int val);
void print();
};
class randint {
public:
// exported constructor
randint(long seed=0);
// exported functions
int draw();
float fdraw();
double ddraw();
void seed(long);
};
class urand: public randint {
public:
// exported data members
int low, high;
// exported constructor
urand(int lo, int hi);
// exported function
int draw();
};
class erand : public randint {
public:
// exported data members
int mean;
// exported constructor
erand(int m);
// exported function
int draw();
};
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, respec-
tively, 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 distribu-
tion. 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(3CC4).
SEE ALSO
task.intro(3CC4), interrupt(3CC4), queue(3CC4), and
task(3CC4).