gpt4 book ai didi

php - 对组合发电机的建议

转载 作者:行者123 更新时间:2023-11-29 23:59:43 26 4
gpt4 key购买 nike

我想编写组合生成器。生成器必须创建以下格式的组合 AAAAA-BBBBB-CCCCC-DDDDD-EEEEE

它可以是字母或数字,但只能来 self 的数组

$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 个相同的字母或数字

并且在所有 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>";

}

?>

最佳答案

正如我在对您的问题的评论中提到的,您有两个问题限制了结果的计数。

  1. array_rand 的结果已订购。您可以应用函数 shuffle基于 array_rand() 的结果来避免这个问题,但是
  2. 您根本不会使用 array_rand()。它将为您提供五个不同的值,但每个部分最多允许四倍的相同值。

替代方法

从输入数组中获取包含 25 个随机元素的数组

根据您的条件检查此数组:

  • 是否有某个元素出现 15 次及以上
  • 检查每个部分,是否仅包含相同的值

如果其中一项检查失败,则获取下一个包含 25 个随机元素的数组,直到获得有效的组合。

检查说明

频率为 15 及以上的元素

使用函数array_count_values获取一个数组,其中原始数组的值作为键,它们在数组中的频率作为值。我们按降序排列 rsort功能。如果第一个元素的值大于14,则本次检查失败。

检查仅具有相同值的部分

  • 使用 array_chunk 拆分数组函数由 5 个元素组成。
  • 使用 array_unique 删除每个部分中的重复项
  • 如果该部分现在仅包含一个元素,则此检查失败。

循环

如果其中任何一项检查失败,则该组合无效,我们将再次尝试,直到获得有效的组合。

代码:

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

关于php - 对组合发电机的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25118476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com