A Discrete-Event Network Simulator
API
random-variable-stream.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 Georgia Tech Research Corporation
4  * Copyright (c) 2011 Mathieu Lacage
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: Rajib Bhattacharjea<raj.b@gatech.edu>
20  * Hadi Arbabi<marbabi@cs.odu.edu>
21  * Mathieu Lacage <mathieu.lacage@gmail.com>
22  *
23  * Modified by Mitch Watrous <watrous@u.washington.edu>
24  *
25  */
26 #include "random-variable-stream.h"
27 #include "assert.h"
28 #include "boolean.h"
29 #include "double.h"
30 #include "integer.h"
31 #include "string.h"
32 #include "pointer.h"
33 #include "log.h"
34 #include "rng-stream.h"
35 #include "rng-seed-manager.h"
36 #include <cmath>
37 #include <iostream>
38 
45 namespace ns3 {
46 
47 NS_LOG_COMPONENT_DEFINE ("RandomVariableStream");
48 
49 NS_OBJECT_ENSURE_REGISTERED (RandomVariableStream);
50 
51 TypeId
53 {
54  static TypeId tid = TypeId ("ns3::RandomVariableStream")
55  .SetParent<Object> ()
56  .SetGroupName ("Core")
57  .AddAttribute("Stream",
58  "The stream number for this RNG stream. -1 means \"allocate a stream automatically\". "
59  "Note that if -1 is set, Get will return -1 so that it is not possible to know which "
60  "value was automatically allocated.",
61  IntegerValue(-1),
64  MakeIntegerChecker<int64_t>())
65  .AddAttribute("Antithetic", "Set this RNG stream to generate antithetic values",
66  BooleanValue (false),
70  ;
71  return tid;
72 }
73 
75  : m_rng (0)
76 {
77  NS_LOG_FUNCTION (this);
78 }
80 {
81  NS_LOG_FUNCTION (this);
82  delete m_rng;
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this << isAntithetic);
89  m_isAntithetic = isAntithetic;
90 }
91 bool
93 {
94  NS_LOG_FUNCTION (this);
95  return m_isAntithetic;
96 }
97 void
99 {
100  NS_LOG_FUNCTION (this << stream);
101  // negative values are not legal.
102  NS_ASSERT (stream >= -1);
103  delete m_rng;
104  if (stream == -1)
105  {
106  // The first 2^63 streams are reserved for automatic stream
107  // number assignment.
108  uint64_t nextStream = RngSeedManager::GetNextStreamIndex ();
109  NS_ASSERT(nextStream <= ((1ULL)<<63));
111  nextStream,
113  }
114  else
115  {
116  // The last 2^63 streams are reserved for deterministic stream
117  // number assignment.
118  uint64_t base = ((1ULL)<<63);
119  uint64_t target = base + stream;
121  target,
123  }
124  m_stream = stream;
125 }
126 int64_t
128 {
129  NS_LOG_FUNCTION (this);
130  return m_stream;
131 }
132 
133 RngStream *
135 {
136  NS_LOG_FUNCTION (this);
137  return m_rng;
138 }
139 
141 
142 TypeId
144 {
145  static TypeId tid = TypeId ("ns3::UniformRandomVariable")
147  .SetGroupName ("Core")
148  .AddConstructor<UniformRandomVariable> ()
149  .AddAttribute("Min", "The lower bound on the values returned by this RNG stream.",
150  DoubleValue(0),
151  MakeDoubleAccessor(&UniformRandomVariable::m_min),
152  MakeDoubleChecker<double>())
153  .AddAttribute("Max", "The upper bound on the values returned by this RNG stream.",
154  DoubleValue(1.0),
155  MakeDoubleAccessor(&UniformRandomVariable::m_max),
156  MakeDoubleChecker<double>())
157  ;
158  return tid;
159 }
161 {
162  // m_min and m_max are initialized after constructor by attributes
163  NS_LOG_FUNCTION (this);
164 }
165 
166 double
168 {
169  NS_LOG_FUNCTION (this);
170  return m_min;
171 }
172 double
174 {
175  NS_LOG_FUNCTION (this);
176  return m_max;
177 }
178 
179 double
181 {
182  NS_LOG_FUNCTION (this << min << max);
183  double v = min + Peek ()->RandU01 () * (max - min);
184  if (IsAntithetic ())
185  {
186  v = min + (max - v);
187  }
188  return v;
189 }
190 uint32_t
191 UniformRandomVariable::GetInteger (uint32_t min, uint32_t max)
192 {
193  NS_LOG_FUNCTION (this << min << max);
194  NS_ASSERT (min <= max);
195  return static_cast<uint32_t> ( GetValue ((double) (min), (double) (max) + 1.0) );
196 }
197 
198 double
200 {
201  NS_LOG_FUNCTION (this);
202  return GetValue (m_min, m_max);
203 }
204 uint32_t
206 {
207  NS_LOG_FUNCTION (this);
208  return (uint32_t)GetValue (m_min, m_max + 1);
209 }
210 
211 NS_OBJECT_ENSURE_REGISTERED(ConstantRandomVariable);
212 
213 TypeId
215 {
216  static TypeId tid = TypeId ("ns3::ConstantRandomVariable")
218  .SetGroupName ("Core")
219  .AddConstructor<ConstantRandomVariable> ()
220  .AddAttribute("Constant", "The constant value returned by this RNG stream.",
221  DoubleValue(0),
223  MakeDoubleChecker<double>())
224  ;
225  return tid;
226 }
228 {
229  // m_constant is initialized after constructor by attributes
230  NS_LOG_FUNCTION (this);
231 }
232 
233 double
235 {
236  NS_LOG_FUNCTION (this);
237  return m_constant;
238 }
239 
240 double
242 {
243  NS_LOG_FUNCTION (this << constant);
244  return constant;
245 }
246 uint32_t
248 {
249  NS_LOG_FUNCTION (this << constant);
250  return constant;
251 }
252 
253 double
255 {
256  NS_LOG_FUNCTION (this);
257  return GetValue (m_constant);
258 }
259 uint32_t
261 {
262  NS_LOG_FUNCTION (this);
263  return (uint32_t)GetValue (m_constant);
264 }
265 
267 
268 TypeId
270 {
271  static TypeId tid = TypeId ("ns3::SequentialRandomVariable")
273  .SetGroupName ("Core")
274  .AddConstructor<SequentialRandomVariable> ()
275  .AddAttribute("Min", "The first value of the sequence.",
276  DoubleValue(0),
278  MakeDoubleChecker<double>())
279  .AddAttribute("Max", "One more than the last value of the sequence.",
280  DoubleValue(0),
282  MakeDoubleChecker<double>())
283  .AddAttribute("Increment", "The sequence random variable increment.",
284  StringValue("ns3::ConstantRandomVariable[Constant=1]"),
286  MakePointerChecker<RandomVariableStream> ())
287  .AddAttribute("Consecutive", "The number of times each member of the sequence is repeated.",
288  IntegerValue(1),
290  MakeIntegerChecker<uint32_t>());
291  ;
292  return tid;
293 }
295  :
296  m_current (0),
297  m_currentConsecutive (0),
298  m_isCurrentSet (false)
299 {
300  // m_min, m_max, m_increment, and m_consecutive are initialized
301  // after constructor by attributes.
302  NS_LOG_FUNCTION (this);
303 }
304 
305 double
307 {
308  NS_LOG_FUNCTION (this);
309  return m_min;
310 }
311 
312 double
314 {
315  NS_LOG_FUNCTION (this);
316  return m_max;
317 }
318 
321 {
322  NS_LOG_FUNCTION (this);
323  return m_increment;
324 }
325 
326 uint32_t
328 {
329  NS_LOG_FUNCTION (this);
330  return m_consecutive;
331 }
332 
333 double
335 {
336  // Set the current sequence value if it hasn't been set.
337  NS_LOG_FUNCTION (this);
338  if (!m_isCurrentSet)
339  {
340  // Start the sequence at its minimium value.
341  m_current = m_min;
342  m_isCurrentSet = true;
343  }
344 
345  // Return a sequential series of values
346  double r = m_current;
348  { // Time to advance to next
351  if (m_current >= m_max)
352  {
353  m_current = m_min + (m_current - m_max);
354  }
355  }
356  return r;
357 }
358 
359 uint32_t
361 {
362  NS_LOG_FUNCTION (this);
363  return (uint32_t)GetValue ();
364 }
365 
367 
368 TypeId
370 {
371  static TypeId tid = TypeId ("ns3::ExponentialRandomVariable")
373  .SetGroupName ("Core")
374  .AddConstructor<ExponentialRandomVariable> ()
375  .AddAttribute("Mean", "The mean of the values returned by this RNG stream.",
376  DoubleValue(1.0),
378  MakeDoubleChecker<double>())
379  .AddAttribute("Bound", "The upper bound on the values returned by this RNG stream.",
380  DoubleValue(0.0),
382  MakeDoubleChecker<double>())
383  ;
384  return tid;
385 }
387 {
388  // m_mean and m_bound are initialized after constructor by attributes
389  NS_LOG_FUNCTION (this);
390 }
391 
392 double
394 {
395  NS_LOG_FUNCTION (this);
396  return m_mean;
397 }
398 double
400 {
401  NS_LOG_FUNCTION (this);
402  return m_bound;
403 }
404 
405 double
406 ExponentialRandomVariable::GetValue (double mean, double bound)
407 {
408  NS_LOG_FUNCTION (this << mean << bound);
409  while (1)
410  {
411  // Get a uniform random variable in [0,1].
412  double v = Peek ()->RandU01 ();
413  if (IsAntithetic ())
414  {
415  v = (1 - v);
416  }
417 
418  // Calculate the exponential random variable.
419  double r = -mean*std::log (v);
420 
421  // Use this value if it's acceptable.
422  if (bound == 0 || r <= bound)
423  {
424  return r;
425  }
426  }
427 }
428 uint32_t
429 ExponentialRandomVariable::GetInteger (uint32_t mean, uint32_t bound)
430 {
431  NS_LOG_FUNCTION (this << mean << bound);
432  return static_cast<uint32_t> ( GetValue (mean, bound) );
433 }
434 
435 double
437 {
438  NS_LOG_FUNCTION (this);
439  return GetValue (m_mean, m_bound);
440 }
441 uint32_t
443 {
444  NS_LOG_FUNCTION (this);
445  return (uint32_t)GetValue (m_mean, m_bound);
446 }
447 
449 
450 TypeId
452 {
453  static TypeId tid = TypeId ("ns3::ParetoRandomVariable")
455  .SetGroupName ("Core")
456  .AddConstructor<ParetoRandomVariable> ()
457  .AddAttribute("Mean", "The mean parameter for the Pareto distribution returned by this RNG stream.",
458  DoubleValue(1.0),
460  MakeDoubleChecker<double>())
461  .AddAttribute("Shape", "The shape parameter for the Pareto distribution returned by this RNG stream.",
462  DoubleValue(2.0),
464  MakeDoubleChecker<double>())
465  .AddAttribute("Bound", "The upper bound on the values returned by this RNG stream (if non-zero).",
466  DoubleValue(0.0),
468  MakeDoubleChecker<double>())
469  ;
470  return tid;
471 }
473 {
474  // m_mean, m_shape, and m_bound are initialized after constructor
475  // by attributes
476  NS_LOG_FUNCTION (this);
477 }
478 
479 double
481 {
482  NS_LOG_FUNCTION (this);
483  return m_mean;
484 }
485 double
487 {
488  NS_LOG_FUNCTION (this);
489  return m_shape;
490 }
491 double
493 {
494  NS_LOG_FUNCTION (this);
495  return m_bound;
496 }
497 
498 double
499 ParetoRandomVariable::GetValue (double mean, double shape, double bound)
500 {
501  // Calculate the scale parameter.
502  NS_LOG_FUNCTION (this << mean << shape << bound);
503  double scale = mean * (shape - 1.0) / shape;
504 
505  while (1)
506  {
507  // Get a uniform random variable in [0,1].
508  double v = Peek ()->RandU01 ();
509  if (IsAntithetic ())
510  {
511  v = (1 - v);
512  }
513 
514  // Calculate the Pareto random variable.
515  double r = (scale * ( 1.0 / std::pow (v, 1.0 / shape)));
516 
517  // Use this value if it's acceptable.
518  if (bound == 0 || r <= bound)
519  {
520  return r;
521  }
522  }
523 }
524 uint32_t
525 ParetoRandomVariable::GetInteger (uint32_t mean, uint32_t shape, uint32_t bound)
526 {
527  NS_LOG_FUNCTION (this << mean << shape << bound);
528  return static_cast<uint32_t> ( GetValue (mean, shape, bound) );
529 }
530 
531 double
533 {
534  NS_LOG_FUNCTION (this);
535  return GetValue (m_mean, m_shape, m_bound);
536 }
537 uint32_t
539 {
540  NS_LOG_FUNCTION (this);
541  return (uint32_t)GetValue (m_mean, m_shape, m_bound);
542 }
543 
545 
546 TypeId
548 {
549  static TypeId tid = TypeId ("ns3::WeibullRandomVariable")
551  .SetGroupName ("Core")
552  .AddConstructor<WeibullRandomVariable> ()
553  .AddAttribute("Scale", "The scale parameter for the Weibull distribution returned by this RNG stream.",
554  DoubleValue(1.0),
556  MakeDoubleChecker<double>())
557  .AddAttribute("Shape", "The shape parameter for the Weibull distribution returned by this RNG stream.",
558  DoubleValue(1),
560  MakeDoubleChecker<double>())
561  .AddAttribute("Bound", "The upper bound on the values returned by this RNG stream.",
562  DoubleValue(0.0),
564  MakeDoubleChecker<double>())
565  ;
566  return tid;
567 }
569 {
570  // m_scale, m_shape, and m_bound are initialized after constructor
571  // by attributes
572  NS_LOG_FUNCTION (this);
573 }
574 
575 double
577 {
578  NS_LOG_FUNCTION (this);
579  return m_scale;
580 }
581 double
583 {
584  NS_LOG_FUNCTION (this);
585  return m_shape;
586 }
587 double
589 {
590  NS_LOG_FUNCTION (this);
591  return m_bound;
592 }
593 
594 double
595 WeibullRandomVariable::GetValue (double scale, double shape, double bound)
596 {
597  NS_LOG_FUNCTION (this << scale << shape << bound);
598  double exponent = 1.0 / shape;
599  while (1)
600  {
601  // Get a uniform random variable in [0,1].
602  double v = Peek ()->RandU01 ();
603  if (IsAntithetic ())
604  {
605  v = (1 - v);
606  }
607 
608  // Calculate the Weibull random variable.
609  double r = scale * std::pow ( -std::log (v), exponent);
610 
611  // Use this value if it's acceptable.
612  if (bound == 0 || r <= bound)
613  {
614  return r;
615  }
616  }
617 }
618 uint32_t
619 WeibullRandomVariable::GetInteger (uint32_t scale, uint32_t shape, uint32_t bound)
620 {
621  NS_LOG_FUNCTION (this << scale << shape << bound);
622  return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
623 }
624 
625 double
627 {
628  NS_LOG_FUNCTION (this);
629  return GetValue (m_scale, m_shape, m_bound);
630 }
631 uint32_t
633 {
634  NS_LOG_FUNCTION (this);
635  return (uint32_t)GetValue (m_scale, m_shape, m_bound);
636 }
637 
639 
640 const double NormalRandomVariable::INFINITE_VALUE = 1e307;
641 
642 TypeId
644 {
645  static TypeId tid = TypeId ("ns3::NormalRandomVariable")
647  .SetGroupName ("Core")
648  .AddConstructor<NormalRandomVariable> ()
649  .AddAttribute("Mean", "The mean value for the normal distribution returned by this RNG stream.",
650  DoubleValue(0.0),
652  MakeDoubleChecker<double>())
653  .AddAttribute("Variance", "The variance value for the normal distribution returned by this RNG stream.",
654  DoubleValue(1.0),
656  MakeDoubleChecker<double>())
657  .AddAttribute("Bound", "The bound on the values returned by this RNG stream.",
660  MakeDoubleChecker<double>())
661  ;
662  return tid;
663 }
665  :
666  m_nextValid (false)
667 {
668  // m_mean, m_variance, and m_bound are initialized after constructor
669  // by attributes
670  NS_LOG_FUNCTION (this);
671 }
672 
673 double
675 {
676  NS_LOG_FUNCTION (this);
677  return m_mean;
678 }
679 double
681 {
682  NS_LOG_FUNCTION (this);
683  return m_variance;
684 }
685 double
687 {
688  NS_LOG_FUNCTION (this);
689  return m_bound;
690 }
691 
692 double
693 NormalRandomVariable::GetValue (double mean, double variance, double bound)
694 {
695  NS_LOG_FUNCTION (this << mean << variance << bound);
696  if (m_nextValid)
697  { // use previously generated
698  m_nextValid = false;
699  return m_next;
700  }
701  while (1)
702  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
703  // for algorithm; basically a Box-Muller transform:
704  // http://en.wikipedia.org/wiki/Box-Muller_transform
705  double u1 = Peek ()->RandU01 ();
706  double u2 = Peek ()->RandU01 ();
707  if (IsAntithetic ())
708  {
709  u1 = (1 - u1);
710  u2 = (1 - u2);
711  }
712  double v1 = 2 * u1 - 1;
713  double v2 = 2 * u2 - 1;
714  double w = v1 * v1 + v2 * v2;
715  if (w <= 1.0)
716  { // Got good pair
717  double y = std::sqrt ((-2 * std::log (w)) / w);
718  m_next = mean + v2 * y * std::sqrt (variance);
719  // if next is in bounds, it is valid
720  m_nextValid = std::fabs (m_next - mean) <= bound;
721  double x1 = mean + v1 * y * std::sqrt (variance);
722  // if x1 is in bounds, return it
723  if (std::fabs (x1 - mean) <= bound)
724  {
725  return x1;
726  }
727  // otherwise try and return m_next if it is valid
728  else if (m_nextValid)
729  {
730  m_nextValid = false;
731  return m_next;
732  }
733  // otherwise, just run this loop again
734  }
735  }
736 }
737 
738 uint32_t
739 NormalRandomVariable::GetInteger (uint32_t mean, uint32_t variance, uint32_t bound)
740 {
741  NS_LOG_FUNCTION (this << mean << variance << bound);
742  return static_cast<uint32_t> ( GetValue (mean, variance, bound) );
743 }
744 
745 double
747 {
748  NS_LOG_FUNCTION (this);
749  return GetValue (m_mean, m_variance, m_bound);
750 }
751 uint32_t
753 {
754  NS_LOG_FUNCTION (this);
755  return (uint32_t)GetValue (m_mean, m_variance, m_bound);
756 }
757 
759 
760 TypeId
762 {
763  static TypeId tid = TypeId ("ns3::LogNormalRandomVariable")
765  .SetGroupName ("Core")
766  .AddConstructor<LogNormalRandomVariable> ()
767  .AddAttribute("Mu", "The mu value for the log-normal distribution returned by this RNG stream.",
768  DoubleValue(0.0),
770  MakeDoubleChecker<double>())
771  .AddAttribute("Sigma", "The sigma value for the log-normal distribution returned by this RNG stream.",
772  DoubleValue(1.0),
774  MakeDoubleChecker<double>())
775  ;
776  return tid;
777 }
779 {
780  // m_mu and m_sigma are initialized after constructor by
781  // attributes
782  NS_LOG_FUNCTION (this);
783 }
784 
785 double
787 {
788  NS_LOG_FUNCTION (this);
789  return m_mu;
790 }
791 double
793 {
794  NS_LOG_FUNCTION (this);
795  return m_sigma;
796 }
797 
798 // The code from this function was adapted from the GNU Scientific
799 // Library 1.8:
800 /* randist/lognormal.c
801  *
802  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
803  *
804  * This program is free software; you can redistribute it and/or modify
805  * it under the terms of the GNU General Public License as published by
806  * the Free Software Foundation; either version 2 of the License, or (at
807  * your option) any later version.
808  *
809  * This program is distributed in the hope that it will be useful, but
810  * WITHOUT ANY WARRANTY; without even the implied warranty of
811  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
812  * General Public License for more details.
813  *
814  * You should have received a copy of the GNU General Public License
815  * along with this program; if not, write to the Free Software
816  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
817  */
818 /* The lognormal distribution has the form
819 
820  p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
821 
822  for x > 0. Lognormal random numbers are the exponentials of
823  gaussian random numbers */
824 double
825 LogNormalRandomVariable::GetValue (double mu, double sigma)
826 {
827  double v1, v2, r2, normal, x;
828 
829  NS_LOG_FUNCTION (this << mu << sigma);
830 
831  do
832  {
833  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
834 
835  double u1 = Peek ()->RandU01 ();
836  double u2 = Peek ()->RandU01 ();
837  if (IsAntithetic ())
838  {
839  u1 = (1 - u1);
840  u2 = (1 - u2);
841  }
842 
843  v1 = -1 + 2 * u1;
844  v2 = -1 + 2 * u2;
845 
846  /* see if it is in the unit circle */
847  r2 = v1 * v1 + v2 * v2;
848  }
849  while (r2 > 1.0 || r2 == 0);
850 
851  normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
852 
853  x = std::exp (sigma * normal + mu);
854 
855  return x;
856 }
857 
858 uint32_t
859 LogNormalRandomVariable::GetInteger (uint32_t mu, uint32_t sigma)
860 {
861  NS_LOG_FUNCTION (this << mu << sigma);
862  return static_cast<uint32_t> ( GetValue (mu, sigma));
863 }
864 
865 double
867 {
868  NS_LOG_FUNCTION (this);
869  return GetValue (m_mu, m_sigma);
870 }
871 uint32_t
873 {
874  NS_LOG_FUNCTION (this);
875  return (uint32_t)GetValue (m_mu, m_sigma);
876 }
877 
879 
880 TypeId
882 {
883  static TypeId tid = TypeId ("ns3::GammaRandomVariable")
885  .SetGroupName ("Core")
886  .AddConstructor<GammaRandomVariable> ()
887  .AddAttribute("Alpha", "The alpha value for the gamma distribution returned by this RNG stream.",
888  DoubleValue(1.0),
890  MakeDoubleChecker<double>())
891  .AddAttribute("Beta", "The beta value for the gamma distribution returned by this RNG stream.",
892  DoubleValue(1.0),
894  MakeDoubleChecker<double>())
895  ;
896  return tid;
897 }
899  :
900  m_nextValid (false)
901 {
902  // m_alpha and m_beta are initialized after constructor by
903  // attributes
904  NS_LOG_FUNCTION (this);
905 }
906 
907 double
909 {
910  NS_LOG_FUNCTION (this);
911  return m_alpha;
912 }
913 double
915 {
916  NS_LOG_FUNCTION (this);
917  return m_beta;
918 }
919 
920 /*
921  The code for the following generator functions was adapted from ns-2
922  tools/ranvar.cc
923 
924  Originally the algorithm was devised by Marsaglia in 2000:
925  G. Marsaglia, W. W. Tsang: A simple method for gereating Gamma variables
926  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
927 
928  The Gamma distribution density function has the form
929 
930  x^(alpha-1) * exp(-x/beta)
931  p(x; alpha, beta) = ----------------------------
932  beta^alpha * Gamma(alpha)
933 
934  for x > 0.
935 */
936 double
937 GammaRandomVariable::GetValue (double alpha, double beta)
938 {
939  NS_LOG_FUNCTION (this << alpha << beta);
940  if (alpha < 1)
941  {
942  double u = Peek ()->RandU01 ();
943  if (IsAntithetic ())
944  {
945  u = (1 - u);
946  }
947  return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
948  }
949 
950  double x, v, u;
951  double d = alpha - 1.0 / 3.0;
952  double c = (1.0 / 3.0) / std::sqrt (d);
953 
954  while (1)
955  {
956  do
957  {
958  // Get a value from a normal distribution that has mean
959  // zero, variance 1, and no bound.
960  double mean = 0.0;
961  double variance = 1.0;
963  x = GetNormalValue (mean, variance, bound);
964 
965  v = 1.0 + c * x;
966  }
967  while (v <= 0);
968 
969  v = v * v * v;
970  u = Peek ()->RandU01 ();
971  if (IsAntithetic ())
972  {
973  u = (1 - u);
974  }
975  if (u < 1 - 0.0331 * x * x * x * x)
976  {
977  break;
978  }
979  if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
980  {
981  break;
982  }
983  }
984 
985  return beta * d * v;
986 }
987 
988 uint32_t
989 GammaRandomVariable::GetInteger (uint32_t alpha, uint32_t beta)
990 {
991  NS_LOG_FUNCTION (this << alpha << beta);
992  return static_cast<uint32_t> ( GetValue (alpha, beta));
993 }
994 
995 double
997 {
998  NS_LOG_FUNCTION (this);
999  return GetValue (m_alpha, m_beta);
1000 }
1001 uint32_t
1003 {
1004  NS_LOG_FUNCTION (this);
1005  return (uint32_t)GetValue (m_alpha, m_beta);
1006 }
1007 
1008 double
1009 GammaRandomVariable::GetNormalValue (double mean, double variance, double bound)
1010 {
1011  NS_LOG_FUNCTION (this << mean << variance << bound);
1012  if (m_nextValid)
1013  { // use previously generated
1014  m_nextValid = false;
1015  return m_next;
1016  }
1017  while (1)
1018  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1019  // for algorithm; basically a Box-Muller transform:
1020  // http://en.wikipedia.org/wiki/Box-Muller_transform
1021  double u1 = Peek ()->RandU01 ();
1022  double u2 = Peek ()->RandU01 ();
1023  if (IsAntithetic ())
1024  {
1025  u1 = (1 - u1);
1026  u2 = (1 - u2);
1027  }
1028  double v1 = 2 * u1 - 1;
1029  double v2 = 2 * u2 - 1;
1030  double w = v1 * v1 + v2 * v2;
1031  if (w <= 1.0)
1032  { // Got good pair
1033  double y = std::sqrt ((-2 * std::log (w)) / w);
1034  m_next = mean + v2 * y * std::sqrt (variance);
1035  // if next is in bounds, it is valid
1036  m_nextValid = std::fabs (m_next - mean) <= bound;
1037  double x1 = mean + v1 * y * std::sqrt (variance);
1038  // if x1 is in bounds, return it
1039  if (std::fabs (x1 - mean) <= bound)
1040  {
1041  return x1;
1042  }
1043  // otherwise try and return m_next if it is valid
1044  else if (m_nextValid)
1045  {
1046  m_nextValid = false;
1047  return m_next;
1048  }
1049  // otherwise, just run this loop again
1050  }
1051  }
1052 }
1053 
1055 
1056 TypeId
1058 {
1059  static TypeId tid = TypeId ("ns3::ErlangRandomVariable")
1061  .SetGroupName ("Core")
1062  .AddConstructor<ErlangRandomVariable> ()
1063  .AddAttribute("K", "The k value for the Erlang distribution returned by this RNG stream.",
1064  IntegerValue(1),
1066  MakeIntegerChecker<uint32_t>())
1067  .AddAttribute("Lambda", "The lambda value for the Erlang distribution returned by this RNG stream.",
1068  DoubleValue(1.0),
1070  MakeDoubleChecker<double>())
1071  ;
1072  return tid;
1073 }
1075 {
1076  // m_k and m_lambda are initialized after constructor by attributes
1077  NS_LOG_FUNCTION (this);
1078 }
1079 
1080 uint32_t
1082 {
1083  NS_LOG_FUNCTION (this);
1084  return m_k;
1085 }
1086 double
1088 {
1089  NS_LOG_FUNCTION (this);
1090  return m_lambda;
1091 }
1092 
1093 /*
1094  The code for the following generator functions was adapted from ns-2
1095  tools/ranvar.cc
1096 
1097  The Erlang distribution density function has the form
1098 
1099  x^(k-1) * exp(-x/lambda)
1100  p(x; k, lambda) = ---------------------------
1101  lambda^k * (k-1)!
1102 
1103  for x > 0.
1104 */
1105 double
1106 ErlangRandomVariable::GetValue (uint32_t k, double lambda)
1107 {
1108  NS_LOG_FUNCTION (this << k << lambda);
1109  double mean = lambda;
1110  double bound = 0.0;
1111 
1112  double result = 0;
1113  for (unsigned int i = 0; i < k; ++i)
1114  {
1115  result += GetExponentialValue (mean, bound);
1116 
1117  }
1118 
1119  return result;
1120 }
1121 
1122 uint32_t
1123 ErlangRandomVariable::GetInteger (uint32_t k, uint32_t lambda)
1124 {
1125  NS_LOG_FUNCTION (this << k << lambda);
1126  return static_cast<uint32_t> ( GetValue (k, lambda));
1127 }
1128 
1129 double
1131 {
1132  NS_LOG_FUNCTION (this);
1133  return GetValue (m_k, m_lambda);
1134 }
1135 uint32_t
1137 {
1138  NS_LOG_FUNCTION (this);
1139  return (uint32_t)GetValue (m_k, m_lambda);
1140 }
1141 
1142 double
1144 {
1145  NS_LOG_FUNCTION (this << mean << bound);
1146  while (1)
1147  {
1148  // Get a uniform random variable in [0,1].
1149  double v = Peek ()->RandU01 ();
1150  if (IsAntithetic ())
1151  {
1152  v = (1 - v);
1153  }
1154 
1155  // Calculate the exponential random variable.
1156  double r = -mean*std::log (v);
1157 
1158  // Use this value if it's acceptable.
1159  if (bound == 0 || r <= bound)
1160  {
1161  return r;
1162  }
1163  }
1164 }
1165 
1167 
1168 TypeId
1170 {
1171  static TypeId tid = TypeId ("ns3::TriangularRandomVariable")
1173  .SetGroupName ("Core")
1174  .AddConstructor<TriangularRandomVariable> ()
1175  .AddAttribute("Mean", "The mean value for the triangular distribution returned by this RNG stream.",
1176  DoubleValue(0.5),
1178  MakeDoubleChecker<double>())
1179  .AddAttribute("Min", "The lower bound on the values returned by this RNG stream.",
1180  DoubleValue(0.0),
1182  MakeDoubleChecker<double>())
1183  .AddAttribute("Max", "The upper bound on the values returned by this RNG stream.",
1184  DoubleValue(1.0),
1186  MakeDoubleChecker<double>())
1187  ;
1188  return tid;
1189 }
1191 {
1192  // m_mean, m_min, and m_max are initialized after constructor by
1193  // attributes
1194  NS_LOG_FUNCTION (this);
1195 }
1196 
1197 double
1199 {
1200  NS_LOG_FUNCTION (this);
1201  return m_mean;
1202 }
1203 double
1205 {
1206  NS_LOG_FUNCTION (this);
1207  return m_min;
1208 }
1209 double
1211 {
1212  NS_LOG_FUNCTION (this);
1213  return m_max;
1214 }
1215 
1216 double
1217 TriangularRandomVariable::GetValue (double mean, double min, double max)
1218 {
1219  // Calculate the mode.
1220  NS_LOG_FUNCTION (this << mean << min << max);
1221  double mode = 3.0 * mean - min - max;
1222 
1223  // Get a uniform random variable in [0,1].
1224  double u = Peek ()->RandU01 ();
1225  if (IsAntithetic ())
1226  {
1227  u = (1 - u);
1228  }
1229 
1230  // Calculate the triangular random variable.
1231  if (u <= (mode - min) / (max - min) )
1232  {
1233  return min + std::sqrt (u * (max - min) * (mode - min) );
1234  }
1235  else
1236  {
1237  return max - std::sqrt ( (1 - u) * (max - min) * (max - mode) );
1238  }
1239 }
1240 
1241 uint32_t
1242 TriangularRandomVariable::GetInteger (uint32_t mean, uint32_t min, uint32_t max)
1243 {
1244  NS_LOG_FUNCTION (this << mean << min << max);
1245  return static_cast<uint32_t> ( GetValue (mean, min, max) );
1246 }
1247 
1248 double
1250 {
1251  NS_LOG_FUNCTION (this);
1252  return GetValue (m_mean, m_min, m_max);
1253 }
1254 uint32_t
1256 {
1257  NS_LOG_FUNCTION (this);
1258  return (uint32_t)GetValue (m_mean, m_min, m_max);
1259 }
1260 
1262 
1263 TypeId
1265 {
1266  static TypeId tid = TypeId ("ns3::ZipfRandomVariable")
1268  .SetGroupName ("Core")
1269  .AddConstructor<ZipfRandomVariable> ()
1270  .AddAttribute("N", "The n value for the Zipf distribution returned by this RNG stream.",
1271  IntegerValue(1),
1273  MakeIntegerChecker<uint32_t>())
1274  .AddAttribute("Alpha", "The alpha value for the Zipf distribution returned by this RNG stream.",
1275  DoubleValue(0.0),
1277  MakeDoubleChecker<double>())
1278  ;
1279  return tid;
1280 }
1282 {
1283  // m_n and m_alpha are initialized after constructor by attributes
1284  NS_LOG_FUNCTION (this);
1285 }
1286 
1287 uint32_t
1289 {
1290  NS_LOG_FUNCTION (this);
1291  return m_n;
1292 }
1293 double
1295 {
1296  NS_LOG_FUNCTION (this);
1297  return m_alpha;
1298 }
1299 
1300 double
1301 ZipfRandomVariable::GetValue (uint32_t n, double alpha)
1302 {
1303  NS_LOG_FUNCTION (this << n << alpha);
1304  // Calculate the normalization constant c.
1305  m_c = 0.0;
1306  for (uint32_t i = 1; i <= n; i++)
1307  {
1308  m_c += (1.0 / std::pow ((double)i,alpha));
1309  }
1310  m_c = 1.0 / m_c;
1311 
1312  // Get a uniform random variable in [0,1].
1313  double u = Peek ()->RandU01 ();
1314  if (IsAntithetic ())
1315  {
1316  u = (1 - u);
1317  }
1318 
1319  double sum_prob = 0,zipf_value = 0;
1320  for (uint32_t i = 1; i <= m_n; i++)
1321  {
1322  sum_prob += m_c / std::pow ((double)i,m_alpha);
1323  if (sum_prob > u)
1324  {
1325  zipf_value = i;
1326  break;
1327  }
1328  }
1329  return zipf_value;
1330 }
1331 
1332 uint32_t
1333 ZipfRandomVariable::GetInteger (uint32_t n, uint32_t alpha)
1334 {
1335  NS_LOG_FUNCTION (this << n << alpha);
1336  return static_cast<uint32_t> ( GetValue (n, alpha));
1337 }
1338 
1339 double
1341 {
1342  NS_LOG_FUNCTION (this);
1343  return GetValue (m_n, m_alpha);
1344 }
1345 uint32_t
1347 {
1348  NS_LOG_FUNCTION (this);
1349  return (uint32_t)GetValue (m_n, m_alpha);
1350 }
1351 
1353 
1354 TypeId
1356 {
1357  static TypeId tid = TypeId ("ns3::ZetaRandomVariable")
1359  .SetGroupName ("Core")
1360  .AddConstructor<ZetaRandomVariable> ()
1361  .AddAttribute("Alpha", "The alpha value for the zeta distribution returned by this RNG stream.",
1362  DoubleValue(3.14),
1364  MakeDoubleChecker<double>())
1365  ;
1366  return tid;
1367 }
1369 {
1370  // m_alpha is initialized after constructor by attributes
1371  NS_LOG_FUNCTION (this);
1372 }
1373 
1374 double
1376 {
1377  NS_LOG_FUNCTION (this);
1378  return m_alpha;
1379 }
1380 
1381 double
1383 {
1384  NS_LOG_FUNCTION (this << alpha);
1385  m_b = std::pow (2.0, alpha - 1.0);
1386 
1387  double u, v;
1388  double X, T;
1389  double test;
1390 
1391  do
1392  {
1393  // Get a uniform random variable in [0,1].
1394  u = Peek ()->RandU01 ();
1395  if (IsAntithetic ())
1396  {
1397  u = (1 - u);
1398  }
1399 
1400  // Get a uniform random variable in [0,1].
1401  v = Peek ()->RandU01 ();
1402  if (IsAntithetic ())
1403  {
1404  v = (1 - v);
1405  }
1406 
1407  X = std::floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
1408  T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
1409  test = v * X * (T - 1.0) / (m_b - 1.0);
1410  }
1411  while ( test > (T / m_b) );
1412 
1413  return X;
1414 }
1415 
1416 uint32_t
1418 {
1419  NS_LOG_FUNCTION (this << alpha);
1420  return static_cast<uint32_t> ( GetValue (alpha));
1421 }
1422 
1423 double
1425 {
1426  NS_LOG_FUNCTION (this);
1427  return GetValue (m_alpha);
1428 }
1429 uint32_t
1431 {
1432  NS_LOG_FUNCTION (this);
1433  return (uint32_t)GetValue (m_alpha);
1434 }
1435 
1437 
1438 TypeId
1440 {
1441  static TypeId tid = TypeId ("ns3::DeterministicRandomVariable")
1443  .SetGroupName ("Core")
1444  .AddConstructor<DeterministicRandomVariable> ()
1445  ;
1446  return tid;
1447 }
1449  :
1450  m_count (0),
1451  m_next (0),
1452  m_data (0)
1453 {
1454  NS_LOG_FUNCTION (this);
1455 }
1457 {
1458  // Delete any values currently set.
1459  NS_LOG_FUNCTION (this);
1460  if (m_data != 0)
1461  {
1462  delete[] m_data;
1463  }
1464 }
1465 
1466 void
1467 DeterministicRandomVariable::SetValueArray (double* values, uint64_t length)
1468 {
1469  NS_LOG_FUNCTION (this << values << length);
1470  // Delete any values currently set.
1471  if (m_data != 0)
1472  {
1473  delete[] m_data;
1474  }
1475 
1476  // Make room for the values being set.
1477  m_data = new double[length];
1478  m_count = length;
1479  m_next = length;
1480 
1481  // Copy the values.
1482  for (uint64_t i = 0; i < m_count; i++)
1483  {
1484  m_data[i] = values[i];
1485  }
1486 }
1487 
1488 double
1490 {
1491  NS_LOG_FUNCTION (this);
1492  // Make sure the array has been set.
1493  NS_ASSERT (m_count > 0);
1494 
1495  if (m_next == m_count)
1496  {
1497  m_next = 0;
1498  }
1499  return m_data[m_next++];
1500 }
1501 
1502 uint32_t
1504 {
1505  NS_LOG_FUNCTION (this);
1506  return (uint32_t)GetValue ();
1507 }
1508 
1510 
1511 // ValueCDF methods
1513  : value (0.0),
1514  cdf (0.0)
1515 {
1516  NS_LOG_FUNCTION (this);
1517 }
1519  : value (v),
1520  cdf (c)
1521 {
1522  NS_LOG_FUNCTION (this << v << c);
1523 }
1525  : value (c.value),
1526  cdf (c.cdf)
1527 {
1528  NS_LOG_FUNCTION (this << &c);
1529 }
1530 
1531 TypeId
1533 {
1534  static TypeId tid = TypeId ("ns3::EmpiricalRandomVariable")
1536  .SetGroupName ("Core")
1537  .AddConstructor<EmpiricalRandomVariable> ()
1538  ;
1539  return tid;
1540 }
1542  :
1543  validated (false)
1544 {
1545  NS_LOG_FUNCTION (this);
1546 }
1547 
1548 double
1550 {
1551  NS_LOG_FUNCTION (this);
1552  // Return a value from the empirical distribution
1553  // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1554  if (emp.size () == 0)
1555  {
1556  return 0.0; // HuH? No empirical data
1557  }
1558  if (!validated)
1559  {
1560  Validate (); // Insure in non-decreasing
1561  }
1562 
1563  // Get a uniform random variable in [0,1].
1564  double r = Peek ()->RandU01 ();
1565  if (IsAntithetic ())
1566  {
1567  r = (1 - r);
1568  }
1569 
1570  if (r <= emp.front ().cdf)
1571  {
1572  return emp.front ().value; // Less than first
1573  }
1574  if (r >= emp.back ().cdf)
1575  {
1576  return emp.back ().value; // Greater than last
1577  }
1578  // Binary search
1579  std::vector<ValueCDF>::size_type bottom = 0;
1580  std::vector<ValueCDF>::size_type top = emp.size () - 1;
1581  while (1)
1582  {
1583  std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1584  if (r >= emp[c].cdf && r < emp[c + 1].cdf)
1585  { // Found it
1586  return Interpolate (emp[c].cdf, emp[c + 1].cdf,
1587  emp[c].value, emp[c + 1].value,
1588  r);
1589  }
1590  // Not here, adjust bounds
1591  if (r < emp[c].cdf)
1592  {
1593  top = c - 1;
1594  }
1595  else
1596  {
1597  bottom = c + 1;
1598  }
1599  }
1600 }
1601 
1602 uint32_t
1604 {
1605  NS_LOG_FUNCTION (this);
1606  return (uint32_t)GetValue ();
1607 }
1608 
1609 void EmpiricalRandomVariable::CDF (double v, double c)
1610 { // Add a new empirical datapoint to the empirical cdf
1611  // NOTE. These MUST be inserted in non-decreasing order
1612  NS_LOG_FUNCTION (this << v << c);
1613  emp.push_back (ValueCDF (v, c));
1614 }
1615 
1617 {
1618  NS_LOG_FUNCTION (this);
1619  ValueCDF prior = emp[0];
1620  for (std::vector<ValueCDF>::size_type i = 0; i < emp.size (); ++i)
1621  {
1622  ValueCDF& current = emp[i];
1623  if (current.value < prior.value || current.cdf < prior.cdf)
1624  { // Error
1625  std::cerr << "Empirical Dist error,"
1626  << " current value " << current.value
1627  << " prior value " << prior.value
1628  << " current cdf " << current.cdf
1629  << " prior cdf " << prior.cdf << std::endl;
1630  NS_FATAL_ERROR ("Empirical Dist error");
1631  }
1632  prior = current;
1633  }
1634  validated = true;
1635 }
1636 
1637 double EmpiricalRandomVariable::Interpolate (double c1, double c2,
1638  double v1, double v2, double r)
1639 { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1640  NS_LOG_FUNCTION (this << c1 << c2 << v1 << v2 << r);
1641  return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1642 }
1643 
1644 } // namespace ns3
The Random Number Generator (RNG) that returns a predetermined sequence.
double m_scale
The scale parameter for the Weibull distribution returned by this RNG stream.
double m_current
The current sequence value.
ExponentialRandomVariable()
Creates a exponential distribution RNG with the default values for the mean and upper bound...
Boolean attribute value declarations.
Double attribute value declarations and template implementations.
double GetSigma(void) const
Returns the sigma value for the log-normal distribution returned by this RNG stream.
double m_next
The algorithm produces two values at a time.
double GetAlpha(void) const
Returns the alpha value for the Zipf distribution returned by this RNG stream.
void SetAntithetic(bool isAntithetic)
Specify whether antithetic values should be generated.
NormalRandomVariable()
Creates a normal distribution RNG with the default values for the mean, variance, and bound...
virtual double GetValue(void)
Returns the next value in the empirical distribution.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
WeibullRandomVariable()
Creates a Weibull distribution RNG with the default values for the scale, shape, and upper bound...
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a Weibull distribution with the current scale, shape, and upper bound.
AttributeValue implementation for Boolean.
Definition: boolean.h:34
double GetAlpha(void) const
Returns the alpha value for the gamma distribution returned by this RNG stream.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
double m_alpha
The alpha value for the Zipf distribution returned by this RNG stream.
double GetLambda(void) const
Returns the lambda value for the Erlang distribution returned by this RNG stream. ...
double GetBound(void) const
Returns the upper bound on values that can be returned by this RNG stream.
double m_mean
The mean value for the triangular distribution returned by this RNG stream.
SequentialRandomVariable()
Creates a sequential RNG with the default values for the sequence parameters.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
double m_bound
The upper bound on values that can be returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
Hold variables of type string.
Definition: string.h:41
#define min(a, b)
Definition: 80211b.c:44
String attribute value declarations.
The exponential distribution Random Number Generator (RNG).
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a triangular distribution with the current mean...
The Random Number Generator (RNG) that returns a pattern of sequential values.
RandomVariableStream()
Default constructor.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:81
The normal (Gaussian) distribution Random Number Generator (RNG) that allows stream numbers to be set...
uint64_t m_count
Position in the array of values.
double GetExponentialValue(double mean, double bound)
Returns a random double from an exponential distribution with the specified mean and upper bound...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
Hold a signed integer type.
Definition: integer.h:44
double m_mean
The mean parameter for the Pareto distribution returned by this RNG stream.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual double GetValue(void)
Returns a random double from a normal distribution with the current mean, variance, and bound.
static TypeId GetTypeId(void)
Register this type.
double m_shape
The shape parameter for the Weibull distribution returned by this RNG stream.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:145
uint32_t GetN(void) const
Returns the n value for the Zipf distribution returned by this RNG stream.
int64_t m_stream
The stream number for this RNG stream.
virtual double Interpolate(double c1, double c2, double v1, double v2, double r)
Linear nterpolation between two points on the CDF to estimate the value at r.
double m_constant
The constant value returned by this RNG stream.
double m_min
The first value of the sequence.
double GetConstant(void) const
Get the constant value returned by this RNG stream.
double GetMin(void) const
Get the first value of the sequence.
Ptr< RandomVariableStream > m_increment
Increment between distinct values.
bool m_nextValid
True if the next value is valid.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
static uint64_t GetRun(void)
Get the current run number.
static TypeId GetTypeId(void)
Register this type.
static TypeId GetTypeId(void)
Register this type.
double m_bound
The upper bound on values that can be returned by this RNG stream.
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: integer.h:45
DeterministicRandomVariable()
Creates a deterministic RNG that will have a predetermined sequence of values.
static TypeId GetTypeId(void)
Register this type.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a gamma distribution with the current alpha and beta...
double m_shape
The shape parameter for the Pareto distribution returned by this RNG stream.
uint32_t m_n
The n value for the Zipf distribution returned by this RNG stream.
int64_t GetStream(void) const
Returns the stream number for this RNG stream.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
RngStream * m_rng
Pointer to the underlying RNG stream.
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:49
virtual void Validate()
Check that the CDF is valid.
double * m_data
Array of values to return in sequence.
Ptr< RandomVariableStream > GetIncrement(void) const
Get the increment for the sequence.
virtual uint32_t GetInteger(void)=0
Get the next random value as an integer drawn from the distribution.
double m_lambda
The lambda value for the Erlang distribution returned by this RNG stream.
double GetMin(void) const
Get the lower bound on randoms returned by GetValue(void).
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
#define max(a, b)
Definition: 80211b.c:45
double m_alpha
The alpha value for the zeta distribution returned by this RNG stream.
virtual double GetValue(void)
Returns the next value in the sequence.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a Pareto distribution with the current mean, shape, and upper bound.
LogNormalRandomVariable()
Creates a log-normal distribution RNG with the default values for mu and sigma.
double GetScale(void) const
Returns the scale parameter for the Weibull distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a log-normal distribution with the current mu and sigma...
double GetMin(void) const
Returns the lower bound for the triangular distribution returned by this RNG stream.
uint32_t GetK(void) const
Returns the k value for the Erlang distribution returned by this RNG stream.
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
ns3::RngSeedManager declaration.
double m_max
Strict upper bound on the sequence.
uint32_t GetConsecutive(void) const
Get the number of times each distinct value of the sequence is repeated before incrementing to the ne...
ZetaRandomVariable()
Creates a zeta distribution RNG with the default value for alpha.
uint32_t m_k
The k value for the Erlang distribution returned by this RNG stream.
The gamma distribution Random Number Generator (RNG) that allows stream numbers to be set determinist...
static TypeId GetTypeId(void)
Register this type.
virtual double GetValue(void)
Returns a random double from a triangular distribution with the current mean, min, and max.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a Zipf distribution with the current n and alpha...
void CDF(double v, double c)
Specifies a point in the empirical distribution.
Helper to hold one point of the CDF.
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
double m_sigma
The sigma value for the log-normal distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
ZipfRandomVariable()
Creates a Zipf distribution RNG with the default values for n and alpha.
The uniform distribution Random Number Generator (RNG).
static TypeId GetTypeId(void)
Register this type.
static TypeId GetTypeId(void)
Register this type.
GammaRandomVariable()
Creates a gamma distribution RNG with the default values for alpha and beta.
double GetMu(void) const
Returns the mu value for the log-normal distribution returned by this RNG stream. ...
uint64_t m_next
Position of the next value in the array of values.
static TypeId GetTypeId(void)
Register this type.
double m_c
The normalization constant.
uint32_t m_consecutive
The number of times each distinct value is repeated.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a normal distribution with the current mean, variance, and bound.
double GetMax(void) const
Returns the upper bound on values that can be returned by this RNG stream.
double m_variance
The variance value for the normal distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
double GetMean(void) const
Get the configured mean value of this RNG.
double m_mean
The mean value of the unbounded exponential distribution.
double GetVariance(void) const
Returns the variance value for the normal distribution returned by this RNG stream.
virtual double GetValue(void)
Returns a random double from a Zipf distribution with the current n and alpha.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
bool m_isCurrentSet
Indicates if the current sequence value has been properly initialized.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual double GetValue(void)
Returns a random double from an Erlang distribution with the current k and lambda.
Declaration of class RngStream.
double m_mean
The mean value for the normal distribution returned by this RNG stream.
static const double INFINITE_VALUE
Large constant to bound the range.
static TypeId GetTypeId(void)
Register this type.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
uint32_t m_currentConsecutive
The number of times the current distinct value has been repeated.
double GetMean(void) const
Returns the mean value for the normal distribution returned by this RNG stream.
void SetValueArray(double *values, uint64_t length)
Sets the array of values that holds the predetermined sequence.
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
static TypeId GetTypeId(void)
Register this type.
double m_bound
The upper bound on values that can be returned by this RNG stream.
EmpiricalRandomVariable()
Creates an empirical RNG that has a specified, empirical distribution.
double m_beta
The beta value for the gamma distribution returned by this RNG stream.
double RandU01(void)
Generate the next random number for this stream.
Definition: rng-stream.cc:338
bool IsAntithetic(void) const
Check if antithetic values will be generated.
virtual double GetValue(void)
Returns a random double from a Weibull distribution with the current scale, shape, and upper bound.
double m_alpha
The alpha value for the gamma distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns the next value in the sequence.
static uint32_t GetSeed(void)
Get the current seed value which will be used by all subsequently instantiated RandomVariableStream o...
double GetNormalValue(double mean, double variance, double bound)
Returns a random double from a normal distribution with the specified mean, variance, and bound.
double GetMax(void) const
Get the limit of the sequence, which is (at least) one more than the last value of the sequence...
virtual double GetValue(void)
Returns a random double from a log-normal distribution with the current mu and sigma.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: double.h:42
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a zeta distribution with the current alpha.
ParetoRandomVariable()
Creates a Pareto distribution RNG with the default values for the mean, the shape, and upper bound.
double GetBound(void) const
Returns the bound on values that can be returned by this RNG stream.
The Zipf distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
The basic uniform Random Number Generator (RNG).
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
Declaration of ns3::RandomVariableStream and derivatives.
double GetBound(void) const
Get the configured upper bound of this RNG.
virtual double GetValue(void)
Returns a random double from a zeta distribution with the current alpha.
double GetShape(void) const
Returns the shape parameter for the Weibull distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns the next value in the empirical distribution.
Integer attribute value declarations and template implementations.
double m_bound
The bound on values that can be returned by this RNG stream.
double GetMean(void) const
Returns the mean parameter for the Pareto distribution returned by this RNG stream.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from an Erlang distribution with the current k and lambda...
virtual ~RandomVariableStream()
Destructor.
double m_next
The algorithm produces two normal values at a time.
RngStream * Peek(void) const
Get the pointer to the underlying RNG stream.
double m_b
Just for calculus simplifications.
UniformRandomVariable()
Creates a uniform distribution RNG with the default range.
The triangular distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
static TypeId GetTypeId(void)
Register this type.
double m_max
The upper bound on values that can be returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
ErlangRandomVariable()
Creates an Erlang distribution RNG with the default values for k and lambda.
bool m_nextValid
True if the next normal value is valid.
double GetBound(void) const
Returns the upper bound on values that can be returned by this RNG stream.
The Erlang distribution Random Number Generator (RNG) that allows stream numbers to be set determinis...
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual double GetValue(void)
Returns a random double from a Pareto distribution with the current mean, shape, and upper bound...
static uint64_t GetNextStreamIndex(void)
Get the next automatically assigned stream index.
The Random Number Generator (RNG) that has a specified empirical distribution.
Debug message logging.
double GetBeta(void) const
Returns the beta value for the gamma distribution returned by this RNG stream.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
virtual double GetValue(void)
Returns a random double from a gamma distribution with the current alpha and beta.
double GetAlpha(void) const
Returns the alpha value for the zeta distribution returned by this RNG stream.
Pointer attribute value declarations and template implementations.
The log-normal distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
a unique identifier for an interface.
Definition: type-id.h:58
TriangularRandomVariable()
Creates a triangular distribution RNG with the default values for the mean, lower bound...
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
ConstantRandomVariable()
Creates a constant RNG with the default constant value.
The Pareto distribution Random Number Generator (RNG).
void test(void)
Example use of ns3::SystemThread.
double GetMean(void) const
Returns the mean value for the triangular distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
double m_mu
The mu value for the log-normal distribution returned by this RNG stream.
double m_min
The lower bound on values that can be returned by this RNG stream.
bool validated
true once the CDF has been validated.
The Random Number Generator (RNG) that returns a constant.
The Weibull distribution Random Number Generator (RNG) that allows stream numbers to be set determini...
The zeta distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
double GetShape(void) const
Returns the shape parameter for the Pareto distribution returned by this RNG stream.
std::vector< ValueCDF > emp
The vector of CDF points.
bool m_isAntithetic
Indicates if antithetic values should be generated by this RNG stream.
double GetMax(void) const
Get the upper bound on values returned by GetValue(void).