Home > Article > Backend Development > Generate random string
##########javascript 版###############
function randomChar(l) {
var x="123456789poiuytrewqasdfghjklmnbvcxzQWERTYUIPLKJHGFDSAZXCVBNM";
var tmp="";
for(var i=0;i< l;i++) {
tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);
}
return tmp;
}
var str = randomChar(20);
document.write(str.toUpperCase());
##########php版###############
function randomChar($len){
if($len<1) return;
$str='';
$ss = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for($i=0;$i<$len;$i++){
$n = rand(0,35);
$str .= $ss[$n];
}
return $str;
}