My requirement is to generate 5 digit unique code which is not in the list already.
For example if I have [12345, 54321, 13245, 11234]
, I would like to generate 34522
etc.. I am using below code for it
function id(){
var text = "", can = "12345";
for( var i = 5; i--; text += can.charAt(Math.floor(Math.random() * can.length)));
return text;
}
var list = [12345, 54321, 13245, 11234];
var generated;
while(!generated){
var t = makeid();
if(list.indexOf(t) == -1){
generated = t;
}
}
This works fine, but when list grows, this would take more time(?). Are there any other ways to write this mechanism.