Java实现红包算法

public static void main(String[] args) {


List<Integer> amountList=divdeRedPackage(1,2);
for(Integer amount:amountList)
System.out.println("抢到的金额"+new BigDecimal(amount).divide(new BigDecimal(100)));
}

public static List<Integer> divdeRedPackage(Integer totalAmount,Integer totalPeopleNum)
{
List<Integer> amountList=new ArrayList<Integer>();
Integer restAmount=totalAmount*100;
Integer restPeopleNum=totalPeopleNum;
Random random=new Random();
for(int i=0;i<totalPeopleNum-1;i++)
{


//随机范围:
int amount=random.nextInt(restAmount/restPeopleNum*2-1)+1;
restAmount-=amount;
restPeopleNum --;
amountList.add(amount);
}
amountList.add(restAmount);
return amountList;



}

猜你喜欢

转载自blog.csdn.net/robot_sh/article/details/80167001
今日推荐