JS獲取隨機數(shù)函數(shù)可自定義最小值最大值
來源:易賢網(wǎng) 閱讀:2050 次 日期:2014-05-09 15:04:27
溫馨提示:易賢網(wǎng)小編為您整理了“JS獲取隨機數(shù)函數(shù)可自定義最小值最大值”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹的是一個JS獲取隨機數(shù)的函數(shù)可自定義最小值最大值,需要的朋友可以參考下

代碼如下:

/**

* lowerValue 最小值

* upperValue 最大值

*/

function selectFrom(lowerValue, upperValue){

//取值范圍總數(shù)

var choices = upperValue - lowerValue + 1;

return Math.floor(Math.random() * choices + lowerValue);

}

var num = selectFrom(2, 10);

alert(num);//介于2和10之間(包括2和10)的一個數(shù)值

var colors = ["red","green","blue","yellow","black","brown"];

var color = colors[selectFrom(0, colors.length-1)];

alert(color);//可能是數(shù)組中包含的任何一個值

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:JS獲取隨機數(shù)函數(shù)可自定義最小值最大值
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)