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 
46 namespace ns3 {
47 
48 NS_LOG_COMPONENT_DEFINE ("RandomVariableStream");
49 
50 NS_OBJECT_ENSURE_REGISTERED (RandomVariableStream);
51 
52 TypeId
54 {
55  static TypeId tid = TypeId ("ns3::RandomVariableStream")
56  .SetParent<Object> ()
57  .SetGroupName ("Core")
58  .AddAttribute("Stream",
59  "The stream number for this RNG stream. -1 means \"allocate a stream automatically\". "
60  "Note that if -1 is set, Get will return -1 so that it is not possible to know which "
61  "value was automatically allocated.",
62  IntegerValue(-1),
65  MakeIntegerChecker<int64_t>())
66  .AddAttribute("Antithetic", "Set this RNG stream to generate antithetic values",
67  BooleanValue (false),
71  ;
72  return tid;
73 }
74 
76  : m_rng (0)
77 {
78  NS_LOG_FUNCTION (this);
79 }
81 {
82  NS_LOG_FUNCTION (this);
83  delete m_rng;
84 }
85 
86 void
88 {
89  NS_LOG_FUNCTION (this << isAntithetic);
90  m_isAntithetic = isAntithetic;
91 }
92 bool
94 {
95  NS_LOG_FUNCTION (this);
96  return m_isAntithetic;
97 }
98 void
100 {
101  NS_LOG_FUNCTION (this << stream);
102  // negative values are not legal.
103  NS_ASSERT (stream >= -1);
104  delete m_rng;
105  if (stream == -1)
106  {
107  // The first 2^63 streams are reserved for automatic stream
108  // number assignment.
109  uint64_t nextStream = RngSeedManager::GetNextStreamIndex ();
110  NS_ASSERT(nextStream <= ((1ULL)<<63));
112  nextStream,
114  }
115  else
116  {
117  // The last 2^63 streams are reserved for deterministic stream
118  // number assignment.
119  uint64_t base = ((1ULL)<<63);
120  uint64_t target = base + stream;
122  target,
124  }
125  m_stream = stream;
126 }
127 int64_t
129 {
130  NS_LOG_FUNCTION (this);
131  return m_stream;
132 }
133 
134 RngStream *
136 {
137  NS_LOG_FUNCTION (this);
138  return m_rng;
139 }
140 
142 
143 TypeId
145 {
146  static TypeId tid = TypeId ("ns3::UniformRandomVariable")
148  .SetGroupName ("Core")
149  .AddConstructor<UniformRandomVariable> ()
150  .AddAttribute("Min", "The lower bound on the values returned by this RNG stream.",
151  DoubleValue(0),
152  MakeDoubleAccessor(&UniformRandomVariable::m_min),
153  MakeDoubleChecker<double>())
154  .AddAttribute("Max", "The upper bound on the values returned by this RNG stream.",
155  DoubleValue(1.0),
156  MakeDoubleAccessor(&UniformRandomVariable::m_max),
157  MakeDoubleChecker<double>())
158  ;
159  return tid;
160 }
162 {
163  // m_min and m_max are initialized after constructor by attributes
164  NS_LOG_FUNCTION (this);
165 }
166 
167 double
169 {
170  NS_LOG_FUNCTION (this);
171  return m_min;
172 }
173 double
175 {
176  NS_LOG_FUNCTION (this);
177  return m_max;
178 }
179 
180 double
182 {
183  NS_LOG_FUNCTION (this << min << max);
184  double v = min + Peek ()->RandU01 () * (max - min);
185  if (IsAntithetic ())
186  {
187  v = min + (max - v);
188  }
189  return v;
190 }
191 uint32_t
192 UniformRandomVariable::GetInteger (uint32_t min, uint32_t max)
193 {
194  NS_LOG_FUNCTION (this << min << max);
195  NS_ASSERT (min <= max);
196  return static_cast<uint32_t> ( GetValue ((double) (min), (double) (max) + 1.0) );
197 }
198 
199 double
201 {
202  NS_LOG_FUNCTION (this);
203  return GetValue (m_min, m_max);
204 }
205 uint32_t
207 {
208  NS_LOG_FUNCTION (this);
209  return (uint32_t)GetValue (m_min, m_max + 1);
210 }
211 
212 NS_OBJECT_ENSURE_REGISTERED(ConstantRandomVariable);
213 
214 TypeId
216 {
217  static TypeId tid = TypeId ("ns3::ConstantRandomVariable")
219  .SetGroupName ("Core")
220  .AddConstructor<ConstantRandomVariable> ()
221  .AddAttribute("Constant", "The constant value returned by this RNG stream.",
222  DoubleValue(0),
224  MakeDoubleChecker<double>())
225  ;
226  return tid;
227 }
229 {
230  // m_constant is initialized after constructor by attributes
231  NS_LOG_FUNCTION (this);
232 }
233 
234 double
236 {
237  NS_LOG_FUNCTION (this);
238  return m_constant;
239 }
240 
241 double
243 {
244  NS_LOG_FUNCTION (this << constant);
245  return constant;
246 }
247 uint32_t
249 {
250  NS_LOG_FUNCTION (this << constant);
251  return constant;
252 }
253 
254 double
256 {
257  NS_LOG_FUNCTION (this);
258  return GetValue (m_constant);
259 }
260 uint32_t
262 {
263  NS_LOG_FUNCTION (this);
264  return (uint32_t)GetValue (m_constant);
265 }
266 
268 
269 TypeId
271 {
272  static TypeId tid = TypeId ("ns3::SequentialRandomVariable")
274  .SetGroupName ("Core")
275  .AddConstructor<SequentialRandomVariable> ()
276  .AddAttribute("Min", "The first value of the sequence.",
277  DoubleValue(0),
279  MakeDoubleChecker<double>())
280  .AddAttribute("Max", "One more than the last value of the sequence.",
281  DoubleValue(0),
283  MakeDoubleChecker<double>())
284  .AddAttribute("Increment", "The sequence random variable increment.",
285  StringValue("ns3::ConstantRandomVariable[Constant=1]"),
287  MakePointerChecker<RandomVariableStream> ())
288  .AddAttribute("Consecutive", "The number of times each member of the sequence is repeated.",
289  IntegerValue(1),
291  MakeIntegerChecker<uint32_t>());
292  ;
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 
492  double mean = std::numeric_limits<double>::infinity();
493 
494  if (m_shape > 1)
495  {
496  mean = m_shape * m_scale / (m_shape -1);
497  }
498 
499  return mean;
500 }
501 
502 double
504 {
505  NS_LOG_FUNCTION (this);
506  return m_scale;
507 }
508 
509 double
511 {
512  NS_LOG_FUNCTION (this);
513  return m_shape;
514 }
515 
516 double
518 {
519  NS_LOG_FUNCTION (this);
520  return m_bound;
521 }
522 
523 double
524 ParetoRandomVariable::GetValue (double scale, double shape, double bound)
525 {
526  // Calculate the scale parameter.
527  NS_LOG_FUNCTION (this << scale << shape << bound);
528 
529  while (1)
530  {
531  // Get a uniform random variable in [0,1].
532  double v = Peek ()->RandU01 ();
533  if (IsAntithetic ())
534  {
535  v = (1 - v);
536  }
537 
538  // Calculate the Pareto random variable.
539  double r = (scale * ( 1.0 / std::pow (v, 1.0 / shape)));
540 
541  // Use this value if it's acceptable.
542  if (bound == 0 || r <= bound)
543  {
544  return r;
545  }
546  }
547 }
548 uint32_t
549 ParetoRandomVariable::GetInteger (uint32_t scale, uint32_t shape, uint32_t bound)
550 {
551  NS_LOG_FUNCTION (this << scale << shape << bound);
552  return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
553 }
554 
555 double
557 {
558  NS_LOG_FUNCTION (this);
559  return GetValue (m_scale, m_shape, m_bound);
560 }
561 uint32_t
563 {
564  NS_LOG_FUNCTION (this);
565  return (uint32_t)GetValue (m_scale, m_shape, m_bound);
566 }
567 
569 
570 TypeId
572 {
573  static TypeId tid = TypeId ("ns3::WeibullRandomVariable")
575  .SetGroupName ("Core")
576  .AddConstructor<WeibullRandomVariable> ()
577  .AddAttribute("Scale", "The scale parameter for the Weibull distribution returned by this RNG stream.",
578  DoubleValue(1.0),
580  MakeDoubleChecker<double>())
581  .AddAttribute("Shape", "The shape parameter for the Weibull distribution returned by this RNG stream.",
582  DoubleValue(1),
584  MakeDoubleChecker<double>())
585  .AddAttribute("Bound", "The upper bound on the values returned by this RNG stream.",
586  DoubleValue(0.0),
588  MakeDoubleChecker<double>())
589  ;
590  return tid;
591 }
593 {
594  // m_scale, m_shape, and m_bound are initialized after constructor
595  // by attributes
596  NS_LOG_FUNCTION (this);
597 }
598 
599 double
601 {
602  NS_LOG_FUNCTION (this);
603  return m_scale;
604 }
605 double
607 {
608  NS_LOG_FUNCTION (this);
609  return m_shape;
610 }
611 double
613 {
614  NS_LOG_FUNCTION (this);
615  return m_bound;
616 }
617 
618 double
619 WeibullRandomVariable::GetValue (double scale, double shape, double bound)
620 {
621  NS_LOG_FUNCTION (this << scale << shape << bound);
622  double exponent = 1.0 / shape;
623  while (1)
624  {
625  // Get a uniform random variable in [0,1].
626  double v = Peek ()->RandU01 ();
627  if (IsAntithetic ())
628  {
629  v = (1 - v);
630  }
631 
632  // Calculate the Weibull random variable.
633  double r = scale * std::pow ( -std::log (v), exponent);
634 
635  // Use this value if it's acceptable.
636  if (bound == 0 || r <= bound)
637  {
638  return r;
639  }
640  }
641 }
642 uint32_t
643 WeibullRandomVariable::GetInteger (uint32_t scale, uint32_t shape, uint32_t bound)
644 {
645  NS_LOG_FUNCTION (this << scale << shape << bound);
646  return static_cast<uint32_t> ( GetValue (scale, shape, bound) );
647 }
648 
649 double
651 {
652  NS_LOG_FUNCTION (this);
653  return GetValue (m_scale, m_shape, m_bound);
654 }
655 uint32_t
657 {
658  NS_LOG_FUNCTION (this);
659  return (uint32_t)GetValue (m_scale, m_shape, m_bound);
660 }
661 
663 
664 const double NormalRandomVariable::INFINITE_VALUE = 1e307;
665 
666 TypeId
668 {
669  static TypeId tid = TypeId ("ns3::NormalRandomVariable")
671  .SetGroupName ("Core")
672  .AddConstructor<NormalRandomVariable> ()
673  .AddAttribute("Mean", "The mean value for the normal distribution returned by this RNG stream.",
674  DoubleValue(0.0),
676  MakeDoubleChecker<double>())
677  .AddAttribute("Variance", "The variance value for the normal distribution returned by this RNG stream.",
678  DoubleValue(1.0),
680  MakeDoubleChecker<double>())
681  .AddAttribute("Bound", "The bound on the values returned by this RNG stream.",
684  MakeDoubleChecker<double>())
685  ;
686  return tid;
687 }
689  :
690  m_nextValid (false)
691 {
692  // m_mean, m_variance, and m_bound are initialized after constructor
693  // by attributes
694  NS_LOG_FUNCTION (this);
695 }
696 
697 double
699 {
700  NS_LOG_FUNCTION (this);
701  return m_mean;
702 }
703 double
705 {
706  NS_LOG_FUNCTION (this);
707  return m_variance;
708 }
709 double
711 {
712  NS_LOG_FUNCTION (this);
713  return m_bound;
714 }
715 
716 double
717 NormalRandomVariable::GetValue (double mean, double variance, double bound)
718 {
719  NS_LOG_FUNCTION (this << mean << variance << bound);
720  if (m_nextValid)
721  { // use previously generated
722  m_nextValid = false;
723  return m_next;
724  }
725  while (1)
726  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
727  // for algorithm; basically a Box-Muller transform:
728  // http://en.wikipedia.org/wiki/Box-Muller_transform
729  double u1 = Peek ()->RandU01 ();
730  double u2 = Peek ()->RandU01 ();
731  if (IsAntithetic ())
732  {
733  u1 = (1 - u1);
734  u2 = (1 - u2);
735  }
736  double v1 = 2 * u1 - 1;
737  double v2 = 2 * u2 - 1;
738  double w = v1 * v1 + v2 * v2;
739  if (w <= 1.0)
740  { // Got good pair
741  double y = std::sqrt ((-2 * std::log (w)) / w);
742  m_next = mean + v2 * y * std::sqrt (variance);
743  // if next is in bounds, it is valid
744  m_nextValid = std::fabs (m_next - mean) <= bound;
745  double x1 = mean + v1 * y * std::sqrt (variance);
746  // if x1 is in bounds, return it
747  if (std::fabs (x1 - mean) <= bound)
748  {
749  return x1;
750  }
751  // otherwise try and return m_next if it is valid
752  else if (m_nextValid)
753  {
754  m_nextValid = false;
755  return m_next;
756  }
757  // otherwise, just run this loop again
758  }
759  }
760 }
761 
762 uint32_t
763 NormalRandomVariable::GetInteger (uint32_t mean, uint32_t variance, uint32_t bound)
764 {
765  NS_LOG_FUNCTION (this << mean << variance << bound);
766  return static_cast<uint32_t> ( GetValue (mean, variance, bound) );
767 }
768 
769 double
771 {
772  NS_LOG_FUNCTION (this);
773  return GetValue (m_mean, m_variance, m_bound);
774 }
775 uint32_t
777 {
778  NS_LOG_FUNCTION (this);
779  return (uint32_t)GetValue (m_mean, m_variance, m_bound);
780 }
781 
783 
784 TypeId
786 {
787  static TypeId tid = TypeId ("ns3::LogNormalRandomVariable")
789  .SetGroupName ("Core")
790  .AddConstructor<LogNormalRandomVariable> ()
791  .AddAttribute("Mu", "The mu value for the log-normal distribution returned by this RNG stream.",
792  DoubleValue(0.0),
794  MakeDoubleChecker<double>())
795  .AddAttribute("Sigma", "The sigma value for the log-normal distribution returned by this RNG stream.",
796  DoubleValue(1.0),
798  MakeDoubleChecker<double>())
799  ;
800  return tid;
801 }
803 {
804  // m_mu and m_sigma are initialized after constructor by
805  // attributes
806  NS_LOG_FUNCTION (this);
807 }
808 
809 double
811 {
812  NS_LOG_FUNCTION (this);
813  return m_mu;
814 }
815 double
817 {
818  NS_LOG_FUNCTION (this);
819  return m_sigma;
820 }
821 
822 // The code from this function was adapted from the GNU Scientific
823 // Library 1.8:
824 /* randist/lognormal.c
825  *
826  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
827  *
828  * This program is free software; you can redistribute it and/or modify
829  * it under the terms of the GNU General Public License as published by
830  * the Free Software Foundation; either version 2 of the License, or (at
831  * your option) any later version.
832  *
833  * This program is distributed in the hope that it will be useful, but
834  * WITHOUT ANY WARRANTY; without even the implied warranty of
835  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
836  * General Public License for more details.
837  *
838  * You should have received a copy of the GNU General Public License
839  * along with this program; if not, write to the Free Software
840  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
841  */
842 /* The lognormal distribution has the form
843 
844  p(x) dx = 1/(x * sqrt(2 pi sigma^2)) exp(-(ln(x) - zeta)^2/2 sigma^2) dx
845 
846  for x > 0. Lognormal random numbers are the exponentials of
847  gaussian random numbers */
848 double
849 LogNormalRandomVariable::GetValue (double mu, double sigma)
850 {
851  double v1, v2, r2, normal, x;
852 
853  NS_LOG_FUNCTION (this << mu << sigma);
854 
855  do
856  {
857  /* choose x,y in uniform square (-1,-1) to (+1,+1) */
858 
859  double u1 = Peek ()->RandU01 ();
860  double u2 = Peek ()->RandU01 ();
861  if (IsAntithetic ())
862  {
863  u1 = (1 - u1);
864  u2 = (1 - u2);
865  }
866 
867  v1 = -1 + 2 * u1;
868  v2 = -1 + 2 * u2;
869 
870  /* see if it is in the unit circle */
871  r2 = v1 * v1 + v2 * v2;
872  }
873  while (r2 > 1.0 || r2 == 0);
874 
875  normal = v1 * std::sqrt (-2.0 * std::log (r2) / r2);
876 
877  x = std::exp (sigma * normal + mu);
878 
879  return x;
880 }
881 
882 uint32_t
883 LogNormalRandomVariable::GetInteger (uint32_t mu, uint32_t sigma)
884 {
885  NS_LOG_FUNCTION (this << mu << sigma);
886  return static_cast<uint32_t> ( GetValue (mu, sigma));
887 }
888 
889 double
891 {
892  NS_LOG_FUNCTION (this);
893  return GetValue (m_mu, m_sigma);
894 }
895 uint32_t
897 {
898  NS_LOG_FUNCTION (this);
899  return (uint32_t)GetValue (m_mu, m_sigma);
900 }
901 
903 
904 TypeId
906 {
907  static TypeId tid = TypeId ("ns3::GammaRandomVariable")
909  .SetGroupName ("Core")
910  .AddConstructor<GammaRandomVariable> ()
911  .AddAttribute("Alpha", "The alpha value for the gamma distribution returned by this RNG stream.",
912  DoubleValue(1.0),
914  MakeDoubleChecker<double>())
915  .AddAttribute("Beta", "The beta value for the gamma distribution returned by this RNG stream.",
916  DoubleValue(1.0),
918  MakeDoubleChecker<double>())
919  ;
920  return tid;
921 }
923  :
924  m_nextValid (false)
925 {
926  // m_alpha and m_beta are initialized after constructor by
927  // attributes
928  NS_LOG_FUNCTION (this);
929 }
930 
931 double
933 {
934  NS_LOG_FUNCTION (this);
935  return m_alpha;
936 }
937 double
939 {
940  NS_LOG_FUNCTION (this);
941  return m_beta;
942 }
943 
944 /*
945  The code for the following generator functions was adapted from ns-2
946  tools/ranvar.cc
947 
948  Originally the algorithm was devised by Marsaglia in 2000:
949  G. Marsaglia, W. W. Tsang: A simple method for gereating Gamma variables
950  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
951 
952  The Gamma distribution density function has the form
953 
954  x^(alpha-1) * exp(-x/beta)
955  p(x; alpha, beta) = ----------------------------
956  beta^alpha * Gamma(alpha)
957 
958  for x > 0.
959 */
960 double
962 {
963  NS_LOG_FUNCTION (this << alpha << beta);
964  if (alpha < 1)
965  {
966  double u = Peek ()->RandU01 ();
967  if (IsAntithetic ())
968  {
969  u = (1 - u);
970  }
971  return GetValue (1.0 + alpha, beta) * std::pow (u, 1.0 / alpha);
972  }
973 
974  double x, v, u;
975  double d = alpha - 1.0 / 3.0;
976  double c = (1.0 / 3.0) / std::sqrt (d);
977 
978  while (1)
979  {
980  do
981  {
982  // Get a value from a normal distribution that has mean
983  // zero, variance 1, and no bound.
984  double mean = 0.0;
985  double variance = 1.0;
987  x = GetNormalValue (mean, variance, bound);
988 
989  v = 1.0 + c * x;
990  }
991  while (v <= 0);
992 
993  v = v * v * v;
994  u = Peek ()->RandU01 ();
995  if (IsAntithetic ())
996  {
997  u = (1 - u);
998  }
999  if (u < 1 - 0.0331 * x * x * x * x)
1000  {
1001  break;
1002  }
1003  if (std::log (u) < 0.5 * x * x + d * (1 - v + std::log (v)))
1004  {
1005  break;
1006  }
1007  }
1008 
1009  return beta * d * v;
1010 }
1011 
1012 uint32_t
1013 GammaRandomVariable::GetInteger (uint32_t alpha, uint32_t beta)
1014 {
1015  NS_LOG_FUNCTION (this << alpha << beta);
1016  return static_cast<uint32_t> ( GetValue (alpha, beta));
1017 }
1018 
1019 double
1021 {
1022  NS_LOG_FUNCTION (this);
1023  return GetValue (m_alpha, m_beta);
1024 }
1025 uint32_t
1027 {
1028  NS_LOG_FUNCTION (this);
1029  return (uint32_t)GetValue (m_alpha, m_beta);
1030 }
1031 
1032 double
1033 GammaRandomVariable::GetNormalValue (double mean, double variance, double bound)
1034 {
1035  NS_LOG_FUNCTION (this << mean << variance << bound);
1036  if (m_nextValid)
1037  { // use previously generated
1038  m_nextValid = false;
1039  return m_next;
1040  }
1041  while (1)
1042  { // See Simulation Modeling and Analysis p. 466 (Averill Law)
1043  // for algorithm; basically a Box-Muller transform:
1044  // http://en.wikipedia.org/wiki/Box-Muller_transform
1045  double u1 = Peek ()->RandU01 ();
1046  double u2 = Peek ()->RandU01 ();
1047  if (IsAntithetic ())
1048  {
1049  u1 = (1 - u1);
1050  u2 = (1 - u2);
1051  }
1052  double v1 = 2 * u1 - 1;
1053  double v2 = 2 * u2 - 1;
1054  double w = v1 * v1 + v2 * v2;
1055  if (w <= 1.0)
1056  { // Got good pair
1057  double y = std::sqrt ((-2 * std::log (w)) / w);
1058  m_next = mean + v2 * y * std::sqrt (variance);
1059  // if next is in bounds, it is valid
1060  m_nextValid = std::fabs (m_next - mean) <= bound;
1061  double x1 = mean + v1 * y * std::sqrt (variance);
1062  // if x1 is in bounds, return it
1063  if (std::fabs (x1 - mean) <= bound)
1064  {
1065  return x1;
1066  }
1067  // otherwise try and return m_next if it is valid
1068  else if (m_nextValid)
1069  {
1070  m_nextValid = false;
1071  return m_next;
1072  }
1073  // otherwise, just run this loop again
1074  }
1075  }
1076 }
1077 
1079 
1080 TypeId
1082 {
1083  static TypeId tid = TypeId ("ns3::ErlangRandomVariable")
1085  .SetGroupName ("Core")
1086  .AddConstructor<ErlangRandomVariable> ()
1087  .AddAttribute("K", "The k value for the Erlang distribution returned by this RNG stream.",
1088  IntegerValue(1),
1090  MakeIntegerChecker<uint32_t>())
1091  .AddAttribute("Lambda", "The lambda value for the Erlang distribution returned by this RNG stream.",
1092  DoubleValue(1.0),
1094  MakeDoubleChecker<double>())
1095  ;
1096  return tid;
1097 }
1099 {
1100  // m_k and m_lambda are initialized after constructor by attributes
1101  NS_LOG_FUNCTION (this);
1102 }
1103 
1104 uint32_t
1106 {
1107  NS_LOG_FUNCTION (this);
1108  return m_k;
1109 }
1110 double
1112 {
1113  NS_LOG_FUNCTION (this);
1114  return m_lambda;
1115 }
1116 
1117 /*
1118  The code for the following generator functions was adapted from ns-2
1119  tools/ranvar.cc
1120 
1121  The Erlang distribution density function has the form
1122 
1123  x^(k-1) * exp(-x/lambda)
1124  p(x; k, lambda) = ---------------------------
1125  lambda^k * (k-1)!
1126 
1127  for x > 0.
1128 */
1129 double
1130 ErlangRandomVariable::GetValue (uint32_t k, double lambda)
1131 {
1132  NS_LOG_FUNCTION (this << k << lambda);
1133  double mean = lambda;
1134  double bound = 0.0;
1135 
1136  double result = 0;
1137  for (unsigned int i = 0; i < k; ++i)
1138  {
1139  result += GetExponentialValue (mean, bound);
1140 
1141  }
1142 
1143  return result;
1144 }
1145 
1146 uint32_t
1147 ErlangRandomVariable::GetInteger (uint32_t k, uint32_t lambda)
1148 {
1149  NS_LOG_FUNCTION (this << k << lambda);
1150  return static_cast<uint32_t> ( GetValue (k, lambda));
1151 }
1152 
1153 double
1155 {
1156  NS_LOG_FUNCTION (this);
1157  return GetValue (m_k, m_lambda);
1158 }
1159 uint32_t
1161 {
1162  NS_LOG_FUNCTION (this);
1163  return (uint32_t)GetValue (m_k, m_lambda);
1164 }
1165 
1166 double
1168 {
1169  NS_LOG_FUNCTION (this << mean << bound);
1170  while (1)
1171  {
1172  // Get a uniform random variable in [0,1].
1173  double v = Peek ()->RandU01 ();
1174  if (IsAntithetic ())
1175  {
1176  v = (1 - v);
1177  }
1178 
1179  // Calculate the exponential random variable.
1180  double r = -mean*std::log (v);
1181 
1182  // Use this value if it's acceptable.
1183  if (bound == 0 || r <= bound)
1184  {
1185  return r;
1186  }
1187  }
1188 }
1189 
1191 
1192 TypeId
1194 {
1195  static TypeId tid = TypeId ("ns3::TriangularRandomVariable")
1197  .SetGroupName ("Core")
1198  .AddConstructor<TriangularRandomVariable> ()
1199  .AddAttribute("Mean", "The mean value for the triangular distribution returned by this RNG stream.",
1200  DoubleValue(0.5),
1202  MakeDoubleChecker<double>())
1203  .AddAttribute("Min", "The lower bound on the values returned by this RNG stream.",
1204  DoubleValue(0.0),
1206  MakeDoubleChecker<double>())
1207  .AddAttribute("Max", "The upper bound on the values returned by this RNG stream.",
1208  DoubleValue(1.0),
1210  MakeDoubleChecker<double>())
1211  ;
1212  return tid;
1213 }
1215 {
1216  // m_mean, m_min, and m_max are initialized after constructor by
1217  // attributes
1218  NS_LOG_FUNCTION (this);
1219 }
1220 
1221 double
1223 {
1224  NS_LOG_FUNCTION (this);
1225  return m_mean;
1226 }
1227 double
1229 {
1230  NS_LOG_FUNCTION (this);
1231  return m_min;
1232 }
1233 double
1235 {
1236  NS_LOG_FUNCTION (this);
1237  return m_max;
1238 }
1239 
1240 double
1241 TriangularRandomVariable::GetValue (double mean, double min, double max)
1242 {
1243  // Calculate the mode.
1244  NS_LOG_FUNCTION (this << mean << min << max);
1245  double mode = 3.0 * mean - min - max;
1246 
1247  // Get a uniform random variable in [0,1].
1248  double u = Peek ()->RandU01 ();
1249  if (IsAntithetic ())
1250  {
1251  u = (1 - u);
1252  }
1253 
1254  // Calculate the triangular random variable.
1255  if (u <= (mode - min) / (max - min) )
1256  {
1257  return min + std::sqrt (u * (max - min) * (mode - min) );
1258  }
1259  else
1260  {
1261  return max - std::sqrt ( (1 - u) * (max - min) * (max - mode) );
1262  }
1263 }
1264 
1265 uint32_t
1266 TriangularRandomVariable::GetInteger (uint32_t mean, uint32_t min, uint32_t max)
1267 {
1268  NS_LOG_FUNCTION (this << mean << min << max);
1269  return static_cast<uint32_t> ( GetValue (mean, min, max) );
1270 }
1271 
1272 double
1274 {
1275  NS_LOG_FUNCTION (this);
1276  return GetValue (m_mean, m_min, m_max);
1277 }
1278 uint32_t
1280 {
1281  NS_LOG_FUNCTION (this);
1282  return (uint32_t)GetValue (m_mean, m_min, m_max);
1283 }
1284 
1286 
1287 TypeId
1289 {
1290  static TypeId tid = TypeId ("ns3::ZipfRandomVariable")
1292  .SetGroupName ("Core")
1293  .AddConstructor<ZipfRandomVariable> ()
1294  .AddAttribute("N", "The n value for the Zipf distribution returned by this RNG stream.",
1295  IntegerValue(1),
1297  MakeIntegerChecker<uint32_t>())
1298  .AddAttribute("Alpha", "The alpha value for the Zipf distribution returned by this RNG stream.",
1299  DoubleValue(0.0),
1301  MakeDoubleChecker<double>())
1302  ;
1303  return tid;
1304 }
1306 {
1307  // m_n and m_alpha are initialized after constructor by attributes
1308  NS_LOG_FUNCTION (this);
1309 }
1310 
1311 uint32_t
1313 {
1314  NS_LOG_FUNCTION (this);
1315  return m_n;
1316 }
1317 double
1319 {
1320  NS_LOG_FUNCTION (this);
1321  return m_alpha;
1322 }
1323 
1324 double
1326 {
1327  NS_LOG_FUNCTION (this << n << alpha);
1328  // Calculate the normalization constant c.
1329  m_c = 0.0;
1330  for (uint32_t i = 1; i <= n; i++)
1331  {
1332  m_c += (1.0 / std::pow ((double)i,alpha));
1333  }
1334  m_c = 1.0 / m_c;
1335 
1336  // Get a uniform random variable in [0,1].
1337  double u = Peek ()->RandU01 ();
1338  if (IsAntithetic ())
1339  {
1340  u = (1 - u);
1341  }
1342 
1343  double sum_prob = 0,zipf_value = 0;
1344  for (uint32_t i = 1; i <= m_n; i++)
1345  {
1346  sum_prob += m_c / std::pow ((double)i,m_alpha);
1347  if (sum_prob > u)
1348  {
1349  zipf_value = i;
1350  break;
1351  }
1352  }
1353  return zipf_value;
1354 }
1355 
1356 uint32_t
1358 {
1359  NS_LOG_FUNCTION (this << n << alpha);
1360  return static_cast<uint32_t> ( GetValue (n, alpha));
1361 }
1362 
1363 double
1365 {
1366  NS_LOG_FUNCTION (this);
1367  return GetValue (m_n, m_alpha);
1368 }
1369 uint32_t
1371 {
1372  NS_LOG_FUNCTION (this);
1373  return (uint32_t)GetValue (m_n, m_alpha);
1374 }
1375 
1377 
1378 TypeId
1380 {
1381  static TypeId tid = TypeId ("ns3::ZetaRandomVariable")
1383  .SetGroupName ("Core")
1384  .AddConstructor<ZetaRandomVariable> ()
1385  .AddAttribute("Alpha", "The alpha value for the zeta distribution returned by this RNG stream.",
1386  DoubleValue(3.14),
1388  MakeDoubleChecker<double>())
1389  ;
1390  return tid;
1391 }
1393 {
1394  // m_alpha is initialized after constructor by attributes
1395  NS_LOG_FUNCTION (this);
1396 }
1397 
1398 double
1400 {
1401  NS_LOG_FUNCTION (this);
1402  return m_alpha;
1403 }
1404 
1405 double
1407 {
1408  NS_LOG_FUNCTION (this << alpha);
1409  m_b = std::pow (2.0, alpha - 1.0);
1410 
1411  double u, v;
1412  double X, T;
1413  double test;
1414 
1415  do
1416  {
1417  // Get a uniform random variable in [0,1].
1418  u = Peek ()->RandU01 ();
1419  if (IsAntithetic ())
1420  {
1421  u = (1 - u);
1422  }
1423 
1424  // Get a uniform random variable in [0,1].
1425  v = Peek ()->RandU01 ();
1426  if (IsAntithetic ())
1427  {
1428  v = (1 - v);
1429  }
1430 
1431  X = std::floor (std::pow (u, -1.0 / (m_alpha - 1.0)));
1432  T = std::pow (1.0 + 1.0 / X, m_alpha - 1.0);
1433  test = v * X * (T - 1.0) / (m_b - 1.0);
1434  }
1435  while ( test > (T / m_b) );
1436 
1437  return X;
1438 }
1439 
1440 uint32_t
1442 {
1443  NS_LOG_FUNCTION (this << alpha);
1444  return static_cast<uint32_t> ( GetValue (alpha));
1445 }
1446 
1447 double
1449 {
1450  NS_LOG_FUNCTION (this);
1451  return GetValue (m_alpha);
1452 }
1453 uint32_t
1455 {
1456  NS_LOG_FUNCTION (this);
1457  return (uint32_t)GetValue (m_alpha);
1458 }
1459 
1461 
1462 TypeId
1464 {
1465  static TypeId tid = TypeId ("ns3::DeterministicRandomVariable")
1467  .SetGroupName ("Core")
1468  .AddConstructor<DeterministicRandomVariable> ()
1469  ;
1470  return tid;
1471 }
1473  :
1474  m_count (0),
1475  m_next (0),
1476  m_data (0)
1477 {
1478  NS_LOG_FUNCTION (this);
1479 }
1481 {
1482  // Delete any values currently set.
1483  NS_LOG_FUNCTION (this);
1484  if (m_data != 0)
1485  {
1486  delete[] m_data;
1487  }
1488 }
1489 
1490 void
1491 DeterministicRandomVariable::SetValueArray (double* values, std::size_t length)
1492 {
1493  NS_LOG_FUNCTION (this << values << length);
1494  // Delete any values currently set.
1495  if (m_data != 0)
1496  {
1497  delete[] m_data;
1498  }
1499 
1500  // Make room for the values being set.
1501  m_data = new double[length];
1502  m_count = length;
1503  m_next = length;
1504 
1505  // Copy the values.
1506  for (std::size_t i = 0; i < m_count; i++)
1507  {
1508  m_data[i] = values[i];
1509  }
1510 }
1511 
1512 double
1514 {
1515  NS_LOG_FUNCTION (this);
1516  // Make sure the array has been set.
1517  NS_ASSERT (m_count > 0);
1518 
1519  if (m_next == m_count)
1520  {
1521  m_next = 0;
1522  }
1523  return m_data[m_next++];
1524 }
1525 
1526 uint32_t
1528 {
1529  NS_LOG_FUNCTION (this);
1530  return (uint32_t)GetValue ();
1531 }
1532 
1534 
1535 // ValueCDF methods
1537  : value (0.0),
1538  cdf (0.0)
1539 {
1540  NS_LOG_FUNCTION (this);
1541 }
1543  : value (v),
1544  cdf (c)
1545 {
1546  NS_LOG_FUNCTION (this << v << c);
1547  NS_ASSERT (c >= 0.0 && c <= 1.0);
1548 }
1550  : value (c.value),
1551  cdf (c.cdf)
1552 {
1553  NS_LOG_FUNCTION (this << &c);
1554 }
1555 
1556 TypeId
1558 {
1559  static TypeId tid = TypeId ("ns3::EmpiricalRandomVariable")
1561  .SetGroupName ("Core")
1562  .AddConstructor<EmpiricalRandomVariable> ()
1563  ;
1564  return tid;
1565 }
1567  :
1568  m_validated (false)
1569 {
1570  NS_LOG_FUNCTION (this);
1571 }
1572 
1573 double
1575 {
1576  NS_LOG_FUNCTION (this);
1577  // Return a value from the empirical distribution
1578  // This code based (loosely) on code by Bruce Mah (Thanks Bruce!)
1579  if (!m_validated)
1580  {
1581  Validate ();
1582  }
1583 
1584  // Get a uniform random variable in [0,1].
1585  double r = Peek ()->RandU01 ();
1586  if (IsAntithetic ())
1587  {
1588  r = (1 - r);
1589  }
1590 
1591  if (r <= m_emp.front ().cdf)
1592  {
1593  return m_emp.front ().value; // Less than first
1594  }
1595  if (r >= m_emp.back ().cdf)
1596  {
1597  return m_emp.back ().value; // Greater than last
1598  }
1599  // Binary search
1600  std::vector<ValueCDF>::size_type bottom = 0;
1601  std::vector<ValueCDF>::size_type top = m_emp.size () - 1;
1602  while (1)
1603  {
1604  std::vector<ValueCDF>::size_type c = (top + bottom) / 2;
1605  if (r >= m_emp[c].cdf && r < m_emp[c + 1].cdf)
1606  { // Found it
1607  return Interpolate (m_emp[c].cdf, m_emp[c + 1].cdf,
1608  m_emp[c].value, m_emp[c + 1].value,
1609  r);
1610  }
1611  // Not here, adjust bounds
1612  if (r < m_emp[c].cdf)
1613  {
1614  top = c - 1;
1615  }
1616  else
1617  {
1618  bottom = c + 1;
1619  }
1620  }
1621 }
1622 
1623 uint32_t
1625 {
1626  NS_LOG_FUNCTION (this);
1627  return (uint32_t)GetValue ();
1628 }
1629 
1630 void EmpiricalRandomVariable::CDF (double v, double c)
1631 { // Add a new empirical datapoint to the empirical cdf
1632  // NOTE. These MUST be inserted in non-decreasing order
1633  NS_LOG_FUNCTION (this << v << c);
1634  m_emp.push_back (ValueCDF (v, c));
1635 }
1636 
1638 {
1639  NS_LOG_FUNCTION (this);
1640  if (m_emp.empty ())
1641  {
1642  NS_FATAL_ERROR ("CDF is not initialized");
1643  }
1644  ValueCDF prior = m_emp[0];
1645  for (std::vector<ValueCDF>::size_type i = 0; i < m_emp.size (); ++i)
1646  {
1647  ValueCDF& current = m_emp[i];
1648  if (current.value < prior.value || current.cdf < prior.cdf)
1649  { // Error
1650  std::cerr << "Empirical Dist error,"
1651  << " current value " << current.value
1652  << " prior value " << prior.value
1653  << " current cdf " << current.cdf
1654  << " prior cdf " << prior.cdf << std::endl;
1655  NS_FATAL_ERROR ("Empirical Dist error");
1656  }
1657  prior = current;
1658  }
1659  if (prior.cdf != 1.0)
1660  {
1661  NS_FATAL_ERROR ("CDF does not cover the whole distribution");
1662  }
1663  m_validated = true;
1664 }
1665 
1666 double EmpiricalRandomVariable::Interpolate (double c1, double c2,
1667  double v1, double v2, double r)
1668 { // Interpolate random value in range [v1..v2) based on [c1 .. r .. c2)
1669  NS_LOG_FUNCTION (this << c1 << c2 << v1 << v2 << r);
1670  return (v1 + ((v2 - v1) / (c2 - c1)) * (r - c1));
1671 }
1672 
1673 } // 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.
#define min(a, b)
Definition: 80211b.c:42
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:84
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
virtual double GetValue(void)
Returns a random double from a normal distribution with the current mean, variance, and bound.
#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:162
int64_t m_stream
The stream number for the RngStream.
virtual double Interpolate(double c1, double c2, double v1, double v2, double r)
Linear nterpolation between two points on the CDF to estimate the value at r.
double 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
virtual void Validate()
Check that the CDF is valid.
double * m_data
Array of values to return in sequence.
virtual uint32_t GetInteger(void)=0
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).
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:220
#define max(a, b)
Definition: 80211b.c:43
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.
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.
double m_mean
The mean value of the unbounded exponential distribution.
int64_t GetStream(void) const
Returns the stream number for the RngStream.
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.
static TypeId GetTypeId(void)
Register this type.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
uint32_t m_currentConsecutive
The number of times the current distinct value has been repeated.
virtual uint32_t GetInteger(void)
Get the next random value as an integer drawn from the distribution.
static TypeId GetTypeId(void)
Register this type.
double m_bound
The upper bound on values that can be returned by this RNG stream.
EmpiricalRandomVariable()
Creates an empirical RNG that has a specified, empirical distribution.
double m_beta
The beta value for the gamma distribution returned by this RNG stream.
double 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:332
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
NS_DEPRECATED double GetMean(void) const
Returns the mean parameter for the Pareto distribution returned by this RNG stream.
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.
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:72
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.
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.
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
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:915
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.
double GetSigma(void) const
Returns the sigma value for the log-normal distribution returned by this RNG stream.
double m_min
The lower bound on values that can be returned by this RNG stream.
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.