我需要生成在两个间隔[a,b]之间均匀分布的X
随机双数,其中a
和b
也是双数。
这些X
数字需要在类函数内部生成,例如myclass::doSomething(a,b)
。事实是,每次由另一个类函数[a,b]
调用doSomething(a,b)
函数时,传递给doSomething(a,b)
函数的间隔doThat()
都会更改。
我想要一个允许我执行以下操作的解决方案:
1.具有一个范围更大的engine
,理想情况下,每个应用程序运行仅应播种一次。
2.在每次对X
函数的单次调用中生成的doSomething()
随机双数应均匀分布。
我下面的解决方案不允许engine
的更大范围,而且似乎生成的数字不一定是均匀分布的。
//file: utilities.h
template <typename Generator>
double randomDoubleEngine(Generator& engine, double low_bound, double high_bound )
{
if (low_bound > high_bound){
std::swap(low_bound, high_bound);
}
return std::uniform_real_distribution<>( low_bound, high_bound )( engine );
}
//file: myclass.h
void myclass::doThat(param1, param2){
for(int i=0; i < myclass.iter; i++){
...
...
doSomething(a,b);
...
}
}
void myclass::doSomething(double a, double b)
{
std::random_device rd;
static std::mt19937 engine(rd());
.....
double curThreshold = randomDoubleEngine(engine, a, b);
...
}
最佳答案
我认为您希望引擎成为myclass的静态成员。我不确定这将与您所拥有的有什么真正的不同,除非您需要以其他方法使用引擎。我在下面粘贴了一个可能的解决方案。
还要注意,与标准相比,gcc看起来是错误的(请参阅代码注释中的链接),因此,如果使用它,它可能会解释为什么您对这些数字进行的任何测试(以检查均匀分布)失败了。据我了解,gcc希望引擎返回[0,1)中的数字,而标准表示它应该是某个范围内的统一整数。
恐怕我只能使用gcc 4.4进行测试,因为我运行的是旧版Ubuntu,而ideone似乎不允许std::random_device。
#include <random>
#include <iostream>
/* In GCC 4.4, uniform_real_distribution is called uniform_real; renamed in 4.5
*
* However, GCC's description here
*
* http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-api-4.6/a00731.html
*
* doesn't match expectation here
*
* http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
*
* which seems to match 26.5.8.2.2 of N3337
*
*/
#if defined(__GNUC_MINOR__) && (__GNUC_MINOR__ <= 4)
# define uniform_real_distribution uniform_real
#endif
template <typename Generator>
double randomDoubleEngine(Generator& engine, double low_bound, double high_bound)
{
if (low_bound > high_bound){
std::swap(low_bound, high_bound);
}
return std::uniform_real_distribution<double>(low_bound, high_bound)(engine);
}
class myclass
{
double curThreshold;
static std::mt19937 engine;
void doSomething(double a, double b)
{
curThreshold= randomDoubleEngine(engine, a, b);
}
public:
myclass(): curThreshold(0) {}
void doThat(){
doSomething(0,10);
std::cout << "threshold is " << curThreshold << std::endl;
}
};
std::mt19937 myclass::engine=std::mt19937(std::random_device()());
int
main()
{
myclass m;
m.doThat();
m.doThat();
return 0;
}