[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ns] Random Variables in ns (2)
On Thu, 11 Oct 2001, Miguel Angel Alvarez wrote:
> I am a student of Telecommunications Engineering and I am using ns for
> simulating internet traffic sources. I need to implement a Weibull
> distribution and a Gamma distribution random variable generators. I have
> been researching how ns implements its random variables (exponential,
> pareto, lognormal,...) but I have some doubts about this. Please, I would
> like to know:
>
> Which files are involved in the implementation of random variables in ns?
> I think that they are: random.cc .h, ranvar.cc .h, rng.cc .h
> I tried to develope a Weibull distribution but the constructor failed, so I
> think that it's necessary to modify some other files, so I would be very
> gratefull if someone told me some instructions for developing new random
> variables into ns source.
Hi,
I've successfully added a Weibull random variable. I added code
to ranvar.cc, ranvar.h, random.h, and rng.h. Here's what I did:
1) Added the following code to ranvar.cc. This sets up
RandomVariable/Weibull that you can call from a TCL script.
static class WeibullRandomVariableClass : public TclClass {
public:
WeibullRandomVariableClass() : TclClass("RandomVariable/Weibull") {}
TclObject* create(int argc, const char*const* argv) {
if (argc >= 6) {
return (new WeibullRandomVariable (
(double) atof(argv[4]),
(double) atof(argv[5])));
}
else {
return(new WeibullRandomVariable());
}
}
} class_weibullranvar;
WeibullRandomVariable::WeibullRandomVariable()
{
bind("shape_", &shape_);
bind("scale_", &scale_);
}
double WeibullRandomVariable::value()
{
return(rng_->rweibull(scale_, shape_));
}
2) Added the defs for these fns in ranvar.h.
class WeibullRandomVariable : public RandomVariable {
public:
virtual double value();
WeibullRandomVariable();
WeibullRandomVariable(double shape, double scale);
double* shapep() { return &shape_; };
double* scalep() { return &scale_; };
double shape() { return shape_; };
double scale() { return scale_; };
void setshape(double d) { shape_ = d; };
void setscale(double d) { scale_ = d; };
private:
double shape_;
double scale_;
};
3) Added the following line to rng.h. Here's the math.
inline double rweibull(double shape, double scale) {
return (pow (-log (uniform()), 1/shape) * scale);
}
4) Added the following line to random.h. I'm not positive this line is
needed, but I was following the format of lognormal() and it was included
here, too.
static double rweibull(double shape, double scale) {return
rng()->rweibull(shape, scale);}
Implementing Gamma should probably follow the same form. I don't know
anything about the empirical distribution implementation, though.
Good Luck,
Michele