Java实现微信红包随机金额算法
class RandSplitNumUtils {
private static final Random random = new Random();
/**
* 根据总数分割个数及限定区间进行数据随机处理
* 数列浮动阀值为0.95
* @param total - 被分割的总数
* @param splitNum - 分割的个数
* @param min - 单个数字下限
* @param max - 单个数字上线
* @return - 返回符合要求的数字列表
*/
public static List<Integer> genRandList(int total, int splitNum, int min, int max) {
return genRandList(total, splitNum, min, max, 0.95f);
}
/**
* 根据总数分割个数及限定区间进行数据随机处理
* @param total - 被分割的总数
* @param splitNum - 分割的个数
* @param min - 单个数字下限
* @param max - 单个数字上线
* @param thresh - 数列浮动阀值[0.0, 1.0]
* @return
*/
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;
}
// 根据阀值进行数据随机处理
for (int i = 0; i < splitNum - 1; i++) {
int nextIndex = i + 1;
int rangeThis = Math.min(list.get(i) - min, max - list.get(i));
int rangeNext = Math.min(list.get(nextIndex) - min, max - list.get(nextIndex));
int rangeFinal = (int) Math.ceil(thresh * (Math.min(rangeThis, rangeNext) + 1));
int randOfRange = random.nextInt(rangeFinal);
int randRom = list.get(i) > list.get(nextIndex) ? -1 : 1;
list.set(i, list.get(i) + randRom * randOfRange);
list.set(nextIndex, list.get(nextIndex) + randRom * randOfRange * -1);
}
return list;
}
public static void main(String[] args) {
System.out.println(genRandList(10000, 300, 1, 200));
}
}
// 共10000随机分成300份,最小值为1,最大值为200。
genRandList(10000, 300, 1, 200, 0.95f)

genRandList(10000, 300, 1, 200, 0.5f)
