一篇文章帶你了解JavaScript隨機(jī)數(shù)
回復(fù)“前端”即可獲贈(zèng)前端相關(guān)學(xué)習(xí)資料
一、Math.random()
Math.random() 返回0到1之間的隨機(jī)數(shù)(包括0,不包括1)。
語(yǔ)法:
Math.random(); // returns a random number代碼:
<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代碼:<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(排除)之間:
<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(包括)之間:
<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 -------------------
往期精彩文章推薦:
盤(pán)點(diǎn)JavaScript中數(shù)組遍歷的全部方式(上篇)
盤(pán)點(diǎn)JavaScript中數(shù)組遍歷的全部方式(下篇)
一篇文章帶你了解JavaScript日期

歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
想加入前端學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群】
萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行
