0

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;
gsamaras
  • 71,951
  • 46
  • 188
  • 305
B. Z.
  • 129
  • 11
  • 4
    What is C++ 5? Do you mean GCC 5? – Cameron Mar 14 '16 at 21:35
  • yes the compiler for the ns3 is giving out this error. error: #error This file requires compiler and library support for the ISO C++ 2011 standard. – B. Z. Mar 14 '16 at 21:37
  • I'm going to guess if you take a look at the complete error message it likely tells you how to fix the issue. http://stackoverflow.com/questions/10363646/compiling-c11-with-g – Retired Ninja Mar 14 '16 at 21:41
  • I only have a .cc file. it uses ./waf to build and compile the program. – B. Z. Mar 14 '16 at 21:53
  • Not sure what is "ns3". After googling around, made an educated guess, and added a tag. Please see if it's appropriate. In the future, please tag your questions appropriately, and/or provide links to terms which are not self-explanatory. – Ami Tavory Mar 14 '16 at 21:59

3 Answers3

0

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

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
0

There are a few ideas that come to mind for porting this code to pre-C++11:

  1. 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.

  2. 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.

  1. Use some other open source rng library. You could look at trng.
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
0

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 ();
Konstantinos
  • 556
  • 4
  • 9