19 #include <gsl/gsl_cdf.h>
20 #include <gsl/gsl_histogram.h>
25 #include "ns3/random-variable.h"
32 double increment = (end -
start) / (n - 1.);
35 for (uint32_t i = 0; i < n; ++i)
48 static const uint32_t N_RUNS = 5;
49 static const uint32_t N_BINS = 50;
50 static const uint32_t N_MEASUREMENTS = 1000000;
58 virtual void DoRun (
void);
62 :
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 ());
83 double expected = ((double)N_MEASUREMENTS / (
double)
N_BINS);
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 SeedManager::SetSeed (time (0));
111 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
113 for (uint32_t i = 0; i <
N_RUNS; ++i)
120 sum /= (double)N_RUNS;
141 virtual void DoRun (
void);
145 :
TestCase (
"Normal Random Number Generator")
156 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
160 range[0] = -std::numeric_limits<double>::max ();
161 range[
N_BINS] = std::numeric_limits<double>::max ();
163 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
169 for (uint32_t i = 0; i <
N_BINS; ++i)
171 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
177 gsl_histogram_increment (h, n.
GetValue ());
182 for (uint32_t i = 0; i <
N_BINS; ++i)
184 tmp[i] = gsl_histogram_get (h, i);
185 tmp[i] -= expected[i];
187 tmp[i] /= expected[i];
190 gsl_histogram_free (h);
192 double chiSquared = 0;
194 for (uint32_t i = 0; i <
N_BINS; ++i)
196 chiSquared += tmp[i];
205 SeedManager::SetSeed (time (0));
208 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
210 for (uint32_t i = 0; i <
N_RUNS; ++i)
217 sum /= (double)N_RUNS;
238 virtual void DoRun (
void);
242 :
TestCase (
"Exponential Random Number Generator")
253 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
257 range[
N_BINS] = std::numeric_limits<double>::max ();
259 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
265 for (uint32_t i = 0; i <
N_BINS; ++i)
267 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
273 gsl_histogram_increment (h, e.
GetValue ());
278 for (uint32_t i = 0; i <
N_BINS; ++i)
280 tmp[i] = gsl_histogram_get (h, i);
281 tmp[i] -= expected[i];
283 tmp[i] /= expected[i];
286 gsl_histogram_free (h);
288 double chiSquared = 0;
290 for (uint32_t i = 0; i <
N_BINS; ++i)
292 chiSquared += tmp[i];
301 SeedManager::SetSeed (time (0));
304 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
306 for (uint32_t i = 0; i <
N_RUNS; ++i)
313 sum /= (double)N_RUNS;
334 virtual void DoRun (
void);
338 :
TestCase (
"Pareto Random Number Generator")
349 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
353 range[
N_BINS] = std::numeric_limits<double>::max ();
355 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
360 double b = 0.33333333;
362 for (uint32_t i = 0; i <
N_BINS; ++i)
364 expected[i] = gsl_cdf_pareto_P (range[i + 1], a, b) - gsl_cdf_pareto_P (range[i], a, b);
370 gsl_histogram_increment (h, p.
GetValue ());
375 for (uint32_t i = 0; i <
N_BINS; ++i)
377 tmp[i] = gsl_histogram_get (h, i);
378 tmp[i] -= expected[i];
380 tmp[i] /= expected[i];
383 gsl_histogram_free (h);
385 double chiSquared = 0;
387 for (uint32_t i = 0; i <
N_BINS; ++i)
389 chiSquared += tmp[i];
398 SeedManager::SetSeed (time (0));
401 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
403 for (uint32_t i = 0; i <
N_RUNS; ++i)
410 sum /= (double)N_RUNS;
422 :
TestSuite (
"random-number-generators", UNIT)
virtual void DoRun(void)
Implementation to actually run this TestCase.
void FillHistoRangeUniformly(double *array, uint32_t n, double start, double end)
virtual ~RngExponentialTestCase()
virtual void DoRun(void)
Implementation to actually run this TestCase.
ParetoVariable distributed random varThis class supports the creation of objects that return random n...
static const uint32_t N_MEASUREMENTS
static RngTestSuite rngTestSuite
virtual ~RngParetoTestCase()
Class NormalVariable defines a random variable with a normal (Gaussian) distribution.
double ChiSquaredTest(ExponentialVariable &n)
static const uint32_t N_RUNS
static const uint32_t N_RUNS
virtual void DoRun(void)
Implementation to actually run this TestCase.
static const uint32_t N_MEASUREMENTS
static const uint32_t N_BINS
Exponentially Distributed random varThis class supports the creation of objects that return random nu...
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
static const uint32_t N_MEASUREMENTS
static const uint32_t N_BINS
double ChiSquaredTest(NormalVariable &n)
static const uint32_t N_BINS
#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.
virtual ~RngNormalTestCase()
double GetValue(void) const
Returns a random double from the underlying distribution.
double ChiSquaredTest(ParetoVariable &p)
static const uint32_t N_RUNS