js 生成随机字符串

今儿碰到这么一个小问题啊,要前端生成一个随机字符串的token,特地写了个小算法,生成随机数字,和随机字符串!

 

//800~1500随机数
var num = Math.random()*700 + 800
num = parseInt(num,10)
console.log(num)

//1~32的随机整数
for (var i=0; i<1000; i++){
	var a1 = Math.floor(Math.random()*(32+1))
	//document.write(a1+';')
}

function randomString(len) {
  len = len || 32;
  var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  var maxPos = $chars.length;
  var pwd = '';
  for (i = 0; i < len; i++) {
		//0~32的整数
    pwd += $chars.charAt(Math.floor(Math.random() * (maxPos+1)));
  }
  return pwd;
}
document.write(randomString(32));

 

 

 

 

 

你可能感兴趣的:(js)