25 #include <gsl/gsl_cdf.h>
26 #include <gsl/gsl_histogram.h>
27 #include <gsl/gsl_sf_zeta.h>
32 #include "ns3/boolean.h"
33 #include "ns3/double.h"
34 #include "ns3/string.h"
35 #include "ns3/integer.h"
37 #include "ns3/rng-seed-manager.h"
38 #include "ns3/random-variable-stream.h"
47 double increment = (end -
start) / (n - 1.);
50 for (uint32_t i = 0; i < n; ++i)
65 static const uint32_t N_RUNS = 5;
66 static const uint32_t N_BINS = 50;
67 static const uint32_t N_MEASUREMENTS = 1000000;
75 virtual void DoRun (
void);
79 :
TestCase (
"Uniform Random Variable Stream Generator")
90 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
94 gsl_histogram_set_ranges_uniform (h, 0., 1.);
98 gsl_histogram_increment (h, u->
GetValue ());
103 double expected = ((double)N_MEASUREMENTS / (
double)
N_BINS);
105 for (uint32_t i = 0; i <
N_BINS; ++i)
107 tmp[i] = gsl_histogram_get (h, i);
113 gsl_histogram_free (h);
115 double chiSquared = 0;
117 for (uint32_t i = 0; i <
N_BINS; ++i)
119 chiSquared += tmp[i];
128 SeedManager::SetSeed (time (0));
131 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
133 for (uint32_t i = 0; i <
N_RUNS; ++i)
140 sum /= (double)N_RUNS;
184 virtual void DoRun (
void);
188 :
TestCase (
"Antithetic Uniform Random Variable Stream Generator")
199 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
203 gsl_histogram_set_ranges_uniform (h, 0., 1.);
207 gsl_histogram_increment (h, u->
GetValue ());
212 double expected = ((double)N_MEASUREMENTS / (
double)
N_BINS);
214 for (uint32_t i = 0; i <
N_BINS; ++i)
216 tmp[i] = gsl_histogram_get (h, i);
222 gsl_histogram_free (h);
224 double chiSquared = 0;
226 for (uint32_t i = 0; i <
N_BINS; ++i)
228 chiSquared += tmp[i];
237 SeedManager::SetSeed (time (0));
240 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
242 for (uint32_t i = 0; i <
N_RUNS; ++i)
253 sum /= (double)N_RUNS;
297 virtual void DoRun (
void);
301 :
TestCase (
"Constant Random Variable Stream Generator")
312 SeedManager::SetSeed (time (0));
345 virtual void DoRun (
void);
349 :
TestCase (
"Sequential Random Variable Stream Generator")
360 SeedManager::SetSeed (time (0));
407 virtual void DoRun (
void);
411 :
TestCase (
"Normal Random Variable Stream Generator")
422 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
426 range[0] = -std::numeric_limits<double>::max ();
427 range[
N_BINS] = std::numeric_limits<double>::max ();
429 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
438 for (uint32_t i = 0; i <
N_BINS; ++i)
440 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
446 gsl_histogram_increment (h, n->
GetValue ());
451 for (uint32_t i = 0; i <
N_BINS; ++i)
453 tmp[i] = gsl_histogram_get (h, i);
454 tmp[i] -= expected[i];
456 tmp[i] /= expected[i];
459 gsl_histogram_free (h);
461 double chiSquared = 0;
463 for (uint32_t i = 0; i <
N_BINS; ++i)
465 chiSquared += tmp[i];
474 SeedManager::SetSeed (time (0));
477 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
479 for (uint32_t i = 0; i <
N_RUNS; ++i)
486 sum /= (double)N_RUNS;
491 double variance = 2.0;
510 double expectedMean = mean;
533 virtual void DoRun (
void);
537 :
TestCase (
"Antithetic Normal Random Variable Stream Generator")
548 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
552 range[0] = -std::numeric_limits<double>::max ();
553 range[
N_BINS] = std::numeric_limits<double>::max ();
555 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
564 for (uint32_t i = 0; i <
N_BINS; ++i)
566 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
572 gsl_histogram_increment (h, n->
GetValue ());
577 for (uint32_t i = 0; i <
N_BINS; ++i)
579 tmp[i] = gsl_histogram_get (h, i);
580 tmp[i] -= expected[i];
582 tmp[i] /= expected[i];
585 gsl_histogram_free (h);
587 double chiSquared = 0;
589 for (uint32_t i = 0; i <
N_BINS; ++i)
591 chiSquared += tmp[i];
600 SeedManager::SetSeed (time (0));
603 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
605 for (uint32_t i = 0; i <
N_RUNS; ++i)
616 sum /= (double)N_RUNS;
621 double variance = 2.0;
643 double expectedMean = mean;
666 virtual void DoRun (
void);
670 :
TestCase (
"Exponential Random Variable Stream Generator")
681 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
685 range[
N_BINS] = std::numeric_limits<double>::max ();
687 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
695 for (uint32_t i = 0; i <
N_BINS; ++i)
697 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
703 gsl_histogram_increment (h, e->
GetValue ());
708 for (uint32_t i = 0; i <
N_BINS; ++i)
710 tmp[i] = gsl_histogram_get (h, i);
711 tmp[i] -= expected[i];
713 tmp[i] /= expected[i];
716 gsl_histogram_free (h);
718 double chiSquared = 0;
720 for (uint32_t i = 0; i <
N_BINS; ++i)
722 chiSquared += tmp[i];
731 SeedManager::SetSeed (time (0));
734 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
736 for (uint32_t i = 0; i <
N_RUNS; ++i)
743 sum /= (double)N_RUNS;
786 virtual void DoRun (
void);
790 :
TestCase (
"Antithetic Exponential Random Variable Stream Generator")
801 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
805 range[
N_BINS] = std::numeric_limits<double>::max ();
807 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
815 for (uint32_t i = 0; i <
N_BINS; ++i)
817 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
823 gsl_histogram_increment (h, e->
GetValue ());
828 for (uint32_t i = 0; i <
N_BINS; ++i)
830 tmp[i] = gsl_histogram_get (h, i);
831 tmp[i] -= expected[i];
833 tmp[i] /= expected[i];
836 gsl_histogram_free (h);
838 double chiSquared = 0;
840 for (uint32_t i = 0; i <
N_BINS; ++i)
842 chiSquared += tmp[i];
851 SeedManager::SetSeed (time (0));
854 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
856 for (uint32_t i = 0; i <
N_RUNS; ++i)
867 sum /= (double)N_RUNS;
913 virtual void DoRun (
void);
917 :
TestCase (
"Pareto Random Variable Stream Generator")
928 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
932 range[
N_BINS] = std::numeric_limits<double>::max ();
934 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
942 double scale = mean * (shape - 1.0) / shape;
944 for (uint32_t i = 0; i <
N_BINS; ++i)
946 expected[i] = gsl_cdf_pareto_P (range[i + 1], shape, scale) - gsl_cdf_pareto_P (range[i], shape, scale);
952 gsl_histogram_increment (h, p->
GetValue ());
957 for (uint32_t i = 0; i <
N_BINS; ++i)
959 tmp[i] = gsl_histogram_get (h, i);
960 tmp[i] -= expected[i];
962 tmp[i] /= expected[i];
965 gsl_histogram_free (h);
967 double chiSquared = 0;
969 for (uint32_t i = 0; i <
N_BINS; ++i)
971 chiSquared += tmp[i];
980 SeedManager::SetSeed (time (0));
983 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
985 for (uint32_t i = 0; i <
N_RUNS; ++i)
992 sum /= (double)N_RUNS;
998 double scale = mean * (shape - 1.0) / shape;
1024 double expectedMean = (shape * scale) / (shape - 1.0);
1047 virtual void DoRun (
void);
1051 :
TestCase (
"Antithetic Pareto Random Variable Stream Generator")
1062 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1064 double range[
N_BINS + 1];
1066 range[
N_BINS] = std::numeric_limits<double>::max ();
1068 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1076 double scale = mean * (shape - 1.0) / shape;
1078 for (uint32_t i = 0; i <
N_BINS; ++i)
1080 expected[i] = gsl_cdf_pareto_P (range[i + 1], shape, scale) - gsl_cdf_pareto_P (range[i], shape, scale);
1086 gsl_histogram_increment (h, p->
GetValue ());
1091 for (uint32_t i = 0; i <
N_BINS; ++i)
1093 tmp[i] = gsl_histogram_get (h, i);
1094 tmp[i] -= expected[i];
1096 tmp[i] /= expected[i];
1099 gsl_histogram_free (h);
1101 double chiSquared = 0;
1103 for (uint32_t i = 0; i <
N_BINS; ++i)
1105 chiSquared += tmp[i];
1114 SeedManager::SetSeed (time (0));
1117 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1119 for (uint32_t i = 0; i <
N_RUNS; ++i)
1130 sum /= (double)N_RUNS;
1136 double scale = mean * (shape - 1.0) / shape;
1166 double expectedMean = (shape * scale) / (shape - 1.0);
1189 virtual void DoRun (
void);
1193 :
TestCase (
"Weibull Random Variable Stream Generator")
1204 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1206 double range[
N_BINS + 1];
1208 range[
N_BINS] = std::numeric_limits<double>::max ();
1210 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1220 for (uint32_t i = 0; i <
N_BINS; ++i)
1222 expected[i] = gsl_cdf_weibull_P (range[i + 1], a, b) - gsl_cdf_weibull_P (range[i], a, b);
1228 gsl_histogram_increment (h, p->
GetValue ());
1233 for (uint32_t i = 0; i <
N_BINS; ++i)
1235 tmp[i] = gsl_histogram_get (h, i);
1236 tmp[i] -= expected[i];
1238 tmp[i] /= expected[i];
1241 gsl_histogram_free (h);
1243 double chiSquared = 0;
1245 for (uint32_t i = 0; i <
N_BINS; ++i)
1247 chiSquared += tmp[i];
1256 SeedManager::SetSeed (time (0));
1259 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1261 for (uint32_t i = 0; i <
N_RUNS; ++i)
1268 sum /= (double)N_RUNS;
1312 double expectedMean = scale;
1335 virtual void DoRun (
void);
1339 :
TestCase (
"Antithetic Weibull Random Variable Stream Generator")
1350 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1352 double range[
N_BINS + 1];
1354 range[
N_BINS] = std::numeric_limits<double>::max ();
1356 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1366 for (uint32_t i = 0; i <
N_BINS; ++i)
1368 expected[i] = gsl_cdf_weibull_P (range[i + 1], a, b) - gsl_cdf_weibull_P (range[i], a, b);
1374 gsl_histogram_increment (h, p->
GetValue ());
1379 for (uint32_t i = 0; i <
N_BINS; ++i)
1381 tmp[i] = gsl_histogram_get (h, i);
1382 tmp[i] -= expected[i];
1384 tmp[i] /= expected[i];
1387 gsl_histogram_free (h);
1389 double chiSquared = 0;
1391 for (uint32_t i = 0; i <
N_BINS; ++i)
1393 chiSquared += tmp[i];
1402 SeedManager::SetSeed (time (0));
1405 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1407 for (uint32_t i = 0; i <
N_RUNS; ++i)
1418 sum /= (double)N_RUNS;
1465 double expectedMean = scale;
1488 virtual void DoRun (
void);
1492 :
TestCase (
"Log-Normal Random Variable Stream Generator")
1503 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1505 double range[
N_BINS + 1];
1507 range[
N_BINS] = std::numeric_limits<double>::max ();
1509 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1519 for (uint32_t i = 0; i <
N_BINS; ++i)
1521 expected[i] = gsl_cdf_lognormal_P (range[i + 1], mu, sigma) - gsl_cdf_lognormal_P (range[i], mu, sigma);
1527 gsl_histogram_increment (h, n->
GetValue ());
1532 for (uint32_t i = 0; i <
N_BINS; ++i)
1534 tmp[i] = gsl_histogram_get (h, i);
1535 tmp[i] -= expected[i];
1537 tmp[i] /= expected[i];
1540 gsl_histogram_free (h);
1542 double chiSquared = 0;
1544 for (uint32_t i = 0; i <
N_BINS; ++i)
1546 chiSquared += tmp[i];
1555 SeedManager::SetSeed (time (0));
1558 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1560 for (uint32_t i = 0; i <
N_RUNS; ++i)
1567 sum /= (double)N_RUNS;
1596 double expectedMean = std::exp(mu + sigma * sigma / 2.0);
1624 virtual void DoRun (
void);
1628 :
TestCase (
"Antithetic Log-Normal Random Variable Stream Generator")
1639 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1641 double range[
N_BINS + 1];
1643 range[
N_BINS] = std::numeric_limits<double>::max ();
1645 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1655 for (uint32_t i = 0; i <
N_BINS; ++i)
1657 expected[i] = gsl_cdf_lognormal_P (range[i + 1], mu, sigma) - gsl_cdf_lognormal_P (range[i], mu, sigma);
1663 gsl_histogram_increment (h, n->
GetValue ());
1668 for (uint32_t i = 0; i <
N_BINS; ++i)
1670 tmp[i] = gsl_histogram_get (h, i);
1671 tmp[i] -= expected[i];
1673 tmp[i] /= expected[i];
1676 gsl_histogram_free (h);
1678 double chiSquared = 0;
1680 for (uint32_t i = 0; i <
N_BINS; ++i)
1682 chiSquared += tmp[i];
1691 SeedManager::SetSeed (time (0));
1694 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1696 for (uint32_t i = 0; i <
N_RUNS; ++i)
1707 sum /= (double)N_RUNS;
1739 double expectedMean = std::exp(mu + sigma * sigma / 2.0);
1767 virtual void DoRun (
void);
1771 :
TestCase (
"Gamma Random Variable Stream Generator")
1782 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1784 double range[
N_BINS + 1];
1786 range[
N_BINS] = std::numeric_limits<double>::max ();
1788 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1798 for (uint32_t i = 0; i <
N_BINS; ++i)
1800 expected[i] = gsl_cdf_gamma_P (range[i + 1], alpha, beta) - gsl_cdf_gamma_P (range[i], alpha, beta);
1806 gsl_histogram_increment (h, n->
GetValue ());
1811 for (uint32_t i = 0; i <
N_BINS; ++i)
1813 tmp[i] = gsl_histogram_get (h, i);
1814 tmp[i] -= expected[i];
1816 tmp[i] /= expected[i];
1819 gsl_histogram_free (h);
1821 double chiSquared = 0;
1823 for (uint32_t i = 0; i <
N_BINS; ++i)
1825 chiSquared += tmp[i];
1834 SeedManager::SetSeed (time (0));
1837 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1839 for (uint32_t i = 0; i <
N_RUNS; ++i)
1846 sum /= (double)N_RUNS;
1873 double expectedMean = alpha * beta;
1896 virtual void DoRun (
void);
1900 :
TestCase (
"Antithetic Gamma Random Variable Stream Generator")
1911 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1913 double range[
N_BINS + 1];
1915 range[
N_BINS] = std::numeric_limits<double>::max ();
1917 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1927 for (uint32_t i = 0; i <
N_BINS; ++i)
1929 expected[i] = gsl_cdf_gamma_P (range[i + 1], alpha, beta) - gsl_cdf_gamma_P (range[i], alpha, beta);
1935 gsl_histogram_increment (h, n->
GetValue ());
1940 for (uint32_t i = 0; i <
N_BINS; ++i)
1942 tmp[i] = gsl_histogram_get (h, i);
1943 tmp[i] -= expected[i];
1945 tmp[i] /= expected[i];
1948 gsl_histogram_free (h);
1950 double chiSquared = 0;
1952 for (uint32_t i = 0; i <
N_BINS; ++i)
1954 chiSquared += tmp[i];
1963 SeedManager::SetSeed (time (0));
1966 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1968 for (uint32_t i = 0; i <
N_RUNS; ++i)
1979 sum /= (double)N_RUNS;
2010 double expectedMean = alpha * beta;
2033 virtual void DoRun (
void);
2037 :
TestCase (
"Erlang Random Variable Stream Generator")
2048 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
2050 double range[
N_BINS + 1];
2052 range[
N_BINS] = std::numeric_limits<double>::max ();
2054 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
2062 double lambda = 1.0;
2067 for (uint32_t i = 0; i <
N_BINS; ++i)
2069 expected[i] = gsl_cdf_gamma_P (range[i + 1], k, lambda) - gsl_cdf_gamma_P (range[i], k, lambda);
2075 gsl_histogram_increment (h, n->
GetValue ());
2080 for (uint32_t i = 0; i <
N_BINS; ++i)
2082 tmp[i] = gsl_histogram_get (h, i);
2083 tmp[i] -= expected[i];
2085 tmp[i] /= expected[i];
2088 gsl_histogram_free (h);
2090 double chiSquared = 0;
2092 for (uint32_t i = 0; i <
N_BINS; ++i)
2094 chiSquared += tmp[i];
2103 SeedManager::SetSeed (time (0));
2106 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
2108 for (uint32_t i = 0; i <
N_RUNS; ++i)
2115 sum /= (double)N_RUNS;
2120 double lambda = 2.0;
2142 double expectedMean = k * lambda;
2165 virtual void DoRun (
void);
2169 :
TestCase (
"Antithetic Erlang Random Variable Stream Generator")
2180 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
2182 double range[
N_BINS + 1];
2184 range[
N_BINS] = std::numeric_limits<double>::max ();
2186 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
2194 double lambda = 1.0;
2199 for (uint32_t i = 0; i <
N_BINS; ++i)
2201 expected[i] = gsl_cdf_gamma_P (range[i + 1], k, lambda) - gsl_cdf_gamma_P (range[i], k, lambda);
2207 gsl_histogram_increment (h, n->
GetValue ());
2212 for (uint32_t i = 0; i <
N_BINS; ++i)
2214 tmp[i] = gsl_histogram_get (h, i);
2215 tmp[i] -= expected[i];
2217 tmp[i] /= expected[i];
2220 gsl_histogram_free (h);
2222 double chiSquared = 0;
2224 for (uint32_t i = 0; i <
N_BINS; ++i)
2226 chiSquared += tmp[i];
2235 SeedManager::SetSeed (time (0));
2238 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
2240 for (uint32_t i = 0; i <
N_RUNS; ++i)
2251 sum /= (double)N_RUNS;
2256 double lambda = 2.0;
2282 double expectedMean = k * lambda;
2301 virtual void DoRun (
void);
2305 :
TestCase (
"Zipf Random Variable Stream Generator")
2316 SeedManager::SetSeed (time (0));
2364 double expectedMean = 1.0;
2383 virtual void DoRun (
void);
2387 :
TestCase (
"Antithetic Zipf Random Variable Stream Generator")
2398 SeedManager::SetSeed (time (0));
2449 double expectedMean = 1.0;
2468 virtual void DoRun (
void);
2472 :
TestCase (
"Zeta Random Variable Stream Generator")
2483 SeedManager::SetSeed (time (0));
2513 double expectedMean = gsl_sf_zeta_int (alpha - 1) / gsl_sf_zeta_int (alpha);
2532 virtual void DoRun (
void);
2536 :
TestCase (
"Antithetic Zeta Random Variable Stream Generator")
2547 SeedManager::SetSeed (time (0));
2580 double expectedMean = gsl_sf_zeta_int (alpha - 1) / gsl_sf_zeta_int (alpha);
2599 virtual void DoRun (
void);
2603 :
TestCase (
"Deterministic Random Variable Stream Generator")
2614 SeedManager::SetSeed (time (0));
2622 double array1 [] = { 4, 4, 7, 7, 10, 10};
2623 uint64_t count1 = 6;
2646 double array2 [] = { 1000, 2000, 3000, 4000};
2647 uint64_t count2 = 4;
2674 virtual void DoRun (
void);
2678 :
TestCase (
"Empirical Random Variable Stream Generator")
2689 SeedManager::SetSeed (time (0));
2712 double expectedMean = 5.0;
2731 virtual void DoRun (
void);
2735 :
TestCase (
"EmpiricalAntithetic Random Variable Stream Generator")
2746 SeedManager::SetSeed (time (0));
2772 double expectedMean = 5.0;
2786 :
TestSuite (
"random-variable-stream-generators", UNIT)