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",
106 uint64_t base = ((1ULL)<<63);
107 uint64_t target = base + stream;
131 static TypeId tid =
TypeId (
"ns3::UniformRandomVariable")
133 .AddConstructor<UniformRandomVariable> ()
134 .AddAttribute(
"Min",
"The lower bound on the values returned by this RNG stream.",
137 MakeDoubleChecker<double>())
138 .AddAttribute(
"Max",
"The upper bound on the values returned by this RNG stream.",
141 MakeDoubleChecker<double>())
164 double v = min +
Peek ()->
RandU01 () * (max - min);
175 return static_cast<uint32_t
> (
GetValue (min, max + 1) );
194 static TypeId tid =
TypeId (
"ns3::ConstantRandomVariable")
196 .AddConstructor<ConstantRandomVariable> ()
197 .AddAttribute(
"Constant",
"The constant value returned by this RNG stream.",
200 MakeDoubleChecker<double>())
242 static TypeId tid =
TypeId (
"ns3::SequentialRandomVariable")
244 .AddConstructor<SequentialRandomVariable> ()
245 .AddAttribute(
"Min",
"The first value of the sequence.",
248 MakeDoubleChecker<double>())
249 .AddAttribute(
"Max",
"One more than the last value of the sequence.",
252 MakeDoubleChecker<double>())
253 .AddAttribute(
"Increment",
"The sequence random variable increment.",
254 StringValue(
"ns3::ConstantRandomVariable[Constant=1]"),
256 MakePointerChecker<RandomVariableStream> ())
257 .AddAttribute(
"Consecutive",
"The number of times each member of the sequence is repeated.",
260 MakeIntegerChecker<uint32_t>());
267 m_currentConsecutive (0),
268 m_isCurrentSet (false)
334 static TypeId tid =
TypeId (
"ns3::ExponentialRandomVariable")
336 .AddConstructor<ExponentialRandomVariable> ()
337 .AddAttribute(
"Mean",
"The mean of the values returned by this RNG stream.",
340 MakeDoubleChecker<double>())
341 .AddAttribute(
"Bound",
"The upper bound on the values returned by this RNG stream.",
344 MakeDoubleChecker<double>())
377 double r = -mean*std::log (v);
380 if (bound == 0 || r <= bound)
389 return static_cast<uint32_t
> (
GetValue (mean, bound) );
408 static TypeId tid =
TypeId (
"ns3::ParetoRandomVariable")
410 .AddConstructor<ParetoRandomVariable> ()
411 .AddAttribute(
"Mean",
"The mean parameter for the Pareto distribution returned by this RNG stream.",
414 MakeDoubleChecker<double>())
415 .AddAttribute(
"Shape",
"The shape parameter for the Pareto distribution returned by this RNG stream.",
418 MakeDoubleChecker<double>())
419 .AddAttribute(
"Bound",
"The upper bound on the values returned by this RNG stream.",
422 MakeDoubleChecker<double>())
452 double scale = mean * (shape - 1.0) / shape;
464 double r = (scale * ( 1.0 / std::pow (v, 1.0 / shape)));
467 if (bound == 0 || r <= bound)
476 return static_cast<uint32_t
> (
GetValue (mean, shape, bound) );
495 static TypeId tid =
TypeId (
"ns3::WeibullRandomVariable")
497 .AddConstructor<WeibullRandomVariable> ()
498 .AddAttribute(
"Scale",
"The scale parameter for the Weibull distribution returned by this RNG stream.",
501 MakeDoubleChecker<double>())
502 .AddAttribute(
"Shape",
"The shape parameter for the Weibull distribution returned by this RNG stream.",
505 MakeDoubleChecker<double>())
506 .AddAttribute(
"Bound",
"The upper bound on the values returned by this RNG stream.",
509 MakeDoubleChecker<double>())
538 double exponent = 1.0 / shape;
549 double r = scale * std::pow ( -std::log (v), exponent);
552 if (bound == 0 || r <= bound)
561 return static_cast<uint32_t
> (
GetValue (scale, shape, bound) );
582 static TypeId tid =
TypeId (
"ns3::NormalRandomVariable")
584 .AddConstructor<NormalRandomVariable> ()
585 .AddAttribute(
"Mean",
"The mean value for the normal distribution returned by this RNG stream.",
588 MakeDoubleChecker<double>())
589 .AddAttribute(
"Variance",
"The variance value for the normal distribution returned by this RNG stream.",
592 MakeDoubleChecker<double>())
593 .AddAttribute(
"Bound",
"The bound on the values returned by this RNG stream.",
596 MakeDoubleChecker<double>())
643 double v1 = 2 * u1 - 1;
644 double v2 = 2 * u2 - 1;
645 double w = v1 * v1 + v2 * v2;
648 double y = sqrt ((-2 * log (w)) / w);
649 m_next = mean + v2 * y * sqrt (variance);
652 double x1 = mean + v1 * y * sqrt (variance);
654 if (fabs (x1 - mean) <= bound)
672 return static_cast<uint32_t
> (
GetValue (mean, variance, bound) );
691 static TypeId tid =
TypeId (
"ns3::LogNormalRandomVariable")
693 .AddConstructor<LogNormalRandomVariable> ()
694 .AddAttribute(
"Mu",
"The mu value for the log-normal distribution returned by this RNG stream.",
697 MakeDoubleChecker<double>())
698 .AddAttribute(
"Sigma",
"The sigma value for the log-normal distribution returned by this RNG stream.",
701 MakeDoubleChecker<double>())
769 r2 = v1 * v1 + v2 * v2;
771 while (r2 > 1.0 || r2 == 0);
773 normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
775 x = std::exp (sigma * normal + mu);
783 return static_cast<uint32_t
> (
GetValue (mu, sigma));
804 .AddConstructor<GammaRandomVariable> ()
805 .AddAttribute(
"Alpha",
"The alpha value for the gamma distribution returned by this RNG stream.",
808 MakeDoubleChecker<double>())
809 .AddAttribute(
"Beta",
"The beta value for the gamma distribution returned by this RNG stream.",
812 MakeDoubleChecker<double>())
861 return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
865 double d = alpha - 1.0 / 3.0;
866 double c = (1.0 / 3.0) / sqrt (d);
875 double variance = 1.0;
889 if (u < 1 - 0.0331 * x * x * x * x)
893 if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
905 return static_cast<uint32_t
> (
GetValue (alpha, beta));
938 double v1 = 2 * u1 - 1;
939 double v2 = 2 * u2 - 1;
940 double w = v1 * v1 + v2 * v2;
943 double y = sqrt ((-2 * log (w)) / w);
944 m_next = mean + v2 * y * sqrt (variance);
947 double x1 = mean + v1 * y * sqrt (variance);
949 if (fabs (x1 - mean) <= bound)
969 static TypeId tid =
TypeId (
"ns3::ErlangRandomVariable")
971 .AddConstructor<ErlangRandomVariable> ()
972 .AddAttribute(
"K",
"The k value for the Erlang distribution returned by this RNG stream.",
975 MakeIntegerChecker<uint32_t>())
976 .AddAttribute(
"Lambda",
"The lambda value for the Erlang distribution returned by this RNG stream.",
979 MakeDoubleChecker<double>())
1014 double mean = lambda;
1018 for (
unsigned int i = 0; i < k; ++i)
1030 return static_cast<uint32_t
> (
GetValue (k, lambda));
1057 double r = -mean*std::log (v);
1060 if (bound == 0 || r <= bound)
1072 static TypeId tid =
TypeId (
"ns3::TriangularRandomVariable")
1074 .AddConstructor<TriangularRandomVariable> ()
1075 .AddAttribute(
"Mean",
"The mean value for the triangular distribution returned by this RNG stream.",
1078 MakeDoubleChecker<double>())
1079 .AddAttribute(
"Min",
"The lower bound on the values returned by this RNG stream.",
1082 MakeDoubleChecker<double>())
1083 .AddAttribute(
"Max",
"The upper bound on the values returned by this RNG stream.",
1086 MakeDoubleChecker<double>())
1118 double mode = 3.0 * mean - min - max;
1128 if (u <= (mode - min) / (max - min) )
1130 return min + sqrt (u * (max - min) * (mode - min) );
1134 return max - sqrt ( (1 - u) * (max - min) * (max - mode) );
1141 return static_cast<uint32_t
> (
GetValue (mean, min, max) );
1162 .AddConstructor<ZipfRandomVariable> ()
1163 .AddAttribute(
"N",
"The n value for the Zipf distribution returned by this RNG stream.",
1166 MakeIntegerChecker<uint32_t>())
1167 .AddAttribute(
"Alpha",
"The alpha value for the Zipf distribution returned by this RNG stream.",
1170 MakeDoubleChecker<double>())
1195 for (uint32_t i = 1; i <= n; i++)
1197 m_c += (1.0 / std::pow ((
double)i,alpha));
1208 double sum_prob = 0,zipf_value = 0;
1209 for (uint32_t i = 1; i <=
m_n; i++)
1211 sum_prob +=
m_c / std::pow ((
double)i,
m_alpha);
1224 return static_cast<uint32_t
> (
GetValue (n, alpha));
1245 .AddConstructor<ZetaRandomVariable> ()
1246 .AddAttribute(
"Alpha",
"The alpha value for the zeta distribution returned by this RNG stream.",
1249 MakeDoubleChecker<double>())
1267 m_b = std::pow (2.0, alpha - 1.0);
1289 X = std::floor (std::pow (u, -1.0 / (
m_alpha - 1.0)));
1290 T = std::pow (1.0 + 1.0 / X,
m_alpha - 1.0);
1291 test = v * X * (T - 1.0) / (
m_b - 1.0);
1293 while ( test > (T /
m_b) );
1301 return static_cast<uint32_t
> (
GetValue (alpha));
1320 static TypeId tid =
TypeId (
"ns3::DeterministicRandomVariable")
1322 .AddConstructor<DeterministicRandomVariable> ()
1352 m_data =
new double[length];
1357 for (uint64_t i = 0; i <
m_count; i++)
1404 static TypeId tid =
TypeId (
"ns3::EmpiricalRandomVariable")
1406 .AddConstructor<EmpiricalRandomVariable> ()
1421 if (
emp.size () == 0)
1437 if (r <=
emp.front ().cdf)
1439 return emp.front ().value;
1441 if (r >=
emp.back ().cdf)
1443 return emp.back ().value;
1446 std::vector<ValueCDF>::size_type bottom = 0;
1447 std::vector<ValueCDF>::size_type top =
emp.size () - 1;
1450 std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1451 if (r >=
emp[c].cdf && r <
emp[c + 1].cdf)
1454 emp[c].value,
emp[c + 1].value,
1484 for (std::vector<ValueCDF>::size_type i = 0; i <
emp.size (); ++i)
1489 std::cerr <<
"Empirical Dist error,"
1490 <<
" current value " << current.
value
1491 <<
" prior value " << prior.
value
1492 <<
" current cdf " << current.
cdf
1493 <<
" prior cdf " << prior.
cdf << std::endl;
1502 double v1,
double v2,
double r)
1504 return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));