获取随机整数使用parseInt() 或者 Math.floor()
1. 获取 [0,x) 的随机整数
parseInt(Math.random() * x);
2. 获取 [0,y] 的随机整数
Math.floor(Math.random() * (y+1));
3. 获取 [n,m) 的随机整数
Math.floor(Math.random() * (m-n) + n);
4. 获取 [p,q) 的随机整数
parseInt(Math.random() * (q-p+1) + p);
<script>
//Math.random();
// [n,m)
// parseInt(Math.random() * (m - n) + n);
// Math.floor(Math.random() * (m - n) + n);
// [1,4)
var i = parseInt(Math.random() * (4 - 1) + 1);
console.log(i);
// [5,10)
var j = Math.floor(Math.random() * (10 - 5) + 5);
console.log(j);
// [10,20]
var k = Math.floor(Math.random() * (20 - 10 + 1) + 10);
console.log(k);
</script>