28 #include <sys/types.h>
35 #include "random-variable.h"
36 #include "rng-seed-manager.h"
37 #include "rng-stream.h"
38 #include "fatal-error.h"
55 virtual double GetValue () = 0;
56 virtual uint32_t GetInteger ();
63 RandomVariableBase::RandomVariableBase ()
69 RandomVariableBase::RandomVariableBase (
const RandomVariableBase& r)
73 if (r.m_generator != 0)
76 RngSeedManager::GetNextStreamIndex (),
81 RandomVariableBase::~RandomVariableBase ()
87 uint32_t RandomVariableBase::GetInteger ()
90 return (uint32_t)GetValue ();
94 RandomVariableBase::GetStream (
void)
100 RngSeedManager::GetNextStreamIndex (),
108 RandomVariable::RandomVariable ()
113 RandomVariable::RandomVariable (
const RandomVariable&o)
114 : m_variable (o.m_variable->Copy ())
117 RandomVariable::RandomVariable (
const RandomVariableBase &variable)
118 : m_variable (variable.Copy ())
122 RandomVariable::operator = (
const RandomVariable &o)
129 m_variable = o.m_variable->Copy ();
132 RandomVariable::~RandomVariable ()
141 return m_variable->GetValue ();
148 return m_variable->GetInteger ();
152 RandomVariable::Peek (
void)
const
184 double GetMin (
void)
const;
185 double GetMax (
void)
const;
195 virtual double GetValue (
double s,
double l);
227 UniformVariableImpl::GetMin (
void)
const
233 UniformVariableImpl::GetMax (
void)
const
244 return m_min + generator->
RandU01 () * (m_max - m_min);
251 return s + generator->
RandU01 () * (l - s);
287 return static_cast<uint32_t
> (
GetValue (s, l + 1) );
323 virtual uint32_t GetInteger ();
360 uint32_t ConstantVariableImpl::GetInteger ()
363 return (uint32_t)m_const;
366 RandomVariableBase* ConstantVariableImpl::Copy ()
const
435 uint32_t m_consecutive;
437 uint32_t m_currentConsecutive;
446 m_currentConsecutive (0)
457 m_currentConsecutive (0)
466 m_increment (c.m_increment),
467 m_consecutive (c.m_consecutive),
468 m_current (c.m_current),
469 m_currentConsecutive (c.m_currentConsecutive)
474 SequentialVariableImpl::~SequentialVariableImpl ()
482 double r = m_current;
483 if (++m_currentConsecutive == m_consecutive)
485 m_currentConsecutive = 0;
486 m_current += m_increment.
GetValue ();
487 if (m_current >= m_max)
489 m_current = m_min + (m_current - m_max);
593 double r = -m_mean*std::log (generator->
RandU01 ());
594 if (m_bound == 0 || r <= m_bound)
704 : m_scale (0.5 / 1.5),
712 : m_scale (m * 0.5 / 1.5),
720 : m_scale (m * (s - 1.0) / s),
728 : m_scale (m * (s - 1.0) / s),
736 : m_scale (params.first),
737 m_shape (params.second),
744 : m_scale (params.first),
745 m_shape (params.second),
766 double r = (m_scale * ( 1.0 / std::pow (generator->
RandU01 (), 1.0 / m_shape)));
767 if (m_bound == 0 || r <= m_bound)
908 double exponent = 1.0 / m_alpha;
911 double r = m_mean * std::pow ( -std::log (generator->
RandU01 ()), exponent);
912 if (m_bound == 0 || r <= m_bound)
955 static const double INFINITE_VALUE;
978 double GetMean (
void)
const;
979 double GetVariance (
void)
const;
980 double GetBound (
void)
const;
990 const double NormalVariableImpl::INFINITE_VALUE = 1e307;
995 m_bound (INFINITE_VALUE),
1013 m_variance (c.m_variance),
1014 m_bound (c.m_bound),
1026 m_nextValid =
false;
1033 double u1 = generator->
RandU01 ();
1034 double u2 = generator->
RandU01 ();
1035 double v1 = 2 * u1 - 1;
1036 double v2 = 2 * u2 - 1;
1037 double w = v1 * v1 + v2 * v2;
1040 double y = std::sqrt ((-2 * std::log (w)) / w);
1041 m_next = m_mean + v2 * y * std::sqrt (m_variance);
1043 m_nextValid = std::fabs (m_next - m_mean) <= m_bound;
1044 double x1 = m_mean + v1 * y * std::sqrt (m_variance);
1046 if (std::fabs (x1 - m_mean) <= m_bound)
1051 else if (m_nextValid)
1053 m_nextValid =
false;
1068 NormalVariableImpl::GetMean (
void)
const
1075 NormalVariableImpl::GetVariance (
void)
const
1082 NormalVariableImpl::GetBound (
void)
const
1126 virtual void CDF (
double v,
double c);
1138 virtual void Validate ();
1139 virtual double Interpolate (
double,
double,
double,
double,
double);
1141 std::vector<ValueCDF> emp;
1146 EmpiricalVariableImpl::ValueCDF::ValueCDF ()
1152 EmpiricalVariableImpl::ValueCDF::ValueCDF (
double v,
double c)
1158 EmpiricalVariableImpl::ValueCDF::ValueCDF (
const ValueCDF& c)
1176 validated (c.validated),
1182 EmpiricalVariableImpl::~EmpiricalVariableImpl ()
1192 if (emp.size () == 0)
1200 double r = generator->
RandU01 ();
1201 if (r <= emp.front ().cdf)
1203 return emp.front ().value;
1205 if (r >= emp.back ().cdf)
1207 return emp.back ().value;
1210 std::vector<ValueCDF>::size_type bottom = 0;
1211 std::vector<ValueCDF>::size_type top = emp.size () - 1;
1214 std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1215 if (r >= emp[c].cdf && r < emp[c + 1].cdf)
1217 return Interpolate (emp[c].cdf, emp[c + 1].cdf,
1218 emp[c].value, emp[c + 1].value,
1246 void EmpiricalVariableImpl::Validate ()
1250 for (std::vector<ValueCDF>::size_type i = 0; i < emp.size (); ++i)
1252 ValueCDF& current = emp[i];
1253 if (current.value < prior.value || current.cdf < prior.cdf)
1255 std::cerr <<
"Empirical Dist error,"
1256 <<
" current value " << current.value
1257 <<
" prior value " << prior.value
1258 <<
" current cdf " << current.cdf
1259 <<
" prior cdf " << prior.cdf << std::endl;
1267 double EmpiricalVariableImpl::Interpolate (
double c1,
double c2,
1268 double v1,
double v2,
double r)
1271 return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1308 virtual double Interpolate (
double,
double,
double,
double,
double);
1312 IntEmpiricalVariableImpl::IntEmpiricalVariableImpl ()
1329 double IntEmpiricalVariableImpl::Interpolate (
double c1,
double c2,
1330 double v1,
double v2,
double r)
1333 return std::ceil (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1336 IntEmpiricalVariable::IntEmpiricalVariable ()
1337 : EmpiricalVariable (IntEmpiricalVariableImpl ())
1382 DeterministicVariableImpl::~DeterministicVariableImpl ()
1394 return data[next++];
1477 double u, v, r2, normal, z;
1483 u = -1 + 2 * generator->
RandU01 ();
1484 v = -1 + 2 * generator->
RandU01 ();
1489 while (r2 > 1.0 || r2 == 0);
1491 normal = u * std::sqrt (-2.0 * std::log (r2) / r2);
1493 z = std::exp (m_sigma * normal + m_mu);
1525 double GetValue (
double alpha,
double beta);
1580 double u = generator->
RandU01 ();
1581 return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
1585 double d = alpha - 1.0 / 3.0;
1586 double c = (1.0 / 3.0) / std::sqrt (d);
1599 if (u < 1 - 0.0331 * x * x * x * x)
1603 if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
1609 return beta * d * v;
1658 double GetValue (
unsigned int k,
double lambda);
1709 for (
unsigned int i = 0; i < k; ++i)
1788 m_mode (3.0 * mean - s - l)
1806 double u = generator->
RandU01 ();
1807 if (u <= (m_mode - m_min) / (m_max - m_min) )
1809 return m_min + std::sqrt (u * (m_max - m_min) * (m_mode - m_min) );
1813 return m_max - std::sqrt ( (1 - u) * (m_max - m_min) * (m_max - m_mode) );
1886 for (
int i = 1; i <= n; i++)
1888 m_c += (1.0 / std::pow ((
double)i, alpha));
1899 double u = generator->
RandU01 ();
1900 double sum_prob = 0,zipf_value = 0;
1901 for (
int i = 1; i <= m_n; i++)
1903 sum_prob += m_c / std::pow ((
double)i, m_alpha);
1962 m_b (std::pow (2.0, 2.14))
1970 m_b (std::pow (2.0, alpha - 1.0))
1994 X = floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
1995 T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
1996 test = v * X * (T - 1.0) / (m_b - 1.0);
1998 while ( test > (T / m_b) );
2022 os <<
"Constant:" << constant->
GetValue ();
2025 UniformVariableImpl *uniform =
dynamic_cast<UniformVariableImpl *
> (base);
2028 os <<
"Uniform:" << uniform->GetMin () <<
":" << uniform->GetMax ();
2031 NormalVariableImpl *normal =
dynamic_cast<NormalVariableImpl *
> (base);
2034 os <<
"Normal:" << normal->GetMean () <<
":" << normal->GetVariance ();
2035 double bound = normal->GetBound ();
2036 if (bound != NormalVariableImpl::INFINITE_VALUE)
2043 os.setstate (std::ios_base::badbit);
2046 std::istream &
operator >> (std::istream &is, RandomVariable &var)
2050 std::string::size_type tmp;
2051 tmp = value.find (
":");
2052 if (tmp == std::string::npos)
2054 is.setstate (std::ios_base::badbit);
2057 std::string type = value.substr (0, tmp);
2058 value = value.substr (tmp + 1, value.npos);
2059 if (type ==
"Constant")
2061 std::istringstream iss (value);
2064 var = ConstantVariable (constant);
2066 else if (type ==
"Uniform")
2068 if (value.size () == 0)
2070 var = UniformVariable ();
2074 tmp = value.find (
":");
2075 if (tmp == value.npos)
2079 std::istringstream issA (value.substr (0, tmp));
2080 std::istringstream issB (value.substr (tmp + 1, value.npos));
2084 var = UniformVariable (a, b);
2087 else if (type ==
"Normal")
2089 if (value.size () == 0)
2091 var = NormalVariable ();
2095 tmp = value.find (
":");
2096 if (tmp == value.npos)
2100 std::string::size_type tmp2;
2101 std::string sub = value.substr (tmp + 1, value.npos);
2102 tmp2 = sub.find (
":");
2103 if (tmp2 == value.npos)
2105 std::istringstream issA (value.substr (0, tmp));
2106 std::istringstream issB (sub);
2110 var = NormalVariable (a, b);
2114 std::istringstream issA (value.substr (0, tmp));
2115 std::istringstream issB (sub.substr (0, tmp2));
2116 std::istringstream issC (sub.substr (tmp2 + 1, value.npos));
2121 var = NormalVariable (a, b, c);
2127 NS_FATAL_ERROR (
"RandomVariable deserialization not implemented for " << type);
virtual double GetValue()
ExponentialVariableImpl()
std::istream & operator>>(std::istream &is, Angles &a)
#define NS_LOG_FUNCTION(parameters)
A random variable that returns a constantClass ConstantVariable defines a random number generator tha...
virtual uint32_t GetInteger()
virtual double GetValue()
#define NS_ASSERT(condition)
virtual double GetValue()
#define NS_LOG_COMPONENT_DEFINE(name)
virtual double GetValue()
virtual double GetValue()
virtual double GetValue()
#define NS_FATAL_ERROR(msg)
fatal error handling
static uint64_t GetRun(void)
void NewConstant(double c)
Specify a new constant RNG for this generator.
Class NormalVariable defines a random variable with a normal (Gaussian) distribution.This class supports the creation of objects that return random numbers from a fixed normal distribution. It also supports the generation of single random numbers from various normal distributions.
Combined Multiple-Recursive Generator MRG32k3a.
#define ATTRIBUTE_VALUE_IMPLEMENT(type)
SequentialVariableImpl(double f, double l, double i=1, uint32_t c=1)
Constructor for the SequentialVariableImpl RNG.
virtual double GetValue()
DeterministicVariable(double *d, uint32_t c)
Constructor.
LogNormalVariableImpl(double mu, double sigma)
double GetValue(void) const
call RandomVariable::GetValue
SequentialVariable(double f, double l, double i=1, uint32_t c=1)
Constructor for the SequentialVariable RNG.
ErlangVariableImpl(unsigned int k, double lambda)
std::ostream & operator<<(std::ostream &os, const Angles &a)
LogNormalVariable(double mu, double sigma)
virtual double GetValue()
virtual void CDF(double v, double c)
Specifies a point in the empirical distribution.
virtual double GetValue()
Exponentially Distributed random varThis class supports the creation of objects that return random nu...
static uint32_t GetSeed(void)
Get the seed value.
double GetValue(void) const
call RandomVariable::GetValue
void CDF(double v, double c)
Specifies a point in the empirical distribution.
virtual double GetValue()
virtual double GetValue()
GammaVariableImpl(double alpha, double beta)
virtual double GetValue()
DeterministicVariableImpl(double *d, uint32_t c)
Constructor.
The basic RNG for NS-3.Note: The underlying random number generation method used by NS-3 is the RngSt...
double GetValue(void) const
Returns a random double from the underlying distribution.
uint32_t GetInteger(void) const
Returns a random integer integer from the underlying distribution.
void SetConstant(double c)
Specify a new constant RNG for this generator.
ParetoVariable()
Constructs a pareto random variable with a mean of 1 and a shape parameter of 1.5.
virtual double GetValue()
#define ATTRIBUTE_CHECKER_IMPLEMENT(type)
virtual double GetValue()