javascript生成隨機(jī)數(shù)方法匯總
今天有又網(wǎng)友問到我 JavaScript 怎么生成指定范圍數(shù)值隨機(jī)數(shù)。Math.random() 這個(gè)方法相信大家都知道,是用來生成隨機(jī)數(shù)的。不過一般的參考手冊時(shí)卻沒有說明如何用這個(gè)方法來生成指定范圍內(nèi)的隨機(jī)數(shù)。這次我就來詳細(xì)的介紹一下Math.random(),以及如何用它來生成制定范圍內(nèi)的隨機(jī)數(shù)。
基礎(chǔ)教程請看這里
http://www.dbjr.com.cn/w3school/js/jsref_random.htm
看完教程,應(yīng)該知道Math.random()方法的基本用法了。
利用 parseInt()、Math.floor() 或者 Math.ceil()進(jìn)行四舍五入處理
我們看到,直接使用Math.random()方法,生成的是一個(gè)小于1的數(shù),所以:
Math.random()*5
得到的結(jié)果是一個(gè)小于5的隨機(jī)數(shù)。而我們通常希望得到的是0-5之間的整數(shù),所以我們需要對得到的結(jié)果四舍五入處理一下,從而得到我們期望的整數(shù)。parseInt()、Math.floor()和Math.ceil()都可以起到四舍五入的作用。
var randomNum = Math.random()*5; alert(randomNum); // 2.9045290905811183 alert(parseInt(randomNum,10)); // 2 alert(Math.floor(randomNum)); // 2 alert(Math.ceil(randomNum)); // 3
由測試的代碼我們可以看到,parseInt()和Math.floor()的效果是一樣的,都是向下取整數(shù)部分。所以parseInt(Math.random()*5,10)和Math.floor(Math.random()*5)都是生成的0-4之間的隨機(jī)數(shù),Math.ceil(Math.random()*5)則是生成的1-5之間的隨機(jī)數(shù)。
生成指定范圍數(shù)值隨機(jī)數(shù)
所以,如果你希望生成1到任意值的隨機(jī)數(shù),公式就是這樣的:
// max - 期望的最大值 parseInt(Math.random()*max,10)+1; Math.floor(Math.random()*max)+1; Math.ceil(Math.random()*max);
如果你希望生成0到任意值的隨機(jī)數(shù),公式就是這樣的:
// max - 期望的最大值 parseInt(Math.random()*(max+1),10); Math.floor(Math.random()*(max+1));
如果你希望生成任意值到任意值的隨機(jī)數(shù),公式就是這樣的:
// max - 期望的最大值 // min - 期望的最小值 parseInt(Math.random()*(max-min+1)+min,10); Math.floor(Math.random()*(max-min+1)+min);
下面我們來看看javascript生成隨機(jī)數(shù)的其他方法
1.使用內(nèi)置的隨機(jī)數(shù)發(fā)生方法:(剛剛講過,這里簡單描述下)
Math.random(); //該方法產(chǎn)生一個(gè)0到1之間的浮點(diǎn)數(shù)。 Math.floor(Math.random()*10+1); //1-10 Math.floor(Math.random()*24);//0-23
2.基于時(shí)間,亦可以產(chǎn)生隨機(jī)數(shù):
var number = now.getSeconds(); //這將產(chǎn)生一個(gè)基于目前時(shí)間的0到59的整數(shù)。
var now=new Date();
var number = now.getSeconds()%43; //這將產(chǎn)生一個(gè)基于目前時(shí)間的0到42的整數(shù)。
3.一個(gè)相當(dāng)優(yōu)秀的的隨機(jī)數(shù)發(fā)生器程序,能應(yīng)用于許多領(lǐng)域。
<script language="JavaScript"><!-- // The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu) // See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html rnd.today=new Date(); rnd.seed=rnd.today.getTime(); function rnd() { rnd.seed = (rnd.seed*9301+49297) % 233280; return rnd.seed/(233280.0); }; function rand(number) { return Math.ceil(rnd()*number); }; // end central randomizer. --> </script>
我們再來看2個(gè)具體的實(shí)例吧,
第一種方法通過重寫Math.random方法實(shí)現(xiàn),第二種方法改自一個(gè)C實(shí)現(xiàn),都可以實(shí)現(xiàn)編程目的。
實(shí)例一:
<script language="javascript"> var native_random = Math.random; Math.random = function(min, max, exact) { if (arguments.length === 0) { return native_random(); } else if (arguments.length === 1) { max = min; min = 0; } var range = min + (native_random()*(max - min)); return exact === void(0) ? Math.round(range) : range.toFixed(exact); }; document.write(Math.random()); document.write('<br />'); document.write(Math.random(10)); document.write('<br />'); document.write(Math.random(3,10)); document.write('<br />'); document.write(Math.random(2,10,4)); </script>
實(shí)例二:
<script type="text/javascript"> var random = (function(){ var high = 1, low = 1 ^ 0x49616E42; var shuffle = function(seed){ high = seed; low = seed ^ 0x49616E42; } return function(){ var a = new Date()-0 shuffle(a); high = (high << 16) + (high >> 16); high += low; low += high; return high; } })(); alert( random() ); </script>
好了,通過這些例子,大家應(yīng)該對javascript生成隨機(jī)數(shù)有了相應(yīng)的了解,希望本文能夠給大家一些啟發(fā)。
- javascript 如何生成不重復(fù)的隨機(jī)數(shù)
- Javascript 生成指定范圍數(shù)值隨機(jī)數(shù)
- js生成隨機(jī)數(shù)之random函數(shù)隨機(jī)示例
- javascript生成隨機(jī)數(shù)的方法
- javascript生成不重復(fù)的隨機(jī)數(shù)
- js生成隨機(jī)數(shù)的方法實(shí)例
- JS生成某個(gè)范圍的隨機(jī)數(shù)【四種情況詳解】
- js生成隨機(jī)數(shù)(指定范圍)的實(shí)例代碼
- JS簡單生成兩個(gè)數(shù)字之間隨機(jī)數(shù)的方法
- JavaScript隨機(jī)數(shù)生成各種技巧及實(shí)例代碼
相關(guān)文章
JavaScript實(shí)現(xiàn)身份證驗(yàn)證代碼
本文給大家分享的是使用javascript實(shí)現(xiàn)身份驗(yàn)證的規(guī)則以及代碼,非常的簡單實(shí)用,有需要的小伙伴可以參考下。2016-02-02JS實(shí)現(xiàn)網(wǎng)頁頂部向下滑出的全國城市切換導(dǎo)航效果
這篇文章主要介紹了JS實(shí)現(xiàn)網(wǎng)頁頂部向下滑出的全國城市切換導(dǎo)航效果,涉及javascript鼠標(biāo)事件及頁面元素顯示的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08JS動(dòng)態(tài)的把左邊列表添加到右邊的實(shí)現(xiàn)代碼(可上下移動(dòng))
在javascript前端開發(fā)過程中經(jīng)常見到動(dòng)態(tài)的把左邊列表添加到右邊,基于js代碼怎么實(shí)現(xiàn)的呢?今天小編通過本文給大家介紹下js 左邊列表添加到右邊的實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2016-11-11js傳中文參數(shù)controller里獲取參數(shù)亂碼問題解決方法
js傳中文參數(shù),在controller里獲取參數(shù)亂碼的問題在本文有個(gè)不錯(cuò)的解決方法,感興趣的朋友可以參考下2014-01-01