Math对象随机数方法 您所在的位置:网站首页 获取随机整数的方法 Math对象随机数方法

Math对象随机数方法

2024-07-16 06:56| 来源: 网络整理| 查看: 265

Math 对象中方法的使用 【1】Math.random() 获取一个随机数,默认取值为0-1(不包含1)【2】Math.round(值) 四舍五入得到一个整数【3】Math.ceil(值) 向上取整【4】Math.floor(值) 向下取整【5】Math.pow(值) 取幂【6】Math.sqrt(值) 开平方【7】Math.max(值) 返回最大值【8】Math.min(值) 返回最小值【9】Math.abs(值) 取绝对值【10】Math.PI得到一个圆周率 3.141592653589793

随机数random()

1、Math对象里面随机数方法,random()返回一个随机的小数 0 return Math.floor(Math.random() * (max - min + 1)) + min } var arrName = ["小白", "小红", "小蓝", "小妞", "小牛", "小黑", "小明"] console.log(arrName[getRandom(0, arrName.length - 1)])

6、补充一点

/* 随机数:Math.random() 得到的是一个0-1(包含0但是不包含1)的随机数 0*10-1*10---0-10 不包含10 得到一个0-10的随机整数数 Math.random()*11 在取整 得到一个 10-30的随机整数 0-1 --- 0*31-1*31---0+10-30+10 --10-40 0*21-1*21--0+10-20+10 ----10-30 */ // console.log(parseInt(Math.random() * 11)); // console.log(parseInt(Math.random() * 21 + 10)); // 30 到 80 之间的随机数 *51+30 // console.log(parseInt(Math.random() * 51 + 30)); // 编写一个函数,求任意两个数值之间的随机数,并返回 function randomNum(n, m) { // (0-1)*191--(0-191)+10--10-201 191 = 200-10 + 1 // return parseInt(Math.random() * 191 + 10) // 判断两个参数的大小 var max, min; max = n > m ? n : m; min = n


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有