如何检查随机字符生成器的重复结果

时间:2015-03-09 13:46:15

标签: php

我制作了一个随机字符生成器,​​但如何检查结果是否重复? 例如,如果我想要100000个结果,如何检查100000结果中的结果是否重复?

for ($i=0; $i<$quantity; $i++){
$a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$code = "$a-$b-$c-$d";
echo $code .'<br>';
}

提前致谢!

4 个答案:

答案 0 :(得分:1)

您要做的第一件事就是将您生成的所有代码保存到数组中。

$codes = array();
for ($i=0; $i<$quantity; $i++){
    $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $code = "$a-$b-$c-$d";
    echo $code .'<br>';
    $codes[] = $code;
}

要检查它是否包含重复项,this answer建议采用以下解决方案:

if (count($codes) == count(array_unique($codes))) {
    // array contains duplicates
}

如果您对哪些代码完全重复感兴趣,请尝试使用此代码。我首先对数组进行排序(以确保相等的值是连续的),然后检查两个连续的值是否相等。

sort($codes);
for ($i = 0; $i < count($codes) - 2; $i++) {
    if ($codes[$i] == $codes[$i + 1]) {
        // $codes[$i] is a duplicate
    }
}

如果你暗示你想要摆脱重复,那就像使用array_unique一样简单:

$codes = array_unique($codes);

答案 1 :(得分:1)

这样的事可能吗?使用in_array()

$previous = array();

for ($i=0; $i<$quantity; $i++){
    $unique_found = false;
    while(!$unique_found){
        $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
        $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
        $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
        $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);

        $code = "$a-$b-$c-$d";

        if(!in_array($code,$previous)){
            $unique_found = true;
            $previous[] = $code;
            echo $code .'<br>';
        }
    }

}

答案 2 :(得分:0)

$myValues = array();
for ($i=0; $i<$quantity; (if($unique){$i++;})){
 $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $code = "$a-$b-$c-$d";
 $unique = false;
 if(!in_array($code,$myValues)){
   $myValues[] = $code;
   echo $code .'<br>';
   $unique = true;
 }
}

答案 3 :(得分:0)

您可以将代码用作数组中的键,因此重复的代码将被覆盖:

$results = array();
$quantity = 10000;
    while (count($results) < $quantity) {
       $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
       $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
       $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
       $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);

       $code = "$a-$b-$c-$d";
       $results[$code] = $code;

    }