简体   繁体   English

Mersenne Twister c ++可能的最大种子

[英]Largest Seed Possible for Mersenne Twister c++

I would like to find out the largest value I can seed a random number generator in c++. 我想找出最大值,我可以在C ++中植入随机数生成器。 My code is as follows: 我的代码如下:

mt19937 myRandomGenerator(seed);

How large can the variable, seed be? 变量种子可以有多大? I have noticed that if the value becomes too large, the random number generator spits out the same sequence of 'random' numbers. 我注意到,如果值太大,则随机数生成器会吐出相同的“随机”数序列。 I would like to know how large the seed can be without this happening. 我想知道没有这种情况的种子有多大。

seed is std::uint_fast32_t which is usually just a 32-bit int. seedstd::uint_fast32_t ,通常只是一个32位int。 Every value in the range [0..2^32) should produce different results. [0..2^32)范围内的每个值都应产生不同的结果。 If you are seeing the same sequence from two different seed values, then you are either making an observational error and the seed you are inputing is actually the same, or there is a bug in your standard library implementation. 如果从两个不同的种子值看到相同的序列,则说明您犯了一个观察错误,而您输入的种子实际上是相同的,或者您的标准库实现中存在错误。

Prepare a short self-contained test program demonstrating the misbehaviour and post it here. 准备一个简短的独立测试程序,演示不当行为并将其发布在此处。

The wording of the question is somewhat ambiguous. 这个问题的措词有些含糊。 It is possible to read the question as asking about this behavior: 可以在询问有关此行为的问题时阅读该问题:

#include <random>
#include <iostream>

int
main()
{
    std::mt19937 eng1(4294967296);
    for (int i = 0; i < 10; ++i)
        std::cout << eng1() << '\n';
    std::cout << '\n';
    std::mt19937 eng2(0);
    for (int i = 0; i < 10; ++i)
        std::cout << eng2() << '\n';
}

My compiler spits out a warning which should be heeded: 我的编译器发出了一个警告,应该注意:

test.cpp:7:23: warning: implicit conversion from 'long' to 'result_type' (aka 'unsigned int') changes value from 4294967296 to 0 [-Wconstant-conversion]
    std::mt19937 eng1(4294967296);
                 ~~~~ ^~~~~~~~~~
1 warning generated.

If this is what you are seeing it is because 如果这就是您所看到的,那是因为

static_cast<std::mt19937::result_type>(4294967296) == 0

If this is not the behavior you're asking about, please clarify your question. 如果这不是您要问的问题,请澄清您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM