0

I have made a string generator:

    <?php

function createRandomPassword() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 12) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}


// Usage
$product_names = array ();
for ($i=0; $i < 100; $i++ )
$product_names[] = "code-" . createRandomPassword();
?>

My problem is that I think there is a chance this could duplicate values, and its very important that doesnt happen as I will be generating about 700,000 of them.

Is there a good way to ensure the generated strings are not duplicates?

Thanks :)

1
  • 4
    Why go to all this trouble? Just use sha1() or md5(). As well, you do NOT re-seed the PRNG on each iteration of the loop. You seed it ONCE, and php does that for you automatically the first time you call rand() anyways. In practical terms, you'd only seed manually if you're testing and want the same 'random' stream each time. Commented Feb 27, 2012 at 14:47

3 Answers 3

1

The following example will generate 100 product names, remove the duplicates and generate new product names until the product name array has a count of 100.

$cnt = 0;
$product_names = array ();
while($cnt < 100) {
   for ($i=0; $i < 100 - $cnt; $i++ )
     $product_names[] = "code-" . createRandomPassword();
   $product_names = array_unique($product_names);
   $cnt = count($product_names);
}

You can put the number 100 into a variable or constant and exchange it with 700,000.

Sign up to request clarification or add additional context in comments.

Comments

0

Several options:

  1. Generate 700000 unique strings and shuffle them.
  2. Use GUIDs

Although your code suggests they are used as passwords; if they really are, then don't bother about duplicates, as there are no obvious candidates that are possibly more likely (e.g. password or qwerty123).

Comments

0

Create a look up/check table (in a database or file) or make an algorithm that makes a better unique key (using md5 or sha or something that's designed to be unique).

2 Comments

Some nitpicking here: SHA1 and MD5 are not designed to be collision-free, but have a very, very low probability of collision (which means they should be collision-free in practice).
@chiborg: Understood, and well-received, however based on the OP's post, anything with [even] a 1% chance of collision is a vast improvement over using rand() and a fixed-string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.