1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        一篇文章帶你了解JavaScript隨機(jī)數(shù)

        共 3601字,需瀏覽 8分鐘

         ·

        2021-12-26 03:22

        點(diǎn)擊上方“前端進(jìn)階學(xué)習(xí)交流”,進(jìn)行關(guān)注

        回復(fù)“前端”即可獲贈(zèng)前端相關(guān)學(xué)習(xí)資料

        魚(yú)和熊掌不可兼得!

        一、Math.random()

        Math.random() 返回0到1之間的隨機(jī)數(shù)(包括0,不包括1)。

        語(yǔ)法

        Math.random();            // returns a random number
        代碼
        <!DOCTYPE html><html>    <title>項(xiàng)目</title>
        <body style="background-color: aqua;"> <h1>JavaScript Math.random()</h1>
        <p>單擊按鈕以顯示0(含)和1(不含)之間的隨機(jī)數(shù):</p>
        <button onclick="myFunc()">Click</button>
        <p id="result"></p>
        <script> function myFunc() { document.getElementById('result').innerHTML = Math.random(); }</script>
        </body></html>

        Math.random() 總是返回小于1的數(shù)字。


        二、JavaScript 隨機(jī)整數(shù)

        Math.random() 和 Math.floor() 一起使用,可以返回一個(gè)隨機(jī)整數(shù)。

        案例1:返回一個(gè)從0到9的隨機(jī)整數(shù)

        Math.floor(Math.random() * 10);     // returns a number between 0 and 9
        代碼
        <!DOCTYPE html><html>    <title>項(xiàng)目</title>
        <body style="background-color: aqua;">
        <p>單擊按鈕以顯示0到9之間的隨機(jī)數(shù):</p>
        <button onclick="myFunc()">Click</button>
        <p id="result"></p>
        <script> function myFunc() { let x = Math.floor(Math.random() * 11); document.getElementById("result").innerHTML = x; // 下面代碼依次替換相對(duì)于的js代碼,實(shí)現(xiàn)以下效果 }</script>
        </body>

        案例2:返回一個(gè)從0到99的隨機(jī)整數(shù)

        Math.floor(Math.random() * 100);     // returns a number between 0 and 9

        案例3:返回一個(gè)從0到100的隨機(jī)整數(shù)

        Math.floor(Math.random() * 101);     // returns a number between 0 and 10

        案例4:返回一個(gè)從11到20的隨機(jī)整數(shù)

        Math.floor((Math.random() * 10) + 11);  // returns a number between 11 and 20

        案例5:返回一個(gè)從1到100的隨機(jī)整數(shù)

        Math.floor(Math.random() * 100) + 1// returns a number between 1 and 100


        三、恰當(dāng)隨機(jī)函數(shù)(min(包括)和max(排除)之間)。

        上面的例子中看到的,創(chuàng)建一個(gè)合適的隨機(jī)函數(shù)用于所有的隨機(jī)整數(shù)可能是個(gè)好主意。

        JavaScript函數(shù)總是返回一個(gè)隨機(jī)數(shù)在min(包括)和max(排除)之間:

        <!DOCTYPE html><html>  <title>項(xiàng)目</title>
        <body style="background-color: aqua;">
        <p>單擊按鈕以顯示0到9之間的隨機(jī)數(shù):</p>
        <button onclick="myFunc()">Click</button>
        <p id="result"></p>
        <script> function myFunc() { document.getElementById("result").innerHTML = getRandom(0, 10); }
        function getRandom(min, max) { return Math.floor(Math.random() * (max - min)) + min; }</script>
        </body></html>

        JavaScript函數(shù)總是返回一個(gè)隨機(jī)數(shù)在min(包括)和max(包括)之間:

        <!DOCTYPE html><html>  <title>項(xiàng)目</title>   <body style="background-color: aqua;">
        <p>單擊按鈕以顯示0到10之間的隨機(jī)數(shù):</p>
        <button onclick="myFunc()">Click</button>
        <p id="result"></p>
        <script> function myFunc() { document.getElementById("result").innerHTML = getRandom(0, 10); }
        function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }</script>
        </body></html>


        四、總結(jié)

        本文主要介紹了JavaScript 隨機(jī)數(shù)(Random)函數(shù)的應(yīng)用,介紹了如何去取一個(gè)區(qū)間的隨機(jī)數(shù),以及隨機(jī)整數(shù)。通過(guò)用豐富的案例幫助大家更好理解。

        希望大家可以根據(jù)文章的內(nèi)容,積極嘗試,有時(shí)候看到別人實(shí)現(xiàn)起來(lái)很簡(jiǎn)單,但是到自己動(dòng)手實(shí)現(xiàn)的時(shí)候,總會(huì)有各種各樣的問(wèn)題,切勿眼高手低,勤動(dòng)手,才可以理解的更加深刻。

        使用JavaScript 語(yǔ)言,方便大家更好理解,希望對(duì)大家的學(xué)習(xí)有幫助。

        ------------------- End -------------------

        往期精彩文章推薦:

        歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持

        想加入前端學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群

        萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行

        瀏覽 43
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            午夜AV天堂 | c逼网站 欧美视频手机在线 | 国产精品久久久久久久久久白浆 | 乱爱性全过程免费视频 | 久久久久久久国产精品毛片 | 我被继夫添我阳道舒服口述 | 水果派av| 免费观看三级片中文字幕 | 日本三级人妇 | 国产一级一级毛片 |