2016-03-14 95 views
0

我想要一個指數分佈來控制何時佔用一個通道和多長時間。我現在的代碼使用C++ 11,並且與ns3不兼容。我想知道是否有辦法生成與ns3使用的C++ 5編譯器兼容的隨機數字。當前的代碼如何在GCC中爲指數分佈生成一個隨機數5

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; 
+4

什麼是C++ 5?你的意思是GCC 5? – Cameron

+0

是的ns3的編譯器正在發佈此錯誤。 錯誤:#error該文件需要ISO C++ 2011標準的編譯器和庫支持。 –

+0

我要猜測,如果你看看完整的錯誤信息,它可能會告訴你如何解決這個問題。 http://stackoverflow.com/questions/10363646/compiling-c11-with-g –

回答

0

NS-3提供了一個Exponential Random變量,您可以從中獲取所需的值。

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(); 
0

有浮現在腦海中的一些想法用於將此代碼移植到Pre-C++ 11:

  1. 使用噓噸。
    Boost random_deviceboost::mt19937應該和C++ 11標準版本一樣好。 Boost也有自己的uniform_real_distribution,這是標準東西的原型。

  2. 將實現帶入樹中。
    介紹mersenne twister隨機數發生器的論文包含了發生器的參考實現。

http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf

如果你的主要興趣在網絡測試中,你可能不關心是跨平臺(特別是在Windows上運行。)提供的libC++實現的std::random_device只有十幾代碼行全部是將/dev/random作爲文件打開,reinterpret_cast從該文件讀取uint32_t並返回它們。

你可以看看std::random_device的msvc版本,並且如果你想在windows上工作,那麼也可以使用它...... iirc目前沒有mingw的實現,並且Windows使用了一些加密API。

  1. 使用其他一些開源的rng庫。你可以看看trng
相關問題