19 #include <gsl/gsl_cdf.h> 20 #include <gsl/gsl_histogram.h> 25 #include "ns3/double.h" 26 #include "ns3/random-variable-stream.h" 27 #include "ns3/rng-seed-manager.h" 34 double increment = (end -
start) / (
n - 1.);
37 for (uint32_t i = 0; i <
n; ++i)
50 static const uint32_t N_RUNS = 5;
51 static const uint32_t N_BINS = 50;
52 static const uint32_t N_MEASUREMENTS = 1000000;
60 virtual void DoRun (
void);
64 :
TestCase (
"Uniform Random Number Generator")
73 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
74 gsl_histogram_set_ranges_uniform (h, 0., 1.);
78 gsl_histogram_increment (h, u->
GetValue ());
85 for (uint32_t i = 0; i <
N_BINS; ++i)
87 tmp[i] = gsl_histogram_get (h, i);
93 gsl_histogram_free (h);
95 double chiSquared = 0;
97 for (uint32_t i = 0; i <
N_BINS; ++i)
108 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
111 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
113 for (uint32_t i = 0; i <
N_RUNS; ++i)
141 virtual void DoRun (
void);
145 :
TestCase (
"Normal Random Number Generator")
154 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
161 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
167 for (uint32_t i = 0; i <
N_BINS; ++i)
169 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
175 gsl_histogram_increment (h,
n->GetValue ());
180 for (uint32_t i = 0; i <
N_BINS; ++i)
182 tmp[i] = gsl_histogram_get (h, i);
183 tmp[i] -= expected[i];
185 tmp[i] /= expected[i];
188 gsl_histogram_free (h);
190 double chiSquared = 0;
192 for (uint32_t i = 0; i <
N_BINS; ++i)
194 chiSquared += tmp[i];
203 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
206 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
208 for (uint32_t i = 0; i <
N_RUNS; ++i)
236 virtual void DoRun (
void);
240 :
TestCase (
"Exponential Random Number Generator")
249 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
255 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
261 for (uint32_t i = 0; i <
N_BINS; ++i)
263 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
269 gsl_histogram_increment (h, e->
GetValue ());
274 for (uint32_t i = 0; i <
N_BINS; ++i)
276 tmp[i] = gsl_histogram_get (h, i);
277 tmp[i] -= expected[i];
279 tmp[i] /= expected[i];
282 gsl_histogram_free (h);
284 double chiSquared = 0;
286 for (uint32_t i = 0; i <
N_BINS; ++i)
288 chiSquared += tmp[i];
297 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
300 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
302 for (uint32_t i = 0; i <
N_RUNS; ++i)
330 virtual void DoRun (
void);
334 :
TestCase (
"Pareto Random Number Generator")
343 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
349 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
354 double b = 0.33333333;
358 for (uint32_t i = 0; i <
N_BINS; ++i)
360 expected[i] = gsl_cdf_pareto_P (range[i + 1], a, b) - gsl_cdf_pareto_P (range[i], a, b);
366 gsl_histogram_increment (h, p->
GetValue ());
371 for (uint32_t i = 0; i <
N_BINS; ++i)
373 tmp[i] = gsl_histogram_get (h, i);
374 tmp[i] -= expected[i];
376 tmp[i] /= expected[i];
379 gsl_histogram_free (h);
381 double chiSquared = 0;
383 for (uint32_t i = 0; i <
N_BINS; ++i)
385 chiSquared += tmp[i];
394 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
397 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
399 for (uint32_t i = 0; i <
N_RUNS; ++i)
420 :
TestSuite (
"random-number-generators", UNIT)
virtual void DoRun(void)
Implementation to actually run this TestCase.
double ChiSquaredTest(Ptr< NormalRandomVariable > n)
void FillHistoRangeUniformly(double *array, uint32_t n, double start, double end)
virtual ~RngExponentialTestCase()
double ChiSquaredTest(Ptr< ParetoRandomVariable > p)
virtual void DoRun(void)
Implementation to actually run this TestCase.
static const uint32_t N_MEASUREMENTS
static RngTestSuite rngTestSuite
virtual ~RngParetoTestCase()
static const uint32_t N_RUNS
static const uint32_t N_RUNS
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
virtual void DoRun(void)
Implementation to actually run this TestCase.
static const uint32_t N_MEASUREMENTS
static const uint32_t N_BINS
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double ChiSquaredTest(Ptr< ExponentialRandomVariable > n)
static const uint32_t N_MEASUREMENTS
static const uint32_t N_BINS
static const uint32_t N_BINS
double GetValue(double mean, double bound)
Get the next random value, as a double from the exponential distribution with the specified mean and ...
double GetValue(double scale, double shape, double bound)
Returns a random double from a Pareto distribution with the specified scale, shape, and upper bound.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
virtual ~RngNormalTestCase()
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
#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.
static const uint32_t N_RUNS