A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
random-variable.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 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: Rajib Bhattacharjea<raj.b@gatech.edu>
19 // Author: Hadi Arbabi<marbabi@cs.odu.edu>
20 //
21 
22 #include <iostream>
23 #include <cmath>
24 #include <cstdlib>
25 #include <sys/time.h> // for gettimeofday
26 #include <unistd.h>
27 #include <iostream>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <sstream>
32 #include <vector>
33 
34 #include "assert.h"
35 #include "random-variable.h"
36 #include "rng-seed-manager.h"
37 #include "rng-stream.h"
38 #include "fatal-error.h"
39 #include "log.h"
40 
41 namespace ns3 {
42 
43 // -----------------------------------------------------------------------------
44 // -----------------------------------------------------------------------------
45 // RandomVariableBase methods
46 
47 NS_LOG_COMPONENT_DEFINE ("RandomVariable")
48  ;
49 
52 {
53 public:
56  virtual ~RandomVariableBase ();
57  virtual double GetValue () = 0;
58  virtual uint32_t GetInteger ();
59  virtual RandomVariableBase* Copy (void) const = 0;
60  RngStream *GetStream(void);
61 private:
62  RngStream* m_generator; // underlying generator being wrapped
63 };
64 
66  : m_generator (0)
67 {
68  NS_LOG_FUNCTION (this);
69 }
70 
72  : m_generator (0)
73 {
74  NS_LOG_FUNCTION (this << &r);
75  if (r.m_generator != 0)
76  {
80  }
81 }
82 
84 {
85  NS_LOG_FUNCTION (this);
86  delete m_generator;
87 }
88 
90 {
91  NS_LOG_FUNCTION (this);
92  return (uint32_t)GetValue ();
93 }
94 
95 RngStream *
97 {
98  NS_LOG_FUNCTION (this);
99  if (m_generator == 0)
100  {
104  }
105  return m_generator;
106 }
107 
108 // -------------------------------------------------------
109 
111  : m_variable (0)
112 {
113  NS_LOG_FUNCTION (this);
114 }
116  : m_variable (o.m_variable->Copy ())
117 {
118 }
120  : m_variable (variable.Copy ())
121 {
122 }
125 {
126  if (&o == this)
127  {
128  return *this;
129  }
130  delete m_variable;
131  m_variable = o.m_variable->Copy ();
132  return *this;
133 }
135 {
136  NS_LOG_FUNCTION (this);
137  delete m_variable;
138 }
139 double
141 {
142  NS_LOG_FUNCTION (this);
143  return m_variable->GetValue ();
144 }
145 
146 uint32_t
148 {
149  NS_LOG_FUNCTION (this);
150  return m_variable->GetInteger ();
151 }
152 
155 {
156  NS_LOG_FUNCTION (this);
157  return m_variable;
158 }
159 
160 
163 
164 // -----------------------------------------------------------------------------
165 // -----------------------------------------------------------------------------
166 // UniformVariableImpl
167 
170 {
171 public:
177 
183  UniformVariableImpl (double s, double l);
184 
186 
187  double GetMin (void) const;
188  double GetMax (void) const;
189 
193  virtual double GetValue ();
194 
198  virtual double GetValue (double s, double l);
199 
200  virtual RandomVariableBase* Copy (void) const;
201 
202 private:
203  double m_min;
204  double m_max;
205 };
206 
208  : m_min (0),
209  m_max (1.0)
210 {
211  NS_LOG_FUNCTION (this);
212 }
213 
215  : m_min (s),
216  m_max (l)
217 {
218  NS_LOG_FUNCTION (this << s << l);
219 }
220 
222  : RandomVariableBase (c),
223  m_min (c.m_min),
224  m_max (c.m_max)
225 {
226  NS_LOG_FUNCTION (this << &c);
227 }
228 
229 double
231 {
232  NS_LOG_FUNCTION (this);
233  return m_min;
234 }
235 double
237 {
238  NS_LOG_FUNCTION (this);
239  return m_max;
240 }
241 
242 
244 {
245  NS_LOG_FUNCTION (this);
246  RngStream *generator = GetStream ();
247  return m_min + generator->RandU01 () * (m_max - m_min);
248 }
249 
250 double UniformVariableImpl::GetValue (double s, double l)
251 {
252  NS_LOG_FUNCTION (this << s << l);
253  RngStream *generator = GetStream ();
254  return s + generator->RandU01 () * (l - s);
255 }
256 
258 {
259  NS_LOG_FUNCTION (this);
260  return new UniformVariableImpl (*this);
261 }
262 
265 {
266  NS_LOG_FUNCTION (this);
267 }
270 {
271  NS_LOG_FUNCTION (this << s << l);
272 }
273 
274 double UniformVariable::GetValue (void) const
275 {
276  NS_LOG_FUNCTION (this);
277  return this->RandomVariable::GetValue ();
278 }
279 
280 double UniformVariable::GetValue (double s, double l)
281 {
282  NS_LOG_FUNCTION (this << s << l);
283  return ((UniformVariableImpl*)Peek ())->GetValue (s,l);
284 }
285 
286 uint32_t UniformVariable::GetInteger (uint32_t s, uint32_t l)
287 {
288  NS_LOG_FUNCTION (this << s << l);
289  NS_ASSERT (s <= l);
290  return static_cast<uint32_t> ( GetValue (s, l + 1) );
291 }
292 
293 // -----------------------------------------------------------------------------
294 // -----------------------------------------------------------------------------
295 // ConstantVariableImpl methods
296 
299 {
300 
301 public:
306 
312  ConstantVariableImpl (double c);
313 
314 
316 
321  void NewConstant (double c);
322 
326  virtual double GetValue ();
327  virtual uint32_t GetInteger ();
328  virtual RandomVariableBase* Copy (void) const;
329 private:
330  double m_const;
331 };
332 
334  : m_const (0)
335 {
336  NS_LOG_FUNCTION (this);
337 }
338 
340  : m_const (c)
341 {
342  NS_LOG_FUNCTION (this << c);
343 }
344 
346  : RandomVariableBase (c),
347  m_const (c.m_const)
348 {
349  NS_LOG_FUNCTION (this << &c);
350 }
351 
353 {
354  NS_LOG_FUNCTION (this << c);
355  m_const = c;
356 }
357 
359 {
360  NS_LOG_FUNCTION (this);
361  return m_const;
362 }
363 
365 {
366  NS_LOG_FUNCTION (this);
367  return (uint32_t)m_const;
368 }
369 
371 {
372  NS_LOG_FUNCTION (this);
373  return new ConstantVariableImpl (*this);
374 }
375 
378 {
379  NS_LOG_FUNCTION (this);
380 }
383 {
384  NS_LOG_FUNCTION (this << c);
385 }
386 void
388 {
389  NS_LOG_FUNCTION (this << c);
390  *this = ConstantVariable (c);
391 }
392 
393 // -----------------------------------------------------------------------------
394 // -----------------------------------------------------------------------------
395 // SequentialVariableImpl methods
396 
397 
400 {
401 
402 public:
414  SequentialVariableImpl (double f, double l, double i = 1, uint32_t c = 1);
415 
426  SequentialVariableImpl (double f, double l, const RandomVariable& i, uint32_t c = 1);
427 
429 
434  virtual double GetValue ();
435  virtual RandomVariableBase* Copy (void) const;
436 private:
437  double m_min;
438  double m_max;
440  uint32_t m_consecutive;
441  double m_current;
443 };
444 
445 SequentialVariableImpl::SequentialVariableImpl (double f, double l, double i, uint32_t c)
446  : m_min (f),
447  m_max (l),
448  m_increment (ConstantVariable (i)),
449  m_consecutive (c),
450  m_current (f),
451  m_currentConsecutive (0)
452 {
453  NS_LOG_FUNCTION (this << f << l << i << c);
454 }
455 
456 SequentialVariableImpl::SequentialVariableImpl (double f, double l, const RandomVariable& i, uint32_t c)
457  : m_min (f),
458  m_max (l),
459  m_increment (i),
460  m_consecutive (c),
461  m_current (f),
462  m_currentConsecutive (0)
463 {
464  NS_LOG_FUNCTION (this << f << l << i << c);
465 }
466 
468  : RandomVariableBase (c),
469  m_min (c.m_min),
470  m_max (c.m_max),
471  m_increment (c.m_increment),
472  m_consecutive (c.m_consecutive),
473  m_current (c.m_current),
474  m_currentConsecutive (c.m_currentConsecutive)
475 {
476  NS_LOG_FUNCTION (this << &c);
477 }
478 
480 {
481  NS_LOG_FUNCTION (this);
482 }
483 
485 { // Return a sequential series of values
486  NS_LOG_FUNCTION (this);
487  double r = m_current;
489  { // Time to advance to next
492  if (m_current >= m_max)
493  {
494  m_current = m_min + (m_current - m_max);
495  }
496  }
497  return r;
498 }
499 
501 {
502  NS_LOG_FUNCTION (this);
503  return new SequentialVariableImpl (*this);
504 }
505 
506 SequentialVariable::SequentialVariable (double f, double l, double i, uint32_t c)
507  : RandomVariable (SequentialVariableImpl (f, l, i, c))
508 {
509  NS_LOG_FUNCTION (this << f << l << i << c);
510 }
511 SequentialVariable::SequentialVariable (double f, double l, const RandomVariable& i, uint32_t c)
512  : RandomVariable (SequentialVariableImpl (f, l, i, c))
513 {
514  NS_LOG_FUNCTION (this << f << l << i << c);
515 }
516 
517 // -----------------------------------------------------------------------------
518 // -----------------------------------------------------------------------------
519 // ExponentialVariableImpl methods
520 
523 {
524 public:
530 
536  explicit ExponentialVariableImpl (double m);
537 
549  ExponentialVariableImpl (double m, double b);
550 
552 
556  virtual double GetValue ();
557  virtual RandomVariableBase* Copy (void) const;
558 
559 private:
560  double m_mean; // Mean value of RV
561  double m_bound; // Upper bound on value (if non-zero)
562 };
563 
565  : m_mean (1.0),
566  m_bound (0)
567 {
568  NS_LOG_FUNCTION (this);
569 }
570 
572  : m_mean (m),
573  m_bound (0)
574 {
575  NS_LOG_FUNCTION (this << m);
576 }
577 
579  : m_mean (m),
580  m_bound (b)
581 {
582  NS_LOG_FUNCTION (this << m << b);
583 }
584 
586  : RandomVariableBase (c),
587  m_mean (c.m_mean),
588  m_bound (c.m_bound)
589 {
590  NS_LOG_FUNCTION (this << &c);
591 }
592 
594 {
595  NS_LOG_FUNCTION (this);
596  RngStream *generator = GetStream ();
597  while (1)
598  {
599  double r = -m_mean*std::log (generator->RandU01 ());
600  if (m_bound == 0 || r <= m_bound)
601  {
602  return r;
603  }
604  // otherwise, try again
605  }
606 }
607 
609 {
610  NS_LOG_FUNCTION (this);
611  return new ExponentialVariableImpl (*this);
612 }
613 
616 {
617  NS_LOG_FUNCTION (this);
618 }
621 {
622  NS_LOG_FUNCTION (this << m);
623 }
626 {
627  NS_LOG_FUNCTION (this << m << b);
628 }
629 
630 // -----------------------------------------------------------------------------
631 // -----------------------------------------------------------------------------
632 // ParetoVariableImpl methods
635 {
636 public:
642 
649  explicit ParetoVariableImpl (double m);
650 
658  ParetoVariableImpl (double m, double s);
659 
672  ParetoVariableImpl (double m, double s, double b);
673 
680  ParetoVariableImpl (std::pair<double, double> params);
681 
694  ParetoVariableImpl (std::pair<double, double> params, double b);
695 
697 
701  virtual double GetValue ();
702  virtual RandomVariableBase* Copy () const;
703 
704 private:
705  double m_scale; // Scale value of RV
706  double m_shape; // Shape parameter
707  double m_bound; // Upper bound on value (if non-zero)
708 };
709 
711  : m_scale (0.5 / 1.5),
712  m_shape (1.5),
713  m_bound (0)
714 {
715  NS_LOG_FUNCTION (this);
716 }
717 
719  : m_scale (m * 0.5 / 1.5),
720  m_shape (1.5),
721  m_bound (0)
722 {
723  NS_LOG_FUNCTION (this << m);
724 }
725 
727  : m_scale (m * (s - 1.0) / s),
728  m_shape (s),
729  m_bound (0)
730 {
731  NS_LOG_FUNCTION (this << m << s);
732 }
733 
734 ParetoVariableImpl::ParetoVariableImpl (double m, double s, double b)
735  : m_scale (m * (s - 1.0) / s),
736  m_shape (s),
737  m_bound (b)
738 {
739  NS_LOG_FUNCTION (this << m << s << b);
740 }
741 
742 ParetoVariableImpl::ParetoVariableImpl (std::pair<double, double> params)
743  : m_scale (params.first),
744  m_shape (params.second),
745  m_bound (0)
746 {
747  NS_LOG_FUNCTION (this << &params);
748 }
749 
750 ParetoVariableImpl::ParetoVariableImpl (std::pair<double, double> params, double b)
751  : m_scale (params.first),
752  m_shape (params.second),
753  m_bound (b)
754 {
755  NS_LOG_FUNCTION (this << &params << b);
756 }
757 
759  : RandomVariableBase (c),
760  m_scale (c.m_scale),
761  m_shape (c.m_shape),
762  m_bound (c.m_bound)
763 {
764  NS_LOG_FUNCTION (this << &c);
765 }
766 
768 {
769  NS_LOG_FUNCTION (this);
770  RngStream *generator = GetStream ();
771  while (1)
772  {
773  double r = (m_scale * ( 1.0 / std::pow (generator->RandU01 (), 1.0 / m_shape)));
774  if (m_bound == 0 || r <= m_bound)
775  {
776  return r;
777  }
778  // otherwise, try again
779  }
780 }
781 
783 {
784  NS_LOG_FUNCTION (this);
785  return new ParetoVariableImpl (*this);
786 }
787 
790 {
791  NS_LOG_FUNCTION (this);
792 }
795 {
796  NS_LOG_FUNCTION (this << m);
797 }
800 {
801  NS_LOG_FUNCTION (this << m << s);
802 }
803 ParetoVariable::ParetoVariable (double m, double s, double b)
804  : RandomVariable (ParetoVariableImpl (m, s, b))
805 {
806  NS_LOG_FUNCTION (this << m << s << b);
807 }
808 ParetoVariable::ParetoVariable (std::pair<double, double> params)
810 {
811  NS_LOG_FUNCTION (this << &params);
812 }
813 ParetoVariable::ParetoVariable (std::pair<double, double> params, double b)
814  : RandomVariable (ParetoVariableImpl (params, b))
815 {
816  NS_LOG_FUNCTION (this << &params << b);
817 }
818 
819 // -----------------------------------------------------------------------------
820 // -----------------------------------------------------------------------------
821 // WeibullVariableImpl methods
822 
825 {
826 public:
832 
833 
839  WeibullVariableImpl (double m);
840 
847  WeibullVariableImpl (double m, double s);
848 
860  WeibullVariableImpl (double m, double s, double b);
861 
863 
867  virtual double GetValue ();
868  virtual RandomVariableBase* Copy (void) const;
869 
870 private:
871  double m_mean; // Mean value of RV
872  double m_alpha; // Shape parameter
873  double m_bound; // Upper bound on value (if non-zero)
874 };
875 
877  m_alpha (1),
878  m_bound (0)
879 {
880  NS_LOG_FUNCTION (this);
881 }
883  : m_mean (m),
884  m_alpha (1),
885  m_bound (0)
886 {
887  NS_LOG_FUNCTION (this << m);
888 }
890  : m_mean (m),
891  m_alpha (s),
892  m_bound (0)
893 {
894  NS_LOG_FUNCTION (this << m << s);
895 }
896 WeibullVariableImpl::WeibullVariableImpl (double m, double s, double b)
897  : m_mean (m),
898  m_alpha (s),
899  m_bound (b)
900 {
901  NS_LOG_FUNCTION (this << m << s << b);
902 }
904  : RandomVariableBase (c),
905  m_mean (c.m_mean),
906  m_alpha (c.m_alpha),
907  m_bound (c.m_bound)
908 {
909  NS_LOG_FUNCTION (this << &c);
910 }
911 
913 {
914  NS_LOG_FUNCTION (this);
915  RngStream *generator = GetStream ();
916  double exponent = 1.0 / m_alpha;
917  while (1)
918  {
919  double r = m_mean * std::pow ( -std::log (generator->RandU01 ()), exponent);
920  if (m_bound == 0 || r <= m_bound)
921  {
922  return r;
923  }
924  // otherwise, try again
925  }
926 }
927 
929 {
930  NS_LOG_FUNCTION (this);
931  return new WeibullVariableImpl (*this);
932 }
933 
936 {
937  NS_LOG_FUNCTION (this);
938 }
941 {
942  NS_LOG_FUNCTION (this << m);
943 }
946 {
947  NS_LOG_FUNCTION (this << m << s);
948 }
949 WeibullVariable::WeibullVariable (double m, double s, double b)
950  : RandomVariable (WeibullVariableImpl (m, s, b))
951 {
952  NS_LOG_FUNCTION (this << m << s << b);
953 }
954 
955 // -----------------------------------------------------------------------------
956 // -----------------------------------------------------------------------------
957 // NormalVariableImpl methods
958 
960 class NormalVariableImpl : public RandomVariableBase // Normally Distributed random var
961 
962 {
963 public:
964  static const double INFINITE_VALUE;
970 
977  NormalVariableImpl (double m, double v, double b = INFINITE_VALUE);
978 
980 
984  virtual double GetValue ();
985  virtual RandomVariableBase* Copy (void) const;
986 
987  double GetMean (void) const;
988  double GetVariance (void) const;
989  double GetBound (void) const;
990 
991 private:
992  double m_mean; // Mean value of RV
993  double m_variance; // Mean value of RV
994  double m_bound; // Bound on value's difference from the mean (absolute value)
995  bool m_nextValid; // True if next valid
996  double m_next; // The algorithm produces two values at a time
997 };
998 
999 const double NormalVariableImpl::INFINITE_VALUE = 1e307;
1000 
1002  : m_mean (0.0),
1003  m_variance (1.0),
1004  m_bound (INFINITE_VALUE),
1005  m_nextValid (false)
1006 {
1007  NS_LOG_FUNCTION (this);
1008 }
1009 
1010 NormalVariableImpl::NormalVariableImpl (double m, double v, double b)
1011  : m_mean (m),
1012  m_variance (v),
1013  m_bound (b),
1014  m_nextValid (false)
1015 {
1016  NS_LOG_FUNCTION (this << m << v << b);
1017 }
1018 
1020  : RandomVariableBase (c),
1021  m_mean (c.m_mean),
1022  m_variance (c.m_variance),
1023  m_bound (c.m_bound),
1024  m_nextValid (false)
1025 {
1026  NS_LOG_FUNCTION (this << &c);
1027 }
1028 
1030 {
1031  NS_LOG_FUNCTION (this);
1032  RngStream *generator = GetStream ();
1033  if (m_nextValid)
1034  { // use previously generated
1035  m_nextValid = false;
1036  return m_next;
1037  }
1038  while (1)
1039  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1040  // for algorithm; basically a Box-Muller transform:
1041  // http://en.wikipedia.org/wiki/Box-Muller_transform
1042  double u1 = generator->RandU01 ();
1043  double u2 = generator->RandU01 ();
1044  double v1 = 2 * u1 - 1;
1045  double v2 = 2 * u2 - 1;
1046  double w = v1 * v1 + v2 * v2;
1047  if (w <= 1.0)
1048  { // Got good pair
1049  double y = std::sqrt ((-2 * std::log (w)) / w);
1050  m_next = m_mean + v2 * y * std::sqrt (m_variance);
1051  // if next is in bounds, it is valid
1052  m_nextValid = std::fabs (m_next - m_mean) <= m_bound;
1053  double x1 = m_mean + v1 * y * std::sqrt (m_variance);
1054  // if x1 is in bounds, return it
1055  if (std::fabs (x1 - m_mean) <= m_bound)
1056  {
1057  return x1;
1058  }
1059  // otherwise try and return m_next if it is valid
1060  else if (m_nextValid)
1061  {
1062  m_nextValid = false;
1063  return m_next;
1064  }
1065  // otherwise, just run this loop again
1066  }
1067  }
1068 }
1069 
1071 {
1072  NS_LOG_FUNCTION (this);
1073  return new NormalVariableImpl (*this);
1074 }
1075 
1076 double
1078 {
1079  NS_LOG_FUNCTION (this);
1080  return m_mean;
1081 }
1082 
1083 double
1085 {
1086  NS_LOG_FUNCTION (this);
1087  return m_variance;
1088 }
1089 
1090 double
1092 {
1093  NS_LOG_FUNCTION (this);
1094  return m_bound;
1095 }
1096 
1099 {
1100  NS_LOG_FUNCTION (this);
1101 }
1104 {
1105  NS_LOG_FUNCTION (this << m << v);
1106 }
1107 NormalVariable::NormalVariable (double m, double v, double b)
1108  : RandomVariable (NormalVariableImpl (m, v, b))
1109 {
1110  NS_LOG_FUNCTION (this << m << v << b);
1111 }
1112 
1113 // -----------------------------------------------------------------------------
1114 // -----------------------------------------------------------------------------
1117 {
1118 public:
1122  explicit EmpiricalVariableImpl ();
1123 
1124  virtual ~EmpiricalVariableImpl ();
1129  virtual double GetValue ();
1130  virtual RandomVariableBase* Copy (void) const;
1136  virtual void CDF (double v, double c); // Value, prob <= Value
1137 
1138 private:
1139  class ValueCDF
1140  {
1141 public:
1142  ValueCDF ();
1143  ValueCDF (double v, double c);
1144  ValueCDF (const ValueCDF& c);
1145  double value;
1146  double cdf;
1147  };
1148  virtual void Validate (); // Insure non-decreasing emiprical values
1149  virtual double Interpolate (double, double, double, double, double);
1150  bool validated; // True if non-decreasing validated
1151  std::vector<ValueCDF> emp; // Empicical CDF
1152 };
1153 
1154 
1155 // ValueCDF methods
1157  : value (0.0),
1158  cdf (0.0)
1159 {
1160  NS_LOG_FUNCTION (this);
1161 }
1163  : value (v),
1164  cdf (c)
1165 {
1166  NS_LOG_FUNCTION (this << v << c);
1167 }
1169  : value (c.value),
1170  cdf (c.cdf)
1171 {
1172  NS_LOG_FUNCTION (this << &c);
1173 }
1174 
1175 // -----------------------------------------------------------------------------
1176 // -----------------------------------------------------------------------------
1177 // EmpiricalVariableImpl methods
1179  : validated (false)
1180 {
1181  NS_LOG_FUNCTION (this);
1182 }
1183 
1185  : RandomVariableBase (c),
1186  validated (c.validated),
1187  emp (c.emp)
1188 {
1189  NS_LOG_FUNCTION (this << &c);
1190 }
1191 
1193 {
1194  NS_LOG_FUNCTION (this);
1195 }
1196 
1198 { // Return a value from the empirical distribution
1199  // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1200  NS_LOG_FUNCTION (this);
1201  RngStream *generator = GetStream ();
1202  if (emp.size () == 0)
1203  {
1204  return 0.0; // HuH? No empirical data
1205  }
1206  if (!validated)
1207  {
1208  Validate (); // Insure in non-decreasing
1209  }
1210  double r = generator->RandU01 ();
1211  if (r <= emp.front ().cdf)
1212  {
1213  return emp.front ().value; // Less than first
1214  }
1215  if (r >= emp.back ().cdf)
1216  {
1217  return emp.back ().value; // Greater than last
1218  }
1219  // Binary search
1220  std::vector<ValueCDF>::size_type bottom = 0;
1221  std::vector<ValueCDF>::size_type top = emp.size () - 1;
1222  while (1)
1223  {
1224  std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1225  if (r >= emp[c].cdf && r < emp[c + 1].cdf)
1226  { // Found it
1227  return Interpolate (emp[c].cdf, emp[c + 1].cdf,
1228  emp[c].value, emp[c + 1].value,
1229  r);
1230  }
1231  // Not here, adjust bounds
1232  if (r < emp[c].cdf)
1233  {
1234  top = c - 1;
1235  }
1236  else
1237  {
1238  bottom = c + 1;
1239  }
1240  }
1241 }
1242 
1244 {
1245  NS_LOG_FUNCTION (this);
1246  return new EmpiricalVariableImpl (*this);
1247 }
1248 
1249 void EmpiricalVariableImpl::CDF (double v, double c)
1250 { // Add a new empirical datapoint to the empirical cdf
1251  // NOTE. These MUST be inserted in non-decreasing order
1252  NS_LOG_FUNCTION (this << v << c);
1253  emp.push_back (ValueCDF (v, c));
1254 }
1255 
1257 {
1258  NS_LOG_FUNCTION (this);
1259  ValueCDF prior;
1260  for (std::vector<ValueCDF>::size_type i = 0; i < emp.size (); ++i)
1261  {
1262  ValueCDF& current = emp[i];
1263  if (current.value < prior.value || current.cdf < prior.cdf)
1264  { // Error
1265  std::cerr << "Empirical Dist error,"
1266  << " current value " << current.value
1267  << " prior value " << prior.value
1268  << " current cdf " << current.cdf
1269  << " prior cdf " << prior.cdf << std::endl;
1270  NS_FATAL_ERROR ("Empirical Dist error");
1271  }
1272  prior = current;
1273  }
1274  validated = true;
1275 }
1276 
1277 double EmpiricalVariableImpl::Interpolate (double c1, double c2,
1278  double v1, double v2, double r)
1279 { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1280  NS_LOG_FUNCTION (this << c1 << c2 << v1 << v2 << r);
1281  return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1282 }
1283 
1286 {
1287  NS_LOG_FUNCTION (this);
1288 }
1290  : RandomVariable (variable)
1291 {
1292  NS_LOG_FUNCTION (this << &variable);
1293 }
1294 void
1295 EmpiricalVariable::CDF (double v, double c)
1296 {
1297  NS_LOG_FUNCTION (this << v << c);
1298  EmpiricalVariableImpl *impl = dynamic_cast<EmpiricalVariableImpl *> (Peek ());
1299  NS_ASSERT (impl);
1300  impl->CDF (v, c);
1301 }
1302 
1303 
1304 // -----------------------------------------------------------------------------
1305 // -----------------------------------------------------------------------------
1306 // IntegerValue EmpiricalVariableImpl methods
1309 {
1310 public:
1312 
1313  virtual RandomVariableBase* Copy (void) const;
1317  virtual uint32_t GetInteger ();
1318 private:
1319  virtual double Interpolate (double, double, double, double, double);
1320 };
1321 
1322 
1324 {
1325  NS_LOG_FUNCTION (this);
1326 }
1327 
1329 {
1330  NS_LOG_FUNCTION (this);
1331  return (uint32_t)GetValue ();
1332 }
1333 
1335 {
1336  NS_LOG_FUNCTION (this);
1337  return new IntEmpiricalVariableImpl (*this);
1338 }
1339 
1340 double IntEmpiricalVariableImpl::Interpolate (double c1, double c2,
1341  double v1, double v2, double r)
1342 { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1343  NS_LOG_FUNCTION (this << c1 << c2 << v1 << v2 << r);
1344  return std::ceil (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1345 }
1346 
1349 {
1350  NS_LOG_FUNCTION (this);
1351 }
1352 
1353 // -----------------------------------------------------------------------------
1354 // -----------------------------------------------------------------------------
1355 // DeterministicVariableImpl
1358 {
1359 
1360 public:
1372  explicit DeterministicVariableImpl (double* d, uint32_t c);
1373 
1374  virtual ~DeterministicVariableImpl ();
1378  virtual double GetValue ();
1379  virtual RandomVariableBase* Copy (void) const;
1380 private:
1381  uint32_t count;
1382  uint32_t next;
1383  double* data;
1384 };
1385 
1387  : count (c),
1388  next (c),
1389  data (d)
1390 { // Nothing else needed
1391  NS_LOG_FUNCTION (this << d << c);
1392 }
1393 
1395 {
1396  NS_LOG_FUNCTION (this);
1397 }
1398 
1400 {
1401  NS_LOG_FUNCTION (this);
1402  if (next == count)
1403  {
1404  next = 0;
1405  }
1406  return data[next++];
1407 }
1408 
1410 {
1411  NS_LOG_FUNCTION (this);
1412  return new DeterministicVariableImpl (*this);
1413 }
1414 
1417 {
1418  NS_LOG_FUNCTION (this << d << c);
1419 }
1420 
1421 // -----------------------------------------------------------------------------
1422 // -----------------------------------------------------------------------------
1423 // LogNormalVariableImpl
1426 {
1427 public:
1432  LogNormalVariableImpl (double mu, double sigma);
1433 
1437  virtual double GetValue ();
1438  virtual RandomVariableBase* Copy (void) const;
1439 
1440 private:
1441  double m_mu;
1442  double m_sigma;
1443 };
1444 
1445 
1447 {
1448  NS_LOG_FUNCTION (this);
1449  return new LogNormalVariableImpl (*this);
1450 }
1451 
1453  : m_mu (mu),
1454  m_sigma (sigma)
1455 {
1456  NS_LOG_FUNCTION (this << mu << sigma);
1457 }
1458 
1459 // The code from this function was adapted from the GNU Scientific
1460 // Library 1.8:
1461 /* randist/lognormal.c
1462  *
1463  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
1464  *
1465  * This program is free software; you can redistribute it and/or modify
1466  * it under the terms of the GNU General Public License as published by
1467  * the Free Software Foundation; either version 2 of the License, or (at
1468  * your option) any later version.
1469  *
1470  * This program is distributed in the hope that it will be useful, but
1471  * WITHOUT ANY WARRANTY; without even the implied warranty of
1472  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1473  * General Public License for more details.
1474  *
1475  * You should have received a copy of the GNU General Public License
1476  * along with this program; if not, write to the Free Software
1477  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1478  */
1479 /* The lognormal distribution has the form
1480 
1481  p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
1482 
1483  for x > 0. Lognormal random numbers are the exponentials of
1484  gaussian random numbers */
1485 double
1487 {
1488  NS_LOG_FUNCTION (this);
1489  RngStream *generator = GetStream ();
1490  double u, v, r2, normal, z;
1491 
1492  do
1493  {
1494  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
1495 
1496  u = -1 + 2 * generator->RandU01 ();
1497  v = -1 + 2 * generator->RandU01 ();
1498 
1499  /* see if it is in the unit circle */
1500  r2 = u * u + v * v;
1501  }
1502  while (r2 > 1.0 || r2 == 0);
1503 
1504  normal = u * std::sqrt (-2.0 * std::log (r2) / r2);
1505 
1506  z = std::exp (m_sigma * normal + m_mu);
1507 
1508  return z;
1509 }
1510 
1511 LogNormalVariable::LogNormalVariable (double mu, double sigma)
1512  : RandomVariable (LogNormalVariableImpl (mu, sigma))
1513 {
1514  NS_LOG_FUNCTION (this << mu << sigma);
1515 }
1516 
1517 // -----------------------------------------------------------------------------
1518 // -----------------------------------------------------------------------------
1519 // GammaVariableImpl
1522 {
1523 public:
1528  GammaVariableImpl (double alpha, double beta);
1529 
1533  virtual double GetValue ();
1534 
1539  double GetValue (double alpha, double beta);
1540 
1541  virtual RandomVariableBase* Copy (void) const;
1542 
1543 private:
1544  double m_alpha;
1545  double m_beta;
1547 };
1548 
1549 
1551 {
1552  NS_LOG_FUNCTION (this);
1553  return new GammaVariableImpl (m_alpha, m_beta);
1554 }
1555 
1556 GammaVariableImpl::GammaVariableImpl (double alpha, double beta)
1557  : m_alpha (alpha),
1558  m_beta (beta)
1559 {
1560  NS_LOG_FUNCTION (this << alpha << beta);
1561 }
1562 
1563 double
1565 {
1566  NS_LOG_FUNCTION (this);
1567  return GetValue (m_alpha, m_beta);
1568 }
1569 
1570 /*
1571  The code for the following generator functions was adapted from ns-2
1572  tools/ranvar.cc
1573 
1574  Originally the algorithm was devised by Marsaglia in 2000:
1575  G. Marsaglia, W. W. Tsang: A simple method for gereating Gamma variables
1576  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
1577 
1578  The Gamma distribution density function has the form
1579 
1580  x^(alpha-1) * exp(-x/beta)
1581  p(x; alpha, beta) = ----------------------------
1582  beta^alpha * Gamma(alpha)
1583 
1584  for x > 0.
1585 */
1586 double
1587 GammaVariableImpl::GetValue (double alpha, double beta)
1588 {
1589  NS_LOG_FUNCTION (this << alpha << beta);
1590  RngStream *generator = GetStream ();
1591 
1592  if (alpha < 1)
1593  {
1594  double u = generator->RandU01 ();
1595  return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
1596  }
1597 
1598  double x, v, u;
1599  double d = alpha - 1.0 / 3.0;
1600  double c = (1.0 / 3.0) / std::sqrt (d);
1601 
1602  while (1)
1603  {
1604  do
1605  {
1606  x = m_normal.GetValue ();
1607  v = 1.0 + c * x;
1608  }
1609  while (v <= 0);
1610 
1611  v = v * v * v;
1612  u = generator->RandU01 ();
1613  if (u < 1 - 0.0331 * x * x * x * x)
1614  {
1615  break;
1616  }
1617  if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
1618  {
1619  break;
1620  }
1621  }
1622 
1623  return beta * d * v;
1624 }
1625 
1627  : RandomVariable (GammaVariableImpl (1.0, 1.0))
1628 {
1629  NS_LOG_FUNCTION (this);
1630 }
1631 
1632 GammaVariable::GammaVariable (double alpha, double beta)
1633  : RandomVariable (GammaVariableImpl (alpha, beta))
1634 {
1635  NS_LOG_FUNCTION (this << alpha << beta);
1636 }
1637 
1638 double GammaVariable::GetValue (void) const
1639 {
1640  NS_LOG_FUNCTION (this);
1641  return this->RandomVariable::GetValue ();
1642 }
1643 
1644 double GammaVariable::GetValue (double alpha, double beta) const
1645 {
1646  NS_LOG_FUNCTION (this << alpha << beta);
1647  return ((GammaVariableImpl*)Peek ())->GetValue (alpha, beta);
1648 }
1649 
1650 // -----------------------------------------------------------------------------
1651 // -----------------------------------------------------------------------------
1652 // ErlangVariableImpl
1653 
1656 {
1657 public:
1662  ErlangVariableImpl (unsigned int k, double lambda);
1663 
1667  virtual double GetValue ();
1668 
1673  double GetValue (unsigned int k, double lambda);
1674 
1675  virtual RandomVariableBase* Copy (void) const;
1676 
1677 private:
1678  unsigned int m_k;
1679  double m_lambda;
1680 };
1681 
1682 
1684 {
1685  NS_LOG_FUNCTION (this);
1686  return new ErlangVariableImpl (m_k, m_lambda);
1687 }
1688 
1689 ErlangVariableImpl::ErlangVariableImpl (unsigned int k, double lambda)
1690  : m_k (k),
1691  m_lambda (lambda)
1692 {
1693  NS_LOG_FUNCTION (this << k << lambda);
1694 }
1695 
1696 double
1698 {
1699  NS_LOG_FUNCTION (this);
1700  return GetValue (m_k, m_lambda);
1701 }
1702 
1703 /*
1704  The code for the following generator functions was adapted from ns-2
1705  tools/ranvar.cc
1706 
1707  The Erlang distribution density function has the form
1708 
1709  x^(k-1) * exp(-x/lambda)
1710  p(x; k, lambda) = ---------------------------
1711  lambda^k * (k-1)!
1712 
1713  for x > 0.
1714 */
1715 double
1716 ErlangVariableImpl::GetValue (unsigned int k, double lambda)
1717 {
1720  NS_LOG_FUNCTION (this << k << lambda);
1721  ExponentialVariable exponential (lambda);
1722 
1723  double result = 0;
1724  for (unsigned int i = 0; i < k; ++i)
1725  {
1726  result += exponential.GetValue ();
1727  }
1728 
1729  return result;
1730 }
1731 
1733  : RandomVariable (ErlangVariableImpl (1, 1.0))
1734 {
1735  NS_LOG_FUNCTION (this);
1736 }
1737 
1738 ErlangVariable::ErlangVariable (unsigned int k, double lambda)
1739  : RandomVariable (ErlangVariableImpl (k, lambda))
1740 {
1741  NS_LOG_FUNCTION (this << k << lambda);
1742 }
1743 
1744 double ErlangVariable::GetValue (void) const
1745 {
1746  NS_LOG_FUNCTION (this);
1747  return this->RandomVariable::GetValue ();
1748 }
1749 
1750 double ErlangVariable::GetValue (unsigned int k, double lambda) const
1751 {
1752  NS_LOG_FUNCTION (this << k << lambda);
1753  return ((ErlangVariableImpl*)Peek ())->GetValue (k, lambda);
1754 }
1755 
1756 // -----------------------------------------------------------------------------
1757 // -----------------------------------------------------------------------------
1758 // TriangularVariableImpl methods
1761 {
1762 public:
1768 
1776  TriangularVariableImpl (double s, double l, double mean);
1777 
1779 
1783  virtual double GetValue ();
1784  virtual RandomVariableBase* Copy (void) const;
1785 
1786 private:
1787  double m_min;
1788  double m_max;
1789  double m_mode; // easier to work with the mode internally instead of the mean
1790  // they are related by the simple: mean = (min+max+mode)/3
1791 };
1792 
1794  : m_min (0),
1795  m_max (1),
1796  m_mode (0.5)
1797 {
1798  NS_LOG_FUNCTION (this);
1799 }
1800 
1801 TriangularVariableImpl::TriangularVariableImpl (double s, double l, double mean)
1802  : m_min (s),
1803  m_max (l),
1804  m_mode (3.0 * mean - s - l)
1805 {
1806  NS_LOG_FUNCTION (this << s << l << mean);
1807 }
1808 
1810  : RandomVariableBase (c),
1811  m_min (c.m_min),
1812  m_max (c.m_max),
1813  m_mode (c.m_mode)
1814 {
1815  NS_LOG_FUNCTION (this << &c);
1816 }
1817 
1819 {
1820  NS_LOG_FUNCTION (this);
1821  RngStream *generator = GetStream ();
1822  double u = generator->RandU01 ();
1823  if (u <= (m_mode - m_min) / (m_max - m_min) )
1824  {
1825  return m_min + std::sqrt (u * (m_max - m_min) * (m_mode - m_min) );
1826  }
1827  else
1828  {
1829  return m_max - std::sqrt ( (1 - u) * (m_max - m_min) * (m_max - m_mode) );
1830  }
1831 }
1832 
1834 {
1835  NS_LOG_FUNCTION (this);
1836  return new TriangularVariableImpl (*this);
1837 }
1838 
1841 {
1842  NS_LOG_FUNCTION (this);
1843 }
1844 TriangularVariable::TriangularVariable (double s, double l, double mean)
1845  : RandomVariable (TriangularVariableImpl (s,l,mean))
1846 {
1847  NS_LOG_FUNCTION (this << s << l << mean);
1848 }
1849 
1850 // -----------------------------------------------------------------------------
1851 // -----------------------------------------------------------------------------
1852 // ZipfVariableImpl
1855 {
1856 public:
1861  ZipfVariableImpl (long n, double alpha);
1862 
1866  ZipfVariableImpl ();
1867 
1871  virtual double GetValue ();
1872  virtual RandomVariableBase* Copy (void) const;
1873 
1874 private:
1875  long m_n;
1876  double m_alpha;
1877  double m_c; // the normalization constant
1878 };
1879 
1880 
1882 {
1883  NS_LOG_FUNCTION (this);
1884  return new ZipfVariableImpl (m_n, m_alpha);
1885 }
1886 
1888  : m_n (1),
1889  m_alpha (0),
1890  m_c (1)
1891 {
1892  NS_LOG_FUNCTION (this);
1893 }
1894 
1895 
1897  : m_n (n),
1898  m_alpha (alpha),
1899  m_c (0)
1900 {
1901  // calculate the normalization constant c
1902  NS_LOG_FUNCTION (this << n << alpha);
1903  for (int i = 1; i <= n; i++)
1904  {
1905  m_c += (1.0 / std::pow ((double)i, alpha));
1906  }
1907  m_c = 1.0 / m_c;
1908 }
1909 
1910 double
1912 {
1913  NS_LOG_FUNCTION (this);
1914  RngStream *generator = GetStream ();
1915 
1916  double u = generator->RandU01 ();
1917  double sum_prob = 0,zipf_value = 0;
1918  for (int i = 1; i <= m_n; i++)
1919  {
1920  sum_prob += m_c / std::pow ((double)i, m_alpha);
1921  if (sum_prob > u)
1922  {
1923  zipf_value = i;
1924  break;
1925  }
1926  }
1927  return zipf_value;
1928 }
1929 
1932 {
1933  NS_LOG_FUNCTION (this);
1934 }
1935 
1936 ZipfVariable::ZipfVariable (long n, double alpha)
1937  : RandomVariable (ZipfVariableImpl (n, alpha))
1938 {
1939  NS_LOG_FUNCTION (this << n << alpha);
1940 }
1941 
1942 
1943 // -----------------------------------------------------------------------------
1944 // -----------------------------------------------------------------------------
1945 // ZetaVariableImpl
1948 {
1949 public:
1953  ZetaVariableImpl (double alpha);
1954 
1958  ZetaVariableImpl ();
1959 
1963  virtual double GetValue ();
1964  virtual RandomVariableBase* Copy (void) const;
1965 
1966 private:
1967  double m_alpha;
1968  double m_b; // just for calculus simplifications
1969 };
1970 
1971 
1973 {
1974  NS_LOG_FUNCTION (this);
1975  return new ZetaVariableImpl (m_alpha);
1976 }
1977 
1979  : m_alpha (3.14),
1980  m_b (std::pow (2.0, 2.14))
1981 {
1982  NS_LOG_FUNCTION (this);
1983 }
1984 
1985 
1987  : m_alpha (alpha),
1988  m_b (std::pow (2.0, alpha - 1.0))
1989 {
1990  NS_LOG_FUNCTION (this << alpha);
1991 }
1992 
1993 /*
1994  The code for the following generator functions is borrowed from:
1995  L. Devroye: Non-Uniform Random Variate Generation, Springer-Verlag, New York, 1986.
1996  page 551
1997  */
1998 double
2000 {
2001  NS_LOG_FUNCTION (this);
2002  RngStream *generator = GetStream ();
2003 
2004  double u, v;
2005  double X, T;
2006  double test;
2007 
2008  do
2009  {
2010  u = generator->RandU01 ();
2011  v = generator->RandU01 ();
2012  X = floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
2013  T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
2014  test = v * X * (T - 1.0) / (m_b - 1.0);
2015  }
2016  while ( test > (T / m_b) );
2017 
2018  return X;
2019 }
2020 
2023 {
2024  NS_LOG_FUNCTION (this);
2025 }
2026 
2028  : RandomVariable (ZetaVariableImpl (alpha))
2029 {
2030  NS_LOG_FUNCTION (this << alpha);
2031 }
2032 
2033 
2034 std::ostream & operator << (std::ostream &os, const RandomVariable &var)
2035 {
2036  RandomVariableBase *base = var.Peek ();
2037  ConstantVariableImpl *constant = dynamic_cast<ConstantVariableImpl *> (base);
2038  if (constant != 0)
2039  {
2040  os << "Constant:" << constant->GetValue ();
2041  return os;
2042  }
2043  UniformVariableImpl *uniform = dynamic_cast<UniformVariableImpl *> (base);
2044  if (uniform != 0)
2045  {
2046  os << "Uniform:" << uniform->GetMin () << ":" << uniform->GetMax ();
2047  return os;
2048  }
2049  NormalVariableImpl *normal = dynamic_cast<NormalVariableImpl *> (base);
2050  if (normal != 0)
2051  {
2052  os << "Normal:" << normal->GetMean () << ":" << normal->GetVariance ();
2053  double bound = normal->GetBound ();
2055  {
2056  os << ":" << bound;
2057  }
2058  return os;
2059  }
2061  os.setstate (std::ios_base::badbit);
2062  return os;
2063 }
2064 std::istream & operator >> (std::istream &is, RandomVariable &var)
2065 {
2066  std::string value;
2067  is >> value;
2068  std::string::size_type tmp;
2069  tmp = value.find (":");
2070  if (tmp == std::string::npos)
2071  {
2072  is.setstate (std::ios_base::badbit);
2073  return is;
2074  }
2075  std::string type = value.substr (0, tmp);
2076  value = value.substr (tmp + 1, value.npos);
2077  if (type == "Constant")
2078  {
2079  std::istringstream iss (value);
2080  double constant;
2081  iss >> constant;
2082  var = ConstantVariable (constant);
2083  }
2084  else if (type == "Uniform")
2085  {
2086  if (value.size () == 0)
2087  {
2088  var = UniformVariable ();
2089  }
2090  else
2091  {
2092  tmp = value.find (":");
2093  if (tmp == value.npos)
2094  {
2095  NS_FATAL_ERROR ("bad Uniform value: " << value);
2096  }
2097  std::istringstream issA (value.substr (0, tmp));
2098  std::istringstream issB (value.substr (tmp + 1, value.npos));
2099  double a, b;
2100  issA >> a;
2101  issB >> b;
2102  var = UniformVariable (a, b);
2103  }
2104  }
2105  else if (type == "Normal")
2106  {
2107  if (value.size () == 0)
2108  {
2109  var = NormalVariable ();
2110  }
2111  else
2112  {
2113  tmp = value.find (":");
2114  if (tmp == value.npos)
2115  {
2116  NS_FATAL_ERROR ("bad Normal value: " << value);
2117  }
2118  std::string::size_type tmp2;
2119  std::string sub = value.substr (tmp + 1, value.npos);
2120  tmp2 = sub.find (":");
2121  if (tmp2 == value.npos)
2122  {
2123  std::istringstream issA (value.substr (0, tmp));
2124  std::istringstream issB (sub);
2125  double a, b;
2126  issA >> a;
2127  issB >> b;
2128  var = NormalVariable (a, b);
2129  }
2130  else
2131  {
2132  std::istringstream issA (value.substr (0, tmp));
2133  std::istringstream issB (sub.substr (0, tmp2));
2134  std::istringstream issC (sub.substr (tmp2 + 1, value.npos));
2135  double a, b, c;
2136  issA >> a;
2137  issB >> b;
2138  issC >> c;
2139  var = NormalVariable (a, b, c);
2140  }
2141  }
2142  }
2143  else
2144  {
2145  NS_FATAL_ERROR ("RandomVariable deserialization not implemented for " << type);
2147  }
2148  return is;
2149 }
2150 
2151 } // namespace ns3
RandomVariableBase * Peek(void) const
virtual RandomVariableBase * Copy(void) const
ExponentialVariableImpl()
Constructs an exponential random variable with a mean value of 1.0.
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:49
TriangularVariable()
Creates a triangle distribution random number generator in the range [0.0 .
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
ErlangVariable()
Constructs an Erlang random variable with k = 1 and lambda = 1.0.
A random variable that returns a constantClass ConstantVariable defines a random number generator tha...
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
virtual uint32_t GetInteger()
GammaVariable()
Constructs a gamma random variable with alpha = 1.0 and beta = 1.0.
WeibullVariableImpl()
Constructs a weibull random variable with a mean value of 1.0 and a shape (alpha) parameter of 1...
#define NS_ASSERT(condition)
Definition: assert.h:64
virtual RandomVariableBase * Copy(void) const
virtual RandomVariableBase * Copy() const
virtual RandomVariableBase * Copy(void) const
virtual RandomVariableBase * Copy(void) const
virtual double GetValue()
ATTRIBUTE_CHECKER_IMPLEMENT(Callback)
Attribute checker.
virtual RandomVariableBase * Copy(void) const
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
static uint64_t GetRun(void)
void NewConstant(double c)
Specify a new constant RNG for this generator.
ExponentialVariable()
Constructs an exponential random variable with a mean value of 1.0.
ZipfVariable()
Constructs a Zipf random variable with N=1 and alpha=0.
NormalVariable()
Constructs an normal random variable with a mean value of 0 and variance of 1.
UniformVariableImpl()
Creates a uniform random number generator in the range [0.0 .
Class NormalVariable defines a random variable with a normal (Gaussian) distribution.
virtual double Interpolate(double, double, double, double, double)
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:38
virtual RandomVariableBase * Copy(void) const
TriangularVariableImpl()
Creates a triangle distribution random number generator in the range [0.0 .
RandomVariable & operator=(const RandomVariable &o)
SequentialVariableImpl(double f, double l, double i=1, uint32_t c=1)
Constructor for the SequentialVariableImpl RNG.
uint8_t data[writeSize]
Ptr< SampleEmitter > s
DeterministicVariable(double *d, uint32_t c)
Constructor.
WeibullVariable()
Constructs a weibull random variable with a mean value of 1.0 and a shape (alpha) parameter of 1...
LogNormalVariableImpl(double mu, double sigma)
double GetValue(void) const
call RandomVariable::GetValue
double GetValue(void) const
call RandomVariable::GetValue
The uniform distribution RNG for NS-3.
SequentialVariable(double f, double l, double i=1, uint32_t c=1)
Constructor for the SequentialVariable RNG.
virtual RandomVariableBase * Copy(void) const =0
virtual RandomVariableBase * Copy(void) const
ErlangVariableImpl(unsigned int k, double lambda)
ParetoVariableImpl()
Constructs a pareto random variable with a mean of 1 and a shape parameter of 1.5.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
LogNormalVariable(double mu, double sigma)
RandomVariableBase * m_variable
virtual RandomVariableBase * Copy(void) const
ZipfVariableImpl()
zipf variable with N=1 and alpha=0
std::vector< ValueCDF > emp
RngStream * GetStream(void)
virtual RandomVariableBase * Copy(void) const
virtual RandomVariableBase * Copy(void) const
virtual void CDF(double v, double c)
Specifies a point in the empirical distribution.
NormalVariableImpl()
Constructs an normal random variable with a mean value of 0 and variance of 1.
virtual double GetValue()
Exponentially Distributed random varThis class supports the creation of objects that return random nu...
double RandU01(void)
Generate the next random number for this stream.
Definition: rng-stream.cc:249
double GetMin(void) const
virtual RandomVariableBase * Copy(void) const
static uint32_t GetSeed(void)
Get the seed value.
double GetMax(void) const
virtual RandomVariableBase * Copy(void) const
ATTRIBUTE_VALUE_IMPLEMENT(RandomVariable)
double GetValue(void) const
call RandomVariable::GetValue
ZetaVariableImpl()
zipf variable with alpha=1.1
Time current
virtual RandomVariableBase * Copy(void) const
double GetMean(void) const
void CDF(double v, double c)
Specifies a point in the empirical distribution.
virtual double GetValue()
EmpiricalVariable()
Constructor for the EmpiricalVariable random variables.
virtual double GetValue()=0
UniformVariable()
Creates a uniform random number generator in the range [0.0 .
virtual double GetValue()
virtual uint32_t GetInteger()
ZetaVariable()
Constructs a Zeta random variable with alpha=3.14.
GammaVariableImpl(double alpha, double beta)
static const double INFINITE_VALUE
ConstantVariableImpl()
Construct a ConstantVariableImpl RNG that returns zero every sample.
double GetBound(void) const
DeterministicVariableImpl(double *d, uint32_t c)
Constructor.
double GetVariance(void) const
EmpiricalVariable distribution random varDefines a random variable that has a specified, empirical distribution.
static uint64_t GetNextStreamIndex(void)
EmpiricalVariableImpl()
Constructor for the EmpiricalVariableImpl random variables.
The basic RNG for NS-3.
ConstantVariable()
Construct a ConstantVariable RNG that returns zero every sample.
double GetValue(void) const
Returns a random double from the underlying distribution.
uint32_t GetInteger(void) const
Returns a random integer integer from the underlying distribution.
virtual double Interpolate(double, double, double, double, double)
virtual RandomVariableBase * Copy(void) const
virtual RandomVariableBase * Copy(void) const
void SetConstant(double c)
Specify a new constant RNG for this generator.
void test(void)
Ptr< T > Copy(Ptr< T > object)
Definition: ptr.h:387
ParetoVariable()
Constructs a pareto random variable with a mean of 1 and a shape parameter of 1.5.