SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    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. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP Monkey View Post
    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 >.>
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    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. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I hear what your saying, but could not use an unsigned BigInt?

    This would give you 18,446,744,073,709,551,615 records.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by imaginethis View Post
    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. #9
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP Monkey View Post
    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.

    Quote Originally Posted by guido2004 View Post
    3000 rows a second.

    9999999999 / 300 / 3600 / 24 = 38,5 days
    >.> ... I didn't see the 3000 a second...
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  10. #10
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    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.

    Quote Originally Posted by guido2004 View Post
    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.

    Quote Originally Posted by imaginethis View 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.
    That would still result in a value that consists of only numbers no?

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by PHP Monkey View Post
    I am only limited to a value that is 10 characters in length.
    Why?

  12. #12
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP Monkey View Post
    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.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  13. #13
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    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. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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().


Bookmarks

Posting Permissions

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