16 #include "ns3/double.h"
17 #include "ns3/random-variable-stream.h"
18 #include "ns3/rng-seed-manager.h"
24 #include <gsl/gsl_cdf.h>
25 #include <gsl/gsl_histogram.h>
52 double increment = (end -
start) / (n - 1.);
55 for (uint32_t i = 0; i < n; ++i)
71 static const uint32_t N_RUNS = 5;
73 static const uint32_t N_BINS = 50;
75 static const uint32_t N_MEASUREMENTS = 1000000;
88 void DoRun()
override;
92 :
TestCase(
"Uniform Random Number Generator")
103 gsl_histogram* h = gsl_histogram_alloc(
N_BINS);
104 gsl_histogram_set_ranges_uniform(h, 0., 1.);
108 gsl_histogram_increment(h, u->
GetValue());
115 for (uint32_t i = 0; i <
N_BINS; ++i)
117 tmp[i] = gsl_histogram_get(h, i);
123 gsl_histogram_free(h);
125 double chiSquared = 0;
127 for (uint32_t i = 0; i <
N_BINS; ++i)
129 chiSquared += tmp[i];
138 RngSeedManager::SetSeed(
static_cast<uint32_t
>(time(
nullptr)));
141 double maxStatistic = gsl_cdf_chisq_Qinv(0.05,
N_BINS);
143 for (uint32_t i = 0; i <
N_RUNS; ++i)
181 void DoRun()
override;
185 :
TestCase(
"Normal Random Number Generator")
196 gsl_histogram* h = gsl_histogram_alloc(
N_BINS);
203 gsl_histogram_set_ranges(h, range,
N_BINS + 1);
209 for (uint32_t i = 0; i <
N_BINS; ++i)
211 expected[i] = gsl_cdf_gaussian_P(range[i + 1],
sigma) - gsl_cdf_gaussian_P(range[i],
sigma);
217 gsl_histogram_increment(h, n->GetValue());
222 for (uint32_t i = 0; i <
N_BINS; ++i)
224 tmp[i] = gsl_histogram_get(h, i);
225 tmp[i] -= expected[i];
227 tmp[i] /= expected[i];
230 gsl_histogram_free(h);
232 double chiSquared = 0;
234 for (uint32_t i = 0; i <
N_BINS; ++i)
236 chiSquared += tmp[i];
245 RngSeedManager::SetSeed(
static_cast<uint32_t
>(time(
nullptr)));
248 double maxStatistic = gsl_cdf_chisq_Qinv(0.05,
N_BINS);
250 for (uint32_t i = 0; i <
N_RUNS; ++i)
288 void DoRun()
override;
292 :
TestCase(
"Exponential Random Number Generator")
303 gsl_histogram* h = gsl_histogram_alloc(
N_BINS);
309 gsl_histogram_set_ranges(h, range,
N_BINS + 1);
315 for (uint32_t i = 0; i <
N_BINS; ++i)
317 expected[i] = gsl_cdf_exponential_P(range[i + 1], mu) - gsl_cdf_exponential_P(range[i], mu);
323 gsl_histogram_increment(h, e->
GetValue());
328 for (uint32_t i = 0; i <
N_BINS; ++i)
330 tmp[i] = gsl_histogram_get(h, i);
331 tmp[i] -= expected[i];
333 tmp[i] /= expected[i];
336 gsl_histogram_free(h);
338 double chiSquared = 0;
340 for (uint32_t i = 0; i <
N_BINS; ++i)
342 chiSquared += tmp[i];
351 RngSeedManager::SetSeed(
static_cast<uint32_t
>(time(
nullptr)));
354 double maxStatistic = gsl_cdf_chisq_Qinv(0.05,
N_BINS);
356 for (uint32_t i = 0; i <
N_RUNS; ++i)
394 void DoRun()
override;
398 :
TestCase(
"Pareto Random Number Generator")
409 gsl_histogram* h = gsl_histogram_alloc(
N_BINS);
415 gsl_histogram_set_ranges(h, range,
N_BINS + 1);
420 double b = 0.33333333;
424 for (uint32_t i = 0; i <
N_BINS; ++i)
426 expected[i] = gsl_cdf_pareto_P(range[i + 1], a, b) - gsl_cdf_pareto_P(range[i], a, b);
432 gsl_histogram_increment(h, p->GetValue());
437 for (uint32_t i = 0; i <
N_BINS; ++i)
439 tmp[i] = gsl_histogram_get(h, i);
440 tmp[i] -= expected[i];
442 tmp[i] /= expected[i];
445 gsl_histogram_free(h);
447 double chiSquared = 0;
449 for (uint32_t i = 0; i <
N_BINS; ++i)
451 chiSquared += tmp[i];
460 RngSeedManager::SetSeed(
static_cast<uint32_t
>(time(
nullptr)));
463 double maxStatistic = gsl_cdf_chisq_Qinv(0.05,
N_BINS);
465 for (uint32_t i = 0; i <
N_RUNS; ++i)
491 :
TestSuite(
"random-number-generators", UNIT)
Test case for exponential distribution random number generator.
~RngExponentialTestCase() override
static const uint32_t N_BINS
Number of bins.
void DoRun() override
Implementation to actually run this TestCase.
static const uint32_t N_MEASUREMENTS
Number of measurements.
double ChiSquaredTest(Ptr< ExponentialRandomVariable > n)
Run a chi-squared test on the results of the random number generator.
static const uint32_t N_RUNS
Number of runs.
Test case for normal distribution random number generator.
~RngNormalTestCase() override
double ChiSquaredTest(Ptr< NormalRandomVariable > n)
Run a chi-squared test on the results of the random number generator.
static const uint32_t N_MEASUREMENTS
Number of measurements.
static const uint32_t N_RUNS
Number of runs.
void DoRun() override
Implementation to actually run this TestCase.
static const uint32_t N_BINS
Number of bins.
Test case for pareto distribution random number generator.
static const uint32_t N_RUNS
Number of runs.
~RngParetoTestCase() override
double ChiSquaredTest(Ptr< ParetoRandomVariable > p)
Run a chi-squared test on the results of the random number generator.
void DoRun() override
Implementation to actually run this TestCase.
static const uint32_t N_BINS
Number of bins.
static const uint32_t N_MEASUREMENTS
Number of measurements.
The random number generators Test Suite.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
double GetValue(double mean, double bound)
Get the next random value drawn from the distribution.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
void FillHistoRangeUniformly(double *array, uint32_t n, double start, double end)
Fill an array with increasing values, in the [start, end] range.
#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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static RngTestSuite g_rngTestSuite
Static variable for test initialization.