根据总中奖概率,奖品数量。建立奖池,在奖池里随机一个。比较简单,也可以增加复杂度。看你的需要了。
[代码][Java]代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public static void main(String[] args) { double baseNum = 0.99 ; String product = "矿泉水,打火机,雨伞,指甲刀" ; String num = "2,2,2,1" ; String[] p = product.split( "," ); String[] n = num.split( "," ); java.util.Random ran = new java.util.Random(); double base = ran.nextDouble(); if (base > baseNum) { System.err.println( "没中奖" ); return ; } List<String> list = new ArrayList<String>(); for ( int i = 0 ; i < p.length; i++) { for ( int j = 0 ; j < Integer.parseInt(n[i]); j++) { list.add(p[i]); } } //TODO 打乱奖池数据 int l = ran.nextInt(list.size()); System.err.println(list.get(l)); } |