Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pseudo random string

Tags:

string

php

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 e.g.:

  1. BBBBBBBBBB
  2. BBBBBBBBBC
  3. BBBBBBBBBD

and so on.. Any ideas?

like image 750
ariel Avatar asked Sep 06 '26 13:09

ariel


1 Answers

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 echoing. 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
like image 186
lonesomeday Avatar answered Sep 08 '26 04:09

lonesomeday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!