1

I need a special unique booking ID, which is easy for costumer to read-out over the phone.

It should be 5 characters long, and only use these letters:

var letters = "ABCDEFGHJKMNPQRSTUXY";

I found this function on stackoverflow but is having a hard time to get it to take chars from my letters and limit itself to 5 chars.

var uniqueId = function() {
  return Math.random().toString(36).substr(2, 16);
};

Any help would be appreciated :-)

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53

3 Answers3

2

See function below, uniqueId() for a string of five characters (it is possible that the function could generate the same string multiple times, so it's not really unique):

function uniqueId(stringLength, possible)
{
  stringLength = stringLength || 5;
  possible = possible || "ABCDEFGHJKMNPQRSTUXY";
  var text = "";

  for(var i = 0; i < stringLength; i++) {
    var character = getCharacter(possible);
    while(text.length > 0 && character === text.substr(-1)) {
      character = getCharacter(possible);
    }
    text += character;
  }

  return text;
}

function getCharacter(possible) {
  return possible.charAt(Math.floor(Math.random() * possible.length));
}
Ganhammar
  • 1,871
  • 1
  • 16
  • 29
1

Something like this

var letters = "ABCDEFGHJKMNPQRSTUXY";
var uniqueId = function() {
  var text = "";
  for (var i = 0; i < 5; i++) {
    text += letters.charAt(Math.floor(Math.random() * letters.length));
  }
  return text;
};
console.log(uniqueId());

[number of possible char]^[lenght of the generatet id] - so you'll have 3.200.000 unique ID's... The 3.200.001'th ID won't be unique anymore...

Take upper / lower cases into account if you want to double the amount of unique ID's...

var letters = "ABCDEFGHJKMNPQRSTUXYabcdefghjkmnpqrstuxy";
    var uniqueId = function() {
      var text = "";
      for (var i = 0; i < 5; i++) {
        text += letters.charAt(Math.floor(Math.random() * letters.length));
      }
      return text;
    };
    console.log(uniqueId());
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33
  • is there a way to avoid two of the same letters side by side? "ABCAB" is ok - "ABBCD" is not. – torbenrudgaard Jun 15 '17 at 11:38
  • 1
    Something like this example would solve that - as demo only used AB letters - https://jsfiddle.net/zxos7456/1/ (now taking into account of checking the letter - checking the index could also be a possibility) – daan.desmedt Jun 15 '17 at 11:41
1

Well some others just beat me, but here's an alternative that is fairly easy to read. It also incorporates not allowing 2 consecutive characters to be the same (as added in a later comment by the OP)

 function uniqueId()
 {
        var letters = "ABCDEFGHJKMNPQRSTUXY";
        var result="";
        while (result.length<5)
        {
            var rand_int = Math.floor((Math.random() * 19) + 1);
            var rand_chr= letters[rand_int];
            if (result.substr(-1, 1)!=rand_chr) result+=rand_chr;
        }
        return (result);
 }
t c
  • 101
  • 2