dimanche 24 septembre 2017

Random 'GUID' and Possible Duplicates

(I am using 'guid' as a term for a random series of letters/numbers.)

I am using a function to create a GUID that will be unique. The function is:

function guid() {
    return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
                  mt_rand(0, 65535), mt_rand(0, 65535),
                  mt_rand(0, 65535), mt_rand(16384, 20479),
                  mt_rand(32768, 49151), mt_rand(0, 65535),
                  mt_rand(0, 65535), mt_rand(0, 65535));
  }

This returns a value like 'A881264F-AB87-4592-BFC1-714AFFDEE698'. The function is used to assign a (hopefully) unique filename to an uploaded picture. (The actual image file name is stored along with the GUID value in a row in the table.) The intent is that each picture's file name will be unique.

This has worked well in this application for several years. Lately I've been getting what I think are duplicate file names, so that a record doesn't point to the original picture filename, but a newer picture that randomly got (re-used) an existing filename.

I am aware that I may need to add additional characters to the filename, like a date/timestamp, so that I can ensure that the filename is (and always will be) unique.

My question is related to the 'chances' of there being a duplicate GUID generated over a long period of time.

Assuming that the GUID is created as the function above, and results are like 'A881264F-AB87-4592-BFC1-714AFFDEE698', what are the chances (maybe not the right term) that the function might come up with the same result?

Note that this is not about 'how many' different results there will be, but what are the chances that the function will return the same result?




Aucun commentaire:

Enregistrer un commentaire