为什么起始值会影响种子随机的结果
Why does starting value affect outcome of Seeded Random?
试图找出为什么起始值会影响此代码中的种子随机值。我希望它在同一位置找到 12 个字符或更多匹配的匹配项,无论种子的起始值如何,但似乎我根据种子的起始值得到不同的结果,这对我来说毫无意义。任何为什么我得到这些结果的人,如 0、1、2 和 3 所示的起始值,当所有 4 个都应该标记相同的值作为匹配的 12 多个字符时。
Poor Key Finder
Search for 12 or more matches
Searching through Key Values of **0** thru 1000000000
WARNING - Program Running Please Wait...
25% Complete
50% Complete
75% Complete
**Greater or Equal to = 12 ===== 923425024**
100% Complete
Completed = 1000000000
Press any key to continue . . .
Poor Key Finder
Search for 12 or more matches
Searching through Key Values of **1** thru 1000000000
WARNING - Program Running Please Wait...
**Greater or Equal to = 12 ===== 204715678**
25% Complete
**Greater or Equal to = 12 ===== 346933630**
50% Complete
75% Complete
100% Complete
Completed = 1000000000
Press any key to continue . . .
Poor Key Finder
Search for 12 or more matches
Searching through Key Values of **2** thru 1000000000
WARNING - Program Running Please Wait...
25% Complete
50% Complete
75% Complete
100% Complete
Completed = 1000000000
Press any key to continue . . .
Poor Key Finder
Search for 12 or more matches
Searching through Key Values of **3** thru 1000000000
WARNING - Program Running Please Wait...
25% Complete
50% Complete
75% Complete
100% Complete
Completed = 1000000000
Press any key to continue . . .
法典:
#include <iostream>
#include <string>
int delay;
long long int counter1 = 0; // Add LL beyond 9 digits
long long int endcount = 0; // while loop end counter
long long int seed1 = 0;
int match2 = 0;
int ST = 0;
int flag = 0;
float progress = 0;
int step1 = 0;
int step2 = 0;
int step3 = 0;
int main()
{
system("color b0");
std::cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn";
std::cout << " Poor Key Finder Version 1.0n";
std::cout << " Build 01/30/2016n";
std::cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnn";
std::cout << " Enter Starting Key Valuen";
std::cin >> counter1;
std::cout << " Enter Ending Key Valuen";
std::cin >> endcount;
std::cout << " Enter Duplicate Character Counter Valuen";
std::cin >> flag;
system("cls");
std::string str =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\/~.,;";
std::string str2=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\/~.,;";
system("@echo. Started on %date% at %time%>>LogKey.txt");
system("color f0");
std::cout << "Poor Key Finder - Search for " << flag
<< " or more matches n";
std::cout << "Searching through Key Values of " << counter1 << " thru "<<endcount<<"n n";
std::cout << " WARNING - Program Running Please Wait...nn";
while (counter1 <= endcount)
{
seed1 = counter1;
srand(seed1);
random_shuffle(str.begin(), str.end()); // Shuffle the string
ST = 0;
match2 = 0;
progress = ((100 * counter1) / endcount);
if (progress == 25)
{
step1++;
if (step1 == 1)
{
std::cout << "25% Completen";
}
else
{
}
}
else if (progress == 50)
{
step2++;
if (step2 == 1)
{
std::cout << "50% Completen";
}
else
{
}
}
else if (progress == 75)
{
step3++;
if (step3 == 1)
{
std::cout << "75% Completen";
}
else
{
}
}
else if (endcount == counter1)
{
std::cout << "100% Completen";
}
else
{
}
while (ST <= 85)
{
if (str[ST] == str2[ST])
{
match2++;
}
else
{
}
ST++;
}
if (match2 >= flag)
{
std::cout << "Greater or Equal to = " << flag << " ===== " << seed1
<< "n";
}
else
{
}
counter1++;
}
std::cout << "Completed = " << endcount << "nnn";
system("@echo. Ended on %date% at %time%>>LogKey.txt");
system("pause");
return 0;
}
我现在明白你在问什么了! 问题在于,您在每次迭代中都对 str
进行随机播放,而没有每次都将其重置为其初始值,因此它会从先前迭代的随机性中累积随机性。
换句话说,你有两个"种子"——一个在计数器中,你使用srand
设置每次迭代,另一个你不重置,包含在str
的洗牌字符顺序中。
为了保持每次迭代的一致性,您需要在每次随机播放之前将str
重置为相同的基值。
相关文章:
- 如何使用要传递给 mt19937 的可选随机种子参数设计函数
- 如何使用可选的随机种子参数创建 roll_die() 函数
- 除了 rand() 之外,是否有任何随机生成器库,开发人员可以手动设置种子?
- 如果我在每个平台上使用相同的种子,随机结果会相同吗?
- 随机化种子值C
- 为什么使用迭代器时必须重新设定随机生成器种子?
- 在给定种子和偏移量的情况下生成下一个伪随机值
- 设置随机种子独立rmath库
- 与C++2011不相关的并行随机种子
- 我应该使用从std::random_device种子的随机引擎,还是每次都使用std::random_device
- 如何生成随机种子/哈希以使Rand实际上是随机的
- 生成一个随机种子给srand()-不是时间(NULL)
- 在哪里初始化随机种子以通过多个随机模块使用
- 为什么起始值会影响种子随机的结果
- 更改程序 C++ 中的随机种子
- C++麦克风输入作为随机种子
- 使用srand作为随机种子时出现重复输出
- 随机生成器,种子不工作
- 多精度随机生成器种子错误
- 在伪随机生成器中使用常量种子从UT和GUI中获得不相同的数字