微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

程序输出的随机数序列与Linux上实际的rand输出不匹配在Windows上正常工作

希望我可以再次问这个问题,现在我的问题被自动删除了?

所以,这就是我想要做的:

程序检查rand()%10输出的数字序列是否为某些数字串。 在这种情况下,string444和555.因此,在Windows上,程序find的第一个包含这些string的序列(仅查看前10个数字)是种子61163输出的序列。序列是3355444555。

现在,为了确认,我用一个简单的程序(下面显示的两个程序)检查rand()%10实际输出的结果是3355444555.非常期待。

到目前为止一切都很有意义

但是,在Linux上,程序的输出与rand()实际输出内容不匹配。 因此,例如,程序在前10个数字中find的包含string444和555的第一个序列是由种子154950输出的序列。序列为4555232444。

仍然一切都很有意义(rand()在Windows / Linux上有不同的实现,所以第一个发现的序列和Windows完全不同)。

然而,这是一个问题:当检查rand()%10实际输出的种子时,与Windows不同,序列不匹配。 种子154950输出的序列实际上是1778785265.(用相同的简单程序testing)。

这是我的问题。 正如前面提到的那样,真正奇怪的部分是代码在Windows上工作,我不确定Linux特定的任何东西如何可能导致这种情况。

程序代码

#include <iostream> #include <sstream> #include <cstdlib> using namespace std; int main() { int result; string text; int minSeed; int maxSeed; string temp; cout << "Seed Seeker. Checks a range of random seeds for specific strings.n"; cout << "Random Seed: lowest value: "; getline(cin,temp); minSeed = atoi(temp.c_str()); cout << "Random Seed: highest value: "; getline(cin,temp); maxSeed = atoi(temp.c_str()); cout << "Generate how many random numbers / seed? "; getline(cin,temp); int rndRange = atoi(temp.c_str()); string lookForThisA="444"; string lookForThisB="555"; for (int j = minSeed; j <= maxSeed; ++j) { text = ""; for (int i = 0; i < rndRange; ++i){ result = rand() % 10; text += static_cast<ostringstream*>(&(ostringstream() << result))->str(); } if (text.find(lookForThisA) != std::string::npos) { if (text.find(lookForThisB) != std::string::npos) { std::cout << "nnnString found!!!!nn"; cout << text << "nnSeed: " << j << " "; } } } }

用来检查rand()输出的程序:

#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main() { srand(154950); cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; cout << rand() % 10; }

确保每次循环迭代都播种RNG:

for (int j = minSeed; j <= maxSeed; ++j) { srand(j); // <----- without that the RNG is not re-seeded text = "";

至于Windows与Linux我的猜测是,在Windows上(我认为Debug版本) srand是由CRT调用你和61163不是在这种情况下的种子,但只是10位数循环迭代之前得到到你的序列;-)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐