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 
51 {
52 public:
55  virtual ~RandomVariableBase ();
56  virtual double GetValue () = 0;
57  virtual uint32_t GetInteger ();
58  virtual RandomVariableBase* Copy (void) const = 0;
59  RngStream *GetStream(void);
60 private:
61  RngStream* m_generator; // underlying generator being wrapped
62 };
63 
65  : m_generator (0)
66 {
67  NS_LOG_FUNCTION (this);
68 }
69 
71  : m_generator (0)
72 {
73  NS_LOG_FUNCTION (this << &r);
74  if (r.m_generator != 0)
75  {
79  }
80 }
81 
83 {
84  NS_LOG_FUNCTION (this);
85  delete m_generator;
86 }
87 
89 {
90  NS_LOG_FUNCTION (this);
91  return (uint32_t)GetValue ();
92 }
93 
94 RngStream *
96 {
97  NS_LOG_FUNCTION (this);
98  if (m_generator == 0)
99  {
103  }
104  return m_generator;
105 }
106 
107 // -------------------------------------------------------
108 
110  : m_variable (0)
111 {
112  NS_LOG_FUNCTION (this);
113 }
115  : m_variable (o.m_variable->Copy ())
116 {
117 }
119  : m_variable (variable.Copy ())
120 {
121 }
124 {
125  if (&o == this)
126  {
127  return *this;
128  }
129  delete m_variable;
130  m_variable = o.m_variable->Copy ();
131  return *this;
132 }
134 {
135  NS_LOG_FUNCTION (this);
136  delete m_variable;
137 }
138 double
140 {
141  NS_LOG_FUNCTION (this);
142  return m_variable->GetValue ();
143 }
144 
145 uint32_t
147 {
148  NS_LOG_FUNCTION (this);
149  return m_variable->GetInteger ();
150 }
151 
154 {
155  NS_LOG_FUNCTION (this);
156  return m_variable;
157 }
158 
159 
162 
163 // -----------------------------------------------------------------------------
164 // -----------------------------------------------------------------------------
165 // UniformVariableImpl
166 
169 {
170 public:
176 
182  UniformVariableImpl (double s, double l);
183 
185 
186  double GetMin (void) const;
187  double GetMax (void) const;
188 
192  virtual double GetValue ();
193 
197  virtual double GetValue (double s, double l);
198 
199  virtual RandomVariableBase* Copy (void) const;
200 
201 private:
202  double m_min;
203  double m_max;
204 };
205 
207  : m_min (0),
208  m_max (1.0)
209 {
210  NS_LOG_FUNCTION (this);
211 }
212 
214  : m_min (s),
215  m_max (l)
216 {
217  NS_LOG_FUNCTION (this << s << l);
218 }
219 
221  : RandomVariableBase (c),
222  m_min (c.m_min),
223  m_max (c.m_max)
224 {
225  NS_LOG_FUNCTION (this << &c);
226 }
227 
228 double
230 {
231  NS_LOG_FUNCTION (this);
232  return m_min;
233 }
234 double
236 {
237  NS_LOG_FUNCTION (this);
238  return m_max;
239 }
240 
241 
243 {
244  NS_LOG_FUNCTION (this);
245  RngStream *generator = GetStream ();
246  return m_min + generator->RandU01 () * (m_max - m_min);
247 }
248 
249 double UniformVariableImpl::GetValue (double s, double l)
250 {
251  NS_LOG_FUNCTION (this << s << l);
252  RngStream *generator = GetStream ();
253  return s + generator->RandU01 () * (l - s);
254 }
255 
257 {
258  NS_LOG_FUNCTION (this);
259  return new UniformVariableImpl (*this);
260 }
261 
264 {
265  NS_LOG_FUNCTION (this);
266 }
269 {
270  NS_LOG_FUNCTION (this << s << l);
271 }
272 
273 double UniformVariable::GetValue (void) const
274 {
275  NS_LOG_FUNCTION (this);
276  return this->RandomVariable::GetValue ();
277 }
278 
279 double UniformVariable::GetValue (double s, double l)
280 {
281  NS_LOG_FUNCTION (this << s << l);
282  return ((UniformVariableImpl*)Peek ())->GetValue (s,l);
283 }
284 
285 uint32_t UniformVariable::GetInteger (uint32_t s, uint32_t l)
286 {
287  NS_LOG_FUNCTION (this << s << l);
288  NS_ASSERT (s <= l);
289  return static_cast<uint32_t> ( GetValue (s, l + 1) );
290 }
291 
292 // -----------------------------------------------------------------------------
293 // -----------------------------------------------------------------------------
294 // ConstantVariableImpl methods
295 
298 {
299 
300 public:
305 
311  ConstantVariableImpl (double c);
312 
313 
315 
320  void NewConstant (double c);
321 
325  virtual double GetValue ();
326  virtual uint32_t GetInteger ();
327  virtual RandomVariableBase* Copy (void) const;
328 private:
329  double m_const;
330 };
331 
333  : m_const (0)
334 {
335  NS_LOG_FUNCTION (this);
336 }
337 
339  : m_const (c)
340 {
341  NS_LOG_FUNCTION (this << c);
342 }
343 
345  : RandomVariableBase (c),
346  m_const (c.m_const)
347 {
348  NS_LOG_FUNCTION (this << &c);
349 }
350 
352 {
353  NS_LOG_FUNCTION (this << c);
354  m_const = c;
355 }
356 
358 {
359  NS_LOG_FUNCTION (this);
360  return m_const;
361 }
362 
364 {
365  NS_LOG_FUNCTION (this);
366  return (uint32_t)m_const;
367 }
368 
370 {
371  NS_LOG_FUNCTION (this);
372  return new ConstantVariableImpl (*this);
373 }
374 
377 {
378  NS_LOG_FUNCTION (this);
379 }
382 {
383  NS_LOG_FUNCTION (this << c);
384 }
385 void
387 {
388  NS_LOG_FUNCTION (this << c);
389  *this = ConstantVariable (c);
390 }
391 
392 // -----------------------------------------------------------------------------
393 // -----------------------------------------------------------------------------
394 // SequentialVariableImpl methods
395 
396 
399 {
400 
401 public:
413  SequentialVariableImpl (double f, double l, double i = 1, uint32_t c = 1);
414 
425  SequentialVariableImpl (double f, double l, const RandomVariable& i, uint32_t c = 1);
426 
428 
433  virtual double GetValue ();
434  virtual RandomVariableBase* Copy (void) const;
435 private:
436  double m_min;
437  double m_max;
439  uint32_t m_consecutive;
440  double m_current;
442 };
443 
444 SequentialVariableImpl::SequentialVariableImpl (double f, double l, double i, uint32_t c)
445  : m_min (f),
446  m_max (l),
447  m_increment (ConstantVariable (i)),
448  m_consecutive (c),
449  m_current (f),
450  m_currentConsecutive (0)
451 {
452  NS_LOG_FUNCTION (this << f << l << i << c);
453 }
454 
455 SequentialVariableImpl::SequentialVariableImpl (double f, double l, const RandomVariable& i, uint32_t c)
456  : m_min (f),
457  m_max (l),
458  m_increment (i),
459  m_consecutive (c),
460  m_current (f),
461  m_currentConsecutive (0)
462 {
463  NS_LOG_FUNCTION (this << f << l << i << c);
464 }
465 
467  : RandomVariableBase (c),
468  m_min (c.m_min),
469  m_max (c.m_max),
470  m_increment (c.m_increment),
471  m_consecutive (c.m_consecutive),
472  m_current (c.m_current),
473  m_currentConsecutive (c.m_currentConsecutive)
474 {
475  NS_LOG_FUNCTION (this << &c);
476 }
477 
479 {
480  NS_LOG_FUNCTION (this);
481 }
482 
484 { // Return a sequential series of values
485  NS_LOG_FUNCTION (this);
486  double r = m_current;
488  { // Time to advance to next
491  if (m_current >= m_max)
492  {
493  m_current = m_min + (m_current - m_max);
494  }
495  }
496  return r;
497 }
498 
500 {
501  NS_LOG_FUNCTION (this);
502  return new SequentialVariableImpl (*this);
503 }
504 
505 SequentialVariable::SequentialVariable (double f, double l, double i, uint32_t c)
506  : RandomVariable (SequentialVariableImpl (f, l, i, c))
507 {
508  NS_LOG_FUNCTION (this << f << l << i << c);
509 }
510 SequentialVariable::SequentialVariable (double f, double l, const RandomVariable& i, uint32_t c)
511  : RandomVariable (SequentialVariableImpl (f, l, i, c))
512 {
513  NS_LOG_FUNCTION (this << f << l << i << c);
514 }
515 
516 // -----------------------------------------------------------------------------
517 // -----------------------------------------------------------------------------
518 // ExponentialVariableImpl methods
519 
522 {
523 public:
529 
535  explicit ExponentialVariableImpl (double m);
536 
548  ExponentialVariableImpl (double m, double b);
549 
551 
555  virtual double GetValue ();
556  virtual RandomVariableBase* Copy (void) const;
557 
558 private:
559  double m_mean; // Mean value of RV
560  double m_bound; // Upper bound on value (if non-zero)
561 };
562 
564  : m_mean (1.0),
565  m_bound (0)
566 {
567  NS_LOG_FUNCTION (this);
568 }
569 
571  : m_mean (m),
572  m_bound (0)
573 {
574  NS_LOG_FUNCTION (this << m);
575 }
576 
578  : m_mean (m),
579  m_bound (b)
580 {
581  NS_LOG_FUNCTION (this << m << b);
582 }
583 
585  : RandomVariableBase (c),
586  m_mean (c.m_mean),
587  m_bound (c.m_bound)
588 {
589  NS_LOG_FUNCTION (this << &c);
590 }
591 
593 {
594  NS_LOG_FUNCTION (this);
595  RngStream *generator = GetStream ();
596  while (1)
597  {
598  double r = -m_mean*std::log (generator->RandU01 ());
599  if (m_bound == 0 || r <= m_bound)
600  {
601  return r;
602  }
603  // otherwise, try again
604  }
605 }
606 
608 {
609  NS_LOG_FUNCTION (this);
610  return new ExponentialVariableImpl (*this);
611 }
612 
615 {
616  NS_LOG_FUNCTION (this);
617 }
620 {
621  NS_LOG_FUNCTION (this << m);
622 }
625 {
626  NS_LOG_FUNCTION (this << m << b);
627 }
628 
629 // -----------------------------------------------------------------------------
630 // -----------------------------------------------------------------------------
631 // ParetoVariableImpl methods
634 {
635 public:
641 
648  explicit ParetoVariableImpl (double m);
649 
657  ParetoVariableImpl (double m, double s);
658 
671  ParetoVariableImpl (double m, double s, double b);
672 
679  ParetoVariableImpl (std::pair<double, double> params);
680 
693  ParetoVariableImpl (std::pair<double, double> params, double b);
694 
696 
700  virtual double GetValue ();
701  virtual RandomVariableBase* Copy () const;
702 
703 private:
704  double m_scale; // Scale value of RV
705  double m_shape; // Shape parameter
706  double m_bound; // Upper bound on value (if non-zero)
707 };
708 
710  : m_scale (0.5 / 1.5),
711  m_shape (1.5),
712  m_bound (0)
713 {
714  NS_LOG_FUNCTION (this);
715 }
716 
718  : m_scale (m * 0.5 / 1.5),
719  m_shape (1.5),
720  m_bound (0)
721 {
722  NS_LOG_FUNCTION (this << m);
723 }
724 
726  : m_scale (m * (s - 1.0) / s),
727  m_shape (s),
728  m_bound (0)
729 {
730  NS_LOG_FUNCTION (this << m << s);
731 }
732 
733 ParetoVariableImpl::ParetoVariableImpl (double m, double s, double b)
734  : m_scale (m * (s - 1.0) / s),
735  m_shape (s),
736  m_bound (b)
737 {
738  NS_LOG_FUNCTION (this << m << s << b);
739 }
740 
741 ParetoVariableImpl::ParetoVariableImpl (std::pair<double, double> params)
742  : m_scale (params.first),
743  m_shape (params.second),
744  m_bound (0)
745 {
746  NS_LOG_FUNCTION (this << &params);
747 }
748 
749 ParetoVariableImpl::ParetoVariableImpl (std::pair<double, double> params, double b)
750  : m_scale (params.first),
751  m_shape (params.second),
752  m_bound (b)
753 {
754  NS_LOG_FUNCTION (this << &params << b);
755 }
756 
758  : RandomVariableBase (c),
759  m_scale (c.m_scale),
760  m_shape (c.m_shape),
761  m_bound (c.m_bound)
762 {
763  NS_LOG_FUNCTION (this << &c);
764 }
765 
767 {
768  NS_LOG_FUNCTION (this);
769  RngStream *generator = GetStream ();
770  while (1)
771  {
772  double r = (m_scale * ( 1.0 / std::pow (generator->RandU01 (), 1.0 / m_shape)));
773  if (m_bound == 0 || r <= m_bound)
774  {
775  return r;
776  }
777  // otherwise, try again
778  }
779 }
780 
782 {
783  NS_LOG_FUNCTION (this);
784  return new ParetoVariableImpl (*this);
785 }
786 
789 {
790  NS_LOG_FUNCTION (this);
791 }
794 {
795  NS_LOG_FUNCTION (this << m);
796 }
799 {
800  NS_LOG_FUNCTION (this << m << s);
801 }
802 ParetoVariable::ParetoVariable (double m, double s, double b)
803  : RandomVariable (ParetoVariableImpl (m, s, b))
804 {
805  NS_LOG_FUNCTION (this << m << s << b);
806 }
807 ParetoVariable::ParetoVariable (std::pair<double, double> params)
809 {
810  NS_LOG_FUNCTION (this << &params);
811 }
812 ParetoVariable::ParetoVariable (std::pair<double, double> params, double b)
813  : RandomVariable (ParetoVariableImpl (params, b))
814 {
815  NS_LOG_FUNCTION (this << &params << b);
816 }
817 
818 // -----------------------------------------------------------------------------
819 // -----------------------------------------------------------------------------
820 // WeibullVariableImpl methods
821 
824 {
825 public:
831 
832 
838  WeibullVariableImpl (double m);
839 
846  WeibullVariableImpl (double m, double s);
847 
859  WeibullVariableImpl (double m, double s, double b);
860 
862 
866  virtual double GetValue ();
867  virtual RandomVariableBase* Copy (void) const;
868 
869 private:
870  double m_mean; // Mean value of RV
871  double m_alpha; // Shape parameter
872  double m_bound; // Upper bound on value (if non-zero)
873 };
874 
876  m_alpha (1),
877  m_bound (0)
878 {
879  NS_LOG_FUNCTION (this);
880 }
882  : m_mean (m),
883  m_alpha (1),
884  m_bound (0)
885 {
886  NS_LOG_FUNCTION (this << m);
887 }
889  : m_mean (m),
890  m_alpha (s),
891  m_bound (0)
892 {
893  NS_LOG_FUNCTION (this << m << s);
894 }
895 WeibullVariableImpl::WeibullVariableImpl (double m, double s, double b)
896  : m_mean (m),
897  m_alpha (s),
898  m_bound (b)
899 {
900  NS_LOG_FUNCTION (this << m << s << b);
901 }
903  : RandomVariableBase (c),
904  m_mean (c.m_mean),
905  m_alpha (c.m_alpha),
906  m_bound (c.m_bound)
907 {
908  NS_LOG_FUNCTION (this << &c);
909 }
910 
912 {
913  NS_LOG_FUNCTION (this);
914  RngStream *generator = GetStream ();
915  double exponent = 1.0 / m_alpha;
916  while (1)
917  {
918  double r = m_mean * std::pow ( -std::log (generator->RandU01 ()), exponent);
919  if (m_bound == 0 || r <= m_bound)
920  {
921  return r;
922  }
923  // otherwise, try again
924  }
925 }
926 
928 {
929  NS_LOG_FUNCTION (this);
930  return new WeibullVariableImpl (*this);
931 }
932 
935 {
936  NS_LOG_FUNCTION (this);
937 }
940 {
941  NS_LOG_FUNCTION (this << m);
942 }
945 {
946  NS_LOG_FUNCTION (this << m << s);
947 }
948 WeibullVariable::WeibullVariable (double m, double s, double b)
949  : RandomVariable (WeibullVariableImpl (m, s, b))
950 {
951  NS_LOG_FUNCTION (this << m << s << b);
952 }
953 
954 // -----------------------------------------------------------------------------
955 // -----------------------------------------------------------------------------
956 // NormalVariableImpl methods
957 
959 class NormalVariableImpl : public RandomVariableBase // Normally Distributed random var
960 
961 {
962 public:
963  static const double INFINITE_VALUE;
969 
976  NormalVariableImpl (double m, double v, double b = INFINITE_VALUE);
977 
979 
983  virtual double GetValue ();
984  virtual RandomVariableBase* Copy (void) const;
985 
986  double GetMean (void) const;
987  double GetVariance (void) const;
988  double GetBound (void) const;
989 
990 private:
991  double m_mean; // Mean value of RV
992  double m_variance; // Mean value of RV
993  double m_bound; // Bound on value's difference from the mean (absolute value)
994  bool m_nextValid; // True if next valid
995  double m_next; // The algorithm produces two values at a time
996 };
997 
998 const double NormalVariableImpl::INFINITE_VALUE = 1e307;
999 
1001  : m_mean (0.0),
1002  m_variance (1.0),
1003  m_bound (INFINITE_VALUE),
1004  m_nextValid (false)
1005 {
1006  NS_LOG_FUNCTION (this);
1007 }
1008 
1009 NormalVariableImpl::NormalVariableImpl (double m, double v, double b)
1010  : m_mean (m),
1011  m_variance (v),
1012  m_bound (b),
1013  m_nextValid (false)
1014 {
1015  NS_LOG_FUNCTION (this << m << v << b);
1016 }
1017 
1019  : RandomVariableBase (c),
1020  m_mean (c.m_mean),
1021  m_variance (c.m_variance),
1022  m_bound (c.m_bound),
1023  m_nextValid (false)
1024 {
1025  NS_LOG_FUNCTION (this << &c);
1026 }
1027 
1029 {
1030  NS_LOG_FUNCTION (this);
1031  RngStream *generator = GetStream ();
1032  if (m_nextValid)
1033  { // use previously generated
1034  m_nextValid = false;
1035  return m_next;
1036  }
1037  while (1)
1038  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1039  // for algorithm; basically a Box-Muller transform:
1040  // http://en.wikipedia.org/wiki/Box-Muller_transform
1041  double u1 = generator->RandU01 ();
1042  double u2 = generator->RandU01 ();
1043  double v1 = 2 * u1 - 1;
1044  double v2 = 2 * u2 - 1;
1045  double w = v1 * v1 + v2 * v2;
1046  if (w <= 1.0)
1047  { // Got good pair
1048  double y = std::sqrt ((-2 * std::log (w)) / w);
1049  m_next = m_mean + v2 * y * std::sqrt (m_variance);
1050  // if next is in bounds, it is valid
1051  m_nextValid = std::fabs (m_next - m_mean) <= m_bound;
1052  double x1 = m_mean + v1 * y * std::sqrt (m_variance);
1053  // if x1 is in bounds, return it
1054  if (std::fabs (x1 - m_mean) <= m_bound)
1055  {
1056  return x1;
1057  }
1058  // otherwise try and return m_next if it is valid
1059  else if (m_nextValid)
1060  {
1061  m_nextValid = false;
1062  return m_next;
1063  }
1064  // otherwise, just run this loop again
1065  }
1066  }
1067 }
1068 
1070 {
1071  NS_LOG_FUNCTION (this);
1072  return new NormalVariableImpl (*this);
1073 }
1074 
1075 double
1077 {
1078  NS_LOG_FUNCTION (this);
1079  return m_mean;
1080 }
1081 
1082 double
1084 {
1085  NS_LOG_FUNCTION (this);
1086  return m_variance;
1087 }
1088 
1089 double
1091 {
1092  NS_LOG_FUNCTION (this);
1093  return m_bound;
1094 }
1095 
1098 {
1099  NS_LOG_FUNCTION (this);
1100 }
1103 {
1104  NS_LOG_FUNCTION (this << m << v);
1105 }
1106 NormalVariable::NormalVariable (double m, double v, double b)
1107  : RandomVariable (NormalVariableImpl (m, v, b))
1108 {
1109  NS_LOG_FUNCTION (this << m << v << b);
1110 }
1111 
1112 // -----------------------------------------------------------------------------
1113 // -----------------------------------------------------------------------------
1116 {
1117 public:
1121  explicit EmpiricalVariableImpl ();
1122 
1123  virtual ~EmpiricalVariableImpl ();
1128  virtual double GetValue ();
1129  virtual RandomVariableBase* Copy (void) const;
1135  virtual void CDF (double v, double c); // Value, prob <= Value
1136 
1137 private:
1138  class ValueCDF
1139  {
1140 public:
1141  ValueCDF ();
1142  ValueCDF (double v, double c);
1143  ValueCDF (const ValueCDF& c);
1144  double value;
1145  double cdf;
1146  };
1147  virtual void Validate (); // Insure non-decreasing emiprical values
1148  virtual double Interpolate (double, double, double, double, double);
1149  bool validated; // True if non-decreasing validated
1150  std::vector<ValueCDF> emp; // Empicical CDF
1151 };
1152 
1153 
1154 // ValueCDF methods
1156  : value (0.0),
1157  cdf (0.0)
1158 {
1159  NS_LOG_FUNCTION (this);
1160 }
1162  : value (v),
1163  cdf (c)
1164 {
1165  NS_LOG_FUNCTION (this << v << c);
1166 }
1168  : value (c.value),
1169  cdf (c.cdf)
1170 {
1171  NS_LOG_FUNCTION (this << &c);
1172 }
1173 
1174 // -----------------------------------------------------------------------------
1175 // -----------------------------------------------------------------------------
1176 // EmpiricalVariableImpl methods
1178  : validated (false)
1179 {
1180  NS_LOG_FUNCTION (this);
1181 }
1182 
1184  : RandomVariableBase (c),
1185  validated (c.validated),
1186  emp (c.emp)
1187 {
1188  NS_LOG_FUNCTION (this << &c);
1189 }
1190 
1192 {
1193  NS_LOG_FUNCTION (this);
1194 }
1195 
1197 { // Return a value from the empirical distribution
1198  // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1199  NS_LOG_FUNCTION (this);
1200  RngStream *generator = GetStream ();
1201  if (emp.size () == 0)
1202  {
1203  return 0.0; // HuH? No empirical data
1204  }
1205  if (!validated)
1206  {
1207  Validate (); // Insure in non-decreasing
1208  }
1209  double r = generator->RandU01 ();
1210  if (r <= emp.front ().cdf)
1211  {
1212  return emp.front ().value; // Less than first
1213  }
1214  if (r >= emp.back ().cdf)
1215  {
1216  return emp.back ().value; // Greater than last
1217  }
1218  // Binary search
1219  std::vector<ValueCDF>::size_type bottom = 0;
1220  std::vector<ValueCDF>::size_type top = emp.size () - 1;
1221  while (1)
1222  {
1223  std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1224  if (r >= emp[c].cdf && r < emp[c + 1].cdf)
1225  { // Found it
1226  return Interpolate (emp[c].cdf, emp[c + 1].cdf,
1227  emp[c].value, emp[c + 1].value,
1228  r);
1229  }
1230  // Not here, adjust bounds
1231  if (r < emp[c].cdf)
1232  {
1233  top = c - 1;
1234  }
1235  else
1236  {
1237  bottom = c + 1;
1238  }
1239  }
1240 }
1241 
1243 {
1244  NS_LOG_FUNCTION (this);
1245  return new EmpiricalVariableImpl (*this);
1246 }
1247 
1248 void EmpiricalVariableImpl::CDF (double v, double c)
1249 { // Add a new empirical datapoint to the empirical cdf
1250  // NOTE. These MUST be inserted in non-decreasing order
1251  NS_LOG_FUNCTION (this << v << c);
1252  emp.push_back (ValueCDF (v, c));
1253 }
1254 
1256 {
1257  NS_LOG_FUNCTION (this);
1258  ValueCDF prior;
1259  for (std::vector<ValueCDF>::size_type i = 0; i < emp.size (); ++i)
1260  {
1261  ValueCDF& current = emp[i];
1262  if (current.value < prior.value || current.cdf < prior.cdf)
1263  { // Error
1264  std::cerr << "Empirical Dist error,"
1265  << " current value " << current.value
1266  << " prior value " << prior.value
1267  << " current cdf " << current.cdf
1268  << " prior cdf " << prior.cdf << std::endl;
1269  NS_FATAL_ERROR ("Empirical Dist error");
1270  }
1271  prior = current;
1272  }
1273  validated = true;
1274 }
1275 
1276 double EmpiricalVariableImpl::Interpolate (double c1, double c2,
1277  double v1, double v2, double r)
1278 { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1279  NS_LOG_FUNCTION (this << c1 << c2 << v1 << v2 << r);
1280  return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1281 }
1282 
1285 {
1286  NS_LOG_FUNCTION (this);
1287 }
1289  : RandomVariable (variable)
1290 {
1291  NS_LOG_FUNCTION (this << &variable);
1292 }
1293 void
1294 EmpiricalVariable::CDF (double v, double c)
1295 {
1296  NS_LOG_FUNCTION (this << v << c);
1297  EmpiricalVariableImpl *impl = dynamic_cast<EmpiricalVariableImpl *> (Peek ());
1298  NS_ASSERT (impl);
1299  impl->CDF (v, c);
1300 }
1301 
1302 
1303 // -----------------------------------------------------------------------------
1304 // -----------------------------------------------------------------------------
1305 // IntegerValue EmpiricalVariableImpl methods
1308 {
1309 public:
1311 
1312  virtual RandomVariableBase* Copy (void) const;
1316  virtual uint32_t GetInteger ();
1317 private:
1318  virtual double Interpolate (double, double, double, double, double);
1319 };
1320 
1321 
1323 {
1324  NS_LOG_FUNCTION (this);
1325 }
1326 
1328 {
1329  NS_LOG_FUNCTION (this);
1330  return (uint32_t)GetValue ();
1331 }
1332 
1334 {
1335  NS_LOG_FUNCTION (this);
1336  return new IntEmpiricalVariableImpl (*this);
1337 }
1338 
1339 double IntEmpiricalVariableImpl::Interpolate (double c1, double c2,
1340  double v1, double v2, double r)
1341 { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1342  NS_LOG_FUNCTION (this << c1 << c2 << v1 << v2 << r);
1343  return std::ceil (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1344 }
1345 
1348 {
1349  NS_LOG_FUNCTION (this);
1350 }
1351 
1352 // -----------------------------------------------------------------------------
1353 // -----------------------------------------------------------------------------
1354 // DeterministicVariableImpl
1357 {
1358 
1359 public:
1371  explicit DeterministicVariableImpl (double* d, uint32_t c);
1372 
1373  virtual ~DeterministicVariableImpl ();
1377  virtual double GetValue ();
1378  virtual RandomVariableBase* Copy (void) const;
1379 private:
1380  uint32_t count;
1381  uint32_t next;
1382  double* data;
1383 };
1384 
1386  : count (c),
1387  next (c),
1388  data (d)
1389 { // Nothing else needed
1390  NS_LOG_FUNCTION (this << d << c);
1391 }
1392 
1394 {
1395  NS_LOG_FUNCTION (this);
1396 }
1397 
1399 {
1400  NS_LOG_FUNCTION (this);
1401  if (next == count)
1402  {
1403  next = 0;
1404  }
1405  return data[next++];
1406 }
1407 
1409 {
1410  NS_LOG_FUNCTION (this);
1411  return new DeterministicVariableImpl (*this);
1412 }
1413 
1416 {
1417  NS_LOG_FUNCTION (this << d << c);
1418 }
1419 
1420 // -----------------------------------------------------------------------------
1421 // -----------------------------------------------------------------------------
1422 // LogNormalVariableImpl
1425 {
1426 public:
1431  LogNormalVariableImpl (double mu, double sigma);
1432 
1436  virtual double GetValue ();
1437  virtual RandomVariableBase* Copy (void) const;
1438 
1439 private:
1440  double m_mu;
1441  double m_sigma;
1442 };
1443 
1444 
1446 {
1447  NS_LOG_FUNCTION (this);
1448  return new LogNormalVariableImpl (*this);
1449 }
1450 
1452  : m_mu (mu),
1453  m_sigma (sigma)
1454 {
1455  NS_LOG_FUNCTION (this << mu << sigma);
1456 }
1457 
1458 // The code from this function was adapted from the GNU Scientific
1459 // Library 1.8:
1460 /* randist/lognormal.c
1461  *
1462  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
1463  *
1464  * This program is free software; you can redistribute it and/or modify
1465  * it under the terms of the GNU General Public License as published by
1466  * the Free Software Foundation; either version 2 of the License, or (at
1467  * your option) any later version.
1468  *
1469  * This program is distributed in the hope that it will be useful, but
1470  * WITHOUT ANY WARRANTY; without even the implied warranty of
1471  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1472  * General Public License for more details.
1473  *
1474  * You should have received a copy of the GNU General Public License
1475  * along with this program; if not, write to the Free Software
1476  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1477  */
1478 /* The lognormal distribution has the form
1479 
1480  p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
1481 
1482  for x > 0. Lognormal random numbers are the exponentials of
1483  gaussian random numbers */
1484 double
1486 {
1487  NS_LOG_FUNCTION (this);
1488  RngStream *generator = GetStream ();
1489  double u, v, r2, normal, z;
1490 
1491  do
1492  {
1493  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
1494 
1495  u = -1 + 2 * generator->RandU01 ();
1496  v = -1 + 2 * generator->RandU01 ();
1497 
1498  /* see if it is in the unit circle */
1499  r2 = u * u + v * v;
1500  }
1501  while (r2 > 1.0 || r2 == 0);
1502 
1503  normal = u * std::sqrt (-2.0 * std::log (r2) / r2);
1504 
1505  z = std::exp (m_sigma * normal + m_mu);
1506 
1507  return z;
1508 }
1509 
1510 LogNormalVariable::LogNormalVariable (double mu, double sigma)
1511  : RandomVariable (LogNormalVariableImpl (mu, sigma))
1512 {
1513  NS_LOG_FUNCTION (this << mu << sigma);
1514 }
1515 
1516 // -----------------------------------------------------------------------------
1517 // -----------------------------------------------------------------------------
1518 // GammaVariableImpl
1521 {
1522 public:
1527  GammaVariableImpl (double alpha, double beta);
1528 
1532  virtual double GetValue ();
1533 
1538  double GetValue (double alpha, double beta);
1539 
1540  virtual RandomVariableBase* Copy (void) const;
1541 
1542 private:
1543  double m_alpha;
1544  double m_beta;
1546 };
1547 
1548 
1550 {
1551  NS_LOG_FUNCTION (this);
1552  return new GammaVariableImpl (m_alpha, m_beta);
1553 }
1554 
1555 GammaVariableImpl::GammaVariableImpl (double alpha, double beta)
1556  : m_alpha (alpha),
1557  m_beta (beta)
1558 {
1559  NS_LOG_FUNCTION (this << alpha << beta);
1560 }
1561 
1562 double
1564 {
1565  NS_LOG_FUNCTION (this);
1566  return GetValue (m_alpha, m_beta);
1567 }
1568 
1569 /*
1570  The code for the following generator functions was adapted from ns-2
1571  tools/ranvar.cc
1572 
1573  Originally the algorithm was devised by Marsaglia in 2000:
1574  G. Marsaglia, W. W. Tsang: A simple method for gereating Gamma variables
1575  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
1576 
1577  The Gamma distribution density function has the form
1578 
1579  x^(alpha-1) * exp(-x/beta)
1580  p(x; alpha, beta) = ----------------------------
1581  beta^alpha * Gamma(alpha)
1582 
1583  for x > 0.
1584 */
1585 double
1586 GammaVariableImpl::GetValue (double alpha, double beta)
1587 {
1588  NS_LOG_FUNCTION (this << alpha << beta);
1589  RngStream *generator = GetStream ();
1590 
1591  if (alpha < 1)
1592  {
1593  double u = generator->RandU01 ();
1594  return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
1595  }
1596 
1597  double x, v, u;
1598  double d = alpha - 1.0 / 3.0;
1599  double c = (1.0 / 3.0) / std::sqrt (d);
1600 
1601  while (1)
1602  {
1603  do
1604  {
1605  x = m_normal.GetValue ();
1606  v = 1.0 + c * x;
1607  }
1608  while (v <= 0);
1609 
1610  v = v * v * v;
1611  u = generator->RandU01 ();
1612  if (u < 1 - 0.0331 * x * x * x * x)
1613  {
1614  break;
1615  }
1616  if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
1617  {
1618  break;
1619  }
1620  }
1621 
1622  return beta * d * v;
1623 }
1624 
1626  : RandomVariable (GammaVariableImpl (1.0, 1.0))
1627 {
1628  NS_LOG_FUNCTION (this);
1629 }
1630 
1631 GammaVariable::GammaVariable (double alpha, double beta)
1632  : RandomVariable (GammaVariableImpl (alpha, beta))
1633 {
1634  NS_LOG_FUNCTION (this << alpha << beta);
1635 }
1636 
1637 double GammaVariable::GetValue (void) const
1638 {
1639  NS_LOG_FUNCTION (this);
1640  return this->RandomVariable::GetValue ();
1641 }
1642 
1643 double GammaVariable::GetValue (double alpha, double beta) const
1644 {
1645  NS_LOG_FUNCTION (this << alpha << beta);
1646  return ((GammaVariableImpl*)Peek ())->GetValue (alpha, beta);
1647 }
1648 
1649 // -----------------------------------------------------------------------------
1650 // -----------------------------------------------------------------------------
1651 // ErlangVariableImpl
1652 
1655 {
1656 public:
1661  ErlangVariableImpl (unsigned int k, double lambda);
1662 
1666  virtual double GetValue ();
1667 
1672  double GetValue (unsigned int k, double lambda);
1673 
1674  virtual RandomVariableBase* Copy (void) const;
1675 
1676 private:
1677  unsigned int m_k;
1678  double m_lambda;
1679 };
1680 
1681 
1683 {
1684  NS_LOG_FUNCTION (this);
1685  return new ErlangVariableImpl (m_k, m_lambda);
1686 }
1687 
1688 ErlangVariableImpl::ErlangVariableImpl (unsigned int k, double lambda)
1689  : m_k (k),
1690  m_lambda (lambda)
1691 {
1692  NS_LOG_FUNCTION (this << k << lambda);
1693 }
1694 
1695 double
1697 {
1698  NS_LOG_FUNCTION (this);
1699  return GetValue (m_k, m_lambda);
1700 }
1701 
1702 /*
1703  The code for the following generator functions was adapted from ns-2
1704  tools/ranvar.cc
1705 
1706  The Erlang distribution density function has the form
1707 
1708  x^(k-1) * exp(-x/lambda)
1709  p(x; k, lambda) = ---------------------------
1710  lambda^k * (k-1)!
1711 
1712  for x > 0.
1713 */
1714 double
1715 ErlangVariableImpl::GetValue (unsigned int k, double lambda)
1716 {
1719  NS_LOG_FUNCTION (this << k << lambda);
1720  ExponentialVariable exponential (lambda);
1721 
1722  double result = 0;
1723  for (unsigned int i = 0; i < k; ++i)
1724  {
1725  result += exponential.GetValue ();
1726  }
1727 
1728  return result;
1729 }
1730 
1732  : RandomVariable (ErlangVariableImpl (1, 1.0))
1733 {
1734  NS_LOG_FUNCTION (this);
1735 }
1736 
1737 ErlangVariable::ErlangVariable (unsigned int k, double lambda)
1738  : RandomVariable (ErlangVariableImpl (k, lambda))
1739 {
1740  NS_LOG_FUNCTION (this << k << lambda);
1741 }
1742 
1743 double ErlangVariable::GetValue (void) const
1744 {
1745  NS_LOG_FUNCTION (this);
1746  return this->RandomVariable::GetValue ();
1747 }
1748 
1749 double ErlangVariable::GetValue (unsigned int k, double lambda) const
1750 {
1751  NS_LOG_FUNCTION (this << k << lambda);
1752  return ((ErlangVariableImpl*)Peek ())->GetValue (k, lambda);
1753 }
1754 
1755 // -----------------------------------------------------------------------------
1756 // -----------------------------------------------------------------------------
1757 // TriangularVariableImpl methods
1760 {
1761 public:
1767 
1775  TriangularVariableImpl (double s, double l, double mean);
1776 
1778 
1782  virtual double GetValue ();
1783  virtual RandomVariableBase* Copy (void) const;
1784 
1785 private:
1786  double m_min;
1787  double m_max;
1788  double m_mode; // easier to work with the mode internally instead of the mean
1789  // they are related by the simple: mean = (min+max+mode)/3
1790 };
1791 
1793  : m_min (0),
1794  m_max (1),
1795  m_mode (0.5)
1796 {
1797  NS_LOG_FUNCTION (this);
1798 }
1799 
1800 TriangularVariableImpl::TriangularVariableImpl (double s, double l, double mean)
1801  : m_min (s),
1802  m_max (l),
1803  m_mode (3.0 * mean - s - l)
1804 {
1805  NS_LOG_FUNCTION (this << s << l << mean);
1806 }
1807 
1809  : RandomVariableBase (c),
1810  m_min (c.m_min),
1811  m_max (c.m_max),
1812  m_mode (c.m_mode)
1813 {
1814  NS_LOG_FUNCTION (this << &c);
1815 }
1816 
1818 {
1819  NS_LOG_FUNCTION (this);
1820  RngStream *generator = GetStream ();
1821  double u = generator->RandU01 ();
1822  if (u <= (m_mode - m_min) / (m_max - m_min) )
1823  {
1824  return m_min + std::sqrt (u * (m_max - m_min) * (m_mode - m_min) );
1825  }
1826  else
1827  {
1828  return m_max - std::sqrt ( (1 - u) * (m_max - m_min) * (m_max - m_mode) );
1829  }
1830 }
1831 
1833 {
1834  NS_LOG_FUNCTION (this);
1835  return new TriangularVariableImpl (*this);
1836 }
1837 
1840 {
1841  NS_LOG_FUNCTION (this);
1842 }
1843 TriangularVariable::TriangularVariable (double s, double l, double mean)
1844  : RandomVariable (TriangularVariableImpl (s,l,mean))
1845 {
1846  NS_LOG_FUNCTION (this << s << l << mean);
1847 }
1848 
1849 // -----------------------------------------------------------------------------
1850 // -----------------------------------------------------------------------------
1851 // ZipfVariableImpl
1854 {
1855 public:
1860  ZipfVariableImpl (long n, double alpha);
1861 
1865  ZipfVariableImpl ();
1866 
1870  virtual double GetValue ();
1871  virtual RandomVariableBase* Copy (void) const;
1872 
1873 private:
1874  long m_n;
1875  double m_alpha;
1876  double m_c; // the normalization constant
1877 };
1878 
1879 
1881 {
1882  NS_LOG_FUNCTION (this);
1883  return new ZipfVariableImpl (m_n, m_alpha);
1884 }
1885 
1887  : m_n (1),
1888  m_alpha (0),
1889  m_c (1)
1890 {
1891  NS_LOG_FUNCTION (this);
1892 }
1893 
1894 
1896  : m_n (n),
1897  m_alpha (alpha),
1898  m_c (0)
1899 {
1900  // calculate the normalization constant c
1901  NS_LOG_FUNCTION (this << n << alpha);
1902  for (int i = 1; i <= n; i++)
1903  {
1904  m_c += (1.0 / std::pow ((double)i, alpha));
1905  }
1906  m_c = 1.0 / m_c;
1907 }
1908 
1909 double
1911 {
1912  NS_LOG_FUNCTION (this);
1913  RngStream *generator = GetStream ();
1914 
1915  double u = generator->RandU01 ();
1916  double sum_prob = 0,zipf_value = 0;
1917  for (int i = 1; i <= m_n; i++)
1918  {
1919  sum_prob += m_c / std::pow ((double)i, m_alpha);
1920  if (sum_prob > u)
1921  {
1922  zipf_value = i;
1923  break;
1924  }
1925  }
1926  return zipf_value;
1927 }
1928 
1931 {
1932  NS_LOG_FUNCTION (this);
1933 }
1934 
1935 ZipfVariable::ZipfVariable (long n, double alpha)
1936  : RandomVariable (ZipfVariableImpl (n, alpha))
1937 {
1938  NS_LOG_FUNCTION (this << n << alpha);
1939 }
1940 
1941 
1942 // -----------------------------------------------------------------------------
1943 // -----------------------------------------------------------------------------
1944 // ZetaVariableImpl
1947 {
1948 public:
1952  ZetaVariableImpl (double alpha);
1953 
1957  ZetaVariableImpl ();
1958 
1962  virtual double GetValue ();
1963  virtual RandomVariableBase* Copy (void) const;
1964 
1965 private:
1966  double m_alpha;
1967  double m_b; // just for calculus simplifications
1968 };
1969 
1970 
1972 {
1973  NS_LOG_FUNCTION (this);
1974  return new ZetaVariableImpl (m_alpha);
1975 }
1976 
1978  : m_alpha (3.14),
1979  m_b (std::pow (2.0, 2.14))
1980 {
1981  NS_LOG_FUNCTION (this);
1982 }
1983 
1984 
1986  : m_alpha (alpha),
1987  m_b (std::pow (2.0, alpha - 1.0))
1988 {
1989  NS_LOG_FUNCTION (this << alpha);
1990 }
1991 
1992 /*
1993  The code for the following generator functions is borrowed from:
1994  L. Devroye: Non-Uniform Random Variate Generation, Springer-Verlag, New York, 1986.
1995  page 551
1996  */
1997 double
1999 {
2000  NS_LOG_FUNCTION (this);
2001  RngStream *generator = GetStream ();
2002 
2003  double u, v;
2004  double X, T;
2005  double test;
2006 
2007  do
2008  {
2009  u = generator->RandU01 ();
2010  v = generator->RandU01 ();
2011  X = floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
2012  T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
2013  test = v * X * (T - 1.0) / (m_b - 1.0);
2014  }
2015  while ( test > (T / m_b) );
2016 
2017  return X;
2018 }
2019 
2022 {
2023  NS_LOG_FUNCTION (this);
2024 }
2025 
2027  : RandomVariable (ZetaVariableImpl (alpha))
2028 {
2029  NS_LOG_FUNCTION (this << alpha);
2030 }
2031 
2032 
2033 std::ostream & operator << (std::ostream &os, const RandomVariable &var)
2034 {
2035  RandomVariableBase *base = var.Peek ();
2036  ConstantVariableImpl *constant = dynamic_cast<ConstantVariableImpl *> (base);
2037  if (constant != 0)
2038  {
2039  os << "Constant:" << constant->GetValue ();
2040  return os;
2041  }
2042  UniformVariableImpl *uniform = dynamic_cast<UniformVariableImpl *> (base);
2043  if (uniform != 0)
2044  {
2045  os << "Uniform:" << uniform->GetMin () << ":" << uniform->GetMax ();
2046  return os;
2047  }
2048  NormalVariableImpl *normal = dynamic_cast<NormalVariableImpl *> (base);
2049  if (normal != 0)
2050  {
2051  os << "Normal:" << normal->GetMean () << ":" << normal->GetVariance ();
2052  double bound = normal->GetBound ();
2054  {
2055  os << ":" << bound;
2056  }
2057  return os;
2058  }
2060  os.setstate (std::ios_base::badbit);
2061  return os;
2062 }
2063 std::istream & operator >> (std::istream &is, RandomVariable &var)
2064 {
2065  std::string value;
2066  is >> value;
2067  std::string::size_type tmp;
2068  tmp = value.find (":");
2069  if (tmp == std::string::npos)
2070  {
2071  is.setstate (std::ios_base::badbit);
2072  return is;
2073  }
2074  std::string type = value.substr (0, tmp);
2075  value = value.substr (tmp + 1, value.npos);
2076  if (type == "Constant")
2077  {
2078  std::istringstream iss (value);
2079  double constant;
2080  iss >> constant;
2081  var = ConstantVariable (constant);
2082  }
2083  else if (type == "Uniform")
2084  {
2085  if (value.size () == 0)
2086  {
2087  var = UniformVariable ();
2088  }
2089  else
2090  {
2091  tmp = value.find (":");
2092  if (tmp == value.npos)
2093  {
2094  NS_FATAL_ERROR ("bad Uniform value: " << value);
2095  }
2096  std::istringstream issA (value.substr (0, tmp));
2097  std::istringstream issB (value.substr (tmp + 1, value.npos));
2098  double a, b;
2099  issA >> a;
2100  issB >> b;
2101  var = UniformVariable (a, b);
2102  }
2103  }
2104  else if (type == "Normal")
2105  {
2106  if (value.size () == 0)
2107  {
2108  var = NormalVariable ();
2109  }
2110  else
2111  {
2112  tmp = value.find (":");
2113  if (tmp == value.npos)
2114  {
2115  NS_FATAL_ERROR ("bad Normal value: " << value);
2116  }
2117  std::string::size_type tmp2;
2118  std::string sub = value.substr (tmp + 1, value.npos);
2119  tmp2 = sub.find (":");
2120  if (tmp2 == value.npos)
2121  {
2122  std::istringstream issA (value.substr (0, tmp));
2123  std::istringstream issB (sub);
2124  double a, b;
2125  issA >> a;
2126  issB >> b;
2127  var = NormalVariable (a, b);
2128  }
2129  else
2130  {
2131  std::istringstream issA (value.substr (0, tmp));
2132  std::istringstream issB (sub.substr (0, tmp2));
2133  std::istringstream issC (sub.substr (tmp2 + 1, value.npos));
2134  double a, b, c;
2135  issA >> a;
2136  issB >> b;
2137  issC >> c;
2138  var = NormalVariable (a, b, c);
2139  }
2140  }
2141  }
2142  else
2143  {
2144  NS_FATAL_ERROR ("RandomVariable deserialization not implemented for " << type);
2146  }
2147  return is;
2148 }
2149 
2150 } // 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)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
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...
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)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
virtual RandomVariableBase * Copy(void) const
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
virtual RandomVariableBase * Copy() const
virtual RandomVariableBase * Copy(void) const
virtual RandomVariableBase * Copy(void) const
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
virtual double GetValue()
ATTRIBUTE_CHECKER_IMPLEMENT(Callback)
Attribute checker.
virtual RandomVariableBase * Copy(void) const
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()
A 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()
A 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:388
ParetoVariable()
Constructs a pareto random variable with a mean of 1 and a shape parameter of 1.5.