0

I'm using this function to create a random string, but how would I make it to generate a unique string list for a 500 strings?

function generateRandomString($length = 8) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

3 Answers3

1

If you are worry about inserted rows in table then there are 3 simple ways to do it.

First way, you can check if the string exists in db then create another random string.

Second way, use UUID() function of mysql that generates random value and there's no need to worry about duplication.

Third way, if you want the array, generate 500 string and put in an array. then use array_unique() to remove duplication. If array length was < 500 then generate more (for example 12 strings) then check if < 500 again and again until it reaches exactly 500. Checking can be done in a while loop or so.

while( count($arr) < 500 )
{
    $arr[] = generateRandomString();

    if( count($arr) == 500 )
        $arr = array_unique( $arr );

}
Mahdyfo
  • 1,155
  • 7
  • 18
0

@MohamedFadili You need to first generate your string using generateRandomString(); then check if it exists in database in that case you need to keep trying till you find one that does not exist, to avoid a dead loop, you can throw an exception after X attempts (X = 10 maybe ?) so the code would look something like below :

a. If you have a unique constraint in your database (it would throw an exception when you insert)

$attempts = 10;
$i = 0;
$inserted = false;
while (!$inserted || $i < $attempts) {
try {
    $myRandomString = generateRandomString();
    // MySQL CODE TO DO YOUR INSERT HERE IT COULD THROW AN EXCEPTION IF THE STRING ALREADY EXIST IN DATABASE

    // here if insertion operation was successful you set $inserted variable to true
    $inserted = true;
    } catch (PDOException $ex){
    // do nothing
    }
}

b. if no unique index constraint in database, you need to check by your self:

$attempts = 10;
$inserted = false;

while (!$inserted || $i < $attempts){
$myRandomString = generateRandomString();

// SQL CODE TO CHECK IF $myRandomString Exist 
// IF NOT EXIST DO INSERT AND SET $inserted to true
$inserted = true;

// ELSE IF EXIST KEEP TRYING

}
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
0

The trick is not generating a list of 500 strings. Clearly, you can accomplish that with a for loop:

$random = array();
for ($i = 0; $i < 500; $i++)
    $random[] = generateRandomString();

What you want is 500 unique strings. As @meh points out, you could do this by continually eliminating duplicates with array_unique. I'm not a huge fan of doing that on every pass, though, as it results in generating a new, unique array at minimum 500 times! What I would prefer to do would be to check prior to the insertion of each value into the array that it isn't a duplicate. A search is faster than a search and removal which in turn is faster than an n² search and removal. Something like this, perhaps:

$unique = array();
for ($i = 0; $i < 500; $i++)
{
    do
    {
        $random = generateRandomString();
    } while (in_array($random, $unique));
    $unique[] = $random;
}
Mr. DOS
  • 379
  • 2
  • 13