版权声明: https://blog.csdn.net/weixin_41187842/article/details/81456485
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<title>js与php生成随机字符串的方法</title>
</head>
<body>
<?php
function randomkeys($length){
$key='';
$pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
for($i=0;$i<$length;$i++)
{
$key .= $pattern{mt_rand(0,35)};
}
return $key;
}
echo 'php随机字符串'.randomkeys(12);
?>
</body>
<script>
//生成随机字符串
var random_num1=Math.random().toString(12).substr(2);
console.log('js随机字符串:'+random_num1);
//数组随机排序
function randomsort(a, b) {
return Math.random()>.5 ? -1 : 1;
}
var arr = [1, 2, 3, 4, 5];
arr.sort(randomsort);
console.log('js数组随机排序:'+arr);
</script>
</html>