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