Create random number with exponential distribution in c++ (visual stduio) -
i trying create random number exponential distribution. when tested simple example, worked well. when create in project, not work. project include many classes different objects. class in project:
user.h:
#include<iostream> #include<queue> #include<math.h> #include<random> #include"packet.h" using namespace std; class user { protected: queue<packet*> packetlist; const double rnd = 1.0 / 100; std::default_random_engine generator; exponential_distribution<> packetarrivalrate(double); // error if "packetarrivalrate(1.0/100)" public: void newpacket(); };
user.cpp:
#include"user.h"; exponential_distribution<> user::packetarrivalrate(double) { return exponential_distribution<>(rnd); } void user::newpacket() { packet* p = new packet; packetlist.push(p); double time = packetarrivalrate(generator); //this line error }
can me create "time" exponentially distributed random number.
the problem calling packetarrivalrate
generator
are parameter, when function expects double
. said function return exponential_distribution<>
needs generator give exponentially distributed numbers.
one simple (and ugly) way solve give packetarrivalrate
proper argument (note argument ignored function, value doesn't matter), , call operator()
on return value:
double time = packetarrivalrate(0)(generator);
alternative design
a more reasonable way not ignore argument, , provide rnd
:
double time = packetarrivalrate(rnd)(generator);
and define packetarrivalrate
this:
exponential_distribution<> user::packetarrivalrate(double lambda) { return exponential_distribution<>(lambda); }
random engines
please use mersenne twister pseudo-random generator (std::mt19937
or std::mt19937_64
) instead of std::default_random_engine
. may provide better random numbers. see this video author of <random>
.
Comments
Post a Comment