JavaEar 专注于收集分享传播有价值的技术资料

Suggestion to combination generator

I want to write combination generator. Generator must create combination in this format AAAAA-BBBBB-CCCCC-DDDDD-EEEEE

It can be letters or numbers but only from my array

$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");

In one sector for example AAAAA can't be 5 the same letters or numbers

And in all 5 sectors can't be 15 the same letters or numbers

After generate script check in MySQL is it unique combination and when database haven't got record with this combo show in browser.

It works but not enought good because after 641376 records in MySQL script give me info to try again because generated combo is in database.

How to fix that because i know is it more combination than 641376 but my generator can create only the same with database

I attached code to my script

<?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>";

}

?>

3个回答

    最佳答案
  1. You can use a rainbow table algorithm. It avoid collisions because it uses the result of the input to generate new hash values.

  2. 参考答案2
  3. I didn't knew the array_rand() function and just made some tests with it...

    I've noticed that the results from this function are not entirely random: the values of your $rand_keys are always ordered and never repeated. I believe this is why you cannot archive all the combinations you need...

    Try using:

    $input[rand(0,sizeof($input)-1)]
    

    to get your random values!

  4. 参考答案3
  5. As I mentioned in my comment to your question, you have two issues that are limiting the count of your results.

    1. The result of array_rand is ordered. You could apply the function shuffle on the result of array_rand() to avoid this problem, but
    2. You won't use array_rand() at all. It will give you five different values, but you're allowing up to four times the same value per section.

    Alternative approach

    Get an array of 25 random elements out of your input array

    Check this array with your conditions:

    • Is there an element 15 times and more
    • Check every section, wether it contains only the same value

    If one of the checks fails get the next array of 25 random elements until you've got a valid combination.

    Explanation of the checks

    Element with a frequency of 15 and higher

    Use the function array_count_values to get an array with the values of the original array as keys and their frequency in array as values. We sort that in descending order with the rsort function. If the value of the first element is greater than 14, then this check failed.

    Check the section of having only the same value

    • Split the array with the array_chunk function in sections of 5 elements.
    • Remove the duplicates in every section with array_unique
    • If the section contains only one element now, then this check failed.

    Loop

    If any of those checks failed, the combination is not valid and we'll have another try until we get a valid one.

    Code:

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