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

时间:2016-02-17 22:39:14

标签: c++ random

试图弄清楚为什么起始值会影响此代码中的种子随机数。我希望它能在12个字符或更多匹配的相同位置找到匹配,无论种子的起始值如何,但似乎我得到的结果不同,这取决于种子的起始值对我来说没有意义。任何人为什么我从0,1,2和3显示这些结果的开始值,当所有4应该标记相同的值匹配到12个以上的字符。

function isIsogram (str) {
    return !/(.).*\1/.test(str);
}

代码:

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 . . .

1 个答案:

答案 0 :(得分:2)

我现在明白你在问什么!问题是因为你在每次迭代时都会对str进行洗牌,而不是每次都将其重置为初始值,因此它会累积先前迭代的随机性中的随机性。

换句话说,你有两个“种子” - 一个在计数器中,你使用srand设置每次迭代,一个你不重置,包含在str的混洗字符顺序中。

为了保持每次迭代的一致性,您需要在每次重复之前将str重置为相同的基值。