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")
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 ());
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 RngSeedManager::SetSeed (
static_cast<uint32_t
> (time (0)));
111 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
113 for (uint32_t i = 0; i <
N_RUNS; ++i)
141 virtual void DoRun (
void);
145 :
TestCase (
"Normal Random Number Generator")
154 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
161 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
167 for (uint32_t i = 0; i <
N_BINS; ++i)
169 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
175 gsl_histogram_increment (h,
n->GetValue ());
180 for (uint32_t i = 0; i <
N_BINS; ++i)
182 tmp[i] = gsl_histogram_get (h, i);
183 tmp[i] -= expected[i];
185 tmp[i] /= expected[i];
188 gsl_histogram_free (h);
190 double chiSquared = 0;
192 for (uint32_t i = 0; i <
N_BINS; ++i)
194 chiSquared += tmp[i];
203 RngSeedManager::SetSeed (
static_cast<uint32_t
> (time (0)));
206 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
208 for (uint32_t i = 0; i <
N_RUNS; ++i)
236 virtual void DoRun (
void);
240 :
TestCase (
"Exponential Random Number Generator")
249 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
255 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
261 for (uint32_t i = 0; i <
N_BINS; ++i)
263 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
269 gsl_histogram_increment (h, e->
GetValue ());
274 for (uint32_t i = 0; i <
N_BINS; ++i)
276 tmp[i] = gsl_histogram_get (h, i);
277 tmp[i] -= expected[i];
279 tmp[i] /= expected[i];
282 gsl_histogram_free (h);
284 double chiSquared = 0;
286 for (uint32_t i = 0; i <
N_BINS; ++i)
288 chiSquared += tmp[i];
297 RngSeedManager::SetSeed (
static_cast<uint32_t
> (time (0)));
300 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
302 for (uint32_t i = 0; i <
N_RUNS; ++i)
330 virtual void DoRun (
void);
334 :
TestCase (
"Pareto Random Number Generator")
343 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
349 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
354 double b = 0.33333333;
358 for (uint32_t i = 0; i <
N_BINS; ++i)
360 expected[i] = gsl_cdf_pareto_P (range[i + 1], a, b) - gsl_cdf_pareto_P (range[i], a, b);
366 gsl_histogram_increment (h, p->
GetValue ());
371 for (uint32_t i = 0; i <
N_BINS; ++i)
373 tmp[i] = gsl_histogram_get (h, i);
374 tmp[i] -= expected[i];
376 tmp[i] /= expected[i];
379 gsl_histogram_free (h);
381 double chiSquared = 0;
383 for (uint32_t i = 0; i <
N_BINS; ++i)
385 chiSquared += tmp[i];
394 RngSeedManager::SetSeed (
static_cast<uint32_t
> (time (0)));
397 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
399 for (uint32_t i = 0; i <
N_RUNS; ++i)
420 :
TestSuite (
"random-number-generators", UNIT)