# HG changeset patch # User Tom Henderson # Date 1424887600 28800 # Wed Feb 25 10:06:40 2015 -0800 # Node ID a59968213a2d1cf5abde635a94f505a4cf31b6f5 # Parent ff75249ead1e136dcf6291295cbe632a60825aab TestSuite random-variable-stream-generators: only seed RNG once; allow deterministic seeds diff -r ff75249ead1e -r a59968213a2d src/core/test/random-variable-stream-test-suite.cc --- a/src/core/test/random-variable-stream-test-suite.cc Wed Feb 25 06:47:21 2015 -0800 +++ b/src/core/test/random-variable-stream-test-suite.cc Wed Feb 25 10:06:40 2015 -0800 @@ -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,53 @@ } } +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 different 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" +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 value: " << seed); + } + SeedManager::SetSeed (seed); + } +} + } // anonymous namespace // =========================================================================== @@ -125,7 +175,7 @@ void RandomVariableStreamUniformTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -268,7 +318,7 @@ void RandomVariableStreamUniformAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -345,7 +395,7 @@ void RandomVariableStreamConstantTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); Ptr c = CreateObject (); @@ -395,7 +445,7 @@ void RandomVariableStreamSequentialTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); Ptr s = CreateObject (); @@ -509,7 +559,7 @@ void RandomVariableStreamNormalTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -635,7 +685,7 @@ void RandomVariableStreamNormalAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -766,7 +816,7 @@ void RandomVariableStreamExponentialTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -886,7 +936,7 @@ void RandomVariableStreamExponentialAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1015,7 +1065,7 @@ void RandomVariableStreamParetoTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1149,7 +1199,7 @@ void RandomVariableStreamParetoAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1291,7 +1341,7 @@ void RandomVariableStreamWeibullTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1437,7 +1487,7 @@ void RandomVariableStreamWeibullAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1590,7 +1640,7 @@ void RandomVariableStreamLogNormalTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1726,7 +1776,7 @@ void RandomVariableStreamLogNormalAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1869,7 +1919,7 @@ void RandomVariableStreamGammaTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -1998,7 +2048,7 @@ void RandomVariableStreamGammaAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -2138,7 +2188,7 @@ void RandomVariableStreamErlangTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -2270,7 +2320,7 @@ void RandomVariableStreamErlangAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double sum = 0.; double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); @@ -2351,7 +2401,7 @@ void RandomVariableStreamZipfTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); uint32_t n = 1; double alpha = 2.0; @@ -2433,7 +2483,7 @@ void RandomVariableStreamZipfAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); uint32_t n = 1; double alpha = 2.0; @@ -2518,7 +2568,7 @@ void RandomVariableStreamZetaTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double alpha = 5.0; double value; @@ -2582,7 +2632,7 @@ void RandomVariableStreamZetaAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); double alpha = 5.0; double value; @@ -2651,7 +2701,7 @@ void RandomVariableStreamDeterministicTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); Ptr s = CreateObject (); @@ -2726,7 +2776,7 @@ void RandomVariableStreamEmpiricalTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); // Create the RNG with a uniform distribution between 0 and 10. Ptr x = CreateObject (); @@ -2783,7 +2833,7 @@ void RandomVariableStreamEmpiricalAntitheticTestCase::DoRun (void) { - SeedManager::SetSeed (time (0)); + SetTestSuiteSeed (); // Create the RNG with a uniform distribution between 0 and 10. Ptr x = CreateObject ();