1

我有一个简单的拒绝抽样应用程序,它包含在一个类中并在外部使用,如下面的虚拟示例所示。我能够将这篇文章改编为 boost::multiprecision 用例。但是我不确定如何适当地播种generator并且找不到任何random_device等效的提升。

下面的代码“有效”,但如果您快速连续多次运行它,您将获得我不想要的相同随机数。有比 更敏感的东西time(NULL)吗?

#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/random.hpp>

using namespace boost::multiprecision; // used only for SO post
using namespace boost::random;

typedef independent_bits_engine<boost::mt19937, std::numeric_limits<cpp_dec_float_50>::digits, cpp_int> generator;


generator &gen()
{
  thread_local static generator genny(time(NULL));
  return genny;  
}

class dummy {
    public:
        dummy() = default;
        cpp_dec_float_50 rejectionSample() {
           uniform_real_distribution<cpp_dec_float_50> ur(0,1);
           cpp_dec_float_50 x = ur(gen());
           while (x > 0.1) 
               x = ur(gen());
           return x;
        }
};



int main()
{
    std::cout << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10) << std::showpoint;

    dummy d;
    int nToGet = 5;
    for (int i = 0; i < nToGet; ++i) 
        std::cout << d.rejectionSample() << std::endl;
}
4

1 回答 1

1

这有效:

#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <random>
#include <boost/random.hpp>

using namespace boost::multiprecision;

typedef boost::random::independent_bits_engine<boost::mt19937, std::numeric_limits<cpp_dec_float_50>::digits, cpp_int> generator;

class dummy {
public:
    dummy()
    {
        ur_ = boost::random::uniform_real_distribution<cpp_dec_float_50>(0,1);
        std::random_device rd;
        gen_ = generator(rd());
        ur_(gen_);
    }

    cpp_dec_float_50 rejectionSample() {
        cpp_dec_float_50 x = ur_(gen_);
        while (x > 0.1)
        {
            x = ur_(gen_);
        }
        return x;
    }
private:
    boost::random::uniform_real_distribution<cpp_dec_float_50> ur_;
    generator gen_;
};



int main()
{
    std::cout << std::setprecision(std::numeric_limits<cpp_dec_float_50>::digits10) << std::showpoint;
    
    dummy d;
    int nToGet = 50;
    for (int i = 0; i < nToGet; ++i) {
        std::cout << d.rejectionSample() << std::endl;
    }
}

所以你的误解是:

  • uniform_real_distribution在每次调用时都调用了构造函数。本身没有错,但价格昂贵。
  • 您在每次通话时都重新播种生成器,使用time(NULL). 这真的很糟糕;以这种方式,您甚至没有真正获得伪随机序列;您正在使用非递减时钟为生成器播种,并且仅使用出现的第一个值。
  • 似乎您对分发的工作方式的语法感到困惑,因为您对gen(). 您正在传递一个可调用的 to uniform_real_distribution
  • 你不应该再使用time(NULL)种子生成器了;我们有rdseed更好的汇编指令。这就是所谓的std::random_device

最后,uniform_real_distribution内部不做拒绝抽样吗?那么你为什么需要这样做呢?

于 2020-08-14T13:22:19.363 回答