A Discrete-Event Network Simulator
API
random-variable-stream.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Georgia Tech Research Corporation
3 * Copyright (c) 2011 Mathieu Lacage
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors: Rajib Bhattacharjea<raj.b@gatech.edu>
19 * Hadi Arbabi<marbabi@cs.odu.edu>
20 * Mathieu Lacage <mathieu.lacage@gmail.com>
21 *
22 * Modified by Mitch Watrous <watrous@u.washington.edu>
23 *
24 */
25#ifndef RANDOM_VARIABLE_STREAM_H
26#define RANDOM_VARIABLE_STREAM_H
27
28#include "attribute-helper.h"
29#include "object.h"
30#include "type-id.h"
31
32#include <stdint.h>
33
40namespace ns3
41{
42
73class RngStream;
74
99{
100 public:
105 static TypeId GetTypeId();
113 ~RandomVariableStream() override;
114
115 // Delete copy constructor and assignment operator to avoid misuse
118
124 void SetStream(int64_t stream);
125
131 int64_t GetStream() const;
132
137 void SetAntithetic(bool isAntithetic);
138
143 bool IsAntithetic() const;
144
149 virtual double GetValue() = 0;
150
155 virtual uint32_t GetInteger() = 0;
156
157 protected:
162 RngStream* Peek() const;
163
164 private:
167
170
172 int64_t m_stream;
173
174}; // class RandomVariableStream
175
218{
219 public:
224 static TypeId GetTypeId();
225
230
235 double GetMin() const;
236
241 double GetMax() const;
242
253 double GetValue(double min, double max);
254
266
267 // Inherited from RandomVariableStream
273 double GetValue() override;
279 uint32_t GetInteger() override;
280
281 private:
283 double m_min;
284
286 double m_max;
287
288}; // class UniformRandomVariable
289
301{
302 public:
307 static TypeId GetTypeId();
308
313
318 double GetConstant() const;
319
325 double GetValue(double constant);
331 uint32_t GetInteger(uint32_t constant);
332
333 // Inherited from RandomVariableStream
334 /* \note This RNG always returns the same value. */
335 double GetValue() override;
336 /* \note This RNG always returns the same value. */
337 uint32_t GetInteger() override;
338
339 private:
342
343}; // class ConstantRandomVariable
344
381{
382 public:
387 static TypeId GetTypeId();
388
394
399 double GetMin() const;
400
406 double GetMax() const;
407
413
419 uint32_t GetConsecutive() const;
420
421 // Inherited from RandomVariableStream
422 double GetValue() override;
423 uint32_t GetInteger() override;
424
425 private:
427 double m_min;
428
430 double m_max;
431
434
437
439 double m_current;
440
443
446
447}; // class SequentialRandomVariable
448
519{
520 public:
525 static TypeId GetTypeId();
526
532
540 double GetMean() const;
541
546 double GetBound() const;
547
555 double GetValue(double mean, double bound);
556
565
566 // Inherited from RandomVariableStream
567 double GetValue() override;
568 uint32_t GetInteger() override;
569
570 private:
572 double m_mean;
573
575 double m_bound;
576
577}; // class ExponentialRandomVariable
578
620{
621 public:
626 static TypeId GetTypeId();
627
633
638 double GetScale() const;
639
644 double GetShape() const;
645
650 double GetBound() const;
651
679 double GetValue(double scale, double shape, double bound);
680
708 uint32_t GetInteger(uint32_t scale, uint32_t shape, uint32_t bound);
709
743 double GetValue() override;
744
769 uint32_t GetInteger() override;
770
771 private:
773 double m_scale;
774
776 double m_shape;
777
779 double m_bound;
780
781}; // class ParetoRandomVariable
782
840{
841 public:
846 static TypeId GetTypeId();
847
853
858 double GetScale() const;
859
864 double GetShape() const;
865
870 double GetBound() const;
871
898 double GetValue(double scale, double shape, double bound);
899
926 uint32_t GetInteger(uint32_t scale, uint32_t shape, uint32_t bound);
927
956 double GetValue() override;
957
981 uint32_t GetInteger() override;
982
983 private:
985 double m_scale;
986
988 double m_shape;
989
991 double m_bound;
992
993}; // class WeibullRandomVariable
994
1029{
1030 public:
1032 static const double INFINITE_VALUE;
1033
1038 static TypeId GetTypeId();
1039
1045
1050 double GetMean() const;
1051
1056 double GetVariance() const;
1057
1062 double GetBound() const;
1063
1102 double GetValue(double mean,
1103 double variance,
1105
1144 uint32_t GetInteger(uint32_t mean, uint32_t variance, uint32_t bound);
1145
1186 double GetValue() override;
1187
1223 uint32_t GetInteger() override;
1224
1225 private:
1227 double m_mean;
1228
1231
1233 double m_bound;
1234
1237
1239 double m_v2;
1241 double m_y;
1242
1243}; // class NormalRandomVariable
1244
1289{
1290 public:
1295 static TypeId GetTypeId();
1296
1302
1307 double GetMu() const;
1308
1313 double GetSigma() const;
1314
1350 double GetValue(double mu, double sigma);
1351
1388
1426 double GetValue() override;
1427
1461 uint32_t GetInteger() override;
1462
1463 private:
1465 double m_mu;
1466
1468 double m_sigma;
1469
1470}; // class LogNormalRandomVariable
1471
1504{
1505 public:
1510 static TypeId GetTypeId();
1511
1517
1522 double GetAlpha() const;
1523
1528 double GetBeta() const;
1529
1543 double GetValue(double alpha, double beta);
1544
1559 uint32_t GetInteger(uint32_t alpha, uint32_t beta);
1560
1577 double GetValue() override;
1578
1591 uint32_t GetInteger() override;
1592
1593 private:
1632 double GetNormalValue(double mean, double variance, double bound);
1633
1635 double m_alpha;
1636
1638 double m_beta;
1639
1642
1644 double m_v2;
1646 double m_y;
1647
1648}; // class GammaRandomVariable
1649
1686{
1687 public:
1692 static TypeId GetTypeId();
1693
1699
1704 uint32_t GetK() const;
1705
1710 double GetLambda() const;
1711
1725 double GetValue(uint32_t k, double lambda);
1726
1742
1759 double GetValue() override;
1760
1773 uint32_t GetInteger() override;
1774
1775 private:
1801 double GetExponentialValue(double mean, double bound);
1802
1805
1807 double m_lambda;
1808
1809}; // class ErlangRandomVariable
1810
1840{
1841 public:
1846 static TypeId GetTypeId();
1847
1853
1858 double GetMean() const;
1859
1864 double GetMin() const;
1865
1870 double GetMax() const;
1871
1910 double GetValue(double mean, double min, double max);
1911
1951
1992 double GetValue() override;
1993
2029 uint32_t GetInteger() override;
2030
2031 private:
2033 double m_mean;
2034
2036 double m_min;
2037
2039 double m_max;
2040
2041}; // class TriangularRandomVariable
2042
2102{
2103 public:
2108 static TypeId GetTypeId();
2109
2115
2120 uint32_t GetN() const;
2121
2126 double GetAlpha() const;
2127
2141 double GetValue(uint32_t n, double alpha);
2142
2158
2175 double GetValue() override;
2176
2189 uint32_t GetInteger() override;
2190
2191 private:
2194
2196 double m_alpha;
2197
2199 double m_c;
2200
2201}; // class ZipfRandomVariable
2202
2244{
2245 public:
2250 static TypeId GetTypeId();
2251
2257
2262 double GetAlpha() const;
2263
2276 double GetValue(double alpha);
2277
2291
2308 double GetValue() override;
2309
2321 uint32_t GetInteger() override;
2322
2323 private:
2325 double m_alpha;
2326
2328 double m_b;
2329
2330}; // class ZetaRandomVariable
2331
2363{
2364 public:
2369 static TypeId GetTypeId();
2370
2377
2386 void SetValueArray(double* values, std::size_t length);
2387
2392 double GetValue() override;
2393
2398 uint32_t GetInteger() override;
2399
2400 private:
2402 std::size_t m_count;
2403
2405 std::size_t m_next;
2406
2408 double* m_data;
2409
2410}; // class DeterministicRandomVariable
2411
2489{
2490 public:
2495 static TypeId GetTypeId();
2496
2502
2510 void CDF(double v, double c); // Value, prob <= Value
2511
2526 double GetValue() override;
2527
2541 uint32_t GetInteger() override;
2542
2549 virtual double Interpolate();
2550
2557 bool SetInterpolate(bool interpolate);
2558
2559 private:
2562 {
2563 public:
2565 ValueCDF();
2572 ValueCDF(double v, double c);
2573
2575 double value;
2577 double cdf;
2578 }; // class ValueCDF
2579
2590 void Validate();
2604 bool PreSample(double& value);
2610 double DoSampleCDF(double r);
2618 double DoInterpolate(double r);
2619
2626 friend bool operator<(ValueCDF a, ValueCDF b);
2627
2631 std::vector<ValueCDF> m_emp;
2637
2638}; // class EmpiricalRandomVariable
2639
2640} // namespace ns3
2641
2642#endif /* RANDOM_VARIABLE_STREAM_H */
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
Attribute helper (ATTRIBUTE_ )macros definition.
The Random Number Generator (RNG) that returns a constant.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value as a double drawn from the distribution.
ConstantRandomVariable()
Creates a constant RNG with the default constant value.
double GetConstant() const
Get the constant value returned by this RNG stream.
uint32_t GetInteger() override
Get the next random value as an integer drawn from the distribution.
double m_constant
The constant value returned by this RNG stream.
The Random Number Generator (RNG) that returns a predetermined sequence.
double GetValue() override
Returns the next value in the sequence.
std::size_t m_next
Position of the next value in the array of values.
static TypeId GetTypeId()
Register this type.
double * m_data
Array of values to return in sequence.
uint32_t GetInteger() override
Returns the next value in the sequence.
DeterministicRandomVariable()
Creates a deterministic RNG that will have a predetermined sequence of values.
void SetValueArray(double *values, std::size_t length)
Sets the array of values that holds the predetermined sequence.
std::size_t m_count
Size of the array of values.
Helper to hold one point of the CDF.
The Random Number Generator (RNG) that has a specified empirical distribution.
bool SetInterpolate(bool interpolate)
Switch the mode between sampling the CDF and interpolating.
void CDF(double v, double c)
Specifies a point in the empirical distribution.
uint32_t GetInteger() override
Returns the next value in the empirical distribution.
bool PreSample(double &value)
Do the initial rng draw and check against the extrema.
double DoSampleCDF(double r)
Sample the CDF as a histogram (without interpolation).
double GetValue() override
Returns the next value in the empirical distribution.
static TypeId GetTypeId()
Register this type.
bool m_interpolate
If true GetValue will interpolate, otherwise treat CDF as normal histogram.
bool m_validated
true once the CDF has been validated.
double DoInterpolate(double r)
Linear interpolation between two points on the CDF to estimate the value at r.
virtual double Interpolate()
Returns the next value in the empirical distribution using linear interpolation.
EmpiricalRandomVariable()
Creates an empirical RNG that has a specified, empirical distribution, and configured for interpolati...
std::vector< ValueCDF > m_emp
The vector of CDF points.
void Validate()
Check that the CDF is valid.
friend bool operator<(ValueCDF a, ValueCDF b)
Comparison operator, for use by std::upper_bound.
The Erlang distribution Random Number Generator (RNG) that allows stream numbers to be set determinis...
double m_lambda
The lambda value for the Erlang distribution returned by this RNG stream.
double GetValue() override
Returns a random double from an Erlang distribution with the current k and lambda.
double GetExponentialValue(double mean, double bound)
Returns a random double from an exponential distribution with the specified mean and upper bound.
static TypeId GetTypeId()
Register this type.
uint32_t GetK() const
Returns the k value for the Erlang distribution returned by this RNG stream.
uint32_t GetInteger() override
Returns a random unsigned integer from an Erlang distribution with the current k and lambda.
double GetLambda() const
Returns the lambda value for the Erlang distribution returned by this RNG stream.
uint32_t m_k
The k value for the Erlang distribution returned by this RNG stream.
ErlangRandomVariable()
Creates an Erlang distribution RNG with the default values for k and lambda.
The exponential distribution Random Number Generator (RNG).
ExponentialRandomVariable()
Creates an exponential distribution RNG with the default values for the mean and upper bound.
uint32_t GetInteger() override
Get the next random value as an integer drawn from the distribution.
double GetBound() const
Get the configured upper bound of this RNG.
double m_mean
The mean value of the unbounded exponential distribution.
double GetMean() const
Get the configured mean value of this RNG.
double m_bound
The upper bound on values that can be returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value as a double drawn from the distribution.
The gamma distribution Random Number Generator (RNG) that allows stream numbers to be set determinist...
double GetValue() override
Returns a random double from a gamma distribution with the current alpha and beta.
GammaRandomVariable()
Creates a gamma distribution RNG with the default values for alpha and beta.
double m_alpha
The alpha value for the gamma distribution returned by this RNG stream.
double GetNormalValue(double mean, double variance, double bound)
Returns a random double from a normal distribution with the specified mean, variance,...
double m_y
The algorithm produces two values at a time.
uint32_t GetInteger() override
Returns a random unsigned integer from a gamma distribution with the current alpha and beta.
double m_v2
The algorithm produces two values at a time.
bool m_nextValid
True if the next normal value is valid.
static TypeId GetTypeId()
Register this type.
double GetAlpha() const
Returns the alpha value for the gamma distribution returned by this RNG stream.
double GetBeta() const
Returns the beta value for the gamma distribution returned by this RNG stream.
double m_beta
The beta value for the gamma distribution returned by this RNG stream.
The log-normal distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
uint32_t GetInteger() override
Returns a random unsigned integer from a log-normal distribution with the current mu and sigma.
double GetMu() const
Returns the mu value for the log-normal distribution returned by this RNG stream.
double GetSigma() const
Returns the sigma value for the log-normal distribution returned by this RNG stream.
double GetValue() override
Returns a random double from a log-normal distribution with the current mu and sigma.
double m_mu
The mu value for the log-normal distribution returned by this RNG stream.
double m_sigma
The sigma value for the log-normal distribution returned by this RNG stream.
LogNormalRandomVariable()
Creates a log-normal distribution RNG with the default values for mu and sigma.
static TypeId GetTypeId()
Register this type.
The normal (Gaussian) distribution Random Number Generator (RNG) that allows stream numbers to be set...
double m_y
The algorithm produces two values at a time.
double GetBound() const
Returns the bound on values that can be returned by this RNG stream.
uint32_t GetInteger() override
Returns a random unsigned integer from a normal distribution with the current mean,...
double GetVariance() const
Returns the variance value for the normal distribution returned by this RNG stream.
double m_mean
The mean value for the normal distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double GetMean() const
Returns the mean value for the normal distribution returned by this RNG stream.
static const double INFINITE_VALUE
Large constant to bound the range.
double m_variance
The variance value for the normal distribution returned by this RNG stream.
double GetValue() override
Returns a random double from a normal distribution with the current mean, variance,...
double m_bound
The bound on values that can be returned by this RNG stream.
bool m_nextValid
True if the next value is valid.
NormalRandomVariable()
Creates a normal distribution RNG with the default values for the mean, variance, and bound.
double m_v2
The algorithm produces two values at a time.
A base class which provides memory management and object aggregation.
Definition: object.h:89
The Pareto distribution Random Number Generator (RNG).
double GetShape() const
Returns the shape parameter for the Pareto distribution returned by this RNG stream.
double m_scale
The scale parameter for the Pareto distribution returned by this RNG stream.
ParetoRandomVariable()
Creates a Pareto distribution RNG with the default values for the mean, the shape,...
static TypeId GetTypeId()
Register this type.
double m_shape
The shape parameter for the Pareto distribution returned by this RNG stream.
uint32_t GetInteger() override
Returns a random unsigned integer from a Pareto distribution with the current mean,...
double GetScale() const
Returns the scale parameter for the Pareto distribution returned by this RNG stream.
double m_bound
The upper bound on values that can be returned by this RNG stream.
double GetValue() override
Returns a random double from a Pareto distribution with the current mean, shape, and upper bound.
double GetBound() const
Returns the upper bound on values that can be returned by this RNG stream.
The basic uniform Random Number Generator (RNG).
static TypeId GetTypeId()
Register this type.
RngStream * Peek() const
Get the pointer to the underlying RngStream.
RandomVariableStream & operator=(const RandomVariableStream &)=delete
bool IsAntithetic() const
Check if antithetic values will be generated.
RandomVariableStream(const RandomVariableStream &)=delete
virtual uint32_t GetInteger()=0
Get the next random value as an integer drawn from the distribution.
virtual double GetValue()=0
Get the next random value as a double drawn from the distribution.
~RandomVariableStream() override
Destructor.
bool m_isAntithetic
Indicates if antithetic values should be generated by this RNG stream.
void SetAntithetic(bool isAntithetic)
Specify whether antithetic values should be generated.
int64_t m_stream
The stream number for the RngStream.
RandomVariableStream()
Default constructor.
RngStream * m_rng
Pointer to the underlying RngStream.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
int64_t GetStream() const
Returns the stream number for the RngStream.
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:50
The Random Number Generator (RNG) that returns a pattern of sequential values.
uint32_t m_currentConsecutive
The number of times the current distinct value has been repeated.
double m_min
The first value of the sequence.
Ptr< RandomVariableStream > GetIncrement() const
Get the increment for the sequence.
uint32_t m_consecutive
The number of times each distinct value is repeated.
static TypeId GetTypeId()
Register this type.
uint32_t GetInteger() override
Get the next random value as an integer drawn from the distribution.
double m_current
The current sequence value.
double m_max
Strict upper bound on the sequence.
Ptr< RandomVariableStream > m_increment
Increment between distinct values.
double GetValue() override
Get the next random value as a double drawn from the distribution.
uint32_t GetConsecutive() const
Get the number of times each distinct value of the sequence is repeated before incrementing to the ne...
double GetMax() const
Get the limit of the sequence, which is (at least) one more than the last value of the sequence.
SequentialRandomVariable()
Creates a sequential RNG with the default values for the sequence parameters.
double GetMin() const
Get the first value of the sequence.
bool m_isCurrentSet
Indicates if the current sequence value has been properly initialized.
The triangular distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
double GetValue() override
Returns a random double from a triangular distribution with the current mean, min,...
uint32_t GetInteger() override
Returns a random unsigned integer from a triangular distribution with the current mean,...
double GetMean() const
Returns the mean value for the triangular distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double m_mean
The mean value for the triangular distribution returned by this RNG stream.
double m_max
The upper bound on values that can be returned by this RNG stream.
double GetMax() const
Returns the upper bound on values that can be returned by this RNG stream.
TriangularRandomVariable()
Creates a triangular distribution RNG with the default values for the mean, lower bound,...
double m_min
The lower bound on values that can be returned by this RNG stream.
double GetMin() const
Returns the lower bound for the triangular distribution returned by this RNG stream.
a unique identifier for an interface.
Definition: type-id.h:60
The uniform distribution Random Number Generator (RNG).
UniformRandomVariable()
Creates a uniform distribution RNG with the default range.
uint32_t GetInteger() override
Get the next random value as an integer drawn from the distribution.
double GetMax() const
Get the upper bound on values returned by GetValue().
double m_min
The lower bound on values that can be returned by this RNG stream.
double m_max
The upper bound on values that can be returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value as a double drawn from the distribution.
double GetMin() const
Get the lower bound on randoms returned by GetValue().
The Weibull distribution Random Number Generator (RNG) that allows stream numbers to be set determini...
double m_shape
The shape parameter for the Weibull distribution returned by this RNG stream.
double m_bound
The upper bound on values that can be returned by this RNG stream.
double m_scale
The scale parameter for the Weibull distribution returned by this RNG stream.
double GetBound() const
Returns the upper bound on values that can be returned by this RNG stream.
double GetValue() override
Returns a random double from a Weibull distribution with the current scale, shape,...
WeibullRandomVariable()
Creates a Weibull distribution RNG with the default values for the scale, shape, and upper bound.
static TypeId GetTypeId()
Register this type.
uint32_t GetInteger() override
Returns a random unsigned integer from a Weibull distribution with the current scale,...
double GetScale() const
Returns the scale parameter for the Weibull distribution returned by this RNG stream.
double GetShape() const
Returns the shape parameter for the Weibull distribution returned by this RNG stream.
The zeta distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
static TypeId GetTypeId()
Register this type.
double m_alpha
The alpha value for the zeta distribution returned by this RNG stream.
double GetValue() override
Returns a random double from a zeta distribution with the current alpha.
ZetaRandomVariable()
Creates a zeta distribution RNG with the default value for alpha.
double GetAlpha() const
Returns the alpha value for the zeta distribution returned by this RNG stream.
uint32_t GetInteger() override
Returns a random unsigned integer from a zeta distribution with the current alpha.
double m_b
Just for calculus simplifications.
The Zipf distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
uint32_t GetN() const
Returns the n value for the Zipf distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double m_c
The normalization constant.
uint32_t GetInteger() override
Returns a random unsigned integer from a Zipf distribution with the current n and alpha.
double GetAlpha() const
Returns the alpha value for the Zipf distribution returned by this RNG stream.
ZipfRandomVariable()
Creates a Zipf distribution RNG with the default values for n and alpha.
double m_alpha
The alpha value for the Zipf distribution returned by this RNG stream.
uint32_t m_n
The n value for the Zipf distribution returned by this RNG stream.
double GetValue() override
Returns a random double from a Zipf distribution with the current n and alpha.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
value
Definition: second.py:41
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::TypeId declaration; inline and template implementations.