欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

随即轮询

程序员文章站 2022-04-19 23:09:38
...
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

/** 随即获取N个数据,并把这些数据拍到数据最前面(假设所有数据都不为空) */
public class RandomRank {
	public static final int k = 56666;
	public static final int num = 56665;

	@Test
	public void test1() {
		String[] bookIDs = new String[k];
		for (int i = 0; i < k; i++) {
			bookIDs[i] = "" + (i + 1000);
		}
		long curr1 = System.currentTimeMillis();
		randomRank(bookIDs, num);
		System.out
				.println("最新的方法执行时间 :" + (System.currentTimeMillis() - curr1));
	}

	@Test
	public void test2() {
		String[] bookIDs = new String[k];
		for (int i = 0; i < k; i++) {
			bookIDs[i] = "" + (i + 1000);
		}
		long curr2 = System.currentTimeMillis();
		randomRank1(bookIDs, num);
		System.out
				.println("先前的方法执行时间 :" + (System.currentTimeMillis() - curr2));
	}

	@Test
	public void test3() {
		String[] bookIDs = new String[k];
		for (int i = 0; i < k; i++) {
			bookIDs[i] = "" + (i + 1000);
		}
		long curr2 = System.currentTimeMillis();
		randomRank2(bookIDs, num);
		System.out
				.println("第三个方法执行时间 :" + (System.currentTimeMillis() - curr2));
	}

	@Test
	public void test4() {
		String[] bookIDs = new String[k];
		for (int i = 0; i < k; i++) {
			bookIDs[i] = "" + (i + 1000);
		}
		long curr2 = System.currentTimeMillis();
		randomRank3(bookIDs, num);
		System.out
				.println("先前的方法执行时间 :" + (System.currentTimeMillis() - curr2));
	}

	/** 第一个方法 */
	public static String[] randomRank(String[] bookIDs, int num) {
		if (null == bookIDs || 0 == bookIDs.length) {
			return null;
		}

		// 如果轮转个数大于数组长度,则设置为数组长度
		if (num >= bookIDs.length) {
			num = bookIDs.length;
		}

		List<String> list = new ArrayList<String>(Arrays.asList(bookIDs));
		List<String> reList = new ArrayList<String>(bookIDs.length);

		int index;
		java.util.Random r = new java.util.Random();
		for (int i = 0; i < num; i++) {
			index = r.nextInt(list.size());
			String s = list.remove(index);
			reList.add(s);
		}
		// 把其他元素保存到新数组中
		reList.addAll(list);
		String[] ids = new String[reList.size()];
		return (String[]) reList.toArray(ids);
	}

	/** 第二个方法 */
	public static String[] randomRank1(String[] bookIDs, int num) {
		if (null == bookIDs || 0 == bookIDs.length) {
			return null;
		}
		// 如果轮转个数大于数组长度,则设置为1
		if (num >= bookIDs.length) {
			num = 1;
		}

		List<String> list = new ArrayList<String>(bookIDs.length);
		for (int i = 0; i < bookIDs.length; i++) {
			list.add(bookIDs[i]);
		}
		int[] ran = randon(num, bookIDs.length);// 随即取数组的下标
		List<String> reList = new ArrayList<String>(bookIDs.length);
		// 取出随机数
		for (int i = 0; i < ran.length; i++) // 按照下标获取数据
		{
			String o = (String) list.get(ran[i]);
			reList.add(o);
		}
		// 在原有数组中删除随机数
		for (int i = 0; i < reList.size(); i++) {
			list.remove(reList.get(i));
		}
		// 把其他元素保存到新数组中
		reList.addAll(list);
		String[] ids = new String[reList.size()];
		return (String[]) reList.toArray(ids);
	}

	public static String[] randomRank2(String[] bookIDs, int num) {
		if (null == bookIDs || 0 == bookIDs.length) {
			return null;
		}

		// 如果轮转个数大于数组长度,则设置为数组长度
		if (num >= bookIDs.length) {
			num = bookIDs.length;
		}

		List<String> list = new ArrayList<String>(Arrays.asList(bookIDs));
		String[] ids = new String[bookIDs.length];
		int index;
		java.util.Random r = new java.util.Random();
		for (int i = 0; i < num; i++) {
			index = r.nextInt(list.size());
			String s = list.remove(index);// 这方面花费时间会比randomRank3多,牵扯到数组的复制
			ids[i] = s;
		}

		for (int i = 0; i < list.size(); i++) {
			ids[num + i] = list.get(i);
		}
		list = null;
		return ids;
	}

	public static String[] randomRank3(String[] bookIDs, int num) {
		if (null == bookIDs || 0 == bookIDs.length) {
			return null;
		}

		if (num >= bookIDs.length) {
			num = bookIDs.length;
		}
		int[] tem = randon(num, bookIDs.length);// 获取所有随即数的数组下标
		String[] ids = new String[bookIDs.length];
		for (int i = 0; i < num; i++) {
			ids[i] = bookIDs[tem[i]];
			bookIDs[tem[i]] = null;
		}
		int k = num;
		for (int i = 0; i < bookIDs.length; i++) {
			if (bookIDs[i] != null) {
				ids[k++] = bookIDs[i];
			}
		}
		return ids;
	}

	private static int[] randon(int num, int length) {
		boolean[] cards = new boolean[length];// 构造一个比对数组,默认都为false
		for (int index = 0; index < length; index++) {
			cards[index] = false;
		}
		java.util.Random r = new java.util.Random();
		int x;

		// 随机返回下标数组
		int[] result = new int[num];
		for (int index = 0; index < num; index++) {
			do {
				x = getRandom(length, r);// 当card[x]元素为true时,一直循环,这浪费时间
			} while (cards[x]);
			cards[x] = true;
			result[index] = x;
		}
		return result;
	}

	private static int getRandom(int length, java.util.Random r) {
		int x;
		x = r.nextInt(length);
		return x;
	}
}
 
相关标签: junit