I wanted an exponential distribution to control when to occupy a channel and for how long. The code I have now uses C++ 11 and is not compatible with ns3. I was wondering if there is a way to generate the random number that is compatible with c++ 5 compiler that ns3 uses. current code is
std::random_device rd;
std::mt19937 gen(rd());
//std::uniform_real_distribution<> dis(1, std::numeric_limits<int>::max());
std::uniform_real_distribution<> dis(0,1);
long double length = log(1-dis(gen))/(-0.25);
std::cout<<length<<std::endl;
If you need exponentially distributed number, just use the log transformation
ed = -std::log(dis(gen));
If you prefer cooked solution, use http://en.cppreference.com/w/cpp/numeric/random/exponential_distribution
There are a few ideas that come to mind for porting this code to pre-C++11:
Use boost.
Boost random_device
and boost::mt19937
should work just as well as the C++11 standard versions. Boost also has its own uniform_real_distribution
, which was the prototype for the standard stuff.
Bring the implementations in tree.
The paper that introduced mersenne twister random number generators included a reference implementation of the generator.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf
If you are mainly interested in network testing, you probably don't care about being cross-platform (specifically, working on windows.) The libc++ implementation of std::random_device
is only about a dozen lines of code, all it does is open /dev/random
as a file, and reinterpret_cast
reads from that file as uint32_t
and return them.
You could look at the msvc version of std::random_device
and incorporate that also if you want to work on windows... iirc there is no mingw implementation of that right now, and the windows one uses some crypto API.
NS-3 provides an Exponential Random Variable from which you can get the values you want.
double mean = 3.14;
double bound = 0.0;
Ptr<ExponentialRandomVariable> x = CreateObject<ExponentialRandomVariable> ();
x->SetAttribute ("Mean", DoubleValue (mean));
x->SetAttribute ("Bound", DoubleValue (bound));
// The expected value for the mean of the values returned by an
// exponentially distributed random variable is equal to mean.
double value = x->GetValue ();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.