js如何生成[n,m]的隨機數(整理總結)


js如何生成[n,m]的隨機數(整理總結)

一、總結

一句話總結:

// max - 期望的最大值 // min - 期望的最小值 parseInt(Math.random()*(max-min+1)+min,10); Math.floor(Math.random()*(max-min+1)+min);

 

1、parseInt(string, radix)的第二個參數是什么意思?

parseInt() 函數可解析一個字符串,並返回一個整數。

radix可選。表示要解析的數字的基數。該值介於 2 ~ 36 之間。

如果省略該參數或其值為 0,則數字將以 10 為基礎來解析。如果它以 “0x” 或 “0X” 開頭,將以 16 為基數。

如果該參數小於 2 或者大於 36,則 parseInt() 將返回 NaN。

 

2、Math.random();生成的數的0和1的包括情況是怎樣的?

左閉右開

Math.random();  //0.0 ~ 1.0 之間的一個偽隨機數。【包含0不包含1】 //比如0.8647578968666494

 

3、Math.ceil(Math.random()*10);可均衡獲取0到10的隨機整數么

不能

可獲取從1到10的隨機整數 ,取0的概率極小。

 

4、Math.round(Math.random()*10);可以均衡獲取0到10的隨機整數么?

不能

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

因為結果在0~0.4 為0,0.5到1.4為1...8.5到9.4為9,9.5到9.9為10。所以頭尾的分布區間只有其他數字的一半。

 

5、均衡生成[0,max]的隨機數有哪些方法

ParseInt和Math.floor方法

// max - 期望的最大值 parseInt(Math.random()*(max+1),10); Math.floor(Math.random()*(max+1));

 

6、js中如何判斷參數個數?

arguments對象的length屬性

arguments.length
//生成從minNum到maxNum的隨機數 function randomNum(minNum,maxNum){ switch(arguments.length){ case 1: return parseInt(Math.random()*minNum+1,10); break; case 2: return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); break; default: return 0; break; } } 

 

7、parseInt的取整模式是怎樣的?

parseInt()和Math.floor()結果都是向下取整。

 

 

二、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的概率極小。

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

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

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

因為結果在0~0.4 為0,0.5到1.4為1...8.5到9.4為9,9.5到9.9為10。所以頭尾的分布區間只有其他數字的一半。

二 、生成[n,m]的隨機整數

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

在js生成驗證碼或者隨機選中一個選項時很有用。。

//生成從minNum到maxNum的隨機數
function randomNum(minNum,maxNum){ 
    switch(arguments.length){ 
        case 1: 
            return parseInt(Math.random()*minNum+1,10); 
        break; 
        case 2: 
            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
        break; 
            default: 
                return 0; 
            break; 
    } 
} 

 過程分析:

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]的隨機數,公式如下:

// max - 期望的最大值
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);

所以生成[0,max]到任意數的隨機數,公式如下:

// max - 期望的最大值
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));

所以希望生成[min,max]的隨機數,公式如下:

// max - 期望的最大值
// min - 期望的最小值
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);

 

參考:js生成[n,m]的隨機數 - starof - 博客園
https://www.cnblogs.com/starof/p/4988516.html

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM