数学関数のjsの使用

以下の通り使用数学関数の多くの方法があります。

var arr=[1,2,3,4,5,6]
//最小值
console.log(Math.min(...arr)) //结果: 1

//最大值
console.log(Math.max(...arr))  //结果: 6

//绝对值
console.log(Math.abs(-10))  //结果: 10

//退一取整(向下取整)
console.log(Math.floor(1.9))   //结果: 1

//进一取整(向上取整) 
console.log(Math.ceil(1.1))   //结果: 2

//幂运算 2的3次方
console.log(Math.pow(2,3))   //结果 8

//平方运算  返回一个数的平方根
console.log(Math.sqrt(9))  // 结果 3

重点

数学乱数取得

そのような乱数のみが、0と1の間の小数を得る無限に近いゼロ小数点以下の最小値を得ることではなく、0に失敗することはありません取得し、無限に1に近い最大小数を得るだけでなく、感情移入のために失敗されている1

console.log(Math.random());

従って間(0-9)の範囲で得られましたそこ進数最大10に限りなく近いですこの整数

console.log(Math.random()*10)

に基づいて番号の範囲(0〜10)を取得するため、乗算および10に加えて1

//方法一
console.log((Math.random()*10)+1)
//方法二
console.log(Math.random()*11)
メソッドを追加するために、ランダムな整数の結果を取得するには
console.log(Math.floor(Math.random()*11))
(5-10)の範囲内のランダムな整数を得るために
console.log(Math.floor(Math.random()*6)+5)

ランダム整数に指定された範囲の要約を取得する法律に従う方法を使用して包装することができます

function random(min,max){
  return Math.floor(Math.random()*(max-min+1))+min
}
console.log(random(10,20))
公開された39元の記事 ウォン称賛13 ビュー2320

おすすめ

転載: blog.csdn.net/qq_43205282/article/details/103664469