gpt4 book ai didi

c++ - 使自己的随机数类与 uniform_int_distribution 兼容

转载 作者:太空宇宙 更新时间:2023-11-04 15:34:06 24 4
gpt4 key购买 nike

我创建了一个实现 MersenneTwister 算法以生成伪随机数的类。我的问题是如何让我的生成器使用默认的 std::uniform_int_distribution?我类(class)的标题如下:

class MersenneTwister
{
public:
void initialize(unsigned int seed);
unsigned int extract();

private:
unsigned int twist();
unsigned int _x[624];
signed int _index;
};

最佳答案

建模 UniformRandomBitGenerator 概念应该做到这一点:

http://en.cppreference.com/w/cpp/concept/UniformRandomBitGenerator

Screenshot of the concept's requirements

像这样:

#include <limits>

class MersenneTwister
{
public:
void initialize(unsigned int seed);
unsigned int extract();

//
// model the concept here
//

using result_type = unsigned int;
static constexpr result_type min() { return std::numeric_limits<result_type>::min(); }
static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
result_type operator()()
{
return extract();
}

private:
unsigned int twist();
unsigned int _x[624];
signed int _index;
};

关于c++ - 使自己的随机数类与 uniform_int_distribution 兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39615931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com