2

std::variate_generator在一个VS2010项目中使用如下方式:

#include <random>
...
using std::variate_generator;
using std::mt19937;
using std::uniform_real_distribution;

typedef mt19937                                     Engine;
typedef uniform_real_distribution<float>            Distribution;
typedef variate_generator< Engine, Distribution >   Generator;  

Generator r( Engine((DWORD)time(NULL)), Distribution(0.0f, 1.0f) ); 

// from now, calling float rnd = r() gave me a random number between 0.0f and 1.0f in rnd.

我现在已将此代码放入 VS2012 解决方案中,我收到的错误消息是它std::variate_generator不是std.

std::variate_generator移动或被移除?

4

1 回答 1

4

variate_generators 未在<random>. variate_generator 是 tr1 的一部分,但从未成为标准,所以我对std::variate_generator在 VS2010 中工作(而不是std::tr1::variate_genrator)感到有点惊讶。我相信它仍然存在于 VS2012 的 tr1 命名空间中。

您可以改为执行以下操作:

#include <random>
#include <functional> // for std::bind
...
using std::mt19937;
using std::uniform_real_distribution;

typedef mt19937                                     Engine;
typedef uniform_real_distribution<float>            Distribution;

auto r = std::bind(Distribution(0.0f, 1.0f), Engine((DWORD)time(NULL)));

// from now, calling float rnd = r() gave me a random number between 0.0f and 1.0f in rnd.
于 2012-12-03T21:17:11.490 回答