久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      js 生成隨機(jī)數(shù)

      js 可以使用 Math(算數(shù)) 對(duì)象來實(shí)現(xiàn)隨機(jī)數(shù)的生成。

      需要了解的 Math 對(duì)象方法

      方法 描述
      ceil(x) 對(duì)數(shù)進(jìn)行上舍入,即向上取整。
      floor(x) 對(duì) x 進(jìn)行下舍入,即向下取整。
      round(x) 四舍五入。
      random() 返回 0 ~ 1 之間的隨機(jī)數(shù),包含 0 不包含 1。

      一些實(shí)例說明:

      Math.ceil(Math.random()*10);     // 獲取從 1 到 10 的隨機(jī)整數(shù),取 0 的概率極小。    Math.round(Math.random());       // 可均衡獲取 0 到 1 的隨機(jī)整數(shù)。    Math.floor(Math.random()*10);    // 可均衡獲取 0 到 9 的隨機(jī)整數(shù)。    Math.round(Math.random()*10);    // 基本均衡獲取 0 到 10 的隨機(jī)整數(shù),其中獲取最小值 0 和最大值 10 的幾率少一半。

      因?yàn)榻Y(jié)果在 0~0.4 為 0,0.5 到 1.4 為 1,8.5 到 9.4 為 9,9.5 到 9.9 為 10。所以頭尾的分布區(qū)間只有其他數(shù)字的一半。


      生成 [n,m] 的隨機(jī)整數(shù)

      函數(shù)功能:生成 [n,m] 的隨機(jī)整數(shù)。

      在 js 生成驗(yàn)證碼或者隨機(jī)選中一個(gè)選項(xiàng)時(shí)很有用。

      //生成從minNum到maxNum的隨機(jī)數(shù)  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) 的數(shù),所以 Math.random()*5 生成 {0,5) 的數(shù)。

      通常期望得到整數(shù),所以要對(duì)得到的結(jié)果處理一下。

      parseInt(),Math.floor(),Math.ceil() 和 Math.round() 都可得到整數(shù)。

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

      所以 Math.random()*5 生成的都是 [0,4] 的隨機(jī)整數(shù)。

      所以生成 [1,max] 的隨機(jī)數(shù),公式如下:

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

      所以生成 [0,max] 到任意數(shù)的隨機(jī)數(shù),公式如下:

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

      所以希望生成 [min,max] 的隨機(jī)數(shù),公式如下:

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

      原文鏈接:http://www.cnblogs.com/starof/p/4988516.html

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)