jeudi 17 juin 2021

Odds of ID Collision Using Milliseconds and 13 letters

Just needing confirmation that my javascript function below does, in fact, only have a 1:302,875,106,592,253 chance of collision between two assignments that occur within the same millisecond.

function getCollisionFreeID(){
    var k='';
    var d = (new Date().getTime()).toString();
    var alphaList = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'h', 9:'i', 10:'j', 11:'k', 12:'l', 13:'m', 14:'n', 15:'o', 16:'p', 17:'q', 18:'r', 19:'s', 20:'t', 21:'u', 22:'v', 23:'w', 24:'x', 25:'y', 26:'z'};
    for (var i=0;i<13;i++){
        k += alphaList[Math.floor(Math.random() * 26) + 1] + d[i];
    }
    return k.substring(2);
}

My thoughts are that the milliseconds eliminates all collision that could occur between two assignments that DO NOT occur in the same millisecond. This leaves the 13 letters to prevent collision within a given millisecond. My research says that the odds of 13 non unique numbers colliding would be 13^13.

  1. Is my math correct?
  2. Is there anything in my Math.floor(Math.random() * 26) + 1 calculation that would create a situation where it would not generate a random number between 1 - 26 (Like INTs or Doubles or some other number format that would cause it to lose precision)



Aucun commentaire:

Enregistrer un commentaire