50,542
社区成员
发帖
与我相关
我的任务
分享
import java.util.Random;
import java.util.ArrayList;
public class Test{
public static void main(String[] args){
ArrayList<Double> list = process(10000,100,130,60);
for(int i = 0 ; i < list.size() ; i ++){
System.out.printf("%-3d:%-6.3f%n",i+1,list.get(i));
}
}
private static ArrayList<Double> process(double total,int count,double max,double min){
ArrayList<Double> list = new ArrayList<>();
assert total >= count * min;
assert max > min;
double totalRest = total - count * min;
double current = 0.0;
Random random = new Random();
while(count > 0){
current = random.nextDouble() * (max - min);
if((totalRest - current) / count > max - min){
continue;
}
if(totalRest >= current){
totalRest -= current;
count --;
list.add(current + min);
}else if(totalRest == 0){
count --;
list.add(min);
}else{
count --;
list.add(totalRest + min);
totalRest = 0;
}
}
return list;
}
}