我想写组合生成器。 Generator必须以这种格式创建组合AAAAA-BBBBB-CCCCC-DDDDD-EEEEE
它可以是字母或数字,但只能来自我的数组
$input = array("2", "3", "4", "6", "7", "8", "9", "Q", "W", "R", "T", "Y", "P", "D", "F", "G", "H", "J", "K", "X", "C", "V", "B", "M");
在一个扇区中,例如AAAAA不能是相同的字母或数字
并且在所有5个扇区中不能有15个相同的字母或数字
在MySQL生成脚本检查后,它是唯一的组合,当数据库没有在浏览器中使用此组合节目进行记录时。
它有效,但不是很好,因为在MySQL脚本中的641376条记录给我信息后再次尝试,因为生成的组合在数据库中。
如何解决这个问题,因为我知道它比641376组合更多,但我的生成器只能创建与数据库相同的
我将代码附加到我的脚本
<?php
require 'config.php';
$input = array("2", "3", "4", "6", "7", "8", "9", "Q", "W", "R", "T", "Y", "P", "D", "F", "G", "H", "J", "K", "X", "C", "V", "B", "M");
$rand_keys = array_rand($input, 5);
$part1 = $input[$rand_keys[0]] . $input[$rand_keys[1]] . $input[$rand_keys[2]] . $input[$rand_keys[3]] . $input[$rand_keys[4]];
$rand_keys = array_rand($input, 5);
$part2 = $input[$rand_keys[0]] . $input[$rand_keys[1]] . $input[$rand_keys[2]] . $input[$rand_keys[3]] . $input[$rand_keys[4]];
$rand_keys = array_rand($input, 5);
$part3 = $input[$rand_keys[0]] . $input[$rand_keys[1]] . $input[$rand_keys[2]] . $input[$rand_keys[3]] . $input[$rand_keys[4]];
$rand_keys = array_rand($input, 5);
$part4 = $input[$rand_keys[0]] . $input[$rand_keys[1]] . $input[$rand_keys[2]] . $input[$rand_keys[3]] . $input[$rand_keys[4]];
$rand_keys = array_rand($input, 5);
$part5 = $input[$rand_keys[0]] . $input[$rand_keys[1]] . $input[$rand_keys[2]] . $input[$rand_keys[3]] . $input[$rand_keys[4]];
if (substr_count($part1, '1') == 5)
die('<div class="error">Za duzo 1 w czesci pierwszej</div>');
elseif (substr_count($part1, '2') == 5)
die('<div class="error">Za duzo 2 w czesci pierwszej</div>');
elseif (substr_count($part1, '3') == 5)
die('<div class="error">Za duzo 3 w czesci pierwszej</div>');
elseif (substr_count($part1, '4') == 5)
die('<div class="error">Za duzo 4 w czesci pierwszej</div>');
elseif (substr_count($part1, '6') == 5)
die('<div class="error">Za duzo 6 w czesci pierwszej</div>');
elseif (substr_count($part1, '7') == 5)
die('<div class="error">Za duzo 7 w czesci pierwszej</div>');
elseif (substr_count($part1, '8') == 5)
die('<div class="error">Za duzo 8 w czesci pierwszej</div>');
elseif (substr_count($part1, 'Q') == 5)
die('<div class="error">Za duzo Q w czesci pierwszej</div>');
elseif (substr_count($part1, 'W') == 5)
die('<div class="error">Za duzo W w czesci pierwszej</div>');
elseif (substr_count($part1, 'R') == 5)
die('<div class="error">Za duzo R w czesci pierwszej</div>');
elseif (substr_count($part1, 'T') == 5)
die('<div class="error">Za duzo T w czesci pierwszej</div>');
elseif (substr_count($part1, 'Y') == 5)
die('<div class="error">Za duzo Y w czesci pierwszej</div>');
elseif (substr_count($part1, 'P') == 5)
die('<div class="error">Za duzo P w czesci pierwszej</div>');
elseif (substr_count($part1, 'D') == 5)
die('<div class="error">Za duzo D w czesci pierwszej</div>');
elseif (substr_count($part1, 'F') == 5)
die('<div class="error">Za duzo F w czesci pierwszej</div>');
elseif (substr_count($part1, 'G') == 5)
die('<div class="error">Za duzo G w czesci pierwszej</div>');
elseif (substr_count($part1, 'H') == 5)
die('<div class="error">Za duzo H w czesci pierwszej</div>');
elseif (substr_count($part1, 'J') == 5)
die('<div class="error">Za duzo J w czesci pierwszej</div>');
elseif (substr_count($part1, 'K') == 5)
die('<div class="error">Za duzo K w czesci pierwszej</div>');
elseif (substr_count($part1, 'X') == 5)
die('<div class="error">Za duzo X w czesci pierwszej</div>');
elseif (substr_count($part1, 'C') == 5)
die('<div class="error">Za duzo C w czesci pierwszej</div>');
elseif (substr_count($part1, 'V') == 5)
die('<div class="error">Za duzo V w czesci pierwszej</div>');
elseif (substr_count($part1, 'B') == 5)
die('<div class="error">Za duzo B w czesci pierwszej</div>');
elseif (substr_count($part1, 'M') == 5)
die('<div class="error">Za duzo M w czesci pierwszej</div>');
elseif (substr_count($part2, '2') == 5)
die('<div class="error">Za duzo 2 w czesci drugiej</div>');
elseif (substr_count($part2, '3') == 5)
die('<div class="error">Za duzo 3 w czesci drugiej</div>');
elseif (substr_count($part2, '4') == 5)
die('<div class="error">Za duzo 4 w czesci drugiej</div>');
elseif (substr_count($part2, '6') == 5)
die('<div class="error">Za duzo 6 w czesci drugiej</div>');
elseif (substr_count($part2, '7') == 5)
die('<div class="error">Za duzo 7 w czesci drugiej</div>');
elseif (substr_count($part2, '8') == 5)
die('<div class="error">Za duzo 8 w czesci drugiej</div>');
elseif (substr_count($part2, '9') == 5)
die('<div class="error">Za duzo 9 w czesci drugiej</div>');
elseif (substr_count($part2, 'Q') == 5)
die('<div class="error">Za duzo Q w czesci drugiej</div>');
elseif (substr_count($part2, 'W') == 5)
die('<div class="error">Za duzo W w czesci drugiej</div>');
elseif (substr_count($part2, 'R') == 5)
die('<div class="error">Za duzo R w czesci drugiej</div>');
elseif (substr_count($part2, 'T') == 5)
die('<div class="error">Za duzo T w czesci drugiej</div>');
elseif (substr_count($part2, 'Y') == 5)
die('<div class="error">Za duzo Y w czesci drugiej</div>');
elseif (substr_count($part2, 'P') == 5)
die('<div class="error">Za duzo P w czesci drugiej</div>');
elseif (substr_count($part2, 'D') == 5)
die('<div class="error">Za duzo D w czesci drugiej</div>');
elseif (substr_count($part2, 'F') == 5)
die('<div class="error">Za duzo F w czesci drugiej</div>');
elseif (substr_count($part2, 'G') == 5)
die('<div class="error">Za duzo G w czesci drugiej</div>');
elseif (substr_count($part2, 'H') == 5)
die('<div class="error">Za duzo H w czesci drugiej</div>');
elseif (substr_count($part2, 'J') == 5)
die('<div class="error">Za duzo J w czesci drugiej</div>');
elseif (substr_count($part2, 'K') == 5)
die('<div class="error">Za duzo K w czesci drugiej</div>');
elseif (substr_count($part2, 'X') == 5)
die('<div class="error">Za duzo X w czesci drugiej</div>');
elseif (substr_count($part2, 'C') == 5)
die('<div class="error">Za duzo C w czesci drugiej</div>');
elseif (substr_count($part2, 'V') == 5)
die('<div class="error">Za duzo V w czesci drugiej</div>');
elseif (substr_count($part2, 'B') == 5)
die('<div class="error">Za duzo B w czesci drugiej</div>');
elseif (substr_count($part2, 'M') == 5)
die('<div class="error">Za duzo M w czesci drugiej</div>');
elseif (substr_count($part3, '2') == 5)
die('<div class="error">Za duzo 2 w czesci trzeciej</div>');
elseif (substr_count($part3, '3') == 5)
die('<div class="error">Za duzo 3 w czesci trzeciej</div>');
elseif (substr_count($part3, '4') == 5)
die('<div class="error">Za duzo 4 w czesci trzeciej</div>');
elseif (substr_count($part3, '6') == 5)
die('<div class="error">Za duzo 6 w czesci trzeciej</div>');
elseif (substr_count($part3, '7') == 5)
die('<div class="error">Za duzo 7 w czesci trzeciej</div>');
elseif (substr_count($part3, '8') == 5)
die('<div class="error">Za duzo 8 w czesci trzeciej</div>');
elseif (substr_count($part3, '9') == 5)
die('<div class="error">Za duzo 9 w czesci trzeciej</div>');
elseif (substr_count($part3, 'Q') == 5)
die('<div class="error">Za duzo Q w czesci trzeciej</div>');
elseif (substr_count($part3, 'W') == 5)
die('<div class="error">Za duzo W w czesci trzeciej</div>');
elseif (substr_count($part3, 'R') == 5)
die('<div class="error">Za duzo R w czesci trzeciej</div>');
elseif (substr_count($part3, 'T') == 5)
die('<div class="error">Za duzo T w czesci trzeciej</div>');
elseif (substr_count($part3, 'Y') == 5)
die('<div class="error">Za duzo Y w czesci trzeciej</div>');
elseif (substr_count($part3, 'P') == 5)
die('<div class="error">Za duzo P w czesci trzeciej</div>');
elseif (substr_count($part3, 'D') == 5)
die('<div class="error">Za duzo D w czesci trzeciej</div>');
elseif (substr_count($part3, 'F') == 5)
die('<div class="error">Za duzo F w czesci trzeciej</div>');
elseif (substr_count($part3, 'G') == 5)
die('<div class="error">Za duzo G w czesci trzeciej</div>');
elseif (substr_count($part3, 'H') == 5)
die('<div class="error">Za duzo H w czesci trzeciej</div>');
elseif (substr_count($part3, 'J') == 5)
die('<div class="error">Za duzo J w czesci trzeciej</div>');
elseif (substr_count($part3, 'K') == 5)
die('<div class="error">Za duzo K w czesci trzeciej</div>');
elseif (substr_count($part3, 'X') == 5)
die('<div class="error">Za duzo X w czesci trzeciej</div>');
elseif (substr_count($part3, 'C') == 5)
die('<div class="error">Za duzo C w czesci trzeciej</div>');
elseif (substr_count($part3, 'V') == 5)
die('<div class="error">Za duzo V w czesci trzeciej</div>');
elseif (substr_count($part3, 'B') == 5)
die('<div class="error">Za duzo B w czesci trzeciej</div>');
elseif (substr_count($part3, 'M') == 5)
die('<div class="error">Za duzo M w czesci trzeciej</div>');
elseif (substr_count($part4, '2') == 5)
die('<div class="error">Za duzo 2 w czesci czwartej</div>');
elseif (substr_count($part4, '3') == 5)
die('<div class="error">Za duzo 3 w czesci czwartej</div>');
elseif (substr_count($part4, '4') == 5)
die('<div class="error">Za duzo 4 w czesci czwartej</div>');
elseif (substr_count($part4, '6') == 5)
die('<div class="error">Za duzo 6 w czesci czwartej</div>');
elseif (substr_count($part4, '7') == 5)
die('<div class="error">Za duzo 7 w czesci czwartej</div>');
elseif (substr_count($part4, '8') == 5)
die('<div class="error">Za duzo 8 w czesci czwartej</div>');
elseif (substr_count($part4, '9') == 5)
die('<div class="error">Za duzo 9 w czesci czwartej</div>');
elseif (substr_count($part4, 'Q') == 5)
die('<div class="error">Za duzo Q w czesci czwartej</div>');
elseif (substr_count($part4, 'W') == 5)
die('<div class="error">Za duzo W w czesci czwartej</div>');
elseif (substr_count($part4, 'R') == 5)
die('<div class="error">Za duzo R w czesci czwartej</div>');
elseif (substr_count($part4, 'T') == 5)
die('<div class="error">Za duzo T w czesci czwartej</div>');
elseif (substr_count($part4, 'Y') == 5)
die('<div class="error">Za duzo Y w czesci czwartej</div>');
elseif (substr_count($part4, 'P') == 5)
die('<div class="error">Za duzo P w czesci czwartej</div>');
elseif (substr_count($part4, 'D') == 5)
die('<div class="error">Za duzo D w czesci czwartej</div>');
elseif (substr_count($part4, 'F') == 5)
die('<div class="error">Za duzo F w czesci czwartej</div>');
elseif (substr_count($part4, 'G') == 5)
die('<div class="error">Za duzo G w czesci czwartej</div>');
elseif (substr_count($part4, 'H') == 5)
die('<div class="error">Za duzo H w czesci czwartej</div>');
elseif (substr_count($part4, 'J') == 5)
die('<div class="error">Za duzo J w czesci czwartej</div>');
elseif (substr_count($part4, 'K') == 5)
die('<div class="error">Za duzo K w czesci czwartej</div>');
elseif (substr_count($part4, 'X') == 5)
die('<div class="error">Za duzo X w czesci czwartej</div>');
elseif (substr_count($part4, 'C') == 5)
die('<div class="error">Za duzo C w czesci czwartej</div>');
elseif (substr_count($part4, 'V') == 5)
die('<div class="error">Za duzo V w czesci czwartej</div>');
elseif (substr_count($part4, 'B') == 5)
die('<div class="error">Za duzo B w czesci czwartej</div>');
elseif (substr_count($part4, 'M') == 5)
die('<div class="error">Za duzo M w czesci czwartej</div>');
elseif (substr_count($part5, '2') == 5)
die('<div class="error">Za duzo 2 w czesci piatej</div>');
elseif (substr_count($part5, '3') == 5)
die('<div class="error">Za duzo 3 w czesci piatej</div>');
elseif (substr_count($part5, '4') == 5)
die('<div class="error">Za duzo 4 w czesci piatej</div>');
elseif (substr_count($part5, '6') == 5)
die('<div class="error">Za duzo 6 w czesci piatej</div>');
elseif (substr_count($part5, '7') == 5)
die('<div class="error">Za duzo 7 w czesci piatej</div>');
elseif (substr_count($part5, '8') == 5)
die('<div class="error">Za duzo 8 w czesci piatej</div>');
elseif (substr_count($part5, '9') == 5)
die('<div class="error">Za duzo 9 w czesci piatej</div>');
elseif (substr_count($part5, 'Q') == 5)
die('<div class="error">Za duzo Q w czesci piatej</div>');
elseif (substr_count($part5, 'W') == 5)
die('<div class="error">Za duzo W w czesci piatej</div>');
elseif (substr_count($part5, 'R') == 5)
die('<div class="error">Za duzo R w czesci piatej</div>');
elseif (substr_count($part5, 'T') == 5)
die('<div class="error">Za duzo T w czesci piatej</div>');
elseif (substr_count($part5, 'Y') == 5)
die('<div class="error">Za duzo Y w czesci piatej</div>');
elseif (substr_count($part5, 'P') == 5)
die('<div class="error">Za duzo P w czesci piatej</div>');
elseif (substr_count($part5, 'D') == 5)
die('<div class="error">Za duzo D w czesci piatej</div>');
elseif (substr_count($part5, 'F') == 5)
die('<div class="error">Za duzo F w czesci piatej</div>');
elseif (substr_count($part5, 'G') == 5)
die('<div class="error">Za duzo G w czesci piatej</div>');
elseif (substr_count($part5, 'H') == 5)
die('<div class="error">Za duzo H w czesci piatej</div>');
elseif (substr_count($part5, 'J') == 5)
die('<div class="error">Za duzo J w czesci piatej</div>');
elseif (substr_count($part5, 'K') == 5)
die('<div class="error">Za duzo K w czesci piatej</div>');
elseif (substr_count($part5, 'X') == 5)
die('<div class="error">Za duzo X w czesci piatej</div>');
elseif (substr_count($part5, 'C') == 5)
die('<div class="error">Za duzo C w czesci piatej</div>');
elseif (substr_count($part5, 'V') == 5)
die('<div class="error">Za duzo V w czesci piatej</div>');
elseif (substr_count($part5, 'B') == 5)
die('<div class="error">Za duzo B w czesci piatej</div>');
elseif (substr_count($part5, 'M') == 5)
die('<div class="error">Za duzo M w czesci piatej</div>');
$code = $part1 . "-" . $part2 . "-" . $part3 . "-" . $part4 . "-" . $part5;
$count_numbers = substr_count($code, '2') + substr_count($code, '3') + substr_count($code, '4') + substr_count($code, '6') + substr_count($code, '7') + substr_count($code, '8') + substr_count($code, '9');
if ($count_numbers >= 15){
die ('<div class="error">Wygenerowano kod w ktorym jest 15 lub wiecej cyfr</div>');
}
$check_code = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM bot_gen WHERE code = '$code' LIMIT 1"));
if ($check_code[0] < 1) {
mysql_query("INSERT INTO bot_gen (code) VALUES ('$code');") or die ('Error with query.');
echo "<span class='code'>" . $code . "</span>";
} else {
echo "<div class='error'>You generate not unique combination.</div>";
}
?>
答案 0 :(得分:0)
您可以使用彩虹表算法。它避免了冲突,因为它使用输入的结果来生成新的哈希值。
答案 1 :(得分:0)
我不知道array_rand()函数,只是用它做了一些测试......
我注意到此函数的结果并非完全随机:$ rand_keys的值始终是有序且永不重复的。我相信这就是为什么你不能存档你需要的所有组合......
尝试使用:
$input[rand(0,sizeof($input)-1)]
获取随机值!
答案 2 :(得分:0)
正如我在对您的问题的评论中提到的,您有两个限制结果计数的问题。
从输入数组中获取25个随机元素的数组
根据您的条件检查此数组:
如果其中一项检查失败,则获得25个随机元素的下一个数组,直到您获得有效组合。
频率为15及更高的元素
使用函数array_count_values获取一个数组,其中原始数组的值为键,其数组的频率为值。我们使用rsort函数按降序排序。如果第一个元素的值大于14,则此检查失败。
检查只有相同值的部分
<强>循环强>
如果其中任何一项检查失败,则该组合无效,我们将再试一次,直到我们获得有效检查。
<?php
$input = array("2", "3", "4", "6", "7", "8", "9", "Q", "W", "R", "T", "Y", "P", "D", "F", "G", "H", "J", "K", "X", "C", "V", "B", "M");
$arr_result = array();
$valid = true;
do {// create an array with 25 random valid input characters
for($i = 0; $i < 25; $i++) {
$arr_result[] = $input[rand(0, count($input)-1)];
}
// check if an value will have a frequency of 15 and more
$result_values = array_count_values($arr_result);
rsort($result_values);
if ($result_values[0] > 14) {
$valid = false;
}
// check that no section contains only the same value
$sections = array_chunk($arr_result, 5);
foreach($sections as $section) {
$reduced = array_unique($section);
if (count($reduced) == 1) {
$valid = false;
}
}
} while (!$valid);
// valid array, let's build the combination
$sections = array_chunk($arr_result, 5);
$parts = array();
foreach($sections as $section) {
$parts[] = implode("", $section);
}
$result = implode("-", $parts);
echo $result; // try to add this result to your database instead ...
?>