案例:使用js中的Math.random()函数生成n到m间的随机数

需求:生成n到m的随机数

思路:分四步走

第一步算出 m-n的值,假设等于w;

第二步Math.random()*w;

第三步Math.random()*w+n

第四步Math.ceil(Math.random()*w+n)

例子:生成300到1000的随机数
Math.ceil(Math.random () * 700 + 300);
//结果生成300到1000的随机数;

补充:
Math.ceil():向上取整;
Math.floor();向下取整;
Math.round();四舍五入;

猜你喜欢

转载自blog.csdn.net/xiaodi520520/article/details/82960196