中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

在指定的范圍內(nèi),生成不重復(fù)的隨機(jī)數(shù)序列(排除法,篩選法)

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 在指定的范圍內(nèi),生成不重復(fù)的隨機(jī)數(shù)序列
 */
public class UnrepeatRandomNumber {
	private int min;
	private int max;

	public UnrepeatRandomNumber() {
		this.min = 0;
		this.max = 10;
	}

	public UnrepeatRandomNumber(int min, int max) {
		this();
		if (max >= min) {
			this.min = min;
			this.max = max;
		} else {
			System.out.println("max比min小,按缺省值生成UnrepeatRandomNumber對(duì)象!");
		}
	}

	/**
	 * 第一種方法:排除法。隨機(jī)生成數(shù)字,如果是新生成的數(shù)字,則放到結(jié)果列表種 否則是已經(jīng)生成過(guò)的,則不加入結(jié)果列表,繼續(xù)隨機(jī)生成。
	 * 
	 * @param length
	 *            結(jié)果列表的長(zhǎng)度
	 * @return
	 */
	public Integer[] getRandomMethodA(int length) {
		if (length <= 0) {
			return new Integer[0];
		} else if (length > (this.max - this.min)) {
			System.out.println("結(jié)果列表長(zhǎng)度不能達(dá)到:" + length + ", 結(jié)果長(zhǎng)度只能是:"
					+ (this.max - this.min));
			length = this.max - this.min;
		}
		Random rd = new Random();// 用于生成隨機(jī)結(jié)果
		List resultList = new ArrayList();
		while (resultList.size() < length) {
			// 將[min, max]區(qū)間等價(jià)于min + [0, max - min + 1)
			Integer randnum = new Integer(this.min
					+ rd.nextInt(this.max - this.min + 1));
			if (!resultList.contains(randnum)) {
				resultList.add(randnum);
			}
		}
		// 使用toArray方法將List轉(zhuǎn)換成對(duì)象數(shù)組返回
		return (Integer[]) resultList.toArray(new Integer[0]);
	}

	/**
	 * 第二種方法:篩選法。將所有可能被生成的數(shù)字放到一個(gè)候選列表中。 然后生成隨機(jī)數(shù),作為下標(biāo),將候選列表中相應(yīng)下標(biāo)的數(shù)字放到放到結(jié)果列表中,
	 * 同時(shí),把它在候選列表中刪除。
	 * 
	 * @param length
	 *            結(jié)果列表的長(zhǎng)度
	 * @return
	 */
	public Integer[] getRandomMethodB(int length) {
		if (length <= 0) {
			return new Integer[0];
		} else if (length > (this.max - this.min)) {
			System.out.println("結(jié)果列表長(zhǎng)度不能達(dá)到:" + length + ", 結(jié)果長(zhǎng)度只能是:"
					+ (this.max - this.min));
			length = this.max - this.min;
		}
		// 初始化候選列表,列表長(zhǎng)度為 max -min + 1
		int candidateLength = this.max - this.min + 1;
		List candidateList = new ArrayList();
		for (int i = this.min; i <= this.max; i++) {
			candidateList.add(new Integer(i));
		}

		Random rd = new Random();// 用于生成隨機(jī)下標(biāo)
		List resultList = new ArrayList();
		while (resultList.size() < length) {
			// 生成下標(biāo),在[0,candidateLength)范圍內(nèi)
			int index = rd.nextInt(candidateLength);
			// 將候選隊(duì)列中下標(biāo)為index的數(shù)字對(duì)象放入結(jié)果隊(duì)列中
			resultList.add(candidateList.get(index));
			// 將下標(biāo)為index的數(shù)字對(duì)象從候選隊(duì)列中刪除
			candidateList.remove(index);
			// 候選隊(duì)列長(zhǎng)度減去1
			candidateLength--;
		}
		// 使用toArray方法將List轉(zhuǎn)換成對(duì)象數(shù)組返回
		return (Integer[]) resultList.toArray(new Integer[0]);
	}

	public static void outputArray(Integer[] array) {
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				System.out.print(array[i] + "  ");
			}
		}
		System.out.println();
	}

	public static void main(String[] args) {

		UnrepeatRandomNumber test = new UnrepeatRandomNumber(5, 15);
		outputArray(test.getRandomMethodA(8));
		outputArray(test.getRandomMethodB(8));
		// 相比之下,第一種方法利用Random對(duì)象生成的隨機(jī)數(shù)的次數(shù)比較多,可能生成了20次數(shù)字,才能找到10個(gè)不一樣的數(shù)字。
		// 第二種方法利用Random對(duì)象生成的隨機(jī)數(shù)的次數(shù)比較少,需要多少個(gè),就生成多少個(gè),保證了每次生成的數(shù)字都不重復(fù)。
		// 也就是說(shuō)第一種方法在時(shí)間花費(fèi)上更多。但是第二種方法需要初始化一個(gè)候選隊(duì)列,需要更多的空間花費(fèi)。
	}
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:java采集網(wǎng)頁(yè) 抓取網(wǎng)頁(yè)

下一篇:Java生成隨機(jī)無(wú)重復(fù)隨機(jī)數(shù),使用ArrayList實(shí)現(xiàn)