The procedures and functions described in this chapter can be found in ~ns/tools/rng.{cc, h}, ~ns/tools/random.{cc, h}, ~ns/tools/ranvar.{cc, h}, ~ns/tools/pareto.{cc, h}, ~ns/tools/expoo.{cc, h}, ~ns/tools/integrator.{cc, h}, and ~ns/tcl/lib/nsrandom.tcl.

The procedures and functions described in this chapter can be found in ~ns/tools/rng.{cc, h}, ~ns/tools/random.{cc, h}, ~ns/tools/ranvar.{cc, h}, ~ns/tools/pareto.{cc, h}, ~ns/tools/expoo.{cc, h}, ~ns/tools/integrator.{cc, h}, and ~ns/tcl/lib/nsrandom.tcl.

Revision as of 19:49, 23 July 2008

The procedures and functions described in this chapter can be found in ~ns/tools/rng.{cc, h}, ~ns/tools/random.{cc, h}, ~ns/tools/ranvar.{cc, h}, ~ns/tools/pareto.{cc, h}, ~ns/tools/expoo.{cc, h}, ~ns/tools/integrator.{cc, h}, and ~ns/tcl/lib/nsrandom.tcl.

Contents

Random Number Generation

The RNG class contains an implementation of the combined multiple recursive generator MRG32k3a proposed by L’Ecuyer [16]. The C++ code was adapted from [18]. This replaces the previous implementation of RNG, which used the minimal standard multiplicative linear congruential generator of Park and Miller [27]. The newer (MRG32k3a) RNG is used in ns versions 2.1b9 and later.

The MRG32k3a generator provides 1.8x1019 independent streams of random numbers, each of which consists of 2.3x1015 substreams. Each substream has a period (i.e., the number of random numbers before overlap) of 7.6x1022. The period of the entire generator is 3.1x1057. Figure 25.1 provides a graphical idea of how the streams and substreams fit together.

A default RNG (defaultRNG), created at simulator initialization time, is provided. If multiple random variables are used in a simulation, each random variable should use a separate RNG object. When a new RNG object is created, it is automatically seeded to the beginning of the next independent stream of random numbers. Used in this manner, the implementation allows for a maximum of 1.8x1019 random variables.

Often, multiple independent replications of a simulation are needed (i.e., to perform statistical analysis given multiple runs with fixed parameters). For each replication, a different substream should be used to ensure that the random number streams are independent. (This process is given as an OTcl example later.) This implementation allows for a maximum of 2.3x1015 independent replications. Each random variable in a single replication can produce up to 7.6x1022 random numbers before overlapping.

Note: Only the most common functions are described here. For more information, see [18] and the source code found in tools/rng.h and tools/rng.cc. For a comparison of this RNG to the more common LCG16807 RNG (and why LCG16807 is not a good RNG), see [17].

Seeding The RNG

Due to the nature of the RNG and its implementation, it is not necessary to set a seed (the default is 12345). If you wish to change the seed, functions are available. You should only set the seed of the default RNG. Any other RNGs you create are automatically seeded such that they produce independent streams. The range of valid seeds is 1 to MAXINT.

To get non-deterministic behavior, set the seed of the default RNG to 0. This will set the seed based on the current time of day and a counter. This method should not be used to set seeds for independent replications. There is no guarantee that the streams produced by two random seeds will not overlap. The only way to guarantee that two streams do not overlap is to use the substream capability provided by the RNG implementation.

Classes derived from this abstract class implement specific distributions. Each distribution is parameterized with the values of appropriate parameters. The value method is used to return a value from the distribution.

The currently defined distributions, and their associated parameters are:

This class provides a base class used by other classes such as QueueMonitor that keep running sums. Each new element of the running sum is added by the newPoint(x, y) function. After the kth execution of newPoint, the running sum is equal to
<math>\sum_{i=1}^k y_{i−1}(x_i − x_{i−1})</math> where x0 = y0 = 0 unless lastx_, lasty_, or sum_ are reset via OTcl. Note that a new point in the sum can be added either by the C++ member newPoint or the OTcl member newpoint. The use of integrals to compute certain types of averages (e.g. mean queue lengths) is given in (pp. 429–430, [15]).

ns-random

ns-random is an obsolete way to generate random numbers. This information is provided only for backward compatibility.

ns-random is implemented in ~ns/misc.{cc,h}. When called with no argument, it generates a random number with uniform distribution between 0 and MAXINT. When an integer argument is provided, it seeds the random generator with the given number. A special case is when ns-random 0 is called, it randomly seeds the generator based on current time. This feature is useful to produce non-deterministic results across runs.

Some mathematical-support related objects

INTEGRATOR OBJECTS: Integrator Objects support the approximate computation of continuous integrals using discrete sums.
The running sum(integral) is computed as: sum_ += [lasty_ * (x lastx_)] where (x, y) is the last element entered
and (lastx_, lasty_) was the element previous to that added to the sum. lastx_ and lasty_ are updated as new elements are
added. The first sample point defaults to (0,0) that can be changed by changing the values of (lastx_,lasty_). $integrator
newpoint <x> <y>

Add the point (x,y) to the sum. Note that it does not make sense for x to be less than lastx_.

There are no configuration parameters specific to this object.

State Variables are:

lastx_ x-coordinate of the last sample point.

lasty_ y-coordinate of the last sample point.

sum_ Running sum (i.e. the integral) of the sample points.

SAMPLES OBJECT: Samples Objects support the computation of mean and variance statistics for a given sample.

$samples mean

Returns mean of the sample.

$samples variance

Returns variance of the sample.

$samples cnt

Returns a count of the sample points considered.

$samples reset

Reset the Samples object to monitor a fresh set of samples.

There are no configuration parameters or state variables specific to this object.

Commands at a glance

Following is a list of mathematical support related commands commonly used in simulation scripts:

set rng [new RNG]

This creates a new random number generator.

$rng seed <0 or n>

This command seeds the RNG. If 0 is specified, the RNG is seeded heuristically. Otherwise the RNG is seeded with the value <n>.

$rng next-random

This returns the next random number from RNG.

$rng uniform <a>

This returns a number uniformly distributed on <a> and <b>.

$rng integer <k>

This returns an integer uniformly distributed on 0 and k-1.

$rng exponential

This returns a number that has exponential distribution with average 1.

set rv [new Randomvariable/<type of random-variable>]

This creates an instance of a random variable object that generates random variables with specific distribution. The different types of random variables derived from the base class are:

RandomVariable/Uniform, RandomVariable/Exponential, RandomVariable/Pareto, RandomVariable/Constant,
RandomVariable/HyperExponential. Each of these distribution types are parameterized with values of appropriate parameters. For details see section Random Variables above.

$rv use-rng <rng>

This method is used to associated a random variable object with a non-default RNG. Otherwise by default, the random variable object is associated with the default random number generator.