隨機數生成

function getRandom(cnt, min, max) {
	/*
		@cnt: 生成隨機數的個數
		@min: 開始數
		@max: 結束數
	*/
	var arr = [];
	var temp;
	for(var i=0; i<cnt; ++i) {
		//temp = Math.round(min+(max-min)*Math.random());   //[a, b]
		//temp = Math.floor(min+(max-min)*Math.random());   //[a, b)
		temp = Math.ceil(min+(max-min)*Math.random());   // (a, b]
		arr.push(temp)
	}

	return arr;
}

var result = getRandom(100, 20, 30);
console.log(result.indexOf(20));
console.log(result.indexOf(30));


可以用indexOf來測試一下是否包含端點


發佈了88 篇原創文章 · 獲贊 37 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章