admin
2019-03-13 69bee82b81626b82b7f39f0e459e4f56b1699b51
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.fanli.random;
 
import com.alibaba.fastjson.JSON;
 
import java.util.ArrayList;
import java.util.List;
 
public class ExtractGift {
 
    /**
     * 生成奖项
     * 
     * @return
     */
    public static RandomGift generateAward() {
        List<RandomGift> giftList = new ArrayList<RandomGift>();
        giftList.add(new RandomGift(1, "\r\n" + 
                "                            na****La,抽中华为手机\r\n" + 
                "                        ", 1));
        giftList.add(new RandomGift(2, "100", 3));
        giftList.add(new RandomGift(3, "50", 30));
        giftList.add(new RandomGift(4, "30", 30));
        giftList.add(new RandomGift(5, "20", 26));
        giftList.add(new RandomGift(6, "10", 10));
 
        long result = (1 + Math.round(Math.random() * (99)));
        System.out.println("result:"+result);
        
        int minRange = 0;
        int maxRange = 0;
        RandomGift returnobj = null;
        
        for (int i = 0; i < giftList.size(); i++) {
            RandomGift obj2 = giftList.get(i);
            int probability = obj2.getProbability();
            
            maxRange = maxRange + probability;
            minRange = 100 - maxRange;
            
            System.out.println("maxRange: "+maxRange);
            System.out.println("minRange: "+minRange);
            
            if (probability != 0) {
                if (result > minRange && result <= maxRange) {
                    returnobj = obj2;
                    break;
                }
            }
        }
        
        return returnobj;
    }
 
 
    public static void main(String[] args) {
        System.out.println(JSON.toJSONString(generateAward()));
    }
 
}