diff --git a/src/core/test/random-variable-stream-test-suite.cc b/src/core/test/random-variable-stream-test-suite.cc index 777c85624..17434ae25 100644 --- a/src/core/test/random-variable-stream-test-suite.cc +++ b/src/core/test/random-variable-stream-test-suite.cc @@ -34,11 +34,14 @@ #include "ns3/string.h" #include "ns3/integer.h" #include "ns3/test.h" +#include "ns3/log.h" #include "ns3/rng-seed-manager.h" #include "ns3/random-variable-stream.h" using namespace ns3; +NS_LOG_COMPONENT_DEFINE ("RandomVariableStreamGenerators"); + namespace { void @@ -54,6 +57,57 @@ FillHistoRangeUniformly (double *array, uint32_t n, double start, double end) } } +bool seedSet = false; + +// Over time, this test suite is designed to be run with varying seed +// values so that the distributions can be evaluated with chi-squared +// tests. To enable this, normal invocation of this test suite will +// result in a seed value corresponding to the seconds since epoch +// (time (0) from ctime). Note: this is not a recommended practice for +// seeding normal simulations, as described in the ns-3 manual, but +// suits our purposes here. +// +// However, we also want to provide the ability to run this test suite +// with a repeatable value, such as when the seed or run number is configured +// to a specific value. Therefore, we adopt the following policy. When +// the test program is being run with the default global values for seed +// and run number, this function will instead pick a random, time-based +// seed for use within this test suite. If the global values for seed or +// run number have been configured differently from the default values, +// the global seed value will be used instead of the time-based one. +// +// For example, this command will cause this test suite to use the +// deterministic value of seed=3 every time: +// NS_GLOBAL_VALUE="RngSeed=3" ./test.py -s random-variable-stream-generators +// or equivalently (to see log output): +// NS_LOG="RandomVariableStreamGenerators" NS_GLOBAL_VALUE="RngSeed=3" ./waf --run "test-runner --suite=random-variable-stream-generators" +// Similarly, if the value of RngRun is not set to 1, the globals will be +// used. +// +void +SetTestSuiteSeed (void) +{ + if (seedSet == false) + { + uint32_t seed; + if (RngSeedManager::GetSeed () == 1 && RngSeedManager::GetRun () == 1) + { + seed = static_cast (time (0)); + seedSet = true; + NS_LOG_DEBUG ("Global seed and run number are default; seeding with time of day: " << seed); + + } + else + { + seed = RngSeedManager::GetSeed (); + seedSet = true; + NS_LOG_DEBUG ("Global seed and run number are not default; using the non-default values seed: " << + seed << " and run: " << RngSeedManager::GetRun ()); + } + SeedManager::SetSeed (seed); + } +} + } // anonymous namespace // =========================================================================== @@ -125,7 +179,7 @@ RandomVariableStreamUniformTestCase::ChiSquaredTest (Ptr void RandomVariableStreamUniformTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -268,7 +322,7 @@ RandomVariableStreamUniformAntitheticTestCase::ChiSquaredTest (Ptr c = CreateObject (); @@ -395,7 +449,7 @@ RandomVariableStreamSequentialTestCase::~RandomVariableStreamSequentialTestCase void RandomVariableStreamSequentialTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); Ptr s = CreateObject (); @@ -509,7 +563,7 @@ RandomVariableStreamNormalTestCase::ChiSquaredTest (Ptr n) void RandomVariableStreamNormalTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -635,7 +689,7 @@ RandomVariableStreamNormalAntitheticTestCase::ChiSquaredTest (Ptr p) void RandomVariableStreamParetoTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1149,7 +1203,7 @@ RandomVariableStreamParetoAntitheticTestCase::ChiSquaredTest (Ptr void RandomVariableStreamWeibullTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1437,7 +1491,7 @@ RandomVariableStreamWeibullAntitheticTestCase::ChiSquaredTest (Ptr n) void RandomVariableStreamGammaTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1998,7 +2052,7 @@ RandomVariableStreamGammaAntitheticTestCase::ChiSquaredTest (Ptr n) void RandomVariableStreamErlangTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -2270,7 +2324,7 @@ RandomVariableStreamErlangAntitheticTestCase::ChiSquaredTest (Ptr s = CreateObject (); @@ -2726,7 +2780,7 @@ RandomVariableStreamEmpiricalTestCase::~RandomVariableStreamEmpiricalTestCase () void RandomVariableStreamEmpiricalTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); // Create the RNG with a uniform distribution between 0 and 10. Ptr x = CreateObject (); @@ -2790,7 +2844,7 @@ RandomVariableStreamEmpiricalAntitheticTestCase::~RandomVariableStreamEmpiricalA void RandomVariableStreamEmpiricalAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); // Create the RNG with a uniform distribution between 0 and 10. Ptr x = CreateObject ();