29 #include <sys/types.h>
56 virtual double GetValue () = 0;
57 virtual uint32_t GetInteger ();
64 RandomVariableBase::RandomVariableBase ()
109 : m_variable (o.m_variable->
Copy ())
113 : m_variable (variable.
Copy ())
175 double GetMin (
void)
const;
176 double GetMax (
void)
const;
186 virtual double GetValue (
double s,
double l);
235 return s + generator->
RandU01 () * (l - s);
265 return static_cast<uint32_t
> (
GetValue (s, l + 1) );
414 m_currentConsecutive (0)
424 m_currentConsecutive (0)
432 m_increment (c.m_increment),
433 m_consecutive (c.m_consecutive),
434 m_current (c.m_current),
435 m_currentConsecutive (c.m_currentConsecutive)
655 : m_scale (0.5 / 1.5),
662 : m_scale (m * 0.5 / 1.5),
669 : m_scale (m * (s - 1.0) / s),
676 : m_scale (m * (s - 1.0) / s),
683 : m_scale (params.first),
684 m_shape (params.second),
690 : m_scale (params.first),
691 m_shape (params.second),
838 double exponent = 1.0 /
m_alpha;
841 double r =
m_mean * pow ( -log (generator->
RandU01 ()), exponent);
920 m_bound (INFINITE_VALUE),
936 m_variance (c.m_variance),
954 double u1 = generator->
RandU01 ();
955 double u2 = generator->
RandU01 ();
956 double v1 = 2 * u1 - 1;
957 double v2 = 2 * u2 - 1;
958 double w = v1 * v1 + v2 * v2;
961 double y = sqrt ((-2 * log (w)) / w);
1040 virtual void CDF (
double v,
double c);
1053 virtual double Interpolate (
double,
double,
double,
double,
double);
1086 validated (c.validated),
1099 if (
emp.size () == 0)
1107 double r = generator->
RandU01 ();
1108 if (r <=
emp.front ().cdf)
1110 return emp.front ().value;
1112 if (r >=
emp.back ().cdf)
1114 return emp.back ().value;
1117 std::vector<ValueCDF>::size_type bottom = 0;
1118 std::vector<ValueCDF>::size_type top =
emp.size () - 1;
1121 std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1122 if (r >=
emp[c].cdf && r <
emp[c + 1].cdf)
1125 emp[c].value,
emp[c + 1].value,
1154 for (std::vector<ValueCDF>::size_type i = 0; i <
emp.size (); ++i)
1159 cerr <<
"Empirical Dist error,"
1160 <<
" current value " << current.
value
1161 <<
" prior value " << prior.
value
1162 <<
" current cdf " << current.
cdf
1163 <<
" prior cdf " << prior.
cdf << endl;
1172 double v1,
double v2,
double r)
1174 return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1208 virtual double Interpolate (
double,
double,
double,
double,
double);
1227 double v1,
double v2,
double r)
1229 return ceil (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1364 double u, v, r2,
normal, z;
1370 u = -1 + 2 * generator->
RandU01 ();
1371 v = -1 + 2 * generator->
RandU01 ();
1376 while (r2 > 1.0 || r2 == 0);
1378 normal = u * sqrt (-2.0 * log (r2) / r2);
1411 double GetValue (
double alpha,
double beta);
1462 double u = generator->
RandU01 ();
1463 return GetValue (1.0 + alpha, beta) * pow (u, 1.0 / alpha);
1467 double d = alpha - 1.0 / 3.0;
1468 double c = (1.0 / 3.0) / sqrt (d);
1481 if (u < 1 - 0.0331 * x * x * x * x)
1485 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
1491 return beta * d * v;
1536 double GetValue (
unsigned int k,
double lambda);
1583 for (
unsigned int i = 0; i < k; ++i)
1657 m_mode (3.0 * mean - s - l)
1672 double u = generator->
RandU01 ();
1746 for (
int i = 1; i <= n; i++)
1748 m_c += (1.0 / pow ((
double)i,alpha));
1758 double u = generator->
RandU01 ();
1759 double sum_prob = 0,zipf_value = 0;
1760 for (
int i = 1; i <=
m_n; i++)
1818 m_b (pow (2.0, 2.14))
1825 m_b (pow (2.0, alpha - 1.0))
1847 X = floor (pow (u, -1.0 / (
m_alpha - 1.0)));
1848 T = pow (1.0 + 1.0 / X,
m_alpha - 1.0);
1849 test = v * X * (T - 1.0) / (
m_b - 1.0);
1851 while ( test > (T /
m_b) );
1873 os <<
"Constant:" << constant->
GetValue ();
1879 os <<
"Uniform:" << uniform->
GetMin () <<
":" << uniform->
GetMax ();
1886 double bound = normal->
GetBound ();
1894 os.setstate (std::ios_base::badbit);
1901 std::string::size_type tmp;
1902 tmp = value.find (
":");
1903 if (tmp == std::string::npos)
1905 is.setstate (std::ios_base::badbit);
1908 std::string type = value.substr (0, tmp);
1909 value = value.substr (tmp + 1, value.npos);
1910 if (type ==
"Constant")
1912 istringstream iss (value);
1917 else if (type ==
"Uniform")
1919 if (value.size () == 0)
1925 tmp = value.find (
":");
1926 if (tmp == value.npos)
1930 istringstream issA (value.substr (0, tmp));
1931 istringstream issB (value.substr (tmp + 1, value.npos));
1938 else if (type ==
"Normal")
1940 if (value.size () == 0)
1946 tmp = value.find (
":");
1947 if (tmp == value.npos)
1951 std::string::size_type tmp2;
1952 std::string sub = value.substr (tmp + 1, value.npos);
1953 tmp2 = sub.find (
":");
1954 if (tmp2 == value.npos)
1956 istringstream issA (value.substr (0, tmp));
1957 istringstream issB (sub);
1965 istringstream issA (value.substr (0, tmp));
1966 istringstream issB (sub.substr (0, tmp2));
1967 istringstream issC (sub.substr (tmp2 + 1, value.npos));
1978 NS_FATAL_ERROR (
"RandomVariable deserialization not implemented for " << type);