# Thread: Randomly generate unique alphanumeric numbers?

1. ## Randomly generate unique alphanumeric numbers?

Hey, I need some help with generating ID's for a system. I have a script that processes 3000+ records a second and runs continuously. Each record needs to have its own unique ID. The catch is that I can only have 10 characters in the ID. I can have A-Z, a-z, and 0-9. Is there a way to generate unique numbers using those characters? I am trying to do it without a system checking if the ID already exists.

So to sum it up:

Making an ID Generator
- 10 characters
- Can have A-Z, a-z, 0-9
- No system that will check if the ID exists
- Needs to generate ID's faster than 3000 second

Note: I realize sooner or later I will have used up every possible combination that I can create.

EDIT: I dont want to use an auto incrementing field in the DB because I will only have 9,999,999,999 combinations and need more than that.

Does any of you PHP guru's on this forum have an idea on how to go about this or if this can even be done using PHP?

2. Why would you not use an auto incrementing field within the database?

I guess you could get the id of the last inserted record and build on top of that, but this sounds awfully complicated for what would be an easy solution with an auto incrementing id.

3. Originally Posted by SilverBulletUK
Why would you not use an auto incrementing field within the database?

I guess you could get the id of the last inserted record and build on top of that, but this sounds awfully complicated for what would be an easy solution with an auto incrementing id.
I could use that but once the field hits 9,999,999,999 I would be out of luck for more ID's. Adding the alphabets increases the combination.

If I did my math correctly I would be out of ID's in 38 days.

4. Yah you can. You can also just use 0-9 to generate enough unique ids. The add alphabetical characters aren't necessary. With 10 characters you can generate 10^10 unique ids. With a-Z and 0-9 you can generate (10 + 26 + 26)^10 which is a bit much.

You can easily use a database to create the ids using a primary keys and auto_increment to handle the incrementation and id assignment.

5. Originally Posted by PHP Monkey
I could use that but once the field hits 9,999,999,999 I would be out of luck for more ID's. Adding the alphabets increases the combination.
What is the likely hood that you will hit 9,999,999,999? 9 billion isn't exactly a small number >.>

6. Originally Posted by imaginethis
You can easily use a database to create the ids using a primary keys and auto_increment to handle the incrementation and id assignment.
Can you explain the bit I quoted from your post?

EDIT: 9 Billion is a small number in my field, cant go into detail, most of you should know why.

7. I hear what your saying, but could not use an unsigned BigInt?

This would give you 18,446,744,073,709,551,615 records.

8. Originally Posted by imaginethis
What is the likely hood that you will hit 9,999,999,999? 9 billion isn't exactly a small number >.>
3000 rows a second.

9999999999 / 300 / 3600 / 24 = 38,5 days

9. Originally Posted by PHP Monkey
Can you explain the bit I quoted from your post?
There isn't anything to it really. When you insert into table that has a primary key column and is set to auto_increment the column, on insert it will generate a unique number.

Originally Posted by guido2004
3000 rows a second.

9999999999 / 300 / 3600 / 24 = 38,5 days
>.> ... I didn't see the 3000 a second...

10. Originally Posted by SilverBulletUK
I hear what your saying, but could not use an unsigned BigInt?

This would give you 18,446,744,073,709,551,615 records.
Pardon my limited understanding here, could you explain that? I am only limited to a value that is 10 characters in length. I am a designer and my coding skills are a bit limited.

Originally Posted by guido2004
3000 rows a second.

9999999999 / 3000 / 3600 / 24 = 38,5 days
Ah, my math was right, thanks for confirming, forgot a 0 in 3000 in the formula though.

Originally Posted by imaginethis
There isn't anything to it really. When you insert into table that has a primary key column and is set to auto_increment the column, on insert it will generate a unique number.
That would still result in a value that consists of only numbers no?

11. Originally Posted by PHP Monkey
I am only limited to a value that is 10 characters in length.
Why?

12. Originally Posted by PHP Monkey
Pardon my limited understanding here, could you explain that? I am only limited to a value that is 10 characters in length. I am a designer and my coding skills are a bit limited.

Ah, my math was right, thanks for confirming, forgot a 0 in 3000 in the formula though.

That would still result in a value that consists of only numbers no?
Well if you want a simple solution you can convert form base 10 to base 62. If you combine a database that simple exists to allow for the persistence of unique keys then create a algorithm to convert the bases? ._. ... Sorry its early. So that's the simplest solution I can think of.

13. Originally Posted by guido2004
Why?
Orders from higher up. Kind of unreasonable since we are dealing with such a huge amount of records.

EDIT: An idea I had was have an array of the alphabets and create the ID with microtime and incorporate the alphabets from the array based on a formula.

14. If the id must be guaranteed unique, you really should be using an auto increment field in the database. microtime does not guarantee uniqueness, although if only one machine/thread/process is generating the id's, its pretty unlikely youll have collision.

If you need to compact the id to 10 characters, convert between bases like suggested by imaginethis. See the documentation and user comments for base_convert().

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•