js基础-Math和随机数random

版权声明:EGEEK https://blog.csdn.net/qq_41604269/article/details/83472645

Math

      Math和其它的对象不同,它不是一个构造函数,它属于一个工具类,不用创建对象,里面封装了与数学相关的属性和方法

比如Math.PI 表示圆周率

常用方法

Math.abs();      //计算一个值的绝对值

Math.ceil();      //向上取整

Math.floor();     //向下取整

Math.round();   //四舍五入

Math.random();  //随机生成(0,1)之间的随机数,不包含0和1

Math.max();     //返回多个数中的最大数

Math.min();      //返回多个数中的最小数

Math.pow(x,y);  //返回x的y次幂数

Math.sqrt(x);    //返回x的开方根运算

Math.abs();      //计算一个值的绝对值

Math.ceil();      //向上取整

Math.floor();     //向下取整

Math.round();   //四舍五入

Math.random();  //随机生成(0,1)之间的随机数,不包含0和1

Math.max();     //返回多个数中的最大数

Math.min();      //返回多个数中的最小数

Math.pow(x,y);  //返回x的y次幂数

Math.sqrt(x);    //返回x的开方根运算

随机数

//例如:随机生成一个0-10的数

Math.round(Math.random()*10);

//随机生成1-10的数

Math.round(Math.random()*9+1);

//随机生成x-y之间的数

Math.round(Math.random()*(y-x)+x);

//例如:随机生成一个0-10的数

     Math.round(Math.random()*10);

//随机生成1-10的数

     Math.round(Math.random()*9+1);

//随机生成x-y之间的数

     Math.round(Math.random()*(y-x)+x);

猜你喜欢

转载自blog.csdn.net/qq_41604269/article/details/83472645