获得给定区间[m,n]的随机数 第一种方法
function getRandomInterval(
min,
max){
return Math.
random()*(
max-
min)+
min;
}
第二种方法
function getRandomInt(
min,
max){
min = Math.
ceil(
min);
max = Math.
round(
max);
return Math.
floor(Math.
random()*(
max-
min)+
min);
}
可以通过round()、ceil()、floor()方法,去掉小数
Math.round();//向下取整 Math.floor();//向下取整 Math.ceil();//向上取整
Math.round(
3.535);
Math.floor(
3.535);
Math.ceil(
3.535);
通过toFixed(n)方法,确定保留n位小数
3.535.toFixed(
2);