<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<title>Math.random 输出随机数</title>
</head>
<body>
<pre>
Math.random(); // 结果为0-1间的一个随机数(包括0,不包括1)
Math.floor(num); // 取结果的整数部分。
Math.round(num); // 产生一个四舍五入后的整数。
Math.ceil(num); // 返回大于等于num的最小整数,就是向上取整。</pre>
<pre>
Math.random(); // 返回0和1间(包括0,不包括1)的一个随机数。
Math.ceil(Math.random()*10); // 获取1到10的随机整数,取0的几率极小。
Math.round(Math.random()); // 可均衡获取0到1的随机整数。
Math.round(Math.random()*10); // 可基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半。
Math.floor(Math.random()*10); // 可均衡获取0到9的随机整数。
Math.floor(Math.random()*(10+1)); // 可均衡获取0到10的随机整数。
Math.floor(Math.random()*(max-min+1)+min); // 获得min-max随机数</pre>
</body>
</html>
pre {
color: deeppink;
font-size: 1.1em;
white-space: pre-wrap;
}
document.querySelectorAll('.nr-code').forEach(p => {
p.innerHTML.split('\n').forEach(row => {
console.log(row = row.replace('num', '1.4').replace(/min/g, '5').replace(/max/g, '20').trim())
if (row != "") {
console.log(eval(row))
}
})
})