派红包随机分配

import java.util.Random;

public class RedbgTest {
	
	final double minBag = 1;
	int myRand(int left,int right){
		Random rand=new Random();
		int i= rand.nextInt(10000000);
		if(left>=right)return left;
		else return i%(right-left)+left;
	}
	//money 金额 , n要给红包的人数
	void redBag(double money,int n){
		double bag_i,limit;
		for(int i=1;i<n;i++){
			//随机安全上限【1,limit】 取最大可用金额的平均值
			limit = money - minBag*(n- i);
	        int k = ((n-i)/2);
	        if(n -i <= 2)
	            k = n -i;
	        limit = limit/k;
	        
			
			//limit = (money-(n-i)*minBag)/(n-i);
			bag_i = myRand((int)minBag,(int)limit);
			money -=bag_i;
			System.out.println(String.format("第 %d 个红包:%.2f",i,bag_i));
		}
		System.out.println(String.format("第 %d 个红包:%.2f",n,money));
	}
	
	public static void main(String[] args) {
		new RedbgTest().redBag(100,20);
	}

}

 

你可能感兴趣的:(随机)