要求总积分平均值在5~6之间
import java.util.HashMap;
import java.util.Map;
public class IntegralUtil {
/**
* 获取积分
* 获取积分区间为3~10
* 3~ 1%
* 4~ 13%
* 5~ 40%
* 6~ 20%
* 7~ 10%
* 8~ 10%
* 9~ 5%
* 10~1%
*
* @return
*/
public static int getIntegral() {
int temp = (int) (1.0 + Math.random() * (100));
if (temp == 1) {
return 3;
} else if (temp >= 2 && temp <= 14) {
return 4;
} else if (temp >= 15 && temp <= 54) {
return 5;
} else if (temp >= 55 && temp <= 74) {
return 6;
} else if (temp >= 75 && temp <= 84) {
return 7;
} else if (temp >= 85 && temp <= 94) {
return 8;
} else if (temp >= 95 && temp <= 99) {
return 9;
} else if (temp == 100) {
return 10;
}
return -1000000000;
}
public static void main(String[] args) {
int[] size = new int[]{100, 000, 000};
for (int i = 0; i < size.length; i++) {
int count = 0;
int sum = 0;
Map<Integer, Integer> map = new HashMap<>(64);
for (int j = 0; j < size[i]; j++) {
count = count + 1;
int result = getIntegral();
sum = getIntegral() + sum;
if (map.get(result) == null) {
map.put(result, 1);
} else {
map.put(result, map.get(result) + 1);
}
}
System.out.println("样本数量" + count + "," + "积分总和" + sum + ",平均值" + Double.valueOf(sum) / Double.valueOf(count));
for (int key : map.keySet()) {
System.out.println("-----" + key + "出现" + map.get(key) + "次");
}
}
}
}