A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
random-variable-stream.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Georgia Tech Research Corporation
3 * Copyright (c) 2011 Mathieu Lacage
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors: Rajib Bhattacharjea<raj.b@gatech.edu>
19 * Hadi Arbabi<marbabi@cs.odu.edu>
20 * Mathieu Lacage <mathieu.lacage@gmail.com>
21 * Alessio Bugetti <alessiobugetti98@gmail.com>
22 *
23 * Modified by Mitch Watrous <watrous@u.washington.edu>
24 *
25 */
26#ifndef RANDOM_VARIABLE_STREAM_H
27#define RANDOM_VARIABLE_STREAM_H
28
29#include "attribute-helper.h"
30#include "object.h"
31#include "type-id.h"
32
33#include <map>
34#include <stdint.h>
35
36/**
37 * \file
38 * \ingroup randomvariable
39 * ns3::RandomVariableStream declaration, and related classes.
40 */
41
42namespace ns3
43{
44
45/**
46 * \ingroup core
47 * \defgroup randomvariable Random Variables
48 *
49 * \brief ns-3 random numbers are provided via instances of
50 * ns3::RandomVariableStream.
51 *
52 * - By default, ns-3 simulations use a fixed seed; if there is any
53 * randomness in the simulation, each run of the program will yield
54 * identical results unless the seed and/or run number is changed.
55 * - In ns-3.3 and earlier, ns-3 simulations used a random seed by default;
56 * this marks a change in policy starting with ns-3.4.
57 * - In ns-3.14 and earlier, ns-3 simulations used a different wrapper
58 * class called ns3::RandomVariable. This implementation is documented
59 * above under Legacy Random Variables. As of ns-3.15, this class has
60 * been replaced by ns3::RandomVariableStream; the underlying
61 * pseudo-random number generator has not changed.
62 * - To obtain randomness across multiple simulation runs, you must
63 * either set the seed differently or set the run number differently.
64 * To set a seed, call ns3::RngSeedManager::SetSeed() at the beginning
65 * of the program; to set a run number with the same seed, call
66 * ns3::RngSeedManager::SetRun() at the beginning of the program.
67 * - Each RandomVariableStream used in ns-3 has a virtual random number
68 * generator associated with it; all random variables use either
69 * a fixed or random seed based on the use of the global seed.
70 * - If you intend to perform multiple runs of the same scenario,
71 * with different random numbers, please be sure to read the manual
72 * section on how to perform independent replications.
73 */
74
75class RngStream;
76
77/**
78 * \ingroup randomvariable
79 * \brief The basic uniform Random Number Generator (RNG).
80 *
81 * \note The underlying random number generation method used
82 * by ns-3 is the RngStream code by Pierre L'Ecuyer at
83 * the University of Montreal.
84 *
85 * ns-3 has a rich set of random number generators that allow stream
86 * numbers to be set deterministically if desired. Class
87 * RandomVariableStream defines the base class functionality required
88 * for all such random number generators.
89 *
90 * By default, the underlying generator is seeded all the time with
91 * the same seed value and run number coming from the ns3::GlobalValue
92 * \ref GlobalValueRngSeed "RngSeed" and \ref GlobalValueRngRun
93 * "RngRun". Also by default, the stream number value for the
94 * underlying RngStream is automatically allocated.
95 *
96 * Instances can be configured to return "antithetic" values.
97 * See the documentation for the specific distributions to see
98 * how this modifies the returned values.
99 */
101{
102 public:
103 /**
104 * \brief Register this type.
105 * \return The object TypeId.
106 */
107 static TypeId GetTypeId();
108 /**
109 * \brief Default constructor.
110 */
112 /**
113 * \brief Destructor.
114 */
115 ~RandomVariableStream() override;
116
117 // Delete copy constructor and assignment operator to avoid misuse
120
121 /**
122 * \brief Specifies the stream number for the RngStream.
123 * \param [in] stream The stream number for the RngStream.
124 * -1 means "allocate a stream number automatically".
125 */
126 void SetStream(int64_t stream);
127
128 /**
129 * \brief Returns the stream number for the RngStream.
130 * \return The stream number for the RngStream.
131 * -1 means this stream was allocated automatically.
132 */
133 int64_t GetStream() const;
134
135 /**
136 * \brief Specify whether antithetic values should be generated.
137 * \param [in] isAntithetic If \c true antithetic value will be generated.
138 */
139 void SetAntithetic(bool isAntithetic);
140
141 /**
142 * \brief Check if antithetic values will be generated.
143 * \return \c true if antithetic values will be generated.
144 */
145 bool IsAntithetic() const;
146
147 /**
148 * \brief Get the next random value drawn from the distribution.
149 * \return A random value.
150 */
151 virtual double GetValue() = 0;
152
153 /** \copydoc GetValue() */
154 // The base implementation returns `(uint32_t)GetValue()`
155 virtual uint32_t GetInteger();
156
157 protected:
158 /**
159 * \brief Get the pointer to the underlying RngStream.
160 * \return The underlying RngStream
161 */
162 RngStream* Peek() const;
163
164 private:
165 /** Pointer to the underlying RngStream. */
167
168 /** Indicates if antithetic values should be generated by this RNG stream. */
170
171 /** The stream number for the RngStream. */
172 int64_t m_stream;
173
174}; // class RandomVariableStream
175
176/**
177 * \ingroup randomvariable
178 * \brief The uniform distribution Random Number Generator (RNG).
179 *
180 * This class supports the creation of objects that return random numbers
181 * from a fixed uniform distribution. It also supports the generation of
182 * single random numbers from various uniform distributions.
183 *
184 * The output range is \f$ x \in \f$ [\c Min, \c Max) for floating point values,
185 * (\c Max _excluded_), and \f$ x \in \f$ [\c Min, \c Max] (\c Max _included_)
186 * for integral values.
187 *
188 * This distribution has mean
189 *
190 * \f[
191 * \mu = \frac{\text{Max} + \text{Min}}{2}
192 * \f]
193 *
194 * and variance
195 *
196 * \f[
197 * \sigma^2 = \frac{\left(\text{Max} - \text{Min}\right)^2}{12}
198 * \f]
199 *
200 * The uniform RNG value \f$x\f$ is generated by
201 *
202 * \f[
203 * x = \text{Min} + u (\text{Max} - \text{Min})
204 * \f]
205 *
206 * where \f$u\f$ is a uniform random variable on [0,1).
207 *
208 * \par Example
209 *
210 * Here is an example of how to use this class:
211 * \code{.cc}
212 * double min = 0.0;
213 * double max = 10.0;
214 *
215 * Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
216 * x->SetAttribute ("Min", DoubleValue (min));
217 * x->SetAttribute ("Max", DoubleValue (max));
218 *
219 * double value = x->GetValue ();
220 * \endcode
221 *
222 * \par Antithetic Values.
223 *
224 * Normally this RNG returns values \f$x\f$ for floating point values
225 * (or \f$k\f$ for integer values) uniformly in the interval [\c Min, \c Max)
226 * (or [\c Min, \c Max] for integer values).
227 * If an instance of this RNG is configured to return antithetic values,
228 * the actual value returned is calculated as follows:
229 *
230 * - Compute the initial random value \f$x\f$ as normal.
231 * - Compute the distance from the maximum, \f$y = \text{Max} - x\f$
232 * - Return \f$x' = \text{Min} + y = \text{Min} + (\text{Max} - x)\f$:
233 */
235{
236 public:
237 /**
238 * \brief Register this type.
239 * \return The object TypeId.
240 */
241 static TypeId GetTypeId();
242
243 /**
244 * \brief Creates a uniform distribution RNG with the default range.
245 */
247
248 /**
249 * \brief Get the lower bound on randoms returned by GetValue().
250 * \return The lower bound on values from GetValue().
251 */
252 double GetMin() const;
253
254 /**
255 * \brief Get the upper bound on values returned by GetValue().
256 * \return The upper bound on values from GetValue().
257 */
258 double GetMax() const;
259
260 /**
261 * \copydoc GetValue()
262 *
263 * \param [in] min Low end of the range (included).
264 * \param [in] max High end of the range (excluded).
265 */
266 double GetValue(double min, double max);
267
268 /**
269 * \copydoc GetValue(double,double)
270 * \note The upper limit is included in the output range, unlike GetValue(double,double).
271 */
273
274 // Inherited
275 /**
276 * \copydoc RandomVariableStream::GetValue()
277 * \note The upper limit is excluded from the output range, unlike GetInteger().
278 */
279 double GetValue() override;
280
281 /**
282 * \copydoc RandomVariableStream::GetInteger()
283 * \note The upper limit is included in the output range, unlike GetValue().
284 */
285 uint32_t GetInteger() override;
286
287 private:
288 /** The lower bound on values that can be returned by this RNG stream. */
289 double m_min;
290
291 /** The upper bound on values that can be returned by this RNG stream. */
292 double m_max;
293
294}; // class UniformRandomVariable
295
296/**
297 * \ingroup randomvariable
298 * \brief The Random Number Generator (RNG) that returns a constant.
299 *
300 * This RNG returns the same \c Constant value for every sample.
301 *
302 * This distribution has mean equal to the \c Constant and zero variance.
303 *
304 * \par Antithetic Values.
305 *
306 * This RNG ignores the antithetic setting.
307 */
309{
310 public:
311 /**
312 * \brief Register this type.
313 * \return The object TypeId.
314 */
315 static TypeId GetTypeId();
316
317 /**
318 * \brief Creates a constant RNG with the default constant value.
319 */
321
322 /**
323 * \brief Get the constant value returned by this RNG stream.
324 * \return The constant value.
325 */
326 double GetConstant() const;
327
328 /**
329 * \copydoc GetValue()
330 * \param [in] constant The value to return.
331 */
332 double GetValue(double constant);
333 /** \copydoc GetValue(double) */
334 uint32_t GetInteger(uint32_t constant);
335
336 // Inherited
337 /*
338 * \copydoc RandomVariableStream::GetValue()
339 * \note This RNG always returns the same value.
340 */
341 double GetValue() override;
342 /* \note This RNG always returns the same value. */
344
345 private:
346 /** The constant value returned by this RNG stream. */
348
349}; // class ConstantRandomVariable
350
351/**
352 * \ingroup randomvariable
353 * \brief The Random Number Generator (RNG) that returns a pattern of
354 * sequential values.
355 *
356 * This RNG has four configuration attributes:
357 *
358 * - An increment, \c Increment.
359 * - A consecutive repeat number, \c Consecutive.
360 * - The minimum value, \c Min.
361 * - The maximum value, \c Max.
362 *
363 * The RNG starts at the \c Min value. Each return value is
364 * repeated \c Consecutive times, before advancing by the \c Increment,
365 * modulo the \c Max. In other words when the \c Increment would cause
366 * the value to equal or exceed \c Max it is reset to \c Min plus the
367 * remainder:
368 *
369 * \code{.cc}
370 * m_current += m_increment->GetValue();
371 * if (m_current >= m_max)
372 * {
373 * m_current = m_min + (m_current - m_max);
374 * }
375 * \endcode
376 *
377 * This RNG returns values in the range \f$ x \in [\text{Min}, \text{Max}) \f$.
378 * See the Example, below, for how this executes in practice.
379 *
380 * Note the \c Increment attribute is itself a RandomVariableStream,
381 * which enables more varied patterns than in the example given here.
382 *
383 * \par Example
384 *
385 * For example, if an instance is configured with:
386 *
387 * Attribute | Value
388 * :---------- | -----:
389 * Min | 2
390 * Max | 13
391 * Increment | 4
392 * Consecutive | 3
393 *
394 * The sequence will return this pattern:
395 *
396 * \f[
397 * x \in \\
398 * \underbrace{ 2, 2, 2, }_{\times 3} \\
399 * \underbrace{ 6, 6, 6, }_{\times 3} \\
400 * \underbrace{10, 10, 10, }_{\times 3} \\
401 * \underbrace{ 3, 3, 3, }_{\times 3} \\
402 * \dots
403 * \f]
404 *
405 * The last value (3) is the result of the update rule in the code snippet
406 * above, `2 + (14 - 13)`.
407 *
408 * \par Antithetic Values.
409 *
410 * This RNG ignores the antithetic setting.
411 */
413{
414 public:
415 /**
416 * \brief Register this type.
417 * \return The object TypeId.
418 */
419 static TypeId GetTypeId();
420
421 /**
422 * \brief Creates a sequential RNG with the default values
423 * for the sequence parameters.
424 */
426
427 /**
428 * \brief Get the first value of the sequence.
429 * \return The first value of the sequence.
430 */
431 double GetMin() const;
432
433 /**
434 * \brief Get the limit of the sequence, which is (at least)
435 * one more than the last value of the sequence.
436 * \return The limit of the sequence.
437 */
438 double GetMax() const;
439
440 /**
441 * \brief Get the increment for the sequence.
442 * \return The increment between distinct values for the sequence.
443 */
445
446 /**
447 * \brief Get the number of times each distinct value of the sequence
448 * is repeated before incrementing to the next value.
449 * \return The number of times each value is repeated.
450 */
451 uint32_t GetConsecutive() const;
452
453 // Inherited
454 double GetValue() override;
456
457 private:
458 /** The first value of the sequence. */
459 double m_min;
460
461 /** Strict upper bound on the sequence. */
462 double m_max;
463
464 /** Increment between distinct values. */
466
467 /** The number of times each distinct value is repeated. */
469
470 /** The current sequence value. */
471 double m_current;
472
473 /** The number of times the current distinct value has been repeated. */
475
476 /** Indicates if the current sequence value has been properly initialized. */
478
479}; // class SequentialRandomVariable
480
481/**
482 * \ingroup randomvariable
483 * \brief The exponential distribution Random Number Generator (RNG).
484 *
485 * This class supports the creation of objects that return random numbers
486 * from a fixed exponential distribution. It also supports the generation of
487 * single random numbers from various exponential distributions.
488 *
489 * The probability density function of an exponential variable
490 * is defined as:
491 *
492 * \f[
493 * P(x; \alpha) dx = \alpha e^{-\alpha x} dx, \\
494 * \quad x \in [0, +\infty)
495 * \f]
496 *
497 * where \f$ \alpha = \frac{1}{\mu} \f$
498 * and \f$\mu\f$ is the \c Mean configurable attribute. This distribution
499 * has variance \f$ \sigma^2 = \alpha^2 \f$.
500 *
501 * The exponential RNG value \f$x\f$ is generated by
502 *
503 * \f[
504 * x = - \frac{\log(u)}{\alpha} = - \text{Mean} \log(u)
505 * \f]
506 *
507 * where \f$u\f$ is a uniform random variable on [0,1).
508 *
509 * \par Bounded Distribution
510 *
511 * Since the exponential distributions can theoretically return unbounded
512 * values, it is sometimes useful to specify a fixed upper \c Bound. The
513 * bounded version is defined over the interval \f$[0,b]\f$ as:
514 *
515 * \f[
516 * P(x; \alpha, b) dx = \alpha e^{-\alpha x} dx, \\
517 * \quad x \in [0, b]
518 * \f]
519 *
520 * Note that in this case the true mean of the distribution is smaller
521 * than the nominal mean value:
522 *
523 * \f[
524 * \langle x | P(x; \alpha, b) \rangle = \frac{1}{\alpha} -
525 * \left(\frac{1}{\alpha} + b\right)\exp^{-\alpha b}
526 * \f]
527 *
528 * \par Example
529 *
530 * Here is an example of how to use this class:
531 * \code{.cc}
532 * double mean = 3.14;
533 * double bound = 0.0;
534 *
535 * Ptr<ExponentialRandomVariable> x = CreateObject<ExponentialRandomVariable> ();
536 * x->SetAttribute ("Mean", DoubleValue (mean));
537 * x->SetAttribute ("Bound", DoubleValue (bound));
538 *
539 * double value = x->GetValue ();
540 * \endcode
541 *
542 * \par Antithetic Values.
543 *
544 * If an instance of this RNG is configured to return antithetic values,
545 * the actual value returned, \f$x'\f$, is generated as follows:
546 *
547 * \f[
548 * x' = - \frac{\log(1 - u)}{\alpha} = - Mean \log(1 - u),
549 * \f]
550 *
551 * where \f$u\f$ is a uniform random variable on [0,1).
552 */
554{
555 public:
556 /**
557 * \brief Register this type.
558 * \return The object TypeId.
559 */
560 static TypeId GetTypeId();
561
562 /**
563 * \brief Creates an exponential distribution RNG with the default
564 * values for the mean and upper bound.
565 */
567
568 /**
569 * \brief Get the configured mean value of this RNG.
570 *
571 * \note This will not be the actual mean if the distribution is
572 * truncated by a bound.
573 * \return The configured mean value.
574 */
575 double GetMean() const;
576
577 /**
578 * \brief Get the configured upper bound of this RNG.
579 * \return The upper bound.
580 */
581 double GetBound() const;
582
583 /**
584 * \copydoc GetValue()
585 * \param [in] mean Mean value of the unbounded exponential distribution.
586 * \param [in] bound Upper bound on values returned.
587 */
588 double GetValue(double mean, double bound);
589
590 /** \copydoc GetValue(double,double) */
592
593 // Inherited
594 double GetValue() override;
596
597 private:
598 /** The mean value of the unbounded exponential distribution. */
599 double m_mean;
600
601 /** The upper bound on values that can be returned by this RNG stream. */
602 double m_bound;
603
604}; // class ExponentialRandomVariable
605
606/**
607 * \ingroup randomvariable
608 * \brief The Pareto distribution Random Number Generator (RNG).
609 *
610 * This class supports the creation of objects that return random numbers
611 * from a fixed Pareto distribution. It also supports the generation of
612 * single random numbers from various Pareto distributions.
613 *
614 * The probability density function of a Pareto variable is:
615 *
616 * \f[
617 * P(x; x_m, \alpha) dx = \alpha \frac{x_m^\alpha}{x^{\alpha + 1}} dx, \\
618 * \quad x \in [x_m, +\infty)
619 * \f]
620 *
621 * where the minimum value \f$x_m > 0\f$ is called the \c Scale parameter
622 * and \f$ \alpha > 0\f$ is called the Pareto index or \c Shape parameter.
623 *
624 * The resulting distribution will have mean
625 *
626 * \f[
627 * \mu = x_m \frac{\alpha}{\alpha - 1} \mbox{ for $\alpha > 1$}
628 * \f]
629 *
630 * and variance
631 *
632 * \f[
633 * \sigma^2 = x_m \frac{1}{(\alpha - 1)(\alpha - 2)} \mbox { for $\alpha > 2$}
634 * \f]
635 *
636 * The minimum value \f$x_m\f$ can be inferred from the desired mean \f$\mu\f$
637 * and the parameter \f$\alpha\f$ with the equation
638 *
639 * \f[
640 * x_m = \mu \frac{\alpha - 1}{\alpha} \mbox{ for $\alpha > 1$}
641 * \f]
642 *
643 * The Pareto RNG value \f$x\f$ is generated by
644 *
645 * \f[
646 * x = \frac{x_m}{u^{\frac{1}{\alpha}}}
647 * \f]
648 *
649 * where \f$u\f$ is a uniform random variable on [0,1).
650 *
651 * \par Bounded Distribution
652 *
653 * Since Pareto distributions can theoretically return unbounded
654 * values, it is sometimes useful to specify a fixed upper \c Bound. The
655 * bounded version is defined over the interval \f$ x \in [x_m, b] \f$.
656 * Note however when the upper limit is specified, the mean
657 * of the resulting distribution is slightly smaller than the mean value
658 * in the unbounded case.
659 *
660 * \par Example
661 *
662 * Here is an example of how to use this class:
663 * \code{.cc}
664 * double scale = 5.0;
665 * double shape = 2.0;
666 *
667 * Ptr<ParetoRandomVariable> x = CreateObject<ParetoRandomVariable> ();
668 * x->SetAttribute ("Scale", DoubleValue (scale));
669 * x->SetAttribute ("Shape", DoubleValue (shape));
670 *
671 * double value = x->GetValue ();
672 * \endcode
673 *
674 * \par Antithetic Values.
675 *
676 * If an instance of this RNG is configured to return antithetic values,
677 * the actual value returned, \f$x'\f$, is generated as follows:
678 *
679 * \f[
680 * x' = \frac{x_m}{{(1 - u)}^{\frac{1}{\alpha}}} ,
681 * \f]
682 *
683 * which now involves the distance \f$u\f$ is from 1 in the denominator.
684 */
686{
687 public:
688 /**
689 * \brief Register this type.
690 * \return The object TypeId.
691 */
692 static TypeId GetTypeId();
693
694 /**
695 * \brief Creates a Pareto distribution RNG with the default
696 * values for the mean, the shape, and upper bound.
697 */
699
700 /**
701 * \brief Returns the scale parameter for the Pareto distribution returned by this RNG stream.
702 * \return The scale parameter for the Pareto distribution returned by this RNG stream.
703 */
704 double GetScale() const;
705
706 /**
707 * \brief Returns the shape parameter for the Pareto distribution returned by this RNG stream.
708 * \return The shape parameter for the Pareto distribution returned by this RNG stream.
709 */
710 double GetShape() const;
711
712 /**
713 * \brief Returns the upper bound on values that can be returned by this RNG stream.
714 * \return The upper bound on values that can be returned by this RNG stream.
715 */
716 double GetBound() const;
717
718 /**
719 * \copydoc GetValue()
720 * \param [in] scale Mean parameter for the Pareto distribution.
721 * \param [in] shape Shape parameter for the Pareto distribution.
722 * \param [in] bound Upper bound on values returned.
723 */
724 double GetValue(double scale, double shape, double bound);
725
726 /** \copydoc GetValue(double,double,double) */
727 uint32_t GetInteger(uint32_t scale, uint32_t shape, uint32_t bound);
728
729 // Inherited
730 double GetValue() override;
732
733 private:
734 /** The scale parameter for the Pareto distribution returned by this RNG stream. */
735 double m_scale;
736
737 /** The shape parameter for the Pareto distribution returned by this RNG stream. */
738 double m_shape;
739
740 /** The upper bound on values that can be returned by this RNG stream. */
741 double m_bound;
742
743}; // class ParetoRandomVariable
744
745/**
746 * \ingroup randomvariable
747 * \brief The Weibull distribution Random Number Generator (RNG)
748 * which allows stream numbers to be set deterministically.
749 *
750 * This class supports the creation of objects that return random numbers
751 * from a fixed Weibull distribution. It also supports the generation of
752 * single random numbers from various Weibull distributions.
753 *
754 * The probability density function is:
755 *
756 * \f[
757 * P(x; \lambda, k) dx = \frac{k}{\lambda} \\
758 * \left(\frac{x}{\lambda}\right)^{k-1} \\
759 * e^{-\left(\frac{x}{\lambda}\right)^k} dx, \\
760 * \quad x \in [0, +\infty)
761 * \f]
762 *
763 * where \f$ k > 0\f$ is the \c Shape parameter and \f$ \lambda > 0\f$
764 * is the \c Scale parameter.
765 *
766 * The mean \f$\mu\f$ is related to the \c Scale and \c Shape parameters
767 * by the following relation:
768 *
769 * \f[
770 * \mu = \lambda\Gamma\left(1+\frac{1}{k}\right)
771 * \f]
772 *
773 * where \f$ \Gamma \f$ is the Gamma function.
774 *
775 * The variance of the distribution is
776 *
777 * \f[
778 * \sigma^2 = \lambda^2 \left[ \Gamma\left(1 + \frac{2}{k}\right) - \\
779 * \left( \Gamma\left(1 + \frac{1}{k}\right)\right)^2
780 * \right]
781 * \f]
782 *
783 * For \f$ k > 1 \f$ the mean rapidly approaches just \f$\lambda\f$,
784 * with variance
785 *
786 * \f[
787 * \sigma^2 \approx \frac{\pi^2}{6 k^2} + \mathcal{O}(k^{-3})
788 * \f]
789 *
790 * The Weibull RNG value \f$x\f$ is generated by
791 *
792 * \f[
793 * x = \lambda {(-\log(u))}^{\frac{1}{k}}
794 * \f]
795 *
796 * where \f$u\f$ is a uniform random variable on [0,1).
797 *
798 * \par Bounded Distribution
799 *
800 * Since Weibull distributions can theoretically return unbounded values,
801 * it is sometimes useful to specify a fixed upper limit. The
802 * bounded version is defined over the interval \f$ x \in [0, b] \f$.
803 * Note however when the upper limit is specified, the mean of the
804 * resulting distribution is slightly smaller than the mean value
805 * in the unbounded case.
806 *
807 * \par Example
808 *
809 * Here is an example of how to use this class:
810 * \code{.cc}
811 * double scale = 5.0;
812 * double shape = 1.0;
813 *
814 * Ptr<WeibullRandomVariable> x = CreateObject<WeibullRandomVariable> ();
815 * x->SetAttribute ("Scale", DoubleValue (scale));
816 * x->SetAttribute ("Shape", DoubleValue (shape));
817 *
818 * double value = x->GetValue ();
819 * \endcode
820 *
821 * \par Antithetic Values.
822 *
823 * If an instance of this RNG is configured to return antithetic values,
824 * the actual value returned, \f$x'\f$, is generated as follows:
825 *
826 * \f[
827 * x' = \lambda {(-\log(1 - u))}^{\frac{1}{k}} ,
828 * \f]
829 *
830 * which now involves the log of the distance \f$u\f$ is from 1.
831 */
833{
834 public:
835 /**
836 * \brief Register this type.
837 * \return The object TypeId.
838 */
839 static TypeId GetTypeId();
840
841 /**
842 * \brief Creates a Weibull distribution RNG with the default
843 * values for the scale, shape, and upper bound.
844 */
846
847 /**
848 * \brief Returns the scale parameter for the Weibull distribution returned by this RNG stream.
849 * \return The scale parameter for the Weibull distribution returned by this RNG stream.
850 */
851 double GetScale() const;
852
853 /**
854 * \brief Returns the shape parameter for the Weibull distribution returned by this RNG stream.
855 * \return The shape parameter for the Weibull distribution returned by this RNG stream.
856 */
857 double GetShape() const;
858
859 /**
860 * \brief Returns the upper bound on values that can be returned by this RNG stream.
861 * \return The upper bound on values that can be returned by this RNG stream.
862 */
863 double GetBound() const;
864
865 /**
866 * \copydoc GetValue()
867 * \param [in] scale Scale parameter for the Weibull distribution.
868 * \param [in] shape Shape parameter for the Weibull distribution.
869 * \param [in] bound Upper bound on values returned.
870 */
871 double GetValue(double scale, double shape, double bound);
872
873 /** \copydoc GetValue(double,double,double) */
874 uint32_t GetInteger(uint32_t scale, uint32_t shape, uint32_t bound);
875
876 // Inherited
877 double GetValue() override;
879
880 private:
881 /** The scale parameter for the Weibull distribution returned by this RNG stream. */
882 double m_scale;
883
884 /** The shape parameter for the Weibull distribution returned by this RNG stream. */
885 double m_shape;
886
887 /** The upper bound on values that can be returned by this RNG stream. */
888 double m_bound;
889
890}; // class WeibullRandomVariable
891
892/**
893 * \ingroup randomvariable
894 * \brief The normal (Gaussian) distribution Random Number Generator
895 * (RNG) that allows stream numbers to be set deterministically.
896 *
897 * This class supports the creation of objects that return random numbers
898 * from a fixed normal distribution. It also supports the generation of
899 * single random numbers from various normal distributions.
900 *
901 * The probability density function is:
902 *
903 * \f[
904 * P(x; \mu, \sigma) dx = \frac{1}{\sqrt{2\pi\sigma^2}}
905 * e^{-\frac{(x-\mu)^2}{2\sigma^2}} dx, \\
906 * \quad x \in (-\infty, +\infty)
907 * \f]
908 *
909 * where the \c Mean is given by \f$\mu\f$ and the \c Variance is \f$\sigma^2\f$
910 *
911 * If \f$u_1\f$, \f$u_2\f$ are uniform variables over [0,1]
912 * then the Gaussian RNG values, \f$x_1\f$ and \f$x_2\f$, are
913 * calculated as follows:
914 *
915 * \f{eqnarray*}{
916 * v_1 & = & 2 u_1 - 1 \\
917 * v_2 & = & 2 u_2 - 1 \\
918 * r^2 & = & v_1^2 + v_2^2 \\
919 * y & = & \sqrt{\frac{-2 \log(r^2)}{r^2}} \\
920 * x_1 & = & \mu + v_1 y \sqrt{\sigma^2} \\
921 * x_2 & = & \mu + v_2 y \sqrt{\sigma^2} .
922 * \f}
923 *
924 * Note this algorithm consumes two uniform random values and produces
925 * two normally distributed values. The implementation used here
926 * caches \f$y\f$ and \f$v_2\f$ to generate \f$x_2\f$ on the next call.
927 *
928 * \par Bounded Distribution
929 *
930 * Since normal distributions can theoretically return unbounded
931 * values, it is sometimes useful to specify a fixed bound. The
932 * NormalRandomVariable is bounded symmetrically about the mean by
933 * the \c Bound parameter, \f$b\f$, _i.e._ its values are confined to the interval
934 * \f$[\mu - b, \mu + b]\f$. This preserves the mean but decreases the variance.
935 *
936 * \par Example
937 *
938 * Here is an example of how to use this class:
939 * \code{.cc}
940 * double mean = 5.0;
941 * double variance = 2.0;
942 *
943 * Ptr<NormalRandomVariable> x = CreateObject<NormalRandomVariable> ();
944 * x->SetAttribute ("Mean", DoubleValue (mean));
945 * x->SetAttribute ("Variance", DoubleValue (variance));
946 *
947 * // The expected value for the mean of the values returned by a
948 * // normally distributed random variable is equal to mean.
949 * double value = x->GetValue ();
950 * \endcode
951 *
952 * \par Antithetic Values.
953 *
954 * If an instance of this RNG is configured to return antithetic values,
955 * the actual values returned, \f$x_1^{\prime}\f$ and \f$x_2^{\prime}\f$,
956 * are calculated as follows:
957 *
958 * \f{eqnarray*}{
959 * v_1^{\prime} & = & 2 (1 - u_1) - 1 \\
960 * v_2^{\prime} & = & 2 (1 - u_2) - 1 \\
961 * r^{\prime 2} & = & v_1^{\prime 2} + v_2^{\prime 2} \\
962 * y^{\prime} & = & \sqrt{\frac{-2 \log(r^{\prime 2})}{r^{\prime 2}}} \\
963 * x_1^{\prime} & = & \mu + v_1^{\prime} y^{\prime} \sqrt{\sigma^2} \\
964 * x_2^{\prime} & = & \mu + v_2^{\prime} y^{\prime} \sqrt{\sigma^2} ,
965 * \f}
966 *
967 * which now involves the distances \f$u_1\f$ and \f$u_2\f$ are from 1.
968 */
970{
971 public:
972 /** Large constant to bound the range. */
973 static const double INFINITE_VALUE;
974
975 /**
976 * \brief Register this type.
977 * \return The object TypeId.
978 */
979 static TypeId GetTypeId();
980
981 /**
982 * \brief Creates a normal distribution RNG with the default
983 * values for the mean, variance, and bound.
984 */
986
987 /**
988 * \brief Returns the mean value for the normal distribution returned by this RNG stream.
989 * \return The mean value for the normal distribution returned by this RNG stream.
990 */
991 double GetMean() const;
992
993 /**
994 * \brief Returns the variance value for the normal distribution returned by this RNG stream.
995 * \return The variance value for the normal distribution returned by this RNG stream.
996 */
997 double GetVariance() const;
998
999 /**
1000 * \brief Returns the bound on values that can be returned by this RNG stream.
1001 * \return The bound on values that can be returned by this RNG stream.
1002 */
1003 double GetBound() const;
1004
1005 /**
1006 * \copydoc GetValue()
1007 * \param [in] mean Mean value for the normal distribution.
1008 * \param [in] variance Variance value for the normal distribution.
1009 * \param [in] bound Bound on values returned.
1010 */
1011 double GetValue(double mean,
1012 double variance,
1014
1015 /** \copydoc GetValue(double,double,double) */
1016 uint32_t GetInteger(uint32_t mean, uint32_t variance, uint32_t bound);
1017
1018 // Inherited
1019 double GetValue() override;
1021
1022 private:
1023 /** The mean value for the normal distribution returned by this RNG stream. */
1024 double m_mean;
1025
1026 /** The variance value for the normal distribution returned by this RNG stream. */
1028
1029 /** The bound on values that can be returned by this RNG stream. */
1030 double m_bound;
1031
1032 /** True if the next value is valid. */
1034
1035 /** The algorithm produces two values at a time. Cache parameters for possible reuse.*/
1036 double m_v2;
1037 /** The algorithm produces two values at a time. Cache parameters for possible reuse.*/
1038 double m_y;
1039
1040}; // class NormalRandomVariable
1041
1042/**
1043 * \ingroup randomvariable
1044 * \brief The log-normal distribution Random Number Generator
1045 * (RNG) that allows stream numbers to be set deterministically.
1046 *
1047 * This class supports the creation of objects that return random
1048 * numbers from a fixed log-normal distribution. It also supports the
1049 * generation of single random numbers from various log-normal
1050 * distributions. If one takes the natural logarithm of a random
1051 * variable following the log-normal distribution, the obtained values
1052 * follow a normal distribution.
1053 *
1054 * The probability density function is defined using two parameters
1055 * \c Mu = \f$\mu\f$ and \c Sigma = \f$\sigma\f$ as:
1056 *
1057 * \f[
1058 * P(x; \mu, \sigma) dx = \frac{1}{x\sqrt{2\pi\sigma^2}}
1059 * e^{-\frac{(ln(x) - \mu)^2}{2\sigma^2}} dx, \\
1060 * \quad x \in [0, +\infty)
1061 * \f]
1062 *
1063 * The distribution has mean value
1064 *
1065 * \f[
1066 * \langle x | P(x; \mu, \sigma) \rangle = e^{\mu+\frac{\sigma^2}{2}}
1067 * \f]
1068 *
1069 * and variance
1070 *
1071 * \f[
1072 * var(x) = (e^{\sigma^2}-1)e^{2\mu+\sigma^2}
1073 * \f]
1074 *
1075 * Note these are the mean and variance of the log-normal distribution,
1076 * not the \c Mu or \c Sigma configuration variables.
1077 *
1078 * If the desired mean and variance are known the \f$\mu\f$ and \f$\sigma\f$
1079 * parameters can be calculated instead with the following equations:
1080 *
1081 * \f[
1082 * \mu = ln(\langle x \rangle) - \\
1083 * \frac{1}{2}ln\left(1+\frac{var(x)}{{\langle x \rangle}^2}\right)
1084 * \f]
1085 *
1086 * and
1087 *
1088 * \f[
1089 * \sigma^2 = ln\left(1+\frac{var(x)}{{\langle x \rangle}^2}\right)
1090 * \f]
1091 *
1092 * If \f$u_1\f$, \f$u_2\f$ are uniform variables over [0,1]
1093 * then the log-normal RNG value, \f$x\f$ is generated as follows:
1094 *
1095 * \f{eqnarray*}{
1096 * v_1 & = & 2 u_1 - 1 \\
1097 * v_2 & = & 2 u_2 - 1 \\
1098 * r^2 & = & v_1^2 + v_2^2 \\
1099 * y & = & \sqrt{\frac{-2 \log{r^2}}{r^2}} \\
1100 * x & = & \exp\left(\mu + v_1 y \sigma\right) .
1101 * \f}
1102 *
1103 * \par Example
1104 *
1105 * Here is an example of how to use this class:
1106 * \code{.cc}
1107 * double mu = 5.0;
1108 * double sigma = 2.0;
1109 *
1110 * Ptr<LogNormalRandomVariable> x = CreateObject<LogNormalRandomVariable> ();
1111 * x->SetAttribute ("Mu", DoubleValue (mu));
1112 * x->SetAttribute ("Sigma", DoubleValue (sigma));
1113 *
1114 * double value = x->GetValue ();
1115 * \endcode
1116 *
1117 * \par Antithetic Values.
1118 *
1119 * If an instance of this RNG is configured to return antithetic values,
1120 * the actual value returned, \f$x'\f$, is generated as follows:
1121 *
1122 * \f{eqnarray*}{
1123 * v_1^{\prime} & = & 2 (1 - u_1) - 1 \\
1124 * v_2^{\prime} & = & 2 (1 - u_2) - 1 \\
1125 * r^{\prime 2} & = & v_1^{\prime 2} + v_2^{\prime 2} \\
1126 * y^{\prime} & = & v_1^{\prime}\sqrt{\frac{-2 \log(r^{\prime 2})}{r^{\prime 2}}} \\
1127 * x^{\prime} & = & \exp\left(\mu + y^{\prime} \sigma\right) .
1128 * \f}
1129 *
1130 * which now involves the distances \f$u_1\f$ and \f$u_2\f$ are from 1.
1131 */
1133{
1134 public:
1135 /**
1136 * \brief Register this type.
1137 * \return The object TypeId.
1138 */
1139 static TypeId GetTypeId();
1140
1141 /**
1142 * \brief Creates a log-normal distribution RNG with the default
1143 * values for mu and sigma.
1144 */
1146
1147 /**
1148 * \brief Returns the mu value for the log-normal distribution returned by this RNG stream.
1149 * \return The mu value for the log-normal distribution returned by this RNG stream.
1150 */
1151 double GetMu() const;
1152
1153 /**
1154 * \brief Returns the sigma value for the log-normal distribution returned by this RNG stream.
1155 * \return The sigma value for the log-normal distribution returned by this RNG stream.
1156 */
1157 double GetSigma() const;
1158
1159 /**
1160 * \copydoc GetValue()
1161 * \param [in] mu Mu value for the log-normal distribution.
1162 * \param [in] sigma Sigma value for the log-normal distribution.
1163 */
1164 double GetValue(double mu, double sigma);
1165
1166 /** \copydoc GetValue(double,double) */
1168
1169 // Inherited
1170 double GetValue() override;
1172
1173 private:
1174 /** The mu value for the log-normal distribution returned by this RNG stream. */
1175 double m_mu;
1176
1177 /** The sigma value for the log-normal distribution returned by this RNG stream. */
1178 double m_sigma;
1179
1180 /** True if m_normal is valid. */
1182
1183 /** The algorithm produces two values at a time. Cache parameters for possible reuse.*/
1184 double m_v2;
1185
1186 /** The algorithm produces two values at a time. Cache parameters for possible reuse.*/
1187 double m_normal;
1188
1189}; // class LogNormalRandomVariable
1190
1191/**
1192 * \ingroup randomvariable
1193 * \brief The gamma distribution Random Number Generator (RNG) that
1194 * allows stream numbers to be set deterministically.
1195 *
1196 * This class supports the creation of objects that return random numbers
1197 * from a fixed gamma distribution. It also supports the generation of
1198 * single random numbers from various gamma distributions.
1199 *
1200 * The probability distribution is defined in terms two parameters,
1201 * \c Alpha = \f$\alpha > 0\f$ and \c Beta = \f$ \beta > 0\f$.
1202 * (Note the Wikipedia entry for the
1203 * [Gamma Distribution](https://en.wikipedia.org/wiki/Gamma_distribution)
1204 * uses either the parameters \f$k, \theta\f$ or \f$\alpha, \beta\f$.
1205 * The parameters used here \f$(\alpha, \beta)_{\mbox{ns-3}}\f$ correspond to
1206 * \f$(\alpha, \frac{1}{\beta})_{\mbox{Wikipedia}}\f$.)
1207 *
1208 * The probability density function is:
1209 *
1210 * \f[
1211 * P(x; \alpha, \beta) dx = x^{\alpha-1} \\
1212 * \frac{e^{-\frac{x}{\beta}}}{\beta^\alpha \Gamma(\alpha)} dx, \\
1213 * \quad x \in [0, +\infty)
1214 * \f]
1215 *
1216 * where the mean is \f$ \mu = \alpha\beta \f$ and the variance is
1217 * \f$ \sigma^2 = \alpha \beta^2\f$.
1218 *
1219 * While gamma RNG values can be generated by an algorithm similar to
1220 * normal RNGs, the implementation used here is based on the paper
1221 * G. Marsaglia and W. W. Tsang,
1222 * [A simple method for generating Gamma variables](https://dl.acm.org/doi/10.1145/358407.358414),
1223 * ACM Transactions on Mathematical Software, Vol. 26, No. 3, Sept. 2000.
1224 *
1225 * \par Example
1226 *
1227 * Here is an example of how to use this class:
1228 * \code{.cc}
1229 * double alpha = 5.0;
1230 * double beta = 2.0;
1231 *
1232 * Ptr<GammaRandomVariable> x = CreateObject<GammaRandomVariable> ();
1233 * x->SetAttribute ("Alpha", DoubleValue (alpha));
1234 * x->SetAttribute ("Beta", DoubleValue (beta));
1235 *
1236 * double value = x->GetValue ();
1237 * \endcode
1238 *
1239 * \par Antithetic Values.
1240 *
1241 * If an instance of this RNG is configured to return antithetic values,
1242 * the actual value returned, \f$x'\f$, is generated using the prescription
1243 * in the Marsaglia, _et al_. paper cited above.
1244 */
1246{
1247 public:
1248 /**
1249 * \brief Register this type.
1250 * \return The object TypeId.
1251 */
1252 static TypeId GetTypeId();
1253
1254 /**
1255 * \brief Creates a gamma distribution RNG with the default values
1256 * for alpha and beta.
1257 */
1259
1260 /**
1261 * \brief Returns the alpha value for the gamma distribution returned by this RNG stream.
1262 * \return The alpha value for the gamma distribution returned by this RNG stream.
1263 */
1264 double GetAlpha() const;
1265
1266 /**
1267 * \brief Returns the beta value for the gamma distribution returned by this RNG stream.
1268 * \return The beta value for the gamma distribution returned by this RNG stream.
1269 */
1270 double GetBeta() const;
1271
1272 /**
1273 * \copydoc GetValue()
1274 * \param [in] alpha Alpha value for the gamma distribution.
1275 * \param [in] beta Beta value for the gamma distribution.
1276 */
1277 double GetValue(double alpha, double beta);
1278
1279 /** \copydoc GetValue(double,double) */
1281
1282 // Inherited
1283 double GetValue() override;
1285
1286 private:
1287 /**
1288 * \brief Returns a random double from a normal distribution with the specified mean, variance,
1289 * and bound.
1290 * \param [in] mean Mean value for the normal distribution.
1291 * \param [in] variance Variance value for the normal distribution.
1292 * \param [in] bound Bound on values returned.
1293 * \return A floating point random value.
1294 */
1295 double GetNormalValue(double mean, double variance, double bound);
1296
1297 /** The alpha value for the gamma distribution returned by this RNG stream. */
1298 double m_alpha;
1299
1300 /** The beta value for the gamma distribution returned by this RNG stream. */
1301 double m_beta;
1302
1303 /** True if the next normal value is valid. */
1305
1306 /** The algorithm produces two values at a time. Cache parameters for possible reuse.*/
1307 double m_v2;
1308 /** The algorithm produces two values at a time. Cache parameters for possible reuse.*/
1309 double m_y;
1310
1311}; // class GammaRandomVariable
1312
1313/**
1314 * \ingroup randomvariable
1315 * \brief The Erlang distribution Random Number Generator (RNG) that
1316 * allows stream numbers to be set deterministically.
1317 *
1318 * This class supports the creation of objects that return random numbers
1319 * from a fixed Erlang distribution. It also supports the generation of
1320 * single random numbers from various Erlang distributions.
1321 *
1322 * The Erlang distribution is a special case of the Gamma distribution
1323 * where \f$k = \alpha > 0 \f$ is a positive definite integer.
1324 * Erlang distributed variables can be generated using a much faster
1325 * algorithm than Gamma variables.
1326 *
1327 * The probability distribution is defined in terms two parameters,
1328 * the \c K or shape parameter \f$ \in {1,2 \dots} \f$, and
1329 * the \c Lambda or scale parameter \f$ \in (0,1] \f$.
1330 * (Note the Wikipedia entry for the
1331 * [Erlang Distribution](https://en.wikipedia.org/wiki/Erlang_distribution)
1332 * uses the parameters \f$(k, \lambda)\f$ or \f$(k, \beta)\f$.
1333 * The parameters used here \f$(k, \lambda)_{\mbox{ns-3}}\f$ correspond to
1334 * \f$(k, \frac{1}{\lambda} = \beta)_{\mbox{Wikipedia}}\f$.)
1335 *
1336 * The probability density function is:
1337 *
1338 * \f[
1339 * P(x; k, \lambda) dx = \lambda^k \\
1340 * \frac{x^{k-1} e^{-\frac{x}{\lambda}}}{(k-1)!} dx, \\
1341 * \quad x \in [0, +\infty)
1342 * \f]
1343 *
1344 * with mean \f$ \mu = k \lambda \f$ and variance \f$ \sigma^2 = k \lambda^2 \f$.
1345 *
1346 * The Erlang RNG value \f$x\f$ is generated by
1347 *
1348 * \f[
1349 * x = - \lambda \sum_{i = 1}^{k}{\ln u_i}
1350 * \f]
1351 *
1352 * where the \f$u_i\f$ are \f$k\f$ uniform random variables on [0,1).
1353 *
1354 * \par Example
1355 *
1356 * Here is an example of how to use this class:
1357 * \code{.cc}
1358 * uint32_t k = 5;
1359 * double lambda = 2.0;
1360 *
1361 * Ptr<ErlangRandomVariable> x = CreateObject<ErlangRandomVariable> ();
1362 * x->SetAttribute ("K", IntegerValue (k));
1363 * x->SetAttribute ("Lambda", DoubleValue (lambda));
1364 *
1365 * double value = x->GetValue ();
1366 * \endcode
1367 *
1368 * \par Antithetic Values.
1369 *
1370 * If an instance of this RNG is configured to return antithetic values,
1371 * the actual value returned, \f$x'\f$, is generated as follows:
1372 *
1373 * \f[
1374 * x' = - \lambda \sum_{i = 1}^{k}{\ln (1 - u_i)}
1375 * \f]
1376 *
1377 * which now involves the log of the distance \f$u\f$ is from 1.
1378 */
1380{
1381 public:
1382 /**
1383 * \brief Register this type.
1384 * \return The object TypeId.
1385 */
1386 static TypeId GetTypeId();
1387
1388 /**
1389 * \brief Creates an Erlang distribution RNG with the default values
1390 * for k and lambda.
1391 */
1393
1394 /**
1395 * \brief Returns the k value for the Erlang distribution returned by this RNG stream.
1396 * \return The k value for the Erlang distribution returned by this RNG stream.
1397 */
1398 uint32_t GetK() const;
1399
1400 /**
1401 * \brief Returns the lambda value for the Erlang distribution returned by this RNG stream.
1402 * \return The lambda value for the Erlang distribution returned by this RNG stream.
1403 */
1404 double GetLambda() const;
1405
1406 /**
1407 * \copydoc GetValue()
1408 * \param [in] k K value for the Erlang distribution.
1409 * \param [in] lambda Lambda value for the Erlang distribution.
1410 */
1411 double GetValue(uint32_t k, double lambda);
1412
1413 /** \copydoc GetValue(uint32_t,double) */
1415
1416 // Inherited
1417 double GetValue() override;
1419
1420 private:
1421 /**
1422 * \brief Returns a random double from an exponential distribution with the specified mean and
1423 * upper bound.
1424 * \param [in] mean Mean value of the random variables.
1425 * \param [in] bound Upper bound on values returned.
1426 * \return A floating point random value.
1427 */
1428 double GetExponentialValue(double mean, double bound);
1429
1430 /** The k value for the Erlang distribution returned by this RNG stream. */
1432
1433 /** The lambda value for the Erlang distribution returned by this RNG stream. */
1434 double m_lambda;
1435
1436}; // class ErlangRandomVariable
1437
1438/**
1439 * \ingroup randomvariable
1440 * \brief The triangular distribution Random Number Generator (RNG) that
1441 * allows stream numbers to be set deterministically.
1442 *
1443 * This class supports the creation of objects that return random numbers
1444 * from a fixed triangular distribution. It also supports the generation of
1445 * single random numbers from various triangular distributions.
1446 *
1447 * The probability density depends on three parameters, the end points
1448 * \f$a\f$ = \c Min and \f$b\f$ = \c Max, and the location of the peak or mode,
1449 * \f$c\f$. For historical reasons this formulation uses the \c Mean,
1450 * \f$\mu = \frac{(a + b + c)}{3}\f$ instead of the mode.
1451 * In terms of the \c Mean, the mode is \f$c = 3 \mu - a - b\f$.
1452 *
1453 * The probability is in the shape of a triangle defined on the interval
1454 * \f$ x \in [a, b] \f$:
1455 *
1456 * \f[
1457 * P(x; a, b, c) dx = \begin{array}{ll}
1458 * 0 &\mbox{ for $x \le a$} \\
1459 * \frac{2(x - a)}{(b - a)(c - a)} dx &\mbox{ for $a \le x \le c$} \\
1460 * \frac{2}{b - 1} dx &\mbox{ for $x = c$} \\
1461 * \frac{2(b - x)}{(b - a)(b - c)} dx &\mbox{ for $c \le x \le b$} \\
1462 * 0 &\mbox{ for $b \le x$}
1463 * \end{array}
1464 * \f]
1465 *
1466 * The triangle RNG \f$x\f$ is generated by
1467 *
1468 * \f[
1469 * x = \left\{ \begin{array}{rl}
1470 * a + \sqrt{u (b - a) (c - a)} &\mbox{ if $u \le (c - a)/(b - a)$} \\
1471 * b - \sqrt{(1 - u) (b - a) (b - c) } &\mbox{ otherwise}
1472 * \end{array}
1473 * \right.
1474 * \f]
1475 *
1476 * where \f$u\f$ is a uniform random variable on [0,1).
1477 *
1478 * \par Example
1479 *
1480 * Here is an example of how to use this class:
1481 * \code{.cc}
1482 * double mean = 5.0;
1483 * double min = 2.0;
1484 * double max = 10.0;
1485 *
1486 * Ptr<TriangularRandomVariable> x = CreateObject<TriangularRandomVariable> ();
1487 * x->SetAttribute ("Mean", DoubleValue (mean));
1488 * x->SetAttribute ("Min", DoubleValue (min));
1489 * x->SetAttribute ("Max", DoubleValue (max));
1490 *
1491 * double value = x->GetValue ();
1492 * \endcode
1493 *
1494 * \par Antithetic Values.
1495 *
1496 * If an instance of this RNG is configured to return antithetic values,
1497 * the actual value returned, \f$x'\f$, is generated as follows:
1498 *
1499 * \f[
1500 * x = \left\{ \begin{array}{rl}
1501 * a + \sqrt{(1 - u) (b - a) (c - a)} &\mbox{ if $(1 - u) \le (c - a)/(b - a)$} \\
1502 * b - \sqrt{u (b - a) (b - c) } &\mbox{ otherwise}
1503 * \end{array}
1504 * \right.
1505 * \f]
1506 *
1507 * which now involves the distance \f$u\f$ is from 1.
1508 */
1510{
1511 public:
1512 /**
1513 * \brief Register this type.
1514 * \return The object TypeId.
1515 */
1516 static TypeId GetTypeId();
1517
1518 /**
1519 * \brief Creates a triangular distribution RNG with the default
1520 * values for the mean, lower bound, and upper bound.
1521 */
1523
1524 /**
1525 * \brief Returns the mean value for the triangular distribution returned by this RNG stream.
1526 * \return The mean value for the triangular distribution returned by this RNG stream.
1527 */
1528 double GetMean() const;
1529
1530 /**
1531 * \brief Returns the lower bound for the triangular distribution returned by this RNG stream.
1532 * \return The lower bound for the triangular distribution returned by this RNG stream.
1533 */
1534 double GetMin() const;
1535
1536 /**
1537 * \brief Returns the upper bound on values that can be returned by this RNG stream.
1538 * \return The upper bound on values that can be returned by this RNG stream.
1539 */
1540 double GetMax() const;
1541
1542 /**
1543 * \copydoc GetValue()
1544 * \param [in] mean Mean value for the triangular distribution.
1545 * \param [in] min Low end of the range.
1546 * \param [in] max High end of the range.
1547 */
1548 double GetValue(double mean, double min, double max);
1549
1550 /** \copydoc GetValue(double,double,double) */
1552
1553 // Inherited
1554 double GetValue() override;
1556
1557 private:
1558 /** The mean value for the triangular distribution returned by this RNG stream. */
1559 double m_mean;
1560
1561 /** The lower bound on values that can be returned by this RNG stream. */
1562 double m_min;
1563
1564 /** The upper bound on values that can be returned by this RNG stream. */
1565 double m_max;
1566
1567}; // class TriangularRandomVariable
1568
1569/**
1570 * \ingroup randomvariable
1571 * \brief The Zipf distribution Random Number Generator (RNG) that
1572 * allows stream numbers to be set deterministically.
1573 *
1574 * This class supports the creation of objects that return random numbers
1575 * from a fixed Zipf distribution. It also supports the generation of
1576 * single random numbers from various Zipf distributions.
1577 *
1578 * Zipf's law states that given some corpus of natural language
1579 * utterances, the frequency of any word is inversely proportional
1580 * to its rank in the frequency table.
1581 *
1582 * Zipf's distribution has two parameters, \c Alpha and \c N, where:
1583 * \f$ \alpha \ge 0 \f$ (real) and \f$ N \in \{1,2,3 \dots\} \f$ (integer).
1584 * (Note the Wikipedia entry for the
1585 * [Zipf Distribution](https://en.wikipedia.org/wiki/Zipf%27s_law)
1586 * uses the symbol \f$s\f$ instead of \f$\alpha\f$.)
1587 *
1588 * The probability mass function is:
1589 *
1590 * \f[
1591 * P(k; \alpha, N) = \frac{1}{k^\alpha H_{N,\alpha}}
1592 * \f]
1593 *
1594 * where the \c N-th generalized harmonic number is
1595 *
1596 * \f[
1597 * H_{N,\alpha} = \sum_{m=1}^N \frac{1}{m^\alpha}
1598 * \f]
1599 *
1600 * Note the Zipf distribution is a discrete distribution, so the
1601 * returned values \f$k\f$ will always be integers in the range \f$k
1602 * \in {1,2 \dots N} \f$.
1603 *
1604 * The mean of the distribution is
1605 *
1606 * \f[
1607 * \mu = \frac{H_{N,\alpha - 1}}{H_{N,\alpha}}
1608 * \f]
1609 *
1610 * The Zipf RNG value \f$k\f$ is the smallest value such that
1611 *
1612 * \f[
1613 * u < \frac{H_k,\alpha}{H_N,\alpha}
1614 * \f]
1615 *
1616 * where \f$u\f$ is a uniform random variable on [0,1).
1617 *
1618 * \par Example
1619 *
1620 * Here is an example of how to use this class:
1621 * \code{.cc}
1622 * uint32_t n = 1;
1623 * double alpha = 2.0;
1624 *
1625 * Ptr<ZipfRandomVariable> x = CreateObject<ZipfRandomVariable> ();
1626 * x->SetAttribute ("N", IntegerValue (n));
1627 * x->SetAttribute ("Alpha", DoubleValue (alpha));
1628 *
1629 * double value = x->GetValue ();
1630 * \endcode
1631 *
1632 * \par Antithetic Values.
1633 *
1634 * If an instance of this RNG is configured to return antithetic values,
1635 * the actual value returned, \f$k'\f$, is the value such that
1636 *
1637 * \f[
1638 * 1 - u < \frac{H_{k'},\alpha}{H_N,\alpha}
1639 * \f]
1640 *
1641 */
1643{
1644 public:
1645 /**
1646 * \brief Register this type.
1647 * \return The object TypeId.
1648 */
1649 static TypeId GetTypeId();
1650
1651 /**
1652 * \brief Creates a Zipf distribution RNG with the default values
1653 * for n and alpha.
1654 */
1656
1657 /**
1658 * \brief Returns the n value for the Zipf distribution returned by this RNG stream.
1659 * \return The n value for the Zipf distribution returned by this RNG stream.
1660 */
1661 uint32_t GetN() const;
1662
1663 /**
1664 * \brief Returns the alpha value for the Zipf distribution returned by this RNG stream.
1665 * \return The alpha value for the Zipf distribution returned by this RNG stream.
1666 */
1667 double GetAlpha() const;
1668
1669 /**
1670 * \copydoc GetValue()
1671 * \param [in] n N value for the Zipf distribution.
1672 * \param [in] alpha Alpha value for the Zipf distribution.
1673 * \return A floating point random value.
1674 */
1675 double GetValue(uint32_t n, double alpha);
1676
1677 /** \copydoc GetValue(uint32_t,double) */
1679
1680 // Inherited
1681 double GetValue() override;
1683
1684 private:
1685 /** The n value for the Zipf distribution returned by this RNG stream. */
1687
1688 /** The alpha value for the Zipf distribution returned by this RNG stream. */
1689 double m_alpha;
1690
1691 /** The normalization constant. */
1692 double m_c;
1693
1694}; // class ZipfRandomVariable
1695
1696/**
1697 * \ingroup randomvariable
1698 * \brief The zeta distribution Random Number Generator (RNG) that
1699 * allows stream numbers to be set deterministically.
1700 *
1701 * This class supports the creation of objects that return random numbers
1702 * from a fixed zeta distribution. It also supports the generation of
1703 * single random numbers from various zeta distributions.
1704 *
1705 * The Zeta distribution is related to Zipf distribution by letting
1706 * \f$N \rightarrow \infty\f$.
1707 *
1708 * Zeta distribution has one parameter, \c Alpha, \f$ \alpha > 1 \f$ (real).
1709 * (Note the Wikipedia entry for the
1710 * [Zeta Distribution](https://en.wikipedia.org/wiki/Zeta_distribution)
1711 * uses the symbol \f$s\f$ instead of \f$\alpha\f$.)
1712 * The probability mass Function is
1713 *
1714 * \f[
1715 * P(k; \alpha) = k^{-\alpha}/\zeta(\alpha)
1716 * \f]
1717 *
1718 * where \f$ \zeta(\alpha) \f$ is the Riemann zeta function
1719 *
1720 * \f[
1721 * \zeta(\alpha) = \sum_{n=1}^\infty \frac{1}{n^\alpha}
1722 * \f]
1723 *
1724 * Note the Zeta distribution is a discrete distribution, so the
1725 * returned values \f$k\f$ will always be integers in the range
1726 * \f$k \in {1,2 \dots} \f$.
1727 *
1728 * The mean value of the distribution is
1729 *
1730 * \f[
1731 * \mu = \frac{\zeta(\alpha - 1)}{\zeta(\alpha)}, \quad \alpha > 2
1732 * \f]
1733 *
1734 * The Zeta RNG \f$x\f$ is generated by an accept-reject algorithm;
1735 * see the implementation of GetValue(double).
1736 *
1737 * \par Example
1738 *
1739 * Here is an example of how to use this class:
1740 * \code{.cc}
1741 * double alpha = 2.0;
1742 *
1743 * Ptr<ZetaRandomVariable> x = CreateObject<ZetaRandomVariable> ();
1744 * x->SetAttribute ("Alpha", DoubleValue (alpha));
1745 *
1746 * double value = x->GetValue ();
1747 * \endcode
1748 *
1749 * \par Antithetic Values.
1750 *
1751 * If an instance of this RNG is configured to return antithetic values,
1752 * the actual value returned, \f$x'\f$, is generated by using
1753 * \f$ 1 - u \f$ instead. of \f$u\f$ on [0, 1].
1754 */
1756{
1757 public:
1758 /**
1759 * \brief Register this type.
1760 * \return The object TypeId.
1761 */
1762 static TypeId GetTypeId();
1763
1764 /**
1765 * \brief Creates a zeta distribution RNG with the default value for
1766 * alpha.
1767 */
1769
1770 /**
1771 * \brief Returns the alpha value for the zeta distribution returned by this RNG stream.
1772 * \return The alpha value for the zeta distribution returned by this RNG stream.
1773 */
1774 double GetAlpha() const;
1775
1776 /**
1777 * \copydoc GetValue()
1778 * \param [in] alpha Alpha value for the zeta distribution.
1779 */
1780 double GetValue(double alpha);
1781
1782 /** \copydoc GetValue(double) */
1784
1785 // Inherited
1786 double GetValue() override;
1788
1789 private:
1790 /** The alpha value for the zeta distribution returned by this RNG stream. */
1791 double m_alpha;
1792
1793 /** Just for calculus simplifications. */
1794 double m_b;
1795
1796}; // class ZetaRandomVariable
1797
1798/**
1799 * \ingroup randomvariable
1800 * \brief The Random Number Generator (RNG) that returns a predetermined sequence.
1801 *
1802 * Defines a random variable that has a specified, predetermined
1803 * sequence. This would be useful when trying to force the RNG to
1804 * return a known sequence, perhaps to compare ns-3 to some other
1805 * simulator
1806 *
1807 * Creates a generator that returns successive elements from the value
1808 * array on successive calls to GetValue(). Note
1809 * that the values in the array are copied and stored by the generator
1810 * (deep-copy). Also note that the sequence repeats if more values
1811 * are requested than are present in the array.
1812 *
1813 * \par Example
1814 *
1815 * Here is an example of how to use this class:
1816 * \code{.cc}
1817 * Ptr<DeterministicRandomVariable> s = CreateObject<DeterministicRandomVariable> ();
1818 *
1819 * std::vector array{ 4, 4, 7, 7, 10, 10};
1820 * s->SetValueArray (array);
1821 *
1822 * double value = x->GetValue ();
1823 * \endcode
1824 *
1825 * This will return values in the repeating sequence
1826 *
1827 * \f[
1828 * x \in 4, 4, 7, 7, 10, 10, 4, \dots
1829 * \f]
1830 *
1831 * \par Antithetic Values.
1832 *
1833 * This RNG ignores the antithetic setting.
1834 */
1836{
1837 public:
1838 /**
1839 * \brief Register this type.
1840 * \return The object TypeId.
1841 */
1842 static TypeId GetTypeId();
1843
1844 /**
1845 * \brief Creates a deterministic RNG that will have a predetermined
1846 * sequence of values.
1847 */
1850
1851 /**
1852 * \brief Sets the array of values that holds the predetermined sequence.
1853 *
1854 * Note that the values in the array are copied and stored
1855 * (deep-copy).
1856 * \param [in] values Array of random values to return in sequence.
1857 */
1858 void SetValueArray(const std::vector<double>& values);
1859 /**
1860 * \brief Sets the array of values that holds the predetermined sequence.
1861 *
1862 * Note that the values in the array are copied and stored
1863 * (deep-copy).
1864 * \param [in] values Array of random values to return in sequence.
1865 * \param [in] length Number of values in the array.
1866 */
1867 void SetValueArray(const double* values, std::size_t length);
1868
1869 // Inherited
1870 double GetValue() override;
1872
1873 private:
1874 /** Size of the array of values. */
1875 std::size_t m_count;
1876
1877 /** Position of the next value in the array of values. */
1878 std::size_t m_next;
1879
1880 /** Array of values to return in sequence. */
1881 double* m_data;
1882
1883}; // class DeterministicRandomVariable
1884
1885/**
1886 * \ingroup randomvariable
1887 * \brief The Random Number Generator (RNG) that has a specified
1888 * empirical distribution.
1889 *
1890 * Defines a random variable that has a specified, empirical
1891 * distribution. The cumulative probability distribution function
1892 * (CDF) is specified by a series of calls to the CDF() member
1893 * function, specifying a value \f$x\f$ and the probability \f$P(x)\f$
1894 * that the distribution is less than the specified value. When
1895 * random values are requested, a uniform random variable
1896 * \f$ u \in [0, 1] \f$ is used to select a probability,
1897 * and the return value is chosen as the largest input value
1898 * with CDF less than the random value. This method is known as
1899 * [inverse transform sampling](http://en.wikipedia.org/wiki/Inverse_transform_sampling).
1900 *
1901 * This generator has two modes: *sampling* and *interpolating*.
1902 * In *sampling* mode this random variable generator
1903 * treats the CDF as an exact histogram and returns
1904 * one of the histogram inputs exactly. This is appropriate
1905 * when the configured CDF represents the exact probability
1906 * distribution, for a categorical variable, for example.
1907 *
1908 * In *interpolating* mode this random variable generator linearly
1909 * interpolates between the CDF values defining the histogram bins.
1910 * This is appropriate when the configured CDF is an approximation
1911 * to a continuous underlying probability distribution.
1912 *
1913 * For historical reasons the default is sampling. To switch modes
1914 * use the \c Interpolate Attribute, or call SetInterpolate(). You
1915 * can change modes at any time.
1916 *
1917 * If you find yourself switching frequently it could be simpler to
1918 * set the mode to sampling, then use the GetValue() function for
1919 * sampled values, and Interpolate() function for interpolated values.
1920 *
1921 * The CDF need not start with a probability of zero, nor end with a
1922 * probability of 1.0. If the selected uniform random value
1923 * \f$ u \in [0,1] \f$ is less than the probability of the first CDF point,
1924 * that point is selected. If \f$u\f$ is greater than the probability of
1925 * the last CDF point the last point is selected. In either case the
1926 * interpolating mode will *not* interpolate (since there is no value
1927 * beyond the first/last to work with), but simply return the extremal CDF
1928 * value, as in sampling.
1929 *
1930 * \par Example
1931 *
1932 * Here is an example of how to use this class:
1933 * \code{.cc}
1934 * // Create the RNG with a non-uniform distribution between 0 and 10.
1935 * // in sampling mode.
1936 * Ptr<EmpiricalRandomVariable> x = CreateObject<EmpiricalRandomVariable> ();
1937 * x->SetInterpolate (false);
1938 * x->CDF ( 0.0, 0.0);
1939 * x->CDF ( 5.0, 0.25);
1940 * x->CDF (10.0, 1.0);
1941 *
1942 * double value = x->GetValue ();
1943 * \endcode
1944 *
1945 * The expected values and probabilities returned by GetValue() are
1946 *
1947 * Value | Probability
1948 * ----: | ----------:
1949 * 0.0 | 0
1950 * 5.0 | 25%
1951 * 10.0 | 75%
1952 *
1953 * The only two values ever returned are 5 and 10, in the ratio 1:3.
1954 *
1955 * If instead you want linear interpolation between the points of the CDF
1956 * use the Interpolate() function:
1957 *
1958 * double interp = x->Interpolate ();
1959 *
1960 * This will return continuous values on the range [0,1), 25% of the time
1961 * less than 5, and 75% of the time between 5 and 10.
1962 *
1963 * See empirical-random-variable-example.cc for an example.
1964 *
1965 * \par Antithetic Values.
1966 *
1967 * If an instance of this RNG is configured to return antithetic values,
1968 * the actual value returned, \f$x'\f$, is generated by using
1969 * \f$ 1 - u \f$ instead. of \f$u\f$ on [0, 1].
1970 */
1972{
1973 public:
1974 /**
1975 * \brief Register this type.
1976 * \return The object TypeId.
1977 */
1978 static TypeId GetTypeId();
1979
1980 /**
1981 * \brief Creates an empirical RNG that has a specified, empirical
1982 * distribution, and configured for interpolating mode.
1983 */
1985
1986 /**
1987 * \brief Specifies a point in the empirical distribution
1988 *
1989 * \param [in] v The function value for this point
1990 * \param [in] c Probability that the function is less than or equal to \p v
1991 * In other words this is cumulative distribution function
1992 * at \p v.
1993 */
1994 void CDF(double v, double c); // Value, prob <= Value
1995
1996 // Inherited
1997 /**
1998 * \copydoc RandomVariableStream::GetValue()
1999 * \note This does not interpolate the CDF, but treats it as a
2000 * stepwise continuous function.
2001 */
2002 double GetValue() override;
2004
2005 /**
2006 * \brief Returns the next value in the empirical distribution using
2007 * linear interpolation.
2008 * \return The floating point next value in the empirical distribution
2009 * using linear interpolation.
2010 */
2011 virtual double Interpolate();
2012
2013 /**
2014 * \brief Switch the mode between sampling the CDF and interpolating.
2015 * The default mode is sampling.
2016 * \param [in] interpolate If \c true set to interpolation, otherwise sampling.
2017 * \returns The previous interpolate flag value.
2018 */
2019 bool SetInterpolate(bool interpolate);
2020
2021 private:
2022 /**
2023 * \brief Check that the CDF is valid.
2024 *
2025 * A valid CDF has
2026 *
2027 * - Strictly increasing arguments, and
2028 * - Strictly increasing CDF.
2029 *
2030 * It is a fatal error to fail validation.
2031 */
2032 void Validate();
2033 /**
2034 * \brief Do the initial rng draw and check against the extrema.
2035 *
2036 * If the extrema apply, \c value will have the extremal value
2037 * and the return will be \c true.
2038 *
2039 * If the extrema do not apply \c value will have the URNG value
2040 * and the return will be \c false.
2041 *
2042 * \param [out] value The extremal value, or the URNG.
2043 * \returns \c true if \p value is the extremal result,
2044 * or \c false if \p value is the URNG value.
2045 */
2046 bool PreSample(double& value);
2047 /**
2048 * \brief Sample the CDF as a histogram (without interpolation).
2049 * \param [in] r The CDF value at which to sample the CDF.
2050 * \return The bin value corresponding to \c r.
2051 */
2052 double DoSampleCDF(double r);
2053 /**
2054 * \brief Linear interpolation between two points on the CDF to estimate
2055 * the value at \p r.
2056 *
2057 * \param [in] r The argument value to interpolate to.
2058 * \returns The interpolated CDF at \pname{r}
2059 */
2060 double DoInterpolate(double r);
2061
2062 /** \c true once the CDF has been validated. */
2064 /**
2065 * The map of CDF points (x, F(x)).
2066 * The CDF points are stored in the std::map in reverse order, as follows:
2067 * Key: CDF F(x) [0, 1] | Value: domain value (x) [-inf, inf].
2068 */
2069 std::map<double, double> m_empCdf;
2070 /**
2071 * If \c true GetValue will interpolate,
2072 * otherwise treat CDF as normal histogram.
2073 */
2075
2076}; // class EmpiricalRandomVariable
2077
2078/**
2079 * \ingroup randomvariable
2080 * \brief The binomial distribution Random Number Generator (RNG).
2081 *
2082 * This class supports the creation of objects that return random numbers
2083 * from a fixed binomial distribution. It also supports the generation of
2084 * single random numbers from various binomial distributions.
2085 *
2086 * The probability mass function of a binomial variable
2087 * is defined as:
2088 *
2089 * \f[
2090 * P(k; n, p) = \binom{n}{k} p^k (1-p)^{n-k}, \\
2091 * \quad k \in [0, n]
2092 * \f]
2093 *
2094 * where \f$ n \f$ is the number of trials and \f$ p \f$ is the probability
2095 * of success in each trial. The mean of this distribution
2096 * is \f$ \mu = np \f$ and the variance is \f$ \sigma^2 = np(1-p) \f$.
2097 *
2098 * The Binomial RNG value \f$n\f$ for a given number of trials \f$ n \f$
2099 * and success probability \f$ p \f$ is generated by
2100 *
2101 * \f[
2102 * k = \sum_{i=1}^{n} I(u_i \leq p)
2103 * \f]
2104 *
2105 * where \f$u_i\f$ is a uniform random variable on [0,1) for each trial,
2106 * and \f$I\f$ is an indicator function that is 1 if \f$u_i \leq p\f$ and 0 otherwise.
2107 * The sum of these indicator functions over all trials gives the total
2108 * number of successes, which is the value of the binomial random variable.
2109 *
2110 * \par Example
2111 *
2112 * Here is an example of how to use this class:
2113 * \code{.cc}
2114 * uint32_t trials = 10;
2115 * double probability = 0.5;
2116 *
2117 * Ptr<BinomialRandomVariable> x = CreateObject<BinomialRandomVariable> ();
2118 * x->SetAttribute ("Trials", UintegerValue (trials));
2119 * x->SetAttribute ("Probability", DoubleValue (probability));
2120 *
2121 * double successes = x->GetValue ();
2122 * \endcode
2123 *
2124 * \par Antithetic Values.
2125 *
2126 * If an instance of this RNG is configured to return antithetic values,
2127 * the actual value returned, \f$n'\f$, for the Binomial process is determined by:
2128 *
2129 * \f[
2130 * k' = \sum_{i=1}^{n} I((1 - u_i) \leq p)
2131 * \f]
2132 *
2133 * where \f$u_i\f$ is a uniform random variable on [0,1) for each trial.
2134 * The antithetic approach uses \f$(1 - u_i)\f$ instead of \f$u_i\f$ in the indicator function.
2135 *
2136 * @par Efficiency and Alternative Methods
2137 *
2138 * There are alternative methods for generating binomial distributions that may offer greater
2139 * efficiency. However, this implementation opts for a simpler approach. Although not as efficient,
2140 * this method was chosen for its simplicity and sufficiency in most applications.
2141 */
2143{
2144 public:
2145 /**
2146 * \brief Register this type.
2147 * \return The object TypeId.
2148 */
2149 static TypeId GetTypeId();
2150
2152
2153 /**
2154 * \copydoc GetValue()
2155 * \param [in] trials Number of trials.
2156 * \param [in] probability Probability of success in each trial.
2157 * \return Returns a number within the range [0, trials] indicating the number of successful
2158 * trials.
2159 */
2160 double GetValue(uint32_t trials, double probability);
2161
2162 /**
2163 * \copydoc GetValue(uint32_t,double)
2164 * This function is similar to GetValue(), but it returns a uint32_t instead of a double.
2165 */
2166 uint32_t GetInteger(uint32_t trials, uint32_t probability);
2167
2168 // Inherited
2169 double GetValue() override;
2171
2172 private:
2173 /** The number of trials. */
2175
2176 /** The probability of success in each trial. */
2178
2179}; // class BinomialRandomVariable
2180
2181/**
2182 * \ingroup randomvariable
2183 * \brief The Bernoulli distribution Random Number Generator (RNG).
2184 *
2185 * This class supports the creation of objects that return random numbers
2186 * from a fixed Bernoulli distribution. It also supports the generation of
2187 * single random numbers from various Bernoulli distributions.
2188 *
2189 * The probability mass function of a Bernoulli variable
2190 * is defined as:
2191 *
2192 * \f[
2193 * P(n; p) = p^n (1-p)^{1-n}, \\
2194 * \quad n \in \{0, 1\}
2195 * \f]
2196 *
2197 * where \f$ p \f$ is the probability of success and n is the indicator of success, with n=1
2198 * representing success and n=0 representing failure. The mean of this distribution is \f$ \mu = p
2199 * \f$ and the variance is \f$ \sigma^2 = p(1-p) \f$.
2200 *
2201 * The Bernoulli RNG value \f$n\f$ is generated by
2202 *
2203 * \f[
2204 * n =
2205 * \begin{cases}
2206 * 1 & \text{if } u \leq p \\
2207 * 0 & \text{otherwise}
2208 * \end{cases}
2209 * \f]
2210 *
2211 * where \f$u\f$ is a uniform random variable on [0,1) and \f$p\f$ is the probability of success.
2212 *
2213 * \par Example
2214 *
2215 * Here is an example of how to use this class:
2216 * \code{.cc}
2217 * double probability = 0.5;
2218 *
2219 * Ptr<BernoulliRandomVariable> x = CreateObject<BernoulliRandomVariable> ();
2220 * x->SetAttribute ("Probability", DoubleValue (probability));
2221 *
2222 * double success = x->GetValue ();
2223 * \endcode
2224 *
2225 * \par Antithetic Values.
2226 *
2227 * If an instance of this RNG is configured to return antithetic values,
2228 * the actual value returned, \f$x'\f$, is determined by:
2229 *
2230 * \f[
2231 * x' =
2232 * \begin{cases}
2233 * 1 & \text{if } (1 - u) \leq p \\
2234 * 0 & \text{otherwise}
2235 * \end{cases}
2236 * \f]
2237 *
2238 * where \f$u\f$ is a uniform random variable on [0,1) and \f$p\f$ is the probability of success.
2239 */
2241{
2242 public:
2243 /**
2244 * \brief Register this type.
2245 * \return The object TypeId.
2246 */
2247 static TypeId GetTypeId();
2248
2250
2251 /**
2252 * \copydoc GetValue()
2253 * \param [in] probability Probability of success.
2254 * \return Returns 1 if the trial is successful, or 0 if it is not.
2255 */
2256 double GetValue(double probability);
2257
2258 /**
2259 * \copydoc GetValue(double)
2260 * This function is similar to GetValue(), but it returns a uint32_t instead of a double.
2261 */
2262 uint32_t GetInteger(uint32_t probability);
2263
2264 // Inherited
2265 double GetValue() override;
2267
2268 private:
2269 /** The probability of success. */
2271
2272}; // class BernoulliRandomVariable
2273
2274} // namespace ns3
2275
2276#endif /* RANDOM_VARIABLE_STREAM_H */
Attribute helper (ATTRIBUTE_ )macros definition.
The Bernoulli distribution Random Number Generator (RNG).
double GetValue() override
Get the next random value drawn from the distribution.
double m_probability
The probability of success.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
The binomial distribution Random Number Generator (RNG).
double m_probability
The probability of success in each trial.
double GetValue() override
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
uint32_t m_trials
The number of trials.
The Random Number Generator (RNG) that returns a constant.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value drawn from the distribution.
ConstantRandomVariable()
Creates a constant RNG with the default constant value.
double GetConstant() const
Get the constant value returned by this RNG stream.
double m_constant
The constant value returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
The Random Number Generator (RNG) that returns a predetermined sequence.
double GetValue() override
Get the next random value drawn from the distribution.
std::size_t m_next
Position of the next value in the array of values.
void SetValueArray(const std::vector< double > &values)
Sets the array of values that holds the predetermined sequence.
static TypeId GetTypeId()
Register this type.
double * m_data
Array of values to return in sequence.
DeterministicRandomVariable()
Creates a deterministic RNG that will have a predetermined sequence of values.
std::size_t m_count
Size of the array of values.
The Random Number Generator (RNG) that has a specified empirical distribution.
bool SetInterpolate(bool interpolate)
Switch the mode between sampling the CDF and interpolating.
void CDF(double v, double c)
Specifies a point in the empirical distribution.
bool PreSample(double &value)
Do the initial rng draw and check against the extrema.
double DoSampleCDF(double r)
Sample the CDF as a histogram (without interpolation).
double GetValue() override
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
bool m_interpolate
If true GetValue will interpolate, otherwise treat CDF as normal histogram.
bool m_validated
true once the CDF has been validated.
double DoInterpolate(double r)
Linear interpolation between two points on the CDF to estimate the value at r.
virtual double Interpolate()
Returns the next value in the empirical distribution using linear interpolation.
EmpiricalRandomVariable()
Creates an empirical RNG that has a specified, empirical distribution, and configured for interpolati...
void Validate()
Check that the CDF is valid.
std::map< double, double > m_empCdf
The map of CDF points (x, F(x)).
The Erlang distribution Random Number Generator (RNG) that allows stream numbers to be set determinis...
double m_lambda
The lambda value for the Erlang distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
double GetExponentialValue(double mean, double bound)
Returns a random double from an exponential distribution with the specified mean and upper bound.
static TypeId GetTypeId()
Register this type.
uint32_t GetK() const
Returns the k value for the Erlang distribution returned by this RNG stream.
double GetLambda() const
Returns the lambda value for the Erlang distribution returned by this RNG stream.
uint32_t m_k
The k value for the Erlang distribution returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
ErlangRandomVariable()
Creates an Erlang distribution RNG with the default values for k and lambda.
The exponential distribution Random Number Generator (RNG).
ExponentialRandomVariable()
Creates an exponential distribution RNG with the default values for the mean and upper bound.
double GetBound() const
Get the configured upper bound of this RNG.
double m_mean
The mean value of the unbounded exponential distribution.
double GetMean() const
Get the configured mean value of this RNG.
double m_bound
The upper bound on values that can be returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value drawn from the distribution.
The gamma distribution Random Number Generator (RNG) that allows stream numbers to be set determinist...
double GetValue() override
Get the next random value drawn from the distribution.
uint32_t GetInteger(uint32_t alpha, uint32_t beta)
Get the next random value drawn from the distribution.
GammaRandomVariable()
Creates a gamma distribution RNG with the default values for alpha and beta.
double m_alpha
The alpha value for the gamma distribution returned by this RNG stream.
double GetNormalValue(double mean, double variance, double bound)
Returns a random double from a normal distribution with the specified mean, variance,...
double m_y
The algorithm produces two values at a time.
double m_v2
The algorithm produces two values at a time.
bool m_nextValid
True if the next normal value is valid.
static TypeId GetTypeId()
Register this type.
double GetAlpha() const
Returns the alpha value for the gamma distribution returned by this RNG stream.
double GetBeta() const
Returns the beta value for the gamma distribution returned by this RNG stream.
double m_beta
The beta value for the gamma distribution returned by this RNG stream.
The log-normal distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
double GetMu() const
Returns the mu value for the log-normal distribution returned by this RNG stream.
double m_v2
The algorithm produces two values at a time.
double GetSigma() const
Returns the sigma value for the log-normal distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
bool m_nextValid
True if m_normal is valid.
double m_mu
The mu value for the log-normal distribution returned by this RNG stream.
double m_sigma
The sigma value for the log-normal distribution returned by this RNG stream.
LogNormalRandomVariable()
Creates a log-normal distribution RNG with the default values for mu and sigma.
double m_normal
The algorithm produces two values at a time.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
The normal (Gaussian) distribution Random Number Generator (RNG) that allows stream numbers to be set...
double m_y
The algorithm produces two values at a time.
double GetBound() const
Returns the bound on values that can be returned by this RNG stream.
double GetVariance() const
Returns the variance 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 TypeId GetTypeId()
Register this type.
double GetMean() const
Returns the mean value for the normal distribution returned by this RNG stream.
static const double INFINITE_VALUE
Large constant to bound the range.
double m_variance
The variance value for the normal distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double m_bound
The bound on values that can be returned by this RNG stream.
bool m_nextValid
True if the next value is valid.
NormalRandomVariable()
Creates a normal distribution RNG with the default values for the mean, variance, and bound.
double m_v2
The algorithm produces two values at a time.
A base class which provides memory management and object aggregation.
Definition: object.h:89
The Pareto distribution Random Number Generator (RNG).
double GetShape() const
Returns the shape parameter for the Pareto distribution returned by this RNG stream.
double m_scale
The scale parameter for the Pareto distribution returned by this RNG stream.
ParetoRandomVariable()
Creates a Pareto distribution RNG with the default values for the mean, the shape,...
static TypeId GetTypeId()
Register this type.
double m_shape
The shape parameter for the Pareto distribution returned by this RNG stream.
double GetScale() const
Returns the scale parameter for the Pareto distribution returned by this RNG stream.
double m_bound
The upper bound on values that can be returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double GetValue() override
Get the next random value drawn from the distribution.
double GetBound() const
Returns the upper bound on values that can be returned by this RNG stream.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
The basic uniform Random Number Generator (RNG).
static TypeId GetTypeId()
Register this type.
RngStream * Peek() const
Get the pointer to the underlying RngStream.
RandomVariableStream & operator=(const RandomVariableStream &)=delete
bool IsAntithetic() const
Check if antithetic values will be generated.
RandomVariableStream(const RandomVariableStream &)=delete
virtual double GetValue()=0
Get the next random value drawn from the distribution.
~RandomVariableStream() override
Destructor.
bool m_isAntithetic
Indicates if antithetic values should be generated by this RNG stream.
void SetAntithetic(bool isAntithetic)
Specify whether antithetic values should be generated.
int64_t m_stream
The stream number for the RngStream.
RandomVariableStream()
Default constructor.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
RngStream * m_rng
Pointer to the underlying RngStream.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
int64_t GetStream() const
Returns the stream number for the RngStream.
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:50
The Random Number Generator (RNG) that returns a pattern of sequential values.
uint32_t m_currentConsecutive
The number of times the current distinct value has been repeated.
double m_min
The first value of the sequence.
Ptr< RandomVariableStream > GetIncrement() const
Get the increment for the sequence.
uint32_t m_consecutive
The number of times each distinct value is repeated.
static TypeId GetTypeId()
Register this type.
double m_current
The current sequence value.
double m_max
Strict upper bound on the sequence.
Ptr< RandomVariableStream > m_increment
Increment between distinct values.
double GetValue() override
Get the next random value drawn from the distribution.
uint32_t GetConsecutive() const
Get the number of times each distinct value of the sequence is repeated before incrementing to the ne...
double GetMax() const
Get the limit of the sequence, which is (at least) one more than the last value of the sequence.
SequentialRandomVariable()
Creates a sequential RNG with the default values for the sequence parameters.
double GetMin() const
Get the first value of the sequence.
bool m_isCurrentSet
Indicates if the current sequence value has been properly initialized.
The triangular distribution Random Number Generator (RNG) that allows stream numbers to be set determ...
double GetValue() override
Get the next random value drawn from the distribution.
double GetMean() const
Returns the mean value for the triangular distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double m_mean
The mean value for the triangular distribution returned by this RNG stream.
double m_max
The upper bound on values that can be returned by this RNG stream.
double GetMax() const
Returns the upper bound on values that can be returned by this RNG stream.
TriangularRandomVariable()
Creates a triangular distribution RNG with the default values for the mean, lower bound,...
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double m_min
The lower bound on values that can be returned by this RNG stream.
double GetMin() const
Returns the lower bound for the triangular distribution returned by this RNG stream.
a unique identifier for an interface.
Definition: type-id.h:59
The uniform distribution Random Number Generator (RNG).
UniformRandomVariable()
Creates a uniform distribution RNG with the default range.
uint32_t GetInteger() override
Get the next random value drawn from the distribution.
double GetMax() const
Get the upper bound on values returned by GetValue().
double m_min
The lower bound on values that can be returned by this RNG stream.
double m_max
The upper bound on values that can be returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double GetValue() override
Get the next random value drawn from the distribution.
double GetMin() const
Get the lower bound on randoms returned by GetValue().
The Weibull distribution Random Number Generator (RNG) which allows stream numbers to be set determin...
double m_shape
The shape parameter for the Weibull distribution returned by this RNG stream.
double m_bound
The upper bound on values that can be returned by this RNG stream.
double m_scale
The scale parameter for the Weibull distribution returned by this RNG stream.
double GetBound() const
Returns the upper bound on values that can be returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
WeibullRandomVariable()
Creates a Weibull distribution RNG with the default values for the scale, shape, and upper bound.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
static TypeId GetTypeId()
Register this type.
double GetScale() const
Returns the scale parameter for the Weibull distribution returned by this RNG stream.
double GetShape() const
Returns the shape parameter for the Weibull distribution returned by this RNG stream.
The zeta distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
static TypeId GetTypeId()
Register this type.
double m_alpha
The alpha value for the zeta distribution returned by this RNG stream.
double GetValue() override
Get the next random value drawn from the distribution.
ZetaRandomVariable()
Creates a zeta distribution RNG with the default value for alpha.
double GetAlpha() const
Returns the alpha value for the zeta distribution returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double m_b
Just for calculus simplifications.
The Zipf distribution Random Number Generator (RNG) that allows stream numbers to be set deterministi...
uint32_t GetN() const
Returns the n value for the Zipf distribution returned by this RNG stream.
static TypeId GetTypeId()
Register this type.
double m_c
The normalization constant.
double GetAlpha() const
Returns the alpha value for the Zipf distribution returned by this RNG stream.
ZipfRandomVariable()
Creates a Zipf distribution RNG with the default values for n and alpha.
double m_alpha
The alpha value for the Zipf distribution returned by this RNG stream.
uint32_t m_n
The n value for the Zipf distribution returned by this RNG stream.
virtual uint32_t GetInteger()
Get the next random value drawn from the distribution.
double GetValue() override
Get the next random value drawn from the distribution.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::TypeId declaration; inline and template implementations.