js生成随机字母

来源:互联网 发布:php 获取项目根目录 编辑:程序博客网 时间:2024/05/16 14:49
 

random函数是0到1之间的小数 其中不包括1. 所以应该乘26 而不是25


var random1 = Math.round(Math.random()*26)+0


通常如下方法

<script language="javascript">
/* len 为长度 */
function getPass(len)
{
var tmpCh = "";
for(var i = 0; i < len; i++)
{
tmpCh += String.fromCharCode(Math.floor( Math.random() * 26) + "a".charCodeAt(0));
}
return tmpCh;
}
alert(getPass(2));
</script>