src/core/random-variable.h
changeset 2336 28ce210b91bb
parent 2217 0b4567d545de
child 2384 500ada6a4874
equal deleted inserted replaced
2335:854dbe96e04c 2336:28ce210b91bb
    31  *
    31  *
    32  */
    32  */
    33 
    33 
    34 namespace ns3{
    34 namespace ns3{
    35 
    35 
    36 class RngStream;
    36 class RandomVariableBase;
    37 
    37 
    38 /**
    38 /**
    39  * \brief The basic RNG for NS-3.
    39  * \brief The basic RNG for NS-3.
    40  * \ingroup randomvariable
    40  * \ingroup randomvariable
    41  *
    41  *
    42  * Note: The underlying random number generation method used
    42  * Note: The underlying random number generation method used
    43  * by NS-3 is the RngStream code by Pierre L'Ecuyer at
    43  * by NS-3 is the RngStream code by Pierre L'Ecuyer at
    44  * the University of Montreal.
    44  * the University of Montreal.
    45  * 
    45  * 
    46  * NS-3 has a rich set of  random number generators.
    46  * NS-3 has a rich set of  random number generators.
    47  * Class RandomVariable defines the base class functionalty
    47  * Class RandomVariableBase defines the base class functionalty
    48  * required for all random number generators.  By default, the underlying
    48  * required for all random number generators.  By default, the underlying
    49  * generator is seeded with the time of day, and then deterministically
    49  * generator is seeded with the time of day, and then deterministically
    50  * creates a sequence of seeds for each subsequent generator that is created.
    50  * creates a sequence of seeds for each subsequent generator that is created.
    51  * The rest of the documentation outlines how to change this behavior.
    51  * The rest of the documentation outlines how to change this behavior.
    52  */
    52  */
    53 class RandomVariable { 
    53 class RandomVariable
    54 
    54 { 
    55 public:
    55 public:
    56   /**
       
    57    * \brief Constructor for a random number generator with a random seed.
       
    58    */
       
    59   RandomVariable();
    56   RandomVariable();
    60   
    57   RandomVariable(const RandomVariable&o);
    61   /**
    58   RandomVariable &operator = (const RandomVariable &o);
    62    * \brief Copy constructor
    59   ~RandomVariable();
    63    */  
       
    64   RandomVariable(const RandomVariable&);
       
    65   
       
    66   /**
       
    67    * \brief Destructor for a random number generator with a random seed.
       
    68    */
       
    69   virtual ~RandomVariable();
       
    70   
    60   
    71   /**
    61   /**
    72    * \brief Returns a random double from the underlying distribution
    62    * \brief Returns a random double from the underlying distribution
    73    * \return A floating point random value
    63    * \return A floating point random value
    74    */
    64    */
    75   virtual double  GetValue() = 0;
    65   double GetValue (void) const;
    76 
    66 
    77   /**
    67   /**
    78    * \brief Returns a random integer integer from the underlying distribution
    68    * \brief Returns a random integer integer from the underlying distribution
    79    * \return  Integer cast of ::GetValue()
    69    * \return  Integer cast of ::GetValue()
    80    */
    70    */
    81   virtual uint32_t GetIntValue();
    71   uint32_t GetIntValue (void) const;
    82 
       
    83   /**
       
    84    * \return A copy of this object
       
    85    */  
       
    86   virtual RandomVariable*   Copy() const = 0;
       
    87   
    72   
    88   /**
    73   /**
    89    * \brief Get the internal state of the RNG
    74    * \brief Get the internal state of the RNG
    90    *
    75    *
    91    * This function is for power users who understand the inner workings
    76    * This function is for power users who understand the inner workings
    92    * of the underlying RngStream method used.  It returns the internal
    77    * of the underlying RngStream method used.  It returns the internal
    93    * state of the RNG via the input parameter.
    78    * state of the RNG via the input parameter.
    94    * \param seed Output parameter; gets overwritten with the internal state of
    79    * \param seed Output parameter; gets overwritten with the internal state of
    95    * of the RNG.
    80    * of the RNG.
    96    */
    81    */
    97   virtual void GetSeed(uint32_t seed[6]);
    82   void GetSeed(uint32_t seed[6]) const;
    98   
    83   
    99   /**
    84   /**
   100    * \brief Set seeding behavior
    85    * \brief Set seeding behavior
   101    * 
    86    * 
   102    * Specify whether the POSIX device /dev/random is to
    87    * Specify whether the POSIX device /dev/random is to
   103    * be used for seeding.  When this is used, the underlying
    88    * be used for seeding.  When this is used, the underlying
   104    * generator is seeded with data from /dev/random instead of
    89    * generator is seeded with data from /dev/random instead of
   105    * being seeded based upon the time of day.  For this to be effective,
    90    * being seeded based upon the time of day.  For this to be effective,
   106    * it must be called before the creation of the first instance of a 
    91    * it must be called before the creation of the first instance of a 
   107    * RandomVariable or subclass.  Example:
    92    * RandomVariableBase or subclass.  Example:
   108    * \code
    93    * \code
   109    * RandomVariable::UseDevRandom();
    94    * RandomVariable::UseDevRandom();
   110    * UniformVariable x(2,3);  //these are seeded randomly
    95    * UniformVariable x(2,3);  //these are seeded randomly
   111    * ExponentialVariable y(120); //etc
    96    * ExponentialVariable y(120); //etc
   112    * \endcode
    97    * \endcode
   172    * ...Results for run 1:...
   157    * ...Results for run 1:...
   173    * \endcode
   158    * \endcode
   174    */
   159    */
   175   static void SetRunNumber(uint32_t n);
   160   static void SetRunNumber(uint32_t n);
   176 private:
   161 private:
   177   static void GetRandomSeeds(uint32_t seeds[6]);
   162   RandomVariableBase *m_variable;
   178 private:
       
   179   static bool useDevRandom;    // True if using /dev/random desired
       
   180   static bool globalSeedSet;   // True if global seed has been specified
       
   181   static int  devRandom;       // File handle for /dev/random
       
   182   static uint32_t globalSeed[6]; // The global seed to use
       
   183   friend class RandomVariableInitializer;
       
   184 protected:
   163 protected:
   185   static unsigned long heuristic_sequence;
   164   RandomVariable (const RandomVariableBase &variable);
   186   static RngStream* m_static_generator;
   165   RandomVariableBase *Peek (void);
   187   static uint32_t runNumber;
   166 };
   188   static void Initialize();    // Initialize  the RNG system
       
   189   static bool initialized;     // True if package seed is set 
       
   190   RngStream* m_generator;  //underlying generator being wrapped
       
   191 };
       
   192 
       
   193 
   167 
   194 /**
   168 /**
   195  * \brief The uniform distribution RNG for NS-3.
   169  * \brief The uniform distribution RNG for NS-3.
   196  * \ingroup randomvariable
   170  * \ingroup randomvariable
   197  *
   171  *
   205  * UniformVariable x(0,10);
   179  * UniformVariable x(0,10);
   206  * x.GetValue();  //will always return numbers [0,10)
   180  * x.GetValue();  //will always return numbers [0,10)
   207  * UniformVariable::GetSingleValue(100,1000); //returns a value [100,1000)
   181  * UniformVariable::GetSingleValue(100,1000); //returns a value [100,1000)
   208  * \endcode
   182  * \endcode
   209  */
   183  */
   210 class UniformVariable : public RandomVariable {
   184 class UniformVariable : public RandomVariable 
       
   185 {
   211 public:
   186 public:
   212   /**
   187   /**
   213    * Creates a uniform random number generator in the
   188    * Creates a uniform random number generator in the
   214    * range [0.0 .. 1.0).
   189    * range [0.0 .. 1.0).
   215    */
   190    */
   219    * Creates a uniform random number generator with the specified range
   194    * Creates a uniform random number generator with the specified range
   220    * \param s Low end of the range
   195    * \param s Low end of the range
   221    * \param l High end of the range
   196    * \param l High end of the range
   222    */
   197    */
   223   UniformVariable(double s, double l);
   198   UniformVariable(double s, double l);
   224 
       
   225   UniformVariable(const UniformVariable& c);
       
   226   
       
   227   /**
       
   228    * \return A value between low and high values specified by the constructor
       
   229    */
       
   230   virtual double GetValue();
       
   231   virtual RandomVariable*  Copy() const;
       
   232 public:
   199 public:
   233   /**
   200   /**
   234    * \param s Low end of the range
   201    * \param s Low end of the range
   235    * \param l High end of the range
   202    * \param l High end of the range
   236    * \return A uniformly distributed random number between s and l
   203    * \return A uniformly distributed random number between s and l
   237    */
   204    */
   238   static double GetSingleValue(double s, double l);
   205   static double GetSingleValue(double s, double l);
   239 private:
       
   240   double m_min;
       
   241   double m_max;
       
   242 };
   206 };
   243 
   207 
   244 /**
   208 /**
   245  * \brief A random variable that returns a constant
   209  * \brief A random variable that returns a constant
   246  * \ingroup randomvariable
   210  * \ingroup randomvariable
   247  *
   211  *
   248  * Class ConstantVariable defines a random number generator that
   212  * Class ConstantVariableImpl defines a random number generator that
   249  * returns the same value every sample.
   213  * returns the same value every sample.
   250  */
   214  */
   251 class ConstantVariable : public RandomVariable { 
   215 class ConstantVariable : public RandomVariable { 
   252 
   216 
   253 public:
   217 public:
   254   /**
   218   /**
   255    * Construct a ConstantVariable RNG that returns zero every sample
   219    * Construct a ConstantVariableImpl RNG that returns zero every sample
   256    */
   220    */
   257   ConstantVariable();
   221   ConstantVariable();
   258   
   222   
   259   /**
   223   /**
   260    * Construct a ConstantVariable RNG that returns the specified value
   224    * Construct a ConstantVariableImpl RNG that returns the specified value
   261    * every sample.
   225    * every sample.
   262    * \param c Unchanging value for this RNG.
   226    * \param c Unchanging value for this RNG.
   263    */
   227    */
   264   ConstantVariable(double c);
   228   ConstantVariable(double c);
   265 
   229 
   266 
       
   267   ConstantVariable(const ConstantVariable& c) ;
       
   268 
       
   269   /**
   230   /**
   270    * \brief Specify a new constant RNG for this generator.
   231    * \brief Specify a new constant RNG for this generator.
   271    * \param c New constant value for this RNG.
   232    * \param c New constant value for this RNG.
   272    */
   233    */
   273   void    NewConstant(double c);
   234   void SetConstant(double c);
   274 
   235 
   275   /**
       
   276    * \return The constant value specified
       
   277    */
       
   278   virtual double  GetValue();
       
   279   virtual uint32_t GetIntValue();
       
   280   virtual RandomVariable*   Copy() const;
       
   281 private:
       
   282   double m_const;
       
   283 };
   236 };
   284 
   237 
   285 /**
   238 /**
   286  * \brief Return a sequential list of values
   239  * \brief Return a sequential list of values
   287  * \ingroup randomvariable
   240  * \ingroup randomvariable
   289  * Class SequentialVariable defines a random number generator that
   242  * Class SequentialVariable defines a random number generator that
   290  * returns a sequential sequence.  The sequence monotonically
   243  * returns a sequential sequence.  The sequence monotonically
   291  * increases for a period, then wraps around to the low value 
   244  * increases for a period, then wraps around to the low value 
   292  * and begins monotonicaly increasing again.
   245  * and begins monotonicaly increasing again.
   293  */
   246  */
   294 class SequentialVariable : public RandomVariable {
   247 class SequentialVariable : public RandomVariable 
   295 
   248 {
   296 public:
   249 public:
   297   /**
   250   /**
   298    * \brief Constructor for the SequentialVariable RNG.
   251    * \brief Constructor for the SequentialVariableImpl RNG.
   299    *
   252    *
   300    * The four parameters define the sequence.  For example
   253    * The four parameters define the sequence.  For example
   301    * SequentialVariable(0,5,1,2) creates a RNG that has the sequence
   254    * SequentialVariableImpl(0,5,1,2) creates a RNG that has the sequence
   302    * 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0 ...
   255    * 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0 ...
   303    * \param f First value of the sequence.
   256    * \param f First value of the sequence.
   304    * \param l One more than the last value of the sequence.
   257    * \param l One more than the last value of the sequence.
   305    * \param i Increment between sequence values
   258    * \param i Increment between sequence values
   306    * \param c Number of times each member of the sequence is repeated
   259    * \param c Number of times each member of the sequence is repeated
   307    */
   260    */
   308   SequentialVariable(double f, double l, double i = 1, uint32_t c = 1);
   261   SequentialVariable(double f, double l, double i = 1, uint32_t c = 1);
   309 
   262 
   310   /**
   263   /**
   311    * \brief Constructor for the SequentialVariable RNG.
   264    * \brief Constructor for the SequentialVariableImpl RNG.
   312    *
   265    *
   313    * Differs from the first only in that the increment parameter is a
   266    * Differs from the first only in that the increment parameter is a
   314    * random variable
   267    * random variable
   315    * \param f First value of the sequence.
   268    * \param f First value of the sequence.
   316    * \param l One more than the last value of the sequence.
   269    * \param l One more than the last value of the sequence.
   317    * \param i Reference to a RandomVariable for the sequence increment
   270    * \param i Reference to a RandomVariableBase for the sequence increment
   318    * \param c Number of times each member of the sequence is repeated
   271    * \param c Number of times each member of the sequence is repeated
   319    */
   272    */
   320   SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c = 1);
   273   SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c = 1);
   321 
   274 
   322   SequentialVariable(const SequentialVariable& c);
       
   323   
       
   324   ~SequentialVariable();
       
   325   /**
       
   326    * \return The next value in the Sequence
       
   327    */
       
   328   virtual double GetValue();
       
   329   virtual RandomVariable*  Copy() const;
       
   330 private:
       
   331   double m_min;
       
   332   double m_max;
       
   333   RandomVariable*  m_increment;
       
   334   uint32_t  m_consecutive;
       
   335   double m_current;
       
   336   uint32_t  m_currentConsecutive;
       
   337 };
   275 };
   338 
   276 
   339 /**
   277 /**
   340  * \brief Exponentially Distributed random var
   278  * \brief Exponentially Distributed random var
   341  * \ingroup randomvariable
   279  * \ingroup randomvariable
   351  *
   289  *
   352  * The bounded version is defined over the internal [0,+inf) as:
   290  * The bounded version is defined over the internal [0,+inf) as:
   353  * \f$ \left\{ \begin{array}{cl} \alpha  e^{-\alpha x} & x < bound \\ bound & x > bound \end{array}\right. \f$
   291  * \f$ \left\{ \begin{array}{cl} \alpha  e^{-\alpha x} & x < bound \\ bound & x > bound \end{array}\right. \f$
   354  * 
   292  * 
   355  * \code
   293  * \code
   356  * ExponentialVariable x(3.14);
   294  * ExponentialVariableImpl x(3.14);
   357  * x.GetValue();  //will always return with mean 3.14
   295  * x.GetValue();  //will always return with mean 3.14
   358  * ExponentialVariable::GetSingleValue(20.1); //returns with mean 20.1
   296  * ExponentialVariableImpl::GetSingleValue(20.1); //returns with mean 20.1
   359  * ExponentialVariable::GetSingleValue(108); //returns with mean 108
   297  * ExponentialVariableImpl::GetSingleValue(108); //returns with mean 108
   360  * \endcode
   298  * \endcode
   361  *
   299  *
   362  */
   300  */
   363 class ExponentialVariable : public RandomVariable { 
   301 class ExponentialVariable : public RandomVariable 
       
   302 { 
   364 public:
   303 public:
   365   /**
   304   /**
   366    * Constructs an exponential random variable  with a mean
   305    * Constructs an exponential random variable  with a mean
   367    * value of 1.0.
   306    * value of 1.0.
   368    */
   307    */
   385    * \param m Mean value of the random variable
   324    * \param m Mean value of the random variable
   386    * \param b Upper bound on returned values
   325    * \param b Upper bound on returned values
   387    */
   326    */
   388   ExponentialVariable(double m, double b);
   327   ExponentialVariable(double m, double b);
   389 
   328 
   390   ExponentialVariable(const ExponentialVariable& c);
       
   391   
       
   392   /**
       
   393    * \return A random value from this exponential distribution
       
   394    */
       
   395   virtual double GetValue();
       
   396   virtual RandomVariable* Copy() const;
       
   397 public:
       
   398   /**
   329   /**
   399    * \param m The mean of the distribution from which the return value is drawn
   330    * \param m The mean of the distribution from which the return value is drawn
   400    * \param b The upper bound value desired, beyond which values get clipped
   331    * \param b The upper bound value desired, beyond which values get clipped
   401    * \return A random number from an exponential distribution with mean m
   332    * \return A random number from an exponential distribution with mean m
   402    */
   333    */
   403   static double GetSingleValue(double m, double b=0);
   334   static double GetSingleValue(double m, double b=0);
   404 private:
   335 };
   405   double m_mean;  // Mean value of RV
   336 
   406   double m_bound; // Upper bound on value (if non-zero)
   337 /**
   407 };
   338  * \brief ParetoVariableImpl distributed random var
   408 
       
   409 /**
       
   410  * \brief ParetoVariable distributed random var
       
   411  * \ingroup randomvariable
   339  * \ingroup randomvariable
   412  *
   340  *
   413  * This class supports the creation of objects that return random numbers
   341  * This class supports the creation of objects that return random numbers
   414  * from a fixed pareto distribution.  It also supports the generation of 
   342  * from a fixed pareto distribution.  It also supports the generation of 
   415  * single random numbers from various pareto distributions.
   343  * single random numbers from various pareto distributions.
   420  * 
   348  * 
   421  * The parameter \f$ x_m \f$ can be infered from the mean and the parameter \f$ k \f$
   349  * The parameter \f$ x_m \f$ can be infered from the mean and the parameter \f$ k \f$
   422  * with the equation \f$ x_m = mean \frac{k-1}{k},  k > 1\f$.
   350  * with the equation \f$ x_m = mean \frac{k-1}{k},  k > 1\f$.
   423  *
   351  *
   424  * \code
   352  * \code
   425  * ParetoVariable x(3.14);
   353  * ParetoVariableImpl x(3.14);
   426  * x.GetValue();  //will always return with mean 3.14
   354  * x.GetValue();  //will always return with mean 3.14
   427  * ParetoVariable::GetSingleValue(20.1); //returns with mean 20.1
   355  * ParetoVariableImpl::GetSingleValue(20.1); //returns with mean 20.1
   428  * ParetoVariable::GetSingleValue(108); //returns with mean 108
   356  * ParetoVariableImpl::GetSingleValue(108); //returns with mean 108
   429  * \endcode
   357  * \endcode
   430  */
   358  */
   431 class ParetoVariable : public RandomVariable {
   359 class ParetoVariable : public RandomVariable
       
   360 {
   432 public:
   361 public:
   433   /**
   362   /**
   434    * Constructs a pareto random variable with a mean of 1 and a shape
   363    * Constructs a pareto random variable with a mean of 1 and a shape
   435    * parameter of 1.5
   364    * parameter of 1.5
   436    */
   365    */
   437   ParetoVariable();
   366   ParetoVariable ();
   438 
   367 
   439   /**
   368   /**
   440    * Constructs a pareto random variable with specified mean and shape
   369    * Constructs a pareto random variable with specified mean and shape
   441    * parameter of 1.5
   370    * parameter of 1.5
   442    * \param m Mean value of the distribution
   371    * \param m Mean value of the distribution
   463    * \param s Shape parameter
   392    * \param s Shape parameter
   464    * \param b Upper limit on returned values
   393    * \param b Upper limit on returned values
   465    */
   394    */
   466   ParetoVariable(double m, double s, double b);
   395   ParetoVariable(double m, double s, double b);
   467 
   396 
   468   ParetoVariable(const ParetoVariable& c);
       
   469   
       
   470   /**
       
   471    * \return A random value from this Pareto distribution
       
   472    */
       
   473   virtual double GetValue();
       
   474   virtual RandomVariable* Copy() const;
       
   475 public:
       
   476   /**
   397   /**
   477    * \param m The mean value of the distribution from which the return value
   398    * \param m The mean value of the distribution from which the return value
   478    * is drawn.
   399    * is drawn.
   479    * \param s The shape parameter of the distribution from which the return
   400    * \param s The shape parameter of the distribution from which the return
   480    * value is drawn.
   401    * value is drawn.
   481    * \param b The upper bound to which to restrict return values
   402    * \param b The upper bound to which to restrict return values
   482    * \return A random number from a Pareto distribution with mean m and shape
   403    * \return A random number from a Pareto distribution with mean m and shape
   483    * parameter s.
   404    * parameter s.
   484    */
   405    */
   485   static double GetSingleValue(double m, double s, double b=0);
   406   static double GetSingleValue(double m, double s, double b=0);
   486 private:
   407 };
   487   double m_mean;  // Mean value of RV
   408 
   488   double m_shape; // Shape parameter
   409 /**
   489   double m_bound; // Upper bound on value (if non-zero)
   410  * \brief WeibullVariableImpl distributed random var
   490 };
       
   491 
       
   492 /**
       
   493  * \brief WeibullVariable distributed random var
       
   494  * \ingroup randomvariable
   411  * \ingroup randomvariable
   495  *
   412  *
   496  * This class supports the creation of objects that return random numbers
   413  * This class supports the creation of objects that return random numbers
   497  * from a fixed weibull distribution.  It also supports the generation of 
   414  * from a fixed weibull distribution.  It also supports the generation of 
   498  * single random numbers from various weibull distributions.
   415  * single random numbers from various weibull distributions.
   528   WeibullVariable(double m, double s);
   445   WeibullVariable(double m, double s);
   529 
   446 
   530    /**
   447    /**
   531    * \brief Constructs a weibull random variable with the specified mean
   448    * \brief Constructs a weibull random variable with the specified mean
   532    * \brief value, shape (alpha), and upper bound.
   449    * \brief value, shape (alpha), and upper bound.
   533    * Since WeibullVariable distributions can theoretically return unbounded values,
   450    * Since WeibullVariableImpl distributions can theoretically return unbounded values,
   534    * it is sometimes usefull to specify a fixed upper limit.  Note however
   451    * it is sometimes usefull to specify a fixed upper limit.  Note however
   535    * that when the upper limit is specified, the true mean of the distribution
   452    * that when the upper limit is specified, the true mean of the distribution
   536    * is slightly smaller than the mean value specified.
   453    * is slightly smaller than the mean value specified.
   537    * \param m Mean value for the distribution.
   454    * \param m Mean value for the distribution.
   538    * \param s Shape (alpha) parameter for the distribution.
   455    * \param s Shape (alpha) parameter for the distribution.
   539    * \param b Upper limit on returned values
   456    * \param b Upper limit on returned values
   540    */
   457    */
   541   WeibullVariable(double m, double s, double b);
   458   WeibullVariable(double m, double s, double b);
   542 
       
   543   WeibullVariable(const WeibullVariable& c);
       
   544   
       
   545   /**
       
   546    * \return A random value from this Weibull distribution
       
   547    */
       
   548   virtual double GetValue();
       
   549   virtual RandomVariable* Copy() const;
       
   550 public:
       
   551   /**
   459   /**
   552    * \param m Mean value for the distribution.
   460    * \param m Mean value for the distribution.
   553    * \param s Shape (alpha) parameter for the distribution.
   461    * \param s Shape (alpha) parameter for the distribution.
   554    * \param b Upper limit on returned values
   462    * \param b Upper limit on returned values
   555    * \return Random number from a distribution specified by m,s, and b
   463    * \return Random number from a distribution specified by m,s, and b
   556    */
   464    */
   557   static double GetSingleValue(double m, double s, double b=0);
   465   static double GetSingleValue(double m, double s, double b=0);
   558 private:
   466 };
   559   double m_mean;  // Mean value of RV
   467 
   560   double m_alpha; // Shape parameter
   468 /**
   561   double m_bound; // Upper bound on value (if non-zero)
   469  * \brief Class NormalVariableImpl defines a random variable with a
   562 };
       
   563 
       
   564 /**
       
   565  * \brief Class NormalVariable defines a random variable with a
       
   566  * normal (Gaussian) distribution.
   470  * normal (Gaussian) distribution.
   567  * \ingroup randomvariable
   471  * \ingroup randomvariable
   568  * 
   472  * 
   569  * This class supports the creation of objects that return random numbers
   473  * This class supports the creation of objects that return random numbers
   570  * from a fixed normal distribution.  It also supports the generation of 
   474  * from a fixed normal distribution.  It also supports the generation of 
   573  * The density probability function is defined over the interval (-inf,+inf)
   477  * The density probability function is defined over the interval (-inf,+inf)
   574  * as: \f$ \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{s\sigma^2}}\f$
   478  * as: \f$ \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{s\sigma^2}}\f$
   575  * where \f$ mean = \mu \f$ and \f$ variance = \sigma^2 \f$
   479  * where \f$ mean = \mu \f$ and \f$ variance = \sigma^2 \f$
   576  *
   480  *
   577  */
   481  */
   578 class NormalVariable : public RandomVariable { // Normally Distributed random var
   482 class NormalVariable : public RandomVariable
   579 
   483 {
   580 public:
   484 public:
   581    static const double INFINITE_VALUE;
   485    static const double INFINITE_VALUE;
   582   /**
   486   /**
   583    * Constructs an normal random variable  with a mean
   487    * Constructs an normal random variable  with a mean
   584    * value of 0 and variance of 1.
   488    * value of 0 and variance of 1.
   587 
   491 
   588   /**
   492   /**
   589    * \brief Construct a normal random variable with specified mean and variance
   493    * \brief Construct a normal random variable with specified mean and variance
   590    * \param m Mean value
   494    * \param m Mean value
   591    * \param v Variance
   495    * \param v Variance
   592    * \param b Bound.  The NormalVariable is bounded within +-bound.
   496    * \param b Bound.  The NormalVariableImpl is bounded within +-bound.
   593    */ 
   497    */ 
   594   NormalVariable(double m, double v, double b = INFINITE_VALUE);
   498   NormalVariable(double m, double v, double b = INFINITE_VALUE);
   595 
       
   596   NormalVariable(const NormalVariable& c);
       
   597   
       
   598   /**
       
   599    * \return A value from this normal distribution
       
   600    */
       
   601   virtual double GetValue();
       
   602   virtual RandomVariable* Copy() const;
       
   603 public:
       
   604   /**
   499   /**
   605    * \param m Mean value
   500    * \param m Mean value
   606    * \param v Variance
   501    * \param v Variance
   607    * \param b Bound.  The NormalVariable is bounded within +-bound.
   502    * \param b Bound.  The NormalVariableImpl is bounded within +-bound.
   608    * \return A random number from a distribution specified by m,v, and b.
   503    * \return A random number from a distribution specified by m,v, and b.
   609    */ 
   504    */ 
   610   static double GetSingleValue(double m, double v, double b = INFINITE_VALUE);
   505   static double GetSingleValue(double m, double v, double b = INFINITE_VALUE);
   611 private:
   506 };
   612   double m_mean;      // Mean value of RV
   507 
   613   double m_variance;  // Mean value of RV
   508 /**
   614   double m_bound;     // Bound on value (absolute value)
   509  * \brief EmpiricalVariableImpl distribution random var
   615   bool   m_nextValid; // True if next valid
       
   616   double m_next;      // The algorithm produces two values at a time
       
   617   static bool   m_static_nextValid;
       
   618   static double m_static_next;
       
   619 };
       
   620 
       
   621 /**
       
   622  * \brief EmpiricalVariable distribution random var
       
   623  * \ingroup randomvariable
   510  * \ingroup randomvariable
   624  *
   511  *
   625  * Defines a random variable  that has a specified, empirical 
   512  * Defines a random variable  that has a specified, empirical 
   626  * distribution.  The distribution is specified by a
   513  * distribution.  The distribution is specified by a
   627  * series of calls to the CDF member function, specifying a
   514  * series of calls to the CDF member function, specifying a
   632  * two appropriate points in the CDF
   519  * two appropriate points in the CDF
   633  */
   520  */
   634 class EmpiricalVariable : public RandomVariable {
   521 class EmpiricalVariable : public RandomVariable {
   635 public:
   522 public:
   636   /**
   523   /**
   637    * Constructor for the EmpiricalVariable random variables.
   524    * Constructor for the EmpiricalVariableImpl random variables.
   638    */
   525    */
   639   explicit EmpiricalVariable();
   526   explicit EmpiricalVariable();
   640 
   527 
   641   virtual ~EmpiricalVariable();
       
   642   EmpiricalVariable(const EmpiricalVariable& c);
       
   643   /**
       
   644    * \return A value from this empirical distribution
       
   645    */
       
   646   virtual double GetValue();
       
   647   virtual RandomVariable* Copy() const;
       
   648   /**
   528   /**
   649    * \brief Specifies a point in the empirical distribution
   529    * \brief Specifies a point in the empirical distribution
   650    * \param v The function value for this point
   530    * \param v The function value for this point
   651    * \param c Probability that the function is less than or equal to v
   531    * \param c Probability that the function is less than or equal to v
   652    */
   532    */
   653   virtual void CDF(double v, double c);  // Value, prob <= Value
   533   void CDF(double v, double c);  // Value, prob <= Value
   654 
   534 protected:
   655 private:
   535   EmpiricalVariable (const RandomVariableBase &variable);
   656   class ValueCDF {
       
   657   public:
       
   658     ValueCDF();
       
   659     ValueCDF(double v, double c);
       
   660     ValueCDF(const ValueCDF& c);
       
   661     double value;
       
   662     double    cdf;
       
   663   };
       
   664   virtual void Validate();  // Insure non-decreasing emiprical values
       
   665   virtual double Interpolate(double, double, double, double, double);
       
   666   bool validated; // True if non-decreasing validated
       
   667   std::vector<ValueCDF> emp;       // Empicical CDF
       
   668 };
   536 };
   669 
   537 
   670 /**
   538 /**
   671  * \brief Integer-based empirical distribution
   539  * \brief Integer-based empirical distribution
   672  * \ingroup randomvariable
   540  * \ingroup randomvariable
   673  *
   541  *
   674  * Defines an empirical distribution where all values are integers.
   542  * Defines an empirical distribution where all values are integers.
   675  * Indentical to EmpiricalVariable, but with slightly different
   543  * Indentical to EmpiricalVariableImpl, but with slightly different
   676  * interpolation between points.
   544  * interpolation between points.
   677  */
   545  */
   678 class IntEmpiricalVariable : public EmpiricalVariable {
   546 class IntEmpiricalVariable : public EmpiricalVariable 
   679 public:
   547 {
   680 
   548 public:
   681   IntEmpiricalVariable();
   549   IntEmpiricalVariable();
   682   
       
   683   virtual RandomVariable* Copy() const;
       
   684   /**
       
   685    * \return An integer value from this empirical distribution
       
   686    */
       
   687   virtual uint32_t GetIntValue();
       
   688   virtual double Interpolate(double, double, double, double, double);
       
   689 };
   550 };
   690 
   551 
   691 /**
   552 /**
   692  * \brief a non-random variable
   553  * \brief a non-random variable
   693  * \ingroup randomvariable
   554  * \ingroup randomvariable
   695  * Defines a random variable  that has a specified, predetermined
   556  * Defines a random variable  that has a specified, predetermined
   696  * sequence.  This would be useful when trying to force
   557  * sequence.  This would be useful when trying to force
   697  * the RNG to return a known sequence, perhaps to
   558  * the RNG to return a known sequence, perhaps to
   698  * compare NS-3 to some other simulator
   559  * compare NS-3 to some other simulator
   699  */
   560  */
   700 class DeterministicVariable : public RandomVariable {
   561 class DeterministicVariable : public RandomVariable
   701 
   562 {
   702 public:
   563 public:
   703   /**
   564   /**
   704    * \brief Constructor
   565    * \brief Constructor
   705    *
   566    *
   706    * Creates a generator that returns successive elements of the d array
   567    * Creates a generator that returns successive elements of the d array
   707    * on successive calls to ::Value().  Note that the d pointer is copied
   568    * on successive calls to ::Value().  Note that the d pointer is copied
   708    * for use by the generator (shallow-copy), not its contents, so the 
   569    * for use by the generator (shallow-copy), not its contents, so the 
   709    * contents of the array d points to have to remain unchanged for the use 
   570    * contents of the array d points to have to remain unchanged for the use 
   710    * of DeterministicVariable to be meaningful.
   571    * of DeterministicVariableImpl to be meaningful.
   711    * \param d Pointer to array of random values to return in sequence
   572    * \param d Pointer to array of random values to return in sequence
   712    * \param c Number of values in the array
   573    * \param c Number of values in the array
   713    */
   574    */
   714   explicit DeterministicVariable(double* d, uint32_t c);
   575   explicit DeterministicVariable(double* d, uint32_t c);
   715 
       
   716   virtual ~DeterministicVariable();
       
   717   /**
       
   718    * \return The next value in the deterministic sequence
       
   719    */
       
   720   virtual double GetValue();
       
   721   virtual RandomVariable* Copy() const;
       
   722 private:
       
   723   uint32_t   count;
       
   724   uint32_t   next;
       
   725   double* data;
       
   726 };
   576 };
   727 
   577 
   728 
   578 
   729 /**
   579 /**
   730  * \brief Log-normal Distributed random var
   580  * \brief Log-normal Distributed random var
   731  * \ingroup randomvariable
   581  * \ingroup randomvariable
   732  *
   582  *
   733  * LogNormalVariable defines a random variable with log-normal
   583  * LogNormalVariableImpl defines a random variable with log-normal
   734  * distribution.  If one takes the natural logarithm of random
   584  * distribution.  If one takes the natural logarithm of random
   735  * variable following the log-normal distribution, the obtained values
   585  * variable following the log-normal distribution, the obtained values
   736  * follow a normal distribution.
   586  * follow a normal distribution.
   737  *  This class supports the creation of objects that return random numbers
   587  *  This class supports the creation of objects that return random numbers
   738  * from a fixed lognormal distribution.  It also supports the generation of
   588  * from a fixed lognormal distribution.  It also supports the generation of
   746  * The \f$ \mu \f$ and \f$ \sigma \f$ parameters can be calculated from the mean
   596  * The \f$ \mu \f$ and \f$ \sigma \f$ parameters can be calculated from the mean
   747  * and standard deviation with the following equations:
   597  * and standard deviation with the following equations:
   748  * \f$ \mu = ln(mean) - \frac{1}{2}ln\left(1+\frac{stddev}{mean^2}\right)\f$, and,
   598  * \f$ \mu = ln(mean) - \frac{1}{2}ln\left(1+\frac{stddev}{mean^2}\right)\f$, and,
   749  * \f$ \sigma = \sqrt{ln\left(1+\frac{stddev}{mean^2}\right)}\f$
   599  * \f$ \sigma = \sqrt{ln\left(1+\frac{stddev}{mean^2}\right)}\f$
   750  */
   600  */
   751 class LogNormalVariable : public RandomVariable { 
   601 class LogNormalVariable : public RandomVariable 
       
   602 {
   752 public:
   603 public:
   753   /**
   604   /**
   754    * \param mu mu parameter of the lognormal distribution
   605    * \param mu mu parameter of the lognormal distribution
   755    * \param sigma sigma parameter of the lognormal distribution
   606    * \param sigma sigma parameter of the lognormal distribution
   756    */
   607    */
   757   LogNormalVariable (double mu, double sigma);
   608   LogNormalVariable (double mu, double sigma);
   758 
   609 
   759   /**
       
   760    * \return A random value from this distribution
       
   761    */
       
   762   virtual double GetValue ();
       
   763   virtual RandomVariable* Copy() const;
       
   764 public:
       
   765   /**
   610   /**
   766    * \param mu mu parameter of the underlying normal distribution
   611    * \param mu mu parameter of the underlying normal distribution
   767    * \param sigma sigma parameter of the underlying normal distribution
   612    * \param sigma sigma parameter of the underlying normal distribution
   768    * \return A random number from the distribution specified by mu and sigma
   613    * \return A random number from the distribution specified by mu and sigma
   769    */
   614    */
   770   static double GetSingleValue(double mu, double sigma);
   615   static double GetSingleValue(double mu, double sigma);
   771 private:
       
   772   double m_mu;
       
   773   double m_sigma;
       
   774 };
   616 };
   775 
   617 
   776 /**
   618 /**
   777  * \brief Triangularly Distributed random var
   619  * \brief Triangularly Distributed random var
   778  * \ingroup randomvariable
   620  * \ingroup randomvariable
   779  * 
   621  * 
   780  * This distribution is a triangular distribution.  The probablility density
   622  * This distribution is a triangular distribution.  The probablility density
   781  * is in the shape of a triangle.
   623  * is in the shape of a triangle.
   782  */
   624  */
   783 class TriangularVariable : public RandomVariable {
   625 class TriangularVariable : public RandomVariable 
       
   626 {
   784 public:
   627 public:
   785   /**
   628   /**
   786    * Creates a triangle distribution random number generator in the
   629    * Creates a triangle distribution random number generator in the
   787    * range [0.0 .. 1.0), with mean of 0.5
   630    * range [0.0 .. 1.0), with mean of 0.5
   788    */
   631    */
   794    * \param s Low end of the range
   637    * \param s Low end of the range
   795    * \param l High end of the range
   638    * \param l High end of the range
   796    * \param mean mean of the distribution
   639    * \param mean mean of the distribution
   797    */
   640    */
   798   TriangularVariable(double s, double l, double mean);
   641   TriangularVariable(double s, double l, double mean);
   799 
       
   800   TriangularVariable(const TriangularVariable& c);
       
   801   
       
   802   /**
       
   803    * \return A value from this distribution
       
   804    */
       
   805   virtual double GetValue();
       
   806   virtual RandomVariable*  Copy() const;
       
   807 public:
       
   808   /**
   642   /**
   809    * \param s Low end of the range
   643    * \param s Low end of the range
   810    * \param l High end of the range
   644    * \param l High end of the range
   811    * \param mean mean of the distribution
   645    * \param mean mean of the distribution
   812    * \return A triangularly distributed random number between s and l
   646    * \return A triangularly distributed random number between s and l
   813    */
   647    */
   814   static double GetSingleValue(double s, double l, double mean);
   648   static double GetSingleValue(double s, double l, double mean);
   815 private:
       
   816   double m_min;
       
   817   double m_max;
       
   818   double m_mode;  //easier to work with the mode internally instead of the mean
       
   819                   //they are related by the simple: mean = (min+max+mode)/3
       
   820 };
   649 };
   821 
   650 
   822 }//namespace ns3
   651 }//namespace ns3
   823 #endif
   652 #endif