A Discrete-Event Network Simulator
API
random-variable-stream.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2006 Georgia Tech Research Corporation
4 * Copyright (c) 2011 Mathieu Lacage
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation;
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Authors: Rajib Bhattacharjea<raj.b@gatech.edu>
20 * Hadi Arbabi<marbabi@cs.odu.edu>
21 * Mathieu Lacage <mathieu.lacage@gmail.com>
22 *
23 * Modified by Mitch Watrous <watrous@u.washington.edu>
24 *
25 */
27#include "assert.h"
28#include "boolean.h"
29#include "double.h"
30#include "integer.h"
31#include "string.h"
32#include "pointer.h"
33#include "log.h"
34#include "rng-stream.h"
35#include "rng-seed-manager.h"
36#include <cmath>
37#include <iostream>
38#include <algorithm> // upper_bound
39
46namespace ns3 {
47
48NS_LOG_COMPONENT_DEFINE ("RandomVariableStream");
49
50NS_OBJECT_ENSURE_REGISTERED (RandomVariableStream);
51
52TypeId
54{
55 static TypeId tid = TypeId ("ns3::RandomVariableStream")
56 .SetParent<Object> ()
57 .SetGroupName ("Core")
58 .AddAttribute ("Stream",
59 "The stream number for this RNG stream. -1 means \"allocate a stream automatically\". "
60 "Note that if -1 is set, Get will return -1 so that it is not possible to know which "
61 "value was automatically allocated.",
62 IntegerValue (-1),
65 MakeIntegerChecker<int64_t>())
66 .AddAttribute ("Antithetic", "Set this RNG stream to generate antithetic values",
67 BooleanValue (false),
71 ;
72 return tid;
73}
74
76 : m_rng (0)
77{
78 NS_LOG_FUNCTION (this);
79}
81{
82 NS_LOG_FUNCTION (this);
83 delete m_rng;
84}
85
86void
88{
89 NS_LOG_FUNCTION (this << isAntithetic);
90 m_isAntithetic = isAntithetic;
91}
92bool
94{
95 NS_LOG_FUNCTION (this);
96 return m_isAntithetic;
97}
98void
100{
101 NS_LOG_FUNCTION (this << stream);
102 // negative values are not legal.
103 NS_ASSERT (stream >= -1);
104 delete m_rng;
105 if (stream == -1)
106 {
107 // The first 2^63 streams are reserved for automatic stream
108 // number assignment.
109 uint64_t nextStream = RngSeedManager::GetNextStreamIndex ();
110 NS_ASSERT (nextStream <= ((1ULL) << 63));
112 nextStream,
114 }
115 else
116 {
117 // The last 2^63 streams are reserved for deterministic stream
118 // number assignment.
119 uint64_t base = ((1ULL) << 63);
120 uint64_t target = base + stream;
122 target,
124 }
125 m_stream = stream;
126}
127int64_t
129{
130 NS_LOG_FUNCTION (this);
131 return m_stream;
132}
133
134RngStream *
136{
137 NS_LOG_FUNCTION (this);
138 return m_rng;
139}
140
142
143TypeId
145{
146 static TypeId tid = TypeId ("ns3::UniformRandomVariable")
148 .SetGroupName ("Core")
149 .AddConstructor<UniformRandomVariable> ()
150 .AddAttribute ("Min", "The lower bound on the values returned by this RNG stream.",
151 DoubleValue (0),
153 MakeDoubleChecker<double>())
154 .AddAttribute ("Max", "The upper bound on the values returned by this RNG stream.",
155 DoubleValue (1.0),
157 MakeDoubleChecker<double>())
158 ;
159 return tid;
160}
162{
163 // m_min and m_max are initialized after constructor by attributes
164 NS_LOG_FUNCTION (this);
165}
166
167double
169{
170 NS_LOG_FUNCTION (this);
171 return m_min;
172}
173double
175{
176 NS_LOG_FUNCTION (this);
177 return m_max;
178}
179
180double
182{
183 NS_LOG_FUNCTION (this << min << max);
184 double v = min + Peek ()->RandU01 () * (max - min);
185 if (IsAntithetic ())
186 {
187 v = min + (max - v);
188 }
189 return v;
190}
193{
194 NS_LOG_FUNCTION (this << min << max);
195 NS_ASSERT (min <= max);
196 return static_cast<uint32_t> ( GetValue ((double) (min), (double) (max) + 1.0) );
197}
198
199double
201{
202 NS_LOG_FUNCTION (this);
203 return GetValue (m_min, m_max);
204}
207{
208 NS_LOG_FUNCTION (this);
209 return (uint32_t)GetValue (m_min, m_max + 1);
210}
211
213
214TypeId
216{
217 static TypeId tid = TypeId ("ns3::ConstantRandomVariable")
219 .SetGroupName ("Core")
220 .AddConstructor<ConstantRandomVariable> ()
221 .AddAttribute ("Constant", "The constant value returned by this RNG stream.",
222 DoubleValue (0),
224 MakeDoubleChecker<double>())
225 ;
226 return tid;
227}
229{
230 // m_constant is initialized after constructor by attributes
231 NS_LOG_FUNCTION (this);
232}
233
234double
236{
237 NS_LOG_FUNCTION (this);
238 return m_constant;
239}
240
241double
243{
244 NS_LOG_FUNCTION (this << constant);
245 return constant;
246}
249{
250 NS_LOG_FUNCTION (this << constant);
251 return constant;
252}
253
254double
256{
257 NS_LOG_FUNCTION (this);
258 return GetValue (m_constant);
259}
262{
263 NS_LOG_FUNCTION (this);
264 return (uint32_t)GetValue (m_constant);
265}
266
268
269TypeId
271{
272 static TypeId tid = TypeId ("ns3::SequentialRandomVariable")
274 .SetGroupName ("Core")
275 .AddConstructor<SequentialRandomVariable> ()
276 .AddAttribute ("Min", "The first value of the sequence.",
277 DoubleValue (0),
279 MakeDoubleChecker<double>())
280 .AddAttribute ("Max", "One more than the last value of the sequence.",
281 DoubleValue (0),
283 MakeDoubleChecker<double>())
284 .AddAttribute ("Increment", "The sequence random variable increment.",
285 StringValue ("ns3::ConstantRandomVariable[Constant=1]"),
287 MakePointerChecker<RandomVariableStream> ())
288 .AddAttribute ("Consecutive", "The number of times each member of the sequence is repeated.",
289 IntegerValue (1),
291 MakeIntegerChecker<uint32_t>());
292 return tid;
293}
295 :
296 m_current (0),
297 m_currentConsecutive (0),
298 m_isCurrentSet (false)
299{
300 // m_min, m_max, m_increment, and m_consecutive are initialized
301 // after constructor by attributes.
302 NS_LOG_FUNCTION (this);
303}
304
305double
307{
308 NS_LOG_FUNCTION (this);
309 return m_min;
310}
311
312double
314{
315 NS_LOG_FUNCTION (this);
316 return m_max;
317}
318
321{
322 NS_LOG_FUNCTION (this);
323 return m_increment;
324}
325
328{
329 NS_LOG_FUNCTION (this);
330 return m_consecutive;
331}
332
333double
335{
336 // Set the current sequence value if it hasn't been set.
337 NS_LOG_FUNCTION (this);
338 if (!m_isCurrentSet)
339 {
340 // Start the sequence at its minimium value.
342 m_isCurrentSet = true;
343 }
344
345 // Return a sequential series of values
346 double r = m_current;
348 { // Time to advance to next
351 if (m_current >= m_max)
352 {
354 }
355 }
356 return r;
357}
358
361{
362 NS_LOG_FUNCTION (this);
363 return (uint32_t)GetValue ();
364}
365
367
368TypeId
370{
371 static TypeId tid = TypeId ("ns3::ExponentialRandomVariable")
373 .SetGroupName ("Core")
374 .AddConstructor<ExponentialRandomVariable> ()
375 .AddAttribute ("Mean", "The mean of the values returned by this RNG stream.",
376 DoubleValue (1.0),
378 MakeDoubleChecker<double>())
379 .AddAttribute ("Bound", "The upper bound on the values returned by this RNG stream.",
380 DoubleValue (0.0),
382 MakeDoubleChecker<double>())
383 ;
384 return tid;
385}
387{
388 // m_mean and m_bound are initialized after constructor by attributes
389 NS_LOG_FUNCTION (this);
390}
391
392double
394{
395 NS_LOG_FUNCTION (this);
396 return m_mean;
397}
398double
400{
401 NS_LOG_FUNCTION (this);
402 return m_bound;
403}
404
405double
406ExponentialRandomVariable::GetValue (double mean, double bound)
407{
408 NS_LOG_FUNCTION (this << mean << bound);
409 while (1)
410 {
411 // Get a uniform random variable in [0,1].
412 double v = Peek ()->RandU01 ();
413 if (IsAntithetic ())
414 {
415 v = (1 - v);
416 }
417
418 // Calculate the exponential random variable.
419 double r = -mean*std::log (v);
420
421 // Use this value if it's acceptable.
422 if (bound == 0 || r <= bound)
423 {
424 return r;
425 }
426 }
427}
430{
431 NS_LOG_FUNCTION (this << mean << bound);
432 return static_cast<uint32_t> ( GetValue (mean, bound) );
433}
434
435double
437{
438 NS_LOG_FUNCTION (this);
439 return GetValue (m_mean, m_bound);
440}
443{
444 NS_LOG_FUNCTION (this);
445 return (uint32_t)GetValue (m_mean, m_bound);
446}
447
449
450TypeId
452{
453 static TypeId tid = TypeId ("ns3::ParetoRandomVariable")
455 .SetGroupName ("Core")
456 .AddConstructor<ParetoRandomVariable> ()
457 .AddAttribute ("Scale", "The scale parameter for the Pareto distribution returned by this RNG stream.",
458 DoubleValue (1.0),
460 MakeDoubleChecker<double>())
461 .AddAttribute ("Shape", "The shape parameter for the Pareto distribution returned by this RNG stream.",
462 DoubleValue (2.0),
464 MakeDoubleChecker<double>())
465 .AddAttribute ("Bound", "The upper bound on the values returned by this RNG stream (if non-zero).",
466 DoubleValue (0.0),
468 MakeDoubleChecker<double>())
469 ;
470 return tid;
471}
473{
474 // m_shape, m_shape, and m_bound are initialized after constructor
475 // by attributes
476 NS_LOG_FUNCTION (this);
477}
478
479double
481{
482 NS_LOG_FUNCTION (this);
483 return m_scale;
484}
485
486double
488{
489 NS_LOG_FUNCTION (this);
490 return m_shape;
491}
492
493double
495{
496 NS_LOG_FUNCTION (this);
497 return m_bound;
498}
499
500double
501ParetoRandomVariable::GetValue (double scale, double shape, double bound)
502{
503 // Calculate the scale parameter.
504 NS_LOG_FUNCTION (this << scale << shape << bound);
505
506 while (1)
507 {
508 // Get a uniform random variable in [0,1].
509 double v = Peek ()->RandU01 ();
510 if (IsAntithetic ())
511 {
512 v = (1 - v);
513 }
514
515 // Calculate the Pareto random variable.
516 double r = (scale * ( 1.0 / std::pow (v, 1.0 / shape)));
517
518 // Use this value if it's acceptable.
519 if (bound == 0 || r <= bound)
520 {
521 return r;
522 }
523 }
524}
527{
528 NS_LOG_FUNCTION (this << scale << shape << bound);
529 return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
530}
531
532double
534{
535 NS_LOG_FUNCTION (this);
536 return GetValue (m_scale, m_shape, m_bound);
537}
540{
541 NS_LOG_FUNCTION (this);
543}
544
546
547TypeId
549{
550 static TypeId tid = TypeId ("ns3::WeibullRandomVariable")
552 .SetGroupName ("Core")
553 .AddConstructor<WeibullRandomVariable> ()
554 .AddAttribute ("Scale", "The scale parameter for the Weibull distribution returned by this RNG stream.",
555 DoubleValue (1.0),
557 MakeDoubleChecker<double>())
558 .AddAttribute ("Shape", "The shape parameter for the Weibull distribution returned by this RNG stream.",
559 DoubleValue (1),
561 MakeDoubleChecker<double>())
562 .AddAttribute ("Bound", "The upper bound on the values returned by this RNG stream.",
563 DoubleValue (0.0),
565 MakeDoubleChecker<double>())
566 ;
567 return tid;
568}
570{
571 // m_scale, m_shape, and m_bound are initialized after constructor
572 // by attributes
573 NS_LOG_FUNCTION (this);
574}
575
576double
578{
579 NS_LOG_FUNCTION (this);
580 return m_scale;
581}
582double
584{
585 NS_LOG_FUNCTION (this);
586 return m_shape;
587}
588double
590{
591 NS_LOG_FUNCTION (this);
592 return m_bound;
593}
594
595double
596WeibullRandomVariable::GetValue (double scale, double shape, double bound)
597{
598 NS_LOG_FUNCTION (this << scale << shape << bound);
599 double exponent = 1.0 / shape;
600 while (1)
601 {
602 // Get a uniform random variable in [0,1].
603 double v = Peek ()->RandU01 ();
604 if (IsAntithetic ())
605 {
606 v = (1 - v);
607 }
608
609 // Calculate the Weibull random variable.
610 double r = scale * std::pow ( -std::log (v), exponent);
611
612 // Use this value if it's acceptable.
613 if (bound == 0 || r <= bound)
614 {
615 return r;
616 }
617 }
618}
621{
622 NS_LOG_FUNCTION (this << scale << shape << bound);
623 return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
624}
625
626double
628{
629 NS_LOG_FUNCTION (this);
630 return GetValue (m_scale, m_shape, m_bound);
631}
634{
635 NS_LOG_FUNCTION (this);
637}
638
640
641const double NormalRandomVariable::INFINITE_VALUE = 1e307;
642
643TypeId
645{
646 static TypeId tid = TypeId ("ns3::NormalRandomVariable")
648 .SetGroupName ("Core")
649 .AddConstructor<NormalRandomVariable> ()
650 .AddAttribute ("Mean", "The mean value for the normal distribution returned by this RNG stream.",
651 DoubleValue (0.0),
653 MakeDoubleChecker<double>())
654 .AddAttribute ("Variance", "The variance value for the normal distribution returned by this RNG stream.",
655 DoubleValue (1.0),
657 MakeDoubleChecker<double>())
658 .AddAttribute ("Bound", "The bound on the values returned by this RNG stream.",
661 MakeDoubleChecker<double>())
662 ;
663 return tid;
664}
666 :
667 m_nextValid (false)
668{
669 // m_mean, m_variance, and m_bound are initialized after constructor
670 // by attributes
671 NS_LOG_FUNCTION (this);
672}
673
674double
676{
677 NS_LOG_FUNCTION (this);
678 return m_mean;
679}
680double
682{
683 NS_LOG_FUNCTION (this);
684 return m_variance;
685}
686double
688{
689 NS_LOG_FUNCTION (this);
690 return m_bound;
691}
692
693double
694NormalRandomVariable::GetValue (double mean, double variance, double bound)
695{
696 NS_LOG_FUNCTION (this << mean << variance << bound);
697 if (m_nextValid)
698 { // use previously generated
699 m_nextValid = false;
700 double x2 = mean + m_v2 * m_y * std::sqrt (variance);
701 if (std::fabs (x2 - mean) <= bound)
702 {
703 return x2;
704 }
705 }
706 while (1)
707 { // See Simulation Modeling and Analysis p. 466 (Averill Law)
708 // for algorithm; basically a Box-Muller transform:
709 // http://en.wikipedia.org/wiki/Box-Muller_transform
710 double u1 = Peek ()->RandU01 ();
711 double u2 = Peek ()->RandU01 ();
712 if (IsAntithetic ())
713 {
714 u1 = (1 - u1);
715 u2 = (1 - u2);
716 }
717 double v1 = 2 * u1 - 1;
718 double v2 = 2 * u2 - 1;
719 double w = v1 * v1 + v2 * v2;
720 if (w <= 1.0)
721 { // Got good pair
722 double y = std::sqrt ((-2 * std::log (w)) / w);
723 double x1 = mean + v1 * y * std::sqrt (variance);
724 // if x1 is in bounds, return it, cache v2 and y
725 if (std::fabs (x1 - mean) <= bound)
726 {
727 m_nextValid = true;
728 m_y = y;
729 m_v2 = v2;
730 return x1;
731 }
732 // otherwise try and return the other if it is valid
733 double x2 = mean + v2 * y * std::sqrt (variance);
734 if (std::fabs (x2 - mean) <= bound)
735 {
736 m_nextValid = false;
737 return x2;
738 }
739 // otherwise, just run this loop again
740 }
741 }
742}
743
746{
747 NS_LOG_FUNCTION (this << mean << variance << bound);
748 return static_cast<uint32_t> ( GetValue (mean, variance, bound) );
749}
750
751double
753{
754 NS_LOG_FUNCTION (this);
756}
759{
760 NS_LOG_FUNCTION (this);
762}
763
765
766TypeId
768{
769 static TypeId tid = TypeId ("ns3::LogNormalRandomVariable")
771 .SetGroupName ("Core")
772 .AddConstructor<LogNormalRandomVariable> ()
773 .AddAttribute ("Mu", "The mu value for the log-normal distribution returned by this RNG stream.",
774 DoubleValue (0.0),
776 MakeDoubleChecker<double>())
777 .AddAttribute ("Sigma", "The sigma value for the log-normal distribution returned by this RNG stream.",
778 DoubleValue (1.0),
780 MakeDoubleChecker<double>())
781 ;
782 return tid;
783}
785{
786 // m_mu and m_sigma are initialized after constructor by
787 // attributes
788 NS_LOG_FUNCTION (this);
789}
790
791double
793{
794 NS_LOG_FUNCTION (this);
795 return m_mu;
796}
797double
799{
800 NS_LOG_FUNCTION (this);
801 return m_sigma;
802}
803
804// The code from this function was adapted from the GNU Scientific
805// Library 1.8:
806/* randist/lognormal.c
807 *
808 * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
809 *
810 * This program is free software; you can redistribute it and/or modify
811 * it under the terms of the GNU General Public License as published by
812 * the Free Software Foundation; either version 2 of the License, or (at
813 * your option) any later version.
814 *
815 * This program is distributed in the hope that it will be useful, but
816 * WITHOUT ANY WARRANTY; without even the implied warranty of
817 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
818 * General Public License for more details.
819 *
820 * You should have received a copy of the GNU General Public License
821 * along with this program; if not, write to the Free Software
822 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
823 */
824/* The lognormal distribution has the form
825
826 p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
827
828 for x > 0. Lognormal random numbers are the exponentials of
829 gaussian random numbers */
830double
831LogNormalRandomVariable::GetValue (double mu, double sigma)
832{
833 double v1, v2, r2, normal, x;
834
835 NS_LOG_FUNCTION (this << mu << sigma);
836
837 do
838 {
839 /* choose x,y in uniform square (-1,-1) to (+1,+1) */
840
841 double u1 = Peek ()->RandU01 ();
842 double u2 = Peek ()->RandU01 ();
843 if (IsAntithetic ())
844 {
845 u1 = (1 - u1);
846 u2 = (1 - u2);
847 }
848
849 v1 = -1 + 2 * u1;
850 v2 = -1 + 2 * u2;
851
852 /* see if it is in the unit circle */
853 r2 = v1 * v1 + v2 * v2;
854 }
855 while (r2 > 1.0 || r2 == 0);
856
857 normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
858
859 x = std::exp (sigma * normal + mu);
860
861 return x;
862}
863
866{
867 NS_LOG_FUNCTION (this << mu << sigma);
868 return static_cast<uint32_t> ( GetValue (mu, sigma));
869}
870
871double
873{
874 NS_LOG_FUNCTION (this);
875 return GetValue (m_mu, m_sigma);
876}
879{
880 NS_LOG_FUNCTION (this);
881 return (uint32_t)GetValue (m_mu, m_sigma);
882}
883
885
886TypeId
888{
889 static TypeId tid = TypeId ("ns3::GammaRandomVariable")
891 .SetGroupName ("Core")
892 .AddConstructor<GammaRandomVariable> ()
893 .AddAttribute ("Alpha", "The alpha value for the gamma distribution returned by this RNG stream.",
894 DoubleValue (1.0),
896 MakeDoubleChecker<double>())
897 .AddAttribute ("Beta", "The beta value for the gamma distribution returned by this RNG stream.",
898 DoubleValue (1.0),
900 MakeDoubleChecker<double>())
901 ;
902 return tid;
903}
905 :
906 m_nextValid (false)
907{
908 // m_alpha and m_beta are initialized after constructor by
909 // attributes
910 NS_LOG_FUNCTION (this);
911}
912
913double
915{
916 NS_LOG_FUNCTION (this);
917 return m_alpha;
918}
919double
921{
922 NS_LOG_FUNCTION (this);
923 return m_beta;
924}
925
926/*
927 The code for the following generator functions was adapted from ns-2
928 tools/ranvar.cc
929
930 Originally the algorithm was devised by Marsaglia in 2000:
931 G. Marsaglia, W. W. Tsang: A simple method for generating Gamma variables
932 ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
933
934 The Gamma distribution density function has the form
935
936 x^(alpha-1) * exp(-x/beta)
937 p(x; alpha, beta) = ----------------------------
938 beta^alpha * Gamma(alpha)
939
940 for x > 0.
941*/
942double
944{
945 NS_LOG_FUNCTION (this << alpha << beta);
946 if (alpha < 1)
947 {
948 double u = Peek ()->RandU01 ();
949 if (IsAntithetic ())
950 {
951 u = (1 - u);
952 }
953 return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
954 }
955
956 double x, v, u;
957 double d = alpha - 1.0 / 3.0;
958 double c = (1.0 / 3.0) / std::sqrt (d);
959
960 while (1)
961 {
962 do
963 {
964 // Get a value from a normal distribution that has mean
965 // zero, variance 1, and no bound.
966 double mean = 0.0;
967 double variance = 1.0;
969 x = GetNormalValue (mean, variance, bound);
970
971 v = 1.0 + c * x;
972 }
973 while (v <= 0);
974
975 v = v * v * v;
976 u = Peek ()->RandU01 ();
977 if (IsAntithetic ())
978 {
979 u = (1 - u);
980 }
981 if (u < 1 - 0.0331 * x * x * x * x)
982 {
983 break;
984 }
985 if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
986 {
987 break;
988 }
989 }
990
991 return beta * d * v;
992}
993
996{
997 NS_LOG_FUNCTION (this << alpha << beta);
998 return static_cast<uint32_t> ( GetValue (alpha, beta));
999}
1000
1001double
1003{
1004 NS_LOG_FUNCTION (this);
1005 return GetValue (m_alpha, m_beta);
1006}
1009{
1010 NS_LOG_FUNCTION (this);
1011 return (uint32_t)GetValue (m_alpha, m_beta);
1012}
1013
1014double
1015GammaRandomVariable::GetNormalValue (double mean, double variance, double bound)
1016{
1017 NS_LOG_FUNCTION (this << mean << variance << bound);
1018 if (m_nextValid)
1019 { // use previously generated
1020 m_nextValid = false;
1021 double x2 = mean + m_v2 * m_y * std::sqrt (variance);
1022 if (std::fabs (x2 - mean) <= bound)
1023 {
1024 return x2;
1025 }
1026 }
1027 while (1)
1028 { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1029 // for algorithm; basically a Box-Muller transform:
1030 // http://en.wikipedia.org/wiki/Box-Muller_transform
1031 double u1 = Peek ()->RandU01 ();
1032 double u2 = Peek ()->RandU01 ();
1033 if (IsAntithetic ())
1034 {
1035 u1 = (1 - u1);
1036 u2 = (1 - u2);
1037 }
1038 double v1 = 2 * u1 - 1;
1039 double v2 = 2 * u2 - 1;
1040 double w = v1 * v1 + v2 * v2;
1041 if (w <= 1.0)
1042 { // Got good pair
1043 double y = std::sqrt ((-2 * std::log (w)) / w);
1044 double x1 = mean + v1 * y * std::sqrt (variance);
1045 // if x1 is in bounds, return it, cache v2 an y
1046 if (std::fabs (x1 - mean) <= bound)
1047 {
1048 m_nextValid = true;
1049 m_y = y;
1050 m_v2 = v2;
1051 return x1;
1052 }
1053 // otherwise try and return the other if it is valid
1054 double x2 = mean + v2 * y * std::sqrt (variance);
1055 if (std::fabs (x2 - mean) <= bound)
1056 {
1057 m_nextValid = false;
1058 return x2;
1059 }
1060 // otherwise, just run this loop again
1061 }
1062 }
1063}
1064
1066
1067TypeId
1069{
1070 static TypeId tid = TypeId ("ns3::ErlangRandomVariable")
1072 .SetGroupName ("Core")
1073 .AddConstructor<ErlangRandomVariable> ()
1074 .AddAttribute ("K", "The k value for the Erlang distribution returned by this RNG stream.",
1075 IntegerValue (1),
1077 MakeIntegerChecker<uint32_t>())
1078 .AddAttribute ("Lambda", "The lambda value for the Erlang distribution returned by this RNG stream.",
1079 DoubleValue (1.0),
1081 MakeDoubleChecker<double>())
1082 ;
1083 return tid;
1084}
1086{
1087 // m_k and m_lambda are initialized after constructor by attributes
1088 NS_LOG_FUNCTION (this);
1089}
1090
1093{
1094 NS_LOG_FUNCTION (this);
1095 return m_k;
1096}
1097double
1099{
1100 NS_LOG_FUNCTION (this);
1101 return m_lambda;
1102}
1103
1104/*
1105 The code for the following generator functions was adapted from ns-2
1106 tools/ranvar.cc
1107
1108 The Erlang distribution density function has the form
1109
1110 x^(k-1) * exp(-x/lambda)
1111 p(x; k, lambda) = ---------------------------
1112 lambda^k * (k-1)!
1113
1114 for x > 0.
1115*/
1116double
1118{
1119 NS_LOG_FUNCTION (this << k << lambda);
1120 double mean = lambda;
1121 double bound = 0.0;
1122
1123 double result = 0;
1124 for (unsigned int i = 0; i < k; ++i)
1125 {
1126 result += GetExponentialValue (mean, bound);
1127
1128 }
1129
1130 return result;
1131}
1132
1135{
1136 NS_LOG_FUNCTION (this << k << lambda);
1137 return static_cast<uint32_t> ( GetValue (k, lambda));
1138}
1139
1140double
1142{
1143 NS_LOG_FUNCTION (this);
1144 return GetValue (m_k, m_lambda);
1145}
1148{
1149 NS_LOG_FUNCTION (this);
1150 return (uint32_t)GetValue (m_k, m_lambda);
1151}
1152
1153double
1155{
1156 NS_LOG_FUNCTION (this << mean << bound);
1157 while (1)
1158 {
1159 // Get a uniform random variable in [0,1].
1160 double v = Peek ()->RandU01 ();
1161 if (IsAntithetic ())
1162 {
1163 v = (1 - v);
1164 }
1165
1166 // Calculate the exponential random variable.
1167 double r = -mean*std::log (v);
1168
1169 // Use this value if it's acceptable.
1170 if (bound == 0 || r <= bound)
1171 {
1172 return r;
1173 }
1174 }
1175}
1176
1178
1179TypeId
1181{
1182 static TypeId tid = TypeId ("ns3::TriangularRandomVariable")
1184 .SetGroupName ("Core")
1185 .AddConstructor<TriangularRandomVariable> ()
1186 .AddAttribute ("Mean", "The mean value for the triangular distribution returned by this RNG stream.",
1187 DoubleValue (0.5),
1189 MakeDoubleChecker<double>())
1190 .AddAttribute ("Min", "The lower bound on the values returned by this RNG stream.",
1191 DoubleValue (0.0),
1193 MakeDoubleChecker<double>())
1194 .AddAttribute ("Max", "The upper bound on the values returned by this RNG stream.",
1195 DoubleValue (1.0),
1197 MakeDoubleChecker<double>())
1198 ;
1199 return tid;
1200}
1202{
1203 // m_mean, m_min, and m_max are initialized after constructor by
1204 // attributes
1205 NS_LOG_FUNCTION (this);
1206}
1207
1208double
1210{
1211 NS_LOG_FUNCTION (this);
1212 return m_mean;
1213}
1214double
1216{
1217 NS_LOG_FUNCTION (this);
1218 return m_min;
1219}
1220double
1222{
1223 NS_LOG_FUNCTION (this);
1224 return m_max;
1225}
1226
1227double
1228TriangularRandomVariable::GetValue (double mean, double min, double max)
1229{
1230 // Calculate the mode.
1231 NS_LOG_FUNCTION (this << mean << min << max);
1232 double mode = 3.0 * mean - min - max;
1233
1234 // Get a uniform random variable in [0,1].
1235 double u = Peek ()->RandU01 ();
1236 if (IsAntithetic ())
1237 {
1238 u = (1 - u);
1239 }
1240
1241 // Calculate the triangular random variable.
1242 if (u <= (mode - min) / (max - min) )
1243 {
1244 return min + std::sqrt (u * (max - min) * (mode - min) );
1245 }
1246 else
1247 {
1248 return max - std::sqrt ( (1 - u) * (max - min) * (max - mode) );
1249 }
1250}
1251
1254{
1255 NS_LOG_FUNCTION (this << mean << min << max);
1256 return static_cast<uint32_t> ( GetValue (mean, min, max) );
1257}
1258
1259double
1261{
1262 NS_LOG_FUNCTION (this);
1263 return GetValue (m_mean, m_min, m_max);
1264}
1267{
1268 NS_LOG_FUNCTION (this);
1269 return (uint32_t)GetValue (m_mean, m_min, m_max);
1270}
1271
1273
1274TypeId
1276{
1277 static TypeId tid = TypeId ("ns3::ZipfRandomVariable")
1279 .SetGroupName ("Core")
1280 .AddConstructor<ZipfRandomVariable> ()
1281 .AddAttribute ("N", "The n value for the Zipf distribution returned by this RNG stream.",
1282 IntegerValue (1),
1284 MakeIntegerChecker<uint32_t>())
1285 .AddAttribute ("Alpha", "The alpha value for the Zipf distribution returned by this RNG stream.",
1286 DoubleValue (0.0),
1288 MakeDoubleChecker<double>())
1289 ;
1290 return tid;
1291}
1293{
1294 // m_n and m_alpha are initialized after constructor by attributes
1295 NS_LOG_FUNCTION (this);
1296}
1297
1300{
1301 NS_LOG_FUNCTION (this);
1302 return m_n;
1303}
1304double
1306{
1307 NS_LOG_FUNCTION (this);
1308 return m_alpha;
1309}
1310
1311double
1313{
1314 NS_LOG_FUNCTION (this << n << alpha);
1315 // Calculate the normalization constant c.
1316 m_c = 0.0;
1317 for (uint32_t i = 1; i <= n; i++)
1318 {
1319 m_c += (1.0 / std::pow ((double)i,alpha));
1320 }
1321 m_c = 1.0 / m_c;
1322
1323 // Get a uniform random variable in [0,1].
1324 double u = Peek ()->RandU01 ();
1325 if (IsAntithetic ())
1326 {
1327 u = (1 - u);
1328 }
1329
1330 double sum_prob = 0,zipf_value = 0;
1331 for (uint32_t i = 1; i <= m_n; i++)
1332 {
1333 sum_prob += m_c / std::pow ((double)i,m_alpha);
1334 if (sum_prob > u)
1335 {
1336 zipf_value = i;
1337 break;
1338 }
1339 }
1340 return zipf_value;
1341}
1342
1345{
1346 NS_LOG_FUNCTION (this << n << alpha);
1347 return static_cast<uint32_t> ( GetValue (n, alpha));
1348}
1349
1350double
1352{
1353 NS_LOG_FUNCTION (this);
1354 return GetValue (m_n, m_alpha);
1355}
1358{
1359 NS_LOG_FUNCTION (this);
1360 return (uint32_t)GetValue (m_n, m_alpha);
1361}
1362
1364
1365TypeId
1367{
1368 static TypeId tid = TypeId ("ns3::ZetaRandomVariable")
1370 .SetGroupName ("Core")
1371 .AddConstructor<ZetaRandomVariable> ()
1372 .AddAttribute ("Alpha", "The alpha value for the zeta distribution returned by this RNG stream.",
1373 DoubleValue (3.14),
1375 MakeDoubleChecker<double>())
1376 ;
1377 return tid;
1378}
1380{
1381 // m_alpha is initialized after constructor by attributes
1382 NS_LOG_FUNCTION (this);
1383}
1384
1385double
1387{
1388 NS_LOG_FUNCTION (this);
1389 return m_alpha;
1390}
1391
1392double
1394{
1395 NS_LOG_FUNCTION (this << alpha);
1396 m_b = std::pow (2.0, alpha - 1.0);
1397
1398 double u, v;
1399 double X, T;
1400 double test;
1401
1402 do
1403 {
1404 // Get a uniform random variable in [0,1].
1405 u = Peek ()->RandU01 ();
1406 if (IsAntithetic ())
1407 {
1408 u = (1 - u);
1409 }
1410
1411 // Get a uniform random variable in [0,1].
1412 v = Peek ()->RandU01 ();
1413 if (IsAntithetic ())
1414 {
1415 v = (1 - v);
1416 }
1417
1418 X = std::floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
1419 T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
1420 test = v * X * (T - 1.0) / (m_b - 1.0);
1421 }
1422 while ( test > (T / m_b) );
1423
1424 return X;
1425}
1426
1429{
1430 NS_LOG_FUNCTION (this << alpha);
1431 return static_cast<uint32_t> ( GetValue (alpha));
1432}
1433
1434double
1436{
1437 NS_LOG_FUNCTION (this);
1438 return GetValue (m_alpha);
1439}
1442{
1443 NS_LOG_FUNCTION (this);
1444 return (uint32_t)GetValue (m_alpha);
1445}
1446
1448
1449TypeId
1451{
1452 static TypeId tid = TypeId ("ns3::DeterministicRandomVariable")
1454 .SetGroupName ("Core")
1455 .AddConstructor<DeterministicRandomVariable> ()
1456 ;
1457 return tid;
1458}
1460 :
1461 m_count (0),
1462 m_next (0),
1463 m_data (0)
1464{
1465 NS_LOG_FUNCTION (this);
1466}
1468{
1469 // Delete any values currently set.
1470 NS_LOG_FUNCTION (this);
1471 if (m_data != 0)
1472 {
1473 delete[] m_data;
1474 }
1475}
1476
1477void
1478DeterministicRandomVariable::SetValueArray (double* values, std::size_t length)
1479{
1480 NS_LOG_FUNCTION (this << values << length);
1481 // Delete any values currently set.
1482 if (m_data != 0)
1483 {
1484 delete[] m_data;
1485 }
1486
1487 // Make room for the values being set.
1488 m_data = new double[length];
1489 m_count = length;
1490 m_next = length;
1491
1492 // Copy the values.
1493 for (std::size_t i = 0; i < m_count; i++)
1494 {
1495 m_data[i] = values[i];
1496 }
1497}
1498
1499double
1501{
1502 NS_LOG_FUNCTION (this);
1503 // Make sure the array has been set.
1504 NS_ASSERT (m_count > 0);
1505
1506 if (m_next == m_count)
1507 {
1508 m_next = 0;
1509 }
1510 return m_data[m_next++];
1511}
1512
1515{
1516 NS_LOG_FUNCTION (this);
1517 return (uint32_t)GetValue ();
1518}
1519
1521
1522// ValueCDF methods
1524 : value (0.0),
1525 cdf (0.0)
1526{
1527 NS_LOG_FUNCTION (this);
1528}
1529
1531 : value (v),
1532 cdf (c)
1533{
1534 NS_LOG_FUNCTION (this << v << c);
1535 NS_ASSERT (c >= 0.0 && c <= 1.0);
1536}
1537
1538bool
1541{
1542 return a.cdf < b.cdf;
1543}
1544
1545TypeId
1547{
1548 static TypeId tid = TypeId ("ns3::EmpiricalRandomVariable")
1550 .SetGroupName ("Core")
1551 .AddConstructor<EmpiricalRandomVariable> ()
1552 .AddAttribute ("Interpolate",
1553 "Treat the CDF as a smooth distribution and interpolate, "
1554 "default is to treat the CDF as a histogram and sample.",
1555 BooleanValue (false),
1558 ;
1559 return tid;
1560}
1562 : m_validated (false)
1563{
1564 NS_LOG_FUNCTION (this);
1565}
1566
1567bool
1569{
1570 NS_LOG_FUNCTION (this << interpolate);
1571 bool prev = m_interpolate;
1572 m_interpolate = interpolate;
1573 return prev;
1574}
1575
1578{
1579 NS_LOG_FUNCTION (this);
1580 return static_cast<uint32_t> (GetValue ());
1581}
1582
1583bool
1585{
1586 NS_LOG_FUNCTION (this);
1587
1588 if (!m_validated)
1589 {
1590 Validate ();
1591 }
1592
1593 // Get a uniform random variable in [0, 1].
1594 double r = Peek ()->RandU01 ();
1595 if (IsAntithetic ())
1596 {
1597 r = (1 - r);
1598 }
1599
1600 value = r;
1601 bool valid = false;
1602 // check extrema
1603 if (r <= m_emp.front ().cdf)
1604 {
1605 value = m_emp.front ().value; // Less than first
1606 valid = true;
1607 }
1608 else if (r >= m_emp.back ().cdf)
1609 {
1610 value = m_emp.back ().value; // Greater than last
1611 valid = true;
1612 }
1613 return valid;
1614}
1615
1616double
1618{
1619 NS_LOG_FUNCTION (this);
1620
1621 double value;
1622 if (PreSample (value))
1623 {
1624 return value;
1625 }
1626
1627 // value now has the (unused) URNG selector
1628 if (m_interpolate)
1629 {
1630 value = DoInterpolate (value);
1631 }
1632 else
1633 {
1634 value = DoSampleCDF (value);
1635 }
1636 return value;
1637}
1638
1639double
1641{
1642 NS_LOG_FUNCTION (this << r);
1643
1644 ValueCDF selector (0, r);
1645 auto bound = std::upper_bound (m_emp.begin (), m_emp.end (), selector);
1646
1647 return bound->value;
1648}
1649
1650double
1652{
1653 NS_LOG_FUNCTION (this);
1654
1655 double value;
1656 if (PreSample (value))
1657 {
1658 return value;
1659 }
1660
1661 // value now has the (unused) URNG selector
1662 value = DoInterpolate (value);
1663 return value;
1664}
1665
1666double
1668{
1669 NS_LOG_FUNCTION (this << r);
1670
1671 // Return a value from the empirical distribution
1672 // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1673
1674 // search
1675 ValueCDF selector (0, r);
1676 auto upper = std::upper_bound (m_emp.begin (), m_emp.end (), selector);
1677 auto lower = std::prev (upper, 1);
1678 if (upper == m_emp.begin ())
1679 {
1680 lower = upper;
1681 }
1682
1683 // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1684 double c1 = lower->cdf;
1685 double c2 = upper->cdf;
1686 double v1 = lower->value;
1687 double v2 = upper->value;
1688
1689 double value = (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1690 return value;
1691}
1692
1693void
1695{
1696 // Add a new empirical datapoint to the empirical cdf
1697 // NOTE. These MUST be inserted in non-decreasing order
1698 NS_LOG_FUNCTION (this << v << c);
1699 m_emp.push_back (ValueCDF (v, c));
1700}
1701
1702void
1704{
1705 NS_LOG_FUNCTION (this);
1706 if (m_emp.empty ())
1707 {
1708 NS_FATAL_ERROR ("CDF is not initialized");
1709 }
1710 ValueCDF prior = m_emp[0];
1711 for (auto current : m_emp)
1712 {
1713 if (current.value < prior.value || current.cdf < prior.cdf)
1714 { // Error
1715 std::cerr << "Empirical Dist error,"
1716 << " current value " << current.value
1717 << " prior value " << prior.value
1718 << " current cdf " << current.cdf
1719 << " prior cdf " << prior.cdf << std::endl;
1720 NS_FATAL_ERROR ("Empirical Dist error");
1721 }
1722 prior = current;
1723 }
1724 if (prior.cdf != 1.0)
1725 {
1726 NS_FATAL_ERROR ("CDF does not cover the whole distribution");
1727 }
1728 m_validated = true;
1729}
1730
1731} // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::BooleanValue attribute value declarations.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
The Random Number Generator (RNG) that returns a constant.
ConstantRandomVariable()
Creates a constant RNG with the default constant value.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
double m_constant
The constant value returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
double GetConstant(void) const
Get the constant value returned by this RNG stream.
The Random Number Generator (RNG) that returns a predetermined sequence.
std::size_t m_next
Position of the next value in the array of values.
static TypeId GetTypeId(void)
Register this type.
virtual double GetValue(void)
Returns the next value in the sequence.
double * m_data
Array of values to return in sequence.
DeterministicRandomVariable()
Creates a deterministic RNG that will have a predetermined sequence of values.
void SetValueArray(double *values, std::size_t length)
Sets the array of values that holds the predetermined sequence.
virtual uint32_t GetInteger(void)
Returns the next value in the sequence.
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:41
Helper to hold one point of the CDF.
The Random Number Generator (RNG) that has a specified empirical distribution.
bool SetInterpolate(bool interpolate)
Switch the mode between sampling the CDF and interpolating.
virtual double Interpolate(void)
Returns the next value in the empirical distribution using linear interpolation.
virtual double GetValue(void)
Returns the next value in the empirical distribution.
void CDF(double v, double c)
Specifies a point in the empirical distribution.
EmpiricalRandomVariable(void)
Creates an empirical RNG that has a specified, empirical distribution, and configured for interpolati...
virtual uint32_t GetInteger(void)
Returns the next value in the empirical distribution.
void Validate(void)
Check that the CDF is valid.
bool PreSample(double &value)
Do the initial rng draw and check against the extrema.
static TypeId GetTypeId(void)
Register this type.
double DoSampleCDF(double r)
Sample the CDF as a histogram (without interpolation).
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.
std::vector< ValueCDF > m_emp
The vector of CDF points.
friend bool operator<(ValueCDF a, ValueCDF b)
Comparison operator, for use by std::upper_bound.
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.
static TypeId GetTypeId(void)
Register this type.
double GetExponentialValue(double mean, double bound)
Returns a random double from an exponential distribution with the specified mean and upper bound.
double GetLambda(void) const
Returns the lambda value for the Erlang distribution returned by this RNG stream.
virtual double GetValue(void)
Returns a random double from an Erlang distribution with the current k and lambda.
uint32_t GetK(void) const
Returns the k value for the Erlang distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from an Erlang distribution with the current k and lambda.
uint32_t m_k
The k value for the Erlang distribution returned by this RNG stream.
ErlangRandomVariable()
Creates an Erlang distribution RNG with the default values for k and lambda.
The exponential distribution Random Number Generator (RNG).
double GetBound(void) const
Get the configured upper bound of this RNG.
ExponentialRandomVariable()
Creates an exponential distribution RNG with the default values for the mean and upper bound.
double GetMean(void) const
Get the configured mean value of this RNG.
double m_mean
The mean value of the unbounded exponential distribution.
double m_bound
The upper bound on values that can be returned by this RNG stream.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
static TypeId GetTypeId(void)
Register this type.
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
The gamma distribution Random Number Generator (RNG) that allows stream numbers to be set determinist...
double GetBeta(void) const
Returns the beta value for the gamma distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a gamma distribution with the current alpha and beta.
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.
virtual double GetValue(void)
Returns a random double from a gamma distribution with the current alpha and beta.
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.
static TypeId GetTypeId(void)
Register this type.
double m_v2
The algorithm produces two values at a time.
bool m_nextValid
True if the next normal value is valid.
double GetAlpha(void) const
Returns the alpha 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:44
The log-normal distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
virtual double GetValue(void)
Returns a random double from a log-normal distribution with the current mu and sigma.
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 GetSigma(void) const
Returns the sigma value for the log-normal distribution returned by this RNG stream.
double GetMu(void) const
Returns the mu value for the log-normal distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a log-normal distribution with the current mu and sigma.
static TypeId GetTypeId(void)
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 GetVariance(void) const
Returns the variance value for the normal distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a normal distribution with the current mean,...
static TypeId GetTypeId(void)
Register this type.
double m_mean
The mean value for the normal distribution returned by this RNG stream.
double GetBound(void) const
Returns the bound on values that can be returned by this RNG stream.
static const double INFINITE_VALUE
Large constant to bound the range.
double GetMean(void) const
Returns the mean value for the normal distribution returned by this RNG stream.
double m_variance
The variance value for the normal distribution returned by this RNG stream.
virtual double GetValue(void)
Returns a random double from a normal distribution with the current mean, variance,...
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.
A base class which provides memory management and object aggregation.
Definition: object.h:88
The Pareto distribution Random Number Generator (RNG).
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,...
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a Pareto distribution with the current mean,...
double GetShape(void) const
Returns the shape parameter for the Pareto distribution returned by this RNG stream.
double m_shape
The shape parameter for the Pareto distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
double m_bound
The upper bound on values that can be returned by this RNG stream.
double GetBound(void) const
Returns the upper bound on values that can be returned by this RNG stream.
double GetScale(void) const
Returns the scale parameter for the Pareto distribution returned by this RNG stream.
virtual double GetValue(void)
Returns a random double from a Pareto distribution with the current mean, shape, and upper bound.
The basic uniform Random Number Generator (RNG).
static TypeId GetTypeId(void)
Register this type.
virtual ~RandomVariableStream()
Destructor.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
RngStream * Peek(void) const
Get the pointer to the underlying RngStream.
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.
RngStream * m_rng
Pointer to the underlying RngStream.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
int64_t GetStream(void) const
Returns the stream number for the RngStream.
bool IsAntithetic(void) const
Check if antithetic values will be generated.
static uint32_t GetSeed(void)
Get the current seed value which will be used by all subsequently instantiated RandomVariableStream o...
static uint64_t GetNextStreamIndex(void)
Get the next automatically assigned stream index.
static uint64_t GetRun(void)
Get the current run number.
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:50
double RandU01(void)
Generate the next random number for this stream.
Definition: rng-stream.cc:335
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.
static TypeId GetTypeId(void)
Register this type.
double m_min
The first value of the sequence.
uint32_t m_consecutive
The number of times each distinct value is repeated.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
Ptr< RandomVariableStream > GetIncrement(void) const
Get the increment for the sequence.
double GetMax(void) const
Get the limit of the sequence, which is (at least) one more than the last value of the sequence.
double m_current
The current sequence value.
double GetMin(void) const
Get the first value of the sequence.
double m_max
Strict upper bound on the sequence.
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
Ptr< RandomVariableStream > m_increment
Increment between distinct values.
uint32_t GetConsecutive(void) const
Get the number of times each distinct value of the sequence is repeated before incrementing to the ne...
SequentialRandomVariable()
Creates a sequential RNG with the default values for the sequence parameters.
bool m_isCurrentSet
Indicates if the current sequence value has been properly initialized.
Hold variables of type string.
Definition: string.h:41
The triangular distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
double GetMin(void) const
Returns the lower bound for the triangular distribution returned by this RNG stream.
virtual double GetValue(void)
Returns a random double from a triangular distribution with the current mean, min,...
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a triangular distribution with the current mean,...
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.
static TypeId GetTypeId(void)
Register this type.
TriangularRandomVariable()
Creates a triangular distribution RNG with the default values for the mean, lower bound,...
double m_min
The lower bound on values that can be returned by this RNG stream.
double GetMax(void) const
Returns the upper bound on values that can be returned by this RNG stream.
double GetMean(void) const
Returns the mean value for the triangular distribution returned by this RNG stream.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
The uniform distribution Random Number Generator (RNG).
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
double GetMax(void) const
Get the upper bound on values returned by GetValue(void).
UniformRandomVariable()
Creates a uniform distribution RNG with the default range.
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.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
static TypeId GetTypeId(void)
Register this type.
double GetMin(void) const
Get the lower bound on randoms returned by GetValue(void).
The Weibull distribution Random Number Generator (RNG) that allows stream numbers to be set determini...
virtual double GetValue(void)
Returns a random double from a Weibull distribution with the current scale, shape,...
double m_shape
The shape parameter for the Weibull distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a Weibull distribution with the current scale,...
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(void) const
Returns the upper bound on values that can be returned by this RNG stream.
WeibullRandomVariable()
Creates a Weibull distribution RNG with the default values for the scale, shape, and upper bound.
double GetScale(void) const
Returns the scale parameter for the Weibull distribution returned by this RNG stream.
double GetShape(void) const
Returns the shape parameter for the Weibull distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
The zeta distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
double GetAlpha(void) const
Returns the alpha value for the zeta distribution returned by this RNG stream.
double m_alpha
The alpha value for the zeta distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a zeta distribution with the current alpha.
ZetaRandomVariable()
Creates a zeta distribution RNG with the default value for alpha.
virtual double GetValue(void)
Returns a random double from a zeta distribution with the current alpha.
double m_b
Just for calculus simplifications.
The Zipf distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
double GetAlpha(void) const
Returns the alpha value for the Zipf distribution returned by this RNG stream.
virtual double GetValue(void)
Returns a random double from a Zipf distribution with the current n and alpha.
double m_c
The normalization constant.
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.
static TypeId GetTypeId(void)
Register this type.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a Zipf distribution with the current n and alpha.
uint32_t GetN(void) const
Returns the n value for the Zipf distribution returned by this RNG stream.
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:67
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:85
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:42
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition: integer.h:45
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::IntegerValue attribute value declarations and template implementations.
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ normal
Definition: ff-mac-common.h:84
float alpha
Plot alpha value (transparency)
list x
Random number samples.
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