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")
75 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
76 gsl_histogram_set_ranges_uniform (h, 0., 1.);
80 gsl_histogram_increment (h, u->
GetValue ());
85 double expected = ((double)N_MEASUREMENTS / (
double)
N_BINS);
87 for (uint32_t i = 0; i <
N_BINS; ++i)
89 tmp[i] = gsl_histogram_get (h, i);
95 gsl_histogram_free (h);
97 double chiSquared = 0;
99 for (uint32_t i = 0; i <
N_BINS; ++i)
101 chiSquared += tmp[i];
110 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
113 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
115 for (uint32_t i = 0; i <
N_RUNS; ++i)
122 sum /= (double)N_RUNS;
143 virtual void DoRun (
void);
147 :
TestCase (
"Normal Random Number Generator")
158 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
162 range[0] = -std::numeric_limits<double>::max ();
163 range[
N_BINS] = std::numeric_limits<double>::max ();
165 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
171 for (uint32_t i = 0; i <
N_BINS; ++i)
173 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
179 gsl_histogram_increment (h, n->
GetValue ());
184 for (uint32_t i = 0; i <
N_BINS; ++i)
186 tmp[i] = gsl_histogram_get (h, i);
187 tmp[i] -= expected[i];
189 tmp[i] /= expected[i];
192 gsl_histogram_free (h);
194 double chiSquared = 0;
196 for (uint32_t i = 0; i <
N_BINS; ++i)
198 chiSquared += tmp[i];
207 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
210 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
212 for (uint32_t i = 0; i <
N_RUNS; ++i)
219 sum /= (double)N_RUNS;
240 virtual void DoRun (
void);
244 :
TestCase (
"Exponential Random Number Generator")
255 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
259 range[
N_BINS] = std::numeric_limits<double>::max ();
261 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
267 for (uint32_t i = 0; i <
N_BINS; ++i)
269 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
275 gsl_histogram_increment (h, e->
GetValue ());
280 for (uint32_t i = 0; i <
N_BINS; ++i)
282 tmp[i] = gsl_histogram_get (h, i);
283 tmp[i] -= expected[i];
285 tmp[i] /= expected[i];
288 gsl_histogram_free (h);
290 double chiSquared = 0;
292 for (uint32_t i = 0; i <
N_BINS; ++i)
294 chiSquared += tmp[i];
303 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
306 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
308 for (uint32_t i = 0; i <
N_RUNS; ++i)
315 sum /= (double)N_RUNS;
336 virtual void DoRun (
void);
340 :
TestCase (
"Pareto Random Number Generator")
351 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
355 range[
N_BINS] = std::numeric_limits<double>::max ();
357 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
362 double b = 0.33333333;
366 for (uint32_t i = 0; i <
N_BINS; ++i)
368 expected[i] = gsl_cdf_pareto_P (range[i + 1], a, b) - gsl_cdf_pareto_P (range[i], a, b);
374 gsl_histogram_increment (h, p->
GetValue ());
379 for (uint32_t i = 0; i <
N_BINS; ++i)
381 tmp[i] = gsl_histogram_get (h, i);
382 tmp[i] -= expected[i];
384 tmp[i] /= expected[i];
387 gsl_histogram_free (h);
389 double chiSquared = 0;
391 for (uint32_t i = 0; i <
N_BINS; ++i)
393 chiSquared += tmp[i];
402 RngSeedManager::SetSeed (static_cast<uint32_t> (time (0)));
405 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
407 for (uint32_t i = 0; i <
N_RUNS; ++i)
415 sum /= (double)N_RUNS;
427 :
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.
double GetValue(double mean, double shape, double bound)
Returns a random double from a Pareto distribution with the specified mean, shape, and upper bound.
double GetValue(double mean, double variance, double bound=NormalRandomVariable::INFINITE_VALUE)
Returns a random double from a normal distribution with the specified mean, variance, and bound.
static const uint32_t N_MEASUREMENTS
static RngTestSuite rngTestSuite
virtual ~RngParetoTestCase()
static const uint32_t N_RUNS
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
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
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)
Returns a random double from an exponential distribution with the specified mean 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