package com.admin.business.utils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
public class RedPacketUtils {
private static final Random random = new Random();
/**
* 根据总数分割个数及限定区间进行数据随机处理
* 数列浮动阀值为0.95
*
* @param totalMoney - 被分割的总数
* @param splitNum - 分割的个数
* @param min - 单个数字下限
* @param max - 单个数字上限
* @return - 返回符合要求的数字列表
*/
public static List<BigDecimal> genRanddomList(BigDecimal totalMoney, Integer splitNum, BigDecimal min, BigDecimal max) {
totalMoney = totalMoney.multiply(new BigDecimal(100));
min = min.multiply(new BigDecimal(100));
max = max.multiply(new BigDecimal(100));
List<Integer> li = genRandList(totalMoney.intValue(), splitNum, min.intValue(), max.intValue(), 0.95f);
List<BigDecimal> randomList = new CopyOnWriteArrayList<>();
for (Integer v : li) {
BigDecimal randomVlue = new BigDecimal(v).divide(new BigDecimal(100));
randomList.add(randomVlue);
}
//打乱红包的排列顺序
Collections.shuffle(randomList);
return randomList;
}
/**
* 根据总数分割个数及限定区间进行数据随机处理
*
* @param total - 被分割的总数
* @param splitNum - 分割的个数
* @param min - 单个数字下限
* @param max - 单个数字上限
* @param thresh - 数列浮动阀值[0.0, 1.0]
*/
public static List<Integer> genRandList(int total, int splitNum, int min, int max, float thresh) {
assert total >= splitNum * min && total <= splitNum * max : "请校验红包参数设置的合理性";
assert thresh >= 0.0f && thresh <= 1.0f;
// 平均分配
int average = total / splitNum;
List<Integer> list = new ArrayList<>(splitNum);
int rest = total - average * splitNum;
for (int i = 0; i < splitNum; i++) {
if (i < rest) {
list.add(average + 1);
} else {
list.add(average);
}
}
// 如果浮动阀值为0则不进行数据随机处理
if (thresh == 0) {
return list;
}
// 根据阀值进行数据随机处理
int randOfRange = 0;
int randRom = 0;
int nextIndex = 0;
int nextValue = 0;
int surplus = 0;//多余
int lack = 0;//缺少
for (int i = 0; i < splitNum - 1; i++) {
nextIndex = i + 1;
int itemThis = list.get(i);
int itemNext = list.get(nextIndex);
boolean isLt = itemThis < itemNext;
int rangeThis = isLt ? max - itemThis : itemThis - min;
int rangeNext = isLt ? itemNext - min : max - itemNext;
int rangeFinal = (int) Math.ceil(thresh * (Math.min(rangeThis, rangeNext) + 100));
randOfRange = random.nextInt(rangeFinal);
randRom = isLt ? 1 : -1;
int iValue = list.get(i) + randRom * randOfRange;
nextValue = list.get(nextIndex) + randRom * randOfRange * -1;
if (iValue > max) {
surplus += (iValue - max);
list.set(i, max);
} else if (iValue < min) {
list.set(i, min);
lack += (min - iValue);
} else {
list.set(i, iValue);
}
list.set(nextIndex, nextValue);
}
if (nextValue > max) {
surplus += (nextValue - max);
list.set(nextIndex, max);
}
if (nextValue < min) {
lack += (min - nextValue);
list.set(nextIndex, min);
}
if (surplus - lack > 0) {//钱发少了 给低于max的凑到max
for (int i = 0; i < list.size(); i++) {
int value = list.get(i);
if (value < max) {
int tmp = max - value;
if (surplus >= tmp) {
surplus -= tmp;
list.set(i, max);
} else {
list.set(i, value + surplus);
return list;
}
}
}
} else if (lack - surplus > 0) {//钱发多了 给超过高于min的人凑到min
for (int i = 0; i < list.size(); i++) {
int value = list.get(i);
if (value > min) {
int tmp = value - min;
if (lack >= tmp) {
lack -= tmp;
list.set(i, min);
} else {
list.set(i, min + tmp - lack);
return list;
}
}
}
}
return list;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
BigDecimal totalMoney = new BigDecimal(1000);
int count = 13;
List<BigDecimal> li = genRanddomList(totalMoney, count, new BigDecimal(1), new BigDecimal(500));
System.out.println("红包总金额:" + totalMoney);
System.out.println("红包个数:" + count);
System.out.println("红包分配列表:" + li);
System.out.println("红包分配数量:" + li.size());
BigDecimal redEnvelopesTotalMoney = li.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("红包分配总金额:" + redEnvelopesTotalMoney);
BigDecimal redEnvelopesMaxMoney = li.stream().reduce(BigDecimal.ZERO, BigDecimal::max);
System.out.println("红包分配最大金额:" + redEnvelopesMaxMoney);
System.out.println("=====================");
}
}
}
红包总金额:1000
红包个数:13
红包分配列表:[23.3, 24.33, 74.28, 26.39, 126.94, 107.91, 68.72, 123.2, 76.81, 76.8, 143.71, 34.53, 93.08]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:143.71
=====================
红包总金额:1000
红包个数:13
红包分配列表:[60.62, 95.64, 71.28, 46.95, 35.82, 77.22, 63.21, 111.14, 87.63, 26.72, 134.93, 74.05, 114.79]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:134.93
=====================
红包总金额:1000
红包个数:13
红包分配列表:[9.32, 22.22, 29.14, 53.46, 85.99, 195.47, 64.39, 82.78, 84.63, 73.95, 106.48, 51.99, 140.18]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:195.47
=====================
红包总金额:1000
红包个数:13
红包分配列表:[82.02, 33.42, 113.32, 87.8, 80.86, 72.12, 110.92, 51.92, 157.67, 67.36, 53.81, 72.18, 16.6]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:157.67
=====================
红包总金额:1000
红包个数:13
红包分配列表:[50.77, 62.7, 70.14, 129.47, 31.39, 79.53, 89.86, 180.18, 61.58, 11.22, 72.49, 72.31, 88.36]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:180.18
=====================
红包总金额:1000
红包个数:13
红包分配列表:[70.32, 24.6, 155.12, 121.07, 76.49, 124.84, 49.65, 9.11, 100.98, 52.97, 37.61, 26.86, 150.38]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:155.12
=====================
红包总金额:1000
红包个数:13
红包分配列表:[131.23, 56.46, 49.23, 13.61, 98.51, 23.41, 88.08, 11.46, 117.93, 56.33, 79.9, 191.84, 82.01]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:191.84
=====================
红包总金额:1000
红包个数:13
红包分配列表:[62.72, 34.5, 170.5, 61.58, 48.61, 178.15, 13.84, 59.2, 37.05, 93.77, 51.97, 166.96, 21.15]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:178.15
=====================
红包总金额:1000
红包个数:13
红包分配列表:[98.12, 57.69, 49.99, 11.95, 11.3, 89.12, 33.42, 72.18, 93.82, 134.5, 63.97, 131.63, 152.31]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:152.31
=====================
红包总金额:1000
红包个数:13
红包分配列表:[161.77, 70.9, 129.04, 42.22, 11.72, 63.65, 38.96, 193.14, 134.35, 32.07, 54.16, 40.83, 27.19]
红包分配数量:13
红包分配总金额:1000.00
红包分配最大金额:193.14