A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
random-variable-stream.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Georgia Tech Research Corporation
3 * Copyright (c) 2011 Mathieu Lacage
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Authors: Rajib Bhattacharjea<raj.b@gatech.edu>
8 * Hadi Arbabi<marbabi@cs.odu.edu>
9 * Mathieu Lacage <mathieu.lacage@gmail.com>
10 *
11 * Modified by Mitch Watrous <watrous@u.washington.edu>
12 *
13 */
15
16#include "assert.h"
17#include "boolean.h"
18#include "double.h"
19#include "integer.h"
20#include "log.h"
21#include "pointer.h"
22#include "rng-seed-manager.h"
23#include "rng-stream.h"
24#include "string.h"
25
26#include <cmath>
27#include <iostream>
28#include <numbers>
29
30/**
31 * @file
32 * @ingroup randomvariable
33 * ns3::RandomVariableStream and related implementations
34 */
35
36namespace ns3
37{
38
39NS_LOG_COMPONENT_DEFINE("RandomVariableStream");
40
42
45{
46 static TypeId tid = TypeId("ns3::RandomVariableStream")
48 .SetGroupName("Core")
49 .AddAttribute("Stream",
50 "The stream number for this RNG stream. -1 means "
51 "\"allocate a stream automatically\". "
52 "Note that if -1 is set, Get will return -1 so that it "
53 "is not possible to know which "
54 "value was automatically allocated.",
55 IntegerValue(-1),
59 .AddAttribute("Antithetic",
60 "Set this RNG stream to generate antithetic values",
61 BooleanValue(false),
65 return tid;
66}
67
73
78
79void
81{
82 NS_LOG_FUNCTION(this << isAntithetic);
83 m_isAntithetic = isAntithetic;
84}
85
86bool
91
94{
95 auto value = static_cast<uint32_t>(GetValue());
97 << " integer value: " << value << " stream: " << GetStream());
98 return value;
99}
100
101void
103{
104 NS_LOG_FUNCTION(this << stream);
105 // negative values are not legal.
106 NS_ASSERT(stream >= -1);
107 delete m_rng;
108 if (stream == -1)
109 {
110 // The first 2^63 streams are reserved for automatic stream
111 // number assignment.
112 uint64_t nextStream = RngSeedManager::GetNextStreamIndex();
113 NS_ASSERT(nextStream <= ((1ULL) << 63));
114 NS_LOG_INFO(GetInstanceTypeId().GetName() << " automatic stream: " << nextStream);
116 }
117 else
118 {
119 // The last 2^63 streams are reserved for deterministic stream
120 // number assignment.
121 uint64_t base = ((1ULL) << 63);
122 uint64_t target = base + stream;
123 NS_LOG_INFO(GetInstanceTypeId().GetName() << " configured stream: " << stream);
125 }
126 m_stream = stream;
127}
128
129int64_t
131{
132 return m_stream;
133}
134
137{
138 return m_rng;
139}
140
142
143TypeId
146 static TypeId tid =
147 TypeId("ns3::UniformRandomVariable")
149 .SetGroupName("Core")
150 .AddConstructor<UniformRandomVariable>()
151 .AddAttribute("Min",
152 "The lower bound on the values returned by this RNG stream.",
153 DoubleValue(0),
156 .AddAttribute("Max",
157 "The upper bound on the values returned by this RNG stream.",
158 DoubleValue(1.0),
161 return tid;
162}
163
165{
166 // m_min and m_max are initialized after constructor by attributes
167 NS_LOG_FUNCTION(this);
168}
169
170double
172{
173 return m_min;
174}
175
176double
178{
179 return m_max;
180}
181
182double
183UniformRandomVariable::GetValue(double min, double max)
184{
185 double v = min + Peek()->RandU01() * (max - min);
186 if (IsAntithetic())
187 {
188 v = min + (max - v);
189 }
190 NS_LOG_DEBUG("value: " << v << " stream: " << GetStream() << " min: " << min
191 << " max: " << max);
192 return v;
193}
194
197{
198 NS_ASSERT(min <= max);
199 auto v =
200 static_cast<uint32_t>(GetValue(static_cast<double>(min), static_cast<double>(max) + 1.0));
201 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " min: " << min << " max "
202 << max);
203 return v;
204}
205
206double
211
214{
215 auto v = static_cast<uint32_t>(GetValue(m_min, m_max + 1));
216 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream());
217 return v;
218}
219
221
222TypeId
224{
225 static TypeId tid = TypeId("ns3::ConstantRandomVariable")
227 .SetGroupName("Core")
228 .AddConstructor<ConstantRandomVariable>()
229 .AddAttribute("Constant",
230 "The constant value returned by this RNG stream.",
231 DoubleValue(0),
234 return tid;
235}
236
238{
239 // m_constant is initialized after constructor by attributes
240 NS_LOG_FUNCTION(this);
241}
242
243double
245{
246 NS_LOG_FUNCTION(this);
247 return m_constant;
248}
249
250double
252{
253 NS_LOG_DEBUG("value: " << constant << " stream: " << GetStream());
254 return constant;
255}
256
259{
260 NS_LOG_DEBUG("integer value: " << constant << " stream: " << GetStream());
261 return constant;
262}
263
264double
269
271
272TypeId
274{
275 static TypeId tid =
276 TypeId("ns3::SequentialRandomVariable")
278 .SetGroupName("Core")
279 .AddConstructor<SequentialRandomVariable>()
280 .AddAttribute("Min",
281 "The first value of the sequence.",
282 DoubleValue(0),
285 .AddAttribute("Max",
286 "One more than the last value of the sequence.",
287 DoubleValue(0),
290 .AddAttribute("Increment",
291 "The sequence random variable increment.",
292 StringValue("ns3::ConstantRandomVariable[Constant=1]"),
295 .AddAttribute("Consecutive",
296 "The number of times each member of the sequence is repeated.",
297 IntegerValue(1),
300 return tid;
301}
302
304 : m_current(0),
306 m_isCurrentSet(false)
307{
308 // m_min, m_max, m_increment, and m_consecutive are initialized
309 // after constructor by attributes.
310 NS_LOG_FUNCTION(this);
311}
312
313double
315{
316 return m_min;
317}
318
319double
321{
322 return m_max;
323}
324
330
336
337double
339{
340 // Set the current sequence value if it hasn't been set.
341 if (!m_isCurrentSet)
342 {
343 // Start the sequence at its minimum value.
345 m_isCurrentSet = true;
346 }
347
348 // Return a sequential series of values
349 double r = m_current;
351 { // Time to advance to next
353 m_current += m_increment->GetValue();
354 if (m_current >= m_max)
355 {
357 }
358 }
359 NS_LOG_DEBUG("value: " << r << " stream: " << GetStream());
360 return r;
361}
362
364
365TypeId
367{
368 static TypeId tid =
369 TypeId("ns3::ExponentialRandomVariable")
371 .SetGroupName("Core")
372 .AddConstructor<ExponentialRandomVariable>()
373 .AddAttribute("Mean",
374 "The mean of the values returned by this RNG stream.",
375 DoubleValue(1.0),
378 .AddAttribute("Bound",
379 "The upper bound on the values returned by this RNG stream.",
380 DoubleValue(0.0),
383 return tid;
384}
385
387{
388 // m_mean and m_bound are initialized after constructor by attributes
389 NS_LOG_FUNCTION(this);
390}
391
392double
394{
395 return m_mean;
396}
397
398double
400{
401 return m_bound;
402}
403
404double
405ExponentialRandomVariable::GetValue(double mean, double bound)
406{
407 while (true)
408 {
409 // Get a uniform random variable in [0,1].
410 double v = Peek()->RandU01();
411 if (IsAntithetic())
412 {
413 v = (1 - v);
414 }
415
416 // Calculate the exponential random variable.
417 double r = -mean * std::log(v);
418
419 // Use this value if it's acceptable.
420 if (bound == 0 || r <= bound)
421 {
422 NS_LOG_DEBUG("value: " << r << " stream: " << GetStream() << " mean: " << mean
423 << " bound: " << bound);
424 return r;
425 }
426 }
427}
428
431{
432 NS_LOG_FUNCTION(this << mean << bound);
433 auto v = static_cast<uint32_t>(GetValue(mean, bound));
434 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " mean: " << mean
435 << " bound: " << bound);
436 return v;
437}
438
439double
444
446
447TypeId
449{
450 static TypeId tid =
451 TypeId("ns3::ParetoRandomVariable")
453 .SetGroupName("Core")
454 .AddConstructor<ParetoRandomVariable>()
455 .AddAttribute(
456 "Scale",
457 "The scale parameter for the Pareto distribution returned by this RNG stream.",
458 DoubleValue(1.0),
461 .AddAttribute(
462 "Shape",
463 "The shape parameter for the Pareto distribution returned by this RNG stream.",
464 DoubleValue(2.0),
467 .AddAttribute(
468 "Bound",
469 "The upper bound on the values returned by this RNG stream (if non-zero).",
470 DoubleValue(0.0),
473 return tid;
474}
475
477{
478 // m_shape, m_shape, and m_bound are initialized after constructor
479 // by attributes
480 NS_LOG_FUNCTION(this);
481}
482
483double
485{
486 return m_scale;
487}
488
489double
491{
492 return m_shape;
493}
494
495double
497{
498 return m_bound;
499}
500
501double
502ParetoRandomVariable::GetValue(double scale, double shape, double bound)
503{
504 while (true)
505 {
506 // Get a uniform random variable in [0,1].
507 double v = Peek()->RandU01();
508 if (IsAntithetic())
509 {
510 v = (1 - v);
511 }
512
513 // Calculate the Pareto random variable.
514 double r = (scale * (1.0 / std::pow(v, 1.0 / shape)));
515
516 // Use this value if it's acceptable.
517 if (bound == 0 || r <= bound)
518 {
519 NS_LOG_DEBUG("value: " << r << " stream: " << GetStream() << " scale: " << scale
520 << " shape: " << shape << " bound: " << bound);
521 return r;
522 }
523 }
524}
525
528{
529 auto v = static_cast<uint32_t>(GetValue(scale, shape, bound));
530 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " scale: " << scale
531 << " shape: " << shape << " bound: " << bound);
532 return v;
533}
534
535double
540
542
543TypeId
545{
546 static TypeId tid =
547 TypeId("ns3::WeibullRandomVariable")
549 .SetGroupName("Core")
550 .AddConstructor<WeibullRandomVariable>()
551 .AddAttribute(
552 "Scale",
553 "The scale parameter for the Weibull distribution returned by this RNG stream.",
554 DoubleValue(1.0),
557 .AddAttribute(
558 "Shape",
559 "The shape parameter for the Weibull distribution returned by this RNG stream.",
560 DoubleValue(1),
563 .AddAttribute("Bound",
564 "The upper bound on the values returned by this RNG stream.",
565 DoubleValue(0.0),
568 return tid;
569}
570
572{
573 // m_scale, m_shape, and m_bound are initialized after constructor
574 // by attributes
575 NS_LOG_FUNCTION(this);
576}
577
578double
580{
581 return m_scale;
582}
583
584double
586{
587 return m_shape;
588}
589
590double
592{
593 return m_bound;
594}
595
596double
597WeibullRandomVariable::GetMean(double scale, double shape)
598{
599 NS_LOG_FUNCTION(scale << shape);
600 return scale * std::tgamma(1 + (1 / shape));
601}
602
603double
605{
606 NS_LOG_FUNCTION(this);
607 return GetMean(m_scale, m_shape);
608}
609
610double
611WeibullRandomVariable::GetValue(double scale, double shape, double bound)
612{
613 double exponent = 1.0 / shape;
614 while (true)
615 {
616 // Get a uniform random variable in [0,1].
617 double v = Peek()->RandU01();
618 if (IsAntithetic())
619 {
620 v = (1 - v);
621 }
622
623 // Calculate the Weibull random variable.
624 double r = scale * std::pow(-std::log(v), exponent);
625
626 // Use this value if it's acceptable.
627 if (bound == 0 || r <= bound)
628 {
629 NS_LOG_DEBUG("value: " << r << " stream: " << GetStream() << " scale: " << scale
630 << " shape: " << shape << " bound: " << bound);
631 return r;
632 }
633 }
634}
635
638{
639 auto v = static_cast<uint32_t>(GetValue(scale, shape, bound));
640 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " scale: " << scale
641 << " shape: " << shape << " bound: " << bound);
642 return v;
643}
644
645double
651
653
654TypeId
656{
657 static TypeId tid =
658 TypeId("ns3::NormalRandomVariable")
660 .SetGroupName("Core")
661 .AddConstructor<NormalRandomVariable>()
662 .AddAttribute("Mean",
663 "The mean value for the normal distribution returned by this RNG stream.",
664 DoubleValue(0.0),
667 .AddAttribute(
668 "Variance",
669 "The variance value for the normal distribution returned by this RNG stream.",
670 DoubleValue(1.0),
673 .AddAttribute("Bound",
674 "The bound on the values returned by this RNG stream.",
678 return tid;
679}
680
682 : m_nextValid(false)
683{
684 // m_mean, m_variance, and m_bound are initialized after constructor
685 // by attributes
686 NS_LOG_FUNCTION(this);
687}
688
689void
691{
692 m_variance = stdDev * stdDev;
693}
694
695double
697{
698 return m_mean;
699}
700
701double
703{
704 return m_variance;
705}
706
707double
709{
710 return sqrt(m_variance);
711}
712
713double
715{
716 return m_bound;
717}
718
719double
720NormalRandomVariable::GetValue(double mean, double variance, double bound)
721{
722 if (m_nextValid)
723 { // use previously generated
724 m_nextValid = false;
725 double x2 = mean + m_v2 * m_y * std::sqrt(variance);
726 if (std::fabs(x2 - mean) <= bound)
727 {
728 NS_LOG_DEBUG("value: " << x2 << " stream: " << GetStream() << " mean: " << mean
729 << " variance: " << variance << " bound: " << bound);
730 return x2;
731 }
732 }
733 while (true)
734 { // See Simulation Modeling and Analysis p. 466 (Averill Law)
735 // for algorithm; basically a Box-Muller transform:
736 // http://en.wikipedia.org/wiki/Box-Muller_transform
737 double u1 = Peek()->RandU01();
738 double u2 = Peek()->RandU01();
739 if (IsAntithetic())
740 {
741 u1 = (1 - u1);
742 u2 = (1 - u2);
743 }
744 double v1 = 2 * u1 - 1;
745 double v2 = 2 * u2 - 1;
746 double w = v1 * v1 + v2 * v2;
747 if (0.0 < w && w < 1.0)
748 { // Got good pair
749 double y = std::sqrt((-2 * std::log(w)) / w);
750 double x1 = mean + v1 * y * std::sqrt(variance);
751 // if x1 is in bounds, return it, cache v2 and y
752 if (std::fabs(x1 - mean) <= bound)
753 {
754 m_nextValid = true;
755 m_y = y;
756 m_v2 = v2;
757 NS_LOG_DEBUG("value: " << x1 << " stream: " << GetStream() << " mean: " << mean
758 << " variance: " << variance << " bound: " << bound);
759 return x1;
760 }
761 // otherwise try and return the other if it is valid
762 double x2 = mean + v2 * y * std::sqrt(variance);
763 if (std::fabs(x2 - mean) <= bound)
764 {
765 m_nextValid = false;
766 NS_LOG_DEBUG("value: " << x2 << " stream: " << GetStream() << " mean: " << mean
767 << " variance: " << variance << " bound: " << bound);
768 return x2;
769 }
770 // otherwise, just run this loop again
771 }
772 }
773}
774
777{
778 auto v = static_cast<uint32_t>(GetValue(mean, variance, bound));
779 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " mean: " << mean
780 << " variance: " << variance << " bound: " << bound);
781 return v;
782}
783
784double
789
791
792TypeId
794{
795 static TypeId tid =
796 TypeId("ns3::LogNormalRandomVariable")
798 .SetGroupName("Core")
799 .AddConstructor<LogNormalRandomVariable>()
800 .AddAttribute(
801 "Mu",
802 "The mu value for the log-normal distribution returned by this RNG stream.",
803 DoubleValue(0.0),
806 .AddAttribute(
807 "Sigma",
808 "The sigma value for the log-normal distribution returned by this RNG stream.",
809 DoubleValue(1.0),
812 return tid;
813}
814
816 : m_nextValid(false)
817{
818 // m_mu and m_sigma are initialized after constructor by
819 // attributes
820 NS_LOG_FUNCTION(this);
821}
822
823double
825{
826 return m_mu;
827}
828
829double
831{
832 return m_sigma;
833}
834
835// The code from this function was adapted from the GNU Scientific
836// Library 1.8:
837/* randist/lognormal.c
838 *
839 * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
840 *
841 * SPDX-License-Identifier: GPL-2.0-or-later
842 */
843/* The lognormal distribution has the form
844
845 p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
846
847 for x > 0. Lognormal random numbers are the exponentials of
848 gaussian random numbers */
849double
850LogNormalRandomVariable::GetValue(double mu, double sigma)
851{
852 if (m_nextValid)
853 { // use previously generated
854 m_nextValid = false;
855 double v = std::exp(sigma * m_v2 * m_normal + mu);
856 NS_LOG_DEBUG("value: " << v << " stream: " << GetStream() << " mu: " << mu
857 << " sigma: " << sigma);
858 return v;
859 }
860
861 double v1;
862 double v2;
863 double r2;
864 double normal;
865 double x;
866
867 do
868 {
869 /* choose x,y in uniform square (-1,-1) to (+1,+1) */
870
871 double u1 = Peek()->RandU01();
872 double u2 = Peek()->RandU01();
873 if (IsAntithetic())
874 {
875 u1 = (1 - u1);
876 u2 = (1 - u2);
877 }
878
879 v1 = -1 + 2 * u1;
880 v2 = -1 + 2 * u2;
881
882 /* see if it is in the unit circle */
883 r2 = v1 * v1 + v2 * v2;
884 } while (r2 > 1.0 || r2 == 0);
885
886 m_normal = std::sqrt(-2.0 * std::log(r2) / r2);
887 normal = v1 * m_normal;
888 m_nextValid = true;
889 m_v2 = v2;
890
891 x = std::exp(sigma * normal + mu);
892 NS_LOG_DEBUG("value: " << x << " stream: " << GetStream() << " mu: " << mu
893 << " sigma: " << sigma);
894
895 return x;
896}
897
900{
901 auto v = static_cast<uint32_t>(GetValue(mu, sigma));
902 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " mu: " << mu
903 << " sigma: " << sigma);
904 return v;
905}
906
907double
912
914
915TypeId
917{
918 static TypeId tid =
919 TypeId("ns3::GammaRandomVariable")
921 .SetGroupName("Core")
922 .AddConstructor<GammaRandomVariable>()
923 .AddAttribute("Alpha",
924 "The alpha value for the gamma distribution returned by this RNG stream.",
925 DoubleValue(1.0),
928 .AddAttribute("Beta",
929 "The beta value for the gamma distribution returned by this RNG stream.",
930 DoubleValue(1.0),
933 return tid;
934}
935
937 : m_nextValid(false)
938{
939 // m_alpha and m_beta are initialized after constructor by
940 // attributes
941 NS_LOG_FUNCTION(this);
942}
943
944double
946{
947 return m_alpha;
948}
949
950double
952{
953 return m_beta;
954}
955
956/*
957 The code for the following generator functions was adapted from ns-2
958 tools/ranvar.cc
959
960 Originally the algorithm was devised by Marsaglia in 2000:
961 G. Marsaglia, W. W. Tsang: A simple method for generating Gamma variables
962 ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
963
964 The Gamma distribution density function has the form
965
966 x^(alpha-1) * exp(-x/beta)
967 p(x; alpha, beta) = ----------------------------
968 beta^alpha * Gamma(alpha)
969
970 for x > 0.
971*/
972double
973GammaRandomVariable::GetValue(double alpha, double beta)
974{
975 if (alpha < 1)
976 {
977 double u = Peek()->RandU01();
978 if (IsAntithetic())
979 {
980 u = (1 - u);
981 }
982 double v = GetValue(1.0 + alpha, beta) * std::pow(u, 1.0 / alpha);
983 NS_LOG_DEBUG("value: " << v << " stream: " << GetStream() << " alpha: " << alpha
984 << " beta: " << beta);
985 return GetValue(1.0 + alpha, beta) * std::pow(u, 1.0 / alpha);
986 }
987
988 double x;
989 double v;
990 double u;
991 double d = alpha - 1.0 / 3.0;
992 double c = (1.0 / 3.0) / std::sqrt(d);
993
994 while (true)
995 {
996 do
997 {
998 // Get a value from a normal distribution that has mean
999 // zero, variance 1, and no bound.
1000 double mean = 0.0;
1001 double variance = 1.0;
1003 x = GetNormalValue(mean, variance, bound);
1004
1005 v = 1.0 + c * x;
1006 } while (v <= 0);
1007
1008 v = v * v * v;
1009 u = Peek()->RandU01();
1010 if (IsAntithetic())
1011 {
1012 u = (1 - u);
1013 }
1014 if (u < 1 - 0.0331 * x * x * x * x)
1015 {
1016 break;
1017 }
1018 if (std::log(u) < 0.5 * x * x + d * (1 - v + std::log(v)))
1019 {
1020 break;
1021 }
1022 }
1023
1024 double value = beta * d * v;
1025 NS_LOG_DEBUG("value: " << value << " stream: " << GetStream() << " alpha: " << alpha
1026 << " beta: " << beta);
1027 return value;
1028}
1029
1030double
1035
1036double
1037GammaRandomVariable::GetNormalValue(double mean, double variance, double bound)
1038{
1039 if (m_nextValid)
1040 { // use previously generated
1041 m_nextValid = false;
1042 double x2 = mean + m_v2 * m_y * std::sqrt(variance);
1043 if (std::fabs(x2 - mean) <= bound)
1044 {
1045 NS_LOG_DEBUG("value: " << x2 << " stream: " << GetStream() << " mean: " << mean
1046 << " variance: " << variance << " bound: " << bound);
1047 return x2;
1048 }
1049 }
1050 while (true)
1051 { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1052 // for algorithm; basically a Box-Muller transform:
1053 // http://en.wikipedia.org/wiki/Box-Muller_transform
1054 double u1 = Peek()->RandU01();
1055 double u2 = Peek()->RandU01();
1056 if (IsAntithetic())
1057 {
1058 u1 = (1 - u1);
1059 u2 = (1 - u2);
1060 }
1061 double v1 = 2 * u1 - 1;
1062 double v2 = 2 * u2 - 1;
1063 double w = v1 * v1 + v2 * v2;
1064 if (0.0 < w && w < 1.0)
1065 { // Got good pair
1066 double y = std::sqrt((-2 * std::log(w)) / w);
1067 double x1 = mean + v1 * y * std::sqrt(variance);
1068 // if x1 is in bounds, return it, cache v2 an y
1069 if (std::fabs(x1 - mean) <= bound)
1070 {
1071 m_nextValid = true;
1072 m_y = y;
1073 m_v2 = v2;
1074 NS_LOG_DEBUG("value: " << x1 << " stream: " << GetStream() << " mean: " << mean
1075 << " variance: " << variance << " bound: " << bound);
1076 return x1;
1077 }
1078 // otherwise try and return the other if it is valid
1079 double x2 = mean + v2 * y * std::sqrt(variance);
1080 if (std::fabs(x2 - mean) <= bound)
1081 {
1082 m_nextValid = false;
1083 NS_LOG_DEBUG("value: " << x2 << " stream: " << GetStream() << " mean: " << mean
1084 << " variance: " << variance << " bound: " << bound);
1085 return x2;
1086 }
1087 // otherwise, just run this loop again
1088 }
1089 }
1090}
1091
1093
1094TypeId
1096{
1097 static TypeId tid =
1098 TypeId("ns3::ErlangRandomVariable")
1100 .SetGroupName("Core")
1101 .AddConstructor<ErlangRandomVariable>()
1102 .AddAttribute("K",
1103 "The k value for the Erlang distribution returned by this RNG stream.",
1104 IntegerValue(1),
1107 .AddAttribute(
1108 "Lambda",
1109 "The lambda value for the Erlang distribution returned by this RNG stream.",
1110 DoubleValue(1.0),
1113 return tid;
1114}
1115
1117{
1118 // m_k and m_lambda are initialized after constructor by attributes
1119 NS_LOG_FUNCTION(this);
1120}
1121
1124{
1125 return m_k;
1126}
1127
1128double
1130{
1131 return m_lambda;
1132}
1133
1134/*
1135 The code for the following generator functions was adapted from ns-2
1136 tools/ranvar.cc
1137
1138 The Erlang distribution density function has the form
1139
1140 x^(k-1) * exp(-x/lambda)
1141 p(x; k, lambda) = ---------------------------
1142 lambda^k * (k-1)!
1143
1144 for x > 0.
1145*/
1146double
1148{
1149 double mean = lambda;
1150 double bound = 0.0;
1151
1152 double result = 0;
1153 for (unsigned int i = 0; i < k; ++i)
1154 {
1155 result += GetExponentialValue(mean, bound);
1156 }
1157 NS_LOG_DEBUG("value: " << result << " stream: " << GetStream() << " k: " << k
1158 << " lambda: " << lambda);
1159 return result;
1160}
1161
1164{
1165 auto v = static_cast<uint32_t>(GetValue(k, lambda));
1166 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " k: " << k
1167 << " lambda: " << lambda);
1168 return v;
1169}
1170
1171double
1176
1177double
1179{
1180 while (true)
1181 {
1182 // Get a uniform random variable in [0,1].
1183 double v = Peek()->RandU01();
1184 if (IsAntithetic())
1185 {
1186 v = (1 - v);
1187 }
1188
1189 // Calculate the exponential random variable.
1190 double r = -mean * std::log(v);
1191
1192 // Use this value if it's acceptable.
1193 if (bound == 0 || r <= bound)
1194 {
1195 NS_LOG_DEBUG("value: " << r << " stream: " << GetStream() << " mean:: " << mean
1196 << " bound: " << bound);
1197 return r;
1198 }
1199 }
1200}
1201
1203
1204TypeId
1206{
1207 static TypeId tid =
1208 TypeId("ns3::TriangularRandomVariable")
1210 .SetGroupName("Core")
1211 .AddConstructor<TriangularRandomVariable>()
1212 .AddAttribute(
1213 "Mean",
1214 "The mean value for the triangular distribution returned by this RNG stream.",
1215 DoubleValue(0.5),
1218 .AddAttribute("Min",
1219 "The lower bound on the values returned by this RNG stream.",
1220 DoubleValue(0.0),
1223 .AddAttribute("Max",
1224 "The upper bound on the values returned by this RNG stream.",
1225 DoubleValue(1.0),
1228 return tid;
1229}
1230
1232{
1233 // m_mean, m_min, and m_max are initialized after constructor by
1234 // attributes
1235 NS_LOG_FUNCTION(this);
1236}
1237
1238double
1240{
1241 return m_mean;
1242}
1243
1244double
1246{
1247 return m_min;
1248}
1249
1250double
1252{
1253 return m_max;
1254}
1255
1256double
1257TriangularRandomVariable::GetValue(double mean, double min, double max)
1258{
1259 // Calculate the mode.
1260 double mode = 3.0 * mean - min - max;
1261
1262 // Get a uniform random variable in [0,1].
1263 double u = Peek()->RandU01();
1264 if (IsAntithetic())
1265 {
1266 u = (1 - u);
1267 }
1268
1269 // Calculate the triangular random variable.
1270 if (u <= (mode - min) / (max - min))
1271 {
1272 double v = min + std::sqrt(u * (max - min) * (mode - min));
1273 NS_LOG_DEBUG("value: " << v << " stream: " << GetStream() << " mean: " << mean
1274 << " min: " << min << " max: " << max);
1275 return v;
1276 }
1277 else
1278 {
1279 double v = max - std::sqrt((1 - u) * (max - min) * (max - mode));
1280 NS_LOG_DEBUG("value: " << v << " stream: " << GetStream() << " mean: " << mean
1281 << " min: " << min << " max: " << max);
1282 return v;
1283 }
1284}
1285
1288{
1289 auto v = static_cast<uint32_t>(GetValue(mean, min, max));
1290 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " mean: " << mean
1291 << " min: " << min << " max: " << max);
1292 return v;
1293}
1294
1295double
1300
1302
1303TypeId
1305{
1306 static TypeId tid =
1307 TypeId("ns3::ZipfRandomVariable")
1309 .SetGroupName("Core")
1310 .AddConstructor<ZipfRandomVariable>()
1311 .AddAttribute("N",
1312 "The n value for the Zipf distribution returned by this RNG stream.",
1313 IntegerValue(1),
1316 .AddAttribute("Alpha",
1317 "The alpha value for the Zipf distribution returned by this RNG stream.",
1318 DoubleValue(0.0),
1321 return tid;
1322}
1323
1325{
1326 // m_n and m_alpha are initialized after constructor by attributes
1327 NS_LOG_FUNCTION(this);
1328}
1329
1332{
1333 return m_n;
1334}
1335
1336double
1338{
1339 return m_alpha;
1340}
1341
1342double
1344{
1345 // Calculate the normalization constant c.
1346 m_c = 0.0;
1347 for (uint32_t i = 1; i <= n; i++)
1348 {
1349 m_c += (1.0 / std::pow(static_cast<double>(i), alpha));
1350 }
1351 m_c = 1.0 / m_c;
1352
1353 // Get a uniform random variable in [0,1].
1354 double u = Peek()->RandU01();
1355 if (IsAntithetic())
1356 {
1357 u = (1 - u);
1358 }
1359
1360 double sum_prob = 0;
1361 double zipf_value = 0;
1362 for (uint32_t i = 1; i <= n; i++)
1363 {
1364 sum_prob += m_c / std::pow(static_cast<double>(i), alpha);
1365 if (sum_prob > u)
1366 {
1367 zipf_value = i;
1368 break;
1369 }
1370 }
1371 NS_LOG_DEBUG("value: " << zipf_value << " stream: " << GetStream() << " n: " << n
1372 << " alpha: " << alpha);
1373 return zipf_value;
1374}
1375
1378{
1379 NS_LOG_FUNCTION(this << n << alpha);
1380 auto v = static_cast<uint32_t>(GetValue(n, alpha));
1381 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " n: " << n
1382 << " alpha: " << alpha);
1383 return v;
1384}
1385
1386double
1391
1393
1394TypeId
1396{
1397 static TypeId tid =
1398 TypeId("ns3::ZetaRandomVariable")
1400 .SetGroupName("Core")
1401 .AddConstructor<ZetaRandomVariable>()
1402 .AddAttribute("Alpha",
1403 "The alpha value for the zeta distribution returned by this RNG stream.",
1404 DoubleValue(3.14),
1407 return tid;
1408}
1409
1411{
1412 // m_alpha is initialized after constructor by attributes
1413 NS_LOG_FUNCTION(this);
1414}
1415
1416double
1418{
1419 return m_alpha;
1420}
1421
1422double
1424{
1425 m_b = std::pow(2.0, alpha - 1.0);
1426
1427 double u;
1428 double v;
1429 double X;
1430 double T;
1431 double test;
1432
1433 do
1434 {
1435 // Get a uniform random variable in [0,1].
1436 u = Peek()->RandU01();
1437 if (IsAntithetic())
1438 {
1439 u = (1 - u);
1440 }
1441
1442 // Get a uniform random variable in [0,1].
1443 v = Peek()->RandU01();
1444 if (IsAntithetic())
1445 {
1446 v = (1 - v);
1447 }
1448
1449 X = std::floor(std::pow(u, -1.0 / (alpha - 1.0)));
1450 T = std::pow(1.0 + 1.0 / X, alpha - 1.0);
1451 test = v * X * (T - 1.0) / (m_b - 1.0);
1452 } while (test > (T / m_b));
1453 NS_LOG_DEBUG("value: " << X << " stream: " << GetStream() << " alpha: " << alpha);
1454 return X;
1455}
1456
1459{
1460 auto v = static_cast<uint32_t>(GetValue(alpha));
1461 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " alpha: " << alpha);
1462 return v;
1463}
1464
1465double
1470
1472
1473TypeId
1475{
1476 static TypeId tid = TypeId("ns3::DeterministicRandomVariable")
1478 .SetGroupName("Core")
1479 .AddConstructor<DeterministicRandomVariable>();
1480 return tid;
1481}
1482
1484 : m_count(0),
1485 m_next(0),
1486 m_data(nullptr)
1487{
1488 NS_LOG_FUNCTION(this);
1489}
1490
1492{
1493 // Delete any values currently set.
1494 NS_LOG_FUNCTION(this);
1495 if (m_data != nullptr)
1496 {
1497 delete[] m_data;
1498 }
1499}
1500
1501void
1502DeterministicRandomVariable::SetValueArray(const std::vector<double>& values)
1503{
1504 SetValueArray(values.data(), values.size());
1505}
1506
1507void
1508DeterministicRandomVariable::SetValueArray(const double* values, std::size_t length)
1509{
1510 NS_LOG_FUNCTION(this << values << length);
1511 // Delete any values currently set.
1512 if (m_data != nullptr)
1513 {
1514 delete[] m_data;
1515 }
1516
1517 // Make room for the values being set.
1518 m_data = new double[length];
1519 m_count = length;
1520 m_next = length;
1521
1522 // Copy the values.
1523 for (std::size_t i = 0; i < m_count; i++)
1524 {
1525 m_data[i] = values[i];
1526 }
1527}
1528
1529double
1531{
1532 // Make sure the array has been set.
1533 NS_ASSERT(m_count > 0);
1534
1535 if (m_next == m_count)
1536 {
1537 m_next = 0;
1538 }
1539 double v = m_data[m_next++];
1540 NS_LOG_DEBUG("value: " << v << " stream: " << GetStream());
1541 return v;
1542}
1543
1545
1546TypeId
1548{
1549 static TypeId tid =
1550 TypeId("ns3::EmpiricalRandomVariable")
1552 .SetGroupName("Core")
1553 .AddConstructor<EmpiricalRandomVariable>()
1554 .AddAttribute("Interpolate",
1555 "Treat the CDF as a smooth distribution and interpolate, "
1556 "default is to treat the CDF as a histogram and sample.",
1557 BooleanValue(false),
1560 return tid;
1561}
1562
1568
1569bool
1571{
1572 NS_LOG_FUNCTION(this << interpolate);
1573 bool prev = m_interpolate;
1574 m_interpolate = interpolate;
1575 return prev;
1576}
1577
1578bool
1580{
1581 NS_LOG_FUNCTION(this << value);
1582 if (!m_validated)
1583 {
1584 Validate();
1585 }
1586
1587 // Get a uniform random variable in [0, 1].
1588 double r = Peek()->RandU01();
1589 if (IsAntithetic())
1590 {
1591 r = (1 - r);
1592 }
1593
1594 value = r;
1595 bool valid = false;
1596 // check extrema
1597 if (r <= m_empCdf.begin()->first)
1598 {
1599 value = m_empCdf.begin()->second; // Less than first
1600 valid = true;
1601 }
1602 else if (r >= m_empCdf.rbegin()->first)
1603 {
1604 value = m_empCdf.rbegin()->second; // Greater than last
1605 valid = true;
1606 }
1607 return valid;
1608}
1609
1610double
1612{
1613 double value;
1614 if (PreSample(value))
1615 {
1616 return value;
1617 }
1618
1619 // value now has the (unused) URNG selector
1620 if (m_interpolate)
1621 {
1622 value = DoInterpolate(value);
1623 }
1624 else
1625 {
1626 value = DoSampleCDF(value);
1627 }
1628 NS_LOG_DEBUG("value: " << value << " stream: " << GetStream());
1629 return value;
1630}
1631
1632double
1634{
1635 NS_LOG_FUNCTION(this << r);
1636
1637 // Find first CDF that is greater than r
1638 auto bound = m_empCdf.upper_bound(r);
1639
1640 return bound->second;
1641}
1642
1643double
1645{
1646 NS_LOG_FUNCTION(this);
1647
1648 double value;
1649 if (PreSample(value))
1650 {
1651 return value;
1652 }
1653
1654 // value now has the (unused) URNG selector
1655 value = DoInterpolate(value);
1656 return value;
1657}
1658
1659double
1661{
1662 NS_LOG_FUNCTION(this << r);
1663
1664 // Return a value from the empirical distribution
1665 // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1666
1667 // search
1668 auto upper = m_empCdf.upper_bound(r);
1669 auto lower = std::prev(upper, 1);
1670
1671 if (upper == m_empCdf.begin())
1672 {
1673 lower = upper;
1674 }
1675
1676 // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1677 double c1 = lower->first;
1678 double c2 = upper->first;
1679 double v1 = lower->second;
1680 double v2 = upper->second;
1681
1682 double value = (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1683 return value;
1684}
1685
1686void
1688{
1689 NS_LOG_FUNCTION(this << v << c);
1690
1691 auto vPrevious = m_empCdf.find(c);
1692
1693 if (vPrevious != m_empCdf.end())
1694 {
1695 NS_LOG_WARN("Empirical CDF already has a value " << vPrevious->second << " for CDF " << c
1696 << ". Overwriting it with value " << v
1697 << ".");
1698 }
1699
1700 m_empCdf[c] = v;
1701}
1702
1703void
1705{
1706 NS_LOG_FUNCTION(this);
1707
1708 if (m_empCdf.empty())
1709 {
1710 NS_FATAL_ERROR("CDF is not initialized");
1711 }
1712
1713 double vPrev = m_empCdf.begin()->second;
1714
1715 // Check if values are non-decreasing
1716 for (const auto& cdfPair : m_empCdf)
1717 {
1718 const auto& vCurr = cdfPair.second;
1719
1720 if (vCurr < vPrev)
1721 {
1722 NS_FATAL_ERROR("Empirical distribution has decreasing CDF values. Current CDF: "
1723 << vCurr << ", prior CDF: " << vPrev);
1724 }
1725
1726 vPrev = vCurr;
1727 }
1728
1729 // Bounds check on CDF endpoints
1730 auto firstCdfPair = m_empCdf.begin();
1731 auto lastCdfPair = m_empCdf.rbegin();
1732
1733 if (firstCdfPair->first < 0.0)
1734 {
1735 NS_FATAL_ERROR("Empirical distribution has invalid first CDF value. CDF: "
1736 << firstCdfPair->first << ", Value: " << firstCdfPair->second);
1737 }
1738
1739 if (lastCdfPair->first > 1.0)
1740 {
1741 NS_FATAL_ERROR("Empirical distribution has invalid last CDF value. CDF: "
1742 << lastCdfPair->first << ", Value: " << lastCdfPair->second);
1743 }
1744
1745 m_validated = true;
1746}
1747
1749
1750TypeId
1752{
1753 static TypeId tid =
1754 TypeId("ns3::BinomialRandomVariable")
1756 .SetGroupName("Core")
1757 .AddConstructor<BinomialRandomVariable>()
1758 .AddAttribute("Trials",
1759 "The number of trials.",
1760 IntegerValue(10),
1763 .AddAttribute("Probability",
1764 "The probability of success in each trial.",
1765 DoubleValue(0.5),
1768 return tid;
1769}
1770
1772{
1773 // m_trials and m_probability are initialized after constructor by attributes
1774 NS_LOG_FUNCTION(this);
1775}
1776
1777double
1779{
1780 double successes = 0;
1781
1782 for (uint32_t i = 0; i < trials; ++i)
1783 {
1784 double v = Peek()->RandU01();
1785 if (IsAntithetic())
1786 {
1787 v = (1 - v);
1788 }
1789
1790 if (v <= probability)
1791 {
1792 successes += 1;
1793 }
1794 }
1795 NS_LOG_DEBUG("value: " << successes << " stream: " << GetStream() << " trials: " << trials
1796 << " probability: " << probability);
1797 return successes;
1798}
1799
1802{
1803 auto v = static_cast<uint32_t>(GetValue(trials, probability));
1804 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream() << " trials: " << trials
1805 << " probability: " << probability);
1806 return v;
1807}
1808
1809double
1814
1816
1817TypeId
1819{
1820 static TypeId tid =
1821 TypeId("ns3::BernoulliRandomVariable")
1823 .SetGroupName("Core")
1824 .AddConstructor<BernoulliRandomVariable>()
1825 .AddAttribute("Probability",
1826 "The probability of the random variable returning a value of 1.",
1827 DoubleValue(0.5),
1830 return tid;
1831}
1832
1834{
1835 // m_probability is initialized after constructor by attributes
1836 NS_LOG_FUNCTION(this);
1837}
1838
1839double
1841{
1842 double v = Peek()->RandU01();
1843 if (IsAntithetic())
1844 {
1845 v = (1 - v);
1846 }
1847
1848 double value = (v <= probability) ? 1.0 : 0.0;
1849 NS_LOG_DEBUG("value: " << value << " stream: " << GetStream()
1850 << " probability: " << probability);
1851 return value;
1852}
1853
1856{
1857 auto v = static_cast<uint32_t>(GetValue(probability));
1858 NS_LOG_DEBUG("integer value: " << v << " stream: " << GetStream()
1859 << " probability: " << probability);
1860 return v;
1861}
1862
1863double
1868
1870
1871TypeId
1873{
1874 static TypeId tid =
1875 TypeId("ns3::LaplacianRandomVariable")
1877 .SetGroupName("Core")
1878 .AddConstructor<LaplacianRandomVariable>()
1879 .AddAttribute("Location",
1880 "The location parameter for the Laplacian distribution returned by this "
1881 "RNG stream.",
1882 DoubleValue(0.0),
1885 .AddAttribute(
1886 "Scale",
1887 "The scale parameter for the Laplacian distribution returned by this RNG stream.",
1888 DoubleValue(1.0),
1891 .AddAttribute("Bound",
1892 "The bound on the values returned by this RNG stream.",
1893 DoubleValue(0.0),
1896 return tid;
1897}
1898
1903
1904double
1906{
1907 return m_location;
1908}
1909
1910double
1912{
1913 return m_scale;
1914}
1915
1916double
1918{
1919 return m_bound;
1920}
1921
1922double
1923LaplacianRandomVariable::GetValue(double location, double scale, double bound)
1924{
1925 NS_LOG_FUNCTION(this << location << scale << bound);
1926 NS_ABORT_MSG_IF(scale <= 0, "Scale parameter should be larger than 0");
1927
1928 while (true)
1929 {
1930 // Get a uniform random variable in [-0.5,0.5].
1931 auto v = (Peek()->RandU01() - 0.5);
1932 if (IsAntithetic())
1933 {
1934 v = (1 - v);
1935 }
1936
1937 // Calculate the laplacian random variable.
1938 const auto sgn = (v > 0) ? 1 : ((v < 0) ? -1 : 0);
1939 const auto r = location - (scale * sgn * std::log(1.0 - (2.0 * std::abs(v))));
1940
1941 // Use this value if it's acceptable.
1942 if (bound == 0.0 || std::fabs(r - location) <= bound)
1943 {
1944 return r;
1945 }
1946 }
1947}
1948
1951{
1952 NS_LOG_FUNCTION(this << location << scale << bound);
1953 return static_cast<uint32_t>(GetValue(location, scale, bound));
1954}
1955
1956double
1962
1963double
1965{
1966 NS_LOG_FUNCTION(scale);
1967 return 2.0 * std::pow(scale, 2.0);
1968}
1969
1970double
1975
1977
1978TypeId
1980{
1981 static TypeId tid =
1982 TypeId("ns3::LargestExtremeValueRandomVariable")
1984 .SetGroupName("Core")
1985 .AddConstructor<LargestExtremeValueRandomVariable>()
1986 .AddAttribute("Location",
1987 "The location parameter for the Largest Extreme Value distribution "
1988 "returned by this RNG stream.",
1989 DoubleValue(0.0),
1992 .AddAttribute("Scale",
1993 "The scale parameter for the Largest Extreme Value distribution "
1994 "returned by this RNG stream.",
1995 DoubleValue(1.0),
1998 return tid;
1999}
2000
2005
2006double
2011
2012double
2017
2018double
2020{
2021 NS_LOG_FUNCTION(this << location << scale);
2022 NS_ABORT_MSG_IF(scale <= 0, "Scale parameter should be larger than 0");
2023
2024 // Get a uniform random variable in [0,1].
2025 auto v = Peek()->RandU01();
2026 if (IsAntithetic())
2027 {
2028 v = (1 - v);
2029 }
2030
2031 // Calculate the largest extreme value random variable.
2032 const auto t = std::log(v) * (-1.0);
2033 const auto r = location - (scale * std::log(t));
2034
2035 return r;
2036}
2037
2040{
2041 NS_LOG_FUNCTION(this << location << scale);
2042 return static_cast<uint32_t>(GetValue(location, scale));
2043}
2044
2045double
2051
2052double
2054{
2055 NS_LOG_FUNCTION(location << scale);
2056 return (location + (scale * std::numbers::egamma));
2057}
2058
2059double
2064
2065double
2067{
2068 NS_LOG_FUNCTION(scale);
2069 return std::pow((scale * std::numbers::pi), 2) / 6.0;
2070}
2071
2072double
2077
2078} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::BooleanValue attribute value declarations.
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
uint32_t r
uint32_t v
uint32_t u
return result
The Bernoulli distribution Random Number Generator (RNG).
double GetValue() override
Get the next random value drawn from the distribution.
double m_probability
The probability of success.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
The binomial distribution Random Number Generator (RNG).
double m_probability
The probability of success in each trial.
double GetValue() override
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
uint32_t m_trials
The number of trials.
The Random Number Generator (RNG) that returns a constant.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value drawn from the distribution.
ConstantRandomVariable()
Creates a constant RNG with the default constant value.
double GetConstant() const
Get the constant value returned by this RNG stream.
double m_constant
The constant value returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
The Random Number Generator (RNG) that returns a predetermined sequence.
double GetValue() override
Get the next random value drawn from the distribution.
std::size_t m_next
Position of the next value in the array of values.
void SetValueArray(const std::vector< double > &values)
Sets the array of values that holds the predetermined sequence.
static TypeId GetTypeId()
Register this type.
double * m_data
Array of values to return in sequence.
DeterministicRandomVariable()
Creates a deterministic RNG that will have a predetermined sequence of values.
std::size_t m_count
Size of the array of values.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
The Random Number Generator (RNG) that has a specified empirical distribution.
bool SetInterpolate(bool interpolate)
Switch the mode between sampling the CDF and interpolating.
void CDF(double v, double c)
Specifies a point in the empirical distribution.
bool PreSample(double &value)
Do the initial rng draw and check against the extrema.
double DoSampleCDF(double r)
Sample the CDF as a histogram (without interpolation).
double GetValue() override
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
bool m_interpolate
If true GetValue will interpolate, otherwise treat CDF as normal histogram.
bool m_validated
true once the CDF has been validated.
double DoInterpolate(double r)
Linear interpolation between two points on the CDF to estimate the value at r.
virtual double Interpolate()
Returns the next value in the empirical distribution using linear interpolation.
EmpiricalRandomVariable()
Creates an empirical RNG that has a specified, empirical distribution, and configured for interpolati...
void Validate()
Check that the CDF is valid.
std::map< double, double > m_empCdf
The map of CDF points (x, F(x)).
The Erlang distribution Random Number Generator (RNG) that allows stream numbers to be set determinis...
double m_lambda
The lambda value for the Erlang distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
double GetExponentialValue(double mean, double bound)
Returns a random double from an exponential distribution with the specified mean and upper bound.
static TypeId GetTypeId()
Register this type.
uint32_t GetK() const
Returns the k value for the Erlang distribution returned by this RNG stream.
double GetLambda() const
Returns the lambda value for the Erlang distribution returned by this RNG stream.
uint32_t m_k
The k value for the Erlang distribution returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
ErlangRandomVariable()
Creates an Erlang distribution RNG with the default values for k and lambda.
The exponential distribution Random Number Generator (RNG).
ExponentialRandomVariable()
Creates an exponential distribution RNG with the default values for the mean and upper bound.
double GetBound() const
Get the configured upper bound of this RNG.
double m_mean
The mean value of the unbounded exponential distribution.
double GetMean() const
Get the configured mean value of this RNG.
double m_bound
The upper bound on values that can be returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value drawn from the distribution.
The gamma distribution Random Number Generator (RNG) that allows stream numbers to be set determinist...
double GetValue() override
Get the next random value drawn from the distribution.
GammaRandomVariable()
Creates a gamma distribution RNG with the default values for alpha and beta.
double m_alpha
The alpha value for the gamma distribution returned by this RNG stream.
double GetNormalValue(double mean, double variance, double bound)
Returns a random double from a normal distribution with the specified mean, variance,...
double m_y
The algorithm produces two values at a time.
double m_v2
The algorithm produces two values at a time.
bool m_nextValid
True if the next normal value is valid.
static TypeId GetTypeId()
Register this type.
double GetAlpha() const
Returns the alpha value for the gamma distribution returned by this RNG stream.
double GetBeta() const
Returns the beta value for the gamma distribution returned by this RNG stream.
double m_beta
The beta value for the gamma distribution returned by this RNG stream.
Hold a signed integer type.
Definition integer.h:34
The laplacian distribution Random Number Generator (RNG).
double m_location
The location value of the laplacian distribution.
double GetValue() override
Get the next random value drawn from the distribution.
double GetBound() const
Get the configured bound of this RNG.
static TypeId GetTypeId()
Register this type.
double m_bound
The bound on values that can be returned by this RNG stream.
LaplacianRandomVariable()
Creates an unbounded laplacian distribution RNG with the default values for the location and the scal...
double GetLocation() const
Get the configured location value of this RNG.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double m_scale
The scale value of the laplacian distribution.
double GetScale() const
Get the configured scale value of this RNG.
double GetVariance() const
Returns the variance value for the laplacian distribution returned by this RNG stream.
The Largest Extreme Value distribution Random Number Generator (RNG).
double m_scale
The scale value of the Largest Extreme Value distribution.
double GetMean() const
Returns the mean value for the Largest Extreme Value distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
LargestExtremeValueRandomVariable()
Creates a Largest Extreme Value distribution RNG with the default values for the location and the sca...
double GetVariance() const
Returns the variance value for the Largest Extreme Value distribution returned by this RNG stream.
double GetScale() const
Get the configured scale value of this RNG.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double GetLocation() const
Get the configured location value of this RNG.
double m_location
The location value of the Largest Extreme Value distribution.
The log-normal distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
double GetMu() const
Returns the mu value for the log-normal distribution returned by this RNG stream.
double m_v2
The algorithm produces two values at a time.
double GetSigma() const
Returns the sigma value for the log-normal distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
bool m_nextValid
True if m_normal is valid.
double m_mu
The mu value for the log-normal distribution returned by this RNG stream.
double m_sigma
The sigma value for the log-normal distribution returned by this RNG stream.
LogNormalRandomVariable()
Creates a log-normal distribution RNG with the default values for mu and sigma.
double m_normal
The algorithm produces two values at a time.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
The normal (Gaussian) distribution Random Number Generator (RNG) that allows stream numbers to be set...
double m_y
The algorithm produces two values at a time.
double GetBound() const
Returns the bound on values that can be returned by this RNG stream.
double GetStdDev() const
Returns the standard deviation value for the normal distribution returned by this RNG stream.
double GetVariance() const
Returns the variance value for the normal distribution returned by this RNG stream.
void SetStdDev(double stdDev)
Set standard deviation of this normal distribution RNG stream.
double m_mean
The mean value for the normal distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double GetMean() const
Returns the mean value for the normal distribution returned by this RNG stream.
static constexpr double INFINITE_VALUE
Large constant to bound the range.
double m_variance
The variance value for the normal distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double m_bound
The bound on values that can be returned by this RNG stream.
bool m_nextValid
True if the next value is valid.
NormalRandomVariable()
Creates a normal distribution RNG with the default values for the mean, variance, and bound.
double m_v2
The algorithm produces two values at a time.
TypeId GetInstanceTypeId() const final
Get the most derived TypeId for this Object.
Definition object.cc:80
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
The Pareto distribution Random Number Generator (RNG).
double GetShape() const
Returns the shape parameter for the Pareto distribution returned by this RNG stream.
double m_scale
The scale parameter for the Pareto distribution returned by this RNG stream.
ParetoRandomVariable()
Creates a Pareto distribution RNG with the default values for the mean, the shape,...
static TypeId GetTypeId()
Register this type.
double m_shape
The shape parameter for the Pareto distribution returned by this RNG stream.
double GetScale() const
Returns the scale parameter for the Pareto distribution returned by this RNG stream.
double m_bound
The upper bound on values that can be returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double GetValue() override
Get the next random value drawn from the distribution.
double GetBound() const
Returns the upper bound on values that can be returned by this RNG stream.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
The basic uniform Random Number Generator (RNG).
static TypeId GetTypeId()
Register this type.
RngStream * Peek() const
Get the pointer to the underlying RngStream.
bool IsAntithetic() const
Check if antithetic values will be generated.
virtual double GetValue()=0
Get the next random value drawn from the distribution.
~RandomVariableStream() override
Destructor.
bool m_isAntithetic
Indicates if antithetic values should be generated by this RNG stream.
void SetAntithetic(bool isAntithetic)
Specify whether antithetic values should be generated.
int64_t m_stream
The stream number for the RngStream.
RandomVariableStream()
Default constructor.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
RngStream * m_rng
Pointer to the underlying RngStream.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
int64_t GetStream() const
Returns the stream number for the RngStream.
static uint64_t GetNextStreamIndex()
Get the next automatically assigned stream index.
static uint64_t GetRun()
Get the current run number.
static uint32_t GetSeed()
Get the current seed value which will be used by all subsequently instantiated RandomVariableStream o...
Combined Multiple-Recursive Generator MRG32k3a.
Definition rng-stream.h:39
double RandU01()
Generate the next random number for this stream.
The Random Number Generator (RNG) that returns a pattern of sequential values.
uint32_t m_currentConsecutive
The number of times the current distinct value has been repeated.
double m_min
The first value of the sequence.
Ptr< RandomVariableStream > GetIncrement() const
Get the increment for the sequence.
uint32_t m_consecutive
The number of times each distinct value is repeated.
static TypeId GetTypeId()
Register this type.
double m_current
The current sequence value.
double m_max
Strict upper bound on the sequence.
Ptr< RandomVariableStream > m_increment
Increment between distinct values.
double GetValue() override
Get the next random value drawn from the distribution.
uint32_t GetConsecutive() const
Get the number of times each distinct value of the sequence is repeated before incrementing to the ne...
double GetMax() const
Get the limit of the sequence, which is (at least) one more than the last value of the sequence.
SequentialRandomVariable()
Creates a sequential RNG with the default values for the sequence parameters.
double GetMin() const
Get the first value of the sequence.
bool m_isCurrentSet
Indicates if the current sequence value has been properly initialized.
Hold variables of type string.
Definition string.h:45
The triangular distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
double GetValue() override
Get the next random value drawn from the distribution.
double GetMean() const
Returns the mean value for the triangular distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double m_mean
The mean value for the triangular distribution returned by this RNG stream.
double m_max
The upper bound on values that can be returned by this RNG stream.
double GetMax() const
Returns the upper bound on values that can be returned by this RNG stream.
TriangularRandomVariable()
Creates a triangular distribution RNG with the default values for the mean, lower bound,...
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double m_min
The lower bound on values that can be returned by this RNG stream.
double GetMin() const
Returns the lower bound for the triangular distribution returned by this RNG stream.
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
The uniform distribution Random Number Generator (RNG).
UniformRandomVariable()
Creates a uniform distribution RNG with the default range.
uint32_t GetInteger() override
Get the next random value drawn from the distribution.
double GetMax() const
Get the upper bound on values returned by GetValue().
double m_min
The lower bound on values that can be returned by this RNG stream.
double m_max
The upper bound on values that can be returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value drawn from the distribution.
double GetMin() const
Get the lower bound on randoms returned by GetValue().
The Weibull distribution Random Number Generator (RNG) which allows stream numbers to be set determin...
double m_shape
The shape parameter for the Weibull distribution returned by this RNG stream.
double m_bound
The upper bound on values that can be returned by this RNG stream.
double m_scale
The scale parameter for the Weibull distribution returned by this RNG stream.
double GetBound() const
Returns the upper bound on values that can be returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
WeibullRandomVariable()
Creates a Weibull distribution RNG with the default values for the scale, shape, and upper bound.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
double GetScale() const
Returns the scale parameter for the Weibull distribution returned by this RNG stream.
double GetMean() const
Returns the mean value for the Weibull distribution returned by this RNG stream.
double GetShape() const
Returns the shape parameter for the Weibull distribution returned by this RNG stream.
The zeta distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
static TypeId GetTypeId()
Register this type.
double m_alpha
The alpha value for the zeta distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
ZetaRandomVariable()
Creates a zeta distribution RNG with the default value for alpha.
double GetAlpha() const
Returns the alpha value for the zeta distribution returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double m_b
Just for calculus simplifications.
The Zipf distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
uint32_t GetN() const
Returns the n value for the Zipf distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double m_c
The normalization constant.
double GetAlpha() const
Returns the alpha value for the Zipf distribution returned by this RNG stream.
ZipfRandomVariable()
Creates a Zipf distribution RNG with the default values for n and alpha.
double m_alpha
The alpha value for the Zipf distribution returned by this RNG stream.
uint32_t m_n
The n value for the Zipf distribution returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double GetValue() override
Get the next random value drawn from the distribution.
ns3::DoubleValue attribute value declarations and template implementations.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:250
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:273
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:99
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:260
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:274
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
ns3::IntegerValue attribute value declarations and template implementations.
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeChecker > MakeIntegerChecker()
Definition integer.h:99
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition integer.h:35
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32
-ns3 Test suite for the ns3 wrapper script
ns3::PointerValue attribute value declarations and template implementations.
ns3::RandomVariableStream declaration, and related classes.
ns3::RngSeedManager declaration.
ns3::RngStream declaration.
ns3::StringValue attribute value declarations.
uint32_t prev