是否有可能创建一个只有10个字母数字字符(全部小写)的无碰撞随机字符串?



是否可以创建无碰撞随机10字符字母数字字符串?顺便说一下,必须全部小写。Bitly似乎已经解决了这个问题,但我知道他们使用大写和小写的组合来增加随机性。这只能用一个case来完成。

这是一个随机化字符串的函数,并诚实地尝试查找重复项。

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;
}

最新更新