js使用技巧 - 随机生成一个字符,字符串
String.formCharCode(Math.floor(Math.random() * 26) + 'a'.charCodeAt(0))
String.formCharCode(Math.floor(Math.random() * 26) + 'A'.charCodeAt(0))
Math.random().toString(36).slice(2);
function randomString(len, charSet) {
charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var randomString = '';
for (var i = 0; i < len; i++) {
var randomPoz = Math.floor(Math.random() * charSet.length);
randomString += charSet.substring(randomPoz,randomPoz+1);
}
return randomString;
}
function randomstring(L){
var s= '';
var randomchar=function(){
var n= Math.floor(Math.random()*62);
if(n<10) return n;
if(n<36) return String.fromCharCode(n+55);
return String.fromCharCode(n+61);
}
while(s.length< L) s+= randomchar();
return s;
}
function makeid(n)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < n; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}