Java Red envelope algorithm, limited minimum and maximum critical value

Source: Internet
Author: User
Package com.cdjj.test;


public class Demo {
public static void Main (string[] args) {
Randompackage ();
}
public static void Randompackage () {
Double total_money=2;
int total_people=20;
Double min_money=5; Each person can receive at least 0.05 yuan,
Double max_money=20; Each person can receive at least 0.05 yuan,
Double lingjiezhi= (total_money/total_people) *100;
Double allresult=0;
for (int i = 0; I <total_people; i++) {
Protection value
Double procte= (total_people-i-1) *min_money/100;
Discretionary amount
Double zpje=total_money-procte;
if (Zpje*100<max_money) {
max_money=zpje*100;
}
Double Result=restround (Min_money, Max_money, I, Zpje, total_people-1);
Total_money=total_money-result;
Allresult+=result;
System.out.format ("Red envelope%.2f, balance%.2f \ \", Result,total_money);
}
System.out.format ("Total Amount%.2f", allresult);
}
public static double Restround (double min,double max,int i,double money,int count) {
Double redpac=0;
if (i==19) {
Redpac=money;
}else{
Redpac= (Math.random () * (max-min) +min)/100;
}
return REDPAC;
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.