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 "unused.h"
37 #include <cmath>
38 #include <iostream>
39 #include <algorithm> // upper_bound
40 
47 namespace ns3 {
48 
49 NS_LOG_COMPONENT_DEFINE ("RandomVariableStream");
50 
51 NS_OBJECT_ENSURE_REGISTERED (RandomVariableStream);
52 
53 TypeId
55 {
56  static TypeId tid = TypeId ("ns3::RandomVariableStream")
57  .SetParent<Object> ()
58  .SetGroupName ("Core")
59  .AddAttribute ("Stream",
60  "The stream number for this RNG stream. -1 means \"allocate a stream automatically\". "
61  "Note that if -1 is set, Get will return -1 so that it is not possible to know which "
62  "value was automatically allocated.",
63  IntegerValue (-1),
66  MakeIntegerChecker<int64_t>())
67  .AddAttribute ("Antithetic", "Set this RNG stream to generate antithetic values",
68  BooleanValue (false),
72  ;
73  return tid;
74 }
75 
77  : m_rng (0)
78 {
79  NS_LOG_FUNCTION (this);
80 }
82 {
83  NS_LOG_FUNCTION (this);
84  delete m_rng;
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION (this << isAntithetic);
91  m_isAntithetic = isAntithetic;
92 }
93 bool
95 {
96  NS_LOG_FUNCTION (this);
97  return m_isAntithetic;
98 }
99 void
101 {
102  NS_LOG_FUNCTION (this << stream);
103  // negative values are not legal.
104  NS_ASSERT (stream >= -1);
105  delete m_rng;
106  if (stream == -1)
107  {
108  // The first 2^63 streams are reserved for automatic stream
109  // number assignment.
110  uint64_t nextStream = RngSeedManager::GetNextStreamIndex ();
111  NS_ASSERT (nextStream <= ((1ULL) << 63));
113  nextStream,
115  }
116  else
117  {
118  // The last 2^63 streams are reserved for deterministic stream
119  // number assignment.
120  uint64_t base = ((1ULL) << 63);
121  uint64_t target = base + stream;
123  target,
125  }
126  m_stream = stream;
127 }
128 int64_t
130 {
131  NS_LOG_FUNCTION (this);
132  return m_stream;
133 }
134 
135 RngStream *
137 {
138  NS_LOG_FUNCTION (this);
139  return m_rng;
140 }
141 
143 
144 TypeId
146 {
147  static TypeId tid = TypeId ("ns3::UniformRandomVariable")
149  .SetGroupName ("Core")
150  .AddConstructor<UniformRandomVariable> ()
151  .AddAttribute ("Min", "The lower bound on the values returned by this RNG stream.",
152  DoubleValue (0),
154  MakeDoubleChecker<double>())
155  .AddAttribute ("Max", "The upper bound on the values returned by this RNG stream.",
156  DoubleValue (1.0),
158  MakeDoubleChecker<double>())
159  ;
160  return tid;
161 }
163 {
164  // m_min and m_max are initialized after constructor by attributes
165  NS_LOG_FUNCTION (this);
166 }
167 
168 double
170 {
171  NS_LOG_FUNCTION (this);
172  return m_min;
173 }
174 double
176 {
177  NS_LOG_FUNCTION (this);
178  return m_max;
179 }
180 
181 double
183 {
184  NS_LOG_FUNCTION (this << min << max);
185  double v = min + Peek ()->RandU01 () * (max - min);
186  if (IsAntithetic ())
187  {
188  v = min + (max - v);
189  }
190  return v;
191 }
192 uint32_t
194 {
195  NS_LOG_FUNCTION (this << min << max);
196  NS_ASSERT (min <= max);
197  return static_cast<uint32_t> ( GetValue ((double) (min), (double) (max) + 1.0) );
198 }
199 
200 double
202 {
203  NS_LOG_FUNCTION (this);
204  return GetValue (m_min, m_max);
205 }
206 uint32_t
208 {
209  NS_LOG_FUNCTION (this);
210  return (uint32_t)GetValue (m_min, m_max + 1);
211 }
212 
214 
215 TypeId
217 {
218  static TypeId tid = TypeId ("ns3::ConstantRandomVariable")
220  .SetGroupName ("Core")
221  .AddConstructor<ConstantRandomVariable> ()
222  .AddAttribute ("Constant", "The constant value returned by this RNG stream.",
223  DoubleValue (0),
225  MakeDoubleChecker<double>())
226  ;
227  return tid;
228 }
230 {
231  // m_constant is initialized after constructor by attributes
232  NS_LOG_FUNCTION (this);
233 }
234 
235 double
237 {
238  NS_LOG_FUNCTION (this);
239  return m_constant;
240 }
241 
242 double
244 {
245  NS_LOG_FUNCTION (this << constant);
246  return constant;
247 }
248 uint32_t
250 {
251  NS_LOG_FUNCTION (this << constant);
252  return constant;
253 }
254 
255 double
257 {
258  NS_LOG_FUNCTION (this);
259  return GetValue (m_constant);
260 }
261 uint32_t
263 {
264  NS_LOG_FUNCTION (this);
265  return (uint32_t)GetValue (m_constant);
266 }
267 
269 
270 TypeId
272 {
273  static TypeId tid = TypeId ("ns3::SequentialRandomVariable")
275  .SetGroupName ("Core")
276  .AddConstructor<SequentialRandomVariable> ()
277  .AddAttribute ("Min", "The first value of the sequence.",
278  DoubleValue (0),
280  MakeDoubleChecker<double>())
281  .AddAttribute ("Max", "One more than the last value of the sequence.",
282  DoubleValue (0),
284  MakeDoubleChecker<double>())
285  .AddAttribute ("Increment", "The sequence random variable increment.",
286  StringValue ("ns3::ConstantRandomVariable[Constant=1]"),
288  MakePointerChecker<RandomVariableStream> ())
289  .AddAttribute ("Consecutive", "The number of times each member of the sequence is repeated.",
290  IntegerValue (1),
292  MakeIntegerChecker<uint32_t>());
293  return tid;
294 }
296  :
297  m_current (0),
298  m_currentConsecutive (0),
299  m_isCurrentSet (false)
300 {
301  // m_min, m_max, m_increment, and m_consecutive are initialized
302  // after constructor by attributes.
303  NS_LOG_FUNCTION (this);
304 }
305 
306 double
308 {
309  NS_LOG_FUNCTION (this);
310  return m_min;
311 }
312 
313 double
315 {
316  NS_LOG_FUNCTION (this);
317  return m_max;
318 }
319 
322 {
323  NS_LOG_FUNCTION (this);
324  return m_increment;
325 }
326 
327 uint32_t
329 {
330  NS_LOG_FUNCTION (this);
331  return m_consecutive;
332 }
333 
334 double
336 {
337  // Set the current sequence value if it hasn't been set.
338  NS_LOG_FUNCTION (this);
339  if (!m_isCurrentSet)
340  {
341  // Start the sequence at its minimium value.
342  m_current = m_min;
343  m_isCurrentSet = true;
344  }
345 
346  // Return a sequential series of values
347  double r = m_current;
349  { // Time to advance to next
352  if (m_current >= m_max)
353  {
354  m_current = m_min + (m_current - m_max);
355  }
356  }
357  return r;
358 }
359 
360 uint32_t
362 {
363  NS_LOG_FUNCTION (this);
364  return (uint32_t)GetValue ();
365 }
366 
368 
369 TypeId
371 {
372  static TypeId tid = TypeId ("ns3::ExponentialRandomVariable")
374  .SetGroupName ("Core")
375  .AddConstructor<ExponentialRandomVariable> ()
376  .AddAttribute ("Mean", "The mean of the values returned by this RNG stream.",
377  DoubleValue (1.0),
379  MakeDoubleChecker<double>())
380  .AddAttribute ("Bound", "The upper bound on the values returned by this RNG stream.",
381  DoubleValue (0.0),
383  MakeDoubleChecker<double>())
384  ;
385  return tid;
386 }
388 {
389  // m_mean and m_bound are initialized after constructor by attributes
390  NS_LOG_FUNCTION (this);
391 }
392 
393 double
395 {
396  NS_LOG_FUNCTION (this);
397  return m_mean;
398 }
399 double
401 {
402  NS_LOG_FUNCTION (this);
403  return m_bound;
404 }
405 
406 double
407 ExponentialRandomVariable::GetValue (double mean, double bound)
408 {
409  NS_LOG_FUNCTION (this << mean << bound);
410  while (1)
411  {
412  // Get a uniform random variable in [0,1].
413  double v = Peek ()->RandU01 ();
414  if (IsAntithetic ())
415  {
416  v = (1 - v);
417  }
418 
419  // Calculate the exponential random variable.
420  double r = -mean*std::log (v);
421 
422  // Use this value if it's acceptable.
423  if (bound == 0 || r <= bound)
424  {
425  return r;
426  }
427  }
428 }
429 uint32_t
430 ExponentialRandomVariable::GetInteger (uint32_t mean, uint32_t bound)
431 {
432  NS_LOG_FUNCTION (this << mean << bound);
433  return static_cast<uint32_t> ( GetValue (mean, bound) );
434 }
435 
436 double
438 {
439  NS_LOG_FUNCTION (this);
440  return GetValue (m_mean, m_bound);
441 }
442 uint32_t
444 {
445  NS_LOG_FUNCTION (this);
446  return (uint32_t)GetValue (m_mean, m_bound);
447 }
448 
450 
451 TypeId
453 {
454  static TypeId tid = TypeId ("ns3::ParetoRandomVariable")
456  .SetGroupName ("Core")
457  .AddConstructor<ParetoRandomVariable> ()
458  .AddAttribute ("Mean", "The mean parameter for the Pareto distribution returned by this RNG stream.",
459  DoubleValue (0.0),
461  MakeDoubleChecker<double>(),
463  "Not anymore used. Use 'Scale' instead - changing this attribute has no effect.")
464  .AddAttribute ("Scale", "The scale parameter for the Pareto distribution returned by this RNG stream.",
465  DoubleValue (1.0),
467  MakeDoubleChecker<double>())
468  .AddAttribute ("Shape", "The shape parameter for the Pareto distribution returned by this RNG stream.",
469  DoubleValue (2.0),
471  MakeDoubleChecker<double>())
472  .AddAttribute ("Bound", "The upper bound on the values returned by this RNG stream (if non-zero).",
473  DoubleValue (0.0),
475  MakeDoubleChecker<double>())
476  ;
477  return tid;
478 }
480 {
481  // m_shape, m_shape, and m_bound are initialized after constructor
482  // by attributes
483  NS_LOG_FUNCTION (this);
484  NS_UNUSED (m_mean);
485 }
486 
487 double
489 {
490  NS_LOG_FUNCTION (this);
491  return m_scale;
492 }
493 
494 double
496 {
497  NS_LOG_FUNCTION (this);
498  return m_shape;
499 }
500 
501 double
503 {
504  NS_LOG_FUNCTION (this);
505  return m_bound;
506 }
507 
508 double
509 ParetoRandomVariable::GetValue (double scale, double shape, double bound)
510 {
511  // Calculate the scale parameter.
512  NS_LOG_FUNCTION (this << scale << shape << bound);
513 
514  while (1)
515  {
516  // Get a uniform random variable in [0,1].
517  double v = Peek ()->RandU01 ();
518  if (IsAntithetic ())
519  {
520  v = (1 - v);
521  }
522 
523  // Calculate the Pareto random variable.
524  double r = (scale * ( 1.0 / std::pow (v, 1.0 / shape)));
525 
526  // Use this value if it's acceptable.
527  if (bound == 0 || r <= bound)
528  {
529  return r;
530  }
531  }
532 }
533 uint32_t
534 ParetoRandomVariable::GetInteger (uint32_t scale, uint32_t shape, uint32_t bound)
535 {
536  NS_LOG_FUNCTION (this << scale << shape << bound);
537  return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
538 }
539 
540 double
542 {
543  NS_LOG_FUNCTION (this);
544  return GetValue (m_scale, m_shape, m_bound);
545 }
546 uint32_t
548 {
549  NS_LOG_FUNCTION (this);
550  return (uint32_t)GetValue (m_scale, m_shape, m_bound);
551 }
552 
554 
555 TypeId
557 {
558  static TypeId tid = TypeId ("ns3::WeibullRandomVariable")
560  .SetGroupName ("Core")
561  .AddConstructor<WeibullRandomVariable> ()
562  .AddAttribute ("Scale", "The scale parameter for the Weibull distribution returned by this RNG stream.",
563  DoubleValue (1.0),
565  MakeDoubleChecker<double>())
566  .AddAttribute ("Shape", "The shape parameter for the Weibull distribution returned by this RNG stream.",
567  DoubleValue (1),
569  MakeDoubleChecker<double>())
570  .AddAttribute ("Bound", "The upper bound on the values returned by this RNG stream.",
571  DoubleValue (0.0),
573  MakeDoubleChecker<double>())
574  ;
575  return tid;
576 }
578 {
579  // m_scale, m_shape, and m_bound are initialized after constructor
580  // by attributes
581  NS_LOG_FUNCTION (this);
582 }
583 
584 double
586 {
587  NS_LOG_FUNCTION (this);
588  return m_scale;
589 }
590 double
592 {
593  NS_LOG_FUNCTION (this);
594  return m_shape;
595 }
596 double
598 {
599  NS_LOG_FUNCTION (this);
600  return m_bound;
601 }
602 
603 double
604 WeibullRandomVariable::GetValue (double scale, double shape, double bound)
605 {
606  NS_LOG_FUNCTION (this << scale << shape << bound);
607  double exponent = 1.0 / shape;
608  while (1)
609  {
610  // Get a uniform random variable in [0,1].
611  double v = Peek ()->RandU01 ();
612  if (IsAntithetic ())
613  {
614  v = (1 - v);
615  }
616 
617  // Calculate the Weibull random variable.
618  double r = scale * std::pow ( -std::log (v), exponent);
619 
620  // Use this value if it's acceptable.
621  if (bound == 0 || r <= bound)
622  {
623  return r;
624  }
625  }
626 }
627 uint32_t
628 WeibullRandomVariable::GetInteger (uint32_t scale, uint32_t shape, uint32_t bound)
629 {
630  NS_LOG_FUNCTION (this << scale << shape << bound);
631  return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
632 }
633 
634 double
636 {
637  NS_LOG_FUNCTION (this);
638  return GetValue (m_scale, m_shape, m_bound);
639 }
640 uint32_t
642 {
643  NS_LOG_FUNCTION (this);
644  return (uint32_t)GetValue (m_scale, m_shape, m_bound);
645 }
646 
648 
649 const double NormalRandomVariable::INFINITE_VALUE = 1e307;
650 
651 TypeId
653 {
654  static TypeId tid = TypeId ("ns3::NormalRandomVariable")
656  .SetGroupName ("Core")
657  .AddConstructor<NormalRandomVariable> ()
658  .AddAttribute ("Mean", "The mean value for the normal distribution returned by this RNG stream.",
659  DoubleValue (0.0),
661  MakeDoubleChecker<double>())
662  .AddAttribute ("Variance", "The variance value for the normal distribution returned by this RNG stream.",
663  DoubleValue (1.0),
665  MakeDoubleChecker<double>())
666  .AddAttribute ("Bound", "The bound on the values returned by this RNG stream.",
669  MakeDoubleChecker<double>())
670  ;
671  return tid;
672 }
674  :
675  m_nextValid (false)
676 {
677  // m_mean, m_variance, and m_bound are initialized after constructor
678  // by attributes
679  NS_LOG_FUNCTION (this);
680 }
681 
682 double
684 {
685  NS_LOG_FUNCTION (this);
686  return m_mean;
687 }
688 double
690 {
691  NS_LOG_FUNCTION (this);
692  return m_variance;
693 }
694 double
696 {
697  NS_LOG_FUNCTION (this);
698  return m_bound;
699 }
700 
701 double
702 NormalRandomVariable::GetValue (double mean, double variance, double bound)
703 {
704  NS_LOG_FUNCTION (this << mean << variance << bound);
705  if (m_nextValid)
706  { // use previously generated
707  m_nextValid = false;
708  return m_next;
709  }
710  while (1)
711  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
712  // for algorithm; basically a Box-Muller transform:
713  // http://en.wikipedia.org/wiki/Box-Muller_transform
714  double u1 = Peek ()->RandU01 ();
715  double u2 = Peek ()->RandU01 ();
716  if (IsAntithetic ())
717  {
718  u1 = (1 - u1);
719  u2 = (1 - u2);
720  }
721  double v1 = 2 * u1 - 1;
722  double v2 = 2 * u2 - 1;
723  double w = v1 * v1 + v2 * v2;
724  if (w <= 1.0)
725  { // Got good pair
726  double y = std::sqrt ((-2 * std::log (w)) / w);
727  m_next = mean + v2 * y * std::sqrt (variance);
728  // if next is in bounds, it is valid
729  m_nextValid = std::fabs (m_next - mean) <= bound;
730  double x1 = mean + v1 * y * std::sqrt (variance);
731  // if x1 is in bounds, return it
732  if (std::fabs (x1 - mean) <= bound)
733  {
734  return x1;
735  }
736  // otherwise try and return m_next if it is valid
737  else if (m_nextValid)
738  {
739  m_nextValid = false;
740  return m_next;
741  }
742  // otherwise, just run this loop again
743  }
744  }
745 }
746 
747 uint32_t
748 NormalRandomVariable::GetInteger (uint32_t mean, uint32_t variance, uint32_t bound)
749 {
750  NS_LOG_FUNCTION (this << mean << variance << bound);
751  return static_cast<uint32_t> ( GetValue (mean, variance, bound) );
752 }
753 
754 double
756 {
757  NS_LOG_FUNCTION (this);
758  return GetValue (m_mean, m_variance, m_bound);
759 }
760 uint32_t
762 {
763  NS_LOG_FUNCTION (this);
764  return (uint32_t)GetValue (m_mean, m_variance, m_bound);
765 }
766 
768 
769 TypeId
771 {
772  static TypeId tid = TypeId ("ns3::LogNormalRandomVariable")
774  .SetGroupName ("Core")
775  .AddConstructor<LogNormalRandomVariable> ()
776  .AddAttribute ("Mu", "The mu value for the log-normal distribution returned by this RNG stream.",
777  DoubleValue (0.0),
779  MakeDoubleChecker<double>())
780  .AddAttribute ("Sigma", "The sigma value for the log-normal distribution returned by this RNG stream.",
781  DoubleValue (1.0),
783  MakeDoubleChecker<double>())
784  ;
785  return tid;
786 }
788 {
789  // m_mu and m_sigma are initialized after constructor by
790  // attributes
791  NS_LOG_FUNCTION (this);
792 }
793 
794 double
796 {
797  NS_LOG_FUNCTION (this);
798  return m_mu;
799 }
800 double
802 {
803  NS_LOG_FUNCTION (this);
804  return m_sigma;
805 }
806 
807 // The code from this function was adapted from the GNU Scientific
808 // Library 1.8:
809 /* randist/lognormal.c
810  *
811  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
812  *
813  * This program is free software; you can redistribute it and/or modify
814  * it under the terms of the GNU General Public License as published by
815  * the Free Software Foundation; either version 2 of the License, or (at
816  * your option) any later version.
817  *
818  * This program is distributed in the hope that it will be useful, but
819  * WITHOUT ANY WARRANTY; without even the implied warranty of
820  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
821  * General Public License for more details.
822  *
823  * You should have received a copy of the GNU General Public License
824  * along with this program; if not, write to the Free Software
825  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
826  */
827 /* The lognormal distribution has the form
828 
829  p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
830 
831  for x > 0. Lognormal random numbers are the exponentials of
832  gaussian random numbers */
833 double
834 LogNormalRandomVariable::GetValue (double mu, double sigma)
835 {
836  double v1, v2, r2, normal, x;
837 
838  NS_LOG_FUNCTION (this << mu << sigma);
839 
840  do
841  {
842  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
843 
844  double u1 = Peek ()->RandU01 ();
845  double u2 = Peek ()->RandU01 ();
846  if (IsAntithetic ())
847  {
848  u1 = (1 - u1);
849  u2 = (1 - u2);
850  }
851 
852  v1 = -1 + 2 * u1;
853  v2 = -1 + 2 * u2;
854 
855  /* see if it is in the unit circle */
856  r2 = v1 * v1 + v2 * v2;
857  }
858  while (r2 > 1.0 || r2 == 0);
859 
860  normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
861 
862  x = std::exp (sigma * normal + mu);
863 
864  return x;
865 }
866 
867 uint32_t
868 LogNormalRandomVariable::GetInteger (uint32_t mu, uint32_t sigma)
869 {
870  NS_LOG_FUNCTION (this << mu << sigma);
871  return static_cast<uint32_t> ( GetValue (mu, sigma));
872 }
873 
874 double
876 {
877  NS_LOG_FUNCTION (this);
878  return GetValue (m_mu, m_sigma);
879 }
880 uint32_t
882 {
883  NS_LOG_FUNCTION (this);
884  return (uint32_t)GetValue (m_mu, m_sigma);
885 }
886 
888 
889 TypeId
891 {
892  static TypeId tid = TypeId ("ns3::GammaRandomVariable")
894  .SetGroupName ("Core")
895  .AddConstructor<GammaRandomVariable> ()
896  .AddAttribute ("Alpha", "The alpha value for the gamma distribution returned by this RNG stream.",
897  DoubleValue (1.0),
899  MakeDoubleChecker<double>())
900  .AddAttribute ("Beta", "The beta value for the gamma distribution returned by this RNG stream.",
901  DoubleValue (1.0),
903  MakeDoubleChecker<double>())
904  ;
905  return tid;
906 }
908  :
909  m_nextValid (false)
910 {
911  // m_alpha and m_beta are initialized after constructor by
912  // attributes
913  NS_LOG_FUNCTION (this);
914 }
915 
916 double
918 {
919  NS_LOG_FUNCTION (this);
920  return m_alpha;
921 }
922 double
924 {
925  NS_LOG_FUNCTION (this);
926  return m_beta;
927 }
928 
929 /*
930  The code for the following generator functions was adapted from ns-2
931  tools/ranvar.cc
932 
933  Originally the algorithm was devised by Marsaglia in 2000:
934  G. Marsaglia, W. W. Tsang: A simple method for generating Gamma variables
935  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
936 
937  The Gamma distribution density function has the form
938 
939  x^(alpha-1) * exp(-x/beta)
940  p(x; alpha, beta) = ----------------------------
941  beta^alpha * Gamma(alpha)
942 
943  for x > 0.
944 */
945 double
947 {
948  NS_LOG_FUNCTION (this << alpha << beta);
949  if (alpha < 1)
950  {
951  double u = Peek ()->RandU01 ();
952  if (IsAntithetic ())
953  {
954  u = (1 - u);
955  }
956  return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
957  }
958 
959  double x, v, u;
960  double d = alpha - 1.0 / 3.0;
961  double c = (1.0 / 3.0) / std::sqrt (d);
962 
963  while (1)
964  {
965  do
966  {
967  // Get a value from a normal distribution that has mean
968  // zero, variance 1, and no bound.
969  double mean = 0.0;
970  double variance = 1.0;
972  x = GetNormalValue (mean, variance, bound);
973 
974  v = 1.0 + c * x;
975  }
976  while (v <= 0);
977 
978  v = v * v * v;
979  u = Peek ()->RandU01 ();
980  if (IsAntithetic ())
981  {
982  u = (1 - u);
983  }
984  if (u < 1 - 0.0331 * x * x * x * x)
985  {
986  break;
987  }
988  if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
989  {
990  break;
991  }
992  }
993 
994  return beta * d * v;
995 }
996 
997 uint32_t
998 GammaRandomVariable::GetInteger (uint32_t alpha, uint32_t beta)
999 {
1000  NS_LOG_FUNCTION (this << alpha << beta);
1001  return static_cast<uint32_t> ( GetValue (alpha, beta));
1002 }
1003 
1004 double
1006 {
1007  NS_LOG_FUNCTION (this);
1008  return GetValue (m_alpha, m_beta);
1009 }
1010 uint32_t
1012 {
1013  NS_LOG_FUNCTION (this);
1014  return (uint32_t)GetValue (m_alpha, m_beta);
1015 }
1016 
1017 double
1018 GammaRandomVariable::GetNormalValue (double mean, double variance, double bound)
1019 {
1020  NS_LOG_FUNCTION (this << mean << variance << bound);
1021  if (m_nextValid)
1022  { // use previously generated
1023  m_nextValid = false;
1024  return m_next;
1025  }
1026  while (1)
1027  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1028  // for algorithm; basically a Box-Muller transform:
1029  // http://en.wikipedia.org/wiki/Box-Muller_transform
1030  double u1 = Peek ()->RandU01 ();
1031  double u2 = Peek ()->RandU01 ();
1032  if (IsAntithetic ())
1033  {
1034  u1 = (1 - u1);
1035  u2 = (1 - u2);
1036  }
1037  double v1 = 2 * u1 - 1;
1038  double v2 = 2 * u2 - 1;
1039  double w = v1 * v1 + v2 * v2;
1040  if (w <= 1.0)
1041  { // Got good pair
1042  double y = std::sqrt ((-2 * std::log (w)) / w);
1043  m_next = mean + v2 * y * std::sqrt (variance);
1044  // if next is in bounds, it is valid
1045  m_nextValid = std::fabs (m_next - mean) <= bound;
1046  double x1 = mean + v1 * y * std::sqrt (variance);
1047  // if x1 is in bounds, return it
1048  if (std::fabs (x1 - mean) <= bound)
1049  {
1050  return x1;
1051  }
1052  // otherwise try and return m_next if it is valid
1053  else if (m_nextValid)
1054  {
1055  m_nextValid = false;
1056  return m_next;
1057  }
1058  // otherwise, just run this loop again
1059  }
1060  }
1061 }
1062 
1064 
1065 TypeId
1067 {
1068  static TypeId tid = TypeId ("ns3::ErlangRandomVariable")
1070  .SetGroupName ("Core")
1071  .AddConstructor<ErlangRandomVariable> ()
1072  .AddAttribute ("K", "The k value for the Erlang distribution returned by this RNG stream.",
1073  IntegerValue (1),
1075  MakeIntegerChecker<uint32_t>())
1076  .AddAttribute ("Lambda", "The lambda value for the Erlang distribution returned by this RNG stream.",
1077  DoubleValue (1.0),
1079  MakeDoubleChecker<double>())
1080  ;
1081  return tid;
1082 }
1084 {
1085  // m_k and m_lambda are initialized after constructor by attributes
1086  NS_LOG_FUNCTION (this);
1087 }
1088 
1089 uint32_t
1091 {
1092  NS_LOG_FUNCTION (this);
1093  return m_k;
1094 }
1095 double
1097 {
1098  NS_LOG_FUNCTION (this);
1099  return m_lambda;
1100 }
1101 
1102 /*
1103  The code for the following generator functions was adapted from ns-2
1104  tools/ranvar.cc
1105 
1106  The Erlang distribution density function has the form
1107 
1108  x^(k-1) * exp(-x/lambda)
1109  p(x; k, lambda) = ---------------------------
1110  lambda^k * (k-1)!
1111 
1112  for x > 0.
1113 */
1114 double
1115 ErlangRandomVariable::GetValue (uint32_t k, double lambda)
1116 {
1117  NS_LOG_FUNCTION (this << k << lambda);
1118  double mean = lambda;
1119  double bound = 0.0;
1120 
1121  double result = 0;
1122  for (unsigned int i = 0; i < k; ++i)
1123  {
1124  result += GetExponentialValue (mean, bound);
1125 
1126  }
1127 
1128  return result;
1129 }
1130 
1131 uint32_t
1132 ErlangRandomVariable::GetInteger (uint32_t k, uint32_t lambda)
1133 {
1134  NS_LOG_FUNCTION (this << k << lambda);
1135  return static_cast<uint32_t> ( GetValue (k, lambda));
1136 }
1137 
1138 double
1140 {
1141  NS_LOG_FUNCTION (this);
1142  return GetValue (m_k, m_lambda);
1143 }
1144 uint32_t
1146 {
1147  NS_LOG_FUNCTION (this);
1148  return (uint32_t)GetValue (m_k, m_lambda);
1149 }
1150 
1151 double
1153 {
1154  NS_LOG_FUNCTION (this << mean << bound);
1155  while (1)
1156  {
1157  // Get a uniform random variable in [0,1].
1158  double v = Peek ()->RandU01 ();
1159  if (IsAntithetic ())
1160  {
1161  v = (1 - v);
1162  }
1163 
1164  // Calculate the exponential random variable.
1165  double r = -mean*std::log (v);
1166 
1167  // Use this value if it's acceptable.
1168  if (bound == 0 || r <= bound)
1169  {
1170  return r;
1171  }
1172  }
1173 }
1174 
1176 
1177 TypeId
1179 {
1180  static TypeId tid = TypeId ("ns3::TriangularRandomVariable")
1182  .SetGroupName ("Core")
1183  .AddConstructor<TriangularRandomVariable> ()
1184  .AddAttribute ("Mean", "The mean value for the triangular distribution returned by this RNG stream.",
1185  DoubleValue (0.5),
1187  MakeDoubleChecker<double>())
1188  .AddAttribute ("Min", "The lower bound on the values returned by this RNG stream.",
1189  DoubleValue (0.0),
1191  MakeDoubleChecker<double>())
1192  .AddAttribute ("Max", "The upper bound on the values returned by this RNG stream.",
1193  DoubleValue (1.0),
1195  MakeDoubleChecker<double>())
1196  ;
1197  return tid;
1198 }
1200 {
1201  // m_mean, m_min, and m_max are initialized after constructor by
1202  // attributes
1203  NS_LOG_FUNCTION (this);
1204 }
1205 
1206 double
1208 {
1209  NS_LOG_FUNCTION (this);
1210  return m_mean;
1211 }
1212 double
1214 {
1215  NS_LOG_FUNCTION (this);
1216  return m_min;
1217 }
1218 double
1220 {
1221  NS_LOG_FUNCTION (this);
1222  return m_max;
1223 }
1224 
1225 double
1226 TriangularRandomVariable::GetValue (double mean, double min, double max)
1227 {
1228  // Calculate the mode.
1229  NS_LOG_FUNCTION (this << mean << min << max);
1230  double mode = 3.0 * mean - min - max;
1231 
1232  // Get a uniform random variable in [0,1].
1233  double u = Peek ()->RandU01 ();
1234  if (IsAntithetic ())
1235  {
1236  u = (1 - u);
1237  }
1238 
1239  // Calculate the triangular random variable.
1240  if (u <= (mode - min) / (max - min) )
1241  {
1242  return min + std::sqrt (u * (max - min) * (mode - min) );
1243  }
1244  else
1245  {
1246  return max - std::sqrt ( (1 - u) * (max - min) * (max - mode) );
1247  }
1248 }
1249 
1250 uint32_t
1251 TriangularRandomVariable::GetInteger (uint32_t mean, uint32_t min, uint32_t max)
1252 {
1253  NS_LOG_FUNCTION (this << mean << min << max);
1254  return static_cast<uint32_t> ( GetValue (mean, min, max) );
1255 }
1256 
1257 double
1259 {
1260  NS_LOG_FUNCTION (this);
1261  return GetValue (m_mean, m_min, m_max);
1262 }
1263 uint32_t
1265 {
1266  NS_LOG_FUNCTION (this);
1267  return (uint32_t)GetValue (m_mean, m_min, m_max);
1268 }
1269 
1271 
1272 TypeId
1274 {
1275  static TypeId tid = TypeId ("ns3::ZipfRandomVariable")
1277  .SetGroupName ("Core")
1278  .AddConstructor<ZipfRandomVariable> ()
1279  .AddAttribute ("N", "The n value for the Zipf distribution returned by this RNG stream.",
1280  IntegerValue (1),
1282  MakeIntegerChecker<uint32_t>())
1283  .AddAttribute ("Alpha", "The alpha value for the Zipf distribution returned by this RNG stream.",
1284  DoubleValue (0.0),
1286  MakeDoubleChecker<double>())
1287  ;
1288  return tid;
1289 }
1291 {
1292  // m_n and m_alpha are initialized after constructor by attributes
1293  NS_LOG_FUNCTION (this);
1294 }
1295 
1296 uint32_t
1298 {
1299  NS_LOG_FUNCTION (this);
1300  return m_n;
1301 }
1302 double
1304 {
1305  NS_LOG_FUNCTION (this);
1306  return m_alpha;
1307 }
1308 
1309 double
1311 {
1312  NS_LOG_FUNCTION (this << n << alpha);
1313  // Calculate the normalization constant c.
1314  m_c = 0.0;
1315  for (uint32_t i = 1; i <= n; i++)
1316  {
1317  m_c += (1.0 / std::pow ((double)i,alpha));
1318  }
1319  m_c = 1.0 / m_c;
1320 
1321  // Get a uniform random variable in [0,1].
1322  double u = Peek ()->RandU01 ();
1323  if (IsAntithetic ())
1324  {
1325  u = (1 - u);
1326  }
1327 
1328  double sum_prob = 0,zipf_value = 0;
1329  for (uint32_t i = 1; i <= m_n; i++)
1330  {
1331  sum_prob += m_c / std::pow ((double)i,m_alpha);
1332  if (sum_prob > u)
1333  {
1334  zipf_value = i;
1335  break;
1336  }
1337  }
1338  return zipf_value;
1339 }
1340 
1341 uint32_t
1343 {
1344  NS_LOG_FUNCTION (this << n << alpha);
1345  return static_cast<uint32_t> ( GetValue (n, alpha));
1346 }
1347 
1348 double
1350 {
1351  NS_LOG_FUNCTION (this);
1352  return GetValue (m_n, m_alpha);
1353 }
1354 uint32_t
1356 {
1357  NS_LOG_FUNCTION (this);
1358  return (uint32_t)GetValue (m_n, m_alpha);
1359 }
1360 
1362 
1363 TypeId
1365 {
1366  static TypeId tid = TypeId ("ns3::ZetaRandomVariable")
1368  .SetGroupName ("Core")
1369  .AddConstructor<ZetaRandomVariable> ()
1370  .AddAttribute ("Alpha", "The alpha value for the zeta distribution returned by this RNG stream.",
1371  DoubleValue (3.14),
1373  MakeDoubleChecker<double>())
1374  ;
1375  return tid;
1376 }
1378 {
1379  // m_alpha is initialized after constructor by attributes
1380  NS_LOG_FUNCTION (this);
1381 }
1382 
1383 double
1385 {
1386  NS_LOG_FUNCTION (this);
1387  return m_alpha;
1388 }
1389 
1390 double
1392 {
1393  NS_LOG_FUNCTION (this << alpha);
1394  m_b = std::pow (2.0, alpha - 1.0);
1395 
1396  double u, v;
1397  double X, T;
1398  double test;
1399 
1400  do
1401  {
1402  // Get a uniform random variable in [0,1].
1403  u = Peek ()->RandU01 ();
1404  if (IsAntithetic ())
1405  {
1406  u = (1 - u);
1407  }
1408 
1409  // Get a uniform random variable in [0,1].
1410  v = Peek ()->RandU01 ();
1411  if (IsAntithetic ())
1412  {
1413  v = (1 - v);
1414  }
1415 
1416  X = std::floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
1417  T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
1418  test = v * X * (T - 1.0) / (m_b - 1.0);
1419  }
1420  while ( test > (T / m_b) );
1421 
1422  return X;
1423 }
1424 
1425 uint32_t
1427 {
1428  NS_LOG_FUNCTION (this << alpha);
1429  return static_cast<uint32_t> ( GetValue (alpha));
1430 }
1431 
1432 double
1434 {
1435  NS_LOG_FUNCTION (this);
1436  return GetValue (m_alpha);
1437 }
1438 uint32_t
1440 {
1441  NS_LOG_FUNCTION (this);
1442  return (uint32_t)GetValue (m_alpha);
1443 }
1444 
1446 
1447 TypeId
1449 {
1450  static TypeId tid = TypeId ("ns3::DeterministicRandomVariable")
1452  .SetGroupName ("Core")
1453  .AddConstructor<DeterministicRandomVariable> ()
1454  ;
1455  return tid;
1456 }
1458  :
1459  m_count (0),
1460  m_next (0),
1461  m_data (0)
1462 {
1463  NS_LOG_FUNCTION (this);
1464 }
1466 {
1467  // Delete any values currently set.
1468  NS_LOG_FUNCTION (this);
1469  if (m_data != 0)
1470  {
1471  delete[] m_data;
1472  }
1473 }
1474 
1475 void
1476 DeterministicRandomVariable::SetValueArray (double* values, std::size_t length)
1477 {
1478  NS_LOG_FUNCTION (this << values << length);
1479  // Delete any values currently set.
1480  if (m_data != 0)
1481  {
1482  delete[] m_data;
1483  }
1484 
1485  // Make room for the values being set.
1486  m_data = new double[length];
1487  m_count = length;
1488  m_next = length;
1489 
1490  // Copy the values.
1491  for (std::size_t i = 0; i < m_count; i++)
1492  {
1493  m_data[i] = values[i];
1494  }
1495 }
1496 
1497 double
1499 {
1500  NS_LOG_FUNCTION (this);
1501  // Make sure the array has been set.
1502  NS_ASSERT (m_count > 0);
1503 
1504  if (m_next == m_count)
1505  {
1506  m_next = 0;
1507  }
1508  return m_data[m_next++];
1509 }
1510 
1511 uint32_t
1513 {
1514  NS_LOG_FUNCTION (this);
1515  return (uint32_t)GetValue ();
1516 }
1517 
1519 
1520 // ValueCDF methods
1522  : value (0.0),
1523  cdf (0.0)
1524 {
1525  NS_LOG_FUNCTION (this);
1526 }
1527 
1529  : value (v),
1530  cdf (c)
1531 {
1532  NS_LOG_FUNCTION (this << v << c);
1533  NS_ASSERT (c >= 0.0 && c <= 1.0);
1534 }
1535 
1536 bool
1539 {
1540  return a.cdf < b.cdf;
1541 }
1542 
1543 TypeId
1545 {
1546  static TypeId tid = TypeId ("ns3::EmpiricalRandomVariable")
1548  .SetGroupName ("Core")
1549  .AddConstructor<EmpiricalRandomVariable> ()
1550  .AddAttribute ("Interpolate",
1551  "Treat the CDF as a smooth distribution and interpolate, "
1552  "default is to treat the CDF as a histogram and sample.",
1553  BooleanValue (false),
1555  MakeBooleanChecker ())
1556  ;
1557  return tid;
1558 }
1560  : m_validated (false)
1561 {
1562  NS_LOG_FUNCTION (this);
1563 }
1564 
1565 bool
1567 {
1568  NS_LOG_FUNCTION (this << interpolate);
1569  bool prev = m_interpolate;
1570  m_interpolate = interpolate;
1571  return prev;
1572 }
1573 
1574 uint32_t
1576 {
1577  NS_LOG_FUNCTION (this);
1578  return static_cast<uint32_t> (GetValue ());
1579 }
1580 
1581 bool
1583 {
1584  NS_LOG_FUNCTION (this);
1585 
1586  if (!m_validated)
1587  {
1588  Validate ();
1589  }
1590 
1591  // Get a uniform random variable in [0, 1].
1592  double r = Peek ()->RandU01 ();
1593  if (IsAntithetic ())
1594  {
1595  r = (1 - r);
1596  }
1597 
1598  value = r;
1599  bool valid = false;
1600  // check extrema
1601  if (r <= m_emp.front ().cdf)
1602  {
1603  value = m_emp.front ().value; // Less than first
1604  valid = true;
1605  }
1606  else if (r >= m_emp.back ().cdf)
1607  {
1608  value = m_emp.back ().value; // Greater than last
1609  valid = true;
1610  }
1611  return valid;
1612 }
1613 
1614 double
1616 {
1617  NS_LOG_FUNCTION (this);
1618 
1619  double value;
1620  if (PreSample (value))
1621  {
1622  return value;
1623  }
1624 
1625  // value now has the (unused) URNG selector
1626  if (m_interpolate)
1627  {
1628  value = DoInterpolate (value);
1629  }
1630  else
1631  {
1632  value = DoSampleCDF (value);
1633  }
1634  return value;
1635 }
1636 
1637 double
1639 {
1640  NS_LOG_FUNCTION (this << r);
1641 
1642  ValueCDF selector (0, r);
1643  auto bound = std::upper_bound (m_emp.begin (), m_emp.end (), selector);
1644 
1645  return bound->value;
1646 }
1647 
1648 double
1650 {
1651  NS_LOG_FUNCTION (this);
1652 
1653  double value;
1654  if (PreSample (value))
1655  {
1656  return value;
1657  }
1658 
1659  // value now has the (unused) URNG selector
1660  value = DoInterpolate (value);
1661  return value;
1662 }
1663 
1664 double
1666 {
1667  NS_LOG_FUNCTION (this << r);
1668 
1669  // Return a value from the empirical distribution
1670  // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1671 
1672  // search
1673  ValueCDF selector (0, r);
1674  auto upper = std::upper_bound (m_emp.begin (), m_emp.end (), selector);
1675  auto lower = std::prev (upper, 1);
1676  if (upper == m_emp.begin ())
1677  {
1678  lower = upper;
1679  }
1680 
1681  // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1682  double c1 = lower->cdf;
1683  double c2 = upper->cdf;
1684  double v1 = lower->value;
1685  double v2 = upper->value;
1686 
1687  double value = (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1688  return value;
1689 }
1690 
1691 void
1692 EmpiricalRandomVariable::CDF (double v, double c)
1693 {
1694  // Add a new empirical datapoint to the empirical cdf
1695  // NOTE. These MUST be inserted in non-decreasing order
1696  NS_LOG_FUNCTION (this << v << c);
1697  m_emp.push_back (ValueCDF (v, c));
1698 }
1699 
1700 void
1702 {
1703  NS_LOG_FUNCTION (this);
1704  if (m_emp.empty ())
1705  {
1706  NS_FATAL_ERROR ("CDF is not initialized");
1707  }
1708  ValueCDF prior = m_emp[0];
1709  for (auto current : m_emp)
1710  {
1711  if (current.value < prior.value || current.cdf < prior.cdf)
1712  { // Error
1713  std::cerr << "Empirical Dist error,"
1714  << " current value " << current.value
1715  << " prior value " << prior.value
1716  << " current cdf " << current.cdf
1717  << " prior cdf " << prior.cdf << std::endl;
1718  NS_FATAL_ERROR ("Empirical Dist error");
1719  }
1720  prior = current;
1721  }
1722  if (prior.cdf != 1.0)
1723  {
1724  NS_FATAL_ERROR ("CDF does not cover the whole distribution");
1725  }
1726  m_validated = true;
1727 }
1728 
1729 } // 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 an exponential distribution RNG with the default values for the mean and upper bound...
ns3::BooleanValue attribute value declarations.
ns3::DoubleValue attribute value declarations and template implementations.
std::vector< ValueCDF > m_emp
The vector of CDF points.
double GetBound(void) const
Get the configured upper bound of this RNG.
double GetAlpha(void) const
Returns the alpha value for the Zipf distribution returned by this RNG stream.
double m_next
The algorithm produces two values at a time.
double GetMax(void) const
Get the upper bound on values returned by GetValue(void).
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 the RngStream.
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.
double GetMin(void) const
Get the first value of the sequence.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
bool m_validated
true once the CDF has been validated.
double m_alpha
The alpha value for the Zipf distribution 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:45
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
double GetMean(void) const
Returns the mean value for the triangular distribution returned by this RNG stream.
ns3::StringValue 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.
double GetMax(void) const
Returns the upper bound on values that can be returned by this RNG stream.
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:85
The normal (Gaussian) distribution Random Number Generator (RNG) that allows stream numbers to be set...
std::size_t m_count
Size of 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.
double DoInterpolate(double r)
Linear interpolation between two points on the CDF to estimate the value at r.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
virtual double GetValue(void)
Returns a random double from a normal distribution with the current mean, variance, and bound.
double m_min
The lower bound on values that can be returned by this RNG stream.
#define NS_UNUSED(x)
Mark a local variable as unused.
Definition: unused.h:36
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:165
int64_t m_stream
The stream number for the RngStream.
double GetShape(void) const
Returns the shape parameter for the Weibull distribution returned by this RNG stream.
double m_constant
The constant value returned by this RNG stream.
double m_min
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.
void SetValueArray(double *values, std::size_t length)
Sets the array of values that holds the predetermined sequence.
double GetLambda(void) const
Returns the lambda value for the Erlang distribution returned by this RNG stream. ...
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.
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
RngStream * m_rng
Pointer to the underlying RngStream.
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:49
double * m_data
Array of values to return in sequence.
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
double GetMin(void) const
Get the lower bound on randoms returned by GetValue(void).
EmpiricalRandomVariable(void)
Creates an empirical RNG that has a specified, empirical distribution, and configured for interpolati...
double m_lambda
The lambda value for the Erlang distribution returned by this RNG stream.
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:227
Ptr< RandomVariableStream > GetIncrement(void) const
Get the increment for the sequence.
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.
virtual uint32_t GetInteger(void)
Returns a random unsigned integer from a log-normal distribution with the current mu and sigma...
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::RngSeedManager declaration.
double m_max
Strict upper bound on the sequence.
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.
double GetBeta(void) const
Returns the beta value for the gamma distribution returned by this RNG stream.
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.
bool PreSample(double &value)
Do the initial rng draw and check against the extrema.
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 m_scale
The scale parameter for the Pareto distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
double GetAlpha(void) const
Returns the alpha value for the gamma distribution returned by this RNG stream.
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 m_variance
The variance value for the normal distribution returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
bool SetInterpolate(bool interpolate)
Switch the mode between sampling the CDF and interpolating.
double m_mean
The mean value of the unbounded exponential distribution.
int64_t GetStream(void) const
Returns the stream number for the RngStream.
friend bool operator<(ValueCDF a, ValueCDF b)
Comparison operator, for use by std::upper_bound.
double GetScale(void) const
Returns the scale parameter for the Pareto distribution returned by this RNG stream.
double GetScale(void) const
Returns the scale parameter for the Weibull 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.
ns3::RngStream declaration.
double GetMean(void) const
Returns the mean value for the normal distribution returned by this RNG stream.
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.
double m_max
The upper bound on values that can be returned by this RNG stream.
static TypeId GetTypeId(void)
Register this type.
uint32_t m_currentConsecutive
The number of times the current distinct value has been repeated.
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.
double m_beta
The beta value for the gamma distribution returned by this RNG stream.
double GetMin(void) const
Returns the lower bound for the triangular distribution returned by this RNG stream.
double RandU01(void)
Generate the next random number for this stream.
Definition: rng-stream.cc:335
double GetAlpha(void) const
Returns the alpha value for the zeta distribution returned by this RNG stream.
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.
double GetBound(void) const
Returns the upper bound on values that can be returned by this RNG stream.
bool IsAntithetic(void) const
Check if antithetic values will be generated.
virtual uint32_t GetInteger(void)
Returns the next value in the sequence.
double GetConstant(void) const
Get the constant value returned by this RNG stream.
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 GetBound(void) const
Returns the bound on values that can be returned by this RNG stream.
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
double max(double x, double y)
uint32_t GetConsecutive(void) const
Get the number of times each distinct value of the sequence is repeated before incrementing to the ne...
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.
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.
ns3::RandomVariableStream declaration, and related classes.
virtual double GetValue(void)
Returns a random double from a zeta distribution with the current alpha.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual uint32_t GetInteger(void)
Returns the next value in the empirical distribution.
ns3::IntegerValue attribute value declarations and template implementations.
double m_bound
The bound on values that can be 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...
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:73
virtual ~RandomVariableStream()
Destructor.
double GetMax(void) const
Get the limit of the sequence, which is (at least) one more than the last value of the sequence...
double m_next
The algorithm produces two normal values at a time.
double GetVariance(void) const
Returns the variance value for the normal distribution returned by this RNG stream.
double m_b
Just for calculus simplifications.
UniformRandomVariable()
Creates a uniform distribution RNG with the default range.
double min(double x, double y)
double DoSampleCDF(double r)
Sample the CDF as a histogram (without interpolation).
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.
virtual double Interpolate(void)
Returns the next value in the empirical distribution using linear interpolation.
The Erlang distribution Random Number Generator (RNG) that allows stream numbers to be set determinis...
uint32_t GetN(void) const
Returns the n value for the Zipf distribution returned by this RNG stream.
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.
double GetMean(void) const
Get the configured mean value of this RNG.
The Random Number Generator (RNG) that has a specified empirical distribution.
Debug message logging.
uint32_t GetK(void) const
Returns the k value for the Erlang distribution returned by this RNG stream.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
virtual double GetValue(void)
Get the next random value as a double drawn from the distribution.
std::size_t m_next
Position of the next value in the array of values.
double GetMu(void) const
Returns the mu value for the log-normal distribution returned by this RNG stream. ...
virtual double GetValue(void)
Returns a random double from a gamma distribution with the current alpha and beta.
ns3::PointerValue 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:923
ConstantRandomVariable()
Creates a constant RNG with the default constant value.
The Pareto distribution Random Number Generator (RNG).
static TypeId GetTypeId(void)
Register this type.
double m_mu
The mu value for the log-normal distribution returned by this RNG stream.
void Validate(void)
Check that the CDF is valid.
double GetSigma(void) const
Returns the sigma value for the log-normal distribution returned by this RNG stream.
bool m_interpolate
If true GetValue will interpolate, otherwise treat CDF as normal histogram.
double m_min
The lower bound on values that can be returned by this RNG stream.
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.
RngStream * Peek(void) const
Get the pointer to the underlying RngStream.
double GetBound(void) const
Returns the upper bound on values that can be returned by this RNG stream.
bool m_isAntithetic
Indicates if antithetic values should be generated by this RNG stream.
NS_UNUSED and NS_UNUSED_GLOBAL macro definitions.