概念
Math.random()方法能取0–1的随机数,它的范围是[0,1),也就是取包括0但不包括1的随机数
另外
Math.floor()方法代表向下取整
接下来例子
//取 [2,32) 之间随机整数
Math.floor(Math.random() * 30) + 2;
公式
//取 [n,m) 之间的随机整数公式
Math.floor(Math.random() * (m-n)) + n;
概念
Math.random()方法能取0–1的随机数,它的范围是[0,1),也就是取包括0但不包括1的随机数
另外
Math.floor()方法代表向下取整
接下来例子
//取 [2,32) 之间随机整数
Math.floor(Math.random() * 30) + 2;
公式
//取 [n,m) 之间的随机整数公式
Math.floor(Math.random() * (m-n)) + n;