js產生隨機數的幾個方法

2022-09-15 09:27:12 字數 580 閱讀 4973

1.math.random(); 結果為0-1間的乙個隨機數(包括0,不包括1) 

2.math.floor(num); 引數num為乙個數值,函式結果為num的整數部分。 

3.math.round(num); 引數num為乙個數值,函式結果為num四捨五入後的整數。

math:數學物件,提供對資料的數學計算。

math.random(); 返回0和1間(包括0,不包括1)的乙個隨機數。

math.ceil(n); 返回大於等於n的最小整數。

用math.ceil(math.random()*10);時,主要獲取1到10的隨機整數,取0的機率極小。

math.round(n); 返回n四捨五入後整數的值。

用math.round(math.random());可均衡獲取0到1的隨機整數。

用math.round(math.random()*10);時,可基本均衡獲取0到10的隨機整數,其中獲取最小值0和最大值10的機率少一半。

math.floor(n); 返回小於等於n的最大整數。

用math.floor(math.random()*10);時,可均衡獲取0到9的隨機整數。

JS產生隨機數的幾個用法!

var chars 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z function generatemixed n return res 1.math.random 結果為0 1間的乙個隨機數 包括0,...

JS產生隨機數的幾個用法!

var chars 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z function generatemixed n return res 1.math.random 結果為0 1間的乙個隨機數 包括0,...

JS產生隨機數的幾個用法!

from var chars 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z function generatemixed n return res 1.math.random 結果為0 1間的乙個隨機數...