顯示廣告
隱藏 ✕
看板 Mesak
作者 mesak (Mesak)
標題 [JS] 亂數 javascript Math.random()
時間 2012年11月29日 Thu. PM 04:39:53


/*
【原始】
 範例:Math.random()
值範圍:0 ~ 0.9999999(無窮小數)
 
【最大值】
 範例:Math.random() * 3
值範圍:0 ~ 2.9999999(無窮小數)
 
【有最小值】
 範例:Math.random() * 2 + 1
值範圍:1 ~ 1.9999999(無窮小數)
 【四捨五入】
 範例:Math.round(Math.random*2+1)
值範圍:(1) - (1.5) - (2) - (2.5) - (3)
 
【取得大於指定數的最小整數值】
 範例:Math.ceil(Math.random()*2)
值範圍:(0) - (0.5) - (1) - (1.5) - (2)
 注意:在Javascript中,Math.ceil(0) 等於 0
 
【取得小於指定數的最大整數值】
 範例:Math.floor(Math.random()*2+1)
值範圍:(1) - (1.5) - (2) - (2.5) - (3)
*/
 
//下列為自訂範圍值的亂數函式(最小值,最大值)
function usefloor(min,max) {
	
return Math.floor(Math.random()*(max-min+1)+min);
}
function useceil(min,max) {
	
return Math.ceil(Math.random()*(max-min+1)+min-1);
}
function useround(min,max) {
	
return Math.round(Math.random()*(max-min)+min);
}
http://liaosankai.pixnet.net/blog/post/19382531

http://www.shawnolson.net/a/789/make_javascript_mathrandom_useful.html

--
※ 作者: mesak 時間: 2012-11-29 16:39:53
※ 看板: Mesak 文章推薦值: 0 目前人氣: 0 累積人氣: 187 
e)編輯 d)刪除 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇