12#include "ns3/boolean.h"
13#include "ns3/double.h"
14#include "ns3/integer.h"
16#include "ns3/random-variable-stream.h"
17#include "ns3/rng-seed-manager.h"
18#include "ns3/shuffle.h"
19#include "ns3/string.h"
21#include "ns3/uinteger.h"
26#include <gsl/gsl_cdf.h>
27#include <gsl/gsl_histogram.h>
28#include <gsl/gsl_randist.h>
29#include <gsl/gsl_sf_zeta.h>
41namespace RandomVariable
95 std::vector<double> range(
nBins + 1);
104 range[0] = -std::numeric_limits<double>::max();
108 range[
nBins] = std::numeric_limits<double>::max();
168 template <
typename RNG>
210 const std::vector<double>&
expected,
215 "Histogram and expected vector have different sizes.");
227 for (std::size_t
i = 0;
i <
nBins; ++
i)
287 for (std::size_t
i = 0;
i <
nRuns; ++
i)
338 "Special run number value of zero; seeding with time of day: " <<
seed);
372 void DoRun()
override;
376 :
TestCaseBase(
"Uniform Random Variable Stream Generator")
438 value = x->GetValue();
455 for (
int i = 0;
i < 20;
i++)
463 for (
int i = 0;
i < 20;
i++)
494 void DoRun()
override;
498 :
TestCaseBase(
"Antithetic Uniform Random Variable Stream Generator")
548 value = x->GetValue();
566 void DoRun()
override;
573 :
TestCaseBase(
"Constant Random Variable Stream Generator")
601 "Constant value changed in loop");
617 void DoRun()
override;
624 :
TestCaseBase(
"Sequential Random Variable Stream Generator")
642 s->SetAttribute(
"Increment",
StringValue(
"ns3::UniformRandomVariable[Min=3.0|Max=3.0]"));
648 value = s->GetValue();
650 value = s->GetValue();
652 value = s->GetValue();
654 value = s->GetValue();
656 value = s->GetValue();
658 value = s->GetValue();
677 void DoRun()
override;
684 :
TestCaseBase(
"Normal Random Variable Stream Generator")
745 "Wrong mean value.");
763 void DoRun()
override;
770 :
TestCaseBase(
"Antithetic Normal Random Variable Stream Generator")
833 "Wrong mean value.");
851 void DoRun()
override;
858 :
TestCaseBase(
"Exponential Random Variable Stream Generator")
914 "Wrong mean value.");
932 void DoRun()
override;
939 :
TestCaseBase(
"Antithetic Exponential Random Variable Stream Generator")
998 "Wrong mean value.");
1016 void DoRun()
override;
1026 :
TestCaseBase(
"Pareto Random Variable Stream Generator")
1041 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1091 "Wrong mean value.");
1109 void DoRun()
override;
1119 :
TestCaseBase(
"Antithetic Pareto Random Variable Stream Generator")
1134 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1188 "Wrong mean value.");
1206 void DoRun()
override;
1216 :
TestCaseBase(
"Weibull Random Variable Stream Generator")
1234 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1258 const auto scale = 5.0;
1259 const auto shape = 1.0;
1301 "Wrong measured mean value.");
1319 void DoRun()
override;
1329 :
TestCaseBase(
"Antithetic Weibull Random Variable Stream Generator")
1347 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1412 "Wrong mean value.");
1430 void DoRun()
override;
1440 :
TestCaseBase(
"Log-Normal Random Variable Stream Generator")
1458 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1514 "Wrong mean value.");
1532 void DoRun()
override;
1542 :
TestCaseBase(
"Antithetic Log-Normal Random Variable Stream Generator")
1560 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1618 "Wrong mean value.");
1636 void DoRun()
override;
1646 :
TestCaseBase(
"Gamma Random Variable Stream Generator")
1664 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1710 "Wrong mean value.");
1728 void DoRun()
override;
1738 :
TestCaseBase(
"Antithetic Gamma Random Variable Stream Generator")
1756 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1806 "Wrong mean value.");
1824 void DoRun()
override;
1834 :
TestCaseBase(
"Erlang Random Variable Stream Generator")
1855 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
1901 "Wrong mean value.");
1919 void DoRun()
override;
1929 :
TestCaseBase(
"Antithetic Erlang Random Variable Stream Generator")
1950 for (std::size_t
i = 0;
i <
N_BINS; ++
i)
2000 "Wrong mean value.");
2015 void DoRun()
override;
2025 :
TestCaseBase(
"Zipf Random Variable Stream Generator")
2080 "Wrong mean value.");
2095 void DoRun()
override;
2105 :
TestCaseBase(
"Antithetic Zipf Random Variable Stream Generator")
2163 "Wrong mean value.");
2178 void DoRun()
override;
2188 :
TestCaseBase(
"Zeta Random Variable Stream Generator")
2226 "Wrong mean value.");
2241 void DoRun()
override;
2251 :
TestCaseBase(
"Antithetic Zeta Random Variable Stream Generator")
2292 "Wrong mean value.");
2307 void DoRun()
override;
2314 :
TestCaseBase(
"Deterministic Random Variable Stream Generator")
2330 double array1[] = {4, 4, 7, 7, 10, 10};
2337 value = s->GetValue();
2339 value = s->GetValue();
2341 value = s->GetValue();
2343 value = s->GetValue();
2345 value = s->GetValue();
2347 value = s->GetValue();
2354 double array2[] = {1000, 2000, 3000, 4000};
2359 value = s->GetValue();
2361 value = s->GetValue();
2363 value = s->GetValue();
2365 value = s->GetValue();
2367 value = s->GetValue();
2382 void DoRun()
override;
2392 :
TestCaseBase(
"Empirical Random Variable Stream Generator")
2404 x->SetInterpolate(
false);
2412 double value = x->GetValue();
2415 "Incorrect value returned, expected only 5 or 10.");
2435 "Wrong mean value.");
2438 x->SetInterpolate(
true);
2459 "Wrong mean value.");
2463 y->SetInterpolate(
false);
2482 void DoRun()
override;
2492 :
TestCaseBase(
"EmpiricalAntithetic Random Variable Stream Generator")
2504 x->SetInterpolate(
false);
2515 double value = x->GetValue();
2518 "Incorrect value returned, expected only 5 or 10.");
2529 "Wrong mean value.");
2532 x->SetInterpolate(
true);
2546 "Wrong mean value.");
2561 void DoRun()
override;
2565 :
TestCaseBase(
"NormalRandomVariable caching of parameters")
2576 double v1 = n->GetValue(-10, 1, 10);
2577 double v2 = n->GetValue(10, 1, 10);
2598 void DoRun()
override;
2605 :
TestCaseBase(
"Bernoulli Random Variable Stream Generator")
2651 "Wrong mean value.");
2669 void DoRun()
override;
2676 :
TestCaseBase(
"Antithetic Bernoulli Random Variable Stream Generator")
2725 "Wrong mean value.");
2743 void DoRun()
override;
2750 :
TestCaseBase(
"Binomial Random Variable Stream Generator")
2764 for (std::size_t
i = 0;
i <
trials + 1; ++
i)
2804 "Wrong mean value.");
2822 void DoRun()
override;
2829 :
TestCaseBase(
"Antithetic Binomial Random Variable Stream Generator")
2843 for (std::size_t
i = 0;
i <
trials + 1; ++
i)
2886 "Wrong mean value.");
2903 void DoRun()
override;
2907 :
TestCase(
"Check correct operation of the Shuffle function")
2921 std::vector<uint8_t>
vec{};
2951 "Expected vector {4, 1, 9, 3, 2, 7}");
2964 void DoRun()
override;
2974 :
TestCaseBase(
"Laplacian Random Variable Stream Generator")
2986 double bound = 20.0;
3008 "Wrong variance value.");
3027 const auto value =
x2->GetValue();
3030 "Value not in expected boundaries.");
3044 void DoRun()
override;
3054 :
TestCaseBase(
"Largest Extreme Value Random Variable Stream Generator")
3087 "Wrong variance value.");
3103 :
TestSuite(
"random-variable-stream-generators",
Type::UNIT)
Simple average, min, max and std.
AttributeValue implementation for Boolean.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Hold a signed integer type.
double GetVariance() const
Returns the variance value for the laplacian distribution returned by this RNG stream.
double GetMean() const
Returns the mean value for the Largest Extreme Value distribution returned by this RNG stream.
double GetVariance() const
Returns the variance value for the Largest Extreme Value distribution returned by this RNG stream.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Smart pointer class similar to boost::intrusive_ptr.
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static uint64_t GetRun()
Get the current run number.
static uint32_t GetSeed()
Get the current seed value which will be used by all subsequently instantiated RandomVariableStream o...
Hold variables of type string.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Test case for antithetic bernoulli distribution random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
BernoulliAntitheticTestCase()
void DoRun() override
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
Test case for bernoulli distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
void DoRun() override
Implementation to actually run this TestCase.
Test case for antithetic binomial distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
BinomialAntitheticTestCase()
Test case for binomial distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
Test case for constant random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation.
void DoRun() override
Implementation to actually run this TestCase.
Test case for deterministic random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation.
Test case for antithetic empirical distribution random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
EmpiricalAntitheticTestCase()
Test case for empirical distribution random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
Test case for antithetic Erlang distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
ErlangAntitheticTestCase()
Test case for Erlang distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
Test case for antithetic exponential distribution random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
void DoRun() override
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
ExponentialAntitheticTestCase()
Test case for exponential distribution random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
void DoRun() override
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
Test case for antithetic gamma distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
GammaAntitheticTestCase()
void DoRun() override
Implementation to actually run this TestCase.
Test case for gamma distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
Test case for laplacian distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
Test case for largest extreme value distribution random variable stream generator.
LargestExtremeValueTestCase()
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
Test case for antithetic log-normal distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
LogNormalAntitheticTestCase()
Test case for log-normal distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
Test case for antithetic normal distribution random variable stream generator.
NormalAntitheticTestCase()
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
void DoRun() override
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
Test case for caching of Normal RV parameters (see issue #302)
void DoRun() override
Implementation to actually run this TestCase.
Test case for normal distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, in rms.
Test case for antithetic Pareto distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
ParetoAntitheticTestCase()
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
Test case for Pareto distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
RandomVariableStream test suite, covering all random number variable stream generator types.
Test case for sequential random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation.
void DoRun() override
Implementation to actually run this TestCase.
Test the Shuffle function.
void DoRun() override
Implementation to actually run this TestCase.
A factory base class to create new instances of a random variable.
virtual Ptr< RandomVariableStream > Create() const =0
Create a new instance of a random variable stream.
Factory class to create new instances of a particular random variable stream.
Ptr< RandomVariableStream > Create() const override
Create a new instance of a random variable stream.
bool m_anti
Whether to create antithetic random variable streams.
RngGenerator(bool anti=false)
Constructor.
Base class for RandomVariableStream test suites.
double ChiSquared(gsl_histogram *h, const std::vector< double > &expected, Ptr< RandomVariableStream > rng) const
Compute the chi squared value of a sampled distribution compared to the expected distribution.
static const uint32_t N_MEASUREMENTS
Number of samples to draw when populating the distributions.
void SetTestSuiteSeed()
Set the seed used for this test suite.
double ChiSquaredsAverage(const RngGeneratorBase *generator, std::size_t nRuns) const
Average the chi squared value over some number of runs, each run with a new instance of the random nu...
std::vector< double > UniformHistogramBins(gsl_histogram *h, double start, double end, bool underflow=true, bool overflow=true) const
Configure a GSL histogram with uniform bins, with optional under/over-flow bins.
virtual double ChiSquaredTest(Ptr< RandomVariableStream > rng) const
Compute the chi square value from a random variable.
static const uint32_t N_BINS
Number of bins for sampling the distributions.
double Variance(Ptr< RandomVariableStream > rng, double average) const
Compute the variance of a random variable.
bool m_seedSet
true if we've already set the seed the correctly.
TestCaseBase(std::string name)
Constructor.
static const uint32_t N_RUNS
Number of retry attempts to pass a chi-square test.
double Average(Ptr< RandomVariableStream > rng) const
Compute the average of a random variable.
Test case for antithetic Weibull distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
WeibullAntitheticTestCase()
Test case for Weibull distribution random variable stream generator.
double ChiSquaredTest(Ptr< RandomVariableStream > rng) const override
Compute the chi square value from a random variable.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
Test case for antithetic Zeta distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
Test case for Zeta distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
Test case for antithetic Zipf distribution random variable stream generator.
void DoRun() override
Implementation to actually run this TestCase.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
Test case for Zipf distribution random variable stream generator.
static constexpr double TOLERANCE
Tolerance for testing rng values against expectation, as a fraction of mean value.
void DoRun() override
Implementation to actually run this TestCase.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report and abort if not.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
static RandomVariableSuite randomVariableSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static unsigned int value(char c)
void Shuffle(RND_ACCESS_ITER first, RND_ACCESS_ITER last, Ptr< UniformRandomVariable > rv)
Shuffle the elements in the range first to last.
-ns3 Test suite for the ns3 wrapper script