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  double x2 = mean + m_v2 * m_y * std::sqrt (variance);
709  if (std::fabs (x2 - mean) <= bound)
710  {
711  return x2;
712  }
713  }
714  while (1)
715  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
716  // for algorithm; basically a Box-Muller transform:
717  // http://en.wikipedia.org/wiki/Box-Muller_transform
718  double u1 = Peek ()->RandU01 ();
719  double u2 = Peek ()->RandU01 ();
720  if (IsAntithetic ())
721  {
722  u1 = (1 - u1);
723  u2 = (1 - u2);
724  }
725  double v1 = 2 * u1 - 1;
726  double v2 = 2 * u2 - 1;
727  double w = v1 * v1 + v2 * v2;
728  if (w <= 1.0)
729  { // Got good pair
730  double y = std::sqrt ((-2 * std::log (w)) / w);
731  double x1 = mean + v1 * y * std::sqrt (variance);
732  // if x1 is in bounds, return it, cache v2 and y
733  if (std::fabs (x1 - mean) <= bound)
734  {
735  m_nextValid = true;
736  m_y = y;
737  m_v2 = v2;
738  return x1;
739  }
740  // otherwise try and return the other if it is valid
741  double x2 = mean + v2 * y * std::sqrt (variance);
742  if (std::fabs (x2 - mean) <= bound)
743  {
744  m_nextValid = false;
745  return x2;
746  }
747  // otherwise, just run this loop again
748  }
749  }
750 }
751 
752 uint32_t
753 NormalRandomVariable::GetInteger (uint32_t mean, uint32_t variance, uint32_t bound)
754 {
755  NS_LOG_FUNCTION (this << mean << variance << bound);
756  return static_cast<uint32_t> ( GetValue (mean, variance, bound) );
757 }
758 
759 double
761 {
762  NS_LOG_FUNCTION (this);
763  return GetValue (m_mean, m_variance, m_bound);
764 }
765 uint32_t
767 {
768  NS_LOG_FUNCTION (this);
769  return (uint32_t)GetValue (m_mean, m_variance, m_bound);
770 }
771 
773 
774 TypeId
776 {
777  static TypeId tid = TypeId ("ns3::LogNormalRandomVariable")
779  .SetGroupName ("Core")
780  .AddConstructor<LogNormalRandomVariable> ()
781  .AddAttribute ("Mu", "The mu value for the log-normal distribution returned by this RNG stream.",
782  DoubleValue (0.0),
784  MakeDoubleChecker<double>())
785  .AddAttribute ("Sigma", "The sigma value for the log-normal distribution returned by this RNG stream.",
786  DoubleValue (1.0),
788  MakeDoubleChecker<double>())
789  ;
790  return tid;
791 }
793 {
794  // m_mu and m_sigma are initialized after constructor by
795  // attributes
796  NS_LOG_FUNCTION (this);
797 }
798 
799 double
801 {
802  NS_LOG_FUNCTION (this);
803  return m_mu;
804 }
805 double
807 {
808  NS_LOG_FUNCTION (this);
809  return m_sigma;
810 }
811 
812 // The code from this function was adapted from the GNU Scientific
813 // Library 1.8:
814 /* randist/lognormal.c
815  *
816  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
817  *
818  * This program is free software; you can redistribute it and/or modify
819  * it under the terms of the GNU General Public License as published by
820  * the Free Software Foundation; either version 2 of the License, or (at
821  * your option) any later version.
822  *
823  * This program is distributed in the hope that it will be useful, but
824  * WITHOUT ANY WARRANTY; without even the implied warranty of
825  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
826  * General Public License for more details.
827  *
828  * You should have received a copy of the GNU General Public License
829  * along with this program; if not, write to the Free Software
830  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
831  */
832 /* The lognormal distribution has the form
833 
834  p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
835 
836  for x > 0. Lognormal random numbers are the exponentials of
837  gaussian random numbers */
838 double
839 LogNormalRandomVariable::GetValue (double mu, double sigma)
840 {
841  double v1, v2, r2, normal, x;
842 
843  NS_LOG_FUNCTION (this << mu << sigma);
844 
845  do
846  {
847  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
848 
849  double u1 = Peek ()->RandU01 ();
850  double u2 = Peek ()->RandU01 ();
851  if (IsAntithetic ())
852  {
853  u1 = (1 - u1);
854  u2 = (1 - u2);
855  }
856 
857  v1 = -1 + 2 * u1;
858  v2 = -1 + 2 * u2;
859 
860  /* see if it is in the unit circle */
861  r2 = v1 * v1 + v2 * v2;
862  }
863  while (r2 > 1.0 || r2 == 0);
864 
865  normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
866 
867  x = std::exp (sigma * normal + mu);
868 
869  return x;
870 }
871 
872 uint32_t
873 LogNormalRandomVariable::GetInteger (uint32_t mu, uint32_t sigma)
874 {
875  NS_LOG_FUNCTION (this << mu << sigma);
876  return static_cast<uint32_t> ( GetValue (mu, sigma));
877 }
878 
879 double
881 {
882  NS_LOG_FUNCTION (this);
883  return GetValue (m_mu, m_sigma);
884 }
885 uint32_t
887 {
888  NS_LOG_FUNCTION (this);
889  return (uint32_t)GetValue (m_mu, m_sigma);
890 }
891 
893 
894 TypeId
896 {
897  static TypeId tid = TypeId ("ns3::GammaRandomVariable")
899  .SetGroupName ("Core")
900  .AddConstructor<GammaRandomVariable> ()
901  .AddAttribute ("Alpha", "The alpha value for the gamma distribution returned by this RNG stream.",
902  DoubleValue (1.0),
904  MakeDoubleChecker<double>())
905  .AddAttribute ("Beta", "The beta value for the gamma distribution returned by this RNG stream.",
906  DoubleValue (1.0),
908  MakeDoubleChecker<double>())
909  ;
910  return tid;
911 }
913  :
914  m_nextValid (false)
915 {
916  // m_alpha and m_beta are initialized after constructor by
917  // attributes
918  NS_LOG_FUNCTION (this);
919 }
920 
921 double
923 {
924  NS_LOG_FUNCTION (this);
925  return m_alpha;
926 }
927 double
929 {
930  NS_LOG_FUNCTION (this);
931  return m_beta;
932 }
933 
934 /*
935  The code for the following generator functions was adapted from ns-2
936  tools/ranvar.cc
937 
938  Originally the algorithm was devised by Marsaglia in 2000:
939  G. Marsaglia, W. W. Tsang: A simple method for generating Gamma variables
940  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
941 
942  The Gamma distribution density function has the form
943 
944  x^(alpha-1) * exp(-x/beta)
945  p(x; alpha, beta) = ----------------------------
946  beta^alpha * Gamma(alpha)
947 
948  for x > 0.
949 */
950 double
952 {
953  NS_LOG_FUNCTION (this << alpha << beta);
954  if (alpha < 1)
955  {
956  double u = Peek ()->RandU01 ();
957  if (IsAntithetic ())
958  {
959  u = (1 - u);
960  }
961  return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
962  }
963 
964  double x, v, u;
965  double d = alpha - 1.0 / 3.0;
966  double c = (1.0 / 3.0) / std::sqrt (d);
967 
968  while (1)
969  {
970  do
971  {
972  // Get a value from a normal distribution that has mean
973  // zero, variance 1, and no bound.
974  double mean = 0.0;
975  double variance = 1.0;
977  x = GetNormalValue (mean, variance, bound);
978 
979  v = 1.0 + c * x;
980  }
981  while (v <= 0);
982 
983  v = v * v * v;
984  u = Peek ()->RandU01 ();
985  if (IsAntithetic ())
986  {
987  u = (1 - u);
988  }
989  if (u < 1 - 0.0331 * x * x * x * x)
990  {
991  break;
992  }
993  if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
994  {
995  break;
996  }
997  }
998 
999  return beta * d * v;
1000 }
1001 
1002 uint32_t
1003 GammaRandomVariable::GetInteger (uint32_t alpha, uint32_t beta)
1004 {
1005  NS_LOG_FUNCTION (this << alpha << beta);
1006  return static_cast<uint32_t> ( GetValue (alpha, beta));
1007 }
1008 
1009 double
1011 {
1012  NS_LOG_FUNCTION (this);
1013  return GetValue (m_alpha, m_beta);
1014 }
1015 uint32_t
1017 {
1018  NS_LOG_FUNCTION (this);
1019  return (uint32_t)GetValue (m_alpha, m_beta);
1020 }
1021 
1022 double
1023 GammaRandomVariable::GetNormalValue (double mean, double variance, double bound)
1024 {
1025  NS_LOG_FUNCTION (this << mean << variance << bound);
1026  if (m_nextValid)
1027  { // use previously generated
1028  m_nextValid = false;
1029  double x2 = mean + m_v2 * m_y * std::sqrt (variance);
1030  if (std::fabs (x2 - mean) <= bound)
1031  {
1032  return x2;
1033  }
1034  }
1035  while (1)
1036  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1037  // for algorithm; basically a Box-Muller transform:
1038  // http://en.wikipedia.org/wiki/Box-Muller_transform
1039  double u1 = Peek ()->RandU01 ();
1040  double u2 = Peek ()->RandU01 ();
1041  if (IsAntithetic ())
1042  {
1043  u1 = (1 - u1);
1044  u2 = (1 - u2);
1045  }
1046  double v1 = 2 * u1 - 1;
1047  double v2 = 2 * u2 - 1;
1048  double w = v1 * v1 + v2 * v2;
1049  if (w <= 1.0)
1050  { // Got good pair
1051  double y = std::sqrt ((-2 * std::log (w)) / w);
1052  double x1 = mean + v1 * y * std::sqrt (variance);
1053  // if x1 is in bounds, return it, cache v2 an y
1054  if (std::fabs (x1 - mean) <= bound)
1055  {
1056  m_nextValid = true;
1057  m_y = y;
1058  m_v2 = v2;
1059  return x1;
1060  }
1061  // otherwise try and return the other if it is valid
1062  double x2 = mean + v2 * y * std::sqrt (variance);
1063  if (std::fabs (x2 - mean) <= bound)
1064  {
1065  m_nextValid = false;
1066  return x2;
1067  }
1068  // otherwise, just run this loop again
1069  }
1070  }
1071 }
1072 
1074 
1075 TypeId
1077 {
1078  static TypeId tid = TypeId ("ns3::ErlangRandomVariable")
1080  .SetGroupName ("Core")
1081  .AddConstructor<ErlangRandomVariable> ()
1082  .AddAttribute ("K", "The k value for the Erlang distribution returned by this RNG stream.",
1083  IntegerValue (1),
1085  MakeIntegerChecker<uint32_t>())
1086  .AddAttribute ("Lambda", "The lambda value for the Erlang distribution returned by this RNG stream.",
1087  DoubleValue (1.0),
1089  MakeDoubleChecker<double>())
1090  ;
1091  return tid;
1092 }
1094 {
1095  // m_k and m_lambda are initialized after constructor by attributes
1096  NS_LOG_FUNCTION (this);
1097 }
1098 
1099 uint32_t
1101 {
1102  NS_LOG_FUNCTION (this);
1103  return m_k;
1104 }
1105 double
1107 {
1108  NS_LOG_FUNCTION (this);
1109  return m_lambda;
1110 }
1111 
1112 /*
1113  The code for the following generator functions was adapted from ns-2
1114  tools/ranvar.cc
1115 
1116  The Erlang distribution density function has the form
1117 
1118  x^(k-1) * exp(-x/lambda)
1119  p(x; k, lambda) = ---------------------------
1120  lambda^k * (k-1)!
1121 
1122  for x > 0.
1123 */
1124 double
1125 ErlangRandomVariable::GetValue (uint32_t k, double lambda)
1126 {
1127  NS_LOG_FUNCTION (this << k << lambda);
1128  double mean = lambda;
1129  double bound = 0.0;
1130 
1131  double result = 0;
1132  for (unsigned int i = 0; i < k; ++i)
1133  {
1134  result += GetExponentialValue (mean, bound);
1135 
1136  }
1137 
1138  return result;
1139 }
1140 
1141 uint32_t
1142 ErlangRandomVariable::GetInteger (uint32_t k, uint32_t lambda)
1143 {
1144  NS_LOG_FUNCTION (this << k << lambda);
1145  return static_cast<uint32_t> ( GetValue (k, lambda));
1146 }
1147 
1148 double
1150 {
1151  NS_LOG_FUNCTION (this);
1152  return GetValue (m_k, m_lambda);
1153 }
1154 uint32_t
1156 {
1157  NS_LOG_FUNCTION (this);
1158  return (uint32_t)GetValue (m_k, m_lambda);
1159 }
1160 
1161 double
1163 {
1164  NS_LOG_FUNCTION (this << mean << bound);
1165  while (1)
1166  {
1167  // Get a uniform random variable in [0,1].
1168  double v = Peek ()->RandU01 ();
1169  if (IsAntithetic ())
1170  {
1171  v = (1 - v);
1172  }
1173 
1174  // Calculate the exponential random variable.
1175  double r = -mean*std::log (v);
1176 
1177  // Use this value if it's acceptable.
1178  if (bound == 0 || r <= bound)
1179  {
1180  return r;
1181  }
1182  }
1183 }
1184 
1186 
1187 TypeId
1189 {
1190  static TypeId tid = TypeId ("ns3::TriangularRandomVariable")
1192  .SetGroupName ("Core")
1193  .AddConstructor<TriangularRandomVariable> ()
1194  .AddAttribute ("Mean", "The mean value for the triangular distribution returned by this RNG stream.",
1195  DoubleValue (0.5),
1197  MakeDoubleChecker<double>())
1198  .AddAttribute ("Min", "The lower bound on the values returned by this RNG stream.",
1199  DoubleValue (0.0),
1201  MakeDoubleChecker<double>())
1202  .AddAttribute ("Max", "The upper bound on the values returned by this RNG stream.",
1203  DoubleValue (1.0),
1205  MakeDoubleChecker<double>())
1206  ;
1207  return tid;
1208 }
1210 {
1211  // m_mean, m_min, and m_max are initialized after constructor by
1212  // attributes
1213  NS_LOG_FUNCTION (this);
1214 }
1215 
1216 double
1218 {
1219  NS_LOG_FUNCTION (this);
1220  return m_mean;
1221 }
1222 double
1224 {
1225  NS_LOG_FUNCTION (this);
1226  return m_min;
1227 }
1228 double
1230 {
1231  NS_LOG_FUNCTION (this);
1232  return m_max;
1233 }
1234 
1235 double
1236 TriangularRandomVariable::GetValue (double mean, double min, double max)
1237 {
1238  // Calculate the mode.
1239  NS_LOG_FUNCTION (this << mean << min << max);
1240  double mode = 3.0 * mean - min - max;
1241 
1242  // Get a uniform random variable in [0,1].
1243  double u = Peek ()->RandU01 ();
1244  if (IsAntithetic ())
1245  {
1246  u = (1 - u);
1247  }
1248 
1249  // Calculate the triangular random variable.
1250  if (u <= (mode - min) / (max - min) )
1251  {
1252  return min + std::sqrt (u * (max - min) * (mode - min) );
1253  }
1254  else
1255  {
1256  return max - std::sqrt ( (1 - u) * (max - min) * (max - mode) );
1257  }
1258 }
1259 
1260 uint32_t
1261 TriangularRandomVariable::GetInteger (uint32_t mean, uint32_t min, uint32_t max)
1262 {
1263  NS_LOG_FUNCTION (this << mean << min << max);
1264  return static_cast<uint32_t> ( GetValue (mean, min, max) );
1265 }
1266 
1267 double
1269 {
1270  NS_LOG_FUNCTION (this);
1271  return GetValue (m_mean, m_min, m_max);
1272 }
1273 uint32_t
1275 {
1276  NS_LOG_FUNCTION (this);
1277  return (uint32_t)GetValue (m_mean, m_min, m_max);
1278 }
1279 
1281 
1282 TypeId
1284 {
1285  static TypeId tid = TypeId ("ns3::ZipfRandomVariable")
1287  .SetGroupName ("Core")
1288  .AddConstructor<ZipfRandomVariable> ()
1289  .AddAttribute ("N", "The n value for the Zipf distribution returned by this RNG stream.",
1290  IntegerValue (1),
1292  MakeIntegerChecker<uint32_t>())
1293  .AddAttribute ("Alpha", "The alpha value for the Zipf distribution returned by this RNG stream.",
1294  DoubleValue (0.0),
1296  MakeDoubleChecker<double>())
1297  ;
1298  return tid;
1299 }
1301 {
1302  // m_n and m_alpha are initialized after constructor by attributes
1303  NS_LOG_FUNCTION (this);
1304 }
1305 
1306 uint32_t
1308 {
1309  NS_LOG_FUNCTION (this);
1310  return m_n;
1311 }
1312 double
1314 {
1315  NS_LOG_FUNCTION (this);
1316  return m_alpha;
1317 }
1318 
1319 double
1321 {
1322  NS_LOG_FUNCTION (this << n << alpha);
1323  // Calculate the normalization constant c.
1324  m_c = 0.0;
1325  for (uint32_t i = 1; i <= n; i++)
1326  {
1327  m_c += (1.0 / std::pow ((double)i,alpha));
1328  }
1329  m_c = 1.0 / m_c;
1330 
1331  // Get a uniform random variable in [0,1].
1332  double u = Peek ()->RandU01 ();
1333  if (IsAntithetic ())
1334  {
1335  u = (1 - u);
1336  }
1337 
1338  double sum_prob = 0,zipf_value = 0;
1339  for (uint32_t i = 1; i <= m_n; i++)
1340  {
1341  sum_prob += m_c / std::pow ((double)i,m_alpha);
1342  if (sum_prob > u)
1343  {
1344  zipf_value = i;
1345  break;
1346  }
1347  }
1348  return zipf_value;
1349 }
1350 
1351 uint32_t
1353 {
1354  NS_LOG_FUNCTION (this << n << alpha);
1355  return static_cast<uint32_t> ( GetValue (n, alpha));
1356 }
1357 
1358 double
1360 {
1361  NS_LOG_FUNCTION (this);
1362  return GetValue (m_n, m_alpha);
1363 }
1364 uint32_t
1366 {
1367  NS_LOG_FUNCTION (this);
1368  return (uint32_t)GetValue (m_n, m_alpha);
1369 }
1370 
1372 
1373 TypeId
1375 {
1376  static TypeId tid = TypeId ("ns3::ZetaRandomVariable")
1378  .SetGroupName ("Core")
1379  .AddConstructor<ZetaRandomVariable> ()
1380  .AddAttribute ("Alpha", "The alpha value for the zeta distribution returned by this RNG stream.",
1381  DoubleValue (3.14),
1383  MakeDoubleChecker<double>())
1384  ;
1385  return tid;
1386 }
1388 {
1389  // m_alpha is initialized after constructor by attributes
1390  NS_LOG_FUNCTION (this);
1391 }
1392 
1393 double
1395 {
1396  NS_LOG_FUNCTION (this);
1397  return m_alpha;
1398 }
1399 
1400 double
1402 {
1403  NS_LOG_FUNCTION (this << alpha);
1404  m_b = std::pow (2.0, alpha - 1.0);
1405 
1406  double u, v;
1407  double X, T;
1408  double test;
1409 
1410  do
1411  {
1412  // Get a uniform random variable in [0,1].
1413  u = Peek ()->RandU01 ();
1414  if (IsAntithetic ())
1415  {
1416  u = (1 - u);
1417  }
1418 
1419  // Get a uniform random variable in [0,1].
1420  v = Peek ()->RandU01 ();
1421  if (IsAntithetic ())
1422  {
1423  v = (1 - v);
1424  }
1425 
1426  X = std::floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
1427  T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
1428  test = v * X * (T - 1.0) / (m_b - 1.0);
1429  }
1430  while ( test > (T / m_b) );
1431 
1432  return X;
1433 }
1434 
1435 uint32_t
1437 {
1438  NS_LOG_FUNCTION (this << alpha);
1439  return static_cast<uint32_t> ( GetValue (alpha));
1440 }
1441 
1442 double
1444 {
1445  NS_LOG_FUNCTION (this);
1446  return GetValue (m_alpha);
1447 }
1448 uint32_t
1450 {
1451  NS_LOG_FUNCTION (this);
1452  return (uint32_t)GetValue (m_alpha);
1453 }
1454 
1456 
1457 TypeId
1459 {
1460  static TypeId tid = TypeId ("ns3::DeterministicRandomVariable")
1462  .SetGroupName ("Core")
1463  .AddConstructor<DeterministicRandomVariable> ()
1464  ;
1465  return tid;
1466 }
1468  :
1469  m_count (0),
1470  m_next (0),
1471  m_data (0)
1472 {
1473  NS_LOG_FUNCTION (this);
1474 }
1476 {
1477  // Delete any values currently set.
1478  NS_LOG_FUNCTION (this);
1479  if (m_data != 0)
1480  {
1481  delete[] m_data;
1482  }
1483 }
1484 
1485 void
1486 DeterministicRandomVariable::SetValueArray (double* values, std::size_t length)
1487 {
1488  NS_LOG_FUNCTION (this << values << length);
1489  // Delete any values currently set.
1490  if (m_data != 0)
1491  {
1492  delete[] m_data;
1493  }
1494 
1495  // Make room for the values being set.
1496  m_data = new double[length];
1497  m_count = length;
1498  m_next = length;
1499 
1500  // Copy the values.
1501  for (std::size_t i = 0; i < m_count; i++)
1502  {
1503  m_data[i] = values[i];
1504  }
1505 }
1506 
1507 double
1509 {
1510  NS_LOG_FUNCTION (this);
1511  // Make sure the array has been set.
1512  NS_ASSERT (m_count > 0);
1513 
1514  if (m_next == m_count)
1515  {
1516  m_next = 0;
1517  }
1518  return m_data[m_next++];
1519 }
1520 
1521 uint32_t
1523 {
1524  NS_LOG_FUNCTION (this);
1525  return (uint32_t)GetValue ();
1526 }
1527 
1529 
1530 // ValueCDF methods
1532  : value (0.0),
1533  cdf (0.0)
1534 {
1535  NS_LOG_FUNCTION (this);
1536 }
1537 
1539  : value (v),
1540  cdf (c)
1541 {
1542  NS_LOG_FUNCTION (this << v << c);
1543  NS_ASSERT (c >= 0.0 && c <= 1.0);
1544 }
1545 
1546 bool
1549 {
1550  return a.cdf < b.cdf;
1551 }
1552 
1553 TypeId
1555 {
1556  static TypeId tid = TypeId ("ns3::EmpiricalRandomVariable")
1558  .SetGroupName ("Core")
1559  .AddConstructor<EmpiricalRandomVariable> ()
1560  .AddAttribute ("Interpolate",
1561  "Treat the CDF as a smooth distribution and interpolate, "
1562  "default is to treat the CDF as a histogram and sample.",
1563  BooleanValue (false),
1565  MakeBooleanChecker ())
1566  ;
1567  return tid;
1568 }
1570  : m_validated (false)
1571 {
1572  NS_LOG_FUNCTION (this);
1573 }
1574 
1575 bool
1577 {
1578  NS_LOG_FUNCTION (this << interpolate);
1579  bool prev = m_interpolate;
1580  m_interpolate = interpolate;
1581  return prev;
1582 }
1583 
1584 uint32_t
1586 {
1587  NS_LOG_FUNCTION (this);
1588  return static_cast<uint32_t> (GetValue ());
1589 }
1590 
1591 bool
1593 {
1594  NS_LOG_FUNCTION (this);
1595 
1596  if (!m_validated)
1597  {
1598  Validate ();
1599  }
1600 
1601  // Get a uniform random variable in [0, 1].
1602  double r = Peek ()->RandU01 ();
1603  if (IsAntithetic ())
1604  {
1605  r = (1 - r);
1606  }
1607 
1608  value = r;
1609  bool valid = false;
1610  // check extrema
1611  if (r <= m_emp.front ().cdf)
1612  {
1613  value = m_emp.front ().value; // Less than first
1614  valid = true;
1615  }
1616  else if (r >= m_emp.back ().cdf)
1617  {
1618  value = m_emp.back ().value; // Greater than last
1619  valid = true;
1620  }
1621  return valid;
1622 }
1623 
1624 double
1626 {
1627  NS_LOG_FUNCTION (this);
1628 
1629  double value;
1630  if (PreSample (value))
1631  {
1632  return value;
1633  }
1634 
1635  // value now has the (unused) URNG selector
1636  if (m_interpolate)
1637  {
1638  value = DoInterpolate (value);
1639  }
1640  else
1641  {
1642  value = DoSampleCDF (value);
1643  }
1644  return value;
1645 }
1646 
1647 double
1649 {
1650  NS_LOG_FUNCTION (this << r);
1651 
1652  ValueCDF selector (0, r);
1653  auto bound = std::upper_bound (m_emp.begin (), m_emp.end (), selector);
1654 
1655  return bound->value;
1656 }
1657 
1658 double
1660 {
1661  NS_LOG_FUNCTION (this);
1662 
1663  double value;
1664  if (PreSample (value))
1665  {
1666  return value;
1667  }
1668 
1669  // value now has the (unused) URNG selector
1670  value = DoInterpolate (value);
1671  return value;
1672 }
1673 
1674 double
1676 {
1677  NS_LOG_FUNCTION (this << r);
1678 
1679  // Return a value from the empirical distribution
1680  // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1681 
1682  // search
1683  ValueCDF selector (0, r);
1684  auto upper = std::upper_bound (m_emp.begin (), m_emp.end (), selector);
1685  auto lower = std::prev (upper, 1);
1686  if (upper == m_emp.begin ())
1687  {
1688  lower = upper;
1689  }
1690 
1691  // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1692  double c1 = lower->cdf;
1693  double c2 = upper->cdf;
1694  double v1 = lower->value;
1695  double v2 = upper->value;
1696 
1697  double value = (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1698  return value;
1699 }
1700 
1701 void
1702 EmpiricalRandomVariable::CDF (double v, double c)
1703 {
1704  // Add a new empirical datapoint to the empirical cdf
1705  // NOTE. These MUST be inserted in non-decreasing order
1706  NS_LOG_FUNCTION (this << v << c);
1707  m_emp.push_back (ValueCDF (v, c));
1708 }
1709 
1710 void
1712 {
1713  NS_LOG_FUNCTION (this);
1714  if (m_emp.empty ())
1715  {
1716  NS_FATAL_ERROR ("CDF is not initialized");
1717  }
1718  ValueCDF prior = m_emp[0];
1719  for (auto current : m_emp)
1720  {
1721  if (current.value < prior.value || current.cdf < prior.cdf)
1722  { // Error
1723  std::cerr << "Empirical Dist error,"
1724  << " current value " << current.value
1725  << " prior value " << prior.value
1726  << " current cdf " << current.cdf
1727  << " prior cdf " << prior.cdf << std::endl;
1728  NS_FATAL_ERROR ("Empirical Dist error");
1729  }
1730  prior = current;
1731  }
1732  if (prior.cdf != 1.0)
1733  {
1734  NS_FATAL_ERROR ("CDF does not cover the whole distribution");
1735  }
1736  m_validated = true;
1737 }
1738 
1739 } // 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_y
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.
uint32_t prev
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.
double m_y
The algorithm produces two values at a time.
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_v2
The algorithm produces two values at a time.
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.
double m_v2
The algorithm produces two values at a time.
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 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.