红包随机算法

public class RedPackUtils {

public static List<Integer> getRandomRedList(int sum, int count) {
	if (sum <= 0 || count <= 0 || count > sum) {
		throw new RunException(ECode.error, "生成随机金额列表方法调用参数不正确 sum:" + sum + ",count:" + count);
	}
	List<Integer> list = new ArrayList<>();
	for (; count >= 1; count--) {
		int money = getRandomMoney(sum, count);
		sum = sum - money;
		list.add(money);
	}
	if (sum != 0 || count != 0) {
		throw new RunException(ECode.error,
				"生成随机金额列表方法调用失败 sum:" + sum + ",count:" + count + ",list.size():" + list.size());
	}
	return list;
}


public static int getRandomMoney(int sum, int count) {
	if (count == 1) {
		return sum;
	}
	Random r = new Random();
	int min = 1;
	int max = sum / count * 2;
	int money = r.nextInt(max);
	money = money <= min ? 100 : money;
	return money;
}


public static List<Integer> getRandomRedList(int totalSum, int totalCount, int min, int max) {
	List<Integer> randomList = getRandomRedList(totalSum, totalCount);
	int sum = 0;
	for (int i = 0; i < randomList.size(); i++) {
		System.out.println("第" + i + "个:" + randomList.get(i));
		sum = sum + randomList.get(i);
	}
	System.out.println("总额:" + sum);
	Collections.reverse(randomList);
	for (int i = 0; i < totalCount; i++) {
		Integer p = randomList.get(i);
		if (p.intValue() < min) {
			int chae = min - p;
			addToMin(min, chae, randomList);
			randomList.set(i, min);
		}
	}
	Collections.sort(randomList);
	for (int i = 0; i < totalCount; i++) {
		Integer p = randomList.get(i);
		if (p.intValue() > max) {
			int chae = p - max;
			subtractToMax(max, chae, randomList);
			randomList.set(i, max);
		}
	}
	Collections.shuffle(randomList);
	sum = 0;
	for (int i = 0; i < randomList.size(); i++) {
		System.out.println("第" + i + "个:" + randomList.get(i));
		sum = sum + randomList.get(i);
	}
	System.out.println("总额:" + sum);
	return randomList;
}

private static void subtractToMax(int max, int chae, List<Integer> randomList) {
	for (int j = 0; j < randomList.size() && chae > 0;) {
		Integer current = randomList.get(j);
		if (current < max) {
			randomList.set(j, current + 1);
			chae--;
		}
		j++;
		if (chae > 0 && j == randomList.size()) {
			j = 0;
		}
	}

}

private static void addToMin(int min, int chae, List<Integer> randomList) {
	// 排续一下
	for (int j = 0; j < randomList.size() && chae > 0;) {
		Integer current = randomList.get(j);
		if (current > min) {
			randomList.set(j, current - 1);
			chae--;
		}
		j++;
		if (chae > 0 && j == randomList.size()) {
			j = 0;
		}
	}

}

public static void main(String[] args) {
	getRandomRedList(10000000, 500, 100, 20000);
}

}

转载于:https://my.oschina.net/u/1379199/blog/1608707

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值