SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generating a random, unique code

    Hi,

    I am looking into some code that was done for our basket by someone else. I intend to use similar code myself in the new web site I am writing. it is pasted below.

    I realise this bit of code generates the customer transaction code. But what makes it random and unique? Why has the web designer used rand twice? Surely once would be fine!? And also how does this code always remember the codes used previously? The current site does not use MySQL. It just uses PHP in the basket but I can see no way it ensures it is unique. We have not encountered a problem with uniqueness of customer transaction numbers, so how does it make sure of this --- is it just lucky --- is there more than can be done --- particularly as the new site does use MySQL.

    PHP Code:
    $VendorTxCode "AGPS".(rand(0,32000)*rand(0,32000)); 
    Matt.

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read through what rand() does and your questions should then be answered.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it says nothing about uniqueness. in theory it could choose 876543 today and also 876543 in a years time too!! is this right?

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    md5 on a date stamp?

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    57 Post(s)
    Tagged
    0 Thread(s)
    This is strongly not recommended. Use an auto increment primary key on the database table that stores the transactions. With 32 bit integers even if your system takes a transaction every second you won't run out of id's for 60 years, and I imagine by that time you can rewrite to 64 bit integers and then see you in a few million years.

    Random transaction ids will, eventually, lead to a collision. Even if you use a hash function like md5 to create the string eventually you are going to get two data streams that create the same hash. When that happens your system will crash and it will be a royal pain in the a** to figure out why. The odds are extremely low, but they aren't non-zero and never underestimate Murphy's Law.

    In short, don't do it.

  6. #6
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post
    it says nothing about uniqueness. in theory it could choose 876543 today and also 876543 in a years time too!! is this right?
    Exactly, so you can see straight away that the random number will not be unique especially as you approach the finite set of possibilities.

    To generate a unique transaction code you need an auto incrementing counter of some sort - either in a database or some other kind of stored counter.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webdev1958 View Post
    Exactly, so you can see straight away that the random number will not be unique especially as you approach the finite set of possibilities.

    To generate a unique transaction code you need an auto incrementing counter of some sort - either in a database or some other kind of stored counter.
    Date time stamps don't repeat. Not unless.....deja vu all over again.

  8. #8
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pittendrigh View Post
    Date time stamps don't repeat. Not unless.....deja vu all over again.
    so you can use that or an auto increment counter - whichever suits best.


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
  •