简体   繁体   中英

Pseudo random string

I need generate codes from this letters

$a = array(
    'B','C','D','F','G','H','J','K','L','M','N','O',
    'P','Q','R','S','T','V','W','X','Y','Z','1','2',
    '3','4','5','6','7','8','9','0'
);

But there are 2 conditions: every code must be unique and contains 10 letters. I don't want to get random because is unefficient. Instead, I want go for every letter eg:

  1. BBBBBBBBBB
  2. BBBBBBBBBC
  3. BBBBBBBBBD

and so on.. Any ideas?

I think that what you actually want is a list of sequential numbers (therefore not random at all) with a rather unconvetional base system. So BBBBBBBBB is 0 , while BBBBBBBC is 1 . This isn't hard to do, but obviously you have to code it yourself. Something like this might work:

function generate($num) {
    $num = base_convert($num, 10, 32); // convert the number to base 32
    $num = str_pad($num, 10, "0", STR_PAD_LEFT); // pad it with zeros to the left
    $num = str_replace(array(
        '0','1','2','3','4','5','6','7','8','9','a','b',
        'c','d','e','f','g','h','i','j','k','l','m','n',
        'o','p','q','r','s','t','u','v'
    ), array(
        'B','C','D','F','G','H','J','K','L','M','N','O',
        'P','Q','R','S','T','V','W','X','Y','Z','1','2',
        '3','4','5','6','7','8','9','0'
    ), $num); // replace the normal characters with your custom array

    echo $num, "\n";
}

for ($i = 0; $i < 10; $i++) generate($i);

Obviously you could change the 10 in the for statement to whatever you liked, and insert into a database rather than echo ing. Obviously 1.6m records would take some time to generate.

The above code gives the following output:

BBBBBBBBBB
BBBBBBBBBC
BBBBBBBBBD
BBBBBBBBBF
BBBBBBBBBG
BBBBBBBBBH
BBBBBBBBBJ
BBBBBBBBBK
BBBBBBBBBL
BBBBBBBBBM
<?php

$length = 3;

$letters = str_split("ABC"); // define your dictionary here

$index = array_fill(0, $length, 0);

$key = $length - 1;
while(true) {   
    $code = "";
    for($i=0;$i<$length;$i++) {     
        $code .= $letters[$index[$i]];      
    }
    echo $code ."<br/>"; // output code

    $index[$key]++;

    while(!isset($letters[$index[$key]])) {
        $index[$key] = 0;
        $key--;
        if($key < 0) {
            break 2;
        }       
        $index[$key]++;
    }   
    $key = $length - 1; 
}

Example:

<?php

$a = array(
    'B','C','D','F','G','H','J','K','L','M','N','O',
    'P','Q','R','S','T','V','W','X','Y','Z','1','2',
    '3','4','5','6','7','8','9','0'
);

function key_increment (&$symbols, $position = null) 
{
    global $a;
    if ($position === null) $position = count($symbols) - 1;
    if ($position == -1) return;

    $index = $symbols[$position];
    $index ++;
    if (!isset($a[$index])) {
        $v = 0;
        key_increment ($symbols, $position-1);
    }
    $symbols[$position] = $value;
}

function generate($length, $total) 
{
    global $a;

    $symbols = array_fill(0, $length, 0);


        for ($i = 0; $i < $total; $i ++) {
            $code = '';
        foreach ($symbols as $index) {
            $code .= $a[$index];
        }
        echo $code;
        echo '<br />';

        key_increment($symbols);
    }
}

generate(3, 100);

It's probably best that you just use uniqid, especially seeing as you said you'll be generating 1.6 million codes . If you're concerned about the case, just use strtoupper()

echo strtoupper(uniqid());

Generating all of these and inserting them is going to take a fair bit of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM