bug 1927: allow deterministic seeding of random-variable-stream-generators TestSuite
authorTom Henderson <tomh@tomh.org>
Tue, 01 Sep 2015 21:03:55 -0700
changeset 11624 5ed58455441a
parent 11623 9973457339ef
child 11625 07bfb4b5ad52
bug 1927: allow deterministic seeding of random-variable-stream-generators TestSuite After this commit, the following will cause the test suite to always fail: NS_GLOBAL_VALUE="RngRun=231" ./test.py -s random-variable-stream-generators
src/core/test/random-variable-stream-test-suite.cc
--- a/src/core/test/random-variable-stream-test-suite.cc	Tue Sep 01 22:48:58 2015 +0200
+++ b/src/core/test/random-variable-stream-test-suite.cc	Tue Sep 01 21:03:55 2015 -0700
@@ -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 @@
     }
 }
 
+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<uint32_t> (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 @@
 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 @@
 void
 RandomVariableStreamUniformAntitheticTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   double sum = 0.;
   double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS);
@@ -345,7 +399,7 @@
 void
 RandomVariableStreamConstantTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   Ptr<ConstantRandomVariable> c = CreateObject<ConstantRandomVariable> ();
 
@@ -395,7 +449,7 @@
 void
 RandomVariableStreamSequentialTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   Ptr<SequentialRandomVariable> s = CreateObject<SequentialRandomVariable> ();
 
@@ -509,7 +563,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 +689,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 +820,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 +940,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 +1069,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 +1203,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 +1345,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 +1491,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 +1644,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 +1780,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 +1923,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 +2052,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 +2192,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 +2324,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 +2405,7 @@
 void
 RandomVariableStreamZipfTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   uint32_t n = 1;
   double alpha = 2.0;
@@ -2433,7 +2487,7 @@
 void
 RandomVariableStreamZipfAntitheticTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   uint32_t n = 1;
   double alpha = 2.0;
@@ -2518,7 +2572,7 @@
 void
 RandomVariableStreamZetaTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   double alpha = 5.0;
   double value;
@@ -2582,7 +2636,7 @@
 void
 RandomVariableStreamZetaAntitheticTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   double alpha = 5.0;
   double value;
@@ -2651,7 +2705,7 @@
 void
 RandomVariableStreamDeterministicTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   Ptr<DeterministicRandomVariable> s = CreateObject<DeterministicRandomVariable> ();
 
@@ -2726,7 +2780,7 @@
 void
 RandomVariableStreamEmpiricalTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   // Create the RNG with a uniform distribution between 0 and 10.
   Ptr<EmpiricalRandomVariable> x = CreateObject<EmpiricalRandomVariable> ();
@@ -2790,7 +2844,7 @@
 void
 RandomVariableStreamEmpiricalAntitheticTestCase::DoRun (void)
 {
-  SeedManager::SetSeed (time (0));
+  SetTestSuiteSeed ();
 
   // Create the RNG with a uniform distribution between 0 and 10.
   Ptr<EmpiricalRandomVariable> x = CreateObject<EmpiricalRandomVariable> ();