26 #include <gsl/gsl_cdf.h>
27 #include <gsl/gsl_histogram.h>
28 #include <gsl/gsl_sf_zeta.h>
33 #include "ns3/boolean.h"
34 #include "ns3/double.h"
35 #include "ns3/string.h"
36 #include "ns3/integer.h"
38 #include "ns3/rng-seed-manager.h"
39 #include "ns3/random-variable-stream.h"
48 double increment = (end -
start) / (n - 1.);
51 for (uint32_t i = 0; i < n; ++i)
66 static const uint32_t N_RUNS = 5;
67 static const uint32_t N_BINS = 50;
68 static const uint32_t N_MEASUREMENTS = 1000000;
76 virtual void DoRun (
void);
80 :
TestCase (
"Uniform Random Variable Stream Generator")
91 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
95 gsl_histogram_set_ranges_uniform (h, 0., 1.);
99 gsl_histogram_increment (h, u->
GetValue ());
104 double expected = ((double)N_MEASUREMENTS / (
double)
N_BINS);
106 for (uint32_t i = 0; i <
N_BINS; ++i)
108 tmp[i] = gsl_histogram_get (h, i);
114 gsl_histogram_free (h);
116 double chiSquared = 0;
118 for (uint32_t i = 0; i <
N_BINS; ++i)
120 chiSquared += tmp[i];
129 SeedManager::SetSeed (time (0));
132 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
134 for (uint32_t i = 0; i <
N_RUNS; ++i)
141 sum /= (double)N_RUNS;
185 virtual void DoRun (
void);
189 :
TestCase (
"Antithetic Uniform Random Variable Stream Generator")
200 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
204 gsl_histogram_set_ranges_uniform (h, 0., 1.);
208 gsl_histogram_increment (h, u->
GetValue ());
213 double expected = ((double)N_MEASUREMENTS / (
double)
N_BINS);
215 for (uint32_t i = 0; i <
N_BINS; ++i)
217 tmp[i] = gsl_histogram_get (h, i);
223 gsl_histogram_free (h);
225 double chiSquared = 0;
227 for (uint32_t i = 0; i <
N_BINS; ++i)
229 chiSquared += tmp[i];
238 SeedManager::SetSeed (time (0));
241 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
243 for (uint32_t i = 0; i <
N_RUNS; ++i)
254 sum /= (double)N_RUNS;
298 virtual void DoRun (
void);
302 :
TestCase (
"Constant Random Variable Stream Generator")
313 SeedManager::SetSeed (time (0));
346 virtual void DoRun (
void);
350 :
TestCase (
"Sequential Random Variable Stream Generator")
361 SeedManager::SetSeed (time (0));
408 virtual void DoRun (
void);
412 :
TestCase (
"Normal Random Variable Stream Generator")
423 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
427 range[0] = -std::numeric_limits<double>::max ();
428 range[
N_BINS] = std::numeric_limits<double>::max ();
430 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
439 for (uint32_t i = 0; i <
N_BINS; ++i)
441 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
447 gsl_histogram_increment (h, n->
GetValue ());
452 for (uint32_t i = 0; i <
N_BINS; ++i)
454 tmp[i] = gsl_histogram_get (h, i);
455 tmp[i] -= expected[i];
457 tmp[i] /= expected[i];
460 gsl_histogram_free (h);
462 double chiSquared = 0;
464 for (uint32_t i = 0; i <
N_BINS; ++i)
466 chiSquared += tmp[i];
475 SeedManager::SetSeed (time (0));
478 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
480 for (uint32_t i = 0; i <
N_RUNS; ++i)
487 sum /= (double)N_RUNS;
492 double variance = 2.0;
511 double expectedMean = mean;
534 virtual void DoRun (
void);
538 :
TestCase (
"Antithetic Normal Random Variable Stream Generator")
549 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
553 range[0] = -std::numeric_limits<double>::max ();
554 range[
N_BINS] = std::numeric_limits<double>::max ();
556 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
565 for (uint32_t i = 0; i <
N_BINS; ++i)
567 expected[i] = gsl_cdf_gaussian_P (range[i + 1], sigma) - gsl_cdf_gaussian_P (range[i], sigma);
573 gsl_histogram_increment (h, n->
GetValue ());
578 for (uint32_t i = 0; i <
N_BINS; ++i)
580 tmp[i] = gsl_histogram_get (h, i);
581 tmp[i] -= expected[i];
583 tmp[i] /= expected[i];
586 gsl_histogram_free (h);
588 double chiSquared = 0;
590 for (uint32_t i = 0; i <
N_BINS; ++i)
592 chiSquared += tmp[i];
601 SeedManager::SetSeed (time (0));
604 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
606 for (uint32_t i = 0; i <
N_RUNS; ++i)
617 sum /= (double)N_RUNS;
622 double variance = 2.0;
644 double expectedMean = mean;
667 virtual void DoRun (
void);
671 :
TestCase (
"Exponential Random Variable Stream Generator")
682 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
686 range[
N_BINS] = std::numeric_limits<double>::max ();
688 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
696 for (uint32_t i = 0; i <
N_BINS; ++i)
698 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
704 gsl_histogram_increment (h, e->
GetValue ());
709 for (uint32_t i = 0; i <
N_BINS; ++i)
711 tmp[i] = gsl_histogram_get (h, i);
712 tmp[i] -= expected[i];
714 tmp[i] /= expected[i];
717 gsl_histogram_free (h);
719 double chiSquared = 0;
721 for (uint32_t i = 0; i <
N_BINS; ++i)
723 chiSquared += tmp[i];
732 SeedManager::SetSeed (time (0));
735 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
737 for (uint32_t i = 0; i <
N_RUNS; ++i)
744 sum /= (double)N_RUNS;
787 virtual void DoRun (
void);
791 :
TestCase (
"Antithetic Exponential Random Variable Stream Generator")
802 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
806 range[
N_BINS] = std::numeric_limits<double>::max ();
808 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
816 for (uint32_t i = 0; i <
N_BINS; ++i)
818 expected[i] = gsl_cdf_exponential_P (range[i + 1], mu) - gsl_cdf_exponential_P (range[i], mu);
824 gsl_histogram_increment (h, e->
GetValue ());
829 for (uint32_t i = 0; i <
N_BINS; ++i)
831 tmp[i] = gsl_histogram_get (h, i);
832 tmp[i] -= expected[i];
834 tmp[i] /= expected[i];
837 gsl_histogram_free (h);
839 double chiSquared = 0;
841 for (uint32_t i = 0; i <
N_BINS; ++i)
843 chiSquared += tmp[i];
852 SeedManager::SetSeed (time (0));
855 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
857 for (uint32_t i = 0; i <
N_RUNS; ++i)
868 sum /= (double)N_RUNS;
914 virtual void DoRun (
void);
918 :
TestCase (
"Pareto Random Variable Stream Generator")
929 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
933 range[
N_BINS] = std::numeric_limits<double>::max ();
935 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
943 double scale = mean * (shape - 1.0) / shape;
945 for (uint32_t i = 0; i <
N_BINS; ++i)
947 expected[i] = gsl_cdf_pareto_P (range[i + 1], shape, scale) - gsl_cdf_pareto_P (range[i], shape, scale);
953 gsl_histogram_increment (h, p->
GetValue ());
958 for (uint32_t i = 0; i <
N_BINS; ++i)
960 tmp[i] = gsl_histogram_get (h, i);
961 tmp[i] -= expected[i];
963 tmp[i] /= expected[i];
966 gsl_histogram_free (h);
968 double chiSquared = 0;
970 for (uint32_t i = 0; i <
N_BINS; ++i)
972 chiSquared += tmp[i];
981 SeedManager::SetSeed (time (0));
984 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
986 for (uint32_t i = 0; i <
N_RUNS; ++i)
993 sum /= (double)N_RUNS;
999 double scale = mean * (shape - 1.0) / shape;
1025 double expectedMean = (shape * scale) / (shape - 1.0);
1048 virtual void DoRun (
void);
1052 :
TestCase (
"Antithetic Pareto Random Variable Stream Generator")
1063 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1065 double range[
N_BINS + 1];
1067 range[
N_BINS] = std::numeric_limits<double>::max ();
1069 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1077 double scale = mean * (shape - 1.0) / shape;
1079 for (uint32_t i = 0; i <
N_BINS; ++i)
1081 expected[i] = gsl_cdf_pareto_P (range[i + 1], shape, scale) - gsl_cdf_pareto_P (range[i], shape, scale);
1087 gsl_histogram_increment (h, p->
GetValue ());
1092 for (uint32_t i = 0; i <
N_BINS; ++i)
1094 tmp[i] = gsl_histogram_get (h, i);
1095 tmp[i] -= expected[i];
1097 tmp[i] /= expected[i];
1100 gsl_histogram_free (h);
1102 double chiSquared = 0;
1104 for (uint32_t i = 0; i <
N_BINS; ++i)
1106 chiSquared += tmp[i];
1115 SeedManager::SetSeed (time (0));
1118 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1120 for (uint32_t i = 0; i <
N_RUNS; ++i)
1131 sum /= (double)N_RUNS;
1137 double scale = mean * (shape - 1.0) / shape;
1167 double expectedMean = (shape * scale) / (shape - 1.0);
1190 virtual void DoRun (
void);
1194 :
TestCase (
"Weibull Random Variable Stream Generator")
1205 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1207 double range[
N_BINS + 1];
1209 range[
N_BINS] = std::numeric_limits<double>::max ();
1211 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1221 for (uint32_t i = 0; i <
N_BINS; ++i)
1223 expected[i] = gsl_cdf_weibull_P (range[i + 1], a, b) - gsl_cdf_weibull_P (range[i], a, b);
1229 gsl_histogram_increment (h, p->
GetValue ());
1234 for (uint32_t i = 0; i <
N_BINS; ++i)
1236 tmp[i] = gsl_histogram_get (h, i);
1237 tmp[i] -= expected[i];
1239 tmp[i] /= expected[i];
1242 gsl_histogram_free (h);
1244 double chiSquared = 0;
1246 for (uint32_t i = 0; i <
N_BINS; ++i)
1248 chiSquared += tmp[i];
1257 SeedManager::SetSeed (time (0));
1260 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1262 for (uint32_t i = 0; i <
N_RUNS; ++i)
1269 sum /= (double)N_RUNS;
1313 double expectedMean = scale;
1336 virtual void DoRun (
void);
1340 :
TestCase (
"Antithetic Weibull Random Variable Stream Generator")
1351 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1353 double range[
N_BINS + 1];
1355 range[
N_BINS] = std::numeric_limits<double>::max ();
1357 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1367 for (uint32_t i = 0; i <
N_BINS; ++i)
1369 expected[i] = gsl_cdf_weibull_P (range[i + 1], a, b) - gsl_cdf_weibull_P (range[i], a, b);
1375 gsl_histogram_increment (h, p->
GetValue ());
1380 for (uint32_t i = 0; i <
N_BINS; ++i)
1382 tmp[i] = gsl_histogram_get (h, i);
1383 tmp[i] -= expected[i];
1385 tmp[i] /= expected[i];
1388 gsl_histogram_free (h);
1390 double chiSquared = 0;
1392 for (uint32_t i = 0; i <
N_BINS; ++i)
1394 chiSquared += tmp[i];
1403 SeedManager::SetSeed (time (0));
1406 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1408 for (uint32_t i = 0; i <
N_RUNS; ++i)
1419 sum /= (double)N_RUNS;
1466 double expectedMean = scale;
1489 virtual void DoRun (
void);
1493 :
TestCase (
"Log-Normal Random Variable Stream Generator")
1504 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1506 double range[
N_BINS + 1];
1508 range[
N_BINS] = std::numeric_limits<double>::max ();
1510 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1520 for (uint32_t i = 0; i <
N_BINS; ++i)
1522 expected[i] = gsl_cdf_lognormal_P (range[i + 1], mu, sigma) - gsl_cdf_lognormal_P (range[i], mu, sigma);
1528 gsl_histogram_increment (h, n->
GetValue ());
1533 for (uint32_t i = 0; i <
N_BINS; ++i)
1535 tmp[i] = gsl_histogram_get (h, i);
1536 tmp[i] -= expected[i];
1538 tmp[i] /= expected[i];
1541 gsl_histogram_free (h);
1543 double chiSquared = 0;
1545 for (uint32_t i = 0; i <
N_BINS; ++i)
1547 chiSquared += tmp[i];
1556 SeedManager::SetSeed (time (0));
1559 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1561 for (uint32_t i = 0; i <
N_RUNS; ++i)
1568 sum /= (double)N_RUNS;
1597 double expectedMean = std::exp(mu + sigma * sigma / 2.0);
1625 virtual void DoRun (
void);
1629 :
TestCase (
"Antithetic Log-Normal Random Variable Stream Generator")
1640 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1642 double range[
N_BINS + 1];
1644 range[
N_BINS] = std::numeric_limits<double>::max ();
1646 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1656 for (uint32_t i = 0; i <
N_BINS; ++i)
1658 expected[i] = gsl_cdf_lognormal_P (range[i + 1], mu, sigma) - gsl_cdf_lognormal_P (range[i], mu, sigma);
1664 gsl_histogram_increment (h, n->
GetValue ());
1669 for (uint32_t i = 0; i <
N_BINS; ++i)
1671 tmp[i] = gsl_histogram_get (h, i);
1672 tmp[i] -= expected[i];
1674 tmp[i] /= expected[i];
1677 gsl_histogram_free (h);
1679 double chiSquared = 0;
1681 for (uint32_t i = 0; i <
N_BINS; ++i)
1683 chiSquared += tmp[i];
1692 SeedManager::SetSeed (time (0));
1695 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1697 for (uint32_t i = 0; i <
N_RUNS; ++i)
1708 sum /= (double)N_RUNS;
1740 double expectedMean = std::exp(mu + sigma * sigma / 2.0);
1768 virtual void DoRun (
void);
1772 :
TestCase (
"Gamma Random Variable Stream Generator")
1783 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1785 double range[
N_BINS + 1];
1787 range[
N_BINS] = std::numeric_limits<double>::max ();
1789 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1799 for (uint32_t i = 0; i <
N_BINS; ++i)
1801 expected[i] = gsl_cdf_gamma_P (range[i + 1], alpha, beta) - gsl_cdf_gamma_P (range[i], alpha, beta);
1807 gsl_histogram_increment (h, n->
GetValue ());
1812 for (uint32_t i = 0; i <
N_BINS; ++i)
1814 tmp[i] = gsl_histogram_get (h, i);
1815 tmp[i] -= expected[i];
1817 tmp[i] /= expected[i];
1820 gsl_histogram_free (h);
1822 double chiSquared = 0;
1824 for (uint32_t i = 0; i <
N_BINS; ++i)
1826 chiSquared += tmp[i];
1835 SeedManager::SetSeed (time (0));
1838 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1840 for (uint32_t i = 0; i <
N_RUNS; ++i)
1847 sum /= (double)N_RUNS;
1874 double expectedMean = alpha * beta;
1897 virtual void DoRun (
void);
1901 :
TestCase (
"Antithetic Gamma Random Variable Stream Generator")
1912 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
1914 double range[
N_BINS + 1];
1916 range[
N_BINS] = std::numeric_limits<double>::max ();
1918 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
1928 for (uint32_t i = 0; i <
N_BINS; ++i)
1930 expected[i] = gsl_cdf_gamma_P (range[i + 1], alpha, beta) - gsl_cdf_gamma_P (range[i], alpha, beta);
1936 gsl_histogram_increment (h, n->
GetValue ());
1941 for (uint32_t i = 0; i <
N_BINS; ++i)
1943 tmp[i] = gsl_histogram_get (h, i);
1944 tmp[i] -= expected[i];
1946 tmp[i] /= expected[i];
1949 gsl_histogram_free (h);
1951 double chiSquared = 0;
1953 for (uint32_t i = 0; i <
N_BINS; ++i)
1955 chiSquared += tmp[i];
1964 SeedManager::SetSeed (time (0));
1967 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
1969 for (uint32_t i = 0; i <
N_RUNS; ++i)
1980 sum /= (double)N_RUNS;
2011 double expectedMean = alpha * beta;
2034 virtual void DoRun (
void);
2038 :
TestCase (
"Erlang Random Variable Stream Generator")
2049 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
2051 double range[
N_BINS + 1];
2053 range[
N_BINS] = std::numeric_limits<double>::max ();
2055 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
2063 double lambda = 1.0;
2068 for (uint32_t i = 0; i <
N_BINS; ++i)
2070 expected[i] = gsl_cdf_gamma_P (range[i + 1], k, lambda) - gsl_cdf_gamma_P (range[i], k, lambda);
2076 gsl_histogram_increment (h, n->
GetValue ());
2081 for (uint32_t i = 0; i <
N_BINS; ++i)
2083 tmp[i] = gsl_histogram_get (h, i);
2084 tmp[i] -= expected[i];
2086 tmp[i] /= expected[i];
2089 gsl_histogram_free (h);
2091 double chiSquared = 0;
2093 for (uint32_t i = 0; i <
N_BINS; ++i)
2095 chiSquared += tmp[i];
2104 SeedManager::SetSeed (time (0));
2107 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
2109 for (uint32_t i = 0; i <
N_RUNS; ++i)
2116 sum /= (double)N_RUNS;
2121 double lambda = 2.0;
2143 double expectedMean = k * lambda;
2166 virtual void DoRun (
void);
2170 :
TestCase (
"Antithetic Erlang Random Variable Stream Generator")
2181 gsl_histogram * h = gsl_histogram_alloc (
N_BINS);
2183 double range[
N_BINS + 1];
2185 range[
N_BINS] = std::numeric_limits<double>::max ();
2187 gsl_histogram_set_ranges (h, range,
N_BINS + 1);
2195 double lambda = 1.0;
2200 for (uint32_t i = 0; i <
N_BINS; ++i)
2202 expected[i] = gsl_cdf_gamma_P (range[i + 1], k, lambda) - gsl_cdf_gamma_P (range[i], k, lambda);
2208 gsl_histogram_increment (h, n->
GetValue ());
2213 for (uint32_t i = 0; i <
N_BINS; ++i)
2215 tmp[i] = gsl_histogram_get (h, i);
2216 tmp[i] -= expected[i];
2218 tmp[i] /= expected[i];
2221 gsl_histogram_free (h);
2223 double chiSquared = 0;
2225 for (uint32_t i = 0; i <
N_BINS; ++i)
2227 chiSquared += tmp[i];
2236 SeedManager::SetSeed (time (0));
2239 double maxStatistic = gsl_cdf_chisq_Qinv (0.05,
N_BINS);
2241 for (uint32_t i = 0; i <
N_RUNS; ++i)
2252 sum /= (double)N_RUNS;
2257 double lambda = 2.0;
2283 double expectedMean = k * lambda;
2302 virtual void DoRun (
void);
2306 :
TestCase (
"Zipf Random Variable Stream Generator")
2317 SeedManager::SetSeed (time (0));
2365 double expectedMean = 1.0;
2384 virtual void DoRun (
void);
2388 :
TestCase (
"Antithetic Zipf Random Variable Stream Generator")
2399 SeedManager::SetSeed (time (0));
2450 double expectedMean = 1.0;
2469 virtual void DoRun (
void);
2473 :
TestCase (
"Zeta Random Variable Stream Generator")
2484 SeedManager::SetSeed (time (0));
2514 double expectedMean = gsl_sf_zeta_int (alpha - 1) / gsl_sf_zeta_int (alpha);
2533 virtual void DoRun (
void);
2537 :
TestCase (
"Antithetic Zeta Random Variable Stream Generator")
2548 SeedManager::SetSeed (time (0));
2581 double expectedMean = gsl_sf_zeta_int (alpha - 1) / gsl_sf_zeta_int (alpha);
2600 virtual void DoRun (
void);
2604 :
TestCase (
"Deterministic Random Variable Stream Generator")
2615 SeedManager::SetSeed (time (0));
2623 double array1 [] = { 4, 4, 7, 7, 10, 10};
2624 uint64_t count1 = 6;
2647 double array2 [] = { 1000, 2000, 3000, 4000};
2648 uint64_t count2 = 4;
2675 virtual void DoRun (
void);
2679 :
TestCase (
"Empirical Random Variable Stream Generator")
2690 SeedManager::SetSeed (time (0));
2713 double expectedMean = 5.0;
2732 virtual void DoRun (
void);
2736 :
TestCase (
"EmpiricalAntithetic Random Variable Stream Generator")
2747 SeedManager::SetSeed (time (0));
2773 double expectedMean = 5.0;
2787 :
TestSuite (
"random-variable-stream-generators", UNIT)