19 #include <gsl/gsl_cdf.h>
20 #include <gsl/gsl_histogram.h>
25 #include "ns3/random-variable.h"
30 FillHistoRangeUniformly (
double *array, uint32_t n,
double start,
double end)
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);
61 RngUniformTestCase::RngUniformTestCase ()
62 :
TestCase (
"Uniform Random Number Generator")
66 RngUniformTestCase::~RngUniformTestCase ()
73 gsl_histogram * h = gsl_histogram_alloc (N_BINS);
74 gsl_histogram_set_ranges_uniform (h, 0., 1.);
76 for (uint32_t i = 0; i < N_MEASUREMENTS; ++i)
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)
116 double result = ChiSquaredTest (u);
120 sum /= (double)N_RUNS;
122 NS_TEST_ASSERT_MSG_LT (sum, maxStatistic,
"Chi-squared statistic out of range");
131 static const uint32_t N_RUNS = 5;
132 static const uint32_t N_BINS = 50;
133 static const uint32_t N_MEASUREMENTS = 1000000;
141 virtual void DoRun (
void);
144 RngNormalTestCase::RngNormalTestCase ()
145 :
TestCase (
"Normal Random Number Generator")
149 RngNormalTestCase::~RngNormalTestCase ()
156 gsl_histogram * h = gsl_histogram_alloc (N_BINS);
158 double range[N_BINS + 1];
159 FillHistoRangeUniformly (range, N_BINS + 1, -4., 4.);
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);
165 double expected[N_BINS];
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);
172 expected[i] *= N_MEASUREMENTS;
175 for (uint32_t i = 0; i < N_MEASUREMENTS; ++i)
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)
213 double result = ChiSquaredTest (n);
217 sum /= (double)N_RUNS;
219 NS_TEST_ASSERT_MSG_LT (sum, maxStatistic,
"Chi-squared statistic out of range");
228 static const uint32_t N_RUNS = 5;
229 static const uint32_t N_BINS = 50;
230 static const uint32_t N_MEASUREMENTS = 1000000;
238 virtual void DoRun (
void);
241 RngExponentialTestCase::RngExponentialTestCase ()
242 :
TestCase (
"Exponential Random Number Generator")
246 RngExponentialTestCase::~RngExponentialTestCase ()
253 gsl_histogram * h = gsl_histogram_alloc (N_BINS);
255 double range[N_BINS + 1];
256 FillHistoRangeUniformly (range, N_BINS + 1, 0., 10.);
257 range[N_BINS] = std::numeric_limits<double>::max ();
259 gsl_histogram_set_ranges (h, range, N_BINS + 1);
261 double expected[N_BINS];
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);
268 expected[i] *= N_MEASUREMENTS;
271 for (uint32_t i = 0; i < N_MEASUREMENTS; ++i)
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)
309 double result = ChiSquaredTest (e);
313 sum /= (double)N_RUNS;
315 NS_TEST_ASSERT_MSG_LT (sum, maxStatistic,
"Chi-squared statistic out of range");
324 static const uint32_t N_RUNS = 5;
325 static const uint32_t N_BINS = 50;
326 static const uint32_t N_MEASUREMENTS = 1000000;
334 virtual void DoRun (
void);
337 RngParetoTestCase::RngParetoTestCase ()
338 :
TestCase (
"Pareto Random Number Generator")
342 RngParetoTestCase::~RngParetoTestCase ()
349 gsl_histogram * h = gsl_histogram_alloc (N_BINS);
351 double range[N_BINS + 1];
352 FillHistoRangeUniformly (range, N_BINS + 1, 1., 10.);
353 range[N_BINS] = std::numeric_limits<double>::max ();
355 gsl_histogram_set_ranges (h, range, N_BINS + 1);
357 double expected[N_BINS];
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);
365 expected[i] *= N_MEASUREMENTS;
368 for (uint32_t i = 0; i < N_MEASUREMENTS; ++i)
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)
406 double result = ChiSquaredTest (e);
410 sum /= (double)N_RUNS;
412 NS_TEST_ASSERT_MSG_LT (sum, maxStatistic,
"Chi-squared statistic out of range");
421 RngTestSuite::RngTestSuite ()
422 :
TestSuite (
"random-number-generators", UNIT)
virtual void DoRun(void)
Implementation to actually run this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
ParetoVariable distributed random varThis class supports the creation of objects that return random n...
Class NormalVariable defines a random variable with a normal (Gaussian) distribution.This class supports the creation of objects that return random numbers from a fixed normal distribution. It also supports the generation of single random numbers from various normal distributions.
virtual void DoRun(void)
Implementation to actually run this test case.
Exponentially Distributed random varThis class supports the creation of objects that return random nu...
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
double GetValue(void) const
Returns a random double from the underlying distribution.