50 .AddAttribute(
"Stream",
51 "The stream number for this RNG stream. -1 means \"allocate a stream automatically\". "
52 "Note that if -1 is set, Get will return -1 so that it is not possible to know which "
53 "value was automatically allocated.",
57 MakeIntegerChecker<int64_t>())
58 .AddAttribute(
"Antithetic",
"Set this RNG stream to generate antithetic values",
111 uint64_t base = ((1ULL)<<63);
112 uint64_t target = base + stream;
138 static TypeId tid =
TypeId (
"ns3::UniformRandomVariable")
140 .AddConstructor<UniformRandomVariable> ()
141 .AddAttribute(
"Min",
"The lower bound on the values returned by this RNG stream.",
144 MakeDoubleChecker<double>())
145 .AddAttribute(
"Max",
"The upper bound on the values returned by this RNG stream.",
148 MakeDoubleChecker<double>())
175 double v = min +
Peek ()->
RandU01 () * (max - min);
187 return static_cast<uint32_t
> (
GetValue (min, max + 1) );
208 static TypeId tid =
TypeId (
"ns3::ConstantRandomVariable")
210 .AddConstructor<ConstantRandomVariable> ()
211 .AddAttribute(
"Constant",
"The constant value returned by this RNG stream.",
214 MakeDoubleChecker<double>())
262 static TypeId tid =
TypeId (
"ns3::SequentialRandomVariable")
264 .AddConstructor<SequentialRandomVariable> ()
265 .AddAttribute(
"Min",
"The first value of the sequence.",
268 MakeDoubleChecker<double>())
269 .AddAttribute(
"Max",
"One more than the last value of the sequence.",
272 MakeDoubleChecker<double>())
273 .AddAttribute(
"Increment",
"The sequence random variable increment.",
274 StringValue(
"ns3::ConstantRandomVariable[Constant=1]"),
276 MakePointerChecker<RandomVariableStream> ())
277 .AddAttribute(
"Consecutive",
"The number of times each member of the sequence is repeated.",
280 MakeIntegerChecker<uint32_t>());
287 m_currentConsecutive (0),
288 m_isCurrentSet (false)
361 static TypeId tid =
TypeId (
"ns3::ExponentialRandomVariable")
363 .AddConstructor<ExponentialRandomVariable> ()
364 .AddAttribute(
"Mean",
"The mean of the values returned by this RNG stream.",
367 MakeDoubleChecker<double>())
368 .AddAttribute(
"Bound",
"The upper bound on the values returned by this RNG stream.",
371 MakeDoubleChecker<double>())
408 double r = -mean*std::log (v);
411 if (bound == 0 || r <= bound)
421 return static_cast<uint32_t
> (
GetValue (mean, bound) );
442 static TypeId tid =
TypeId (
"ns3::ParetoRandomVariable")
444 .AddConstructor<ParetoRandomVariable> ()
445 .AddAttribute(
"Mean",
"The mean parameter for the Pareto distribution returned by this RNG stream.",
448 MakeDoubleChecker<double>())
449 .AddAttribute(
"Shape",
"The shape parameter for the Pareto distribution returned by this RNG stream.",
452 MakeDoubleChecker<double>())
453 .AddAttribute(
"Bound",
"The upper bound on the values returned by this RNG stream.",
456 MakeDoubleChecker<double>())
491 double scale = mean * (shape - 1.0) / shape;
503 double r = (scale * ( 1.0 / std::pow (v, 1.0 / shape)));
506 if (bound == 0 || r <= bound)
516 return static_cast<uint32_t
> (
GetValue (mean, shape, bound) );
537 static TypeId tid =
TypeId (
"ns3::WeibullRandomVariable")
539 .AddConstructor<WeibullRandomVariable> ()
540 .AddAttribute(
"Scale",
"The scale parameter for the Weibull distribution returned by this RNG stream.",
543 MakeDoubleChecker<double>())
544 .AddAttribute(
"Shape",
"The shape parameter for the Weibull distribution returned by this RNG stream.",
547 MakeDoubleChecker<double>())
548 .AddAttribute(
"Bound",
"The upper bound on the values returned by this RNG stream.",
551 MakeDoubleChecker<double>())
585 double exponent = 1.0 / shape;
596 double r = scale * std::pow ( -std::log (v), exponent);
599 if (bound == 0 || r <= bound)
609 return static_cast<uint32_t
> (
GetValue (scale, shape, bound) );
632 static TypeId tid =
TypeId (
"ns3::NormalRandomVariable")
634 .AddConstructor<NormalRandomVariable> ()
635 .AddAttribute(
"Mean",
"The mean value for the normal distribution returned by this RNG stream.",
638 MakeDoubleChecker<double>())
639 .AddAttribute(
"Variance",
"The variance value for the normal distribution returned by this RNG stream.",
642 MakeDoubleChecker<double>())
643 .AddAttribute(
"Bound",
"The bound on the values returned by this RNG stream.",
646 MakeDoubleChecker<double>())
698 double v1 = 2 * u1 - 1;
699 double v2 = 2 * u2 - 1;
700 double w = v1 * v1 + v2 * v2;
703 double y = std::sqrt ((-2 * std::log (w)) / w);
704 m_next = mean + v2 * y * std::sqrt (variance);
707 double x1 = mean + v1 * y * std::sqrt (variance);
709 if (std::fabs (x1 - mean) <= bound)
728 return static_cast<uint32_t
> (
GetValue (mean, variance, bound) );
749 static TypeId tid =
TypeId (
"ns3::LogNormalRandomVariable")
751 .AddConstructor<LogNormalRandomVariable> ()
752 .AddAttribute(
"Mu",
"The mu value for the log-normal distribution returned by this RNG stream.",
755 MakeDoubleChecker<double>())
756 .AddAttribute(
"Sigma",
"The sigma value for the log-normal distribution returned by this RNG stream.",
759 MakeDoubleChecker<double>())
832 r2 = v1 * v1 + v2 * v2;
834 while (r2 > 1.0 || r2 == 0);
836 normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
838 x = std::exp (sigma * normal + mu);
847 return static_cast<uint32_t
> (
GetValue (mu, sigma));
870 .AddConstructor<GammaRandomVariable> ()
871 .AddAttribute(
"Alpha",
"The alpha value for the gamma distribution returned by this RNG stream.",
874 MakeDoubleChecker<double>())
875 .AddAttribute(
"Beta",
"The beta value for the gamma distribution returned by this RNG stream.",
878 MakeDoubleChecker<double>())
931 return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
935 double d = alpha - 1.0 / 3.0;
936 double c = (1.0 / 3.0) / std::sqrt (d);
945 double variance = 1.0;
959 if (u < 1 - 0.0331 * x * x * x * x)
963 if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
976 return static_cast<uint32_t
> (
GetValue (alpha, beta));
1012 double v1 = 2 * u1 - 1;
1013 double v2 = 2 * u2 - 1;
1014 double w = v1 * v1 + v2 * v2;
1017 double y = std::sqrt ((-2 * std::log (w)) / w);
1018 m_next = mean + v2 * y * std::sqrt (variance);
1021 double x1 = mean + v1 * y * std::sqrt (variance);
1023 if (std::fabs (x1 - mean) <= bound)
1043 static TypeId tid =
TypeId (
"ns3::ErlangRandomVariable")
1045 .AddConstructor<ErlangRandomVariable> ()
1046 .AddAttribute(
"K",
"The k value for the Erlang distribution returned by this RNG stream.",
1049 MakeIntegerChecker<uint32_t>())
1050 .AddAttribute(
"Lambda",
"The lambda value for the Erlang distribution returned by this RNG stream.",
1053 MakeDoubleChecker<double>())
1092 double mean = lambda;
1096 for (
unsigned int i = 0; i < k; ++i)
1109 return static_cast<uint32_t
> (
GetValue (k, lambda));
1139 double r = -mean*std::log (v);
1142 if (bound == 0 || r <= bound)
1154 static TypeId tid =
TypeId (
"ns3::TriangularRandomVariable")
1156 .AddConstructor<TriangularRandomVariable> ()
1157 .AddAttribute(
"Mean",
"The mean value for the triangular distribution returned by this RNG stream.",
1160 MakeDoubleChecker<double>())
1161 .AddAttribute(
"Min",
"The lower bound on the values returned by this RNG stream.",
1164 MakeDoubleChecker<double>())
1165 .AddAttribute(
"Max",
"The upper bound on the values returned by this RNG stream.",
1168 MakeDoubleChecker<double>())
1203 double mode = 3.0 * mean - min - max;
1213 if (u <= (mode - min) / (max - min) )
1215 return min + std::sqrt (u * (max - min) * (mode - min) );
1219 return max - std::sqrt ( (1 - u) * (max - min) * (max - mode) );
1227 return static_cast<uint32_t
> (
GetValue (mean, min, max) );
1250 .AddConstructor<ZipfRandomVariable> ()
1251 .AddAttribute(
"N",
"The n value for the Zipf distribution returned by this RNG stream.",
1254 MakeIntegerChecker<uint32_t>())
1255 .AddAttribute(
"Alpha",
"The alpha value for the Zipf distribution returned by this RNG stream.",
1258 MakeDoubleChecker<double>())
1287 for (uint32_t i = 1; i <= n; i++)
1289 m_c += (1.0 / std::pow ((
double)i,alpha));
1300 double sum_prob = 0,zipf_value = 0;
1301 for (uint32_t i = 1; i <=
m_n; i++)
1303 sum_prob +=
m_c / std::pow ((
double)i,
m_alpha);
1317 return static_cast<uint32_t
> (
GetValue (n, alpha));
1340 .AddConstructor<ZetaRandomVariable> ()
1341 .AddAttribute(
"Alpha",
"The alpha value for the zeta distribution returned by this RNG stream.",
1344 MakeDoubleChecker<double>())
1365 m_b = std::pow (2.0, alpha - 1.0);
1387 X = std::floor (std::pow (u, -1.0 / (
m_alpha - 1.0)));
1388 T = std::pow (1.0 + 1.0 / X,
m_alpha - 1.0);
1389 test = v * X * (T - 1.0) / (
m_b - 1.0);
1391 while ( test > (T /
m_b) );
1400 return static_cast<uint32_t
> (
GetValue (alpha));
1421 static TypeId tid =
TypeId (
"ns3::DeterministicRandomVariable")
1423 .AddConstructor<DeterministicRandomVariable> ()
1456 m_data =
new double[length];
1461 for (uint64_t i = 0; i <
m_count; i++)
1513 static TypeId tid =
TypeId (
"ns3::EmpiricalRandomVariable")
1515 .AddConstructor<EmpiricalRandomVariable> ()
1532 if (
emp.size () == 0)
1548 if (r <=
emp.front ().cdf)
1550 return emp.front ().value;
1552 if (r >=
emp.back ().cdf)
1554 return emp.back ().value;
1557 std::vector<ValueCDF>::size_type bottom = 0;
1558 std::vector<ValueCDF>::size_type top =
emp.size () - 1;
1561 std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1562 if (r >=
emp[c].cdf && r <
emp[c + 1].cdf)
1565 emp[c].value,
emp[c + 1].value,
1598 for (std::vector<ValueCDF>::size_type i = 0; i <
emp.size (); ++i)
1603 std::cerr <<
"Empirical Dist error,"
1604 <<
" current value " << current.
value
1605 <<
" prior value " << prior.
value
1606 <<
" current cdf " << current.
cdf
1607 <<
" prior cdf " << prior.
cdf << std::endl;
1616 double v1,
double v2,
double r)
1619 return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));