2

Possible Duplicate:
PHP: How to generate a random, unique, alphanumeric string?

Does this formula produce collision ?

substr(md5(uniqid(rand()))),0,6)

How to generate a unique ID with only numbers (i.e 7 digits) ?

Community
  • 1
  • 1
Bertaud
  • 2,888
  • 5
  • 35
  • 48
  • 5
    If you want numbers only and want to avoid collision, the only way to do it is with an auto-increment value. Any random value will have the potential for collision given enough time and a large enough sample. – Michael Berkowski Apr 15 '12 at 12:10
  • In your case generating more than 10 million IDs will give 100% of collisions :) – nico Apr 15 '12 at 12:11
  • 2
    Was neither of the other similar questions on this site any help? – DanMan Apr 15 '12 at 12:12
  • The duplicate [PHP: How to generate a random, unique, alphanumeric string?](http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string) answers the following: Does `uniqid(rand())` already creates collision? If yes, yours will have these collisions as well. The answer to that question is: [Yes.](http://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string) – hakre Apr 15 '12 at 12:12
  • uniqid generates a 13 char string, how to reduce it to 7 char or 7 digits without risk of collision ? – Bertaud Apr 15 '12 at 12:45
  • @Bertaud: That is a completely different question. Please ask it separately or if it was the question you wanted to ask, edit your question. Otherwise this might get unnoticed in comments. – hakre Apr 15 '12 at 13:16

2 Answers2

2

uniqid generates a 13 char string, how to reduce it to 7 char or 7 digits without risk of collision ?

As long as you mean collision while transposing output of uniqid into a new representation, you can do it like the following:

  • uniqid returns a 13 character string, each character can be 0-9 or a-f, so basically a hexadecimal number with one place, 0-15 decimal.
  • As if you take two of those can be transformed into one character or digit in the range from 0-255, it's really easy to reduce 13 0-15 values into 7 0-255 values:

Example:

 $unique = uniqid(); # 13 characters
 $sevenPairs = array_map('hexdec', str_split($unique, 2));
 printf("Unique ID: %s\nSeven Pairs (digits): %s\n"
        , $unique, implode(', ', $sevenPairs));

Output:

Unique ID: 4f8ace1eb736a
Seven Pairs (digits): 79, 138, 206, 30, 183, 54, 10

Take care that if you make use of the prefix part (in your question you use rand), the length of the returned string by uniqid can be larger than 13.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

The easiest way is to get it from a database or text file, starting from 0 and incrementing it with 1 for each ID.

$id = strint(file_get_contents('counter.txt'));
file_put_content('counter.txt',++$id);

when using rand() there will be a bigger chance that you will get the same number. if you dont want an incremental value, use rand and check if the number is already used with a database.

Werring
  • 362
  • 3
  • 14