为什么起始值会影响种子随机的结果

Why does starting value affect outcome of Seeded Random?

本文关键字:种子 随机 结果 影响 为什么      更新时间:2023-10-16

试图找出为什么起始值会影响此代码中的种子随机值。我希望它在同一位置找到 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重置为相同的基值。