最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS生成隨機(jī)數(shù)的多種方法匯總(不同范圍、類型的隨機(jī)數(shù))

 更新時(shí)間:2024年06月29日 23:55:55   作者:growb  
js產(chǎn)生隨機(jī)數(shù)通常是使用javascript的Math.random()函數(shù),下面這篇文章主要給大家介紹了關(guān)于JS生成隨機(jī)數(shù)的多種方法(不同范圍、類型的隨機(jī)數(shù))的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1,生成 [ 0, 1 ) 范圍內(nèi)的隨機(jī)數(shù)(大于等于0,小于1)

(1)使用 random() 方法可以返回一個(gè)介于 0 ~ 1 之間的偽隨機(jī)數(shù)(包括 0,不包括 1)。

Math.random()

(2)下面是一個(gè)測(cè)試樣例

var random = Math.random();
console.log(random);

2,生成 [ n, m ) 范圍內(nèi)的隨機(jī)數(shù)(大于等于n,小于m)

(1)這種最簡(jiǎn)單,因?yàn)楹?random 的特點(diǎn)保持一致。只需使用如下公式即可:

Math.random()*(m-n)+n

(2)比如下面生成 [10,15) 范圍內(nèi)的隨機(jī)浮點(diǎn)數(shù)。

var random1 = Math.random()*(15-10)+10;
var random2 = Math.random()*(15-10)+10;
var random3 = Math.random()*(15-10)+10;
console.log(random1);
console.log(random2);
console.log(random3);

3,生成 [n,m]、(n,m)、(n,m] 范圍內(nèi)的隨機(jī)數(shù)

因?yàn)?random 的特點(diǎn),要取得這幾個(gè)區(qū)間內(nèi)的浮點(diǎn)數(shù)稍微麻煩些,需要借助一些判斷才能滿足要求。

//取得[n,m]范圍隨機(jī)數(shù)
function fullClose(n,m) {
   var result = Math.random()*(m+1-n)+n;
   while(result>m) {
       result = Math.random()*(m+1-n)+n;
   }
   return result;
}
 
//取得(n,m)范圍隨機(jī)數(shù)
function fullOpen(n,m) {
   var result = Math.random()*(m-n)+n;
   while(result == n) {
       result = Math.random()*(m-n)+n;
   }
   return result;
}
 
//取得(n,m]范圍隨機(jī)數(shù)
function leftOpen(n,m) {
   var result = Math.random()*(m-n+1)+n-1;
   while(result<n) {
       result = Math.random()*(m-n+1)+n-1;
   }
   return result;
}

隨機(jī)整數(shù)的生成

要生成隨機(jī)整數(shù),我們還需要借助如下兩個(gè)方法: Math.round(num):將 num 四舍五入取整 Math.floor(num):將 num 向下取整,即返回 num 的整數(shù)部分。當(dāng)然我們也可以使用 parseInt() 方法代替。

1,隨機(jī)生成 0、1 這兩個(gè)整數(shù)

(1)下面這個(gè)方法可以隨機(jī)獲取 0 或 1,它們獲取到的幾率是比較均衡的。

Math.round(Math.random())

(2)下面是一個(gè)測(cè)試樣例

var random1 = Math.round(Math.random());
var random2 = Math.round(Math.random());
var random3 = Math.round(Math.random());
console.log(random1);
console.log(random2);
console.log(random3);

2,生成 [ 0, n ) 范圍內(nèi)的隨機(jī)整數(shù)(大于等于0,小于n)

(1)下面方法生成一個(gè) 0 到 n-1 的隨機(jī)整數(shù)(這 n 個(gè)數(shù)獲取幾率都是均衡的)

Math.floor(Math.random()*n)

(2)比如下面生成幾個(gè) 0 到 4 的隨機(jī)整數(shù)(包括 0 和 4)。

var random1 = Math.floor(Math.random()*5);
var random2 = Math.floor(Math.random()*5);
var random3 = Math.floor(Math.random()*5);
console.log(random1);
console.log(random2);
console.log(random3);

3,生成 [ 1, n ] 范圍內(nèi)的隨機(jī)整數(shù)(大于等于1,小于等于n)

(1)下面方法生成一個(gè) 1 到 n 的隨機(jī)整數(shù)(這 n 個(gè)數(shù)獲取幾率都是均衡的)

Math.floor(Math.random()*n)+1

(2)比如下面生成幾個(gè) 1 到 5 的隨機(jī)整數(shù)(包括 1 和 5)。

var random1 = Math.floor(Math.random()*5)+1;
var random2 = Math.floor(Math.random()*5)+1;
var random3 = Math.floor(Math.random()*5)+1;
console.log(random1);
console.log(random2);
console.log(random3);

4,生成 [ min, max ] 范圍內(nèi)的隨機(jī)整數(shù)(大于等于min,小于等于max)

(1)下面方法生成一個(gè)最小值為 min,最大值為 max 的隨機(jī)整數(shù)。

Math.floor(Math.random()*(max-min+1))+min

(2)比如下面生成幾個(gè) 5 到 10 的隨機(jī)整數(shù)

var random1 = Math.floor(Math.random()*5)+1;
var random2 = Math.floor(Math.random()*5)+1;
var random3 = Math.floor(Math.random()*5)+1;
console.log(random1);
console.log(random2);
console.log(random3);

隨機(jī)字符串的生成

1,生成指定位數(shù)的純數(shù)字字符串

//生成n位數(shù)字字符串
function randomNum(n){
  var res = "";
  for(var i=0;i<n;i++){
    res += Math.floor(Math.random()*10);
  }
  return res;
}
 
//測(cè)試
console.log(randomNum(3))
console.log(randomNum(5))
console.log(randomNum(7))

2,生成指定位數(shù)的數(shù)字字母混合的字符串

//生成n位數(shù)字字母混合字符串
function generateMixed(n) {
  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'];
  var res = "";
  for(var i = 0; i < n ; i++) {
     var id = Math.floor(Math.random()*36);
     res += chars[id];
  }
  return res;
}
 
//測(cè)試
console.log(generateMixed(3))
console.log(generateMixed(5))
console.log(generateMixed(7))

對(duì)比上面代碼的補(bǔ)充版,加入 數(shù)字、大小寫字符

function getjl(n){
var chas=["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","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"];
var binint=["0","1","2","3","4","5","6","7","8","9"];
var res="";
for(var i=0; i < n; i++){
		var id = Math.floor(Math.random() * 62);
		res+=chas[id];
	}
						
return res;
}
//測(cè)試
console.log(getjl(3))
console.log(getjl(5))
console.log(getjl(7))

總結(jié)

到此這篇關(guān)于JS生成隨機(jī)數(shù)的多種方法匯總的文章就介紹到這了,更多相關(guān)JS生成隨機(jī)數(shù)方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

潞城市| 阿合奇县| 通海县| 海南省| 富顺县| 扶风县| 敦煌市| 永兴县| 富宁县| 河源市| 康马县| 敦化市| 五寨县| 峨边| 辽源市| 郴州市| 双柏县| 阿荣旗| 南涧| 涿州市| 玉林市| 衡东县| 旺苍县| 桃源县| 沁阳市| 淮阳县| 洛扎县| 恩施市| 开鲁县| 石棉县| 武川县| 张家界市| 黎城县| 通城县| 吉木乃县| 临夏市| 基隆市| 九龙县| 伽师县| 长寿区| 九寨沟县|