两个区间[a,b]之间的二重随机数生成

random number generation of doubles between two intervals [a,b]

本文关键字:随机数 之间 二重 两个 区间      更新时间:2023-10-16

我需要生成均匀分布在两个区间[a,b]之间的X随机双数,其中ab也是双数。

这些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;
}