两个区间[a,b]之间的二重随机数生成
random number generation of doubles between two intervals [a,b]
我需要生成均匀分布在两个区间[a,b]之间的X
随机双数,其中a
和b
也是双数。
这些X
数字需要在类函数(比如myclass::doSomething(a,b)
)内部生成。问题是,每次doSomething(a,b)
函数被另一个类函数(比如doThat()
)调用时,传递给doSomething(a,b)
函数的间隔[a,b]
都会发生变化。
我想要一个允许我执行以下操作的解决方案:
1.有一个具有更高作用域的engine
,理想情况下,它应该在每次应用程序运行时只播种一次
2.对doSomething()
函数的每次调用内部生成的X
随机双数应均匀分布。
我下面的解决方案不允许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;
}
相关文章:
- 查找彼此之间具有不同最小距离的随机数
- 使用 std::binomial_distribution 在范围之间生成随机数
- 如何在 C++ 中获取 5 到 -5 之间的随机数
- C++ 和 Python 实现之间的不同伪随机数
- 生成随机数,并保证在给定域之间覆盖所有
- 我如何确保我的随机数在兰德生成的0和1之间不是0
- 在[0,1)C 之间生成一个随机数
- 0-1之间的随机数生成以填充容器
- 盒子muller随机数在C 中的0到1之间
- 在random_device和seed_seq之间做出决定,为多个随机数序列生成种子
- C++ 中介于 0 和 1 之间的随机数
- 本应生成 2 个介于 1 和 9 之间的随机数的程序C++只生成 6 和 9 并且没有正确循环
- 在0到1之间生成随机数
- 分叉进程之间的随机数是相同的
- 如何在 c++ 中生成 0 到 4 之间的随机数
- 介于 2 个值之间的随机数
- 如何在我存储的两个变量之间生成随机数
- 使用 C++ 的 1 到 10 之间的随机数
- 如何在c++中生成一个介于1和16之间的随机数
- C++生成一个介于 0 和 100,000 之间的随机数