public static void main(String[] args) throws IOException {
System.out.println(lottery(100));
}
public static String lottery(int weightSum) {
String[] prizes = new String[] {"一等奖", "二等奖", "三等奖", "未中奖"};
//概率数组
List<Integer> weights = new ArrayList<>();
weights.add(1);
weights.add(5);
weights.add(20);
weights.add(74);
Random random = new Random();
//随机值1-100
int num = random.nextInt(weightSum) + 1;
weights.add(num);
Collections.sort(weights);
int index = weights.indexOf(num);
index = Math.min(index, prizes.length - 1);
System.out.println(weights.toString());
return prizes[index];
}
java实现权重抽奖
最新推荐文章于 2023-08-03 16:56:41 发布