随机红包
import java.util.Random;
import java.util.Scanner;
public class RedEnvelope {
    public static void main(String[] args) {
        System.out.println("-------------------微信抢红包-----------------");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入红包金额:");
        double total = sc.nextDouble();
        System.out.println("请输入红包个数:");
        int bagCount = sc.nextInt();
        double min = 0.01;//红包最小金额
        Random c = new Random();
        for (int i = 1; i < bagCount ; i++) {
            //本次红包可用最大金额 = 可分配金额 - (红包个数 - 已发出的红包个数)*红包的最小金额
            double max = total - (bagCount - i) * min;
            double bound = max - min;//随机金额范围,本次红包可用最大金额 - 红包的最小金额
            double safe = (double) c.nextInt((int) (bound * 100)) / 100;
            double money = safe + min;//即便随机数为0时再加红包的最小金额,最终红包金额也不会为0
            total = total - money;//可分配金额=原金额-已发金额
            System.out.println("第"+i+"个红包:"+String.format("%.2f",money)+"元");
        }
        System.out.println("第"+bagCount+"个红包:"+String.format("%.2f",total)+"元");
        sc.close();
    }
}
-------------------微信抢红包-----------------
请输入红包金额:
20
请输入红包个数:
10
第1个红包:4.38元
第2个红包:10.43元
第3个红包:1.26元
第4个红包:1.27元
第5个红包:1.90元
第6个红包:0.22元
第7个红包:0.09元
第8个红包:0.01元
第9个红包:0.31元
第10个红包:0.13元
Process finished with exit code 0