简体   繁体   English

如何在GCC 5中为指数分布生成随机数

[英]how to generate a random number for an exponential distribution in GCC 5

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. 我现在使用的代码使用C ++ 11,并且与ns3不兼容。 I was wondering if there is a way to generate the random number that is compatible with c++ 5 compiler that ns3 uses. 我想知道是否有一种方法可以生成与ns3使用的c ++ 5编译器兼容的随机数。 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 如果您喜欢煮熟的溶液,请使用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: 将这些代码移植到C ++ 11之前的版本时,我想到了一些想法:

  1. Use boost. 使用升压。
    Boost random_device and boost::mt19937 should work just as well as the C++11 standard versions. Boost random_deviceboost::mt19937应该和C ++ 11标准版本一样工作。 Boost also has its own uniform_real_distribution , which was the prototype for the standard stuff. Boost还具有自己的uniform_real_distribution ,它是标准内容的原型。

  2. Bring the implementations in tree. 将实现放入树中。
    The paper that introduced mersenne twister random number generators included a reference implementation of the generator. 介绍mersenne twister随机数生成器的论文包括该生成器的参考实现。

http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf 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. 如果您主要对网络测试感兴趣,那么您可能不必在意跨平台(特别是在Windows上工作。) std::random_device的libc ++实现只有十几行代码,它所做的只是开放的/dev/random作为文件,并且reinterpret_cast从该文件中读取为uint32_t并将其返回。

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. 您可以查看std::random_device的msvc版本,如果您想在Windows上工作,也可以将其合并... iirc现在没有mingw实现,而Windows使用了一些加密API。

  1. Use some other open source rng library. 使用其他一些开源rng库。 You could look at trng . 你可以看看trng

NS-3 provides an Exponential Random Variable from which you can get the values you want. NS-3提供了一个指数随机变量,您可以从中获取所需的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM