Is it possible to create collision-free random 10 character alphanumeric string? It has to be all lower-case by the way. Bitly seemed to have solved it but I do know they use a combination of uppercase and lowercase that increases randomness. This has to be done with just one case though.
Here's a function to randomize string and an honest attempt to find duplicates.
function random_str(length, possible) {
var text = "";
length = length || 5;
possible = possible || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
var max = 1e4;
var i = 0;
var arr = [];
while (true) {
var id = random_str(10, "abcdefghijklmnopqrstuvwxyz0123456789");
console.log(id)
if (arr.indexOf(id) > -1) {
alert("end of universe");
break;
}
arr.push(id);
if (i++ >= max) {
break;
}
}
console.log("done. no duplicates found after " + i + " attempts")
.as-console-wrapper {
max-height: 100% !important;
}