C++ RNG (Mersenne Twister) 需要种子

发布于 2024-12-07 17:45:21 字数 658 浏览 1 评论 0原文

我编写了一个 RNG 类,它包含不同的算法,但是它没有按预期工作。除了我想使用正态(而不是均匀)分布这一事实之外,我的代码总是返回相同的数字(最大值)或仅返回区间 [min,max] 之外的 2 个数字:

std::function<int(int, int)> mt19937 =
    [](int min, int max) -> int {
        std::uniform_int_distribution<int> distribution(min, max);
        std::mt19937 engine;
        engine.seed(time(null));
        auto generator = std::bind(distribution, engine);
        return generator();
    };

任何人都可以解释一下解决这个难题时缺少什么?此外,我如何实现正态分布?上次我尝试 std::normal_distribution 我无法进入边界!

编辑:当我谈到正态分布时,我的意思是两个边界附近的 RNG 结果不应像两者的平均值那样频繁生成。例如,看一下标准高斯分布的图形表示。我指的是它,因为它可视化了结果值的概率,如果你理解的话,我想以这种方式实现/使用它。

I have written a RNG class which holds different algorithms, however it does not work as expected. Besides the fact that i want use normal (rather than uniform) distribution my code always returns either the same number (max) or just 2 numbers out of the interval [min,max]:

std::function<int(int, int)> mt19937 =
    [](int min, int max) -> int {
        std::uniform_int_distribution<int> distribution(min, max);
        std::mt19937 engine;
        engine.seed(time(null));
        auto generator = std::bind(distribution, engine);
        return generator();
    };

Can anyone explain me what is missing to solve this puzzle? Furthermore, how can i implement normal distribution? Last time i tried out std::normal_distribution i was not able to enter bounds!

EDIT: When i speak of a normal distribution i mean that the results of the RNG near the two bounds should not be generated as often as the mean of both. E.g. look at the graphical representation of the standard Gauss distribution. I am referring to it because it visualizes the probabilities of the resulting values which i want to implement/use this way, if you understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮倦 2024-12-14 17:45:21

正态分布就是这样(x 是一个随机均匀数):

Normal

但我明白了可能有问题的事情:

std::uniform_int_distribution<int> distribution(min, max);

这不是给你的数字生成器一个 int 类型吗?


要解决播种问题,请在 lambda 外部创建引擎,并在创建时为其播种。

RNG 使用一种算法来生成看似随机的数字,但具有非常长的重复周期(梅森旋转器的一个亮点)。当你做种子时,你给 RNG 一个初始值来开始这个过程。每次您询问另一个数字时,它都会输出算法的另一次迭代。

当您为每次迭代播种时:

time(NULL)

此代码每秒仅更改一次,因此当您请求新的随机数时,它每秒只会更改一次。

The normal distribution is just this (x is a random uniform number):

Normal

But I see something that could be problematic:

std::uniform_int_distribution<int> distribution(min, max);

Isn't this giving your number generator an int type?


To fix the seeding problem, create your engine outside of the lambda and seed it when you create it.

A RNG uses an algorithm that produces numbers that appear random, but have a a very large period of repetition (a highlight of the Mersenne Twister). When you seed, you give the RNG an initial value to start the process with. Each time you ask for another number, it spits out another iteration of the algorithm.

When you seed every iteration:

time(NULL)

this code changes only every second, so when you request a new random number, it will only change every second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文