2

I am looking to generate a random string based on the amount of products I have. For instance I have 100 products named "test" I want to be able to generate 100 product codes which will all be unique.

I am currently using this code:

<?php   
/**
* The letter l (lowercase L) and the number 1
* have been removed, as they can be mistaken
* for each other.
*/

function createRandomPassword() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
// Usage
$password = createRandomPassword();
echo "Your random password is: $password";
?>

Cheers

2
  • 1
    You need to include a reference to the created datetime at some point. Your random numbers and using modulo are not guranteed to return a unique value. Commented Feb 17, 2012 at 18:20
  • 1
    Worth noting that rand() is a bad option for random integers - look at mt_rand() instead for better randomness. also, seeing as you removed l and 1, look at just using base58 - which is similar, but also removes o and 0 for readability reasons. Commented Feb 17, 2012 at 23:28

2 Answers 2

2

Using your function you can generate 100 random strings, i.e.

$product_names = array ();
for ($i=0; $i < 10; $i++ )
  $product_names[] = "code-" . createRandomPassword();

print_r ( $product_names );

Your question is not clear though. Do you have a naming convention you want to follow, do you want to generate codes in a pattern, such as 'product1', 'product2', ... , 'product100', etc ?

EDIT: The code above creates the following output:

Array
(
    [0] => code-opt6ggji
    [1] => code-4qfjt653
    [2] => code-8ky4xxo0
    [3] => code-dfip2o5x
    [4] => code-3e3irymv
    [5] => code-dgqk0rzt
    [6] => code-3fbeq0gr
    [7] => code-tev7fbwo
    [8] => code-idg04mdm
    [9] => code-8c2uuvsj
)
Sign up to request clarification or add additional context in comments.

3 Comments

Melissa - i would like to generate 100 completly random codes but to all start with say "code-" and then the random characters after.
Melissa - i have implemented your code but the array is returning empty? any ideas?
I tested the code again and updated the answer with demo output. Looks like it works just fine.
1

There is already a built in function that will handle this for you trivially. uniqid generates a prefixed unique identifier based on the current time in microseconds.

http://php.net/manual/en/function.uniqid.php

<?php
// loop 100 times
for ($i=0; $i<100; $i++)
{
  // print the uniqid based on 'test'
  echo uniqid('test', true);
}
?>

It is worth noting that to ensure true uniqueness you would need to store all the codes generated and check that no duplicated are issued.

3 Comments

Hi Fraser, this is exactly what i need but unfortunatly it would mean i would have to echo out however many times i want. is there away around that?
Thanks Fraser. is there anyway to customise how many characters are echo'ed out?
Not really without compromising the uniqueness of the result. You could set the more_entropy param to false, or just leave it out, which would shorten them. echo uniqid('test');

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.