JS生成 n,m 的隨機數

2021-08-15 17:17:31 字數 1239 閱讀 3631

math.ceil();   //天花板函式,向上取整。

math.floor();   //地板函式,向下取整。

math.round();   //四捨五入。

math.random();   //[ 0,1 )之間的隨機數。

math.ceil(math.random()*10);   //獲取從1到0的隨機整數,取0的概率最小。

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

math.floor(math.random()*10);   //可均衡獲得0到9的隨機整數。

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

函式功能:生成[ n,m ]的隨機整數。

function

randomnum

(minnum,maxnum)

}

math.random()生成[ 0,1 )的數,

所以math.random()*5生成[ 0,5)的數。

通常希望得到整數,所以得作如下處理:

parseint(),math.floor(),math.ceil(),math.round()都可以得到整數。

parseint()和math.floor()結果都是向下取整。

所以可以得知:math.random()*5生成的是[ 0 ,4 )的隨機整數。

所以生成[ 1, max ]的隨機數,如下:

parseint(math.random() * max , 10)+1;

math . floor(math. random()*max)+1;

math.ceil(math.random()*max);

生成[ 0 , max ]的任意數的隨機數,如下:

parseint(math.random()*(max+1),10);

math.floor(math.random()*(max+1));

生成[min,max]的隨機數,如下:

parseint(math.random*(max-min+1)+min,10);

math .floor(math.random()*(max-min+1)+min);

js生成 n,m 的隨機數

一 預備知識 math.ceil 向上取整。math.floor 向下取整。math.round 四捨五入。math.random 1.0 之間的乙個偽隨機數。包含0不包含1 比如0.8647578968666494 math.ceil math.random 10 獲取從1到10的隨機整數 取0的...

js生成 n,m 的隨機數

math.ceil 向上取整。math.floor 向下取整。math.round 四捨五入。math.random 0.0 1.0 之間的乙個偽隨機數。包含0不包含1 比如0.8647578968666494 math.ceil math.random 10 獲取從1到10的隨機整數 取0的概率極...

js生成 n,m 的隨機數

math.ceil 向上取整。math.floor 向下取整。math.round 四捨五入。math.random 1.0 之間的乙個偽隨機數。包含0不包含1 比如0.8647578968666494 math.ceil math.random 10 獲取從1到10的隨機整數,取0的概率極小。ma...