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"
54 double increment = (end -
start) / (n - 1.);
57 for (uint32_t i = 0; i < n; ++i)
73 static const uint32_t N_RUNS = 5;
75 static const uint32_t N_BINS = 50;
77 static const uint32_t N_MEASUREMENTS = 1000000;
90 virtual void DoRun (
void);
94 :
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 (0)));
141 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
143 for (uint32_t i = 0; i <
N_RUNS; ++i)
181 virtual void DoRun (
void);
185 :
TestCase (
"Normal Random Number Generator")
194 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
201 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
207 for (uint32_t i = 0; i <
N_BINS; ++i)
209 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
215 gsl_histogram_increment (h, n->GetValue ());
220 for (uint32_t i = 0; i <
N_BINS; ++i)
222 tmp[i] = gsl_histogram_get (h, i);
223 tmp[i] -= expected[i];
225 tmp[i] /= expected[i];
228 gsl_histogram_free (h);
230 double chiSquared = 0;
232 for (uint32_t i = 0; i <
N_BINS; ++i)
234 chiSquared += tmp[i];
243 RngSeedManager::SetSeed (
static_cast<uint32_t
> (time (0)));
246 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
248 for (uint32_t i = 0; i <
N_RUNS; ++i)
286 virtual void DoRun (
void);
290 :
TestCase (
"Exponential Random Number Generator")
299 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
305 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
311 for (uint32_t i = 0; i <
N_BINS; ++i)
313 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
319 gsl_histogram_increment (h, e->
GetValue ());
324 for (uint32_t i = 0; i <
N_BINS; ++i)
326 tmp[i] = gsl_histogram_get (h, i);
327 tmp[i] -= expected[i];
329 tmp[i] /= expected[i];
332 gsl_histogram_free (h);
334 double chiSquared = 0;
336 for (uint32_t i = 0; i <
N_BINS; ++i)
338 chiSquared += tmp[i];
347 RngSeedManager::SetSeed (
static_cast<uint32_t
> (time (0)));
350 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
352 for (uint32_t i = 0; i <
N_RUNS; ++i)
390 virtual void DoRun (
void);
394 :
TestCase (
"Pareto Random Number Generator")
403 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
409 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
414 double b = 0.33333333;
418 for (uint32_t i = 0; i <
N_BINS; ++i)
420 expected[i] = gsl_cdf_pareto_P (range[i + 1], a, b) - gsl_cdf_pareto_P (range[i], a, b);
426 gsl_histogram_increment (h, p->GetValue ());
431 for (uint32_t i = 0; i <
N_BINS; ++i)
433 tmp[i] = gsl_histogram_get (h, i);
434 tmp[i] -= expected[i];
436 tmp[i] /= expected[i];
439 gsl_histogram_free (h);
441 double chiSquared = 0;
443 for (uint32_t i = 0; i <
N_BINS; ++i)
445 chiSquared += tmp[i];
454 RngSeedManager::SetSeed (
static_cast<uint32_t
> (time (0)));
457 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
459 for (uint32_t i = 0; i <
N_RUNS; ++i)
463 e->SetAttribute (
"Scale",
DoubleValue (0.33333333));
485 :
TestSuite (
"random-number-generators", UNIT)
Test case for exponential distribution random number generator.
static const uint32_t N_BINS
Number of bins.
virtual void DoRun(void)
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.
virtual ~RngExponentialTestCase()
Test case for normal distribution random number generator.
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.
virtual ~RngNormalTestCase()
static const uint32_t N_RUNS
Number of runs.
virtual void DoRun(void)
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.
double ChiSquaredTest(Ptr< ParetoRandomVariable > p)
Run a chi-squared test on the results of the random number generator.
static const uint32_t N_BINS
Number of bins.
static const uint32_t N_MEASUREMENTS
Number of measurements.
virtual ~RngParetoTestCase()
virtual void DoRun(void)
Implementation to actually run this TestCase.
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, as a double from the exponential distribution with the specified mean and ...
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.