在指定区间内,生成K个随机数

在指定区间[min, max],生成K个随机数:


public class TestDemo8 {

	private final static int MIN = 1;
	private final static int MAX = 100;
	private final static int GENERATE_NUMBER = 100;

	public static void main(String[] args) throws Exception {
		new TestDemo8().work(MIN, MAX, GENERATE_NUMBER);
		System.out.println("-----------华丽的分割线---------------");
		new TestDemo8().work2(MIN, MAX, GENERATE_NUMBER);
		System.out.println("-----------华丽的分割线---------------");
		new TestDemo8().work3(MIN, MAX, GENERATE_NUMBER);
	}

	/**
	 * @Description: 针对大批量数据处理,方案一
	 * @param min
	 * @param max
	 * @param k
	 * @throws Exception
	 */
	public void work(int min, int max, int k) throws Exception {
		AssertUtil.assertExpress(max > min, "max must more than min");
		AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
		AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
		long startTime = System.currentTimeMillis();
		int[] result = new int[k];
		int[] seqNum = new int[max - min + 1];
		for (int i = 0, size = seqNum.length; i < size; i++) {
			seqNum[i] = i + min;
		}

		int end = max;
		for (int i = 0; i < k; i++) {
			int randomNum = RandomUtil.getRandomDigit(min, end);
			result[i] = seqNum[randomNum - min];
			seqNum[randomNum - min] = seqNum[end - min];
			--end;
		}

		print(result);

		System.out.println("case 1 --- length: " + result.length);
		long endTime = System.currentTimeMillis();
		System.out.println("case 1 --- spend time: " + (endTime - startTime));
	}

	/**
	 * @Description: 针对方案一,内存占用大得改进版,效率高,内存占用少
	 * @param min
	 * @param max
	 * @param k
	 * @throws Exception
	 */
	public void work2(int min, int max, int k) throws Exception {
		AssertUtil.assertExpress(max > min, "max must more than min");
		AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
		AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
		long startTime = System.currentTimeMillis();
		int[] arr = new int[max - min + 1];
		for (int i = 0, size = arr.length; i < size; i++) {
			arr[i] = i + min;
		}

		for (int i = 0; i < k; i++) {
			int randomNum = RandomUtil.getRandomDigit(i, max - min);
			swapArr(arr, i, randomNum);
		}

		print(arr);

		System.out.println("case 2 --- length: " + k);
		long endTime = System.currentTimeMillis();
		System.out.println("case 2 --- spend time: " + (endTime - startTime));
	}

	private void swapArr(int[] arr, int i, int j) {
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}

	private void swapList(List<Integer> list, int i, int j) {
		list.set(i, list.set(j, list.get(i)));
	}

	/**
	 * @Description: 针对大批量,去小部分数据,max与k值相差越大,效率内存低,非均匀随机分配
	 * @param min
	 * @param max
	 * @param k
	 * @throws Exception
	 */
	public void work3(int min, int max, int k) throws Exception {
		AssertUtil.assertExpress(max > min, "max must more than min");
		AssertUtil.assertExpress(k >= 1, "generete random number must more than 1");
		AssertUtil.assertExpress(k <= (max - min + 1), "can't generate more than " + (max - min + 1) + " number");
		long startTime = System.currentTimeMillis();
		List<Integer> result2 = new ArrayList<Integer>(k);
		while (k > 0) {
			int step = (max - min) / k;
			if (step < 1) {
				for (int i = min; i <= max; i++) {
					result2.add(i);
				}
				break;
			}
			int randomNum = RandomUtil.getRandomDigit(1, step);
			min += randomNum;
			result2.add(min);
			k--;
		}

		for (int i = result2.size(); i >= 1; i--) {
			swapList(result2, i - 1, RandomUtil.getRandomDigit(0, i - 1));
		}

		print(result2);

		System.out.println("case 3 --- length: " + result2.size());
		long endTime = System.currentTimeMillis();
		System.out.println("case 3 --- spend time: " + (endTime - startTime));
	}

	private void print(int[] arr) {
		for (int i = 0, length = arr.length; i < length; i++) {
			System.out.print(arr[i] + " ");
		}
	}

	private void print(List<Integer> list) {
		for (Integer value : list) {
			System.out.print(value + " ");
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值