VS2012中的std :: variate_generator

时间:2012-12-03 21:13:08

标签: c++ c++11 visual-studio-2012

我在VS2010项目中以下列方式使用std::variate_generator

#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移动或被移除了吗?

1 个答案:

答案 0 :(得分:4)

variate_generators未在<random>的最终标准版本中使用。 variate_generator是tr1的一部分,但从未进入标准,所以我在VS2010中工作时感到有些惊讶std::variate_generator(而不是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.
相关问题